1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include "xfs.h"
19#include "xfs_fs.h"
20#include "xfs_types.h"
21#include "xfs_log.h"
22#include "xfs_inum.h"
23#include "xfs_trans.h"
24#include "xfs_trans_priv.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
27#include "xfs_mount.h"
28#include "xfs_bmap_btree.h"
29#include "xfs_inode.h"
30#include "xfs_dinode.h"
31#include "xfs_error.h"
32#include "xfs_filestream.h"
33#include "xfs_vnodeops.h"
34#include "xfs_inode_item.h"
35#include "xfs_quota.h"
36#include "xfs_trace.h"
37#include "xfs_fsops.h"
38
39#include <linux/kthread.h>
40#include <linux/freezer.h>
41
42struct workqueue_struct *xfs_syncd_wq;
43
44
45
46
47
48
49
50#define XFS_LOOKUP_BATCH 32
51
52STATIC int
53xfs_inode_ag_walk_grab(
54 struct xfs_inode *ip)
55{
56 struct inode *inode = VFS_I(ip);
57
58 ASSERT(rcu_read_lock_held());
59
60
61
62
63
64
65
66
67
68
69 spin_lock(&ip->i_flags_lock);
70 if (!ip->i_ino)
71 goto out_unlock_noent;
72
73
74 if (__xfs_iflags_test(ip, XFS_INEW | XFS_IRECLAIMABLE | XFS_IRECLAIM))
75 goto out_unlock_noent;
76 spin_unlock(&ip->i_flags_lock);
77
78
79 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
80 return EFSCORRUPTED;
81
82
83 if (!igrab(inode))
84 return ENOENT;
85
86 if (is_bad_inode(inode)) {
87 IRELE(ip);
88 return ENOENT;
89 }
90
91
92 return 0;
93
94out_unlock_noent:
95 spin_unlock(&ip->i_flags_lock);
96 return ENOENT;
97}
98
99STATIC int
100xfs_inode_ag_walk(
101 struct xfs_mount *mp,
102 struct xfs_perag *pag,
103 int (*execute)(struct xfs_inode *ip,
104 struct xfs_perag *pag, int flags),
105 int flags)
106{
107 uint32_t first_index;
108 int last_error = 0;
109 int skipped;
110 int done;
111 int nr_found;
112
113restart:
114 done = 0;
115 skipped = 0;
116 first_index = 0;
117 nr_found = 0;
118 do {
119 struct xfs_inode *batch[XFS_LOOKUP_BATCH];
120 int error = 0;
121 int i;
122
123 rcu_read_lock();
124 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
125 (void **)batch, first_index,
126 XFS_LOOKUP_BATCH);
127 if (!nr_found) {
128 rcu_read_unlock();
129 break;
130 }
131
132
133
134
135
136 for (i = 0; i < nr_found; i++) {
137 struct xfs_inode *ip = batch[i];
138
139 if (done || xfs_inode_ag_walk_grab(ip))
140 batch[i] = NULL;
141
142
143
144
145
146
147
148
149
150
151
152
153
154 if (XFS_INO_TO_AGNO(mp, ip->i_ino) != pag->pag_agno)
155 continue;
156 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
157 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
158 done = 1;
159 }
160
161
162 rcu_read_unlock();
163
164 for (i = 0; i < nr_found; i++) {
165 if (!batch[i])
166 continue;
167 error = execute(batch[i], pag, flags);
168 IRELE(batch[i]);
169 if (error == EAGAIN) {
170 skipped++;
171 continue;
172 }
173 if (error && last_error != EFSCORRUPTED)
174 last_error = error;
175 }
176
177
178 if (error == EFSCORRUPTED)
179 break;
180
181 cond_resched();
182
183 } while (nr_found && !done);
184
185 if (skipped) {
186 delay(1);
187 goto restart;
188 }
189 return last_error;
190}
191
192int
193xfs_inode_ag_iterator(
194 struct xfs_mount *mp,
195 int (*execute)(struct xfs_inode *ip,
196 struct xfs_perag *pag, int flags),
197 int flags)
198{
199 struct xfs_perag *pag;
200 int error = 0;
201 int last_error = 0;
202 xfs_agnumber_t ag;
203
204 ag = 0;
205 while ((pag = xfs_perag_get(mp, ag))) {
206 ag = pag->pag_agno + 1;
207 error = xfs_inode_ag_walk(mp, pag, execute, flags);
208 xfs_perag_put(pag);
209 if (error) {
210 last_error = error;
211 if (error == EFSCORRUPTED)
212 break;
213 }
214 }
215 return XFS_ERROR(last_error);
216}
217
218STATIC int
219xfs_sync_inode_data(
220 struct xfs_inode *ip,
221 struct xfs_perag *pag,
222 int flags)
223{
224 struct inode *inode = VFS_I(ip);
225 struct address_space *mapping = inode->i_mapping;
226 int error = 0;
227
228 if (!mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
229 return 0;
230
231 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED)) {
232 if (flags & SYNC_TRYLOCK)
233 return 0;
234 xfs_ilock(ip, XFS_IOLOCK_SHARED);
235 }
236
237 error = xfs_flush_pages(ip, 0, -1, (flags & SYNC_WAIT) ?
238 0 : XBF_ASYNC, FI_NONE);
239 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
240 return error;
241}
242
243
244
245
246STATIC int
247xfs_sync_data(
248 struct xfs_mount *mp,
249 int flags)
250{
251 int error;
252
253 ASSERT((flags & ~(SYNC_TRYLOCK|SYNC_WAIT)) == 0);
254
255 error = xfs_inode_ag_iterator(mp, xfs_sync_inode_data, flags);
256 if (error)
257 return XFS_ERROR(error);
258
259 xfs_log_force(mp, (flags & SYNC_WAIT) ? XFS_LOG_SYNC : 0);
260 return 0;
261}
262
263STATIC int
264xfs_sync_fsdata(
265 struct xfs_mount *mp)
266{
267 struct xfs_buf *bp;
268 int error;
269
270
271
272
273
274
275
276
277
278 bp = xfs_getsb(mp, 0);
279 if (xfs_buf_ispinned(bp))
280 xfs_log_force(mp, 0);
281 error = xfs_bwrite(bp);
282 xfs_buf_relse(bp);
283 return error;
284}
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303int
304xfs_quiesce_data(
305 struct xfs_mount *mp)
306{
307 int error, error2 = 0;
308
309
310 xfs_log_force(mp, XFS_LOG_SYNC);
311
312
313 error = xfs_sync_fsdata(mp);
314
315
316 if (xfs_log_need_covered(mp))
317 error2 = xfs_fs_log_dummy(mp);
318
319 return error ? error : error2;
320}
321
322
323
324
325
326
327void
328xfs_quiesce_attr(
329 struct xfs_mount *mp)
330{
331 int error = 0;
332
333
334 while (atomic_read(&mp->m_active_trans) > 0)
335 delay(100);
336
337
338 xfs_reclaim_inodes(mp, 0);
339 xfs_reclaim_inodes(mp, SYNC_WAIT);
340
341
342 xfs_ail_push_all_sync(mp->m_ail);
343
344
345
346
347
348 WARN_ON(atomic_read(&mp->m_active_trans) != 0);
349
350
351 error = xfs_log_sbcount(mp);
352 if (error)
353 xfs_warn(mp, "xfs_attr_quiesce: failed to log sb changes. "
354 "Frozen image may not be consistent.");
355 xfs_log_unmount_write(mp);
356
357
358
359
360
361 xfs_ail_push_all_sync(mp->m_ail);
362
363
364
365
366
367
368
369 xfs_buf_lock(mp->m_sb_bp);
370 xfs_buf_unlock(mp->m_sb_bp);
371}
372
373static void
374xfs_syncd_queue_sync(
375 struct xfs_mount *mp)
376{
377 queue_delayed_work(xfs_syncd_wq, &mp->m_sync_work,
378 msecs_to_jiffies(xfs_syncd_centisecs * 10));
379}
380
381
382
383
384
385
386STATIC void
387xfs_sync_worker(
388 struct work_struct *work)
389{
390 struct xfs_mount *mp = container_of(to_delayed_work(work),
391 struct xfs_mount, m_sync_work);
392 int error;
393
394
395
396
397
398
399
400
401
402
403 if (!(mp->m_super->s_flags & MS_ACTIVE) &&
404 !(mp->m_flags & XFS_MOUNT_RDONLY)) {
405
406 if (mp->m_super->s_writers.frozen == SB_UNFROZEN &&
407 xfs_log_need_covered(mp))
408 error = xfs_fs_log_dummy(mp);
409 else
410 xfs_log_force(mp, 0);
411
412
413
414 xfs_ail_push_all(mp->m_ail);
415 }
416
417
418 xfs_syncd_queue_sync(mp);
419}
420
421
422
423
424
425
426
427
428static void
429xfs_syncd_queue_reclaim(
430 struct xfs_mount *mp)
431{
432
433 rcu_read_lock();
434 if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_RECLAIM_TAG)) {
435 queue_delayed_work(xfs_syncd_wq, &mp->m_reclaim_work,
436 msecs_to_jiffies(xfs_syncd_centisecs / 6 * 10));
437 }
438 rcu_read_unlock();
439}
440
441
442
443
444
445
446
447
448STATIC void
449xfs_reclaim_worker(
450 struct work_struct *work)
451{
452 struct xfs_mount *mp = container_of(to_delayed_work(work),
453 struct xfs_mount, m_reclaim_work);
454
455 xfs_reclaim_inodes(mp, SYNC_TRYLOCK);
456 xfs_syncd_queue_reclaim(mp);
457}
458
459
460
461
462
463
464
465
466
467
468
469
470
471void
472xfs_flush_inodes(
473 struct xfs_inode *ip)
474{
475 struct xfs_mount *mp = ip->i_mount;
476
477 queue_work(xfs_syncd_wq, &mp->m_flush_work);
478 flush_work_sync(&mp->m_flush_work);
479}
480
481STATIC void
482xfs_flush_worker(
483 struct work_struct *work)
484{
485 struct xfs_mount *mp = container_of(work,
486 struct xfs_mount, m_flush_work);
487
488 xfs_sync_data(mp, SYNC_TRYLOCK);
489 xfs_sync_data(mp, SYNC_TRYLOCK | SYNC_WAIT);
490}
491
492int
493xfs_syncd_init(
494 struct xfs_mount *mp)
495{
496 INIT_WORK(&mp->m_flush_work, xfs_flush_worker);
497 INIT_DELAYED_WORK(&mp->m_sync_work, xfs_sync_worker);
498 INIT_DELAYED_WORK(&mp->m_reclaim_work, xfs_reclaim_worker);
499
500 xfs_syncd_queue_sync(mp);
501
502 return 0;
503}
504
505void
506xfs_syncd_stop(
507 struct xfs_mount *mp)
508{
509 cancel_delayed_work_sync(&mp->m_sync_work);
510 cancel_delayed_work_sync(&mp->m_reclaim_work);
511 cancel_work_sync(&mp->m_flush_work);
512}
513
514void
515__xfs_inode_set_reclaim_tag(
516 struct xfs_perag *pag,
517 struct xfs_inode *ip)
518{
519 radix_tree_tag_set(&pag->pag_ici_root,
520 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
521 XFS_ICI_RECLAIM_TAG);
522
523 if (!pag->pag_ici_reclaimable) {
524
525 spin_lock(&ip->i_mount->m_perag_lock);
526 radix_tree_tag_set(&ip->i_mount->m_perag_tree,
527 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
528 XFS_ICI_RECLAIM_TAG);
529 spin_unlock(&ip->i_mount->m_perag_lock);
530
531
532 xfs_syncd_queue_reclaim(ip->i_mount);
533
534 trace_xfs_perag_set_reclaim(ip->i_mount, pag->pag_agno,
535 -1, _RET_IP_);
536 }
537 pag->pag_ici_reclaimable++;
538}
539
540
541
542
543
544
545void
546xfs_inode_set_reclaim_tag(
547 xfs_inode_t *ip)
548{
549 struct xfs_mount *mp = ip->i_mount;
550 struct xfs_perag *pag;
551
552 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
553 spin_lock(&pag->pag_ici_lock);
554 spin_lock(&ip->i_flags_lock);
555 __xfs_inode_set_reclaim_tag(pag, ip);
556 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
557 spin_unlock(&ip->i_flags_lock);
558 spin_unlock(&pag->pag_ici_lock);
559 xfs_perag_put(pag);
560}
561
562STATIC void
563__xfs_inode_clear_reclaim(
564 xfs_perag_t *pag,
565 xfs_inode_t *ip)
566{
567 pag->pag_ici_reclaimable--;
568 if (!pag->pag_ici_reclaimable) {
569
570 spin_lock(&ip->i_mount->m_perag_lock);
571 radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
572 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
573 XFS_ICI_RECLAIM_TAG);
574 spin_unlock(&ip->i_mount->m_perag_lock);
575 trace_xfs_perag_clear_reclaim(ip->i_mount, pag->pag_agno,
576 -1, _RET_IP_);
577 }
578}
579
580void
581__xfs_inode_clear_reclaim_tag(
582 xfs_mount_t *mp,
583 xfs_perag_t *pag,
584 xfs_inode_t *ip)
585{
586 radix_tree_tag_clear(&pag->pag_ici_root,
587 XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
588 __xfs_inode_clear_reclaim(pag, ip);
589}
590
591
592
593
594
595STATIC int
596xfs_reclaim_inode_grab(
597 struct xfs_inode *ip,
598 int flags)
599{
600 ASSERT(rcu_read_lock_held());
601
602
603 if (!ip->i_ino)
604 return 1;
605
606
607
608
609
610
611 if ((flags & SYNC_TRYLOCK) &&
612 __xfs_iflags_test(ip, XFS_IFLOCK | XFS_IRECLAIM))
613 return 1;
614
615
616
617
618
619
620
621
622
623
624
625 spin_lock(&ip->i_flags_lock);
626 if (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) ||
627 __xfs_iflags_test(ip, XFS_IRECLAIM)) {
628
629 spin_unlock(&ip->i_flags_lock);
630 return 1;
631 }
632 __xfs_iflags_set(ip, XFS_IRECLAIM);
633 spin_unlock(&ip->i_flags_lock);
634 return 0;
635}
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676STATIC int
677xfs_reclaim_inode(
678 struct xfs_inode *ip,
679 struct xfs_perag *pag,
680 int sync_mode)
681{
682 struct xfs_buf *bp = NULL;
683 int error;
684
685restart:
686 error = 0;
687 xfs_ilock(ip, XFS_ILOCK_EXCL);
688 if (!xfs_iflock_nowait(ip)) {
689 if (!(sync_mode & SYNC_WAIT))
690 goto out;
691 xfs_iflock(ip);
692 }
693
694 if (is_bad_inode(VFS_I(ip)))
695 goto reclaim;
696 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
697 xfs_iunpin_wait(ip);
698 xfs_iflush_abort(ip, false);
699 goto reclaim;
700 }
701 if (xfs_ipincount(ip)) {
702 if (!(sync_mode & SYNC_WAIT))
703 goto out_ifunlock;
704 xfs_iunpin_wait(ip);
705 }
706 if (xfs_iflags_test(ip, XFS_ISTALE))
707 goto reclaim;
708 if (xfs_inode_clean(ip))
709 goto reclaim;
710
711
712
713
714
715 if (!(sync_mode & SYNC_WAIT))
716 goto out_ifunlock;
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734 error = xfs_iflush(ip, &bp);
735 if (error == EAGAIN) {
736 xfs_iunlock(ip, XFS_ILOCK_EXCL);
737
738 delay(2);
739 goto restart;
740 }
741
742 if (!error) {
743 error = xfs_bwrite(bp);
744 xfs_buf_relse(bp);
745 }
746
747 xfs_iflock(ip);
748reclaim:
749 xfs_ifunlock(ip);
750 xfs_iunlock(ip, XFS_ILOCK_EXCL);
751
752 XFS_STATS_INC(xs_ig_reclaims);
753
754
755
756
757
758
759
760 spin_lock(&pag->pag_ici_lock);
761 if (!radix_tree_delete(&pag->pag_ici_root,
762 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino)))
763 ASSERT(0);
764 __xfs_inode_clear_reclaim(pag, ip);
765 spin_unlock(&pag->pag_ici_lock);
766
767
768
769
770
771
772
773
774
775 xfs_ilock(ip, XFS_ILOCK_EXCL);
776 xfs_qm_dqdetach(ip);
777 xfs_iunlock(ip, XFS_ILOCK_EXCL);
778
779 xfs_inode_free(ip);
780 return error;
781
782out_ifunlock:
783 xfs_ifunlock(ip);
784out:
785 xfs_iflags_clear(ip, XFS_IRECLAIM);
786 xfs_iunlock(ip, XFS_ILOCK_EXCL);
787
788
789
790
791
792
793
794 return 0;
795}
796
797
798
799
800
801
802
803int
804xfs_reclaim_inodes_ag(
805 struct xfs_mount *mp,
806 int flags,
807 int *nr_to_scan)
808{
809 struct xfs_perag *pag;
810 int error = 0;
811 int last_error = 0;
812 xfs_agnumber_t ag;
813 int trylock = flags & SYNC_TRYLOCK;
814 int skipped;
815
816restart:
817 ag = 0;
818 skipped = 0;
819 while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
820 unsigned long first_index = 0;
821 int done = 0;
822 int nr_found = 0;
823
824 ag = pag->pag_agno + 1;
825
826 if (trylock) {
827 if (!mutex_trylock(&pag->pag_ici_reclaim_lock)) {
828 skipped++;
829 xfs_perag_put(pag);
830 continue;
831 }
832 first_index = pag->pag_ici_reclaim_cursor;
833 } else
834 mutex_lock(&pag->pag_ici_reclaim_lock);
835
836 do {
837 struct xfs_inode *batch[XFS_LOOKUP_BATCH];
838 int i;
839
840 rcu_read_lock();
841 nr_found = radix_tree_gang_lookup_tag(
842 &pag->pag_ici_root,
843 (void **)batch, first_index,
844 XFS_LOOKUP_BATCH,
845 XFS_ICI_RECLAIM_TAG);
846 if (!nr_found) {
847 done = 1;
848 rcu_read_unlock();
849 break;
850 }
851
852
853
854
855
856 for (i = 0; i < nr_found; i++) {
857 struct xfs_inode *ip = batch[i];
858
859 if (done || xfs_reclaim_inode_grab(ip, flags))
860 batch[i] = NULL;
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876 if (XFS_INO_TO_AGNO(mp, ip->i_ino) !=
877 pag->pag_agno)
878 continue;
879 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
880 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
881 done = 1;
882 }
883
884
885 rcu_read_unlock();
886
887 for (i = 0; i < nr_found; i++) {
888 if (!batch[i])
889 continue;
890 error = xfs_reclaim_inode(batch[i], pag, flags);
891 if (error && last_error != EFSCORRUPTED)
892 last_error = error;
893 }
894
895 *nr_to_scan -= XFS_LOOKUP_BATCH;
896
897 cond_resched();
898
899 } while (nr_found && !done && *nr_to_scan > 0);
900
901 if (trylock && !done)
902 pag->pag_ici_reclaim_cursor = first_index;
903 else
904 pag->pag_ici_reclaim_cursor = 0;
905 mutex_unlock(&pag->pag_ici_reclaim_lock);
906 xfs_perag_put(pag);
907 }
908
909
910
911
912
913
914
915
916 if (skipped && (flags & SYNC_WAIT) && *nr_to_scan > 0) {
917 trylock = 0;
918 goto restart;
919 }
920 return XFS_ERROR(last_error);
921}
922
923int
924xfs_reclaim_inodes(
925 xfs_mount_t *mp,
926 int mode)
927{
928 int nr_to_scan = INT_MAX;
929
930 return xfs_reclaim_inodes_ag(mp, mode, &nr_to_scan);
931}
932
933
934
935
936
937
938
939
940
941
942void
943xfs_reclaim_inodes_nr(
944 struct xfs_mount *mp,
945 int nr_to_scan)
946{
947
948 xfs_syncd_queue_reclaim(mp);
949 xfs_ail_push_all(mp->m_ail);
950
951 xfs_reclaim_inodes_ag(mp, SYNC_TRYLOCK | SYNC_WAIT, &nr_to_scan);
952}
953
954
955
956
957
958int
959xfs_reclaim_inodes_count(
960 struct xfs_mount *mp)
961{
962 struct xfs_perag *pag;
963 xfs_agnumber_t ag = 0;
964 int reclaimable = 0;
965
966 while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
967 ag = pag->pag_agno + 1;
968 reclaimable += pag->pag_ici_reclaimable;
969 xfs_perag_put(pag);
970 }
971 return reclaimable;
972}
973
974