1
2
3
4
5
6
7
8
9
10
11#include <linux/kernel.h>
12#include <linux/backing-dev.h>
13#include <linux/dax.h>
14#include <linux/gfp.h>
15#include <linux/mm.h>
16#include <linux/swap.h>
17#include <linux/export.h>
18#include <linux/pagemap.h>
19#include <linux/highmem.h>
20#include <linux/pagevec.h>
21#include <linux/task_io_accounting_ops.h>
22#include <linux/shmem_fs.h>
23#include <linux/rmap.h>
24#include "internal.h"
25
26static void clear_shadow_entries(struct address_space *mapping,
27 unsigned long start, unsigned long max)
28{
29 XA_STATE(xas, &mapping->i_pages, start);
30 struct folio *folio;
31
32
33 if (shmem_mapping(mapping) || dax_mapping(mapping))
34 return;
35
36 xas_set_update(&xas, workingset_update_node);
37
38 spin_lock(&mapping->host->i_lock);
39 xas_lock_irq(&xas);
40
41
42 xas_for_each(&xas, folio, max) {
43 if (xa_is_value(folio))
44 xas_store(&xas, NULL);
45 }
46
47 xas_unlock_irq(&xas);
48 if (mapping_shrinkable(mapping))
49 inode_add_lru(mapping->host);
50 spin_unlock(&mapping->host->i_lock);
51}
52
53
54
55
56
57
58
59
60static void truncate_folio_batch_exceptionals(struct address_space *mapping,
61 struct folio_batch *fbatch, pgoff_t *indices)
62{
63 XA_STATE(xas, &mapping->i_pages, indices[0]);
64 int nr = folio_batch_count(fbatch);
65 struct folio *folio;
66 int i, j;
67
68
69 if (shmem_mapping(mapping))
70 return;
71
72 for (j = 0; j < nr; j++)
73 if (xa_is_value(fbatch->folios[j]))
74 break;
75
76 if (j == nr)
77 return;
78
79 if (dax_mapping(mapping)) {
80 for (i = j; i < nr; i++) {
81 if (xa_is_value(fbatch->folios[i])) {
82
83
84
85
86
87
88
89 WARN_ON_ONCE(1);
90
91
92
93
94
95 dax_delete_mapping_entry(mapping, indices[i]);
96 }
97 }
98 goto out;
99 }
100
101 xas_set(&xas, indices[j]);
102 xas_set_update(&xas, workingset_update_node);
103
104 spin_lock(&mapping->host->i_lock);
105 xas_lock_irq(&xas);
106
107 xas_for_each(&xas, folio, indices[nr-1]) {
108 if (xa_is_value(folio))
109 xas_store(&xas, NULL);
110 }
111
112 xas_unlock_irq(&xas);
113 if (mapping_shrinkable(mapping))
114 inode_add_lru(mapping->host);
115 spin_unlock(&mapping->host->i_lock);
116out:
117 folio_batch_remove_exceptionals(fbatch);
118}
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135void folio_invalidate(struct folio *folio, size_t offset, size_t length)
136{
137 const struct address_space_operations *aops = folio->mapping->a_ops;
138
139 if (aops->invalidate_folio)
140 aops->invalidate_folio(folio, offset, length);
141}
142EXPORT_SYMBOL_GPL(folio_invalidate);
143
144
145
146
147
148
149
150
151
152
153
154static void truncate_cleanup_folio(struct folio *folio)
155{
156 if (folio_mapped(folio))
157 unmap_mapping_folio(folio);
158
159 if (folio_needs_release(folio))
160 folio_invalidate(folio, 0, folio_size(folio));
161
162
163
164
165
166
167 folio_cancel_dirty(folio);
168}
169
170int truncate_inode_folio(struct address_space *mapping, struct folio *folio)
171{
172 if (folio->mapping != mapping)
173 return -EIO;
174
175 truncate_cleanup_folio(folio);
176 filemap_remove_folio(folio);
177 return 0;
178}
179
180
181
182
183
184
185
186
187
188
189
190
191bool truncate_inode_partial_folio(struct folio *folio, loff_t start, loff_t end)
192{
193 loff_t pos = folio_pos(folio);
194 size_t size = folio_size(folio);
195 unsigned int offset, length;
196 struct page *split_at, *split_at2;
197
198 if (pos < start)
199 offset = start - pos;
200 else
201 offset = 0;
202 if (pos + size <= (u64)end)
203 length = size - offset;
204 else
205 length = end + 1 - pos - offset;
206
207 folio_wait_writeback(folio);
208 if (length == size) {
209 truncate_inode_folio(folio->mapping, folio);
210 return true;
211 }
212
213
214
215
216
217
218 if (!mapping_inaccessible(folio->mapping))
219 folio_zero_range(folio, offset, length);
220
221 if (folio_needs_release(folio))
222 folio_invalidate(folio, offset, length);
223 if (!folio_test_large(folio))
224 return true;
225
226 split_at = folio_page(folio, PAGE_ALIGN_DOWN(offset) / PAGE_SIZE);
227 if (!try_folio_split(folio, split_at, NULL)) {
228
229
230
231
232
233 struct folio *folio2;
234
235 if (offset + length == size)
236 goto no_split;
237
238 split_at2 = folio_page(folio,
239 PAGE_ALIGN_DOWN(offset + length) / PAGE_SIZE);
240 folio2 = page_folio(split_at2);
241
242 if (!folio_try_get(folio2))
243 goto no_split;
244
245 if (!folio_test_large(folio2))
246 goto out;
247
248 if (!folio_trylock(folio2))
249 goto out;
250
251
252
253
254
255 if (folio_test_large(folio2) &&
256 folio2->mapping == folio->mapping)
257 try_folio_split(folio2, split_at2, NULL);
258
259 folio_unlock(folio2);
260out:
261 folio_put(folio2);
262no_split:
263 return true;
264 }
265 if (folio_test_dirty(folio))
266 return false;
267 truncate_inode_folio(folio->mapping, folio);
268 return true;
269}
270
271
272
273
274int generic_error_remove_folio(struct address_space *mapping,
275 struct folio *folio)
276{
277 if (!mapping)
278 return -EINVAL;
279
280
281
282
283 if (!S_ISREG(mapping->host->i_mode))
284 return -EIO;
285 return truncate_inode_folio(mapping, folio);
286}
287EXPORT_SYMBOL(generic_error_remove_folio);
288
289
290
291
292
293
294
295
296
297
298
299
300long mapping_evict_folio(struct address_space *mapping, struct folio *folio)
301{
302
303 if (!mapping)
304 return 0;
305 if (folio_test_dirty(folio) || folio_test_writeback(folio))
306 return 0;
307
308 if (folio_ref_count(folio) >
309 folio_nr_pages(folio) + folio_has_private(folio) + 1)
310 return 0;
311 if (!filemap_release_folio(folio, 0))
312 return 0;
313
314 return remove_mapping(mapping, folio);
315}
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341void truncate_inode_pages_range(struct address_space *mapping,
342 loff_t lstart, loff_t lend)
343{
344 pgoff_t start;
345 pgoff_t end;
346 struct folio_batch fbatch;
347 pgoff_t indices[PAGEVEC_SIZE];
348 pgoff_t index;
349 int i;
350 struct folio *folio;
351 bool same_folio;
352
353 if (mapping_empty(mapping))
354 return;
355
356
357
358
359
360
361
362 start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT;
363 if (lend == -1)
364
365
366
367
368
369 end = -1;
370 else
371 end = (lend + 1) >> PAGE_SHIFT;
372
373 folio_batch_init(&fbatch);
374 index = start;
375 while (index < end && find_lock_entries(mapping, &index, end - 1,
376 &fbatch, indices)) {
377 truncate_folio_batch_exceptionals(mapping, &fbatch, indices);
378 for (i = 0; i < folio_batch_count(&fbatch); i++)
379 truncate_cleanup_folio(fbatch.folios[i]);
380 delete_from_page_cache_batch(mapping, &fbatch);
381 for (i = 0; i < folio_batch_count(&fbatch); i++)
382 folio_unlock(fbatch.folios[i]);
383 folio_batch_release(&fbatch);
384 cond_resched();
385 }
386
387 same_folio = (lstart >> PAGE_SHIFT) == (lend >> PAGE_SHIFT);
388 folio = __filemap_get_folio(mapping, lstart >> PAGE_SHIFT, FGP_LOCK, 0);
389 if (!IS_ERR(folio)) {
390 same_folio = lend < folio_pos(folio) + folio_size(folio);
391 if (!truncate_inode_partial_folio(folio, lstart, lend)) {
392 start = folio_next_index(folio);
393 if (same_folio)
394 end = folio->index;
395 }
396 folio_unlock(folio);
397 folio_put(folio);
398 folio = NULL;
399 }
400
401 if (!same_folio) {
402 folio = __filemap_get_folio(mapping, lend >> PAGE_SHIFT,
403 FGP_LOCK, 0);
404 if (!IS_ERR(folio)) {
405 if (!truncate_inode_partial_folio(folio, lstart, lend))
406 end = folio->index;
407 folio_unlock(folio);
408 folio_put(folio);
409 }
410 }
411
412 index = start;
413 while (index < end) {
414 cond_resched();
415 if (!find_get_entries(mapping, &index, end - 1, &fbatch,
416 indices)) {
417
418 if (index == start)
419 break;
420
421 index = start;
422 continue;
423 }
424
425 for (i = 0; i < folio_batch_count(&fbatch); i++) {
426 struct folio *folio = fbatch.folios[i];
427
428
429
430 if (xa_is_value(folio))
431 continue;
432
433 folio_lock(folio);
434 VM_BUG_ON_FOLIO(!folio_contains(folio, indices[i]), folio);
435 folio_wait_writeback(folio);
436 truncate_inode_folio(mapping, folio);
437 folio_unlock(folio);
438 }
439 truncate_folio_batch_exceptionals(mapping, &fbatch, indices);
440 folio_batch_release(&fbatch);
441 }
442}
443EXPORT_SYMBOL(truncate_inode_pages_range);
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458void truncate_inode_pages(struct address_space *mapping, loff_t lstart)
459{
460 truncate_inode_pages_range(mapping, lstart, (loff_t)-1);
461}
462EXPORT_SYMBOL(truncate_inode_pages);
463
464
465
466
467
468
469
470
471
472
473void truncate_inode_pages_final(struct address_space *mapping)
474{
475
476
477
478
479
480
481
482 mapping_set_exiting(mapping);
483
484 if (!mapping_empty(mapping)) {
485
486
487
488
489
490
491 xa_lock_irq(&mapping->i_pages);
492 xa_unlock_irq(&mapping->i_pages);
493 }
494
495 truncate_inode_pages(mapping, 0);
496}
497EXPORT_SYMBOL(truncate_inode_pages_final);
498
499
500
501
502
503
504
505
506
507
508
509unsigned long mapping_try_invalidate(struct address_space *mapping,
510 pgoff_t start, pgoff_t end, unsigned long *nr_failed)
511{
512 pgoff_t indices[PAGEVEC_SIZE];
513 struct folio_batch fbatch;
514 pgoff_t index = start;
515 unsigned long ret;
516 unsigned long count = 0;
517 int i;
518
519 folio_batch_init(&fbatch);
520 while (find_lock_entries(mapping, &index, end, &fbatch, indices)) {
521 bool xa_has_values = false;
522 int nr = folio_batch_count(&fbatch);
523
524 for (i = 0; i < nr; i++) {
525 struct folio *folio = fbatch.folios[i];
526
527
528
529 if (xa_is_value(folio)) {
530 xa_has_values = true;
531 count++;
532 continue;
533 }
534
535 ret = mapping_evict_folio(mapping, folio);
536 folio_unlock(folio);
537
538
539
540
541 if (!ret) {
542 deactivate_file_folio(folio);
543
544 if (nr_failed)
545 (*nr_failed)++;
546 }
547 count += ret;
548 }
549
550 if (xa_has_values)
551 clear_shadow_entries(mapping, indices[0], indices[nr-1]);
552
553 folio_batch_remove_exceptionals(&fbatch);
554 folio_batch_release(&fbatch);
555 cond_resched();
556 }
557 return count;
558}
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574unsigned long invalidate_mapping_pages(struct address_space *mapping,
575 pgoff_t start, pgoff_t end)
576{
577 return mapping_try_invalidate(mapping, start, end, NULL);
578}
579EXPORT_SYMBOL(invalidate_mapping_pages);
580
581static int folio_launder(struct address_space *mapping, struct folio *folio)
582{
583 if (!folio_test_dirty(folio))
584 return 0;
585 if (folio->mapping != mapping || mapping->a_ops->launder_folio == NULL)
586 return 0;
587 return mapping->a_ops->launder_folio(folio);
588}
589
590
591
592
593
594
595
596
597int folio_unmap_invalidate(struct address_space *mapping, struct folio *folio,
598 gfp_t gfp)
599{
600 int ret;
601
602 VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
603
604 if (folio_mapped(folio))
605 unmap_mapping_folio(folio);
606 BUG_ON(folio_mapped(folio));
607
608 ret = folio_launder(mapping, folio);
609 if (ret)
610 return ret;
611 if (folio->mapping != mapping)
612 return -EBUSY;
613 if (!filemap_release_folio(folio, gfp))
614 return -EBUSY;
615
616 spin_lock(&mapping->host->i_lock);
617 xa_lock_irq(&mapping->i_pages);
618 if (folio_test_dirty(folio))
619 goto failed;
620
621 BUG_ON(folio_has_private(folio));
622 __filemap_remove_folio(folio, NULL);
623 xa_unlock_irq(&mapping->i_pages);
624 if (mapping_shrinkable(mapping))
625 inode_add_lru(mapping->host);
626 spin_unlock(&mapping->host->i_lock);
627
628 filemap_free_folio(mapping, folio);
629 return 1;
630failed:
631 xa_unlock_irq(&mapping->i_pages);
632 spin_unlock(&mapping->host->i_lock);
633 return -EBUSY;
634}
635
636
637
638
639
640
641
642
643
644
645
646
647int invalidate_inode_pages2_range(struct address_space *mapping,
648 pgoff_t start, pgoff_t end)
649{
650 pgoff_t indices[PAGEVEC_SIZE];
651 struct folio_batch fbatch;
652 pgoff_t index;
653 int i;
654 int ret = 0;
655 int ret2 = 0;
656 int did_range_unmap = 0;
657
658 if (mapping_empty(mapping))
659 return 0;
660
661 folio_batch_init(&fbatch);
662 index = start;
663 while (find_get_entries(mapping, &index, end, &fbatch, indices)) {
664 bool xa_has_values = false;
665 int nr = folio_batch_count(&fbatch);
666
667 for (i = 0; i < nr; i++) {
668 struct folio *folio = fbatch.folios[i];
669
670
671
672 if (xa_is_value(folio)) {
673 xa_has_values = true;
674 if (dax_mapping(mapping) &&
675 !dax_invalidate_mapping_entry_sync(mapping, indices[i]))
676 ret = -EBUSY;
677 continue;
678 }
679
680 if (!did_range_unmap && folio_mapped(folio)) {
681
682
683
684
685 unmap_mapping_pages(mapping, indices[i],
686 (1 + end - indices[i]), false);
687 did_range_unmap = 1;
688 }
689
690 folio_lock(folio);
691 if (unlikely(folio->mapping != mapping)) {
692 folio_unlock(folio);
693 continue;
694 }
695 VM_BUG_ON_FOLIO(!folio_contains(folio, indices[i]), folio);
696 folio_wait_writeback(folio);
697 ret2 = folio_unmap_invalidate(mapping, folio, GFP_KERNEL);
698 if (ret2 < 0)
699 ret = ret2;
700 folio_unlock(folio);
701 }
702
703 if (xa_has_values)
704 clear_shadow_entries(mapping, indices[0], indices[nr-1]);
705
706 folio_batch_remove_exceptionals(&fbatch);
707 folio_batch_release(&fbatch);
708 cond_resched();
709 }
710
711
712
713
714
715
716
717 if (dax_mapping(mapping)) {
718 unmap_mapping_pages(mapping, start, end - start + 1, false);
719 }
720 return ret;
721}
722EXPORT_SYMBOL_GPL(invalidate_inode_pages2_range);
723
724
725
726
727
728
729
730
731
732
733int invalidate_inode_pages2(struct address_space *mapping)
734{
735 return invalidate_inode_pages2_range(mapping, 0, -1);
736}
737EXPORT_SYMBOL_GPL(invalidate_inode_pages2);
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754void truncate_pagecache(struct inode *inode, loff_t newsize)
755{
756 struct address_space *mapping = inode->i_mapping;
757 loff_t holebegin = round_up(newsize, PAGE_SIZE);
758
759
760
761
762
763
764
765
766
767
768 unmap_mapping_range(mapping, holebegin, 0, 1);
769 truncate_inode_pages(mapping, newsize);
770 unmap_mapping_range(mapping, holebegin, 0, 1);
771}
772EXPORT_SYMBOL(truncate_pagecache);
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787void truncate_setsize(struct inode *inode, loff_t newsize)
788{
789 loff_t oldsize = inode->i_size;
790
791 i_size_write(inode, newsize);
792 if (newsize > oldsize)
793 pagecache_isize_extended(inode, oldsize, newsize);
794 truncate_pagecache(inode, newsize);
795}
796EXPORT_SYMBOL(truncate_setsize);
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817void pagecache_isize_extended(struct inode *inode, loff_t from, loff_t to)
818{
819 int bsize = i_blocksize(inode);
820 loff_t rounded_from;
821 struct folio *folio;
822
823 WARN_ON(to > inode->i_size);
824
825 if (from >= to || bsize >= PAGE_SIZE)
826 return;
827
828 rounded_from = round_up(from, bsize);
829 if (to <= rounded_from || !(rounded_from & (PAGE_SIZE - 1)))
830 return;
831
832 folio = filemap_lock_folio(inode->i_mapping, from / PAGE_SIZE);
833
834 if (IS_ERR(folio))
835 return;
836
837
838
839
840 if (folio_mkclean(folio))
841 folio_mark_dirty(folio);
842
843
844
845
846
847
848 if (folio_test_dirty(folio)) {
849 unsigned int offset, end;
850
851 offset = from - folio_pos(folio);
852 end = min_t(unsigned int, to - folio_pos(folio),
853 folio_size(folio));
854 folio_zero_segment(folio, offset, end);
855 }
856
857 folio_unlock(folio);
858 folio_put(folio);
859}
860EXPORT_SYMBOL(pagecache_isize_extended);
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875void truncate_pagecache_range(struct inode *inode, loff_t lstart, loff_t lend)
876{
877 struct address_space *mapping = inode->i_mapping;
878 loff_t unmap_start = round_up(lstart, PAGE_SIZE);
879 loff_t unmap_end = round_down(1 + lend, PAGE_SIZE) - 1;
880
881
882
883
884
885
886
887
888
889
890
891
892
893 if ((u64)unmap_end > (u64)unmap_start)
894 unmap_mapping_range(mapping, unmap_start,
895 1 + unmap_end - unmap_start, 0);
896 truncate_inode_pages_range(mapping, lstart, lend);
897}
898EXPORT_SYMBOL(truncate_pagecache_range);
899