xref: /linux/fs/bcachefs/fs-io-pagecache.c (revision e7b2b108cdeab76a7e7324459e50b0c1214c0386)
1 // SPDX-License-Identifier: GPL-2.0
2 #ifndef NO_BCACHEFS_FS
3 
4 #include "bcachefs.h"
5 #include "btree_iter.h"
6 #include "extents.h"
7 #include "fs-io.h"
8 #include "fs-io-pagecache.h"
9 #include "subvolume.h"
10 
11 #include <linux/pagevec.h>
12 #include <linux/writeback.h>
13 
14 int bch2_filemap_get_contig_folios_d(struct address_space *mapping,
15 				     loff_t start, u64 end,
16 				     fgf_t fgp_flags, gfp_t gfp,
17 				     folios *fs)
18 {
19 	struct folio *f;
20 	u64 pos = start;
21 	int ret = 0;
22 
23 	while (pos < end) {
24 		if ((u64) pos >= (u64) start + (1ULL << 20))
25 			fgp_flags &= ~FGP_CREAT;
26 
27 		ret = darray_make_room_gfp(fs, 1, gfp & GFP_KERNEL);
28 		if (ret)
29 			break;
30 
31 		f = __filemap_get_folio(mapping, pos >> PAGE_SHIFT, fgp_flags, gfp);
32 		if (IS_ERR_OR_NULL(f))
33 			break;
34 
35 		BUG_ON(fs->nr && folio_pos(f) != pos);
36 
37 		pos = folio_end_pos(f);
38 		darray_push(fs, f);
39 	}
40 
41 	if (!fs->nr && !ret && (fgp_flags & FGP_CREAT))
42 		ret = -ENOMEM;
43 
44 	return fs->nr ? 0 : ret;
45 }
46 
47 /* pagecache_block must be held */
48 int bch2_write_invalidate_inode_pages_range(struct address_space *mapping,
49 					    loff_t start, loff_t end)
50 {
51 	int ret;
52 
53 	/*
54 	 * XXX: the way this is currently implemented, we can spin if a process
55 	 * is continually redirtying a specific page
56 	 */
57 	do {
58 		if (!mapping->nrpages)
59 			return 0;
60 
61 		ret = filemap_write_and_wait_range(mapping, start, end);
62 		if (ret)
63 			break;
64 
65 		if (!mapping->nrpages)
66 			return 0;
67 
68 		ret = invalidate_inode_pages2_range(mapping,
69 				start >> PAGE_SHIFT,
70 				end >> PAGE_SHIFT);
71 	} while (ret == -EBUSY);
72 
73 	return ret;
74 }
75 
76 #if 0
77 /* Useful for debug tracing: */
78 static const char * const bch2_folio_sector_states[] = {
79 #define x(n)	#n,
80 	BCH_FOLIO_SECTOR_STATE()
81 #undef x
82 	NULL
83 };
84 #endif
85 
86 static inline enum bch_folio_sector_state
87 folio_sector_dirty(enum bch_folio_sector_state state)
88 {
89 	switch (state) {
90 	case SECTOR_unallocated:
91 		return SECTOR_dirty;
92 	case SECTOR_reserved:
93 		return SECTOR_dirty_reserved;
94 	default:
95 		return state;
96 	}
97 }
98 
99 static inline enum bch_folio_sector_state
100 folio_sector_undirty(enum bch_folio_sector_state state)
101 {
102 	switch (state) {
103 	case SECTOR_dirty:
104 		return SECTOR_unallocated;
105 	case SECTOR_dirty_reserved:
106 		return SECTOR_reserved;
107 	default:
108 		return state;
109 	}
110 }
111 
112 static inline enum bch_folio_sector_state
113 folio_sector_reserve(enum bch_folio_sector_state state)
114 {
115 	switch (state) {
116 	case SECTOR_unallocated:
117 		return SECTOR_reserved;
118 	case SECTOR_dirty:
119 		return SECTOR_dirty_reserved;
120 	default:
121 		return state;
122 	}
123 }
124 
125 /* for newly allocated folios: */
126 struct bch_folio *__bch2_folio_create(struct folio *folio, gfp_t gfp)
127 {
128 	struct bch_folio *s;
129 
130 	s = kzalloc(sizeof(*s) +
131 		    sizeof(struct bch_folio_sector) *
132 		    folio_sectors(folio), gfp);
133 	if (!s)
134 		return NULL;
135 
136 	spin_lock_init(&s->lock);
137 	folio_attach_private(folio, s);
138 	return s;
139 }
140 
141 struct bch_folio *bch2_folio_create(struct folio *folio, gfp_t gfp)
142 {
143 	return bch2_folio(folio) ?: __bch2_folio_create(folio, gfp);
144 }
145 
146 static unsigned bkey_to_sector_state(struct bkey_s_c k)
147 {
148 	if (bkey_extent_is_reservation(k))
149 		return SECTOR_reserved;
150 	if (bkey_extent_is_allocation(k.k))
151 		return SECTOR_allocated;
152 	return SECTOR_unallocated;
153 }
154 
155 static void __bch2_folio_set(struct folio *folio,
156 			     unsigned pg_offset, unsigned pg_len,
157 			     unsigned nr_ptrs, unsigned state)
158 {
159 	struct bch_folio *s = bch2_folio(folio);
160 	unsigned i, sectors = folio_sectors(folio);
161 
162 	BUG_ON(pg_offset >= sectors);
163 	BUG_ON(pg_offset + pg_len > sectors);
164 
165 	spin_lock(&s->lock);
166 
167 	for (i = pg_offset; i < pg_offset + pg_len; i++) {
168 		s->s[i].nr_replicas	= nr_ptrs;
169 		bch2_folio_sector_set(folio, s, i, state);
170 	}
171 
172 	if (i == sectors)
173 		s->uptodate = true;
174 
175 	spin_unlock(&s->lock);
176 }
177 
178 /*
179  * Initialize bch_folio state (allocated/unallocated, nr_replicas) from the
180  * extents btree:
181  */
182 int bch2_folio_set(struct bch_fs *c, subvol_inum inum,
183 		   struct folio **fs, unsigned nr_folios)
184 {
185 	struct btree_trans *trans;
186 	struct btree_iter iter;
187 	struct bkey_s_c k;
188 	struct bch_folio *s;
189 	u64 offset = folio_sector(fs[0]);
190 	unsigned folio_idx;
191 	u32 snapshot;
192 	bool need_set = false;
193 	int ret;
194 
195 	for (folio_idx = 0; folio_idx < nr_folios; folio_idx++) {
196 		s = bch2_folio_create(fs[folio_idx], GFP_KERNEL);
197 		if (!s)
198 			return -ENOMEM;
199 
200 		need_set |= !s->uptodate;
201 	}
202 
203 	if (!need_set)
204 		return 0;
205 
206 	folio_idx = 0;
207 	trans = bch2_trans_get(c);
208 retry:
209 	bch2_trans_begin(trans);
210 
211 	ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
212 	if (ret)
213 		goto err;
214 
215 	for_each_btree_key_norestart(trans, iter, BTREE_ID_extents,
216 			   SPOS(inum.inum, offset, snapshot),
217 			   BTREE_ITER_SLOTS, k, ret) {
218 		unsigned nr_ptrs = bch2_bkey_nr_ptrs_fully_allocated(k);
219 		unsigned state = bkey_to_sector_state(k);
220 
221 		while (folio_idx < nr_folios) {
222 			struct folio *folio = fs[folio_idx];
223 			u64 folio_start	= folio_sector(folio);
224 			u64 folio_end	= folio_end_sector(folio);
225 			unsigned folio_offset = max(bkey_start_offset(k.k), folio_start) -
226 				folio_start;
227 			unsigned folio_len = min(k.k->p.offset, folio_end) -
228 				folio_offset - folio_start;
229 
230 			BUG_ON(k.k->p.offset < folio_start);
231 			BUG_ON(bkey_start_offset(k.k) > folio_end);
232 
233 			if (!bch2_folio(folio)->uptodate)
234 				__bch2_folio_set(folio, folio_offset, folio_len, nr_ptrs, state);
235 
236 			if (k.k->p.offset < folio_end)
237 				break;
238 			folio_idx++;
239 		}
240 
241 		if (folio_idx == nr_folios)
242 			break;
243 	}
244 
245 	offset = iter.pos.offset;
246 	bch2_trans_iter_exit(trans, &iter);
247 err:
248 	if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
249 		goto retry;
250 	bch2_trans_put(trans);
251 
252 	return ret;
253 }
254 
255 void bch2_bio_page_state_set(struct bio *bio, struct bkey_s_c k)
256 {
257 	struct bvec_iter iter;
258 	struct folio_vec fv;
259 	unsigned nr_ptrs = k.k->type == KEY_TYPE_reflink_v
260 		? 0 : bch2_bkey_nr_ptrs_fully_allocated(k);
261 	unsigned state = bkey_to_sector_state(k);
262 
263 	bio_for_each_folio(fv, bio, iter)
264 		__bch2_folio_set(fv.fv_folio,
265 				 fv.fv_offset >> 9,
266 				 fv.fv_len >> 9,
267 				 nr_ptrs, state);
268 }
269 
270 void bch2_mark_pagecache_unallocated(struct bch_inode_info *inode,
271 				     u64 start, u64 end)
272 {
273 	pgoff_t index = start >> PAGE_SECTORS_SHIFT;
274 	pgoff_t end_index = (end - 1) >> PAGE_SECTORS_SHIFT;
275 	struct folio_batch fbatch;
276 	unsigned i, j;
277 
278 	if (end <= start)
279 		return;
280 
281 	folio_batch_init(&fbatch);
282 
283 	while (filemap_get_folios(inode->v.i_mapping,
284 				  &index, end_index, &fbatch)) {
285 		for (i = 0; i < folio_batch_count(&fbatch); i++) {
286 			struct folio *folio = fbatch.folios[i];
287 			u64 folio_start = folio_sector(folio);
288 			u64 folio_end = folio_end_sector(folio);
289 			unsigned folio_offset = max(start, folio_start) - folio_start;
290 			unsigned folio_len = min(end, folio_end) - folio_offset - folio_start;
291 			struct bch_folio *s;
292 
293 			BUG_ON(end <= folio_start);
294 
295 			folio_lock(folio);
296 			s = bch2_folio(folio);
297 
298 			if (s) {
299 				spin_lock(&s->lock);
300 				for (j = folio_offset; j < folio_offset + folio_len; j++)
301 					s->s[j].nr_replicas = 0;
302 				spin_unlock(&s->lock);
303 			}
304 
305 			folio_unlock(folio);
306 		}
307 		folio_batch_release(&fbatch);
308 		cond_resched();
309 	}
310 }
311 
312 void bch2_mark_pagecache_reserved(struct bch_inode_info *inode,
313 				  u64 start, u64 end)
314 {
315 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
316 	pgoff_t index = start >> PAGE_SECTORS_SHIFT;
317 	pgoff_t end_index = (end - 1) >> PAGE_SECTORS_SHIFT;
318 	struct folio_batch fbatch;
319 	s64 i_sectors_delta = 0;
320 	unsigned i, j;
321 
322 	if (end <= start)
323 		return;
324 
325 	folio_batch_init(&fbatch);
326 
327 	while (filemap_get_folios(inode->v.i_mapping,
328 				  &index, end_index, &fbatch)) {
329 		for (i = 0; i < folio_batch_count(&fbatch); i++) {
330 			struct folio *folio = fbatch.folios[i];
331 			u64 folio_start = folio_sector(folio);
332 			u64 folio_end = folio_end_sector(folio);
333 			unsigned folio_offset = max(start, folio_start) - folio_start;
334 			unsigned folio_len = min(end, folio_end) - folio_offset - folio_start;
335 			struct bch_folio *s;
336 
337 			BUG_ON(end <= folio_start);
338 
339 			folio_lock(folio);
340 			s = bch2_folio(folio);
341 
342 			if (s) {
343 				spin_lock(&s->lock);
344 				for (j = folio_offset; j < folio_offset + folio_len; j++) {
345 					i_sectors_delta -= s->s[j].state == SECTOR_dirty;
346 					bch2_folio_sector_set(folio, s, j,
347 						folio_sector_reserve(s->s[j].state));
348 				}
349 				spin_unlock(&s->lock);
350 			}
351 
352 			folio_unlock(folio);
353 		}
354 		folio_batch_release(&fbatch);
355 		cond_resched();
356 	}
357 
358 	bch2_i_sectors_acct(c, inode, NULL, i_sectors_delta);
359 }
360 
361 static inline unsigned sectors_to_reserve(struct bch_folio_sector *s,
362 					  unsigned nr_replicas)
363 {
364 	return max(0, (int) nr_replicas -
365 		   s->nr_replicas -
366 		   s->replicas_reserved);
367 }
368 
369 int bch2_get_folio_disk_reservation(struct bch_fs *c,
370 				struct bch_inode_info *inode,
371 				struct folio *folio, bool check_enospc)
372 {
373 	struct bch_folio *s = bch2_folio_create(folio, 0);
374 	unsigned nr_replicas = inode_nr_replicas(c, inode);
375 	struct disk_reservation disk_res = { 0 };
376 	unsigned i, sectors = folio_sectors(folio), disk_res_sectors = 0;
377 	int ret;
378 
379 	if (!s)
380 		return -ENOMEM;
381 
382 	for (i = 0; i < sectors; i++)
383 		disk_res_sectors += sectors_to_reserve(&s->s[i], nr_replicas);
384 
385 	if (!disk_res_sectors)
386 		return 0;
387 
388 	ret = bch2_disk_reservation_get(c, &disk_res,
389 					disk_res_sectors, 1,
390 					!check_enospc
391 					? BCH_DISK_RESERVATION_NOFAIL
392 					: 0);
393 	if (unlikely(ret))
394 		return ret;
395 
396 	for (i = 0; i < sectors; i++)
397 		s->s[i].replicas_reserved +=
398 			sectors_to_reserve(&s->s[i], nr_replicas);
399 
400 	return 0;
401 }
402 
403 void bch2_folio_reservation_put(struct bch_fs *c,
404 			struct bch_inode_info *inode,
405 			struct bch2_folio_reservation *res)
406 {
407 	bch2_disk_reservation_put(c, &res->disk);
408 	bch2_quota_reservation_put(c, inode, &res->quota);
409 }
410 
411 int bch2_folio_reservation_get(struct bch_fs *c,
412 			struct bch_inode_info *inode,
413 			struct folio *folio,
414 			struct bch2_folio_reservation *res,
415 			unsigned offset, unsigned len)
416 {
417 	struct bch_folio *s = bch2_folio_create(folio, 0);
418 	unsigned i, disk_sectors = 0, quota_sectors = 0;
419 	int ret;
420 
421 	if (!s)
422 		return -ENOMEM;
423 
424 	BUG_ON(!s->uptodate);
425 
426 	for (i = round_down(offset, block_bytes(c)) >> 9;
427 	     i < round_up(offset + len, block_bytes(c)) >> 9;
428 	     i++) {
429 		disk_sectors += sectors_to_reserve(&s->s[i],
430 						res->disk.nr_replicas);
431 		quota_sectors += s->s[i].state == SECTOR_unallocated;
432 	}
433 
434 	if (disk_sectors) {
435 		ret = bch2_disk_reservation_add(c, &res->disk, disk_sectors, 0);
436 		if (unlikely(ret))
437 			return ret;
438 	}
439 
440 	if (quota_sectors) {
441 		ret = bch2_quota_reservation_add(c, inode, &res->quota,
442 						 quota_sectors, true);
443 		if (unlikely(ret)) {
444 			struct disk_reservation tmp = {
445 				.sectors = disk_sectors
446 			};
447 
448 			bch2_disk_reservation_put(c, &tmp);
449 			res->disk.sectors -= disk_sectors;
450 			return ret;
451 		}
452 	}
453 
454 	return 0;
455 }
456 
457 static void bch2_clear_folio_bits(struct folio *folio)
458 {
459 	struct bch_inode_info *inode = to_bch_ei(folio->mapping->host);
460 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
461 	struct bch_folio *s = bch2_folio(folio);
462 	struct disk_reservation disk_res = { 0 };
463 	int i, sectors = folio_sectors(folio), dirty_sectors = 0;
464 
465 	if (!s)
466 		return;
467 
468 	EBUG_ON(!folio_test_locked(folio));
469 	EBUG_ON(folio_test_writeback(folio));
470 
471 	for (i = 0; i < sectors; i++) {
472 		disk_res.sectors += s->s[i].replicas_reserved;
473 		s->s[i].replicas_reserved = 0;
474 
475 		dirty_sectors -= s->s[i].state == SECTOR_dirty;
476 		bch2_folio_sector_set(folio, s, i, folio_sector_undirty(s->s[i].state));
477 	}
478 
479 	bch2_disk_reservation_put(c, &disk_res);
480 
481 	bch2_i_sectors_acct(c, inode, NULL, dirty_sectors);
482 
483 	bch2_folio_release(folio);
484 }
485 
486 void bch2_set_folio_dirty(struct bch_fs *c,
487 			  struct bch_inode_info *inode,
488 			  struct folio *folio,
489 			  struct bch2_folio_reservation *res,
490 			  unsigned offset, unsigned len)
491 {
492 	struct bch_folio *s = bch2_folio(folio);
493 	unsigned i, dirty_sectors = 0;
494 
495 	WARN_ON((u64) folio_pos(folio) + offset + len >
496 		round_up((u64) i_size_read(&inode->v), block_bytes(c)));
497 
498 	BUG_ON(!s->uptodate);
499 
500 	spin_lock(&s->lock);
501 
502 	for (i = round_down(offset, block_bytes(c)) >> 9;
503 	     i < round_up(offset + len, block_bytes(c)) >> 9;
504 	     i++) {
505 		unsigned sectors = sectors_to_reserve(&s->s[i],
506 						res->disk.nr_replicas);
507 
508 		/*
509 		 * This can happen if we race with the error path in
510 		 * bch2_writepage_io_done():
511 		 */
512 		sectors = min_t(unsigned, sectors, res->disk.sectors);
513 
514 		s->s[i].replicas_reserved += sectors;
515 		res->disk.sectors -= sectors;
516 
517 		dirty_sectors += s->s[i].state == SECTOR_unallocated;
518 
519 		bch2_folio_sector_set(folio, s, i, folio_sector_dirty(s->s[i].state));
520 	}
521 
522 	spin_unlock(&s->lock);
523 
524 	bch2_i_sectors_acct(c, inode, &res->quota, dirty_sectors);
525 
526 	if (!folio_test_dirty(folio))
527 		filemap_dirty_folio(inode->v.i_mapping, folio);
528 }
529 
530 vm_fault_t bch2_page_fault(struct vm_fault *vmf)
531 {
532 	struct file *file = vmf->vma->vm_file;
533 	struct address_space *mapping = file->f_mapping;
534 	struct address_space *fdm = faults_disabled_mapping();
535 	struct bch_inode_info *inode = file_bch_inode(file);
536 	vm_fault_t ret;
537 
538 	if (fdm == mapping)
539 		return VM_FAULT_SIGBUS;
540 
541 	/* Lock ordering: */
542 	if (fdm > mapping) {
543 		struct bch_inode_info *fdm_host = to_bch_ei(fdm->host);
544 
545 		if (bch2_pagecache_add_tryget(inode))
546 			goto got_lock;
547 
548 		bch2_pagecache_block_put(fdm_host);
549 
550 		bch2_pagecache_add_get(inode);
551 		bch2_pagecache_add_put(inode);
552 
553 		bch2_pagecache_block_get(fdm_host);
554 
555 		/* Signal that lock has been dropped: */
556 		set_fdm_dropped_locks();
557 		return VM_FAULT_SIGBUS;
558 	}
559 
560 	bch2_pagecache_add_get(inode);
561 got_lock:
562 	ret = filemap_fault(vmf);
563 	bch2_pagecache_add_put(inode);
564 
565 	return ret;
566 }
567 
568 vm_fault_t bch2_page_mkwrite(struct vm_fault *vmf)
569 {
570 	struct folio *folio = page_folio(vmf->page);
571 	struct file *file = vmf->vma->vm_file;
572 	struct bch_inode_info *inode = file_bch_inode(file);
573 	struct address_space *mapping = file->f_mapping;
574 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
575 	struct bch2_folio_reservation res;
576 	unsigned len;
577 	loff_t isize;
578 	vm_fault_t ret;
579 
580 	bch2_folio_reservation_init(c, inode, &res);
581 
582 	sb_start_pagefault(inode->v.i_sb);
583 	file_update_time(file);
584 
585 	/*
586 	 * Not strictly necessary, but helps avoid dio writes livelocking in
587 	 * bch2_write_invalidate_inode_pages_range() - can drop this if/when we get
588 	 * a bch2_write_invalidate_inode_pages_range() that works without dropping
589 	 * page lock before invalidating page
590 	 */
591 	bch2_pagecache_add_get(inode);
592 
593 	folio_lock(folio);
594 	isize = i_size_read(&inode->v);
595 
596 	if (folio->mapping != mapping || folio_pos(folio) >= isize) {
597 		folio_unlock(folio);
598 		ret = VM_FAULT_NOPAGE;
599 		goto out;
600 	}
601 
602 	len = min_t(loff_t, folio_size(folio), isize - folio_pos(folio));
603 
604 	if (bch2_folio_set(c, inode_inum(inode), &folio, 1) ?:
605 	    bch2_folio_reservation_get(c, inode, folio, &res, 0, len)) {
606 		folio_unlock(folio);
607 		ret = VM_FAULT_SIGBUS;
608 		goto out;
609 	}
610 
611 	bch2_set_folio_dirty(c, inode, folio, &res, 0, len);
612 	bch2_folio_reservation_put(c, inode, &res);
613 
614 	folio_wait_stable(folio);
615 	ret = VM_FAULT_LOCKED;
616 out:
617 	bch2_pagecache_add_put(inode);
618 	sb_end_pagefault(inode->v.i_sb);
619 
620 	return ret;
621 }
622 
623 void bch2_invalidate_folio(struct folio *folio, size_t offset, size_t length)
624 {
625 	if (offset || length < folio_size(folio))
626 		return;
627 
628 	bch2_clear_folio_bits(folio);
629 }
630 
631 bool bch2_release_folio(struct folio *folio, gfp_t gfp_mask)
632 {
633 	if (folio_test_dirty(folio) || folio_test_writeback(folio))
634 		return false;
635 
636 	bch2_clear_folio_bits(folio);
637 	return true;
638 }
639 
640 /* fseek: */
641 
642 static int folio_data_offset(struct folio *folio, loff_t pos,
643 			     unsigned min_replicas)
644 {
645 	struct bch_folio *s = bch2_folio(folio);
646 	unsigned i, sectors = folio_sectors(folio);
647 
648 	if (s)
649 		for (i = folio_pos_to_s(folio, pos); i < sectors; i++)
650 			if (s->s[i].state >= SECTOR_dirty &&
651 			    s->s[i].nr_replicas + s->s[i].replicas_reserved >= min_replicas)
652 				return i << SECTOR_SHIFT;
653 
654 	return -1;
655 }
656 
657 loff_t bch2_seek_pagecache_data(struct inode *vinode,
658 				loff_t start_offset,
659 				loff_t end_offset,
660 				unsigned min_replicas,
661 				bool nonblock)
662 {
663 	struct folio_batch fbatch;
664 	pgoff_t start_index	= start_offset >> PAGE_SHIFT;
665 	pgoff_t end_index	= end_offset >> PAGE_SHIFT;
666 	pgoff_t index		= start_index;
667 	unsigned i;
668 	loff_t ret;
669 	int offset;
670 
671 	folio_batch_init(&fbatch);
672 
673 	while (filemap_get_folios(vinode->i_mapping,
674 				  &index, end_index, &fbatch)) {
675 		for (i = 0; i < folio_batch_count(&fbatch); i++) {
676 			struct folio *folio = fbatch.folios[i];
677 
678 			if (!nonblock) {
679 				folio_lock(folio);
680 			} else if (!folio_trylock(folio)) {
681 				folio_batch_release(&fbatch);
682 				return -EAGAIN;
683 			}
684 
685 			offset = folio_data_offset(folio,
686 					max(folio_pos(folio), start_offset),
687 					min_replicas);
688 			if (offset >= 0) {
689 				ret = clamp(folio_pos(folio) + offset,
690 					    start_offset, end_offset);
691 				folio_unlock(folio);
692 				folio_batch_release(&fbatch);
693 				return ret;
694 			}
695 			folio_unlock(folio);
696 		}
697 		folio_batch_release(&fbatch);
698 		cond_resched();
699 	}
700 
701 	return end_offset;
702 }
703 
704 /*
705  * Search for a hole in a folio.
706  *
707  * The filemap layer returns -ENOENT if no folio exists, so reuse the same error
708  * code to indicate a pagecache hole exists at the returned offset. Otherwise
709  * return 0 if the folio is filled with data, or an error code. This function
710  * can return -EAGAIN if nonblock is specified.
711  */
712 static int folio_hole_offset(struct address_space *mapping, loff_t *offset,
713 			      unsigned min_replicas, bool nonblock)
714 {
715 	struct folio *folio;
716 	struct bch_folio *s;
717 	unsigned i, sectors;
718 	int ret = -ENOENT;
719 
720 	folio = __filemap_get_folio(mapping, *offset >> PAGE_SHIFT,
721 				    FGP_LOCK|(nonblock ? FGP_NOWAIT : 0), 0);
722 	if (IS_ERR(folio))
723 		return PTR_ERR(folio);
724 
725 	s = bch2_folio(folio);
726 	if (!s)
727 		goto unlock;
728 
729 	sectors = folio_sectors(folio);
730 	for (i = folio_pos_to_s(folio, *offset); i < sectors; i++)
731 		if (s->s[i].state < SECTOR_dirty ||
732 		    s->s[i].nr_replicas + s->s[i].replicas_reserved < min_replicas) {
733 			*offset = max(*offset,
734 				      folio_pos(folio) + (i << SECTOR_SHIFT));
735 			goto unlock;
736 		}
737 
738 	*offset = folio_end_pos(folio);
739 	ret = 0;
740 unlock:
741 	folio_unlock(folio);
742 	folio_put(folio);
743 	return ret;
744 }
745 
746 loff_t bch2_seek_pagecache_hole(struct inode *vinode,
747 				loff_t start_offset,
748 				loff_t end_offset,
749 				unsigned min_replicas,
750 				bool nonblock)
751 {
752 	struct address_space *mapping = vinode->i_mapping;
753 	loff_t offset = start_offset;
754 	loff_t ret = 0;
755 
756 	while (!ret && offset < end_offset)
757 		ret = folio_hole_offset(mapping, &offset, min_replicas, nonblock);
758 
759 	if (ret && ret != -ENOENT)
760 		return ret;
761 	return min(offset, end_offset);
762 }
763 
764 int bch2_clamp_data_hole(struct inode *inode,
765 			 u64 *hole_start,
766 			 u64 *hole_end,
767 			 unsigned min_replicas,
768 			 bool nonblock)
769 {
770 	loff_t ret;
771 
772 	ret = bch2_seek_pagecache_hole(inode,
773 		*hole_start << 9, *hole_end << 9, min_replicas, nonblock) >> 9;
774 	if (ret < 0)
775 		return ret;
776 
777 	*hole_start = ret;
778 
779 	if (*hole_start == *hole_end)
780 		return 0;
781 
782 	ret = bch2_seek_pagecache_data(inode,
783 		*hole_start << 9, *hole_end << 9, min_replicas, nonblock) >> 9;
784 	if (ret < 0)
785 		return ret;
786 
787 	*hole_end = ret;
788 	return 0;
789 }
790 
791 #endif /* NO_BCACHEFS_FS */
792