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