xref: /linux/fs/bcachefs/fs-io.c (revision dcfc593f7b3a35e340f0cefa3281a3285ddb48e8)
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 {
75f8f30863SKent Overstreet 	struct completion		done;
761c6fdbd8SKent Overstreet 	struct kiocb			*req;
77ed484030SKent Overstreet 	struct mm_struct		*mm;
781c6fdbd8SKent Overstreet 	unsigned			loop:1,
791c6fdbd8SKent Overstreet 					sync:1,
801c6fdbd8SKent Overstreet 					free_iov:1;
811c6fdbd8SKent Overstreet 	struct quota_res		quota_res;
82042a1f26SKent Overstreet 	u64				written;
831c6fdbd8SKent Overstreet 
841c6fdbd8SKent Overstreet 	struct iov_iter			iter;
851c6fdbd8SKent Overstreet 	struct iovec			inline_vecs[2];
861c6fdbd8SKent Overstreet 
871c6fdbd8SKent Overstreet 	/* must be last: */
889a3df993SKent Overstreet 	struct bch_write_op		op;
891c6fdbd8SKent Overstreet };
901c6fdbd8SKent Overstreet 
911c6fdbd8SKent Overstreet struct dio_read {
921c6fdbd8SKent Overstreet 	struct closure			cl;
931c6fdbd8SKent Overstreet 	struct kiocb			*req;
941c6fdbd8SKent Overstreet 	long				ret;
95b4725cc1SKent Overstreet 	bool				should_dirty;
961c6fdbd8SKent Overstreet 	struct bch_read_bio		rbio;
971c6fdbd8SKent Overstreet };
981c6fdbd8SKent Overstreet 
991c6fdbd8SKent Overstreet /* pagecache_block must be held */
100a023127aSKent Overstreet static noinline int write_invalidate_inode_pages_range(struct address_space *mapping,
1011c6fdbd8SKent Overstreet 					      loff_t start, loff_t end)
1021c6fdbd8SKent Overstreet {
1031c6fdbd8SKent Overstreet 	int ret;
1041c6fdbd8SKent Overstreet 
1051c6fdbd8SKent Overstreet 	/*
1061c6fdbd8SKent Overstreet 	 * XXX: the way this is currently implemented, we can spin if a process
1071c6fdbd8SKent Overstreet 	 * is continually redirtying a specific page
1081c6fdbd8SKent Overstreet 	 */
1091c6fdbd8SKent Overstreet 	do {
1101c6fdbd8SKent Overstreet 		if (!mapping->nrpages)
1111c6fdbd8SKent Overstreet 			return 0;
1121c6fdbd8SKent Overstreet 
1131c6fdbd8SKent Overstreet 		ret = filemap_write_and_wait_range(mapping, start, end);
1141c6fdbd8SKent Overstreet 		if (ret)
1151c6fdbd8SKent Overstreet 			break;
1161c6fdbd8SKent Overstreet 
1171c6fdbd8SKent Overstreet 		if (!mapping->nrpages)
1181c6fdbd8SKent Overstreet 			return 0;
1191c6fdbd8SKent Overstreet 
1201c6fdbd8SKent Overstreet 		ret = invalidate_inode_pages2_range(mapping,
1211c6fdbd8SKent Overstreet 				start >> PAGE_SHIFT,
1221c6fdbd8SKent Overstreet 				end >> PAGE_SHIFT);
1231c6fdbd8SKent Overstreet 	} while (ret == -EBUSY);
1241c6fdbd8SKent Overstreet 
1251c6fdbd8SKent Overstreet 	return ret;
1261c6fdbd8SKent Overstreet }
1271c6fdbd8SKent Overstreet 
1281c6fdbd8SKent Overstreet /* quotas */
1291c6fdbd8SKent Overstreet 
1301c6fdbd8SKent Overstreet #ifdef CONFIG_BCACHEFS_QUOTA
1311c6fdbd8SKent Overstreet 
1321c6fdbd8SKent Overstreet static void bch2_quota_reservation_put(struct bch_fs *c,
1331c6fdbd8SKent Overstreet 				       struct bch_inode_info *inode,
1341c6fdbd8SKent Overstreet 				       struct quota_res *res)
1351c6fdbd8SKent Overstreet {
1361c6fdbd8SKent Overstreet 	if (!res->sectors)
1371c6fdbd8SKent Overstreet 		return;
1381c6fdbd8SKent Overstreet 
1391c6fdbd8SKent Overstreet 	mutex_lock(&inode->ei_quota_lock);
1401c6fdbd8SKent Overstreet 	BUG_ON(res->sectors > inode->ei_quota_reserved);
1411c6fdbd8SKent Overstreet 
1421c6fdbd8SKent Overstreet 	bch2_quota_acct(c, inode->ei_qid, Q_SPC,
14326609b61SKent Overstreet 			-((s64) res->sectors), KEY_TYPE_QUOTA_PREALLOC);
1441c6fdbd8SKent Overstreet 	inode->ei_quota_reserved -= res->sectors;
1451c6fdbd8SKent Overstreet 	mutex_unlock(&inode->ei_quota_lock);
1461c6fdbd8SKent Overstreet 
1471c6fdbd8SKent Overstreet 	res->sectors = 0;
1481c6fdbd8SKent Overstreet }
1491c6fdbd8SKent Overstreet 
1501c6fdbd8SKent Overstreet static int bch2_quota_reservation_add(struct bch_fs *c,
1511c6fdbd8SKent Overstreet 				      struct bch_inode_info *inode,
1521c6fdbd8SKent Overstreet 				      struct quota_res *res,
1531c6fdbd8SKent Overstreet 				      unsigned sectors,
1541c6fdbd8SKent Overstreet 				      bool check_enospc)
1551c6fdbd8SKent Overstreet {
1561c6fdbd8SKent Overstreet 	int ret;
1571c6fdbd8SKent Overstreet 
1581c6fdbd8SKent Overstreet 	mutex_lock(&inode->ei_quota_lock);
1591c6fdbd8SKent Overstreet 	ret = bch2_quota_acct(c, inode->ei_qid, Q_SPC, sectors,
16026609b61SKent Overstreet 			      check_enospc ? KEY_TYPE_QUOTA_PREALLOC : KEY_TYPE_QUOTA_NOCHECK);
1611c6fdbd8SKent Overstreet 	if (likely(!ret)) {
1621c6fdbd8SKent Overstreet 		inode->ei_quota_reserved += sectors;
1631c6fdbd8SKent Overstreet 		res->sectors += sectors;
1641c6fdbd8SKent Overstreet 	}
1651c6fdbd8SKent Overstreet 	mutex_unlock(&inode->ei_quota_lock);
1661c6fdbd8SKent Overstreet 
1671c6fdbd8SKent Overstreet 	return ret;
1681c6fdbd8SKent Overstreet }
1691c6fdbd8SKent Overstreet 
1701c6fdbd8SKent Overstreet #else
1711c6fdbd8SKent Overstreet 
1721c6fdbd8SKent Overstreet static void bch2_quota_reservation_put(struct bch_fs *c,
1731c6fdbd8SKent Overstreet 				       struct bch_inode_info *inode,
1741c6fdbd8SKent Overstreet 				       struct quota_res *res)
1751c6fdbd8SKent Overstreet {
1761c6fdbd8SKent Overstreet }
1771c6fdbd8SKent Overstreet 
1781c6fdbd8SKent Overstreet static int bch2_quota_reservation_add(struct bch_fs *c,
1791c6fdbd8SKent Overstreet 				      struct bch_inode_info *inode,
1801c6fdbd8SKent Overstreet 				      struct quota_res *res,
1811c6fdbd8SKent Overstreet 				      unsigned sectors,
1821c6fdbd8SKent Overstreet 				      bool check_enospc)
1831c6fdbd8SKent Overstreet {
1841c6fdbd8SKent Overstreet 	return 0;
1851c6fdbd8SKent Overstreet }
1861c6fdbd8SKent Overstreet 
1871c6fdbd8SKent Overstreet #endif
1881c6fdbd8SKent Overstreet 
1891c6fdbd8SKent Overstreet /* i_size updates: */
1901c6fdbd8SKent Overstreet 
1912ea90048SKent Overstreet struct inode_new_size {
1922ea90048SKent Overstreet 	loff_t		new_size;
1932ea90048SKent Overstreet 	u64		now;
1942ea90048SKent Overstreet 	unsigned	fields;
1952ea90048SKent Overstreet };
1962ea90048SKent Overstreet 
1971c6fdbd8SKent Overstreet static int inode_set_size(struct bch_inode_info *inode,
1981c6fdbd8SKent Overstreet 			  struct bch_inode_unpacked *bi,
1991c6fdbd8SKent Overstreet 			  void *p)
2001c6fdbd8SKent Overstreet {
2012ea90048SKent Overstreet 	struct inode_new_size *s = p;
2021c6fdbd8SKent Overstreet 
2032ea90048SKent Overstreet 	bi->bi_size = s->new_size;
2042ea90048SKent Overstreet 	if (s->fields & ATTR_ATIME)
2052ea90048SKent Overstreet 		bi->bi_atime = s->now;
2062ea90048SKent Overstreet 	if (s->fields & ATTR_MTIME)
2072ea90048SKent Overstreet 		bi->bi_mtime = s->now;
2082ea90048SKent Overstreet 	if (s->fields & ATTR_CTIME)
2092ea90048SKent Overstreet 		bi->bi_ctime = s->now;
2101c6fdbd8SKent Overstreet 
2111c6fdbd8SKent Overstreet 	return 0;
2121c6fdbd8SKent Overstreet }
2131c6fdbd8SKent Overstreet 
21476426098SKent Overstreet int __must_check bch2_write_inode_size(struct bch_fs *c,
2151c6fdbd8SKent Overstreet 				       struct bch_inode_info *inode,
2162ea90048SKent Overstreet 				       loff_t new_size, unsigned fields)
2171c6fdbd8SKent Overstreet {
2182ea90048SKent Overstreet 	struct inode_new_size s = {
2192ea90048SKent Overstreet 		.new_size	= new_size,
2202ea90048SKent Overstreet 		.now		= bch2_current_time(c),
2212ea90048SKent Overstreet 		.fields		= fields,
2222ea90048SKent Overstreet 	};
2232ea90048SKent Overstreet 
2242ea90048SKent Overstreet 	return bch2_write_inode(c, inode, inode_set_size, &s, fields);
2251c6fdbd8SKent Overstreet }
2261c6fdbd8SKent Overstreet 
2271c6fdbd8SKent Overstreet static void i_sectors_acct(struct bch_fs *c, struct bch_inode_info *inode,
228190fa7afSKent Overstreet 			   struct quota_res *quota_res, s64 sectors)
2291c6fdbd8SKent Overstreet {
230190fa7afSKent Overstreet 	if (!sectors)
231190fa7afSKent Overstreet 		return;
232190fa7afSKent Overstreet 
2331c6fdbd8SKent Overstreet 	mutex_lock(&inode->ei_quota_lock);
234b44a66a6SKent Overstreet 	BUG_ON((s64) inode->v.i_blocks + sectors < 0);
235b44a66a6SKent Overstreet 	inode->v.i_blocks += sectors;
236b44a66a6SKent Overstreet 
2371c6fdbd8SKent Overstreet #ifdef CONFIG_BCACHEFS_QUOTA
2381c6fdbd8SKent Overstreet 	if (quota_res && sectors > 0) {
2391c6fdbd8SKent Overstreet 		BUG_ON(sectors > quota_res->sectors);
2401c6fdbd8SKent Overstreet 		BUG_ON(sectors > inode->ei_quota_reserved);
2411c6fdbd8SKent Overstreet 
2421c6fdbd8SKent Overstreet 		quota_res->sectors -= sectors;
2431c6fdbd8SKent Overstreet 		inode->ei_quota_reserved -= sectors;
2441c6fdbd8SKent Overstreet 	} else {
24526609b61SKent Overstreet 		bch2_quota_acct(c, inode->ei_qid, Q_SPC, sectors, KEY_TYPE_QUOTA_WARN);
2461c6fdbd8SKent Overstreet 	}
2471c6fdbd8SKent Overstreet #endif
2481c6fdbd8SKent Overstreet 	mutex_unlock(&inode->ei_quota_lock);
2491c6fdbd8SKent Overstreet }
2501c6fdbd8SKent Overstreet 
2511c6fdbd8SKent Overstreet /* page state: */
2521c6fdbd8SKent Overstreet 
2531c6fdbd8SKent Overstreet /* stored in page->private: */
2541c6fdbd8SKent Overstreet 
255f57a6a5dSKent Overstreet struct bch_page_sector {
256b44a66a6SKent Overstreet 	/* Uncompressed, fully allocated replicas (or on disk reservation): */
257b44a66a6SKent Overstreet 	unsigned		nr_replicas:4;
258f81b648dSKent Overstreet 
259b44a66a6SKent Overstreet 	/* Owns PAGE_SECTORS * replicas_reserved sized in memory reservation: */
260b44a66a6SKent Overstreet 	unsigned		replicas_reserved:4;
2611c6fdbd8SKent Overstreet 
262f57a6a5dSKent Overstreet 	/* i_sectors: */
263f57a6a5dSKent Overstreet 	enum {
264f57a6a5dSKent Overstreet 		SECTOR_UNALLOCATED,
2652ba5d38bSKent Overstreet 		SECTOR_RESERVED,
266f57a6a5dSKent Overstreet 		SECTOR_DIRTY,
267b44a66a6SKent Overstreet 		SECTOR_DIRTY_RESERVED,
268f57a6a5dSKent Overstreet 		SECTOR_ALLOCATED,
269b44a66a6SKent Overstreet 	}			state:8;
2701c6fdbd8SKent Overstreet };
2711c6fdbd8SKent Overstreet 
272f57a6a5dSKent Overstreet struct bch_page_state {
2733826ee0bSKent Overstreet 	spinlock_t		lock;
2747f5e31e1SKent Overstreet 	atomic_t		write_count;
275e6ec361fSKent Overstreet 	bool			uptodate;
276f57a6a5dSKent Overstreet 	struct bch_page_sector	s[PAGE_SECTORS];
277f57a6a5dSKent Overstreet };
278f57a6a5dSKent Overstreet 
279f57a6a5dSKent Overstreet static inline struct bch_page_state *__bch2_page_state(struct page *page)
2801c6fdbd8SKent Overstreet {
281f57a6a5dSKent Overstreet 	return page_has_private(page)
282f57a6a5dSKent Overstreet 		? (struct bch_page_state *) page_private(page)
283f57a6a5dSKent Overstreet 		: NULL;
284f57a6a5dSKent Overstreet }
2851c6fdbd8SKent Overstreet 
286f57a6a5dSKent Overstreet static inline struct bch_page_state *bch2_page_state(struct page *page)
287f57a6a5dSKent Overstreet {
288e1036a2aSKent Overstreet 	EBUG_ON(!PageLocked(page));
2891c6fdbd8SKent Overstreet 
290f57a6a5dSKent Overstreet 	return __bch2_page_state(page);
291f57a6a5dSKent Overstreet }
292f57a6a5dSKent Overstreet 
293f57a6a5dSKent Overstreet /* for newly allocated pages: */
294f57a6a5dSKent Overstreet static void __bch2_page_state_release(struct page *page)
295f57a6a5dSKent Overstreet {
29600276f9fSMatthew Wilcox (Oracle) 	kfree(detach_page_private(page));
297f57a6a5dSKent Overstreet }
298f57a6a5dSKent Overstreet 
299f57a6a5dSKent Overstreet static void bch2_page_state_release(struct page *page)
300f57a6a5dSKent Overstreet {
30100276f9fSMatthew Wilcox (Oracle) 	EBUG_ON(!PageLocked(page));
30200276f9fSMatthew Wilcox (Oracle) 	__bch2_page_state_release(page);
303f57a6a5dSKent Overstreet }
304f57a6a5dSKent Overstreet 
305f57a6a5dSKent Overstreet /* for newly allocated pages: */
306f57a6a5dSKent Overstreet static struct bch_page_state *__bch2_page_state_create(struct page *page,
307f57a6a5dSKent Overstreet 						       gfp_t gfp)
308f57a6a5dSKent Overstreet {
309f57a6a5dSKent Overstreet 	struct bch_page_state *s;
310f57a6a5dSKent Overstreet 
311f57a6a5dSKent Overstreet 	s = kzalloc(sizeof(*s), GFP_NOFS|gfp);
312f57a6a5dSKent Overstreet 	if (!s)
313f57a6a5dSKent Overstreet 		return NULL;
314f57a6a5dSKent Overstreet 
3153826ee0bSKent Overstreet 	spin_lock_init(&s->lock);
31600276f9fSMatthew Wilcox (Oracle) 	attach_page_private(page, s);
3171c6fdbd8SKent Overstreet 	return s;
3181c6fdbd8SKent Overstreet }
3191c6fdbd8SKent Overstreet 
320f57a6a5dSKent Overstreet static struct bch_page_state *bch2_page_state_create(struct page *page,
321f57a6a5dSKent Overstreet 						     gfp_t gfp)
322f57a6a5dSKent Overstreet {
323f57a6a5dSKent Overstreet 	return bch2_page_state(page) ?: __bch2_page_state_create(page, gfp);
324f57a6a5dSKent Overstreet }
325f57a6a5dSKent Overstreet 
326b44a66a6SKent Overstreet static unsigned bkey_to_sector_state(const struct bkey *k)
327b44a66a6SKent Overstreet {
328b44a66a6SKent Overstreet 	if (k->type == KEY_TYPE_reservation)
329b44a66a6SKent Overstreet 		return SECTOR_RESERVED;
330b44a66a6SKent Overstreet 	if (bkey_extent_is_allocation(k))
331b44a66a6SKent Overstreet 		return SECTOR_ALLOCATED;
332b44a66a6SKent Overstreet 	return SECTOR_UNALLOCATED;
333b44a66a6SKent Overstreet }
334b44a66a6SKent Overstreet 
335e6ec361fSKent Overstreet static void __bch2_page_state_set(struct page *page,
336e6ec361fSKent Overstreet 				  unsigned pg_offset, unsigned pg_len,
337e6ec361fSKent Overstreet 				  unsigned nr_ptrs, unsigned state)
338e6ec361fSKent Overstreet {
339e6ec361fSKent Overstreet 	struct bch_page_state *s = bch2_page_state_create(page, __GFP_NOFAIL);
340e6ec361fSKent Overstreet 	unsigned i;
341e6ec361fSKent Overstreet 
342e6ec361fSKent Overstreet 	BUG_ON(pg_offset >= PAGE_SECTORS);
343e6ec361fSKent Overstreet 	BUG_ON(pg_offset + pg_len > PAGE_SECTORS);
344e6ec361fSKent Overstreet 
345e6ec361fSKent Overstreet 	spin_lock(&s->lock);
346e6ec361fSKent Overstreet 
347e6ec361fSKent Overstreet 	for (i = pg_offset; i < pg_offset + pg_len; i++) {
348e6ec361fSKent Overstreet 		s->s[i].nr_replicas = nr_ptrs;
349e6ec361fSKent Overstreet 		s->s[i].state = state;
350e6ec361fSKent Overstreet 	}
351e6ec361fSKent Overstreet 
352e6ec361fSKent Overstreet 	if (i == PAGE_SECTORS)
353e6ec361fSKent Overstreet 		s->uptodate = true;
354e6ec361fSKent Overstreet 
355e6ec361fSKent Overstreet 	spin_unlock(&s->lock);
356e6ec361fSKent Overstreet }
357e6ec361fSKent Overstreet 
358e6ec361fSKent Overstreet static int bch2_page_state_set(struct bch_fs *c, subvol_inum inum,
359e6ec361fSKent Overstreet 			       struct page **pages, unsigned nr_pages)
360e6ec361fSKent Overstreet {
361e6ec361fSKent Overstreet 	struct btree_trans trans;
362e6ec361fSKent Overstreet 	struct btree_iter iter;
363e6ec361fSKent Overstreet 	struct bkey_s_c k;
364e6ec361fSKent Overstreet 	u64 offset = pages[0]->index << PAGE_SECTORS_SHIFT;
365e6ec361fSKent Overstreet 	unsigned pg_idx = 0;
366e6ec361fSKent Overstreet 	u32 snapshot;
367e6ec361fSKent Overstreet 	int ret;
368e6ec361fSKent Overstreet 
369e6ec361fSKent Overstreet 	bch2_trans_init(&trans, c, 0, 0);
370e6ec361fSKent Overstreet retry:
371e6ec361fSKent Overstreet 	bch2_trans_begin(&trans);
372e6ec361fSKent Overstreet 
373e6ec361fSKent Overstreet 	ret = bch2_subvolume_get_snapshot(&trans, inum.subvol, &snapshot);
374e6ec361fSKent Overstreet 	if (ret)
375e6ec361fSKent Overstreet 		goto err;
376e6ec361fSKent Overstreet 
377e6ec361fSKent Overstreet 	for_each_btree_key_norestart(&trans, iter, BTREE_ID_extents,
378e6ec361fSKent Overstreet 			   SPOS(inum.inum, offset, snapshot),
379e6ec361fSKent Overstreet 			   BTREE_ITER_SLOTS, k, ret) {
380e6ec361fSKent Overstreet 		unsigned nr_ptrs = bch2_bkey_nr_ptrs_fully_allocated(k);
381e6ec361fSKent Overstreet 		unsigned state = bkey_to_sector_state(k.k);
382e6ec361fSKent Overstreet 
383e6ec361fSKent Overstreet 		while (pg_idx < nr_pages) {
384e6ec361fSKent Overstreet 			struct page *page = pages[pg_idx];
385e6ec361fSKent Overstreet 			u64 pg_start = page->index << PAGE_SECTORS_SHIFT;
386e6ec361fSKent Overstreet 			u64 pg_end = (page->index + 1) << PAGE_SECTORS_SHIFT;
387e6ec361fSKent Overstreet 			unsigned pg_offset = max(bkey_start_offset(k.k), pg_start) - pg_start;
388e6ec361fSKent Overstreet 			unsigned pg_len = min(k.k->p.offset, pg_end) - pg_offset - pg_start;
389e6ec361fSKent Overstreet 
390e6ec361fSKent Overstreet 			BUG_ON(k.k->p.offset < pg_start);
391e6ec361fSKent Overstreet 			BUG_ON(bkey_start_offset(k.k) > pg_end);
392e6ec361fSKent Overstreet 
393e6ec361fSKent Overstreet 			if (!bch2_page_state_create(page, __GFP_NOFAIL)->uptodate)
394e6ec361fSKent Overstreet 				__bch2_page_state_set(page, pg_offset, pg_len, nr_ptrs, state);
395e6ec361fSKent Overstreet 
396e6ec361fSKent Overstreet 			if (k.k->p.offset < pg_end)
397e6ec361fSKent Overstreet 				break;
398e6ec361fSKent Overstreet 			pg_idx++;
399e6ec361fSKent Overstreet 		}
400e6ec361fSKent Overstreet 
401e6ec361fSKent Overstreet 		if (pg_idx == nr_pages)
402e6ec361fSKent Overstreet 			break;
403e6ec361fSKent Overstreet 	}
404e6ec361fSKent Overstreet 
405e6ec361fSKent Overstreet 	offset = iter.pos.offset;
406e6ec361fSKent Overstreet 	bch2_trans_iter_exit(&trans, &iter);
407e6ec361fSKent Overstreet err:
408e6ec361fSKent Overstreet 	if (ret == -EINTR)
409e6ec361fSKent Overstreet 		goto retry;
410e6ec361fSKent Overstreet 	bch2_trans_exit(&trans);
411e6ec361fSKent Overstreet 
412e6ec361fSKent Overstreet 	return ret;
413e6ec361fSKent Overstreet }
414e6ec361fSKent Overstreet 
415b44a66a6SKent Overstreet static void bch2_bio_page_state_set(struct bio *bio, struct bkey_s_c k)
416b44a66a6SKent Overstreet {
417b44a66a6SKent Overstreet 	struct bvec_iter iter;
418b44a66a6SKent Overstreet 	struct bio_vec bv;
419b44a66a6SKent Overstreet 	unsigned nr_ptrs = k.k->type == KEY_TYPE_reflink_v
420b44a66a6SKent Overstreet 		? 0 : bch2_bkey_nr_ptrs_fully_allocated(k);
421b44a66a6SKent Overstreet 	unsigned state = bkey_to_sector_state(k.k);
422b44a66a6SKent Overstreet 
423e6ec361fSKent Overstreet 	bio_for_each_segment(bv, bio, iter)
424e6ec361fSKent Overstreet 		__bch2_page_state_set(bv.bv_page, bv.bv_offset >> 9,
425e6ec361fSKent Overstreet 				      bv.bv_len >> 9, nr_ptrs, state);
426b44a66a6SKent Overstreet }
427b44a66a6SKent Overstreet 
428*dcfc593fSKent Overstreet static void mark_pagecache_unallocated(struct bch_inode_info *inode,
429*dcfc593fSKent Overstreet 				       u64 start, u64 end)
430*dcfc593fSKent Overstreet {
431*dcfc593fSKent Overstreet 	pgoff_t index = start >> PAGE_SECTORS_SHIFT;
432*dcfc593fSKent Overstreet 	pgoff_t end_index = (end - 1) >> PAGE_SECTORS_SHIFT;
433*dcfc593fSKent Overstreet 	struct folio_batch fbatch;
434*dcfc593fSKent Overstreet 	unsigned i, j;
435*dcfc593fSKent Overstreet 
436*dcfc593fSKent Overstreet 	if (end <= start)
437*dcfc593fSKent Overstreet 		return;
438*dcfc593fSKent Overstreet 
439*dcfc593fSKent Overstreet 	folio_batch_init(&fbatch);
440*dcfc593fSKent Overstreet 
441*dcfc593fSKent Overstreet 	while (filemap_get_folios(inode->v.i_mapping,
442*dcfc593fSKent Overstreet 				  &index, end_index, &fbatch)) {
443*dcfc593fSKent Overstreet 		for (i = 0; i < folio_batch_count(&fbatch); i++) {
444*dcfc593fSKent Overstreet 			struct folio *folio = fbatch.folios[i];
445*dcfc593fSKent Overstreet 			u64 pg_start = folio->index << PAGE_SECTORS_SHIFT;
446*dcfc593fSKent Overstreet 			u64 pg_end = (folio->index + 1) << PAGE_SECTORS_SHIFT;
447*dcfc593fSKent Overstreet 			unsigned pg_offset = max(start, pg_start) - pg_start;
448*dcfc593fSKent Overstreet 			unsigned pg_len = min(end, pg_end) - pg_offset - pg_start;
449*dcfc593fSKent Overstreet 			struct bch_page_state *s;
450*dcfc593fSKent Overstreet 
451*dcfc593fSKent Overstreet 			BUG_ON(end <= pg_start);
452*dcfc593fSKent Overstreet 			BUG_ON(pg_offset >= PAGE_SECTORS);
453*dcfc593fSKent Overstreet 			BUG_ON(pg_offset + pg_len > PAGE_SECTORS);
454*dcfc593fSKent Overstreet 
455*dcfc593fSKent Overstreet 			folio_lock(folio);
456*dcfc593fSKent Overstreet 			s = bch2_page_state(&folio->page);
457*dcfc593fSKent Overstreet 
458*dcfc593fSKent Overstreet 			if (s) {
459*dcfc593fSKent Overstreet 				spin_lock(&s->lock);
460*dcfc593fSKent Overstreet 				for (j = pg_offset; j < pg_offset + pg_len; j++)
461*dcfc593fSKent Overstreet 					s->s[j].nr_replicas = 0;
462*dcfc593fSKent Overstreet 				spin_unlock(&s->lock);
463*dcfc593fSKent Overstreet 			}
464*dcfc593fSKent Overstreet 
465*dcfc593fSKent Overstreet 			folio_unlock(folio);
466*dcfc593fSKent Overstreet 		}
467*dcfc593fSKent Overstreet 		folio_batch_release(&fbatch);
468*dcfc593fSKent Overstreet 		cond_resched();
469*dcfc593fSKent Overstreet 	}
470*dcfc593fSKent Overstreet }
471*dcfc593fSKent Overstreet 
472*dcfc593fSKent Overstreet static void mark_pagecache_reserved(struct bch_inode_info *inode,
473*dcfc593fSKent Overstreet 				    u64 start, u64 end)
474*dcfc593fSKent Overstreet {
475*dcfc593fSKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
476*dcfc593fSKent Overstreet 	pgoff_t index = start >> PAGE_SECTORS_SHIFT;
477*dcfc593fSKent Overstreet 	pgoff_t end_index = (end - 1) >> PAGE_SECTORS_SHIFT;
478*dcfc593fSKent Overstreet 	struct folio_batch fbatch;
479*dcfc593fSKent Overstreet 	s64 i_sectors_delta = 0;
480*dcfc593fSKent Overstreet 	unsigned i, j;
481*dcfc593fSKent Overstreet 
482*dcfc593fSKent Overstreet 	if (end <= start)
483*dcfc593fSKent Overstreet 		return;
484*dcfc593fSKent Overstreet 
485*dcfc593fSKent Overstreet 	folio_batch_init(&fbatch);
486*dcfc593fSKent Overstreet 
487*dcfc593fSKent Overstreet 	while (filemap_get_folios(inode->v.i_mapping,
488*dcfc593fSKent Overstreet 				  &index, end_index, &fbatch)) {
489*dcfc593fSKent Overstreet 		for (i = 0; i < folio_batch_count(&fbatch); i++) {
490*dcfc593fSKent Overstreet 			struct folio *folio = fbatch.folios[i];
491*dcfc593fSKent Overstreet 			u64 pg_start = folio->index << PAGE_SECTORS_SHIFT;
492*dcfc593fSKent Overstreet 			u64 pg_end = (folio->index + 1) << PAGE_SECTORS_SHIFT;
493*dcfc593fSKent Overstreet 			unsigned pg_offset = max(start, pg_start) - pg_start;
494*dcfc593fSKent Overstreet 			unsigned pg_len = min(end, pg_end) - pg_offset - pg_start;
495*dcfc593fSKent Overstreet 			struct bch_page_state *s;
496*dcfc593fSKent Overstreet 
497*dcfc593fSKent Overstreet 			BUG_ON(end <= pg_start);
498*dcfc593fSKent Overstreet 			BUG_ON(pg_offset >= PAGE_SECTORS);
499*dcfc593fSKent Overstreet 			BUG_ON(pg_offset + pg_len > PAGE_SECTORS);
500*dcfc593fSKent Overstreet 
501*dcfc593fSKent Overstreet 			folio_lock(folio);
502*dcfc593fSKent Overstreet 			s = bch2_page_state(&folio->page);
503*dcfc593fSKent Overstreet 
504*dcfc593fSKent Overstreet 			if (s) {
505*dcfc593fSKent Overstreet 				spin_lock(&s->lock);
506*dcfc593fSKent Overstreet 				for (j = pg_offset; j < pg_offset + pg_len; j++)
507*dcfc593fSKent Overstreet 					switch (s->s[j].state) {
508*dcfc593fSKent Overstreet 					case SECTOR_UNALLOCATED:
509*dcfc593fSKent Overstreet 						s->s[j].state = SECTOR_RESERVED;
510*dcfc593fSKent Overstreet 						break;
511*dcfc593fSKent Overstreet 					case SECTOR_DIRTY:
512*dcfc593fSKent Overstreet 						s->s[j].state = SECTOR_DIRTY_RESERVED;
513*dcfc593fSKent Overstreet 						i_sectors_delta--;
514*dcfc593fSKent Overstreet 						break;
515*dcfc593fSKent Overstreet 					default:
516*dcfc593fSKent Overstreet 						break;
517*dcfc593fSKent Overstreet 					}
518*dcfc593fSKent Overstreet 				spin_unlock(&s->lock);
519*dcfc593fSKent Overstreet 			}
520*dcfc593fSKent Overstreet 
521*dcfc593fSKent Overstreet 			folio_unlock(folio);
522*dcfc593fSKent Overstreet 		}
523*dcfc593fSKent Overstreet 		folio_batch_release(&fbatch);
524*dcfc593fSKent Overstreet 		cond_resched();
525*dcfc593fSKent Overstreet 	}
526*dcfc593fSKent Overstreet 
527*dcfc593fSKent Overstreet 	i_sectors_acct(c, inode, NULL, i_sectors_delta);
528*dcfc593fSKent Overstreet }
529*dcfc593fSKent Overstreet 
530e1036a2aSKent Overstreet static inline unsigned inode_nr_replicas(struct bch_fs *c, struct bch_inode_info *inode)
531e1036a2aSKent Overstreet {
532e1036a2aSKent Overstreet 	/* XXX: this should not be open coded */
533e1036a2aSKent Overstreet 	return inode->ei_inode.bi_data_replicas
534e1036a2aSKent Overstreet 		? inode->ei_inode.bi_data_replicas - 1
535e1036a2aSKent Overstreet 		: c->opts.data_replicas;
536e1036a2aSKent Overstreet }
537e1036a2aSKent Overstreet 
538f57a6a5dSKent Overstreet static inline unsigned sectors_to_reserve(struct bch_page_sector *s,
539f57a6a5dSKent Overstreet 						  unsigned nr_replicas)
540f57a6a5dSKent Overstreet {
541f57a6a5dSKent Overstreet 	return max(0, (int) nr_replicas -
542f57a6a5dSKent Overstreet 		   s->nr_replicas -
543f57a6a5dSKent Overstreet 		   s->replicas_reserved);
544f57a6a5dSKent Overstreet }
545f57a6a5dSKent Overstreet 
546f57a6a5dSKent Overstreet static int bch2_get_page_disk_reservation(struct bch_fs *c,
547f57a6a5dSKent Overstreet 				struct bch_inode_info *inode,
5481c6fdbd8SKent Overstreet 				struct page *page, bool check_enospc)
5491c6fdbd8SKent Overstreet {
550f57a6a5dSKent Overstreet 	struct bch_page_state *s = bch2_page_state_create(page, 0);
551e1036a2aSKent Overstreet 	unsigned nr_replicas = inode_nr_replicas(c, inode);
552f57a6a5dSKent Overstreet 	struct disk_reservation disk_res = { 0 };
553f57a6a5dSKent Overstreet 	unsigned i, disk_res_sectors = 0;
554f81b648dSKent Overstreet 	int ret;
5551c6fdbd8SKent Overstreet 
556f57a6a5dSKent Overstreet 	if (!s)
557f57a6a5dSKent Overstreet 		return -ENOMEM;
5581c6fdbd8SKent Overstreet 
559f57a6a5dSKent Overstreet 	for (i = 0; i < ARRAY_SIZE(s->s); i++)
560f57a6a5dSKent Overstreet 		disk_res_sectors += sectors_to_reserve(&s->s[i], nr_replicas);
561f57a6a5dSKent Overstreet 
562f57a6a5dSKent Overstreet 	if (!disk_res_sectors)
563f57a6a5dSKent Overstreet 		return 0;
564f57a6a5dSKent Overstreet 
565f57a6a5dSKent Overstreet 	ret = bch2_disk_reservation_get(c, &disk_res,
566f57a6a5dSKent Overstreet 					disk_res_sectors, 1,
567f57a6a5dSKent Overstreet 					!check_enospc
568f57a6a5dSKent Overstreet 					? BCH_DISK_RESERVATION_NOFAIL
569f57a6a5dSKent Overstreet 					: 0);
5701c6fdbd8SKent Overstreet 	if (unlikely(ret))
571f81b648dSKent Overstreet 		return ret;
572f81b648dSKent Overstreet 
573f57a6a5dSKent Overstreet 	for (i = 0; i < ARRAY_SIZE(s->s); i++)
574f57a6a5dSKent Overstreet 		s->s[i].replicas_reserved +=
575f57a6a5dSKent Overstreet 			sectors_to_reserve(&s->s[i], nr_replicas);
576f57a6a5dSKent Overstreet 
577f57a6a5dSKent Overstreet 	return 0;
5781c6fdbd8SKent Overstreet }
5791c6fdbd8SKent Overstreet 
580d1542e03SKent Overstreet struct bch2_page_reservation {
581d1542e03SKent Overstreet 	struct disk_reservation	disk;
582d1542e03SKent Overstreet 	struct quota_res	quota;
583d1542e03SKent Overstreet };
584d1542e03SKent Overstreet 
585d1542e03SKent Overstreet static void bch2_page_reservation_init(struct bch_fs *c,
586f57a6a5dSKent Overstreet 			struct bch_inode_info *inode,
587d1542e03SKent Overstreet 			struct bch2_page_reservation *res)
588d1542e03SKent Overstreet {
589d1542e03SKent Overstreet 	memset(res, 0, sizeof(*res));
590d1542e03SKent Overstreet 
591d1542e03SKent Overstreet 	res->disk.nr_replicas = inode_nr_replicas(c, inode);
592d1542e03SKent Overstreet }
593d1542e03SKent Overstreet 
594d1542e03SKent Overstreet static void bch2_page_reservation_put(struct bch_fs *c,
595d1542e03SKent Overstreet 			struct bch_inode_info *inode,
596d1542e03SKent Overstreet 			struct bch2_page_reservation *res)
597d1542e03SKent Overstreet {
598d1542e03SKent Overstreet 	bch2_disk_reservation_put(c, &res->disk);
599d1542e03SKent Overstreet 	bch2_quota_reservation_put(c, inode, &res->quota);
600d1542e03SKent Overstreet }
601d1542e03SKent Overstreet 
602d1542e03SKent Overstreet static int bch2_page_reservation_get(struct bch_fs *c,
603d1542e03SKent Overstreet 			struct bch_inode_info *inode, struct page *page,
604d1542e03SKent Overstreet 			struct bch2_page_reservation *res,
605d1542e03SKent Overstreet 			unsigned offset, unsigned len, bool check_enospc)
606f57a6a5dSKent Overstreet {
607f57a6a5dSKent Overstreet 	struct bch_page_state *s = bch2_page_state_create(page, 0);
608d1542e03SKent Overstreet 	unsigned i, disk_sectors = 0, quota_sectors = 0;
609f57a6a5dSKent Overstreet 	int ret;
610f57a6a5dSKent Overstreet 
611f57a6a5dSKent Overstreet 	if (!s)
612f57a6a5dSKent Overstreet 		return -ENOMEM;
613f57a6a5dSKent Overstreet 
614e6ec361fSKent Overstreet 	BUG_ON(!s->uptodate);
615e6ec361fSKent Overstreet 
6164b0a66d5SKent Overstreet 	for (i = round_down(offset, block_bytes(c)) >> 9;
6174b0a66d5SKent Overstreet 	     i < round_up(offset + len, block_bytes(c)) >> 9;
618d1542e03SKent Overstreet 	     i++) {
619d1542e03SKent Overstreet 		disk_sectors += sectors_to_reserve(&s->s[i],
620d1542e03SKent Overstreet 						res->disk.nr_replicas);
621d1542e03SKent Overstreet 		quota_sectors += s->s[i].state == SECTOR_UNALLOCATED;
6221c6fdbd8SKent Overstreet 	}
6231c6fdbd8SKent Overstreet 
624d1542e03SKent Overstreet 	if (disk_sectors) {
625d1542e03SKent Overstreet 		ret = bch2_disk_reservation_add(c, &res->disk,
626d1542e03SKent Overstreet 						disk_sectors,
627d1542e03SKent Overstreet 						!check_enospc
628d1542e03SKent Overstreet 						? BCH_DISK_RESERVATION_NOFAIL
629d1542e03SKent Overstreet 						: 0);
630d1542e03SKent Overstreet 		if (unlikely(ret))
631d1542e03SKent Overstreet 			return ret;
632d1542e03SKent Overstreet 	}
633d1542e03SKent Overstreet 
634d1542e03SKent Overstreet 	if (quota_sectors) {
635d1542e03SKent Overstreet 		ret = bch2_quota_reservation_add(c, inode, &res->quota,
636d1542e03SKent Overstreet 						 quota_sectors,
637d1542e03SKent Overstreet 						 check_enospc);
638d1542e03SKent Overstreet 		if (unlikely(ret)) {
639d1542e03SKent Overstreet 			struct disk_reservation tmp = {
640d1542e03SKent Overstreet 				.sectors = disk_sectors
641d1542e03SKent Overstreet 			};
642d1542e03SKent Overstreet 
643d1542e03SKent Overstreet 			bch2_disk_reservation_put(c, &tmp);
644d1542e03SKent Overstreet 			res->disk.sectors -= disk_sectors;
645d1542e03SKent Overstreet 			return ret;
646d1542e03SKent Overstreet 		}
647d1542e03SKent Overstreet 	}
648d1542e03SKent Overstreet 
649d1542e03SKent Overstreet 	return 0;
650f57a6a5dSKent Overstreet }
651f57a6a5dSKent Overstreet 
6521c6fdbd8SKent Overstreet static void bch2_clear_page_bits(struct page *page)
6531c6fdbd8SKent Overstreet {
6541c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = to_bch_ei(page->mapping->host);
6551c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
656f57a6a5dSKent Overstreet 	struct bch_page_state *s = bch2_page_state(page);
657d1542e03SKent Overstreet 	struct disk_reservation disk_res = { 0 };
658f57a6a5dSKent Overstreet 	int i, dirty_sectors = 0;
6591c6fdbd8SKent Overstreet 
660f57a6a5dSKent Overstreet 	if (!s)
6611c6fdbd8SKent Overstreet 		return;
6621c6fdbd8SKent Overstreet 
6633826ee0bSKent Overstreet 	EBUG_ON(!PageLocked(page));
6643826ee0bSKent Overstreet 	EBUG_ON(PageWriteback(page));
6653826ee0bSKent Overstreet 
666f57a6a5dSKent Overstreet 	for (i = 0; i < ARRAY_SIZE(s->s); i++) {
667d1542e03SKent Overstreet 		disk_res.sectors += s->s[i].replicas_reserved;
668d1542e03SKent Overstreet 		s->s[i].replicas_reserved = 0;
669d1542e03SKent Overstreet 
670b44a66a6SKent Overstreet 		switch (s->s[i].state) {
671b44a66a6SKent Overstreet 		case SECTOR_DIRTY:
672f57a6a5dSKent Overstreet 			s->s[i].state = SECTOR_UNALLOCATED;
673b44a66a6SKent Overstreet 			--dirty_sectors;
674b44a66a6SKent Overstreet 			break;
675b44a66a6SKent Overstreet 		case SECTOR_DIRTY_RESERVED:
676b44a66a6SKent Overstreet 			s->s[i].state = SECTOR_RESERVED;
677b44a66a6SKent Overstreet 			break;
678b44a66a6SKent Overstreet 		default:
679b44a66a6SKent Overstreet 			break;
680f57a6a5dSKent Overstreet 		}
681f57a6a5dSKent Overstreet 	}
682adfcfaf0SKent Overstreet 
683d1542e03SKent Overstreet 	bch2_disk_reservation_put(c, &disk_res);
684d1542e03SKent Overstreet 
685b44a66a6SKent Overstreet 	i_sectors_acct(c, inode, NULL, dirty_sectors);
686adfcfaf0SKent Overstreet 
687f57a6a5dSKent Overstreet 	bch2_page_state_release(page);
6881c6fdbd8SKent Overstreet }
6891c6fdbd8SKent Overstreet 
690d1542e03SKent Overstreet static void bch2_set_page_dirty(struct bch_fs *c,
691d1542e03SKent Overstreet 			struct bch_inode_info *inode, struct page *page,
692d1542e03SKent Overstreet 			struct bch2_page_reservation *res,
693d1542e03SKent Overstreet 			unsigned offset, unsigned len)
6941c6fdbd8SKent Overstreet {
695d1542e03SKent Overstreet 	struct bch_page_state *s = bch2_page_state(page);
696f57a6a5dSKent Overstreet 	unsigned i, dirty_sectors = 0;
6971c6fdbd8SKent Overstreet 
698877dfb34SKent Overstreet 	WARN_ON((u64) page_offset(page) + offset + len >
699877dfb34SKent Overstreet 		round_up((u64) i_size_read(&inode->v), block_bytes(c)));
700fb472ac5SKent Overstreet 
7013826ee0bSKent Overstreet 	spin_lock(&s->lock);
7023826ee0bSKent Overstreet 
7034b0a66d5SKent Overstreet 	for (i = round_down(offset, block_bytes(c)) >> 9;
7044b0a66d5SKent Overstreet 	     i < round_up(offset + len, block_bytes(c)) >> 9;
705d1542e03SKent Overstreet 	     i++) {
706d1542e03SKent Overstreet 		unsigned sectors = sectors_to_reserve(&s->s[i],
707d1542e03SKent Overstreet 						res->disk.nr_replicas);
7081c6fdbd8SKent Overstreet 
709406d6d5aSKent Overstreet 		/*
710406d6d5aSKent Overstreet 		 * This can happen if we race with the error path in
711406d6d5aSKent Overstreet 		 * bch2_writepage_io_done():
712406d6d5aSKent Overstreet 		 */
713406d6d5aSKent Overstreet 		sectors = min_t(unsigned, sectors, res->disk.sectors);
714406d6d5aSKent Overstreet 
715d1542e03SKent Overstreet 		s->s[i].replicas_reserved += sectors;
716d1542e03SKent Overstreet 		res->disk.sectors -= sectors;
717adfcfaf0SKent Overstreet 
718b44a66a6SKent Overstreet 		switch (s->s[i].state) {
719b44a66a6SKent Overstreet 		case SECTOR_UNALLOCATED:
720b44a66a6SKent Overstreet 			s->s[i].state = SECTOR_DIRTY;
721f57a6a5dSKent Overstreet 			dirty_sectors++;
722b44a66a6SKent Overstreet 			break;
723b44a66a6SKent Overstreet 		case SECTOR_RESERVED:
724b44a66a6SKent Overstreet 			s->s[i].state = SECTOR_DIRTY_RESERVED;
725b44a66a6SKent Overstreet 			break;
726b44a66a6SKent Overstreet 		default:
727b44a66a6SKent Overstreet 			break;
728b44a66a6SKent Overstreet 		}
729f57a6a5dSKent Overstreet 	}
730f57a6a5dSKent Overstreet 
7313826ee0bSKent Overstreet 	spin_unlock(&s->lock);
7323826ee0bSKent Overstreet 
733d1542e03SKent Overstreet 	i_sectors_acct(c, inode, &res->quota, dirty_sectors);
7341c6fdbd8SKent Overstreet 
735d1542e03SKent Overstreet 	if (!PageDirty(page))
736d1542e03SKent Overstreet 		filemap_dirty_folio(inode->v.i_mapping, page_folio(page));
7371c6fdbd8SKent Overstreet }
7381c6fdbd8SKent Overstreet 
7391c6fdbd8SKent Overstreet vm_fault_t bch2_page_fault(struct vm_fault *vmf)
7401c6fdbd8SKent Overstreet {
7411c6fdbd8SKent Overstreet 	struct file *file = vmf->vma->vm_file;
742eb8e6e9cSKent Overstreet 	struct address_space *mapping = file->f_mapping;
743eb8e6e9cSKent Overstreet 	struct address_space *fdm = faults_disabled_mapping();
7441c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = file_bch_inode(file);
7451c6fdbd8SKent Overstreet 	int ret;
7461c6fdbd8SKent Overstreet 
747eb8e6e9cSKent Overstreet 	if (fdm == mapping)
748eb8e6e9cSKent Overstreet 		return VM_FAULT_SIGBUS;
749eb8e6e9cSKent Overstreet 
750eb8e6e9cSKent Overstreet 	/* Lock ordering: */
751eb8e6e9cSKent Overstreet 	if (fdm > mapping) {
752eb8e6e9cSKent Overstreet 		struct bch_inode_info *fdm_host = to_bch_ei(fdm->host);
753eb8e6e9cSKent Overstreet 
754eb8e6e9cSKent Overstreet 		if (bch2_pagecache_add_tryget(&inode->ei_pagecache_lock))
755eb8e6e9cSKent Overstreet 			goto got_lock;
756eb8e6e9cSKent Overstreet 
757eb8e6e9cSKent Overstreet 		bch2_pagecache_block_put(&fdm_host->ei_pagecache_lock);
758eb8e6e9cSKent Overstreet 
7591c6fdbd8SKent Overstreet 		bch2_pagecache_add_get(&inode->ei_pagecache_lock);
760eb8e6e9cSKent Overstreet 		bch2_pagecache_add_put(&inode->ei_pagecache_lock);
761eb8e6e9cSKent Overstreet 
762eb8e6e9cSKent Overstreet 		bch2_pagecache_block_get(&fdm_host->ei_pagecache_lock);
763eb8e6e9cSKent Overstreet 
764eb8e6e9cSKent Overstreet 		/* Signal that lock has been dropped: */
765eb8e6e9cSKent Overstreet 		set_fdm_dropped_locks();
766eb8e6e9cSKent Overstreet 		return VM_FAULT_SIGBUS;
767eb8e6e9cSKent Overstreet 	}
768eb8e6e9cSKent Overstreet 
769eb8e6e9cSKent Overstreet 	bch2_pagecache_add_get(&inode->ei_pagecache_lock);
770eb8e6e9cSKent Overstreet got_lock:
7711c6fdbd8SKent Overstreet 	ret = filemap_fault(vmf);
7721c6fdbd8SKent Overstreet 	bch2_pagecache_add_put(&inode->ei_pagecache_lock);
7731c6fdbd8SKent Overstreet 
7741c6fdbd8SKent Overstreet 	return ret;
7751c6fdbd8SKent Overstreet }
7761c6fdbd8SKent Overstreet 
7771c6fdbd8SKent Overstreet vm_fault_t bch2_page_mkwrite(struct vm_fault *vmf)
7781c6fdbd8SKent Overstreet {
7791c6fdbd8SKent Overstreet 	struct page *page = vmf->page;
7801c6fdbd8SKent Overstreet 	struct file *file = vmf->vma->vm_file;
7811c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = file_bch_inode(file);
7821c6fdbd8SKent Overstreet 	struct address_space *mapping = file->f_mapping;
7831c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
784d1542e03SKent Overstreet 	struct bch2_page_reservation res;
7856cc3535dSKent Overstreet 	unsigned len;
7866cc3535dSKent Overstreet 	loff_t isize;
787e6ec361fSKent Overstreet 	int ret;
7881c6fdbd8SKent Overstreet 
789d1542e03SKent Overstreet 	bch2_page_reservation_init(c, inode, &res);
790d1542e03SKent Overstreet 
7911c6fdbd8SKent Overstreet 	sb_start_pagefault(inode->v.i_sb);
7921c6fdbd8SKent Overstreet 	file_update_time(file);
7931c6fdbd8SKent Overstreet 
7941c6fdbd8SKent Overstreet 	/*
7951c6fdbd8SKent Overstreet 	 * Not strictly necessary, but helps avoid dio writes livelocking in
7961c6fdbd8SKent Overstreet 	 * write_invalidate_inode_pages_range() - can drop this if/when we get
7971c6fdbd8SKent Overstreet 	 * a write_invalidate_inode_pages_range() that works without dropping
7981c6fdbd8SKent Overstreet 	 * page lock before invalidating page
7991c6fdbd8SKent Overstreet 	 */
8001c6fdbd8SKent Overstreet 	bch2_pagecache_add_get(&inode->ei_pagecache_lock);
8011c6fdbd8SKent Overstreet 
8021c6fdbd8SKent Overstreet 	lock_page(page);
8036cc3535dSKent Overstreet 	isize = i_size_read(&inode->v);
8046cc3535dSKent Overstreet 
8056cc3535dSKent Overstreet 	if (page->mapping != mapping || page_offset(page) >= isize) {
8061c6fdbd8SKent Overstreet 		unlock_page(page);
8071c6fdbd8SKent Overstreet 		ret = VM_FAULT_NOPAGE;
8081c6fdbd8SKent Overstreet 		goto out;
8091c6fdbd8SKent Overstreet 	}
8101c6fdbd8SKent Overstreet 
811d55460bbSKent Overstreet 	len = min_t(loff_t, PAGE_SIZE, isize - page_offset(page));
8126cc3535dSKent Overstreet 
813e6ec361fSKent Overstreet 	if (!bch2_page_state_create(page, __GFP_NOFAIL)->uptodate) {
814e6ec361fSKent Overstreet 		if (bch2_page_state_set(c, inode_inum(inode), &page, 1)) {
815e6ec361fSKent Overstreet 			unlock_page(page);
816e6ec361fSKent Overstreet 			ret = VM_FAULT_SIGBUS;
817e6ec361fSKent Overstreet 			goto out;
818e6ec361fSKent Overstreet 		}
819e6ec361fSKent Overstreet 	}
820e6ec361fSKent Overstreet 
8216cc3535dSKent Overstreet 	if (bch2_page_reservation_get(c, inode, page, &res, 0, len, true)) {
8221c6fdbd8SKent Overstreet 		unlock_page(page);
8231c6fdbd8SKent Overstreet 		ret = VM_FAULT_SIGBUS;
8241c6fdbd8SKent Overstreet 		goto out;
8251c6fdbd8SKent Overstreet 	}
8261c6fdbd8SKent Overstreet 
8276cc3535dSKent Overstreet 	bch2_set_page_dirty(c, inode, page, &res, 0, len);
8281b783a69SKent Overstreet 	bch2_page_reservation_put(c, inode, &res);
8291b783a69SKent Overstreet 
8301c6fdbd8SKent Overstreet 	wait_for_stable_page(page);
831e6ec361fSKent Overstreet 	ret = VM_FAULT_LOCKED;
8321c6fdbd8SKent Overstreet out:
8331c6fdbd8SKent Overstreet 	bch2_pagecache_add_put(&inode->ei_pagecache_lock);
8341c6fdbd8SKent Overstreet 	sb_end_pagefault(inode->v.i_sb);
835d1542e03SKent Overstreet 
8361c6fdbd8SKent Overstreet 	return ret;
8371c6fdbd8SKent Overstreet }
8381c6fdbd8SKent Overstreet 
8391c6fdbd8SKent Overstreet void bch2_invalidate_folio(struct folio *folio, size_t offset, size_t length)
8401c6fdbd8SKent Overstreet {
8411c6fdbd8SKent Overstreet 	if (offset || length < folio_size(folio))
8421c6fdbd8SKent Overstreet 		return;
8431c6fdbd8SKent Overstreet 
8441c6fdbd8SKent Overstreet 	bch2_clear_page_bits(&folio->page);
8451c6fdbd8SKent Overstreet }
8461c6fdbd8SKent Overstreet 
8471c6fdbd8SKent Overstreet bool bch2_release_folio(struct folio *folio, gfp_t gfp_mask)
8481c6fdbd8SKent Overstreet {
849a6d90385SKent Overstreet 	if (folio_test_dirty(folio) || folio_test_writeback(folio))
8501c6fdbd8SKent Overstreet 		return false;
8511c6fdbd8SKent Overstreet 
8521c6fdbd8SKent Overstreet 	bch2_clear_page_bits(&folio->page);
8531c6fdbd8SKent Overstreet 	return true;
8541c6fdbd8SKent Overstreet }
8551c6fdbd8SKent Overstreet 
8561c6fdbd8SKent Overstreet /* readpage(s): */
8571c6fdbd8SKent Overstreet 
8581c6fdbd8SKent Overstreet static void bch2_readpages_end_io(struct bio *bio)
8591c6fdbd8SKent Overstreet {
8601c6fdbd8SKent Overstreet 	struct bvec_iter_all iter;
8611c6fdbd8SKent Overstreet 	struct bio_vec *bv;
8621c6fdbd8SKent Overstreet 
8631c6fdbd8SKent Overstreet 	bio_for_each_segment_all(bv, bio, iter) {
8641c6fdbd8SKent Overstreet 		struct page *page = bv->bv_page;
8651c6fdbd8SKent Overstreet 
8661c6fdbd8SKent Overstreet 		if (!bio->bi_status) {
8671c6fdbd8SKent Overstreet 			SetPageUptodate(page);
8681c6fdbd8SKent Overstreet 		} else {
8691c6fdbd8SKent Overstreet 			ClearPageUptodate(page);
8701c6fdbd8SKent Overstreet 			SetPageError(page);
8711c6fdbd8SKent Overstreet 		}
8721c6fdbd8SKent Overstreet 		unlock_page(page);
8731c6fdbd8SKent Overstreet 	}
8741c6fdbd8SKent Overstreet 
8751c6fdbd8SKent Overstreet 	bio_put(bio);
8761c6fdbd8SKent Overstreet }
8771c6fdbd8SKent Overstreet 
8781c6fdbd8SKent Overstreet struct readpages_iter {
8791c6fdbd8SKent Overstreet 	struct address_space	*mapping;
8801c6fdbd8SKent Overstreet 	struct page		**pages;
8811c6fdbd8SKent Overstreet 	unsigned		nr_pages;
8821c6fdbd8SKent Overstreet 	unsigned		idx;
8831c6fdbd8SKent Overstreet 	pgoff_t			offset;
8841c6fdbd8SKent Overstreet };
8851c6fdbd8SKent Overstreet 
8861c6fdbd8SKent Overstreet static int readpages_iter_init(struct readpages_iter *iter,
8871c6fdbd8SKent Overstreet 			       struct readahead_control *ractl)
8881c6fdbd8SKent Overstreet {
8891c6fdbd8SKent Overstreet 	unsigned i, nr_pages = readahead_count(ractl);
8901c6fdbd8SKent Overstreet 
8911c6fdbd8SKent Overstreet 	memset(iter, 0, sizeof(*iter));
8921c6fdbd8SKent Overstreet 
8931c6fdbd8SKent Overstreet 	iter->mapping	= ractl->mapping;
8941c6fdbd8SKent Overstreet 	iter->offset	= readahead_index(ractl);
8951c6fdbd8SKent Overstreet 	iter->nr_pages	= nr_pages;
8961c6fdbd8SKent Overstreet 
8971c6fdbd8SKent Overstreet 	iter->pages = kmalloc_array(nr_pages, sizeof(struct page *), GFP_NOFS);
8981c6fdbd8SKent Overstreet 	if (!iter->pages)
8991c6fdbd8SKent Overstreet 		return -ENOMEM;
9001c6fdbd8SKent Overstreet 
90189931472SKent Overstreet 	nr_pages = __readahead_batch(ractl, iter->pages, nr_pages);
9021c6fdbd8SKent Overstreet 	for (i = 0; i < nr_pages; i++) {
903f57a6a5dSKent Overstreet 		__bch2_page_state_create(iter->pages[i], __GFP_NOFAIL);
9041c6fdbd8SKent Overstreet 		put_page(iter->pages[i]);
9051c6fdbd8SKent Overstreet 	}
9061c6fdbd8SKent Overstreet 
9071c6fdbd8SKent Overstreet 	return 0;
9081c6fdbd8SKent Overstreet }
9091c6fdbd8SKent Overstreet 
9101c6fdbd8SKent Overstreet static inline struct page *readpage_iter_next(struct readpages_iter *iter)
9111c6fdbd8SKent Overstreet {
9121c6fdbd8SKent Overstreet 	if (iter->idx >= iter->nr_pages)
9131c6fdbd8SKent Overstreet 		return NULL;
9141c6fdbd8SKent Overstreet 
9151c6fdbd8SKent Overstreet 	EBUG_ON(iter->pages[iter->idx]->index != iter->offset + iter->idx);
9161c6fdbd8SKent Overstreet 
9171c6fdbd8SKent Overstreet 	return iter->pages[iter->idx];
9181c6fdbd8SKent Overstreet }
9191c6fdbd8SKent Overstreet 
92035189e09SKent Overstreet static bool extent_partial_reads_expensive(struct bkey_s_c k)
92135189e09SKent Overstreet {
92235189e09SKent Overstreet 	struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
92335189e09SKent Overstreet 	struct bch_extent_crc_unpacked crc;
92435189e09SKent Overstreet 	const union bch_extent_entry *i;
92535189e09SKent Overstreet 
92635189e09SKent Overstreet 	bkey_for_each_crc(k.k, ptrs, crc, i)
92735189e09SKent Overstreet 		if (crc.csum_type || crc.compression_type)
92835189e09SKent Overstreet 			return true;
92935189e09SKent Overstreet 	return false;
93035189e09SKent Overstreet }
93135189e09SKent Overstreet 
9321c6fdbd8SKent Overstreet static void readpage_bio_extend(struct readpages_iter *iter,
93376426098SKent Overstreet 				struct bio *bio,
93476426098SKent Overstreet 				unsigned sectors_this_extent,
9351c6fdbd8SKent Overstreet 				bool get_more)
9361c6fdbd8SKent Overstreet {
93776426098SKent Overstreet 	while (bio_sectors(bio) < sectors_this_extent &&
9381c6fdbd8SKent Overstreet 	       bio->bi_vcnt < bio->bi_max_vecs) {
9397279c1a2SKent Overstreet 		pgoff_t page_offset = bio_end_sector(bio) >> PAGE_SECTORS_SHIFT;
9401c6fdbd8SKent Overstreet 		struct page *page = readpage_iter_next(iter);
9411c6fdbd8SKent Overstreet 		int ret;
9421c6fdbd8SKent Overstreet 
9431c6fdbd8SKent Overstreet 		if (page) {
9441c6fdbd8SKent Overstreet 			if (iter->offset + iter->idx != page_offset)
9451c6fdbd8SKent Overstreet 				break;
9461c6fdbd8SKent Overstreet 
9471c6fdbd8SKent Overstreet 			iter->idx++;
9481c6fdbd8SKent Overstreet 		} else {
9491c6fdbd8SKent Overstreet 			if (!get_more)
9501c6fdbd8SKent Overstreet 				break;
9511c6fdbd8SKent Overstreet 
9521c6fdbd8SKent Overstreet 			page = xa_load(&iter->mapping->i_pages, page_offset);
9531c6fdbd8SKent Overstreet 			if (page && !xa_is_value(page))
9541c6fdbd8SKent Overstreet 				break;
9551c6fdbd8SKent Overstreet 
9561c6fdbd8SKent Overstreet 			page = __page_cache_alloc(readahead_gfp_mask(iter->mapping));
9571c6fdbd8SKent Overstreet 			if (!page)
9581c6fdbd8SKent Overstreet 				break;
9591c6fdbd8SKent Overstreet 
960f57a6a5dSKent Overstreet 			if (!__bch2_page_state_create(page, 0)) {
961f57a6a5dSKent Overstreet 				put_page(page);
962f57a6a5dSKent Overstreet 				break;
963f57a6a5dSKent Overstreet 			}
9641c6fdbd8SKent Overstreet 
9651c6fdbd8SKent Overstreet 			ret = add_to_page_cache_lru(page, iter->mapping,
9661c6fdbd8SKent Overstreet 						    page_offset, GFP_NOFS);
9671c6fdbd8SKent Overstreet 			if (ret) {
968f57a6a5dSKent Overstreet 				__bch2_page_state_release(page);
9691c6fdbd8SKent Overstreet 				put_page(page);
9701c6fdbd8SKent Overstreet 				break;
9711c6fdbd8SKent Overstreet 			}
9721c6fdbd8SKent Overstreet 
9731c6fdbd8SKent Overstreet 			put_page(page);
9741c6fdbd8SKent Overstreet 		}
9751c6fdbd8SKent Overstreet 
976885678f6SKent Overstreet 		BUG_ON(!bio_add_page(bio, page, PAGE_SIZE, 0));
9771c6fdbd8SKent Overstreet 	}
9781c6fdbd8SKent Overstreet }
9791c6fdbd8SKent Overstreet 
9808c6d298aSKent Overstreet static void bchfs_read(struct btree_trans *trans,
9818c6d298aSKent Overstreet 		       struct bch_read_bio *rbio,
9828c6d298aSKent Overstreet 		       subvol_inum inum,
9831c6fdbd8SKent Overstreet 		       struct readpages_iter *readpages_iter)
9841c6fdbd8SKent Overstreet {
9850f238367SKent Overstreet 	struct bch_fs *c = trans->c;
9868c6d298aSKent Overstreet 	struct btree_iter iter;
98707a1006aSKent Overstreet 	struct bkey_buf sk;
9881c6fdbd8SKent Overstreet 	int flags = BCH_READ_RETRY_IF_STALE|
9891c6fdbd8SKent Overstreet 		BCH_READ_MAY_PROMOTE;
9908c6d298aSKent Overstreet 	u32 snapshot;
99176426098SKent Overstreet 	int ret = 0;
9921c6fdbd8SKent Overstreet 
9931c6fdbd8SKent Overstreet 	rbio->c = c;
9941c6fdbd8SKent Overstreet 	rbio->start_time = local_clock();
9958c6d298aSKent Overstreet 	rbio->subvol = inum.subvol;
99635189e09SKent Overstreet 
99707a1006aSKent Overstreet 	bch2_bkey_buf_init(&sk);
99876426098SKent Overstreet retry:
999700c25b3SKent Overstreet 	bch2_trans_begin(trans);
10008c6d298aSKent Overstreet 	iter = (struct btree_iter) { NULL };
1001700c25b3SKent Overstreet 
10028c6d298aSKent Overstreet 	ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
10038c6d298aSKent Overstreet 	if (ret)
10048c6d298aSKent Overstreet 		goto err;
10058c6d298aSKent Overstreet 
10068c6d298aSKent Overstreet 	bch2_trans_iter_init(trans, &iter, BTREE_ID_extents,
10078c6d298aSKent Overstreet 			     SPOS(inum.inum, rbio->bio.bi_iter.bi_sector, snapshot),
10088c6d298aSKent Overstreet 			     BTREE_ITER_SLOTS|BTREE_ITER_FILTER_SNAPSHOTS);
10091c6fdbd8SKent Overstreet 	while (1) {
10101c6fdbd8SKent Overstreet 		struct bkey_s_c k;
101176426098SKent Overstreet 		unsigned bytes, sectors, offset_into_extent;
10125ff75ccbSKent Overstreet 		enum btree_id data_btree = BTREE_ID_extents;
10131c6fdbd8SKent Overstreet 
10143737e0ddSKent Overstreet 		/*
10153737e0ddSKent Overstreet 		 * read_extent -> io_time_reset may cause a transaction restart
10163737e0ddSKent Overstreet 		 * without returning an error, we need to check for that here:
10173737e0ddSKent Overstreet 		 */
10183737e0ddSKent Overstreet 		if (!bch2_trans_relock(trans)) {
10193737e0ddSKent Overstreet 			ret = -EINTR;
10203737e0ddSKent Overstreet 			break;
10213737e0ddSKent Overstreet 		}
10223737e0ddSKent Overstreet 
10238c6d298aSKent Overstreet 		bch2_btree_iter_set_pos(&iter,
10248c6d298aSKent Overstreet 				POS(inum.inum, rbio->bio.bi_iter.bi_sector));
10251c6fdbd8SKent Overstreet 
10268c6d298aSKent Overstreet 		k = bch2_btree_iter_peek_slot(&iter);
102776426098SKent Overstreet 		ret = bkey_err(k);
102876426098SKent Overstreet 		if (ret)
102976426098SKent Overstreet 			break;
10301c6fdbd8SKent Overstreet 
10318c6d298aSKent Overstreet 		offset_into_extent = iter.pos.offset -
103206ed8558SKent Overstreet 			bkey_start_offset(k.k);
103376426098SKent Overstreet 		sectors = k.k->size - offset_into_extent;
103476426098SKent Overstreet 
103507a1006aSKent Overstreet 		bch2_bkey_buf_reassemble(&sk, c, k);
103613dcd4abSKent Overstreet 
10375ff75ccbSKent Overstreet 		ret = bch2_read_indirect_extent(trans, &data_btree,
103822d8a33dSYuxuan Shui 					&offset_into_extent, &sk);
103976426098SKent Overstreet 		if (ret)
104076426098SKent Overstreet 			break;
104176426098SKent Overstreet 
104213dcd4abSKent Overstreet 		k = bkey_i_to_s_c(sk.k);
104313dcd4abSKent Overstreet 
104476426098SKent Overstreet 		sectors = min(sectors, k.k->size - offset_into_extent);
104576426098SKent Overstreet 
104676426098SKent Overstreet 		bch2_trans_unlock(trans);
104706ed8558SKent Overstreet 
104835189e09SKent Overstreet 		if (readpages_iter)
104935189e09SKent Overstreet 			readpage_bio_extend(readpages_iter, &rbio->bio, sectors,
105035189e09SKent Overstreet 					    extent_partial_reads_expensive(k));
10511c6fdbd8SKent Overstreet 
105276426098SKent Overstreet 		bytes = min(sectors, bio_sectors(&rbio->bio)) << 9;
105306ed8558SKent Overstreet 		swap(rbio->bio.bi_iter.bi_size, bytes);
10541c6fdbd8SKent Overstreet 
105506ed8558SKent Overstreet 		if (rbio->bio.bi_iter.bi_size == bytes)
10561c6fdbd8SKent Overstreet 			flags |= BCH_READ_LAST_FRAGMENT;
10571c6fdbd8SKent Overstreet 
1058b44a66a6SKent Overstreet 		bch2_bio_page_state_set(&rbio->bio, k);
10591c6fdbd8SKent Overstreet 
10608c6d298aSKent Overstreet 		bch2_read_extent(trans, rbio, iter.pos,
10615ff75ccbSKent Overstreet 				 data_btree, k, offset_into_extent, flags);
10621c6fdbd8SKent Overstreet 
10631c6fdbd8SKent Overstreet 		if (flags & BCH_READ_LAST_FRAGMENT)
106435189e09SKent Overstreet 			break;
10651c6fdbd8SKent Overstreet 
106606ed8558SKent Overstreet 		swap(rbio->bio.bi_iter.bi_size, bytes);
106706ed8558SKent Overstreet 		bio_advance(&rbio->bio, bytes);
1068084d42bbSKent Overstreet 
1069084d42bbSKent Overstreet 		ret = btree_trans_too_many_iters(trans);
1070084d42bbSKent Overstreet 		if (ret)
1071084d42bbSKent Overstreet 			break;
10721c6fdbd8SKent Overstreet 	}
10738c6d298aSKent Overstreet err:
10748c6d298aSKent Overstreet 	bch2_trans_iter_exit(trans, &iter);
107576426098SKent Overstreet 
107676426098SKent Overstreet 	if (ret == -EINTR)
107776426098SKent Overstreet 		goto retry;
107876426098SKent Overstreet 
107935189e09SKent Overstreet 	if (ret) {
10808c6d298aSKent Overstreet 		bch_err_inum_ratelimited(c, inum.inum,
10810fefe8d8SKent Overstreet 				"read error %i from btree lookup", ret);
10820fefe8d8SKent Overstreet 		rbio->bio.bi_status = BLK_STS_IOERR;
108376426098SKent Overstreet 		bio_endio(&rbio->bio);
10841c6fdbd8SKent Overstreet 	}
10851c6fdbd8SKent Overstreet 
108607a1006aSKent Overstreet 	bch2_bkey_buf_exit(&sk, c);
108735189e09SKent Overstreet }
108835189e09SKent Overstreet 
10891c6fdbd8SKent Overstreet void bch2_readahead(struct readahead_control *ractl)
10901c6fdbd8SKent Overstreet {
10911c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = to_bch_ei(ractl->mapping->host);
10921c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
10939a3df993SKent Overstreet 	struct bch_io_opts opts = io_opts(c, &inode->ei_inode);
1094424eb881SKent Overstreet 	struct btree_trans trans;
10951c6fdbd8SKent Overstreet 	struct page *page;
10961c6fdbd8SKent Overstreet 	struct readpages_iter readpages_iter;
10971c6fdbd8SKent Overstreet 	int ret;
10981c6fdbd8SKent Overstreet 
10991c6fdbd8SKent Overstreet 	ret = readpages_iter_init(&readpages_iter, ractl);
11001c6fdbd8SKent Overstreet 	BUG_ON(ret);
11011c6fdbd8SKent Overstreet 
110220bceecbSKent Overstreet 	bch2_trans_init(&trans, c, 0, 0);
11031c6fdbd8SKent Overstreet 
11041c6fdbd8SKent Overstreet 	bch2_pagecache_add_get(&inode->ei_pagecache_lock);
11051c6fdbd8SKent Overstreet 
11061c6fdbd8SKent Overstreet 	while ((page = readpage_iter_next(&readpages_iter))) {
11071c6fdbd8SKent Overstreet 		pgoff_t index = readpages_iter.offset + readpages_iter.idx;
11081c6fdbd8SKent Overstreet 		unsigned n = min_t(unsigned,
11091c6fdbd8SKent Overstreet 				   readpages_iter.nr_pages -
11101c6fdbd8SKent Overstreet 				   readpages_iter.idx,
11111c6fdbd8SKent Overstreet 				   BIO_MAX_VECS);
11121c6fdbd8SKent Overstreet 		struct bch_read_bio *rbio =
11131c6fdbd8SKent Overstreet 			rbio_init(bio_alloc_bioset(NULL, n, REQ_OP_READ,
11141c6fdbd8SKent Overstreet 						   GFP_NOFS, &c->bio_read),
11151c6fdbd8SKent Overstreet 				  opts);
11161c6fdbd8SKent Overstreet 
11171c6fdbd8SKent Overstreet 		readpages_iter.idx++;
11181c6fdbd8SKent Overstreet 
11197279c1a2SKent Overstreet 		rbio->bio.bi_iter.bi_sector = (sector_t) index << PAGE_SECTORS_SHIFT;
11201c6fdbd8SKent Overstreet 		rbio->bio.bi_end_io = bch2_readpages_end_io;
1121885678f6SKent Overstreet 		BUG_ON(!bio_add_page(&rbio->bio, page, PAGE_SIZE, 0));
11221c6fdbd8SKent Overstreet 
11238c6d298aSKent Overstreet 		bchfs_read(&trans, rbio, inode_inum(inode),
11240f238367SKent Overstreet 			   &readpages_iter);
11251c6fdbd8SKent Overstreet 	}
11261c6fdbd8SKent Overstreet 
11271c6fdbd8SKent Overstreet 	bch2_pagecache_add_put(&inode->ei_pagecache_lock);
1128424eb881SKent Overstreet 
1129424eb881SKent Overstreet 	bch2_trans_exit(&trans);
11301c6fdbd8SKent Overstreet 	kfree(readpages_iter.pages);
11311c6fdbd8SKent Overstreet }
11321c6fdbd8SKent Overstreet 
11331c6fdbd8SKent Overstreet static void __bchfs_readpage(struct bch_fs *c, struct bch_read_bio *rbio,
11348c6d298aSKent Overstreet 			     subvol_inum inum, struct page *page)
11351c6fdbd8SKent Overstreet {
1136424eb881SKent Overstreet 	struct btree_trans trans;
11371c6fdbd8SKent Overstreet 
1138f57a6a5dSKent Overstreet 	bch2_page_state_create(page, __GFP_NOFAIL);
11391c6fdbd8SKent Overstreet 
11401c6fdbd8SKent Overstreet 	rbio->bio.bi_opf = REQ_OP_READ|REQ_SYNC;
11417f5e31e1SKent Overstreet 	rbio->bio.bi_iter.bi_sector =
11427279c1a2SKent Overstreet 		(sector_t) page->index << PAGE_SECTORS_SHIFT;
11437f5e31e1SKent Overstreet 	BUG_ON(!bio_add_page(&rbio->bio, page, PAGE_SIZE, 0));
11441c6fdbd8SKent Overstreet 
114520bceecbSKent Overstreet 	bch2_trans_init(&trans, c, 0, 0);
11468c6d298aSKent Overstreet 	bchfs_read(&trans, rbio, inum, NULL);
1147424eb881SKent Overstreet 	bch2_trans_exit(&trans);
11481c6fdbd8SKent Overstreet }
11491c6fdbd8SKent Overstreet 
11501c6fdbd8SKent Overstreet static void bch2_read_single_page_end_io(struct bio *bio)
11511c6fdbd8SKent Overstreet {
11521c6fdbd8SKent Overstreet 	complete(bio->bi_private);
11531c6fdbd8SKent Overstreet }
11541c6fdbd8SKent Overstreet 
11551c6fdbd8SKent Overstreet static int bch2_read_single_page(struct page *page,
11561c6fdbd8SKent Overstreet 				 struct address_space *mapping)
11571c6fdbd8SKent Overstreet {
11581c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = to_bch_ei(mapping->host);
11591c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
11601c6fdbd8SKent Overstreet 	struct bch_read_bio *rbio;
11611c6fdbd8SKent Overstreet 	int ret;
11621c6fdbd8SKent Overstreet 	DECLARE_COMPLETION_ONSTACK(done);
11631c6fdbd8SKent Overstreet 
11641c6fdbd8SKent Overstreet 	rbio = rbio_init(bio_alloc_bioset(NULL, 1, REQ_OP_READ, GFP_NOFS, &c->bio_read),
11659a3df993SKent Overstreet 			 io_opts(c, &inode->ei_inode));
11661c6fdbd8SKent Overstreet 	rbio->bio.bi_private = &done;
11671c6fdbd8SKent Overstreet 	rbio->bio.bi_end_io = bch2_read_single_page_end_io;
11681c6fdbd8SKent Overstreet 
11698c6d298aSKent Overstreet 	__bchfs_readpage(c, rbio, inode_inum(inode), page);
11701c6fdbd8SKent Overstreet 	wait_for_completion(&done);
11711c6fdbd8SKent Overstreet 
11721c6fdbd8SKent Overstreet 	ret = blk_status_to_errno(rbio->bio.bi_status);
11731c6fdbd8SKent Overstreet 	bio_put(&rbio->bio);
11741c6fdbd8SKent Overstreet 
11751c6fdbd8SKent Overstreet 	if (ret < 0)
11761c6fdbd8SKent Overstreet 		return ret;
11771c6fdbd8SKent Overstreet 
11781c6fdbd8SKent Overstreet 	SetPageUptodate(page);
11791c6fdbd8SKent Overstreet 	return 0;
11801c6fdbd8SKent Overstreet }
11811c6fdbd8SKent Overstreet 
11821c6fdbd8SKent Overstreet int bch2_read_folio(struct file *file, struct folio *folio)
11831c6fdbd8SKent Overstreet {
11841c6fdbd8SKent Overstreet 	struct page *page = &folio->page;
11851c6fdbd8SKent Overstreet 	int ret;
11861c6fdbd8SKent Overstreet 
11871c6fdbd8SKent Overstreet 	ret = bch2_read_single_page(page, page->mapping);
11881c6fdbd8SKent Overstreet 	folio_unlock(folio);
11891c6fdbd8SKent Overstreet 	return ret;
11901c6fdbd8SKent Overstreet }
11911c6fdbd8SKent Overstreet 
11921c6fdbd8SKent Overstreet /* writepages: */
11931c6fdbd8SKent Overstreet 
11941c6fdbd8SKent Overstreet struct bch_writepage_state {
11951c6fdbd8SKent Overstreet 	struct bch_writepage_io	*io;
11961c6fdbd8SKent Overstreet 	struct bch_io_opts	opts;
11971c6fdbd8SKent Overstreet };
11981c6fdbd8SKent Overstreet 
11991c6fdbd8SKent Overstreet static inline struct bch_writepage_state bch_writepage_state_init(struct bch_fs *c,
12001c6fdbd8SKent Overstreet 								  struct bch_inode_info *inode)
12011c6fdbd8SKent Overstreet {
12029a3df993SKent Overstreet 	return (struct bch_writepage_state) {
12039a3df993SKent Overstreet 		.opts = io_opts(c, &inode->ei_inode)
12049a3df993SKent Overstreet 	};
12051c6fdbd8SKent Overstreet }
12061c6fdbd8SKent Overstreet 
12079f311f21SKent Overstreet static void bch2_writepage_io_done(struct bch_write_op *op)
12081c6fdbd8SKent Overstreet {
12099f311f21SKent Overstreet 	struct bch_writepage_io *io =
12109f311f21SKent Overstreet 		container_of(op, struct bch_writepage_io, op);
12119a3df993SKent Overstreet 	struct bch_fs *c = io->op.c;
12129a3df993SKent Overstreet 	struct bio *bio = &io->op.wbio.bio;
12131c6fdbd8SKent Overstreet 	struct bvec_iter_all iter;
12141c6fdbd8SKent Overstreet 	struct bio_vec *bvec;
1215b3fce09cSKent Overstreet 	unsigned i;
12161c6fdbd8SKent Overstreet 
1217ef1b2092SKent Overstreet 	up(&io->op.c->io_in_flight);
1218ef1b2092SKent Overstreet 
12199a3df993SKent Overstreet 	if (io->op.error) {
122033c74e41SKent Overstreet 		set_bit(EI_INODE_ERROR, &io->inode->ei_flags);
122133c74e41SKent Overstreet 
122275812e70SKent Overstreet 		bio_for_each_segment_all(bvec, bio, iter) {
1223b3fce09cSKent Overstreet 			struct bch_page_state *s;
1224b3fce09cSKent Overstreet 
12251c6fdbd8SKent Overstreet 			SetPageError(bvec->bv_page);
122675812e70SKent Overstreet 			mapping_set_error(bvec->bv_page->mapping, -EIO);
1227b3fce09cSKent Overstreet 
12283826ee0bSKent Overstreet 			s = __bch2_page_state(bvec->bv_page);
12293826ee0bSKent Overstreet 			spin_lock(&s->lock);
1230b3fce09cSKent Overstreet 			for (i = 0; i < PAGE_SECTORS; i++)
1231b3fce09cSKent Overstreet 				s->s[i].nr_replicas = 0;
12323826ee0bSKent Overstreet 			spin_unlock(&s->lock);
123375812e70SKent Overstreet 		}
12341c6fdbd8SKent Overstreet 	}
12351c6fdbd8SKent Overstreet 
12364be1a412SKent Overstreet 	if (io->op.flags & BCH_WRITE_WROTE_DATA_INLINE) {
12374be1a412SKent Overstreet 		bio_for_each_segment_all(bvec, bio, iter) {
12384be1a412SKent Overstreet 			struct bch_page_state *s;
12394be1a412SKent Overstreet 
12404be1a412SKent Overstreet 			s = __bch2_page_state(bvec->bv_page);
12414be1a412SKent Overstreet 			spin_lock(&s->lock);
12424be1a412SKent Overstreet 			for (i = 0; i < PAGE_SECTORS; i++)
12434be1a412SKent Overstreet 				s->s[i].nr_replicas = 0;
12444be1a412SKent Overstreet 			spin_unlock(&s->lock);
12454be1a412SKent Overstreet 		}
12464be1a412SKent Overstreet 	}
12474be1a412SKent Overstreet 
12481c6fdbd8SKent Overstreet 	/*
12491c6fdbd8SKent Overstreet 	 * racing with fallocate can cause us to add fewer sectors than
12501c6fdbd8SKent Overstreet 	 * expected - but we shouldn't add more sectors than expected:
12511c6fdbd8SKent Overstreet 	 */
12529a3df993SKent Overstreet 	BUG_ON(io->op.i_sectors_delta > 0);
12531c6fdbd8SKent Overstreet 
12541c6fdbd8SKent Overstreet 	/*
12551c6fdbd8SKent Overstreet 	 * (error (due to going RO) halfway through a page can screw that up
12561c6fdbd8SKent Overstreet 	 * slightly)
12571c6fdbd8SKent Overstreet 	 * XXX wtf?
12589a3df993SKent Overstreet 	   BUG_ON(io->op.op.i_sectors_delta >= PAGE_SECTORS);
12591c6fdbd8SKent Overstreet 	 */
12601c6fdbd8SKent Overstreet 
12611c6fdbd8SKent Overstreet 	/*
12621c6fdbd8SKent Overstreet 	 * PageWriteback is effectively our ref on the inode - fixup i_blocks
12631c6fdbd8SKent Overstreet 	 * before calling end_page_writeback:
12641c6fdbd8SKent Overstreet 	 */
12659a3df993SKent Overstreet 	i_sectors_acct(c, io->inode, NULL, io->op.i_sectors_delta);
12661c6fdbd8SKent Overstreet 
12677f5e31e1SKent Overstreet 	bio_for_each_segment_all(bvec, bio, iter) {
12687f5e31e1SKent Overstreet 		struct bch_page_state *s = __bch2_page_state(bvec->bv_page);
12697f5e31e1SKent Overstreet 
12707f5e31e1SKent Overstreet 		if (atomic_dec_and_test(&s->write_count))
12711c6fdbd8SKent Overstreet 			end_page_writeback(bvec->bv_page);
12727f5e31e1SKent Overstreet 	}
12731c6fdbd8SKent Overstreet 
12749f311f21SKent Overstreet 	bio_put(&io->op.wbio.bio);
12751c6fdbd8SKent Overstreet }
12761c6fdbd8SKent Overstreet 
12771c6fdbd8SKent Overstreet static void bch2_writepage_do_io(struct bch_writepage_state *w)
12781c6fdbd8SKent Overstreet {
12791c6fdbd8SKent Overstreet 	struct bch_writepage_io *io = w->io;
12801c6fdbd8SKent Overstreet 
1281ef1b2092SKent Overstreet 	down(&io->op.c->io_in_flight);
1282ef1b2092SKent Overstreet 
12831c6fdbd8SKent Overstreet 	w->io = NULL;
12849f311f21SKent Overstreet 	closure_call(&io->op.cl, bch2_write, NULL, NULL);
12851c6fdbd8SKent Overstreet }
12861c6fdbd8SKent Overstreet 
12871c6fdbd8SKent Overstreet /*
12881c6fdbd8SKent Overstreet  * Get a bch_writepage_io and add @page to it - appending to an existing one if
12891c6fdbd8SKent Overstreet  * possible, else allocating a new one:
12901c6fdbd8SKent Overstreet  */
12911c6fdbd8SKent Overstreet static void bch2_writepage_io_alloc(struct bch_fs *c,
129250fe5bd6SKent Overstreet 				    struct writeback_control *wbc,
12931c6fdbd8SKent Overstreet 				    struct bch_writepage_state *w,
12941c6fdbd8SKent Overstreet 				    struct bch_inode_info *inode,
12957f5e31e1SKent Overstreet 				    u64 sector,
12961c6fdbd8SKent Overstreet 				    unsigned nr_replicas)
12971c6fdbd8SKent Overstreet {
12981c6fdbd8SKent Overstreet 	struct bch_write_op *op;
12991c6fdbd8SKent Overstreet 
13001c6fdbd8SKent Overstreet 	w->io = container_of(bio_alloc_bioset(NULL, BIO_MAX_VECS,
13011c6fdbd8SKent Overstreet 					      REQ_OP_WRITE,
13021c6fdbd8SKent Overstreet 					      GFP_NOFS,
13031c6fdbd8SKent Overstreet 					      &c->writepage_bioset),
13049a3df993SKent Overstreet 			     struct bch_writepage_io, op.wbio.bio);
13051c6fdbd8SKent Overstreet 
13069a3df993SKent Overstreet 	w->io->inode		= inode;
13079a3df993SKent Overstreet 	op			= &w->io->op;
13089a3df993SKent Overstreet 	bch2_write_op_init(op, c, w->opts);
13099a3df993SKent Overstreet 	op->target		= w->opts.foreground_target;
13101c6fdbd8SKent Overstreet 	op->nr_replicas		= nr_replicas;
13111c6fdbd8SKent Overstreet 	op->res.nr_replicas	= nr_replicas;
13121c6fdbd8SKent Overstreet 	op->write_point		= writepoint_hashed(inode->ei_last_dirtied);
13138c6d298aSKent Overstreet 	op->subvol		= inode->ei_subvol;
13147f5e31e1SKent Overstreet 	op->pos			= POS(inode->v.i_ino, sector);
13159f311f21SKent Overstreet 	op->end_io		= bch2_writepage_io_done;
13167f5e31e1SKent Overstreet 	op->wbio.bio.bi_iter.bi_sector = sector;
131750fe5bd6SKent Overstreet 	op->wbio.bio.bi_opf	= wbc_to_write_flags(wbc);
13181c6fdbd8SKent Overstreet }
13191c6fdbd8SKent Overstreet 
13201c6fdbd8SKent Overstreet static int __bch2_writepage(struct folio *folio,
13211c6fdbd8SKent Overstreet 			    struct writeback_control *wbc,
13221c6fdbd8SKent Overstreet 			    void *data)
13231c6fdbd8SKent Overstreet {
13241c6fdbd8SKent Overstreet 	struct page *page = &folio->page;
13251c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = to_bch_ei(page->mapping->host);
13261c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
13271c6fdbd8SKent Overstreet 	struct bch_writepage_state *w = data;
13287f5e31e1SKent Overstreet 	struct bch_page_state *s, orig;
13297f5e31e1SKent Overstreet 	unsigned i, offset, nr_replicas_this_write = U32_MAX;
13301c6fdbd8SKent Overstreet 	loff_t i_size = i_size_read(&inode->v);
13311c6fdbd8SKent Overstreet 	pgoff_t end_index = i_size >> PAGE_SHIFT;
1332e1036a2aSKent Overstreet 	int ret;
13331c6fdbd8SKent Overstreet 
13341c6fdbd8SKent Overstreet 	EBUG_ON(!PageUptodate(page));
13351c6fdbd8SKent Overstreet 
13361c6fdbd8SKent Overstreet 	/* Is the page fully inside i_size? */
13371c6fdbd8SKent Overstreet 	if (page->index < end_index)
13381c6fdbd8SKent Overstreet 		goto do_io;
13391c6fdbd8SKent Overstreet 
13401c6fdbd8SKent Overstreet 	/* Is the page fully outside i_size? (truncate in progress) */
13411c6fdbd8SKent Overstreet 	offset = i_size & (PAGE_SIZE - 1);
13421c6fdbd8SKent Overstreet 	if (page->index > end_index || !offset) {
13431c6fdbd8SKent Overstreet 		unlock_page(page);
13441c6fdbd8SKent Overstreet 		return 0;
13451c6fdbd8SKent Overstreet 	}
13461c6fdbd8SKent Overstreet 
13471c6fdbd8SKent Overstreet 	/*
13481c6fdbd8SKent Overstreet 	 * The page straddles i_size.  It must be zeroed out on each and every
13491c6fdbd8SKent Overstreet 	 * writepage invocation because it may be mmapped.  "A file is mapped
13501c6fdbd8SKent Overstreet 	 * in multiples of the page size.  For a file that is not a multiple of
13511c6fdbd8SKent Overstreet 	 * the  page size, the remaining memory is zeroed when mapped, and
13521c6fdbd8SKent Overstreet 	 * writes to that region are not written out to the file."
13531c6fdbd8SKent Overstreet 	 */
13541c6fdbd8SKent Overstreet 	zero_user_segment(page, offset, PAGE_SIZE);
13551c6fdbd8SKent Overstreet do_io:
1356f57a6a5dSKent Overstreet 	s = bch2_page_state_create(page, __GFP_NOFAIL);
1357f81b648dSKent Overstreet 
1358f74a5051SKent Overstreet 	/*
1359f74a5051SKent Overstreet 	 * Things get really hairy with errors during writeback:
1360f74a5051SKent Overstreet 	 */
1361f74a5051SKent Overstreet 	ret = bch2_get_page_disk_reservation(c, inode, page, false);
1362f74a5051SKent Overstreet 	BUG_ON(ret);
1363e1036a2aSKent Overstreet 
13647f5e31e1SKent Overstreet 	/* Before unlocking the page, get copy of reservations: */
1365f74a5051SKent Overstreet 	spin_lock(&s->lock);
13667f5e31e1SKent Overstreet 	orig = *s;
1367f74a5051SKent Overstreet 	spin_unlock(&s->lock);
13687f5e31e1SKent Overstreet 
13697f5e31e1SKent Overstreet 	for (i = 0; i < PAGE_SECTORS; i++) {
13702ba5d38bSKent Overstreet 		if (s->s[i].state < SECTOR_DIRTY)
13717f5e31e1SKent Overstreet 			continue;
13727f5e31e1SKent Overstreet 
1373f81b648dSKent Overstreet 		nr_replicas_this_write =
1374f57a6a5dSKent Overstreet 			min_t(unsigned, nr_replicas_this_write,
1375f57a6a5dSKent Overstreet 			      s->s[i].nr_replicas +
1376f57a6a5dSKent Overstreet 			      s->s[i].replicas_reserved);
13777f5e31e1SKent Overstreet 	}
13781c6fdbd8SKent Overstreet 
1379f57a6a5dSKent Overstreet 	for (i = 0; i < PAGE_SECTORS; i++) {
13802ba5d38bSKent Overstreet 		if (s->s[i].state < SECTOR_DIRTY)
13817f5e31e1SKent Overstreet 			continue;
13827f5e31e1SKent Overstreet 
1383f57a6a5dSKent Overstreet 		s->s[i].nr_replicas = w->opts.compression
1384f57a6a5dSKent Overstreet 			? 0 : nr_replicas_this_write;
1385e1036a2aSKent Overstreet 
1386f57a6a5dSKent Overstreet 		s->s[i].replicas_reserved = 0;
1387f57a6a5dSKent Overstreet 		s->s[i].state = SECTOR_ALLOCATED;
1388f57a6a5dSKent Overstreet 	}
13891c6fdbd8SKent Overstreet 
13907f5e31e1SKent Overstreet 	BUG_ON(atomic_read(&s->write_count));
13917f5e31e1SKent Overstreet 	atomic_set(&s->write_count, 1);
13927f5e31e1SKent Overstreet 
13931c6fdbd8SKent Overstreet 	BUG_ON(PageWriteback(page));
13941c6fdbd8SKent Overstreet 	set_page_writeback(page);
13957f5e31e1SKent Overstreet 
13961c6fdbd8SKent Overstreet 	unlock_page(page);
13971c6fdbd8SKent Overstreet 
13987f5e31e1SKent Overstreet 	offset = 0;
13997f5e31e1SKent Overstreet 	while (1) {
1400f74a5051SKent Overstreet 		unsigned sectors = 0, dirty_sectors = 0, reserved_sectors = 0;
14017f5e31e1SKent Overstreet 		u64 sector;
14027f5e31e1SKent Overstreet 
14037f5e31e1SKent Overstreet 		while (offset < PAGE_SECTORS &&
14042ba5d38bSKent Overstreet 		       orig.s[offset].state < SECTOR_DIRTY)
14057f5e31e1SKent Overstreet 			offset++;
14067f5e31e1SKent Overstreet 
14077f5e31e1SKent Overstreet 		if (offset == PAGE_SECTORS)
14087f5e31e1SKent Overstreet 			break;
14097f5e31e1SKent Overstreet 
14107f5e31e1SKent Overstreet 		while (offset + sectors < PAGE_SECTORS &&
1411f74a5051SKent Overstreet 		       orig.s[offset + sectors].state >= SECTOR_DIRTY) {
1412f74a5051SKent Overstreet 			reserved_sectors += orig.s[offset + sectors].replicas_reserved;
1413f74a5051SKent Overstreet 			dirty_sectors += orig.s[offset + sectors].state == SECTOR_DIRTY;
14147f5e31e1SKent Overstreet 			sectors++;
14157f5e31e1SKent Overstreet 		}
1416f74a5051SKent Overstreet 		BUG_ON(!sectors);
1417f74a5051SKent Overstreet 
14187279c1a2SKent Overstreet 		sector = ((u64) page->index << PAGE_SECTORS_SHIFT) + offset;
14197f5e31e1SKent Overstreet 
14201c6fdbd8SKent Overstreet 		if (w->io &&
14219a3df993SKent Overstreet 		    (w->io->op.res.nr_replicas != nr_replicas_this_write ||
14229a3df993SKent Overstreet 		     bio_full(&w->io->op.wbio.bio, PAGE_SIZE) ||
1423f59b3464SKent Overstreet 		     w->io->op.wbio.bio.bi_iter.bi_size + (sectors << 9) >=
1424f59b3464SKent Overstreet 		     (BIO_MAX_VECS * PAGE_SIZE) ||
14259a3df993SKent Overstreet 		     bio_end_sector(&w->io->op.wbio.bio) != sector))
14261c6fdbd8SKent Overstreet 			bch2_writepage_do_io(w);
14271c6fdbd8SKent Overstreet 
14281c6fdbd8SKent Overstreet 		if (!w->io)
142950fe5bd6SKent Overstreet 			bch2_writepage_io_alloc(c, wbc, w, inode, sector,
1430f81b648dSKent Overstreet 						nr_replicas_this_write);
14311c6fdbd8SKent Overstreet 
14327f5e31e1SKent Overstreet 		atomic_inc(&s->write_count);
14337f5e31e1SKent Overstreet 
14349a3df993SKent Overstreet 		BUG_ON(inode != w->io->inode);
14359a3df993SKent Overstreet 		BUG_ON(!bio_add_page(&w->io->op.wbio.bio, page,
14367f5e31e1SKent Overstreet 				     sectors << 9, offset << 9));
14371c6fdbd8SKent Overstreet 
14386cc3535dSKent Overstreet 		/* Check for writing past i_size: */
14399a3df993SKent Overstreet 		WARN_ON((bio_end_sector(&w->io->op.wbio.bio) << 9) >
14406cc3535dSKent Overstreet 			round_up(i_size, block_bytes(c)));
14416cc3535dSKent Overstreet 
14429a3df993SKent Overstreet 		w->io->op.res.sectors += reserved_sectors;
14439a3df993SKent Overstreet 		w->io->op.i_sectors_delta -= dirty_sectors;
14441c6fdbd8SKent Overstreet 		w->io->op.new_i_size = i_size;
14451c6fdbd8SKent Overstreet 
14467f5e31e1SKent Overstreet 		offset += sectors;
14477f5e31e1SKent Overstreet 	}
14487f5e31e1SKent Overstreet 
14497f5e31e1SKent Overstreet 	if (atomic_dec_and_test(&s->write_count))
14507f5e31e1SKent Overstreet 		end_page_writeback(page);
14517f5e31e1SKent Overstreet 
14521c6fdbd8SKent Overstreet 	return 0;
14531c6fdbd8SKent Overstreet }
14541c6fdbd8SKent Overstreet 
14551c6fdbd8SKent Overstreet int bch2_writepages(struct address_space *mapping, struct writeback_control *wbc)
14561c6fdbd8SKent Overstreet {
14571c6fdbd8SKent Overstreet 	struct bch_fs *c = mapping->host->i_sb->s_fs_info;
14581c6fdbd8SKent Overstreet 	struct bch_writepage_state w =
14591c6fdbd8SKent Overstreet 		bch_writepage_state_init(c, to_bch_ei(mapping->host));
14601c6fdbd8SKent Overstreet 	struct blk_plug plug;
14611c6fdbd8SKent Overstreet 	int ret;
14621c6fdbd8SKent Overstreet 
14631c6fdbd8SKent Overstreet 	blk_start_plug(&plug);
14641c6fdbd8SKent Overstreet 	ret = write_cache_pages(mapping, wbc, __bch2_writepage, &w);
14651c6fdbd8SKent Overstreet 	if (w.io)
14661c6fdbd8SKent Overstreet 		bch2_writepage_do_io(&w);
14671c6fdbd8SKent Overstreet 	blk_finish_plug(&plug);
14681c6fdbd8SKent Overstreet 	return ret;
14691c6fdbd8SKent Overstreet }
14701c6fdbd8SKent Overstreet 
14711c6fdbd8SKent Overstreet int bch2_writepage(struct page *page, struct writeback_control *wbc)
14721c6fdbd8SKent Overstreet {
14731c6fdbd8SKent Overstreet 	struct bch_fs *c = page->mapping->host->i_sb->s_fs_info;
14741c6fdbd8SKent Overstreet 	struct bch_writepage_state w =
14751c6fdbd8SKent Overstreet 		bch_writepage_state_init(c, to_bch_ei(page->mapping->host));
14761c6fdbd8SKent Overstreet 	int ret;
14771c6fdbd8SKent Overstreet 
14781c6fdbd8SKent Overstreet 	ret = __bch2_writepage(page_folio(page), wbc, &w);
14791c6fdbd8SKent Overstreet 	if (w.io)
14801c6fdbd8SKent Overstreet 		bch2_writepage_do_io(&w);
14811c6fdbd8SKent Overstreet 
14821c6fdbd8SKent Overstreet 	return ret;
14831c6fdbd8SKent Overstreet }
14841c6fdbd8SKent Overstreet 
14851c6fdbd8SKent Overstreet /* buffered writes: */
14861c6fdbd8SKent Overstreet 
14871c6fdbd8SKent Overstreet int bch2_write_begin(struct file *file, struct address_space *mapping,
14881c6fdbd8SKent Overstreet 		     loff_t pos, unsigned len,
14891c6fdbd8SKent Overstreet 		     struct page **pagep, void **fsdata)
14901c6fdbd8SKent Overstreet {
14911c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = to_bch_ei(mapping->host);
14921c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
1493d1542e03SKent Overstreet 	struct bch2_page_reservation *res;
14941c6fdbd8SKent Overstreet 	pgoff_t index = pos >> PAGE_SHIFT;
14951c6fdbd8SKent Overstreet 	unsigned offset = pos & (PAGE_SIZE - 1);
14961c6fdbd8SKent Overstreet 	struct page *page;
14971c6fdbd8SKent Overstreet 	int ret = -ENOMEM;
14981c6fdbd8SKent Overstreet 
1499d1542e03SKent Overstreet 	res = kmalloc(sizeof(*res), GFP_KERNEL);
1500d1542e03SKent Overstreet 	if (!res)
1501d1542e03SKent Overstreet 		return -ENOMEM;
1502d1542e03SKent Overstreet 
1503d1542e03SKent Overstreet 	bch2_page_reservation_init(c, inode, res);
1504d1542e03SKent Overstreet 	*fsdata = res;
15051c6fdbd8SKent Overstreet 
15061c6fdbd8SKent Overstreet 	bch2_pagecache_add_get(&inode->ei_pagecache_lock);
15071c6fdbd8SKent Overstreet 
15081c6fdbd8SKent Overstreet 	page = grab_cache_page_write_begin(mapping, index);
15091c6fdbd8SKent Overstreet 	if (!page)
15101c6fdbd8SKent Overstreet 		goto err_unlock;
15111c6fdbd8SKent Overstreet 
15121c6fdbd8SKent Overstreet 	if (PageUptodate(page))
15131c6fdbd8SKent Overstreet 		goto out;
15141c6fdbd8SKent Overstreet 
15151c6fdbd8SKent Overstreet 	/* If we're writing entire page, don't need to read it in first: */
15161c6fdbd8SKent Overstreet 	if (len == PAGE_SIZE)
15171c6fdbd8SKent Overstreet 		goto out;
15181c6fdbd8SKent Overstreet 
15191c6fdbd8SKent Overstreet 	if (!offset && pos + len >= inode->v.i_size) {
15201c6fdbd8SKent Overstreet 		zero_user_segment(page, len, PAGE_SIZE);
15211c6fdbd8SKent Overstreet 		flush_dcache_page(page);
15221c6fdbd8SKent Overstreet 		goto out;
15231c6fdbd8SKent Overstreet 	}
15241c6fdbd8SKent Overstreet 
15251c6fdbd8SKent Overstreet 	if (index > inode->v.i_size >> PAGE_SHIFT) {
15261c6fdbd8SKent Overstreet 		zero_user_segments(page, 0, offset, offset + len, PAGE_SIZE);
15271c6fdbd8SKent Overstreet 		flush_dcache_page(page);
15281c6fdbd8SKent Overstreet 		goto out;
15291c6fdbd8SKent Overstreet 	}
15301c6fdbd8SKent Overstreet readpage:
15311c6fdbd8SKent Overstreet 	ret = bch2_read_single_page(page, mapping);
15321c6fdbd8SKent Overstreet 	if (ret)
15331c6fdbd8SKent Overstreet 		goto err;
15341c6fdbd8SKent Overstreet out:
1535e6ec361fSKent Overstreet 	if (!bch2_page_state_create(page, __GFP_NOFAIL)->uptodate) {
1536e6ec361fSKent Overstreet 		ret = bch2_page_state_set(c, inode_inum(inode), &page, 1);
1537e6ec361fSKent Overstreet 		if (ret)
1538e6ec361fSKent Overstreet 			goto out;
1539e6ec361fSKent Overstreet 	}
1540e6ec361fSKent Overstreet 
1541d1542e03SKent Overstreet 	ret = bch2_page_reservation_get(c, inode, page, res,
1542d1542e03SKent Overstreet 					offset, len, true);
15431c6fdbd8SKent Overstreet 	if (ret) {
15441c6fdbd8SKent Overstreet 		if (!PageUptodate(page)) {
15451c6fdbd8SKent Overstreet 			/*
15461c6fdbd8SKent Overstreet 			 * If the page hasn't been read in, we won't know if we
15471c6fdbd8SKent Overstreet 			 * actually need a reservation - we don't actually need
15481c6fdbd8SKent Overstreet 			 * to read here, we just need to check if the page is
15491c6fdbd8SKent Overstreet 			 * fully backed by uncompressed data:
15501c6fdbd8SKent Overstreet 			 */
15511c6fdbd8SKent Overstreet 			goto readpage;
15521c6fdbd8SKent Overstreet 		}
15531c6fdbd8SKent Overstreet 
15541c6fdbd8SKent Overstreet 		goto err;
15551c6fdbd8SKent Overstreet 	}
15561c6fdbd8SKent Overstreet 
15571c6fdbd8SKent Overstreet 	*pagep = page;
15581c6fdbd8SKent Overstreet 	return 0;
15591c6fdbd8SKent Overstreet err:
15601c6fdbd8SKent Overstreet 	unlock_page(page);
15611c6fdbd8SKent Overstreet 	put_page(page);
15621c6fdbd8SKent Overstreet 	*pagep = NULL;
15631c6fdbd8SKent Overstreet err_unlock:
15641c6fdbd8SKent Overstreet 	bch2_pagecache_add_put(&inode->ei_pagecache_lock);
1565d1542e03SKent Overstreet 	kfree(res);
1566d1542e03SKent Overstreet 	*fsdata = NULL;
15671c6fdbd8SKent Overstreet 	return ret;
15681c6fdbd8SKent Overstreet }
15691c6fdbd8SKent Overstreet 
15701c6fdbd8SKent Overstreet int bch2_write_end(struct file *file, struct address_space *mapping,
15711c6fdbd8SKent Overstreet 		   loff_t pos, unsigned len, unsigned copied,
15721c6fdbd8SKent Overstreet 		   struct page *page, void *fsdata)
15731c6fdbd8SKent Overstreet {
15741c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = to_bch_ei(mapping->host);
15751c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
1576d1542e03SKent Overstreet 	struct bch2_page_reservation *res = fsdata;
1577d1542e03SKent Overstreet 	unsigned offset = pos & (PAGE_SIZE - 1);
15781c6fdbd8SKent Overstreet 
15791c6fdbd8SKent Overstreet 	lockdep_assert_held(&inode->v.i_rwsem);
15801c6fdbd8SKent Overstreet 
15811c6fdbd8SKent Overstreet 	if (unlikely(copied < len && !PageUptodate(page))) {
15821c6fdbd8SKent Overstreet 		/*
15831c6fdbd8SKent Overstreet 		 * The page needs to be read in, but that would destroy
15841c6fdbd8SKent Overstreet 		 * our partial write - simplest thing is to just force
15851c6fdbd8SKent Overstreet 		 * userspace to redo the write:
15861c6fdbd8SKent Overstreet 		 */
15871c6fdbd8SKent Overstreet 		zero_user(page, 0, PAGE_SIZE);
15881c6fdbd8SKent Overstreet 		flush_dcache_page(page);
15891c6fdbd8SKent Overstreet 		copied = 0;
15901c6fdbd8SKent Overstreet 	}
15911c6fdbd8SKent Overstreet 
15921c6fdbd8SKent Overstreet 	spin_lock(&inode->v.i_lock);
15931c6fdbd8SKent Overstreet 	if (pos + copied > inode->v.i_size)
15941c6fdbd8SKent Overstreet 		i_size_write(&inode->v, pos + copied);
15951c6fdbd8SKent Overstreet 	spin_unlock(&inode->v.i_lock);
15961c6fdbd8SKent Overstreet 
15971c6fdbd8SKent Overstreet 	if (copied) {
15981c6fdbd8SKent Overstreet 		if (!PageUptodate(page))
15991c6fdbd8SKent Overstreet 			SetPageUptodate(page);
1600d1542e03SKent Overstreet 
1601d1542e03SKent Overstreet 		bch2_set_page_dirty(c, inode, page, res, offset, copied);
16021c6fdbd8SKent Overstreet 
16031c6fdbd8SKent Overstreet 		inode->ei_last_dirtied = (unsigned long) current;
16041c6fdbd8SKent Overstreet 	}
16051c6fdbd8SKent Overstreet 
16061c6fdbd8SKent Overstreet 	unlock_page(page);
16071c6fdbd8SKent Overstreet 	put_page(page);
16081c6fdbd8SKent Overstreet 	bch2_pagecache_add_put(&inode->ei_pagecache_lock);
16091c6fdbd8SKent Overstreet 
1610d1542e03SKent Overstreet 	bch2_page_reservation_put(c, inode, res);
1611d1542e03SKent Overstreet 	kfree(res);
1612d1542e03SKent Overstreet 
16131c6fdbd8SKent Overstreet 	return copied;
16141c6fdbd8SKent Overstreet }
16151c6fdbd8SKent Overstreet 
16161c6fdbd8SKent Overstreet #define WRITE_BATCH_PAGES	32
16171c6fdbd8SKent Overstreet 
16181c6fdbd8SKent Overstreet static int __bch2_buffered_write(struct bch_inode_info *inode,
16191c6fdbd8SKent Overstreet 				 struct address_space *mapping,
16201c6fdbd8SKent Overstreet 				 struct iov_iter *iter,
16211c6fdbd8SKent Overstreet 				 loff_t pos, unsigned len)
16221c6fdbd8SKent Overstreet {
16231c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
16241c6fdbd8SKent Overstreet 	struct page *pages[WRITE_BATCH_PAGES];
1625d1542e03SKent Overstreet 	struct bch2_page_reservation res;
16261c6fdbd8SKent Overstreet 	unsigned long index = pos >> PAGE_SHIFT;
16271c6fdbd8SKent Overstreet 	unsigned offset = pos & (PAGE_SIZE - 1);
16281c6fdbd8SKent Overstreet 	unsigned nr_pages = DIV_ROUND_UP(offset + len, PAGE_SIZE);
1629d1542e03SKent Overstreet 	unsigned i, reserved = 0, set_dirty = 0;
1630d1542e03SKent Overstreet 	unsigned copied = 0, nr_pages_copied = 0;
16311c6fdbd8SKent Overstreet 	int ret = 0;
16321c6fdbd8SKent Overstreet 
16331c6fdbd8SKent Overstreet 	BUG_ON(!len);
16341c6fdbd8SKent Overstreet 	BUG_ON(nr_pages > ARRAY_SIZE(pages));
16351c6fdbd8SKent Overstreet 
1636d1542e03SKent Overstreet 	bch2_page_reservation_init(c, inode, &res);
1637d1542e03SKent Overstreet 
16381c6fdbd8SKent Overstreet 	for (i = 0; i < nr_pages; i++) {
16391c6fdbd8SKent Overstreet 		pages[i] = grab_cache_page_write_begin(mapping, index + i);
16401c6fdbd8SKent Overstreet 		if (!pages[i]) {
16411c6fdbd8SKent Overstreet 			nr_pages = i;
16428de819f8SKent Overstreet 			if (!i) {
16431c6fdbd8SKent Overstreet 				ret = -ENOMEM;
16441c6fdbd8SKent Overstreet 				goto out;
16451c6fdbd8SKent Overstreet 			}
16468de819f8SKent Overstreet 			len = min_t(unsigned, len,
16478de819f8SKent Overstreet 				    nr_pages * PAGE_SIZE - offset);
16488de819f8SKent Overstreet 			break;
16498de819f8SKent Overstreet 		}
16501c6fdbd8SKent Overstreet 	}
16511c6fdbd8SKent Overstreet 
16521c6fdbd8SKent Overstreet 	if (offset && !PageUptodate(pages[0])) {
16531c6fdbd8SKent Overstreet 		ret = bch2_read_single_page(pages[0], mapping);
16541c6fdbd8SKent Overstreet 		if (ret)
16551c6fdbd8SKent Overstreet 			goto out;
16561c6fdbd8SKent Overstreet 	}
16571c6fdbd8SKent Overstreet 
16581c6fdbd8SKent Overstreet 	if ((pos + len) & (PAGE_SIZE - 1) &&
16591c6fdbd8SKent Overstreet 	    !PageUptodate(pages[nr_pages - 1])) {
16601c6fdbd8SKent Overstreet 		if ((index + nr_pages - 1) << PAGE_SHIFT >= inode->v.i_size) {
16611c6fdbd8SKent Overstreet 			zero_user(pages[nr_pages - 1], 0, PAGE_SIZE);
16621c6fdbd8SKent Overstreet 		} else {
16631c6fdbd8SKent Overstreet 			ret = bch2_read_single_page(pages[nr_pages - 1], mapping);
16641c6fdbd8SKent Overstreet 			if (ret)
16651c6fdbd8SKent Overstreet 				goto out;
16661c6fdbd8SKent Overstreet 		}
16671c6fdbd8SKent Overstreet 	}
16681c6fdbd8SKent Overstreet 
1669d1542e03SKent Overstreet 	while (reserved < len) {
1670e6ec361fSKent Overstreet 		unsigned i = (offset + reserved) >> PAGE_SHIFT;
1671e6ec361fSKent Overstreet 		struct page *page = pages[i];
1672d1542e03SKent Overstreet 		unsigned pg_offset = (offset + reserved) & (PAGE_SIZE - 1);
1673d1542e03SKent Overstreet 		unsigned pg_len = min_t(unsigned, len - reserved,
1674d1542e03SKent Overstreet 					PAGE_SIZE - pg_offset);
16751c6fdbd8SKent Overstreet 
1676e6ec361fSKent Overstreet 		if (!bch2_page_state_create(page, __GFP_NOFAIL)->uptodate) {
1677e6ec361fSKent Overstreet 			ret = bch2_page_state_set(c, inode_inum(inode),
1678e6ec361fSKent Overstreet 						  pages + i, nr_pages - i);
1679e6ec361fSKent Overstreet 			if (ret)
1680e6ec361fSKent Overstreet 				goto out;
16811c6fdbd8SKent Overstreet 		}
16821c6fdbd8SKent Overstreet 
1683e6ec361fSKent Overstreet 		ret = bch2_page_reservation_get(c, inode, page, &res,
1684e6ec361fSKent Overstreet 						pg_offset, pg_len, true);
16851c6fdbd8SKent Overstreet 		if (ret)
16861c6fdbd8SKent Overstreet 			goto out;
1687d1542e03SKent Overstreet 
1688d1542e03SKent Overstreet 		reserved += pg_len;
16891c6fdbd8SKent Overstreet 	}
16901c6fdbd8SKent Overstreet 
16911c6fdbd8SKent Overstreet 	if (mapping_writably_mapped(mapping))
16921c6fdbd8SKent Overstreet 		for (i = 0; i < nr_pages; i++)
16931c6fdbd8SKent Overstreet 			flush_dcache_page(pages[i]);
16941c6fdbd8SKent Overstreet 
16951c6fdbd8SKent Overstreet 	while (copied < len) {
16961c6fdbd8SKent Overstreet 		struct page *page = pages[(offset + copied) >> PAGE_SHIFT];
16971c6fdbd8SKent Overstreet 		unsigned pg_offset = (offset + copied) & (PAGE_SIZE - 1);
1698d1542e03SKent Overstreet 		unsigned pg_len = min_t(unsigned, len - copied,
16991c6fdbd8SKent Overstreet 					PAGE_SIZE - pg_offset);
17001c6fdbd8SKent Overstreet 		unsigned pg_copied = copy_page_from_iter_atomic(page,
1701d1542e03SKent Overstreet 						pg_offset, pg_len, iter);
1702d1542e03SKent Overstreet 
1703d1542e03SKent Overstreet 		if (!pg_copied)
1704d1542e03SKent Overstreet 			break;
17051c6fdbd8SKent Overstreet 
1706912bdf17SKent Overstreet 		if (!PageUptodate(page) &&
1707912bdf17SKent Overstreet 		    pg_copied != PAGE_SIZE &&
1708912bdf17SKent Overstreet 		    pos + copied + pg_copied < inode->v.i_size) {
1709912bdf17SKent Overstreet 			zero_user(page, 0, PAGE_SIZE);
1710912bdf17SKent Overstreet 			break;
1711912bdf17SKent Overstreet 		}
1712912bdf17SKent Overstreet 
17131c6fdbd8SKent Overstreet 		flush_dcache_page(page);
17141c6fdbd8SKent Overstreet 		copied += pg_copied;
1715912bdf17SKent Overstreet 
1716912bdf17SKent Overstreet 		if (pg_copied != pg_len)
1717912bdf17SKent Overstreet 			break;
17181c6fdbd8SKent Overstreet 	}
17191c6fdbd8SKent Overstreet 
17201c6fdbd8SKent Overstreet 	if (!copied)
17211c6fdbd8SKent Overstreet 		goto out;
17221c6fdbd8SKent Overstreet 
1723877dfb34SKent Overstreet 	spin_lock(&inode->v.i_lock);
1724877dfb34SKent Overstreet 	if (pos + copied > inode->v.i_size)
1725877dfb34SKent Overstreet 		i_size_write(&inode->v, pos + copied);
1726877dfb34SKent Overstreet 	spin_unlock(&inode->v.i_lock);
1727877dfb34SKent Overstreet 
1728d1542e03SKent Overstreet 	while (set_dirty < copied) {
1729d1542e03SKent Overstreet 		struct page *page = pages[(offset + set_dirty) >> PAGE_SHIFT];
1730d1542e03SKent Overstreet 		unsigned pg_offset = (offset + set_dirty) & (PAGE_SIZE - 1);
1731d1542e03SKent Overstreet 		unsigned pg_len = min_t(unsigned, copied - set_dirty,
1732d1542e03SKent Overstreet 					PAGE_SIZE - pg_offset);
1733d1542e03SKent Overstreet 
1734d1542e03SKent Overstreet 		if (!PageUptodate(page))
1735d1542e03SKent Overstreet 			SetPageUptodate(page);
1736d1542e03SKent Overstreet 
1737d1542e03SKent Overstreet 		bch2_set_page_dirty(c, inode, page, &res, pg_offset, pg_len);
1738d1542e03SKent Overstreet 		unlock_page(page);
1739d1542e03SKent Overstreet 		put_page(page);
1740d1542e03SKent Overstreet 
1741d1542e03SKent Overstreet 		set_dirty += pg_len;
1742d1542e03SKent Overstreet 	}
1743877dfb34SKent Overstreet 
1744877dfb34SKent Overstreet 	nr_pages_copied = DIV_ROUND_UP(offset + copied, PAGE_SIZE);
1745877dfb34SKent Overstreet 	inode->ei_last_dirtied = (unsigned long) current;
17461c6fdbd8SKent Overstreet out:
1747d1542e03SKent Overstreet 	for (i = nr_pages_copied; i < nr_pages; i++) {
17481c6fdbd8SKent Overstreet 		unlock_page(pages[i]);
17491c6fdbd8SKent Overstreet 		put_page(pages[i]);
17501c6fdbd8SKent Overstreet 	}
17511c6fdbd8SKent Overstreet 
1752d1542e03SKent Overstreet 	bch2_page_reservation_put(c, inode, &res);
17531c6fdbd8SKent Overstreet 
17541c6fdbd8SKent Overstreet 	return copied ?: ret;
17551c6fdbd8SKent Overstreet }
17561c6fdbd8SKent Overstreet 
17571c6fdbd8SKent Overstreet static ssize_t bch2_buffered_write(struct kiocb *iocb, struct iov_iter *iter)
17581c6fdbd8SKent Overstreet {
17591c6fdbd8SKent Overstreet 	struct file *file = iocb->ki_filp;
17601c6fdbd8SKent Overstreet 	struct address_space *mapping = file->f_mapping;
17611c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = file_bch_inode(file);
17621c6fdbd8SKent Overstreet 	loff_t pos = iocb->ki_pos;
17631c6fdbd8SKent Overstreet 	ssize_t written = 0;
17641c6fdbd8SKent Overstreet 	int ret = 0;
17651c6fdbd8SKent Overstreet 
17661c6fdbd8SKent Overstreet 	bch2_pagecache_add_get(&inode->ei_pagecache_lock);
17671c6fdbd8SKent Overstreet 
17681c6fdbd8SKent Overstreet 	do {
17691c6fdbd8SKent Overstreet 		unsigned offset = pos & (PAGE_SIZE - 1);
17701c6fdbd8SKent Overstreet 		unsigned bytes = min_t(unsigned long, iov_iter_count(iter),
17711c6fdbd8SKent Overstreet 			      PAGE_SIZE * WRITE_BATCH_PAGES - offset);
17721c6fdbd8SKent Overstreet again:
17731c6fdbd8SKent Overstreet 		/*
17741c6fdbd8SKent Overstreet 		 * Bring in the user page that we will copy from _first_.
17751c6fdbd8SKent Overstreet 		 * Otherwise there's a nasty deadlock on copying from the
17761c6fdbd8SKent Overstreet 		 * same page as we're writing to, without it being marked
17771c6fdbd8SKent Overstreet 		 * up-to-date.
17781c6fdbd8SKent Overstreet 		 *
17791c6fdbd8SKent Overstreet 		 * Not only is this an optimisation, but it is also required
17801c6fdbd8SKent Overstreet 		 * to check that the address is actually valid, when atomic
17811c6fdbd8SKent Overstreet 		 * usercopies are used, below.
17821c6fdbd8SKent Overstreet 		 */
17831c6fdbd8SKent Overstreet 		if (unlikely(fault_in_iov_iter_readable(iter, bytes))) {
17841c6fdbd8SKent Overstreet 			bytes = min_t(unsigned long, iov_iter_count(iter),
17851c6fdbd8SKent Overstreet 				      PAGE_SIZE - offset);
17861c6fdbd8SKent Overstreet 
17871c6fdbd8SKent Overstreet 			if (unlikely(fault_in_iov_iter_readable(iter, bytes))) {
17881c6fdbd8SKent Overstreet 				ret = -EFAULT;
17891c6fdbd8SKent Overstreet 				break;
17901c6fdbd8SKent Overstreet 			}
17911c6fdbd8SKent Overstreet 		}
17921c6fdbd8SKent Overstreet 
17931c6fdbd8SKent Overstreet 		if (unlikely(fatal_signal_pending(current))) {
17941c6fdbd8SKent Overstreet 			ret = -EINTR;
17951c6fdbd8SKent Overstreet 			break;
17961c6fdbd8SKent Overstreet 		}
17971c6fdbd8SKent Overstreet 
17981c6fdbd8SKent Overstreet 		ret = __bch2_buffered_write(inode, mapping, iter, pos, bytes);
17991c6fdbd8SKent Overstreet 		if (unlikely(ret < 0))
18001c6fdbd8SKent Overstreet 			break;
18011c6fdbd8SKent Overstreet 
18021c6fdbd8SKent Overstreet 		cond_resched();
18031c6fdbd8SKent Overstreet 
18041c6fdbd8SKent Overstreet 		if (unlikely(ret == 0)) {
18051c6fdbd8SKent Overstreet 			/*
18061c6fdbd8SKent Overstreet 			 * If we were unable to copy any data at all, we must
18071c6fdbd8SKent Overstreet 			 * fall back to a single segment length write.
18081c6fdbd8SKent Overstreet 			 *
18091c6fdbd8SKent Overstreet 			 * If we didn't fallback here, we could livelock
18101c6fdbd8SKent Overstreet 			 * because not all segments in the iov can be copied at
18111c6fdbd8SKent Overstreet 			 * once without a pagefault.
18121c6fdbd8SKent Overstreet 			 */
18131c6fdbd8SKent Overstreet 			bytes = min_t(unsigned long, PAGE_SIZE - offset,
18141c6fdbd8SKent Overstreet 				      iov_iter_single_seg_count(iter));
18151c6fdbd8SKent Overstreet 			goto again;
18161c6fdbd8SKent Overstreet 		}
18171c6fdbd8SKent Overstreet 		pos += ret;
18181c6fdbd8SKent Overstreet 		written += ret;
1819912bdf17SKent Overstreet 		ret = 0;
18201c6fdbd8SKent Overstreet 
18211c6fdbd8SKent Overstreet 		balance_dirty_pages_ratelimited(mapping);
18221c6fdbd8SKent Overstreet 	} while (iov_iter_count(iter));
18231c6fdbd8SKent Overstreet 
18241c6fdbd8SKent Overstreet 	bch2_pagecache_add_put(&inode->ei_pagecache_lock);
18251c6fdbd8SKent Overstreet 
18261c6fdbd8SKent Overstreet 	return written ? written : ret;
18271c6fdbd8SKent Overstreet }
18281c6fdbd8SKent Overstreet 
18291c6fdbd8SKent Overstreet /* O_DIRECT reads */
18301c6fdbd8SKent Overstreet 
1831b4725cc1SKent Overstreet static void bio_check_or_release(struct bio *bio, bool check_dirty)
1832b4725cc1SKent Overstreet {
1833b4725cc1SKent Overstreet 	if (check_dirty) {
1834b4725cc1SKent Overstreet 		bio_check_pages_dirty(bio);
1835b4725cc1SKent Overstreet 	} else {
1836b4725cc1SKent Overstreet 		bio_release_pages(bio, false);
1837b4725cc1SKent Overstreet 		bio_put(bio);
1838b4725cc1SKent Overstreet 	}
1839b4725cc1SKent Overstreet }
1840b4725cc1SKent Overstreet 
18411c6fdbd8SKent Overstreet static void bch2_dio_read_complete(struct closure *cl)
18421c6fdbd8SKent Overstreet {
18431c6fdbd8SKent Overstreet 	struct dio_read *dio = container_of(cl, struct dio_read, cl);
18441c6fdbd8SKent Overstreet 
18451c6fdbd8SKent Overstreet 	dio->req->ki_complete(dio->req, dio->ret);
1846b4725cc1SKent Overstreet 	bio_check_or_release(&dio->rbio.bio, dio->should_dirty);
18471c6fdbd8SKent Overstreet }
18481c6fdbd8SKent Overstreet 
18491c6fdbd8SKent Overstreet static void bch2_direct_IO_read_endio(struct bio *bio)
18501c6fdbd8SKent Overstreet {
18511c6fdbd8SKent Overstreet 	struct dio_read *dio = bio->bi_private;
18521c6fdbd8SKent Overstreet 
18531c6fdbd8SKent Overstreet 	if (bio->bi_status)
18541c6fdbd8SKent Overstreet 		dio->ret = blk_status_to_errno(bio->bi_status);
18551c6fdbd8SKent Overstreet 
18561c6fdbd8SKent Overstreet 	closure_put(&dio->cl);
18571c6fdbd8SKent Overstreet }
18581c6fdbd8SKent Overstreet 
18591c6fdbd8SKent Overstreet static void bch2_direct_IO_read_split_endio(struct bio *bio)
18601c6fdbd8SKent Overstreet {
1861b4725cc1SKent Overstreet 	struct dio_read *dio = bio->bi_private;
1862b4725cc1SKent Overstreet 	bool should_dirty = dio->should_dirty;
1863b4725cc1SKent Overstreet 
18641c6fdbd8SKent Overstreet 	bch2_direct_IO_read_endio(bio);
1865b4725cc1SKent Overstreet 	bio_check_or_release(bio, should_dirty);
18661c6fdbd8SKent Overstreet }
18671c6fdbd8SKent Overstreet 
18681c6fdbd8SKent Overstreet static int bch2_direct_IO_read(struct kiocb *req, struct iov_iter *iter)
18691c6fdbd8SKent Overstreet {
18701c6fdbd8SKent Overstreet 	struct file *file = req->ki_filp;
18711c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = file_bch_inode(file);
18721c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
18739a3df993SKent Overstreet 	struct bch_io_opts opts = io_opts(c, &inode->ei_inode);
18741c6fdbd8SKent Overstreet 	struct dio_read *dio;
18751c6fdbd8SKent Overstreet 	struct bio *bio;
18761c6fdbd8SKent Overstreet 	loff_t offset = req->ki_pos;
18771c6fdbd8SKent Overstreet 	bool sync = is_sync_kiocb(req);
18781c6fdbd8SKent Overstreet 	size_t shorten;
18791c6fdbd8SKent Overstreet 	ssize_t ret;
18801c6fdbd8SKent Overstreet 
18811c6fdbd8SKent Overstreet 	if ((offset|iter->count) & (block_bytes(c) - 1))
18821c6fdbd8SKent Overstreet 		return -EINVAL;
18831c6fdbd8SKent Overstreet 
18841c6fdbd8SKent Overstreet 	ret = min_t(loff_t, iter->count,
18851c6fdbd8SKent Overstreet 		    max_t(loff_t, 0, i_size_read(&inode->v) - offset));
18861c6fdbd8SKent Overstreet 
18871c6fdbd8SKent Overstreet 	if (!ret)
18881c6fdbd8SKent Overstreet 		return ret;
18891c6fdbd8SKent Overstreet 
18901c6fdbd8SKent Overstreet 	shorten = iov_iter_count(iter) - round_up(ret, block_bytes(c));
18911c6fdbd8SKent Overstreet 	iter->count -= shorten;
18921c6fdbd8SKent Overstreet 
18931c6fdbd8SKent Overstreet 	bio = bio_alloc_bioset(NULL,
18941c6fdbd8SKent Overstreet 			       iov_iter_npages(iter, BIO_MAX_VECS),
18951c6fdbd8SKent Overstreet 			       REQ_OP_READ,
18961c6fdbd8SKent Overstreet 			       GFP_KERNEL,
18971c6fdbd8SKent Overstreet 			       &c->dio_read_bioset);
18981c6fdbd8SKent Overstreet 
18991c6fdbd8SKent Overstreet 	bio->bi_end_io = bch2_direct_IO_read_endio;
19001c6fdbd8SKent Overstreet 
19011c6fdbd8SKent Overstreet 	dio = container_of(bio, struct dio_read, rbio.bio);
19021c6fdbd8SKent Overstreet 	closure_init(&dio->cl, NULL);
19031c6fdbd8SKent Overstreet 
19041c6fdbd8SKent Overstreet 	/*
19051c6fdbd8SKent Overstreet 	 * this is a _really_ horrible hack just to avoid an atomic sub at the
19061c6fdbd8SKent Overstreet 	 * end:
19071c6fdbd8SKent Overstreet 	 */
19081c6fdbd8SKent Overstreet 	if (!sync) {
19091c6fdbd8SKent Overstreet 		set_closure_fn(&dio->cl, bch2_dio_read_complete, NULL);
19101c6fdbd8SKent Overstreet 		atomic_set(&dio->cl.remaining,
19111c6fdbd8SKent Overstreet 			   CLOSURE_REMAINING_INITIALIZER -
19121c6fdbd8SKent Overstreet 			   CLOSURE_RUNNING +
19131c6fdbd8SKent Overstreet 			   CLOSURE_DESTRUCTOR);
19141c6fdbd8SKent Overstreet 	} else {
19151c6fdbd8SKent Overstreet 		atomic_set(&dio->cl.remaining,
19161c6fdbd8SKent Overstreet 			   CLOSURE_REMAINING_INITIALIZER + 1);
19171c6fdbd8SKent Overstreet 	}
19181c6fdbd8SKent Overstreet 
19191c6fdbd8SKent Overstreet 	dio->req	= req;
19201c6fdbd8SKent Overstreet 	dio->ret	= ret;
1921b4725cc1SKent Overstreet 	/*
1922b4725cc1SKent Overstreet 	 * This is one of the sketchier things I've encountered: we have to skip
1923b4725cc1SKent Overstreet 	 * the dirtying of requests that are internal from the kernel (i.e. from
1924b4725cc1SKent Overstreet 	 * loopback), because we'll deadlock on page_lock.
1925b4725cc1SKent Overstreet 	 */
1926b4725cc1SKent Overstreet 	dio->should_dirty = iter_is_iovec(iter);
19271c6fdbd8SKent Overstreet 
19281c6fdbd8SKent Overstreet 	goto start;
19291c6fdbd8SKent Overstreet 	while (iter->count) {
19301c6fdbd8SKent Overstreet 		bio = bio_alloc_bioset(NULL,
19311c6fdbd8SKent Overstreet 				       iov_iter_npages(iter, BIO_MAX_VECS),
19321c6fdbd8SKent Overstreet 				       REQ_OP_READ,
19331c6fdbd8SKent Overstreet 				       GFP_KERNEL,
19341c6fdbd8SKent Overstreet 				       &c->bio_read);
19351c6fdbd8SKent Overstreet 		bio->bi_end_io		= bch2_direct_IO_read_split_endio;
19361c6fdbd8SKent Overstreet start:
19371c6fdbd8SKent Overstreet 		bio->bi_opf		= REQ_OP_READ|REQ_SYNC;
19381c6fdbd8SKent Overstreet 		bio->bi_iter.bi_sector	= offset >> 9;
19391c6fdbd8SKent Overstreet 		bio->bi_private		= dio;
19401c6fdbd8SKent Overstreet 
19411c6fdbd8SKent Overstreet 		ret = bio_iov_iter_get_pages(bio, iter);
19421c6fdbd8SKent Overstreet 		if (ret < 0) {
19431c6fdbd8SKent Overstreet 			/* XXX: fault inject this path */
19441c6fdbd8SKent Overstreet 			bio->bi_status = BLK_STS_RESOURCE;
19451c6fdbd8SKent Overstreet 			bio_endio(bio);
19461c6fdbd8SKent Overstreet 			break;
19471c6fdbd8SKent Overstreet 		}
19481c6fdbd8SKent Overstreet 
19491c6fdbd8SKent Overstreet 		offset += bio->bi_iter.bi_size;
1950b4725cc1SKent Overstreet 
1951b4725cc1SKent Overstreet 		if (dio->should_dirty)
19521c6fdbd8SKent Overstreet 			bio_set_pages_dirty(bio);
19531c6fdbd8SKent Overstreet 
19541c6fdbd8SKent Overstreet 		if (iter->count)
19551c6fdbd8SKent Overstreet 			closure_get(&dio->cl);
19561c6fdbd8SKent Overstreet 
19578c6d298aSKent Overstreet 		bch2_read(c, rbio_init(bio, opts), inode_inum(inode));
19581c6fdbd8SKent Overstreet 	}
19591c6fdbd8SKent Overstreet 
19601c6fdbd8SKent Overstreet 	iter->count += shorten;
19611c6fdbd8SKent Overstreet 
19621c6fdbd8SKent Overstreet 	if (sync) {
19631c6fdbd8SKent Overstreet 		closure_sync(&dio->cl);
19641c6fdbd8SKent Overstreet 		closure_debug_destroy(&dio->cl);
19651c6fdbd8SKent Overstreet 		ret = dio->ret;
1966b4725cc1SKent Overstreet 		bio_check_or_release(&dio->rbio.bio, dio->should_dirty);
19671c6fdbd8SKent Overstreet 		return ret;
19681c6fdbd8SKent Overstreet 	} else {
19691c6fdbd8SKent Overstreet 		return -EIOCBQUEUED;
19701c6fdbd8SKent Overstreet 	}
19711c6fdbd8SKent Overstreet }
19721c6fdbd8SKent Overstreet 
19731c6fdbd8SKent Overstreet ssize_t bch2_read_iter(struct kiocb *iocb, struct iov_iter *iter)
19741c6fdbd8SKent Overstreet {
19751c6fdbd8SKent Overstreet 	struct file *file = iocb->ki_filp;
19761c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = file_bch_inode(file);
19771c6fdbd8SKent Overstreet 	struct address_space *mapping = file->f_mapping;
19781c6fdbd8SKent Overstreet 	size_t count = iov_iter_count(iter);
19791c6fdbd8SKent Overstreet 	ssize_t ret;
19801c6fdbd8SKent Overstreet 
19811c6fdbd8SKent Overstreet 	if (!count)
19821c6fdbd8SKent Overstreet 		return 0; /* skip atime */
19831c6fdbd8SKent Overstreet 
19841c6fdbd8SKent Overstreet 	if (iocb->ki_flags & IOCB_DIRECT) {
19851c6fdbd8SKent Overstreet 		struct blk_plug plug;
19861c6fdbd8SKent Overstreet 
1987a023127aSKent Overstreet 		if (unlikely(mapping->nrpages)) {
19881c6fdbd8SKent Overstreet 			ret = filemap_write_and_wait_range(mapping,
19891c6fdbd8SKent Overstreet 						iocb->ki_pos,
19901c6fdbd8SKent Overstreet 						iocb->ki_pos + count - 1);
19911c6fdbd8SKent Overstreet 			if (ret < 0)
19921c6fdbd8SKent Overstreet 				return ret;
1993a023127aSKent Overstreet 		}
19941c6fdbd8SKent Overstreet 
19951c6fdbd8SKent Overstreet 		file_accessed(file);
19961c6fdbd8SKent Overstreet 
19971c6fdbd8SKent Overstreet 		blk_start_plug(&plug);
19981c6fdbd8SKent Overstreet 		ret = bch2_direct_IO_read(iocb, iter);
19991c6fdbd8SKent Overstreet 		blk_finish_plug(&plug);
20001c6fdbd8SKent Overstreet 
20011c6fdbd8SKent Overstreet 		if (ret >= 0)
20021c6fdbd8SKent Overstreet 			iocb->ki_pos += ret;
20031c6fdbd8SKent Overstreet 	} else {
20041c6fdbd8SKent Overstreet 		bch2_pagecache_add_get(&inode->ei_pagecache_lock);
20051c6fdbd8SKent Overstreet 		ret = generic_file_read_iter(iocb, iter);
20061c6fdbd8SKent Overstreet 		bch2_pagecache_add_put(&inode->ei_pagecache_lock);
20071c6fdbd8SKent Overstreet 	}
20081c6fdbd8SKent Overstreet 
20091c6fdbd8SKent Overstreet 	return ret;
20101c6fdbd8SKent Overstreet }
20111c6fdbd8SKent Overstreet 
20121c6fdbd8SKent Overstreet /* O_DIRECT writes */
20131c6fdbd8SKent Overstreet 
20146fed42bbSKent Overstreet static bool bch2_check_range_allocated(struct bch_fs *c, subvol_inum inum,
20156fed42bbSKent Overstreet 				       u64 offset, u64 size,
20166fed42bbSKent Overstreet 				       unsigned nr_replicas, bool compressed)
20176fed42bbSKent Overstreet {
20186fed42bbSKent Overstreet 	struct btree_trans trans;
20196fed42bbSKent Overstreet 	struct btree_iter iter;
20206fed42bbSKent Overstreet 	struct bkey_s_c k;
20216fed42bbSKent Overstreet 	u64 end = offset + size;
20226fed42bbSKent Overstreet 	u32 snapshot;
20236fed42bbSKent Overstreet 	bool ret = true;
20246fed42bbSKent Overstreet 	int err;
20256fed42bbSKent Overstreet 
20266fed42bbSKent Overstreet 	bch2_trans_init(&trans, c, 0, 0);
20276fed42bbSKent Overstreet retry:
20286fed42bbSKent Overstreet 	bch2_trans_begin(&trans);
20296fed42bbSKent Overstreet 
20306fed42bbSKent Overstreet 	err = bch2_subvolume_get_snapshot(&trans, inum.subvol, &snapshot);
20316fed42bbSKent Overstreet 	if (err)
20326fed42bbSKent Overstreet 		goto err;
20336fed42bbSKent Overstreet 
2034e5fa91d7SKent Overstreet 	for_each_btree_key_norestart(&trans, iter, BTREE_ID_extents,
20356fed42bbSKent Overstreet 			   SPOS(inum.inum, offset, snapshot),
20366fed42bbSKent Overstreet 			   BTREE_ITER_SLOTS, k, err) {
20376fed42bbSKent Overstreet 		if (bkey_cmp(bkey_start_pos(k.k), POS(inum.inum, end)) >= 0)
20386fed42bbSKent Overstreet 			break;
20396fed42bbSKent Overstreet 
20408c6d298aSKent Overstreet 		if (k.k->p.snapshot != snapshot ||
20418c6d298aSKent Overstreet 		    nr_replicas > bch2_bkey_replicas(c, k) ||
20426fed42bbSKent Overstreet 		    (!compressed && bch2_bkey_sectors_compressed(k))) {
20436fed42bbSKent Overstreet 			ret = false;
20446fed42bbSKent Overstreet 			break;
20456fed42bbSKent Overstreet 		}
20466fed42bbSKent Overstreet 	}
20476fed42bbSKent Overstreet 
20486fed42bbSKent Overstreet 	offset = iter.pos.offset;
20496fed42bbSKent Overstreet 	bch2_trans_iter_exit(&trans, &iter);
20506fed42bbSKent Overstreet err:
20516fed42bbSKent Overstreet 	if (err == -EINTR)
20526fed42bbSKent Overstreet 		goto retry;
20536fed42bbSKent Overstreet 	bch2_trans_exit(&trans);
20546fed42bbSKent Overstreet 
20556fed42bbSKent Overstreet 	return err ? false : ret;
20566fed42bbSKent Overstreet }
20576fed42bbSKent Overstreet 
20581c6fdbd8SKent Overstreet /*
20591c6fdbd8SKent Overstreet  * We're going to return -EIOCBQUEUED, but we haven't finished consuming the
20601c6fdbd8SKent Overstreet  * iov_iter yet, so we need to stash a copy of the iovec: it might be on the
20611c6fdbd8SKent Overstreet  * caller's stack, we're not guaranteed that it will live for the duration of
20621c6fdbd8SKent Overstreet  * the IO:
20631c6fdbd8SKent Overstreet  */
20641c6fdbd8SKent Overstreet static noinline int bch2_dio_write_copy_iov(struct dio_write *dio)
20651c6fdbd8SKent Overstreet {
20661c6fdbd8SKent Overstreet 	struct iovec *iov = dio->inline_vecs;
20671c6fdbd8SKent Overstreet 
20681c6fdbd8SKent Overstreet 	/*
20691c6fdbd8SKent Overstreet 	 * iov_iter has a single embedded iovec - nothing to do:
20701c6fdbd8SKent Overstreet 	 */
20711c6fdbd8SKent Overstreet 	if (iter_is_ubuf(&dio->iter))
20721c6fdbd8SKent Overstreet 		return 0;
20731c6fdbd8SKent Overstreet 
20741c6fdbd8SKent Overstreet 	/*
20751c6fdbd8SKent Overstreet 	 * We don't currently handle non-iovec iov_iters here - return an error,
20761c6fdbd8SKent Overstreet 	 * and we'll fall back to doing the IO synchronously:
20771c6fdbd8SKent Overstreet 	 */
20781c6fdbd8SKent Overstreet 	if (!iter_is_iovec(&dio->iter))
20791c6fdbd8SKent Overstreet 		return -1;
20801c6fdbd8SKent Overstreet 
20811c6fdbd8SKent Overstreet 	if (dio->iter.nr_segs > ARRAY_SIZE(dio->inline_vecs)) {
20821c6fdbd8SKent Overstreet 		iov = kmalloc_array(dio->iter.nr_segs, sizeof(*iov),
20831c6fdbd8SKent Overstreet 				    GFP_KERNEL);
20841c6fdbd8SKent Overstreet 		if (unlikely(!iov))
20851c6fdbd8SKent Overstreet 			return -ENOMEM;
20861c6fdbd8SKent Overstreet 
20871c6fdbd8SKent Overstreet 		dio->free_iov = true;
20881c6fdbd8SKent Overstreet 	}
20891c6fdbd8SKent Overstreet 
20901c6fdbd8SKent Overstreet 	memcpy(iov, dio->iter.__iov, dio->iter.nr_segs * sizeof(*iov));
20911c6fdbd8SKent Overstreet 	dio->iter.__iov = iov;
20921c6fdbd8SKent Overstreet 	return 0;
20931c6fdbd8SKent Overstreet }
20941c6fdbd8SKent Overstreet 
2095042a1f26SKent Overstreet static void bch2_dio_write_loop_async(struct bch_write_op *);
2096042a1f26SKent Overstreet 
20971c6fdbd8SKent Overstreet static long bch2_dio_write_loop(struct dio_write *dio)
20981c6fdbd8SKent Overstreet {
2099ed484030SKent Overstreet 	bool kthread = (current->flags & PF_KTHREAD) != 0;
21001c6fdbd8SKent Overstreet 	struct kiocb *req = dio->req;
21011c6fdbd8SKent Overstreet 	struct address_space *mapping = req->ki_filp->f_mapping;
21029a3df993SKent Overstreet 	struct bch_inode_info *inode = file_bch_inode(req->ki_filp);
2103042a1f26SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
21049a3df993SKent Overstreet 	struct bio *bio = &dio->op.wbio.bio;
2105eb8e6e9cSKent Overstreet 	unsigned unaligned, iter_count;
2106eb8e6e9cSKent Overstreet 	bool sync = dio->sync, dropped_locks;
21071c6fdbd8SKent Overstreet 	long ret;
21081c6fdbd8SKent Overstreet 
21091c6fdbd8SKent Overstreet 	if (dio->loop)
21101c6fdbd8SKent Overstreet 		goto loop;
21111c6fdbd8SKent Overstreet 
21121c6fdbd8SKent Overstreet 	while (1) {
2113eb8e6e9cSKent Overstreet 		iter_count = dio->iter.count;
2114eb8e6e9cSKent Overstreet 
2115ed484030SKent Overstreet 		if (kthread)
2116ed484030SKent Overstreet 			kthread_use_mm(dio->mm);
21171c6fdbd8SKent Overstreet 		BUG_ON(current->faults_disabled_mapping);
21181c6fdbd8SKent Overstreet 		current->faults_disabled_mapping = mapping;
21191c6fdbd8SKent Overstreet 
21201c6fdbd8SKent Overstreet 		ret = bio_iov_iter_get_pages(bio, &dio->iter);
21211c6fdbd8SKent Overstreet 
2122eb8e6e9cSKent Overstreet 		dropped_locks = fdm_dropped_locks();
2123eb8e6e9cSKent Overstreet 
21241c6fdbd8SKent Overstreet 		current->faults_disabled_mapping = NULL;
2125ed484030SKent Overstreet 		if (kthread)
2126ed484030SKent Overstreet 			kthread_unuse_mm(dio->mm);
21271c6fdbd8SKent Overstreet 
2128eb8e6e9cSKent Overstreet 		/*
2129eb8e6e9cSKent Overstreet 		 * If the fault handler returned an error but also signalled
2130eb8e6e9cSKent Overstreet 		 * that it dropped & retook ei_pagecache_lock, we just need to
2131eb8e6e9cSKent Overstreet 		 * re-shoot down the page cache and retry:
2132eb8e6e9cSKent Overstreet 		 */
2133eb8e6e9cSKent Overstreet 		if (dropped_locks && ret)
2134eb8e6e9cSKent Overstreet 			ret = 0;
2135eb8e6e9cSKent Overstreet 
21361c6fdbd8SKent Overstreet 		if (unlikely(ret < 0))
21371c6fdbd8SKent Overstreet 			goto err;
21381c6fdbd8SKent Overstreet 
2139eb8e6e9cSKent Overstreet 		if (unlikely(dropped_locks)) {
2140eb8e6e9cSKent Overstreet 			ret = write_invalidate_inode_pages_range(mapping,
2141eb8e6e9cSKent Overstreet 					req->ki_pos,
2142eb8e6e9cSKent Overstreet 					req->ki_pos + iter_count - 1);
2143eb8e6e9cSKent Overstreet 			if (unlikely(ret))
2144eb8e6e9cSKent Overstreet 				goto err;
2145eb8e6e9cSKent Overstreet 
2146eb8e6e9cSKent Overstreet 			if (!bio->bi_iter.bi_size)
2147eb8e6e9cSKent Overstreet 				continue;
2148eb8e6e9cSKent Overstreet 		}
2149eb8e6e9cSKent Overstreet 
21500a426c32SKent Overstreet 		unaligned = bio->bi_iter.bi_size & (block_bytes(c) - 1);
21510a426c32SKent Overstreet 		bio->bi_iter.bi_size -= unaligned;
21520a426c32SKent Overstreet 		iov_iter_revert(&dio->iter, unaligned);
21530a426c32SKent Overstreet 
21540a426c32SKent Overstreet 		if (!bio->bi_iter.bi_size) {
21550a426c32SKent Overstreet 			/*
21560a426c32SKent Overstreet 			 * bio_iov_iter_get_pages was only able to get <
21570a426c32SKent Overstreet 			 * blocksize worth of pages:
21580a426c32SKent Overstreet 			 */
21590a426c32SKent Overstreet 			ret = -EFAULT;
21600a426c32SKent Overstreet 			goto err;
21610a426c32SKent Overstreet 		}
21620a426c32SKent Overstreet 
2163042a1f26SKent Overstreet 		bch2_write_op_init(&dio->op, c, io_opts(c, &inode->ei_inode));
2164042a1f26SKent Overstreet 		dio->op.end_io		= bch2_dio_write_loop_async;
2165042a1f26SKent Overstreet 		dio->op.target		= dio->op.opts.foreground_target;
2166042a1f26SKent Overstreet 		dio->op.write_point	= writepoint_hashed((unsigned long) current);
2167042a1f26SKent Overstreet 		dio->op.nr_replicas	= dio->op.opts.data_replicas;
21688c6d298aSKent Overstreet 		dio->op.subvol		= inode->ei_subvol;
2169042a1f26SKent Overstreet 		dio->op.pos		= POS(inode->v.i_ino, (u64) req->ki_pos >> 9);
2170042a1f26SKent Overstreet 
2171042a1f26SKent Overstreet 		if ((req->ki_flags & IOCB_DSYNC) &&
2172042a1f26SKent Overstreet 		    !c->opts.journal_flush_disabled)
2173042a1f26SKent Overstreet 			dio->op.flags |= BCH_WRITE_FLUSH;
2174a6336910SKent Overstreet 		dio->op.flags |= BCH_WRITE_CHECK_ENOSPC;
2175042a1f26SKent Overstreet 
2176042a1f26SKent Overstreet 		ret = bch2_disk_reservation_get(c, &dio->op.res, bio_sectors(bio),
2177042a1f26SKent Overstreet 						dio->op.opts.data_replicas, 0);
2178042a1f26SKent Overstreet 		if (unlikely(ret) &&
21796fed42bbSKent Overstreet 		    !bch2_check_range_allocated(c, inode_inum(inode),
21806fed42bbSKent Overstreet 				dio->op.pos.offset, bio_sectors(bio),
218135a067b4SKent Overstreet 				dio->op.opts.data_replicas,
218235a067b4SKent Overstreet 				dio->op.opts.compression != 0))
2183042a1f26SKent Overstreet 			goto err;
21841c6fdbd8SKent Overstreet 
21851c6fdbd8SKent Overstreet 		task_io_account_write(bio->bi_iter.bi_size);
21861c6fdbd8SKent Overstreet 
21871c6fdbd8SKent Overstreet 		if (!dio->sync && !dio->loop && dio->iter.count) {
21881c6fdbd8SKent Overstreet 			if (bch2_dio_write_copy_iov(dio)) {
2189286d8ad0SKent Overstreet 				dio->sync = sync = true;
2190f8f30863SKent Overstreet 				goto do_io;
21911c6fdbd8SKent Overstreet 			}
21921c6fdbd8SKent Overstreet 		}
2193f8f30863SKent Overstreet do_io:
21941c6fdbd8SKent Overstreet 		dio->loop = true;
2195f8f30863SKent Overstreet 		closure_call(&dio->op.cl, bch2_write, NULL, NULL);
21961c6fdbd8SKent Overstreet 
2197286d8ad0SKent Overstreet 		if (sync)
2198f8f30863SKent Overstreet 			wait_for_completion(&dio->done);
2199f8f30863SKent Overstreet 		else
22001c6fdbd8SKent Overstreet 			return -EIOCBQUEUED;
22011c6fdbd8SKent Overstreet loop:
22029a3df993SKent Overstreet 		i_sectors_acct(c, inode, &dio->quota_res,
22039a3df993SKent Overstreet 			       dio->op.i_sectors_delta);
2204042a1f26SKent Overstreet 		req->ki_pos += (u64) dio->op.written << 9;
2205042a1f26SKent Overstreet 		dio->written += dio->op.written;
22069a3df993SKent Overstreet 
22079a3df993SKent Overstreet 		spin_lock(&inode->v.i_lock);
2208042a1f26SKent Overstreet 		if (req->ki_pos > inode->v.i_size)
2209042a1f26SKent Overstreet 			i_size_write(&inode->v, req->ki_pos);
22109a3df993SKent Overstreet 		spin_unlock(&inode->v.i_lock);
22119a3df993SKent Overstreet 
2212032ac32cSKent Overstreet 		bio_release_pages(bio, false);
22135468f119SKent Overstreet 		bio->bi_vcnt = 0;
221433c74e41SKent Overstreet 
221533c74e41SKent Overstreet 		if (dio->op.error) {
221633c74e41SKent Overstreet 			set_bit(EI_INODE_ERROR, &inode->ei_flags);
221733c74e41SKent Overstreet 			break;
221833c74e41SKent Overstreet 		}
221933c74e41SKent Overstreet 
222033c74e41SKent Overstreet 		if (!dio->iter.count)
22211c6fdbd8SKent Overstreet 			break;
2222f8f30863SKent Overstreet 
22231c6fdbd8SKent Overstreet 		bio_reset(bio, NULL, REQ_OP_WRITE);
2224f8f30863SKent Overstreet 		reinit_completion(&dio->done);
22251c6fdbd8SKent Overstreet 	}
22261c6fdbd8SKent Overstreet 
2227042a1f26SKent Overstreet 	ret = dio->op.error ?: ((long) dio->written << 9);
22281c6fdbd8SKent Overstreet err:
22291c6fdbd8SKent Overstreet 	bch2_pagecache_block_put(&inode->ei_pagecache_lock);
22300a426c32SKent Overstreet 	bch2_quota_reservation_put(c, inode, &dio->quota_res);
22311c6fdbd8SKent Overstreet 
22321c6fdbd8SKent Overstreet 	if (dio->free_iov)
22331c6fdbd8SKent Overstreet 		kfree(dio->iter.__iov);
22341c6fdbd8SKent Overstreet 
22355468f119SKent Overstreet 	bio_release_pages(bio, false);
22361c6fdbd8SKent Overstreet 	bio_put(bio);
22371c6fdbd8SKent Overstreet 
22381c6fdbd8SKent Overstreet 	/* inode->i_dio_count is our ref on inode and thus bch_fs */
22391c6fdbd8SKent Overstreet 	inode_dio_end(&inode->v);
22401c6fdbd8SKent Overstreet 
22411c6fdbd8SKent Overstreet 	if (!sync) {
22421c6fdbd8SKent Overstreet 		req->ki_complete(req, ret);
22431c6fdbd8SKent Overstreet 		ret = -EIOCBQUEUED;
22441c6fdbd8SKent Overstreet 	}
22451c6fdbd8SKent Overstreet 	return ret;
22461c6fdbd8SKent Overstreet }
22471c6fdbd8SKent Overstreet 
2248f8f30863SKent Overstreet static void bch2_dio_write_loop_async(struct bch_write_op *op)
22491c6fdbd8SKent Overstreet {
2250f8f30863SKent Overstreet 	struct dio_write *dio = container_of(op, struct dio_write, op);
22511c6fdbd8SKent Overstreet 
2252f8f30863SKent Overstreet 	if (dio->sync)
2253f8f30863SKent Overstreet 		complete(&dio->done);
2254f8f30863SKent Overstreet 	else
22551c6fdbd8SKent Overstreet 		bch2_dio_write_loop(dio);
22561c6fdbd8SKent Overstreet }
22571c6fdbd8SKent Overstreet 
22581c6fdbd8SKent Overstreet static noinline
22591c6fdbd8SKent Overstreet ssize_t bch2_direct_write(struct kiocb *req, struct iov_iter *iter)
22601c6fdbd8SKent Overstreet {
22611c6fdbd8SKent Overstreet 	struct file *file = req->ki_filp;
226254847d25SKent Overstreet 	struct address_space *mapping = file->f_mapping;
22631c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = file_bch_inode(file);
22641c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
22651c6fdbd8SKent Overstreet 	struct dio_write *dio;
22661c6fdbd8SKent Overstreet 	struct bio *bio;
22677edcfbfeSKent Overstreet 	bool locked = true, extending;
22681c6fdbd8SKent Overstreet 	ssize_t ret;
22691c6fdbd8SKent Overstreet 
22707edcfbfeSKent Overstreet 	prefetch(&c->opts);
22717edcfbfeSKent Overstreet 	prefetch((void *) &c->opts + 64);
22727edcfbfeSKent Overstreet 	prefetch(&inode->ei_inode);
22737edcfbfeSKent Overstreet 	prefetch((void *) &inode->ei_inode + 64);
22741c6fdbd8SKent Overstreet 
22757edcfbfeSKent Overstreet 	inode_lock(&inode->v);
22767edcfbfeSKent Overstreet 
22777edcfbfeSKent Overstreet 	ret = generic_write_checks(req, iter);
22787edcfbfeSKent Overstreet 	if (unlikely(ret <= 0))
22797edcfbfeSKent Overstreet 		goto err;
22807edcfbfeSKent Overstreet 
22817edcfbfeSKent Overstreet 	ret = file_remove_privs(file);
22827edcfbfeSKent Overstreet 	if (unlikely(ret))
22837edcfbfeSKent Overstreet 		goto err;
22847edcfbfeSKent Overstreet 
22857edcfbfeSKent Overstreet 	ret = file_update_time(file);
22867edcfbfeSKent Overstreet 	if (unlikely(ret))
22877edcfbfeSKent Overstreet 		goto err;
22881c6fdbd8SKent Overstreet 
2289919dbbd1SKent Overstreet 	if (unlikely((req->ki_pos|iter->count) & (block_bytes(c) - 1)))
22907edcfbfeSKent Overstreet 		goto err;
22917edcfbfeSKent Overstreet 
22927edcfbfeSKent Overstreet 	inode_dio_begin(&inode->v);
22937edcfbfeSKent Overstreet 	bch2_pagecache_block_get(&inode->ei_pagecache_lock);
22947edcfbfeSKent Overstreet 
22957edcfbfeSKent Overstreet 	extending = req->ki_pos + iter->count > inode->v.i_size;
22967edcfbfeSKent Overstreet 	if (!extending) {
22977edcfbfeSKent Overstreet 		inode_unlock(&inode->v);
22987edcfbfeSKent Overstreet 		locked = false;
22997edcfbfeSKent Overstreet 	}
23001c6fdbd8SKent Overstreet 
23011c6fdbd8SKent Overstreet 	bio = bio_alloc_bioset(NULL,
23022ed5cd50SKent Overstreet 			       iov_iter_is_bvec(iter)
23032ed5cd50SKent Overstreet 			       ? 0
23042ed5cd50SKent Overstreet 			       : iov_iter_npages(iter, BIO_MAX_VECS),
23051c6fdbd8SKent Overstreet 			       REQ_OP_WRITE,
23061c6fdbd8SKent Overstreet 			       GFP_KERNEL,
23071c6fdbd8SKent Overstreet 			       &c->dio_write_bioset);
23089a3df993SKent Overstreet 	dio = container_of(bio, struct dio_write, op.wbio.bio);
2309f8f30863SKent Overstreet 	init_completion(&dio->done);
23101c6fdbd8SKent Overstreet 	dio->req		= req;
2311ed484030SKent Overstreet 	dio->mm			= current->mm;
23121c6fdbd8SKent Overstreet 	dio->loop		= false;
23137edcfbfeSKent Overstreet 	dio->sync		= is_sync_kiocb(req) || extending;
23141c6fdbd8SKent Overstreet 	dio->free_iov		= false;
23151c6fdbd8SKent Overstreet 	dio->quota_res.sectors	= 0;
2316042a1f26SKent Overstreet 	dio->written		= 0;
23171c6fdbd8SKent Overstreet 	dio->iter		= *iter;
23189a3df993SKent Overstreet 
23191c6fdbd8SKent Overstreet 	ret = bch2_quota_reservation_add(c, inode, &dio->quota_res,
23201c6fdbd8SKent Overstreet 					 iter->count >> 9, true);
23211c6fdbd8SKent Overstreet 	if (unlikely(ret))
23227edcfbfeSKent Overstreet 		goto err_put_bio;
23231c6fdbd8SKent Overstreet 
2324a023127aSKent Overstreet 	if (unlikely(mapping->nrpages)) {
232554847d25SKent Overstreet 		ret = write_invalidate_inode_pages_range(mapping,
232654847d25SKent Overstreet 						req->ki_pos,
232754847d25SKent Overstreet 						req->ki_pos + iter->count - 1);
232854847d25SKent Overstreet 		if (unlikely(ret))
232954847d25SKent Overstreet 			goto err_put_bio;
2330a023127aSKent Overstreet 	}
233154847d25SKent Overstreet 
23327edcfbfeSKent Overstreet 	ret = bch2_dio_write_loop(dio);
23331c6fdbd8SKent Overstreet err:
23347edcfbfeSKent Overstreet 	if (locked)
23357edcfbfeSKent Overstreet 		inode_unlock(&inode->v);
23367edcfbfeSKent Overstreet 	return ret;
23377edcfbfeSKent Overstreet err_put_bio:
23387edcfbfeSKent Overstreet 	bch2_pagecache_block_put(&inode->ei_pagecache_lock);
23391c6fdbd8SKent Overstreet 	bch2_quota_reservation_put(c, inode, &dio->quota_res);
23401c6fdbd8SKent Overstreet 	bio_put(bio);
23417edcfbfeSKent Overstreet 	inode_dio_end(&inode->v);
23427edcfbfeSKent Overstreet 	goto err;
23431c6fdbd8SKent Overstreet }
23441c6fdbd8SKent Overstreet 
23457edcfbfeSKent Overstreet ssize_t bch2_write_iter(struct kiocb *iocb, struct iov_iter *from)
23461c6fdbd8SKent Overstreet {
23471c6fdbd8SKent Overstreet 	struct file *file = iocb->ki_filp;
23487edcfbfeSKent Overstreet 	struct bch_inode_info *inode = file_bch_inode(file);
23491c6fdbd8SKent Overstreet 	ssize_t	ret;
23501c6fdbd8SKent Overstreet 
23511c6fdbd8SKent Overstreet 	if (iocb->ki_flags & IOCB_DIRECT)
23521c6fdbd8SKent Overstreet 		return bch2_direct_write(iocb, from);
23531c6fdbd8SKent Overstreet 
23547edcfbfeSKent Overstreet 	inode_lock(&inode->v);
23557edcfbfeSKent Overstreet 
23567edcfbfeSKent Overstreet 	ret = generic_write_checks(iocb, from);
23577edcfbfeSKent Overstreet 	if (ret <= 0)
23587edcfbfeSKent Overstreet 		goto unlock;
23597edcfbfeSKent Overstreet 
23601c6fdbd8SKent Overstreet 	ret = file_remove_privs(file);
23611c6fdbd8SKent Overstreet 	if (ret)
23627edcfbfeSKent Overstreet 		goto unlock;
23631c6fdbd8SKent Overstreet 
23641c6fdbd8SKent Overstreet 	ret = file_update_time(file);
23651c6fdbd8SKent Overstreet 	if (ret)
23667edcfbfeSKent Overstreet 		goto unlock;
23671c6fdbd8SKent Overstreet 
23687edcfbfeSKent Overstreet 	ret = bch2_buffered_write(iocb, from);
23691c6fdbd8SKent Overstreet 	if (likely(ret > 0))
23701c6fdbd8SKent Overstreet 		iocb->ki_pos += ret;
23717edcfbfeSKent Overstreet unlock:
23721c6fdbd8SKent Overstreet 	inode_unlock(&inode->v);
23731c6fdbd8SKent Overstreet 
23747edcfbfeSKent Overstreet 	if (ret > 0)
23751c6fdbd8SKent Overstreet 		ret = generic_write_sync(iocb, ret);
23761c6fdbd8SKent Overstreet 
23771c6fdbd8SKent Overstreet 	return ret;
23781c6fdbd8SKent Overstreet }
23791c6fdbd8SKent Overstreet 
23801c6fdbd8SKent Overstreet /* fsync: */
23811c6fdbd8SKent Overstreet 
238268a2054dSKent Overstreet /*
238368a2054dSKent Overstreet  * inode->ei_inode.bi_journal_seq won't be up to date since it's set in an
238468a2054dSKent Overstreet  * insert trigger: look up the btree inode instead
238568a2054dSKent Overstreet  */
238668a2054dSKent Overstreet static int bch2_flush_inode(struct bch_fs *c, subvol_inum inum)
238768a2054dSKent Overstreet {
238868a2054dSKent Overstreet 	struct bch_inode_unpacked inode;
238968a2054dSKent Overstreet 	int ret;
239068a2054dSKent Overstreet 
239168a2054dSKent Overstreet 	if (c->opts.journal_flush_disabled)
239268a2054dSKent Overstreet 		return 0;
239368a2054dSKent Overstreet 
239468a2054dSKent Overstreet 	ret = bch2_inode_find_by_inum(c, inum, &inode);
239568a2054dSKent Overstreet 	if (ret)
239668a2054dSKent Overstreet 		return ret;
239768a2054dSKent Overstreet 
239868a2054dSKent Overstreet 	return bch2_journal_flush_seq(&c->journal, inode.bi_journal_seq);
239968a2054dSKent Overstreet }
240068a2054dSKent Overstreet 
24011c6fdbd8SKent Overstreet int bch2_fsync(struct file *file, loff_t start, loff_t end, int datasync)
24021c6fdbd8SKent Overstreet {
24031c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = file_bch_inode(file);
24041c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
240568a2054dSKent Overstreet 	int ret, ret2, ret3;
24061c6fdbd8SKent Overstreet 
24071c6fdbd8SKent Overstreet 	ret = file_write_and_wait_range(file, start, end);
240868a2054dSKent Overstreet 	ret2 = sync_inode_metadata(&inode->v, 1);
240968a2054dSKent Overstreet 	ret3 = bch2_flush_inode(c, inode_inum(inode));
24101c6fdbd8SKent Overstreet 
241168a2054dSKent Overstreet 	return ret ?: ret2 ?: ret3;
24121c6fdbd8SKent Overstreet }
24131c6fdbd8SKent Overstreet 
24141c6fdbd8SKent Overstreet /* truncate: */
24151c6fdbd8SKent Overstreet 
24166fed42bbSKent Overstreet static inline int range_has_data(struct bch_fs *c, u32 subvol,
24171c6fdbd8SKent Overstreet 				 struct bpos start,
24181c6fdbd8SKent Overstreet 				 struct bpos end)
24191c6fdbd8SKent Overstreet {
2420424eb881SKent Overstreet 	struct btree_trans trans;
242167e0dd8fSKent Overstreet 	struct btree_iter iter;
24221c6fdbd8SKent Overstreet 	struct bkey_s_c k;
24231c6fdbd8SKent Overstreet 	int ret = 0;
24241c6fdbd8SKent Overstreet 
242520bceecbSKent Overstreet 	bch2_trans_init(&trans, c, 0, 0);
24266fed42bbSKent Overstreet retry:
24276fed42bbSKent Overstreet 	bch2_trans_begin(&trans);
24286fed42bbSKent Overstreet 
24296fed42bbSKent Overstreet 	ret = bch2_subvolume_get_snapshot(&trans, subvol, &start.snapshot);
24306fed42bbSKent Overstreet 	if (ret)
24316fed42bbSKent Overstreet 		goto err;
2432424eb881SKent Overstreet 
2433e5fa91d7SKent Overstreet 	for_each_btree_key_norestart(&trans, iter, BTREE_ID_extents, start, 0, k, ret) {
24341c6fdbd8SKent Overstreet 		if (bkey_cmp(bkey_start_pos(k.k), end) >= 0)
24351c6fdbd8SKent Overstreet 			break;
24361c6fdbd8SKent Overstreet 
24371c6fdbd8SKent Overstreet 		if (bkey_extent_is_data(k.k)) {
24381c6fdbd8SKent Overstreet 			ret = 1;
24391c6fdbd8SKent Overstreet 			break;
24401c6fdbd8SKent Overstreet 		}
24411c6fdbd8SKent Overstreet 	}
24426fed42bbSKent Overstreet 	start = iter.pos;
244367e0dd8fSKent Overstreet 	bch2_trans_iter_exit(&trans, &iter);
24446fed42bbSKent Overstreet err:
24456fed42bbSKent Overstreet 	if (ret == -EINTR)
24466fed42bbSKent Overstreet 		goto retry;
24471c6fdbd8SKent Overstreet 
24489a796fdbSKent Overstreet 	bch2_trans_exit(&trans);
24499a796fdbSKent Overstreet 	return ret;
24501c6fdbd8SKent Overstreet }
24511c6fdbd8SKent Overstreet 
24521c6fdbd8SKent Overstreet static int __bch2_truncate_page(struct bch_inode_info *inode,
24531c6fdbd8SKent Overstreet 				pgoff_t index, loff_t start, loff_t end)
24541c6fdbd8SKent Overstreet {
24551c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
24561c6fdbd8SKent Overstreet 	struct address_space *mapping = inode->v.i_mapping;
2457a99b1cafSKent Overstreet 	struct bch_page_state *s;
24581c6fdbd8SKent Overstreet 	unsigned start_offset = start & (PAGE_SIZE - 1);
24591c6fdbd8SKent Overstreet 	unsigned end_offset = ((end - 1) & (PAGE_SIZE - 1)) + 1;
2460a99b1cafSKent Overstreet 	unsigned i;
24611c6fdbd8SKent Overstreet 	struct page *page;
2462b19d307dSKent Overstreet 	s64 i_sectors_delta = 0;
24631c6fdbd8SKent Overstreet 	int ret = 0;
24641c6fdbd8SKent Overstreet 
24651c6fdbd8SKent Overstreet 	/* Page boundary? Nothing to do */
24661c6fdbd8SKent Overstreet 	if (!((index == start >> PAGE_SHIFT && start_offset) ||
24671c6fdbd8SKent Overstreet 	      (index == end >> PAGE_SHIFT && end_offset != PAGE_SIZE)))
24681c6fdbd8SKent Overstreet 		return 0;
24691c6fdbd8SKent Overstreet 
24701c6fdbd8SKent Overstreet 	/* Above i_size? */
24711c6fdbd8SKent Overstreet 	if (index << PAGE_SHIFT >= inode->v.i_size)
24721c6fdbd8SKent Overstreet 		return 0;
24731c6fdbd8SKent Overstreet 
24741c6fdbd8SKent Overstreet 	page = find_lock_page(mapping, index);
24751c6fdbd8SKent Overstreet 	if (!page) {
24761c6fdbd8SKent Overstreet 		/*
24771c6fdbd8SKent Overstreet 		 * XXX: we're doing two index lookups when we end up reading the
24781c6fdbd8SKent Overstreet 		 * page
24791c6fdbd8SKent Overstreet 		 */
24806fed42bbSKent Overstreet 		ret = range_has_data(c, inode->ei_subvol,
24817279c1a2SKent Overstreet 				POS(inode->v.i_ino, index << PAGE_SECTORS_SHIFT),
24827279c1a2SKent Overstreet 				POS(inode->v.i_ino, (index + 1) << PAGE_SECTORS_SHIFT));
24831c6fdbd8SKent Overstreet 		if (ret <= 0)
24841c6fdbd8SKent Overstreet 			return ret;
24851c6fdbd8SKent Overstreet 
24861c6fdbd8SKent Overstreet 		page = find_or_create_page(mapping, index, GFP_KERNEL);
24871c6fdbd8SKent Overstreet 		if (unlikely(!page)) {
24881c6fdbd8SKent Overstreet 			ret = -ENOMEM;
24891c6fdbd8SKent Overstreet 			goto out;
24901c6fdbd8SKent Overstreet 		}
24911c6fdbd8SKent Overstreet 	}
24921c6fdbd8SKent Overstreet 
2493a99b1cafSKent Overstreet 	s = bch2_page_state_create(page, 0);
2494a99b1cafSKent Overstreet 	if (!s) {
2495a99b1cafSKent Overstreet 		ret = -ENOMEM;
2496a99b1cafSKent Overstreet 		goto unlock;
2497a99b1cafSKent Overstreet 	}
2498a99b1cafSKent Overstreet 
24991c6fdbd8SKent Overstreet 	if (!PageUptodate(page)) {
25001c6fdbd8SKent Overstreet 		ret = bch2_read_single_page(page, mapping);
25011c6fdbd8SKent Overstreet 		if (ret)
25021c6fdbd8SKent Overstreet 			goto unlock;
25031c6fdbd8SKent Overstreet 	}
25041c6fdbd8SKent Overstreet 
2505a99b1cafSKent Overstreet 	if (index != start >> PAGE_SHIFT)
2506a99b1cafSKent Overstreet 		start_offset = 0;
2507a99b1cafSKent Overstreet 	if (index != end >> PAGE_SHIFT)
2508a99b1cafSKent Overstreet 		end_offset = PAGE_SIZE;
2509a99b1cafSKent Overstreet 
2510a99b1cafSKent Overstreet 	for (i = round_up(start_offset, block_bytes(c)) >> 9;
2511a99b1cafSKent Overstreet 	     i < round_down(end_offset, block_bytes(c)) >> 9;
2512a99b1cafSKent Overstreet 	     i++) {
2513a99b1cafSKent Overstreet 		s->s[i].nr_replicas	= 0;
2514b19d307dSKent Overstreet 		if (s->s[i].state == SECTOR_DIRTY)
2515b19d307dSKent Overstreet 			i_sectors_delta--;
2516a99b1cafSKent Overstreet 		s->s[i].state		= SECTOR_UNALLOCATED;
2517a99b1cafSKent Overstreet 	}
2518a99b1cafSKent Overstreet 
2519b19d307dSKent Overstreet 	i_sectors_acct(c, inode, NULL, i_sectors_delta);
2520b19d307dSKent Overstreet 
252174163da7SKent Overstreet 	/*
252274163da7SKent Overstreet 	 * Caller needs to know whether this page will be written out by
252374163da7SKent Overstreet 	 * writeback - doing an i_size update if necessary - or whether it will
252474163da7SKent Overstreet 	 * be responsible for the i_size update:
252574163da7SKent Overstreet 	 */
252674163da7SKent Overstreet 	ret = s->s[(min_t(u64, inode->v.i_size - (index << PAGE_SHIFT),
252774163da7SKent Overstreet 			  PAGE_SIZE) - 1) >> 9].state >= SECTOR_DIRTY;
252874163da7SKent Overstreet 
2529a99b1cafSKent Overstreet 	zero_user_segment(page, start_offset, end_offset);
2530a99b1cafSKent Overstreet 
25311c6fdbd8SKent Overstreet 	/*
25321c6fdbd8SKent Overstreet 	 * Bit of a hack - we don't want truncate to fail due to -ENOSPC.
25331c6fdbd8SKent Overstreet 	 *
25341c6fdbd8SKent Overstreet 	 * XXX: because we aren't currently tracking whether the page has actual
25351c6fdbd8SKent Overstreet 	 * data in it (vs. just 0s, or only partially written) this wrong. ick.
25361c6fdbd8SKent Overstreet 	 */
253774163da7SKent Overstreet 	BUG_ON(bch2_get_page_disk_reservation(c, inode, page, false));
25381c6fdbd8SKent Overstreet 
25399ba2eb25SKent Overstreet 	/*
25409ba2eb25SKent Overstreet 	 * This removes any writeable userspace mappings; we need to force
25419ba2eb25SKent Overstreet 	 * .page_mkwrite to be called again before any mmapped writes, to
25429ba2eb25SKent Overstreet 	 * redirty the full page:
25439ba2eb25SKent Overstreet 	 */
25449ba2eb25SKent Overstreet 	page_mkclean(page);
2545d1542e03SKent Overstreet 	filemap_dirty_folio(mapping, page_folio(page));
25461c6fdbd8SKent Overstreet unlock:
25471c6fdbd8SKent Overstreet 	unlock_page(page);
25481c6fdbd8SKent Overstreet 	put_page(page);
25491c6fdbd8SKent Overstreet out:
25501c6fdbd8SKent Overstreet 	return ret;
25511c6fdbd8SKent Overstreet }
25521c6fdbd8SKent Overstreet 
25531c6fdbd8SKent Overstreet static int bch2_truncate_page(struct bch_inode_info *inode, loff_t from)
25541c6fdbd8SKent Overstreet {
25551c6fdbd8SKent Overstreet 	return __bch2_truncate_page(inode, from >> PAGE_SHIFT,
2556a99b1cafSKent Overstreet 				    from, round_up(from, PAGE_SIZE));
25571c6fdbd8SKent Overstreet }
25581c6fdbd8SKent Overstreet 
255974163da7SKent Overstreet static int bch2_truncate_pages(struct bch_inode_info *inode,
256074163da7SKent Overstreet 			       loff_t start, loff_t end)
256174163da7SKent Overstreet {
256274163da7SKent Overstreet 	int ret = __bch2_truncate_page(inode, start >> PAGE_SHIFT,
256374163da7SKent Overstreet 				       start, end);
256474163da7SKent Overstreet 
256574163da7SKent Overstreet 	if (ret >= 0 &&
256674163da7SKent Overstreet 	    start >> PAGE_SHIFT != end >> PAGE_SHIFT)
256774163da7SKent Overstreet 		ret = __bch2_truncate_page(inode,
256874163da7SKent Overstreet 					   end >> PAGE_SHIFT,
256974163da7SKent Overstreet 					   start, end);
257074163da7SKent Overstreet 	return ret;
257174163da7SKent Overstreet }
257274163da7SKent Overstreet 
257368a507a2SKent Overstreet static int bch2_extend(struct mnt_idmap *idmap,
257468a507a2SKent Overstreet 		       struct bch_inode_info *inode,
2575e0541a93SKent Overstreet 		       struct bch_inode_unpacked *inode_u,
2576e0541a93SKent Overstreet 		       struct iattr *iattr)
25771c6fdbd8SKent Overstreet {
25781c6fdbd8SKent Overstreet 	struct address_space *mapping = inode->v.i_mapping;
25791c6fdbd8SKent Overstreet 	int ret;
25801c6fdbd8SKent Overstreet 
2581e0541a93SKent Overstreet 	/*
2582e0541a93SKent Overstreet 	 * sync appends:
25832925fc49SKent Overstreet 	 *
25842925fc49SKent Overstreet 	 * this has to be done _before_ extending i_size:
2585e0541a93SKent Overstreet 	 */
2586e0541a93SKent Overstreet 	ret = filemap_write_and_wait_range(mapping, inode_u->bi_size, S64_MAX);
25871c6fdbd8SKent Overstreet 	if (ret)
25881c6fdbd8SKent Overstreet 		return ret;
25891c6fdbd8SKent Overstreet 
25901c6fdbd8SKent Overstreet 	truncate_setsize(&inode->v, iattr->ia_size);
25911c6fdbd8SKent Overstreet 
259268a507a2SKent Overstreet 	return bch2_setattr_nonsize(idmap, inode, iattr);
25931c6fdbd8SKent Overstreet }
25941c6fdbd8SKent Overstreet 
259554e2264eSKent Overstreet static int bch2_truncate_finish_fn(struct bch_inode_info *inode,
259654e2264eSKent Overstreet 				   struct bch_inode_unpacked *bi,
259754e2264eSKent Overstreet 				   void *p)
259854e2264eSKent Overstreet {
259954e2264eSKent Overstreet 	bi->bi_flags &= ~BCH_INODE_I_SIZE_DIRTY;
260054e2264eSKent Overstreet 	return 0;
260154e2264eSKent Overstreet }
260254e2264eSKent Overstreet 
260354e2264eSKent Overstreet static int bch2_truncate_start_fn(struct bch_inode_info *inode,
260454e2264eSKent Overstreet 				  struct bch_inode_unpacked *bi, void *p)
260554e2264eSKent Overstreet {
260654e2264eSKent Overstreet 	u64 *new_i_size = p;
260754e2264eSKent Overstreet 
260854e2264eSKent Overstreet 	bi->bi_flags |= BCH_INODE_I_SIZE_DIRTY;
260954e2264eSKent Overstreet 	bi->bi_size = *new_i_size;
261054e2264eSKent Overstreet 	return 0;
261154e2264eSKent Overstreet }
261254e2264eSKent Overstreet 
261368a507a2SKent Overstreet int bch2_truncate(struct mnt_idmap *idmap,
261468a507a2SKent Overstreet 		  struct bch_inode_info *inode, struct iattr *iattr)
26151c6fdbd8SKent Overstreet {
26161c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
26171c6fdbd8SKent Overstreet 	struct address_space *mapping = inode->v.i_mapping;
2618e0541a93SKent Overstreet 	struct bch_inode_unpacked inode_u;
261954e2264eSKent Overstreet 	u64 new_i_size = iattr->ia_size;
26202e87eae1SKent Overstreet 	s64 i_sectors_delta = 0;
26211c6fdbd8SKent Overstreet 	int ret = 0;
26221c6fdbd8SKent Overstreet 
262368a507a2SKent Overstreet 	/*
262478d66ab1SDan Robertson 	 * If the truncate call with change the size of the file, the
262578d66ab1SDan Robertson 	 * cmtimes should be updated. If the size will not change, we
262678d66ab1SDan Robertson 	 * do not need to update the cmtimes.
262768a507a2SKent Overstreet 	 */
262878d66ab1SDan Robertson 	if (iattr->ia_size != inode->v.i_size) {
262968a507a2SKent Overstreet 		if (!(iattr->ia_valid & ATTR_MTIME))
263068a507a2SKent Overstreet 			ktime_get_coarse_real_ts64(&iattr->ia_mtime);
263168a507a2SKent Overstreet 		if (!(iattr->ia_valid & ATTR_CTIME))
263268a507a2SKent Overstreet 			ktime_get_coarse_real_ts64(&iattr->ia_ctime);
263368a507a2SKent Overstreet 		iattr->ia_valid |= ATTR_MTIME|ATTR_CTIME;
263478d66ab1SDan Robertson 	}
263568a507a2SKent Overstreet 
26361c6fdbd8SKent Overstreet 	inode_dio_wait(&inode->v);
26371c6fdbd8SKent Overstreet 	bch2_pagecache_block_get(&inode->ei_pagecache_lock);
26381c6fdbd8SKent Overstreet 
26396fed42bbSKent Overstreet 	ret = bch2_inode_find_by_inum(c, inode_inum(inode), &inode_u);
2640e0541a93SKent Overstreet 	if (ret)
2641e0541a93SKent Overstreet 		goto err;
26421c6fdbd8SKent Overstreet 
2643c45d473dSKent Overstreet 	/*
2644c45d473dSKent Overstreet 	 * check this before next assertion; on filesystem error our normal
2645c45d473dSKent Overstreet 	 * invariants are a bit broken (truncate has to truncate the page cache
2646c45d473dSKent Overstreet 	 * before the inode).
2647c45d473dSKent Overstreet 	 */
2648c45d473dSKent Overstreet 	ret = bch2_journal_error(&c->journal);
2649c45d473dSKent Overstreet 	if (ret)
2650c45d473dSKent Overstreet 		goto err;
2651c45d473dSKent Overstreet 
265233c74e41SKent Overstreet 	WARN_ON(!test_bit(EI_INODE_ERROR, &inode->ei_flags) &&
265333c74e41SKent Overstreet 		inode->v.i_size < inode_u.bi_size);
2654e0541a93SKent Overstreet 
2655e0541a93SKent Overstreet 	if (iattr->ia_size > inode->v.i_size) {
265668a507a2SKent Overstreet 		ret = bch2_extend(idmap, inode, &inode_u, iattr);
265754e2264eSKent Overstreet 		goto err;
26581c6fdbd8SKent Overstreet 	}
26591c6fdbd8SKent Overstreet 
266068a507a2SKent Overstreet 	iattr->ia_valid &= ~ATTR_SIZE;
266168a507a2SKent Overstreet 
26621c6fdbd8SKent Overstreet 	ret = bch2_truncate_page(inode, iattr->ia_size);
266374163da7SKent Overstreet 	if (unlikely(ret < 0))
266454e2264eSKent Overstreet 		goto err;
26651c6fdbd8SKent Overstreet 
26666cc3535dSKent Overstreet 	/*
26676cc3535dSKent Overstreet 	 * When extending, we're going to write the new i_size to disk
26686cc3535dSKent Overstreet 	 * immediately so we need to flush anything above the current on disk
26696cc3535dSKent Overstreet 	 * i_size first:
26706cc3535dSKent Overstreet 	 *
26716cc3535dSKent Overstreet 	 * Also, when extending we need to flush the page that i_size currently
26726cc3535dSKent Overstreet 	 * straddles - if it's mapped to userspace, we need to ensure that
26736cc3535dSKent Overstreet 	 * userspace has to redirty it and call .mkwrite -> set_page_dirty
26746cc3535dSKent Overstreet 	 * again to allocate the part of the page that was extended.
26756cc3535dSKent Overstreet 	 */
2676e0541a93SKent Overstreet 	if (iattr->ia_size > inode_u.bi_size)
26771c6fdbd8SKent Overstreet 		ret = filemap_write_and_wait_range(mapping,
2678e0541a93SKent Overstreet 				inode_u.bi_size,
26791c6fdbd8SKent Overstreet 				iattr->ia_size - 1);
26801c6fdbd8SKent Overstreet 	else if (iattr->ia_size & (PAGE_SIZE - 1))
26811c6fdbd8SKent Overstreet 		ret = filemap_write_and_wait_range(mapping,
26821c6fdbd8SKent Overstreet 				round_down(iattr->ia_size, PAGE_SIZE),
26831c6fdbd8SKent Overstreet 				iattr->ia_size - 1);
26841c6fdbd8SKent Overstreet 	if (ret)
268554e2264eSKent Overstreet 		goto err;
26861c6fdbd8SKent Overstreet 
268754e2264eSKent Overstreet 	mutex_lock(&inode->ei_update_lock);
268854e2264eSKent Overstreet 	ret = bch2_write_inode(c, inode, bch2_truncate_start_fn,
268954e2264eSKent Overstreet 			       &new_i_size, 0);
269054e2264eSKent Overstreet 	mutex_unlock(&inode->ei_update_lock);
26911c6fdbd8SKent Overstreet 
26921c6fdbd8SKent Overstreet 	if (unlikely(ret))
269354e2264eSKent Overstreet 		goto err;
26941c6fdbd8SKent Overstreet 
26951c6fdbd8SKent Overstreet 	truncate_setsize(&inode->v, iattr->ia_size);
26961c6fdbd8SKent Overstreet 
26978c6d298aSKent Overstreet 	ret = bch2_fpunch(c, inode_inum(inode),
2698a99b1cafSKent Overstreet 			round_up(iattr->ia_size, block_bytes(c)) >> 9,
269968a2054dSKent Overstreet 			U64_MAX, &i_sectors_delta);
27002e87eae1SKent Overstreet 	i_sectors_acct(c, inode, NULL, i_sectors_delta);
27012e87eae1SKent Overstreet 
2702*dcfc593fSKent Overstreet 	BUG_ON(!inode->v.i_size && inode->v.i_blocks);
2703*dcfc593fSKent Overstreet 
27041c6fdbd8SKent Overstreet 	if (unlikely(ret))
270554e2264eSKent Overstreet 		goto err;
27061c6fdbd8SKent Overstreet 
270754e2264eSKent Overstreet 	mutex_lock(&inode->ei_update_lock);
270868a507a2SKent Overstreet 	ret = bch2_write_inode(c, inode, bch2_truncate_finish_fn, NULL, 0);
270954e2264eSKent Overstreet 	mutex_unlock(&inode->ei_update_lock);
271068a507a2SKent Overstreet 
271168a507a2SKent Overstreet 	ret = bch2_setattr_nonsize(idmap, inode, iattr);
271254e2264eSKent Overstreet err:
27131c6fdbd8SKent Overstreet 	bch2_pagecache_block_put(&inode->ei_pagecache_lock);
27141c6fdbd8SKent Overstreet 	return ret;
27151c6fdbd8SKent Overstreet }
27161c6fdbd8SKent Overstreet 
27171c6fdbd8SKent Overstreet /* fallocate: */
27181c6fdbd8SKent Overstreet 
2719050197b1SKent Overstreet static int inode_update_times_fn(struct bch_inode_info *inode,
2720050197b1SKent Overstreet 				 struct bch_inode_unpacked *bi, void *p)
2721050197b1SKent Overstreet {
2722050197b1SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
2723050197b1SKent Overstreet 
2724050197b1SKent Overstreet 	bi->bi_mtime = bi->bi_ctime = bch2_current_time(c);
2725050197b1SKent Overstreet 	return 0;
2726050197b1SKent Overstreet }
2727050197b1SKent Overstreet 
27282e87eae1SKent Overstreet static long bchfs_fpunch(struct bch_inode_info *inode, loff_t offset, loff_t len)
27291c6fdbd8SKent Overstreet {
27301c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
273174163da7SKent Overstreet 	u64 end		= offset + len;
273274163da7SKent Overstreet 	u64 block_start	= round_up(offset, block_bytes(c));
273374163da7SKent Overstreet 	u64 block_end	= round_down(end, block_bytes(c));
273474163da7SKent Overstreet 	bool truncated_last_page;
27351c6fdbd8SKent Overstreet 	int ret = 0;
27361c6fdbd8SKent Overstreet 
273774163da7SKent Overstreet 	ret = bch2_truncate_pages(inode, offset, end);
273874163da7SKent Overstreet 	if (unlikely(ret < 0))
27391c6fdbd8SKent Overstreet 		goto err;
27401c6fdbd8SKent Overstreet 
274174163da7SKent Overstreet 	truncated_last_page = ret;
27421c6fdbd8SKent Overstreet 
274374163da7SKent Overstreet 	truncate_pagecache_range(&inode->v, offset, end - 1);
27441c6fdbd8SKent Overstreet 
274574163da7SKent Overstreet 	if (block_start < block_end ) {
27462e87eae1SKent Overstreet 		s64 i_sectors_delta = 0;
27472e87eae1SKent Overstreet 
27488c6d298aSKent Overstreet 		ret = bch2_fpunch(c, inode_inum(inode),
274974163da7SKent Overstreet 				  block_start >> 9, block_end >> 9,
27502e87eae1SKent Overstreet 				  &i_sectors_delta);
27512e87eae1SKent Overstreet 		i_sectors_acct(c, inode, NULL, i_sectors_delta);
27522e87eae1SKent Overstreet 	}
2753050197b1SKent Overstreet 
2754050197b1SKent Overstreet 	mutex_lock(&inode->ei_update_lock);
275574163da7SKent Overstreet 	if (end >= inode->v.i_size && !truncated_last_page) {
275674163da7SKent Overstreet 		ret = bch2_write_inode_size(c, inode, inode->v.i_size,
275774163da7SKent Overstreet 					    ATTR_MTIME|ATTR_CTIME);
275874163da7SKent Overstreet 	} else {
2759050197b1SKent Overstreet 		ret = bch2_write_inode(c, inode, inode_update_times_fn, NULL,
276074163da7SKent Overstreet 				       ATTR_MTIME|ATTR_CTIME);
276174163da7SKent Overstreet 	}
2762050197b1SKent Overstreet 	mutex_unlock(&inode->ei_update_lock);
27631c6fdbd8SKent Overstreet err:
27641c6fdbd8SKent Overstreet 	return ret;
27651c6fdbd8SKent Overstreet }
27661c6fdbd8SKent Overstreet 
27672e87eae1SKent Overstreet static long bchfs_fcollapse_finsert(struct bch_inode_info *inode,
27685f786787SKent Overstreet 				   loff_t offset, loff_t len,
27695f786787SKent Overstreet 				   bool insert)
27701c6fdbd8SKent Overstreet {
27711c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
27721c6fdbd8SKent Overstreet 	struct address_space *mapping = inode->v.i_mapping;
277307a1006aSKent Overstreet 	struct bkey_buf copy;
2774d69f41d6SKent Overstreet 	struct btree_trans trans;
277567e0dd8fSKent Overstreet 	struct btree_iter src, dst, del;
27765f786787SKent Overstreet 	loff_t shift, new_size;
27775f786787SKent Overstreet 	u64 src_start;
277850dc0f69SKent Overstreet 	int ret = 0;
27791c6fdbd8SKent Overstreet 
27801c6fdbd8SKent Overstreet 	if ((offset | len) & (block_bytes(c) - 1))
27811c6fdbd8SKent Overstreet 		return -EINVAL;
27821c6fdbd8SKent Overstreet 
27835f786787SKent Overstreet 	if (insert) {
27845f786787SKent Overstreet 		if (inode->v.i_sb->s_maxbytes - inode->v.i_size < len)
278574163da7SKent Overstreet 			return -EFBIG;
27865f786787SKent Overstreet 
27875f786787SKent Overstreet 		if (offset >= inode->v.i_size)
278874163da7SKent Overstreet 			return -EINVAL;
27895f786787SKent Overstreet 
27905f786787SKent Overstreet 		src_start	= U64_MAX;
27915f786787SKent Overstreet 		shift		= len;
27925f786787SKent Overstreet 	} else {
27931c6fdbd8SKent Overstreet 		if (offset + len >= inode->v.i_size)
279474163da7SKent Overstreet 			return -EINVAL;
27951c6fdbd8SKent Overstreet 
27965f786787SKent Overstreet 		src_start	= offset + len;
27975f786787SKent Overstreet 		shift		= -len;
27985f786787SKent Overstreet 	}
27991c6fdbd8SKent Overstreet 
28005f786787SKent Overstreet 	new_size = inode->v.i_size + shift;
28011c6fdbd8SKent Overstreet 
28021c6fdbd8SKent Overstreet 	ret = write_invalidate_inode_pages_range(mapping, offset, LLONG_MAX);
28031c6fdbd8SKent Overstreet 	if (ret)
280474163da7SKent Overstreet 		return ret;
28051c6fdbd8SKent Overstreet 
28065f786787SKent Overstreet 	if (insert) {
28075f786787SKent Overstreet 		i_size_write(&inode->v, new_size);
28085f786787SKent Overstreet 		mutex_lock(&inode->ei_update_lock);
28095f786787SKent Overstreet 		ret = bch2_write_inode_size(c, inode, new_size,
28105f786787SKent Overstreet 					    ATTR_MTIME|ATTR_CTIME);
28115f786787SKent Overstreet 		mutex_unlock(&inode->ei_update_lock);
28125f786787SKent Overstreet 	} else {
28132e87eae1SKent Overstreet 		s64 i_sectors_delta = 0;
28142e87eae1SKent Overstreet 
28158c6d298aSKent Overstreet 		ret = bch2_fpunch(c, inode_inum(inode),
28162e87eae1SKent Overstreet 				  offset >> 9, (offset + len) >> 9,
28172e87eae1SKent Overstreet 				  &i_sectors_delta);
28182e87eae1SKent Overstreet 		i_sectors_acct(c, inode, NULL, i_sectors_delta);
28192e87eae1SKent Overstreet 
282063095894SKent Overstreet 		if (ret)
282174163da7SKent Overstreet 			return ret;
28225f786787SKent Overstreet 	}
28238ef231bdSKent Overstreet 
282450dc0f69SKent Overstreet 	bch2_bkey_buf_init(&copy);
2825f7beb4caSKent Overstreet 	bch2_trans_init(&trans, c, BTREE_ITER_MAX, 1024);
282667e0dd8fSKent Overstreet 	bch2_trans_iter_init(&trans, &src, BTREE_ID_extents,
28275f786787SKent Overstreet 			POS(inode->v.i_ino, src_start >> 9),
282863095894SKent Overstreet 			BTREE_ITER_INTENT);
282967e0dd8fSKent Overstreet 	bch2_trans_copy_iter(&dst, &src);
283067e0dd8fSKent Overstreet 	bch2_trans_copy_iter(&del, &src);
28315f786787SKent Overstreet 
283250dc0f69SKent Overstreet 	while (ret == 0 || ret == -EINTR) {
283363095894SKent Overstreet 		struct disk_reservation disk_res =
283463095894SKent Overstreet 			bch2_disk_reservation_init(c, 0);
283563095894SKent Overstreet 		struct bkey_i delete;
283663095894SKent Overstreet 		struct bkey_s_c k;
283763095894SKent Overstreet 		struct bpos next_pos;
28385f786787SKent Overstreet 		struct bpos move_pos = POS(inode->v.i_ino, offset >> 9);
28395f786787SKent Overstreet 		struct bpos atomic_end;
28402d594dfbSKent Overstreet 		unsigned trigger_flags = 0;
28416fed42bbSKent Overstreet 		u32 snapshot;
28426fed42bbSKent Overstreet 
28436fed42bbSKent Overstreet 		bch2_trans_begin(&trans);
28446fed42bbSKent Overstreet 
28456fed42bbSKent Overstreet 		ret = bch2_subvolume_get_snapshot(&trans,
28466fed42bbSKent Overstreet 					inode->ei_subvol, &snapshot);
28476fed42bbSKent Overstreet 		if (ret)
28486fed42bbSKent Overstreet 			continue;
28496fed42bbSKent Overstreet 
28506fed42bbSKent Overstreet 		bch2_btree_iter_set_snapshot(&src, snapshot);
28516fed42bbSKent Overstreet 		bch2_btree_iter_set_snapshot(&dst, snapshot);
28526fed42bbSKent Overstreet 		bch2_btree_iter_set_snapshot(&del, snapshot);
285363095894SKent Overstreet 
2854700c25b3SKent Overstreet 		bch2_trans_begin(&trans);
2855700c25b3SKent Overstreet 
28565f786787SKent Overstreet 		k = insert
285767e0dd8fSKent Overstreet 			? bch2_btree_iter_peek_prev(&src)
285867e0dd8fSKent Overstreet 			: bch2_btree_iter_peek(&src);
285963095894SKent Overstreet 		if ((ret = bkey_err(k)))
286050dc0f69SKent Overstreet 			continue;
286163095894SKent Overstreet 
286263095894SKent Overstreet 		if (!k.k || k.k->p.inode != inode->v.i_ino)
286363095894SKent Overstreet 			break;
286463095894SKent Overstreet 
28655f786787SKent Overstreet 		if (insert &&
28665f786787SKent Overstreet 		    bkey_cmp(k.k->p, POS(inode->v.i_ino, offset >> 9)) <= 0)
28675f786787SKent Overstreet 			break;
28685f786787SKent Overstreet reassemble:
286907a1006aSKent Overstreet 		bch2_bkey_buf_reassemble(&copy, c, k);
28705f786787SKent Overstreet 
28715f786787SKent Overstreet 		if (insert &&
2872283eda57SKent Overstreet 		    bkey_cmp(bkey_start_pos(k.k), move_pos) < 0)
287335189e09SKent Overstreet 			bch2_cut_front(move_pos, copy.k);
28745f786787SKent Overstreet 
287535189e09SKent Overstreet 		copy.k->k.p.offset += shift >> 9;
287667e0dd8fSKent Overstreet 		bch2_btree_iter_set_pos(&dst, bkey_start_pos(&copy.k->k));
28771c6fdbd8SKent Overstreet 
287867e0dd8fSKent Overstreet 		ret = bch2_extent_atomic_end(&trans, &dst, copy.k, &atomic_end);
28793c7f3b7aSKent Overstreet 		if (ret)
288050dc0f69SKent Overstreet 			continue;
2881e2d9912cSKent Overstreet 
288235189e09SKent Overstreet 		if (bkey_cmp(atomic_end, copy.k->k.p)) {
28835f786787SKent Overstreet 			if (insert) {
28845f786787SKent Overstreet 				move_pos = atomic_end;
28855f786787SKent Overstreet 				move_pos.offset -= shift >> 9;
28865f786787SKent Overstreet 				goto reassemble;
28875f786787SKent Overstreet 			} else {
2888085ab693SKent Overstreet 				bch2_cut_back(atomic_end, copy.k);
28895f786787SKent Overstreet 			}
28905f786787SKent Overstreet 		}
28915f786787SKent Overstreet 
289263095894SKent Overstreet 		bkey_init(&delete.k);
2893283eda57SKent Overstreet 		delete.k.p = copy.k->k.p;
2894283eda57SKent Overstreet 		delete.k.size = copy.k->k.size;
2895283eda57SKent Overstreet 		delete.k.p.offset -= shift >> 9;
289667e0dd8fSKent Overstreet 		bch2_btree_iter_set_pos(&del, bkey_start_pos(&delete.k));
28971c6fdbd8SKent Overstreet 
28985f786787SKent Overstreet 		next_pos = insert ? bkey_start_pos(&delete.k) : delete.k.p;
289963095894SKent Overstreet 
290035189e09SKent Overstreet 		if (copy.k->k.size == k.k->size) {
290163095894SKent Overstreet 			/*
290263095894SKent Overstreet 			 * If we're moving the entire extent, we can skip
290363095894SKent Overstreet 			 * running triggers:
290463095894SKent Overstreet 			 */
29052d594dfbSKent Overstreet 			trigger_flags |= BTREE_TRIGGER_NORUN;
290663095894SKent Overstreet 		} else {
290763095894SKent Overstreet 			/* We might end up splitting compressed extents: */
290863095894SKent Overstreet 			unsigned nr_ptrs =
29094de77495SKent Overstreet 				bch2_bkey_nr_ptrs_allocated(bkey_i_to_s_c(copy.k));
291063095894SKent Overstreet 
291163095894SKent Overstreet 			ret = bch2_disk_reservation_get(c, &disk_res,
291235189e09SKent Overstreet 					copy.k->k.size, nr_ptrs,
29131c6fdbd8SKent Overstreet 					BCH_DISK_RESERVATION_NOFAIL);
29141c6fdbd8SKent Overstreet 			BUG_ON(ret);
291563095894SKent Overstreet 		}
29161c6fdbd8SKent Overstreet 
291767e0dd8fSKent Overstreet 		ret =   bch2_btree_iter_traverse(&del) ?:
291867e0dd8fSKent Overstreet 			bch2_trans_update(&trans, &del, &delete, trigger_flags) ?:
291967e0dd8fSKent Overstreet 			bch2_trans_update(&trans, &dst, copy.k, trigger_flags) ?:
292068a2054dSKent Overstreet 			bch2_trans_commit(&trans, &disk_res, NULL,
29212d594dfbSKent Overstreet 					  BTREE_INSERT_NOFAIL);
29221c6fdbd8SKent Overstreet 		bch2_disk_reservation_put(c, &disk_res);
292350dc0f69SKent Overstreet 
292463095894SKent Overstreet 		if (!ret)
292567e0dd8fSKent Overstreet 			bch2_btree_iter_set_pos(&src, next_pos);
292650dc0f69SKent Overstreet 	}
292767e0dd8fSKent Overstreet 	bch2_trans_iter_exit(&trans, &del);
292867e0dd8fSKent Overstreet 	bch2_trans_iter_exit(&trans, &dst);
292967e0dd8fSKent Overstreet 	bch2_trans_iter_exit(&trans, &src);
293050dc0f69SKent Overstreet 	bch2_trans_exit(&trans);
293150dc0f69SKent Overstreet 	bch2_bkey_buf_exit(&copy, c);
293263095894SKent Overstreet 
29338ef231bdSKent Overstreet 	if (ret)
293474163da7SKent Overstreet 		return ret;
29351c6fdbd8SKent Overstreet 
293674163da7SKent Overstreet 	mutex_lock(&inode->ei_update_lock);
29375f786787SKent Overstreet 	if (!insert) {
29388ef231bdSKent Overstreet 		i_size_write(&inode->v, new_size);
29398ef231bdSKent Overstreet 		ret = bch2_write_inode_size(c, inode, new_size,
29408ef231bdSKent Overstreet 					    ATTR_MTIME|ATTR_CTIME);
294174163da7SKent Overstreet 	} else {
294274163da7SKent Overstreet 		/* We need an inode update to update bi_journal_seq for fsync: */
294374163da7SKent Overstreet 		ret = bch2_write_inode(c, inode, inode_update_times_fn, NULL,
294474163da7SKent Overstreet 				       ATTR_MTIME|ATTR_CTIME);
29455f786787SKent Overstreet 	}
294674163da7SKent Overstreet 	mutex_unlock(&inode->ei_update_lock);
29471c6fdbd8SKent Overstreet 	return ret;
29481c6fdbd8SKent Overstreet }
29491c6fdbd8SKent Overstreet 
2950694015c2SKent Overstreet static int __bchfs_fallocate(struct bch_inode_info *inode, int mode,
2951694015c2SKent Overstreet 			     u64 start_sector, u64 end_sector)
29521c6fdbd8SKent Overstreet {
29531c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
2954190fa7afSKent Overstreet 	struct btree_trans trans;
295567e0dd8fSKent Overstreet 	struct btree_iter iter;
2956694015c2SKent Overstreet 	struct bpos end_pos = POS(inode->v.i_ino, end_sector);
29579a3df993SKent Overstreet 	unsigned replicas = io_opts(c, &inode->ei_inode).data_replicas;
2958694015c2SKent Overstreet 	int ret = 0;
29591c6fdbd8SKent Overstreet 
2960f7beb4caSKent Overstreet 	bch2_trans_init(&trans, c, BTREE_ITER_MAX, 512);
29611c6fdbd8SKent Overstreet 
296267e0dd8fSKent Overstreet 	bch2_trans_iter_init(&trans, &iter, BTREE_ID_extents,
2963694015c2SKent Overstreet 			POS(inode->v.i_ino, start_sector),
2964190fa7afSKent Overstreet 			BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
29651c6fdbd8SKent Overstreet 
296667e0dd8fSKent Overstreet 	while (!ret && bkey_cmp(iter.pos, end_pos) < 0) {
29672e87eae1SKent Overstreet 		s64 i_sectors_delta = 0;
29681c6fdbd8SKent Overstreet 		struct disk_reservation disk_res = { 0 };
2969190fa7afSKent Overstreet 		struct quota_res quota_res = { 0 };
29701c6fdbd8SKent Overstreet 		struct bkey_i_reservation reservation;
29711c6fdbd8SKent Overstreet 		struct bkey_s_c k;
2972694015c2SKent Overstreet 		unsigned sectors;
29736fed42bbSKent Overstreet 		u32 snapshot;
29741c6fdbd8SKent Overstreet 
2975163e885aSKent Overstreet 		bch2_trans_begin(&trans);
2976a8abd3a7SKent Overstreet 
29776fed42bbSKent Overstreet 		ret = bch2_subvolume_get_snapshot(&trans,
29786fed42bbSKent Overstreet 					inode->ei_subvol, &snapshot);
29796fed42bbSKent Overstreet 		if (ret)
29806fed42bbSKent Overstreet 			goto bkey_err;
29816fed42bbSKent Overstreet 
29826fed42bbSKent Overstreet 		bch2_btree_iter_set_snapshot(&iter, snapshot);
29836fed42bbSKent Overstreet 
298467e0dd8fSKent Overstreet 		k = bch2_btree_iter_peek_slot(&iter);
29850f238367SKent Overstreet 		if ((ret = bkey_err(k)))
29860f238367SKent Overstreet 			goto bkey_err;
29871c6fdbd8SKent Overstreet 
29881c6fdbd8SKent Overstreet 		/* already reserved */
298926609b61SKent Overstreet 		if (k.k->type == KEY_TYPE_reservation &&
29901c6fdbd8SKent Overstreet 		    bkey_s_c_to_reservation(k).v->nr_replicas >= replicas) {
299167e0dd8fSKent Overstreet 			bch2_btree_iter_advance(&iter);
29921c6fdbd8SKent Overstreet 			continue;
29931c6fdbd8SKent Overstreet 		}
29941c6fdbd8SKent Overstreet 
2995190fa7afSKent Overstreet 		if (bkey_extent_is_data(k.k) &&
2996190fa7afSKent Overstreet 		    !(mode & FALLOC_FL_ZERO_RANGE)) {
299767e0dd8fSKent Overstreet 			bch2_btree_iter_advance(&iter);
29981c6fdbd8SKent Overstreet 			continue;
29991c6fdbd8SKent Overstreet 		}
30001c6fdbd8SKent Overstreet 
30011c6fdbd8SKent Overstreet 		bkey_reservation_init(&reservation.k_i);
300226609b61SKent Overstreet 		reservation.k.type	= KEY_TYPE_reservation;
30031c6fdbd8SKent Overstreet 		reservation.k.p		= k.k->p;
30041c6fdbd8SKent Overstreet 		reservation.k.size	= k.k->size;
30051c6fdbd8SKent Overstreet 
300667e0dd8fSKent Overstreet 		bch2_cut_front(iter.pos,	&reservation.k_i);
3007085ab693SKent Overstreet 		bch2_cut_back(end_pos,		&reservation.k_i);
30081c6fdbd8SKent Overstreet 
30091c6fdbd8SKent Overstreet 		sectors = reservation.k.size;
30104de77495SKent Overstreet 		reservation.v.nr_replicas = bch2_bkey_nr_ptrs_allocated(k);
30111c6fdbd8SKent Overstreet 
30121c6fdbd8SKent Overstreet 		if (!bkey_extent_is_allocation(k.k)) {
30131c6fdbd8SKent Overstreet 			ret = bch2_quota_reservation_add(c, inode,
3014190fa7afSKent Overstreet 					&quota_res,
30151c6fdbd8SKent Overstreet 					sectors, true);
30161c6fdbd8SKent Overstreet 			if (unlikely(ret))
30170f238367SKent Overstreet 				goto bkey_err;
30181c6fdbd8SKent Overstreet 		}
30191c6fdbd8SKent Overstreet 
30201c6fdbd8SKent Overstreet 		if (reservation.v.nr_replicas < replicas ||
30214de77495SKent Overstreet 		    bch2_bkey_sectors_compressed(k)) {
30221c6fdbd8SKent Overstreet 			ret = bch2_disk_reservation_get(c, &disk_res, sectors,
30231c6fdbd8SKent Overstreet 							replicas, 0);
30241c6fdbd8SKent Overstreet 			if (unlikely(ret))
30250f238367SKent Overstreet 				goto bkey_err;
30261c6fdbd8SKent Overstreet 
30271c6fdbd8SKent Overstreet 			reservation.v.nr_replicas = disk_res.nr_replicas;
30281c6fdbd8SKent Overstreet 		}
30291c6fdbd8SKent Overstreet 
30308c6d298aSKent Overstreet 		ret = bch2_extent_update(&trans, inode_inum(inode), &iter,
30318c6d298aSKent Overstreet 					 &reservation.k_i,
303268a2054dSKent Overstreet 				&disk_res, NULL,
3033a6336910SKent Overstreet 				0, &i_sectors_delta, true);
30348810386fSKent Overstreet 		if (ret)
30358810386fSKent Overstreet 			goto bkey_err;
30362e87eae1SKent Overstreet 		i_sectors_acct(c, inode, &quota_res, i_sectors_delta);
30370f238367SKent Overstreet bkey_err:
3038190fa7afSKent Overstreet 		bch2_quota_reservation_put(c, inode, &quota_res);
30391c6fdbd8SKent Overstreet 		bch2_disk_reservation_put(c, &disk_res);
30401c6fdbd8SKent Overstreet 		if (ret == -EINTR)
30411c6fdbd8SKent Overstreet 			ret = 0;
304250dc0f69SKent Overstreet 	}
304374163da7SKent Overstreet 
3044*dcfc593fSKent Overstreet 	bch2_trans_unlock(&trans); /* lock ordering, before taking pagecache locks: */
3045*dcfc593fSKent Overstreet 	mark_pagecache_reserved(inode, start_sector, iter.pos.offset);
3046*dcfc593fSKent Overstreet 
304774163da7SKent Overstreet 	if (ret == -ENOSPC && (mode & FALLOC_FL_ZERO_RANGE)) {
304874163da7SKent Overstreet 		struct quota_res quota_res = { 0 };
304974163da7SKent Overstreet 		s64 i_sectors_delta = 0;
305074163da7SKent Overstreet 
305174163da7SKent Overstreet 		bch2_fpunch_at(&trans, &iter, inode_inum(inode),
305274163da7SKent Overstreet 			       end_sector, &i_sectors_delta);
305374163da7SKent Overstreet 		i_sectors_acct(c, inode, &quota_res, i_sectors_delta);
305474163da7SKent Overstreet 		bch2_quota_reservation_put(c, inode, &quota_res);
305574163da7SKent Overstreet 	}
305674163da7SKent Overstreet 
305767e0dd8fSKent Overstreet 	bch2_trans_iter_exit(&trans, &iter);
3058694015c2SKent Overstreet 	bch2_trans_exit(&trans);
3059694015c2SKent Overstreet 	return ret;
3060694015c2SKent Overstreet }
306150dc0f69SKent Overstreet 
3062694015c2SKent Overstreet static long bchfs_fallocate(struct bch_inode_info *inode, int mode,
3063694015c2SKent Overstreet 			    loff_t offset, loff_t len)
3064694015c2SKent Overstreet {
3065694015c2SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
306674163da7SKent Overstreet 	u64 end		= offset + len;
306774163da7SKent Overstreet 	u64 block_start	= round_down(offset,	block_bytes(c));
306874163da7SKent Overstreet 	u64 block_end	= round_up(end,		block_bytes(c));
306974163da7SKent Overstreet 	bool truncated_last_page = false;
307074163da7SKent Overstreet 	int ret, ret2 = 0;
3071694015c2SKent Overstreet 
3072694015c2SKent Overstreet 	if (!(mode & FALLOC_FL_KEEP_SIZE) && end > inode->v.i_size) {
3073694015c2SKent Overstreet 		ret = inode_newsize_ok(&inode->v, end);
3074694015c2SKent Overstreet 		if (ret)
307574163da7SKent Overstreet 			return ret;
3076694015c2SKent Overstreet 	}
3077694015c2SKent Overstreet 
3078694015c2SKent Overstreet 	if (mode & FALLOC_FL_ZERO_RANGE) {
307974163da7SKent Overstreet 		ret = bch2_truncate_pages(inode, offset, end);
308074163da7SKent Overstreet 		if (unlikely(ret < 0))
308174163da7SKent Overstreet 			return ret;
3082694015c2SKent Overstreet 
308374163da7SKent Overstreet 		truncated_last_page = ret;
3084694015c2SKent Overstreet 
3085694015c2SKent Overstreet 		truncate_pagecache_range(&inode->v, offset, end - 1);
308674163da7SKent Overstreet 
308774163da7SKent Overstreet 		block_start	= round_up(offset,	block_bytes(c));
308874163da7SKent Overstreet 		block_end	= round_down(end,	block_bytes(c));
3089694015c2SKent Overstreet 	}
3090694015c2SKent Overstreet 
3091694015c2SKent Overstreet 	ret = __bchfs_fallocate(inode, mode, block_start >> 9, block_end >> 9);
3092e0541a93SKent Overstreet 
3093e0541a93SKent Overstreet 	/*
309474163da7SKent Overstreet 	 * On -ENOSPC in ZERO_RANGE mode, we still want to do the inode update,
309574163da7SKent Overstreet 	 * so that the VFS cache i_size is consistent with the btree i_size:
3096e0541a93SKent Overstreet 	 */
309774163da7SKent Overstreet 	if (ret &&
309874163da7SKent Overstreet 	    !(ret == -ENOSPC && (mode & FALLOC_FL_ZERO_RANGE)))
309974163da7SKent Overstreet 		return ret;
31001c6fdbd8SKent Overstreet 
310174163da7SKent Overstreet 	if (mode & FALLOC_FL_KEEP_SIZE && end > inode->v.i_size)
3102e0541a93SKent Overstreet 		end = inode->v.i_size;
310374163da7SKent Overstreet 
310474163da7SKent Overstreet 	if (end >= inode->v.i_size &&
310574163da7SKent Overstreet 	    (((mode & FALLOC_FL_ZERO_RANGE) && !truncated_last_page) ||
310674163da7SKent Overstreet 	     !(mode & FALLOC_FL_KEEP_SIZE))) {
310774163da7SKent Overstreet 		spin_lock(&inode->v.i_lock);
3108e0541a93SKent Overstreet 		i_size_write(&inode->v, end);
310974163da7SKent Overstreet 		spin_unlock(&inode->v.i_lock);
3110e0541a93SKent Overstreet 
31111c6fdbd8SKent Overstreet 		mutex_lock(&inode->ei_update_lock);
311274163da7SKent Overstreet 		ret2 = bch2_write_inode_size(c, inode, end, 0);
31131c6fdbd8SKent Overstreet 		mutex_unlock(&inode->ei_update_lock);
31141c6fdbd8SKent Overstreet 	}
311574163da7SKent Overstreet 
311674163da7SKent Overstreet 	return ret ?: ret2;
31171c6fdbd8SKent Overstreet }
31181c6fdbd8SKent Overstreet 
31191c6fdbd8SKent Overstreet long bch2_fallocate_dispatch(struct file *file, int mode,
31201c6fdbd8SKent Overstreet 			     loff_t offset, loff_t len)
31211c6fdbd8SKent Overstreet {
31221c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = file_bch_inode(file);
31232a9101a9SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
31242a9101a9SKent Overstreet 	long ret;
31252a9101a9SKent Overstreet 
31262a9101a9SKent Overstreet 	if (!percpu_ref_tryget(&c->writes))
31272a9101a9SKent Overstreet 		return -EROFS;
31281c6fdbd8SKent Overstreet 
312974163da7SKent Overstreet 	inode_lock(&inode->v);
313074163da7SKent Overstreet 	inode_dio_wait(&inode->v);
313174163da7SKent Overstreet 	bch2_pagecache_block_get(&inode->ei_pagecache_lock);
313274163da7SKent Overstreet 
31331c6fdbd8SKent Overstreet 	if (!(mode & ~(FALLOC_FL_KEEP_SIZE|FALLOC_FL_ZERO_RANGE)))
31342a9101a9SKent Overstreet 		ret = bchfs_fallocate(inode, mode, offset, len);
31352a9101a9SKent Overstreet 	else if (mode == (FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE))
31362a9101a9SKent Overstreet 		ret = bchfs_fpunch(inode, offset, len);
31372a9101a9SKent Overstreet 	else if (mode == FALLOC_FL_INSERT_RANGE)
31382a9101a9SKent Overstreet 		ret = bchfs_fcollapse_finsert(inode, offset, len, true);
31392a9101a9SKent Overstreet 	else if (mode == FALLOC_FL_COLLAPSE_RANGE)
31402a9101a9SKent Overstreet 		ret = bchfs_fcollapse_finsert(inode, offset, len, false);
31412a9101a9SKent Overstreet 	else
31422a9101a9SKent Overstreet 		ret = -EOPNOTSUPP;
31431c6fdbd8SKent Overstreet 
314474163da7SKent Overstreet 
314574163da7SKent Overstreet 	bch2_pagecache_block_put(&inode->ei_pagecache_lock);
314674163da7SKent Overstreet 	inode_unlock(&inode->v);
31472a9101a9SKent Overstreet 	percpu_ref_put(&c->writes);
31481c6fdbd8SKent Overstreet 
31492a9101a9SKent Overstreet 	return ret;
31501c6fdbd8SKent Overstreet }
31511c6fdbd8SKent Overstreet 
315276426098SKent Overstreet loff_t bch2_remap_file_range(struct file *file_src, loff_t pos_src,
315376426098SKent Overstreet 			     struct file *file_dst, loff_t pos_dst,
315476426098SKent Overstreet 			     loff_t len, unsigned remap_flags)
315576426098SKent Overstreet {
315676426098SKent Overstreet 	struct bch_inode_info *src = file_bch_inode(file_src);
315776426098SKent Overstreet 	struct bch_inode_info *dst = file_bch_inode(file_dst);
315876426098SKent Overstreet 	struct bch_fs *c = src->v.i_sb->s_fs_info;
31592e87eae1SKent Overstreet 	s64 i_sectors_delta = 0;
3160677fc056SKent Overstreet 	u64 aligned_len;
316176426098SKent Overstreet 	loff_t ret = 0;
316276426098SKent Overstreet 
316376426098SKent Overstreet 	if (remap_flags & ~(REMAP_FILE_DEDUP|REMAP_FILE_ADVISORY))
316476426098SKent Overstreet 		return -EINVAL;
316576426098SKent Overstreet 
316676426098SKent Overstreet 	if (remap_flags & REMAP_FILE_DEDUP)
316776426098SKent Overstreet 		return -EOPNOTSUPP;
316876426098SKent Overstreet 
316976426098SKent Overstreet 	if ((pos_src & (block_bytes(c) - 1)) ||
317076426098SKent Overstreet 	    (pos_dst & (block_bytes(c) - 1)))
317176426098SKent Overstreet 		return -EINVAL;
317276426098SKent Overstreet 
317376426098SKent Overstreet 	if (src == dst &&
317476426098SKent Overstreet 	    abs(pos_src - pos_dst) < len)
317576426098SKent Overstreet 		return -EINVAL;
317676426098SKent Overstreet 
317776426098SKent Overstreet 	bch2_lock_inodes(INODE_LOCK|INODE_PAGECACHE_BLOCK, src, dst);
317876426098SKent Overstreet 
31792e87eae1SKent Overstreet 	file_update_time(file_dst);
31802e87eae1SKent Overstreet 
318176426098SKent Overstreet 	inode_dio_wait(&src->v);
318276426098SKent Overstreet 	inode_dio_wait(&dst->v);
318376426098SKent Overstreet 
318476426098SKent Overstreet 	ret = generic_remap_file_range_prep(file_src, pos_src,
318576426098SKent Overstreet 					    file_dst, pos_dst,
318676426098SKent Overstreet 					    &len, remap_flags);
318776426098SKent Overstreet 	if (ret < 0 || len == 0)
31882e87eae1SKent Overstreet 		goto err;
318976426098SKent Overstreet 
3190677fc056SKent Overstreet 	aligned_len = round_up((u64) len, block_bytes(c));
319176426098SKent Overstreet 
319276426098SKent Overstreet 	ret = write_invalidate_inode_pages_range(dst->v.i_mapping,
3193677fc056SKent Overstreet 				pos_dst, pos_dst + len - 1);
319476426098SKent Overstreet 	if (ret)
31952e87eae1SKent Overstreet 		goto err;
319676426098SKent Overstreet 
3197*dcfc593fSKent Overstreet 	mark_pagecache_unallocated(src, pos_src >> 9,
3198*dcfc593fSKent Overstreet 				   (pos_src + aligned_len) >> 9);
319976426098SKent Overstreet 
32002e87eae1SKent Overstreet 	ret = bch2_remap_range(c,
32016fed42bbSKent Overstreet 			       inode_inum(dst), pos_dst >> 9,
32026fed42bbSKent Overstreet 			       inode_inum(src), pos_src >> 9,
320376426098SKent Overstreet 			       aligned_len >> 9,
32042e87eae1SKent Overstreet 			       pos_dst + len, &i_sectors_delta);
32052e87eae1SKent Overstreet 	if (ret < 0)
32062e87eae1SKent Overstreet 		goto err;
320776426098SKent Overstreet 
32082e87eae1SKent Overstreet 	/*
32092e87eae1SKent Overstreet 	 * due to alignment, we might have remapped slightly more than requsted
32102e87eae1SKent Overstreet 	 */
3211677fc056SKent Overstreet 	ret = min((u64) ret << 9, (u64) len);
32122e87eae1SKent Overstreet 
32132e87eae1SKent Overstreet 	/* XXX get a quota reservation */
32142e87eae1SKent Overstreet 	i_sectors_acct(c, dst, NULL, i_sectors_delta);
32152e87eae1SKent Overstreet 
32162e87eae1SKent Overstreet 	spin_lock(&dst->v.i_lock);
3217677fc056SKent Overstreet 	if (pos_dst + ret > dst->v.i_size)
3218677fc056SKent Overstreet 		i_size_write(&dst->v, pos_dst + ret);
32192e87eae1SKent Overstreet 	spin_unlock(&dst->v.i_lock);
3220e7084c9cSKent Overstreet 
322168a2054dSKent Overstreet 	if ((file_dst->f_flags & (__O_SYNC | O_DSYNC)) ||
322268a2054dSKent Overstreet 	    IS_SYNC(file_inode(file_dst)))
322368a2054dSKent Overstreet 		ret = bch2_flush_inode(c, inode_inum(dst));
32242e87eae1SKent Overstreet err:
322576426098SKent Overstreet 	bch2_unlock_inodes(INODE_LOCK|INODE_PAGECACHE_BLOCK, src, dst);
322676426098SKent Overstreet 
322776426098SKent Overstreet 	return ret;
322876426098SKent Overstreet }
322976426098SKent Overstreet 
32301c6fdbd8SKent Overstreet /* fseek: */
32311c6fdbd8SKent Overstreet 
3232543ef2ebSKent Overstreet static int folio_data_offset(struct folio *folio, unsigned offset)
32331c6fdbd8SKent Overstreet {
3234f57a6a5dSKent Overstreet 	struct bch_page_state *s = bch2_page_state(&folio->page);
3235f57a6a5dSKent Overstreet 	unsigned i;
3236f81b648dSKent Overstreet 
3237543ef2ebSKent Overstreet 	if (s)
3238543ef2ebSKent Overstreet 		for (i = offset >> 9; i < PAGE_SECTORS; i++)
3239f57a6a5dSKent Overstreet 			if (s->s[i].state >= SECTOR_DIRTY)
3240543ef2ebSKent Overstreet 				return i << 9;
3241f57a6a5dSKent Overstreet 
3242543ef2ebSKent Overstreet 	return -1;
32431c6fdbd8SKent Overstreet }
32441c6fdbd8SKent Overstreet 
3245543ef2ebSKent Overstreet static loff_t bch2_seek_pagecache_data(struct inode *vinode,
32461c6fdbd8SKent Overstreet 				       loff_t start_offset,
32471c6fdbd8SKent Overstreet 				       loff_t end_offset)
32481c6fdbd8SKent Overstreet {
32491c6fdbd8SKent Overstreet 	struct folio_batch fbatch;
32501c6fdbd8SKent Overstreet 	pgoff_t start_index	= start_offset >> PAGE_SHIFT;
32511c6fdbd8SKent Overstreet 	pgoff_t end_index	= end_offset >> PAGE_SHIFT;
32521c6fdbd8SKent Overstreet 	pgoff_t index		= start_index;
32531c6fdbd8SKent Overstreet 	unsigned i;
3254543ef2ebSKent Overstreet 	loff_t ret;
3255543ef2ebSKent Overstreet 	int offset;
32561c6fdbd8SKent Overstreet 
32571c6fdbd8SKent Overstreet 	folio_batch_init(&fbatch);
32581c6fdbd8SKent Overstreet 
32591c6fdbd8SKent Overstreet 	while (filemap_get_folios(vinode->i_mapping,
32601c6fdbd8SKent Overstreet 				  &index, end_index, &fbatch)) {
32611c6fdbd8SKent Overstreet 		for (i = 0; i < folio_batch_count(&fbatch); i++) {
32621c6fdbd8SKent Overstreet 			struct folio *folio = fbatch.folios[i];
32631c6fdbd8SKent Overstreet 
32641c6fdbd8SKent Overstreet 			folio_lock(folio);
3265543ef2ebSKent Overstreet 			offset = folio_data_offset(folio,
3266543ef2ebSKent Overstreet 					folio->index == start_index
3267543ef2ebSKent Overstreet 					? start_offset & (PAGE_SIZE - 1)
3268543ef2ebSKent Overstreet 					: 0);
3269543ef2ebSKent Overstreet 			if (offset >= 0) {
3270543ef2ebSKent Overstreet 				ret = clamp(((loff_t) folio->index << PAGE_SHIFT) +
3271543ef2ebSKent Overstreet 					    offset,
3272543ef2ebSKent Overstreet 					    start_offset, end_offset);
32731c6fdbd8SKent Overstreet 				folio_unlock(folio);
32741c6fdbd8SKent Overstreet 				folio_batch_release(&fbatch);
3275543ef2ebSKent Overstreet 				return ret;
32761c6fdbd8SKent Overstreet 			}
32771c6fdbd8SKent Overstreet 			folio_unlock(folio);
32781c6fdbd8SKent Overstreet 		}
32791c6fdbd8SKent Overstreet 		folio_batch_release(&fbatch);
32801c6fdbd8SKent Overstreet 		cond_resched();
32811c6fdbd8SKent Overstreet 	}
32821c6fdbd8SKent Overstreet 
32831c6fdbd8SKent Overstreet 	return end_offset;
32841c6fdbd8SKent Overstreet }
32851c6fdbd8SKent Overstreet 
32861c6fdbd8SKent Overstreet static loff_t bch2_seek_data(struct file *file, u64 offset)
32871c6fdbd8SKent Overstreet {
32881c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = file_bch_inode(file);
32891c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
3290424eb881SKent Overstreet 	struct btree_trans trans;
329167e0dd8fSKent Overstreet 	struct btree_iter iter;
32921c6fdbd8SKent Overstreet 	struct bkey_s_c k;
32936fed42bbSKent Overstreet 	subvol_inum inum = inode_inum(inode);
32941c6fdbd8SKent Overstreet 	u64 isize, next_data = MAX_LFS_FILESIZE;
32956fed42bbSKent Overstreet 	u32 snapshot;
32961c6fdbd8SKent Overstreet 	int ret;
32971c6fdbd8SKent Overstreet 
32981c6fdbd8SKent Overstreet 	isize = i_size_read(&inode->v);
32991c6fdbd8SKent Overstreet 	if (offset >= isize)
33001c6fdbd8SKent Overstreet 		return -ENXIO;
33011c6fdbd8SKent Overstreet 
330220bceecbSKent Overstreet 	bch2_trans_init(&trans, c, 0, 0);
33036fed42bbSKent Overstreet retry:
33046fed42bbSKent Overstreet 	bch2_trans_begin(&trans);
33056fed42bbSKent Overstreet 
33066fed42bbSKent Overstreet 	ret = bch2_subvolume_get_snapshot(&trans, inum.subvol, &snapshot);
33076fed42bbSKent Overstreet 	if (ret)
33086fed42bbSKent Overstreet 		goto err;
3309424eb881SKent Overstreet 
3310e5fa91d7SKent Overstreet 	for_each_btree_key_norestart(&trans, iter, BTREE_ID_extents,
33116fed42bbSKent Overstreet 			   SPOS(inode->v.i_ino, offset >> 9, snapshot), 0, k, ret) {
33121c6fdbd8SKent Overstreet 		if (k.k->p.inode != inode->v.i_ino) {
33131c6fdbd8SKent Overstreet 			break;
33141c6fdbd8SKent Overstreet 		} else if (bkey_extent_is_data(k.k)) {
33151c6fdbd8SKent Overstreet 			next_data = max(offset, bkey_start_offset(k.k) << 9);
33161c6fdbd8SKent Overstreet 			break;
33171c6fdbd8SKent Overstreet 		} else if (k.k->p.offset >> 9 > isize)
33181c6fdbd8SKent Overstreet 			break;
33191c6fdbd8SKent Overstreet 	}
332067e0dd8fSKent Overstreet 	bch2_trans_iter_exit(&trans, &iter);
33216fed42bbSKent Overstreet err:
33226fed42bbSKent Overstreet 	if (ret == -EINTR)
33236fed42bbSKent Overstreet 		goto retry;
33241c6fdbd8SKent Overstreet 
33259a796fdbSKent Overstreet 	bch2_trans_exit(&trans);
33261c6fdbd8SKent Overstreet 	if (ret)
33271c6fdbd8SKent Overstreet 		return ret;
33281c6fdbd8SKent Overstreet 
33291c6fdbd8SKent Overstreet 	if (next_data > offset)
3330543ef2ebSKent Overstreet 		next_data = bch2_seek_pagecache_data(&inode->v,
33311c6fdbd8SKent Overstreet 						     offset, next_data);
33321c6fdbd8SKent Overstreet 
3333e10d3094SKent Overstreet 	if (next_data >= isize)
33341c6fdbd8SKent Overstreet 		return -ENXIO;
33351c6fdbd8SKent Overstreet 
33361c6fdbd8SKent Overstreet 	return vfs_setpos(file, next_data, MAX_LFS_FILESIZE);
33371c6fdbd8SKent Overstreet }
33381c6fdbd8SKent Overstreet 
3339543ef2ebSKent Overstreet static int __page_hole_offset(struct page *page, unsigned offset)
33401c6fdbd8SKent Overstreet {
3341543ef2ebSKent Overstreet 	struct bch_page_state *s = bch2_page_state(page);
3342543ef2ebSKent Overstreet 	unsigned i;
3343543ef2ebSKent Overstreet 
3344543ef2ebSKent Overstreet 	if (!s)
3345543ef2ebSKent Overstreet 		return 0;
3346543ef2ebSKent Overstreet 
3347543ef2ebSKent Overstreet 	for (i = offset >> 9; i < PAGE_SECTORS; i++)
3348543ef2ebSKent Overstreet 		if (s->s[i].state < SECTOR_DIRTY)
3349543ef2ebSKent Overstreet 			return i << 9;
3350543ef2ebSKent Overstreet 
3351543ef2ebSKent Overstreet 	return -1;
3352543ef2ebSKent Overstreet }
3353543ef2ebSKent Overstreet 
3354543ef2ebSKent Overstreet static loff_t page_hole_offset(struct address_space *mapping, loff_t offset)
3355543ef2ebSKent Overstreet {
3356543ef2ebSKent Overstreet 	pgoff_t index = offset >> PAGE_SHIFT;
33571c6fdbd8SKent Overstreet 	struct page *page;
3358543ef2ebSKent Overstreet 	int pg_offset;
3359543ef2ebSKent Overstreet 	loff_t ret = -1;
33601c6fdbd8SKent Overstreet 
33611c6fdbd8SKent Overstreet 	page = find_lock_page(mapping, index);
33621c6fdbd8SKent Overstreet 	if (!page)
3363543ef2ebSKent Overstreet 		return offset;
33641c6fdbd8SKent Overstreet 
3365543ef2ebSKent Overstreet 	pg_offset = __page_hole_offset(page, offset & (PAGE_SIZE - 1));
3366543ef2ebSKent Overstreet 	if (pg_offset >= 0)
3367543ef2ebSKent Overstreet 		ret = ((loff_t) index << PAGE_SHIFT) + pg_offset;
3368543ef2ebSKent Overstreet 
33691c6fdbd8SKent Overstreet 	unlock_page(page);
33701c6fdbd8SKent Overstreet 
33711c6fdbd8SKent Overstreet 	return ret;
33721c6fdbd8SKent Overstreet }
33731c6fdbd8SKent Overstreet 
3374543ef2ebSKent Overstreet static loff_t bch2_seek_pagecache_hole(struct inode *vinode,
33751c6fdbd8SKent Overstreet 				       loff_t start_offset,
33761c6fdbd8SKent Overstreet 				       loff_t end_offset)
33771c6fdbd8SKent Overstreet {
33781c6fdbd8SKent Overstreet 	struct address_space *mapping = vinode->i_mapping;
3379543ef2ebSKent Overstreet 	loff_t offset = start_offset, hole;
33801c6fdbd8SKent Overstreet 
3381543ef2ebSKent Overstreet 	while (offset < end_offset) {
3382543ef2ebSKent Overstreet 		hole = page_hole_offset(mapping, offset);
3383543ef2ebSKent Overstreet 		if (hole >= 0 && hole <= end_offset)
3384543ef2ebSKent Overstreet 			return max(start_offset, hole);
3385543ef2ebSKent Overstreet 
3386543ef2ebSKent Overstreet 		offset += PAGE_SIZE;
3387543ef2ebSKent Overstreet 		offset &= PAGE_MASK;
3388543ef2ebSKent Overstreet 	}
33891c6fdbd8SKent Overstreet 
33901c6fdbd8SKent Overstreet 	return end_offset;
33911c6fdbd8SKent Overstreet }
33921c6fdbd8SKent Overstreet 
33931c6fdbd8SKent Overstreet static loff_t bch2_seek_hole(struct file *file, u64 offset)
33941c6fdbd8SKent Overstreet {
33951c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = file_bch_inode(file);
33961c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
3397424eb881SKent Overstreet 	struct btree_trans trans;
339867e0dd8fSKent Overstreet 	struct btree_iter iter;
33991c6fdbd8SKent Overstreet 	struct bkey_s_c k;
34006fed42bbSKent Overstreet 	subvol_inum inum = inode_inum(inode);
34011c6fdbd8SKent Overstreet 	u64 isize, next_hole = MAX_LFS_FILESIZE;
34026fed42bbSKent Overstreet 	u32 snapshot;
34031c6fdbd8SKent Overstreet 	int ret;
34041c6fdbd8SKent Overstreet 
34051c6fdbd8SKent Overstreet 	isize = i_size_read(&inode->v);
34061c6fdbd8SKent Overstreet 	if (offset >= isize)
34071c6fdbd8SKent Overstreet 		return -ENXIO;
34081c6fdbd8SKent Overstreet 
340920bceecbSKent Overstreet 	bch2_trans_init(&trans, c, 0, 0);
34106fed42bbSKent Overstreet retry:
34116fed42bbSKent Overstreet 	bch2_trans_begin(&trans);
34126fed42bbSKent Overstreet 
34136fed42bbSKent Overstreet 	ret = bch2_subvolume_get_snapshot(&trans, inum.subvol, &snapshot);
34146fed42bbSKent Overstreet 	if (ret)
34156fed42bbSKent Overstreet 		goto err;
3416424eb881SKent Overstreet 
3417e5fa91d7SKent Overstreet 	for_each_btree_key_norestart(&trans, iter, BTREE_ID_extents,
34186fed42bbSKent Overstreet 			   SPOS(inode->v.i_ino, offset >> 9, snapshot),
341994f651e2SKent Overstreet 			   BTREE_ITER_SLOTS, k, ret) {
34201c6fdbd8SKent Overstreet 		if (k.k->p.inode != inode->v.i_ino) {
3421543ef2ebSKent Overstreet 			next_hole = bch2_seek_pagecache_hole(&inode->v,
34221c6fdbd8SKent Overstreet 					offset, MAX_LFS_FILESIZE);
34231c6fdbd8SKent Overstreet 			break;
34241c6fdbd8SKent Overstreet 		} else if (!bkey_extent_is_data(k.k)) {
3425543ef2ebSKent Overstreet 			next_hole = bch2_seek_pagecache_hole(&inode->v,
34261c6fdbd8SKent Overstreet 					max(offset, bkey_start_offset(k.k) << 9),
34271c6fdbd8SKent Overstreet 					k.k->p.offset << 9);
34281c6fdbd8SKent Overstreet 
34291c6fdbd8SKent Overstreet 			if (next_hole < k.k->p.offset << 9)
34301c6fdbd8SKent Overstreet 				break;
34311c6fdbd8SKent Overstreet 		} else {
34321c6fdbd8SKent Overstreet 			offset = max(offset, bkey_start_offset(k.k) << 9);
34331c6fdbd8SKent Overstreet 		}
34341c6fdbd8SKent Overstreet 	}
343567e0dd8fSKent Overstreet 	bch2_trans_iter_exit(&trans, &iter);
34366fed42bbSKent Overstreet err:
34376fed42bbSKent Overstreet 	if (ret == -EINTR)
34386fed42bbSKent Overstreet 		goto retry;
34391c6fdbd8SKent Overstreet 
34409a796fdbSKent Overstreet 	bch2_trans_exit(&trans);
34411c6fdbd8SKent Overstreet 	if (ret)
34421c6fdbd8SKent Overstreet 		return ret;
34431c6fdbd8SKent Overstreet 
34441c6fdbd8SKent Overstreet 	if (next_hole > isize)
34451c6fdbd8SKent Overstreet 		next_hole = isize;
34461c6fdbd8SKent Overstreet 
34471c6fdbd8SKent Overstreet 	return vfs_setpos(file, next_hole, MAX_LFS_FILESIZE);
34481c6fdbd8SKent Overstreet }
34491c6fdbd8SKent Overstreet 
34501c6fdbd8SKent Overstreet loff_t bch2_llseek(struct file *file, loff_t offset, int whence)
34511c6fdbd8SKent Overstreet {
34521c6fdbd8SKent Overstreet 	switch (whence) {
34531c6fdbd8SKent Overstreet 	case SEEK_SET:
34541c6fdbd8SKent Overstreet 	case SEEK_CUR:
34551c6fdbd8SKent Overstreet 	case SEEK_END:
34561c6fdbd8SKent Overstreet 		return generic_file_llseek(file, offset, whence);
34571c6fdbd8SKent Overstreet 	case SEEK_DATA:
34581c6fdbd8SKent Overstreet 		return bch2_seek_data(file, offset);
34591c6fdbd8SKent Overstreet 	case SEEK_HOLE:
34601c6fdbd8SKent Overstreet 		return bch2_seek_hole(file, offset);
34611c6fdbd8SKent Overstreet 	}
34621c6fdbd8SKent Overstreet 
34631c6fdbd8SKent Overstreet 	return -EINVAL;
34641c6fdbd8SKent Overstreet }
34651c6fdbd8SKent Overstreet 
34661c6fdbd8SKent Overstreet void bch2_fs_fsio_exit(struct bch_fs *c)
34671c6fdbd8SKent Overstreet {
34681c6fdbd8SKent Overstreet 	bioset_exit(&c->dio_write_bioset);
34691c6fdbd8SKent Overstreet 	bioset_exit(&c->dio_read_bioset);
34701c6fdbd8SKent Overstreet 	bioset_exit(&c->writepage_bioset);
34711c6fdbd8SKent Overstreet }
34721c6fdbd8SKent Overstreet 
34731c6fdbd8SKent Overstreet int bch2_fs_fsio_init(struct bch_fs *c)
34741c6fdbd8SKent Overstreet {
34751c6fdbd8SKent Overstreet 	int ret = 0;
34761c6fdbd8SKent Overstreet 
34771c6fdbd8SKent Overstreet 	pr_verbose_init(c->opts, "");
34781c6fdbd8SKent Overstreet 
34791c6fdbd8SKent Overstreet 	if (bioset_init(&c->writepage_bioset,
34809a3df993SKent Overstreet 			4, offsetof(struct bch_writepage_io, op.wbio.bio),
34811c6fdbd8SKent Overstreet 			BIOSET_NEED_BVECS) ||
34821c6fdbd8SKent Overstreet 	    bioset_init(&c->dio_read_bioset,
34831c6fdbd8SKent Overstreet 			4, offsetof(struct dio_read, rbio.bio),
34841c6fdbd8SKent Overstreet 			BIOSET_NEED_BVECS) ||
34851c6fdbd8SKent Overstreet 	    bioset_init(&c->dio_write_bioset,
34869a3df993SKent Overstreet 			4, offsetof(struct dio_write, op.wbio.bio),
34871c6fdbd8SKent Overstreet 			BIOSET_NEED_BVECS))
34881c6fdbd8SKent Overstreet 		ret = -ENOMEM;
34891c6fdbd8SKent Overstreet 
34901c6fdbd8SKent Overstreet 	pr_verbose_init(c->opts, "ret %i", ret);
34911c6fdbd8SKent Overstreet 	return ret;
34921c6fdbd8SKent Overstreet }
34931c6fdbd8SKent Overstreet 
34941c6fdbd8SKent Overstreet #endif /* NO_BCACHEFS_FS */
3495