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_bit.h"
22#include "xfs_log.h"
23#include "xfs_inum.h"
24#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
27#include "xfs_dir2.h"
28#include "xfs_dmapi.h"
29#include "xfs_mount.h"
30#include "xfs_bmap_btree.h"
31#include "xfs_alloc_btree.h"
32#include "xfs_ialloc_btree.h"
33#include "xfs_btree.h"
34#include "xfs_dir2_sf.h"
35#include "xfs_attr_sf.h"
36#include "xfs_inode.h"
37#include "xfs_dinode.h"
38#include "xfs_error.h"
39#include "xfs_mru_cache.h"
40#include "xfs_filestream.h"
41#include "xfs_vnodeops.h"
42#include "xfs_utils.h"
43#include "xfs_buf_item.h"
44#include "xfs_inode_item.h"
45#include "xfs_rw.h"
46#include "xfs_quota.h"
47
48#include <linux/kthread.h>
49#include <linux/freezer.h>
50
51
52STATIC xfs_inode_t *
53xfs_inode_ag_lookup(
54 struct xfs_mount *mp,
55 struct xfs_perag *pag,
56 uint32_t *first_index,
57 int tag)
58{
59 int nr_found;
60 struct xfs_inode *ip;
61
62
63
64
65
66
67 read_lock(&pag->pag_ici_lock);
68 if (tag == XFS_ICI_NO_TAG) {
69 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
70 (void **)&ip, *first_index, 1);
71 } else {
72 nr_found = radix_tree_gang_lookup_tag(&pag->pag_ici_root,
73 (void **)&ip, *first_index, 1, tag);
74 }
75 if (!nr_found)
76 goto unlock;
77
78
79
80
81
82
83
84 *first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
85 if (*first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
86 goto unlock;
87
88 return ip;
89
90unlock:
91 read_unlock(&pag->pag_ici_lock);
92 return NULL;
93}
94
95STATIC int
96xfs_inode_ag_walk(
97 struct xfs_mount *mp,
98 xfs_agnumber_t ag,
99 int (*execute)(struct xfs_inode *ip,
100 struct xfs_perag *pag, int flags),
101 int flags,
102 int tag)
103{
104 struct xfs_perag *pag = &mp->m_perag[ag];
105 uint32_t first_index;
106 int last_error = 0;
107 int skipped;
108
109restart:
110 skipped = 0;
111 first_index = 0;
112 do {
113 int error = 0;
114 xfs_inode_t *ip;
115
116 ip = xfs_inode_ag_lookup(mp, pag, &first_index, tag);
117 if (!ip)
118 break;
119
120 error = execute(ip, pag, flags);
121 if (error == EAGAIN) {
122 skipped++;
123 continue;
124 }
125 if (error)
126 last_error = error;
127
128
129
130 if (error == EFSCORRUPTED)
131 break;
132
133 } while (1);
134
135 if (skipped) {
136 delay(1);
137 goto restart;
138 }
139
140 xfs_put_perag(mp, pag);
141 return last_error;
142}
143
144int
145xfs_inode_ag_iterator(
146 struct xfs_mount *mp,
147 int (*execute)(struct xfs_inode *ip,
148 struct xfs_perag *pag, int flags),
149 int flags,
150 int tag)
151{
152 int error = 0;
153 int last_error = 0;
154 xfs_agnumber_t ag;
155
156 for (ag = 0; ag < mp->m_sb.sb_agcount; ag++) {
157 if (!mp->m_perag[ag].pag_ici_init)
158 continue;
159 error = xfs_inode_ag_walk(mp, ag, execute, flags, tag);
160 if (error) {
161 last_error = error;
162 if (error == EFSCORRUPTED)
163 break;
164 }
165 }
166 return XFS_ERROR(last_error);
167}
168
169
170int
171xfs_sync_inode_valid(
172 struct xfs_inode *ip,
173 struct xfs_perag *pag)
174{
175 struct inode *inode = VFS_I(ip);
176
177
178 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
179 read_unlock(&pag->pag_ici_lock);
180 return EFSCORRUPTED;
181 }
182
183
184
185
186
187
188 if (!igrab(inode)) {
189 read_unlock(&pag->pag_ici_lock);
190 return ENOENT;
191 }
192 read_unlock(&pag->pag_ici_lock);
193
194 if (is_bad_inode(inode) || xfs_iflags_test(ip, XFS_INEW)) {
195 IRELE(ip);
196 return ENOENT;
197 }
198
199 return 0;
200}
201
202STATIC int
203xfs_sync_inode_data(
204 struct xfs_inode *ip,
205 struct xfs_perag *pag,
206 int flags)
207{
208 struct inode *inode = VFS_I(ip);
209 struct address_space *mapping = inode->i_mapping;
210 int error = 0;
211
212 error = xfs_sync_inode_valid(ip, pag);
213 if (error)
214 return error;
215
216 if (!mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
217 goto out_wait;
218
219 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED)) {
220 if (flags & SYNC_TRYLOCK)
221 goto out_wait;
222 xfs_ilock(ip, XFS_IOLOCK_SHARED);
223 }
224
225 error = xfs_flush_pages(ip, 0, -1, (flags & SYNC_WAIT) ?
226 0 : XFS_B_ASYNC, FI_NONE);
227 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
228
229 out_wait:
230 if (flags & SYNC_WAIT)
231 xfs_ioend_wait(ip);
232 IRELE(ip);
233 return error;
234}
235
236STATIC int
237xfs_sync_inode_attr(
238 struct xfs_inode *ip,
239 struct xfs_perag *pag,
240 int flags)
241{
242 int error = 0;
243
244 error = xfs_sync_inode_valid(ip, pag);
245 if (error)
246 return error;
247
248 xfs_ilock(ip, XFS_ILOCK_SHARED);
249 if (xfs_inode_clean(ip))
250 goto out_unlock;
251 if (!xfs_iflock_nowait(ip)) {
252 if (!(flags & SYNC_WAIT))
253 goto out_unlock;
254 xfs_iflock(ip);
255 }
256
257 if (xfs_inode_clean(ip)) {
258 xfs_ifunlock(ip);
259 goto out_unlock;
260 }
261
262 error = xfs_iflush(ip, (flags & SYNC_WAIT) ?
263 XFS_IFLUSH_SYNC : XFS_IFLUSH_DELWRI);
264
265 out_unlock:
266 xfs_iunlock(ip, XFS_ILOCK_SHARED);
267 IRELE(ip);
268 return error;
269}
270
271
272
273
274int
275xfs_sync_data(
276 struct xfs_mount *mp,
277 int flags)
278{
279 int error;
280
281 ASSERT((flags & ~(SYNC_TRYLOCK|SYNC_WAIT)) == 0);
282
283 error = xfs_inode_ag_iterator(mp, xfs_sync_inode_data, flags,
284 XFS_ICI_NO_TAG);
285 if (error)
286 return XFS_ERROR(error);
287
288 xfs_log_force(mp, 0,
289 (flags & SYNC_WAIT) ?
290 XFS_LOG_FORCE | XFS_LOG_SYNC :
291 XFS_LOG_FORCE);
292 return 0;
293}
294
295
296
297
298int
299xfs_sync_attr(
300 struct xfs_mount *mp,
301 int flags)
302{
303 ASSERT((flags & ~SYNC_WAIT) == 0);
304
305 return xfs_inode_ag_iterator(mp, xfs_sync_inode_attr, flags,
306 XFS_ICI_NO_TAG);
307}
308
309STATIC int
310xfs_commit_dummy_trans(
311 struct xfs_mount *mp,
312 uint flags)
313{
314 struct xfs_inode *ip = mp->m_rootip;
315 struct xfs_trans *tp;
316 int error;
317 int log_flags = XFS_LOG_FORCE;
318
319 if (flags & SYNC_WAIT)
320 log_flags |= XFS_LOG_SYNC;
321
322
323
324
325
326 tp = xfs_trans_alloc(mp, XFS_TRANS_DUMMY1);
327 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
328 if (error) {
329 xfs_trans_cancel(tp, 0);
330 return error;
331 }
332
333 xfs_ilock(ip, XFS_ILOCK_EXCL);
334
335 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
336 xfs_trans_ihold(tp, ip);
337 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
338 error = xfs_trans_commit(tp, 0);
339 xfs_iunlock(ip, XFS_ILOCK_EXCL);
340
341
342 xfs_log_force(mp, 0, log_flags);
343 return error;
344}
345
346int
347xfs_sync_fsdata(
348 struct xfs_mount *mp,
349 int flags)
350{
351 struct xfs_buf *bp;
352 struct xfs_buf_log_item *bip;
353 int error = 0;
354
355
356
357
358
359 if (flags & SYNC_TRYLOCK) {
360 ASSERT(!(flags & SYNC_WAIT));
361
362 bp = xfs_getsb(mp, XFS_BUF_TRYLOCK);
363 if (!bp)
364 goto out;
365
366 bip = XFS_BUF_FSPRIVATE(bp, struct xfs_buf_log_item *);
367 if (!bip || !xfs_buf_item_dirty(bip) || XFS_BUF_ISPINNED(bp))
368 goto out_brelse;
369 } else {
370 bp = xfs_getsb(mp, 0);
371
372
373
374
375
376
377
378
379
380
381 if (XFS_BUF_ISPINNED(bp))
382 xfs_log_force(mp, 0, XFS_LOG_FORCE);
383 }
384
385
386 if (flags & SYNC_WAIT)
387 XFS_BUF_UNASYNC(bp);
388 else
389 XFS_BUF_ASYNC(bp);
390
391 error = xfs_bwrite(mp, bp);
392 if (error)
393 return error;
394
395
396
397
398
399 if (flags & SYNC_WAIT)
400 xfs_flush_buftarg(mp->m_ddev_targp, 1);
401
402 if (xfs_log_need_covered(mp))
403 error = xfs_commit_dummy_trans(mp, flags);
404 return error;
405
406 out_brelse:
407 xfs_buf_relse(bp);
408 out:
409 return error;
410}
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429int
430xfs_quiesce_data(
431 struct xfs_mount *mp)
432{
433 int error;
434
435
436 xfs_sync_data(mp, 0);
437 xfs_qm_sync(mp, SYNC_TRYLOCK);
438
439
440 xfs_sync_data(mp, SYNC_WAIT);
441 xfs_qm_sync(mp, SYNC_WAIT);
442
443
444 xfs_filestream_flush(mp);
445
446
447 error = xfs_sync_fsdata(mp, SYNC_WAIT);
448
449
450 if (mp->m_rtdev_targp)
451 XFS_bflush(mp->m_rtdev_targp);
452
453 return error;
454}
455
456STATIC void
457xfs_quiesce_fs(
458 struct xfs_mount *mp)
459{
460 int count = 0, pincount;
461
462 xfs_flush_buftarg(mp->m_ddev_targp, 0);
463 xfs_reclaim_inodes(mp, XFS_IFLUSH_DELWRI_ELSE_ASYNC);
464
465
466
467
468
469
470
471 do {
472 xfs_sync_attr(mp, SYNC_WAIT);
473 pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1);
474 if (!pincount) {
475 delay(50);
476 count++;
477 }
478 } while (count < 2);
479}
480
481
482
483
484
485
486void
487xfs_quiesce_attr(
488 struct xfs_mount *mp)
489{
490 int error = 0;
491
492
493 while (atomic_read(&mp->m_active_trans) > 0)
494 delay(100);
495
496
497 xfs_quiesce_fs(mp);
498
499
500
501
502
503 WARN_ON(atomic_read(&mp->m_active_trans) != 0);
504
505
506 error = xfs_log_sbcount(mp, 1);
507 if (error)
508 xfs_fs_cmn_err(CE_WARN, mp,
509 "xfs_attr_quiesce: failed to log sb changes. "
510 "Frozen image may not be consistent.");
511 xfs_log_unmount_write(mp);
512 xfs_unmountfs_writesb(mp);
513}
514
515
516
517
518
519
520
521
522STATIC void
523xfs_syncd_queue_work(
524 struct xfs_mount *mp,
525 void *data,
526 void (*syncer)(struct xfs_mount *, void *),
527 struct completion *completion)
528{
529 struct xfs_sync_work *work;
530
531 work = kmem_alloc(sizeof(struct xfs_sync_work), KM_SLEEP);
532 INIT_LIST_HEAD(&work->w_list);
533 work->w_syncer = syncer;
534 work->w_data = data;
535 work->w_mount = mp;
536 work->w_completion = completion;
537 spin_lock(&mp->m_sync_lock);
538 list_add_tail(&work->w_list, &mp->m_sync_list);
539 spin_unlock(&mp->m_sync_lock);
540 wake_up_process(mp->m_sync_task);
541}
542
543
544
545
546
547
548
549STATIC void
550xfs_flush_inodes_work(
551 struct xfs_mount *mp,
552 void *arg)
553{
554 struct inode *inode = arg;
555 xfs_sync_data(mp, SYNC_TRYLOCK);
556 xfs_sync_data(mp, SYNC_TRYLOCK | SYNC_WAIT);
557 iput(inode);
558}
559
560void
561xfs_flush_inodes(
562 xfs_inode_t *ip)
563{
564 struct inode *inode = VFS_I(ip);
565 DECLARE_COMPLETION_ONSTACK(completion);
566
567 igrab(inode);
568 xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_inodes_work, &completion);
569 wait_for_completion(&completion);
570 xfs_log_force(ip->i_mount, (xfs_lsn_t)0, XFS_LOG_FORCE|XFS_LOG_SYNC);
571}
572
573
574
575
576
577
578STATIC void
579xfs_sync_worker(
580 struct xfs_mount *mp,
581 void *unused)
582{
583 int error;
584
585 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
586 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
587 xfs_reclaim_inodes(mp, XFS_IFLUSH_DELWRI_ELSE_ASYNC);
588
589 error = xfs_qm_sync(mp, SYNC_TRYLOCK);
590 error = xfs_sync_fsdata(mp, SYNC_TRYLOCK);
591 }
592 mp->m_sync_seq++;
593 wake_up(&mp->m_wait_single_sync_task);
594}
595
596STATIC int
597xfssyncd(
598 void *arg)
599{
600 struct xfs_mount *mp = arg;
601 long timeleft;
602 xfs_sync_work_t *work, *n;
603 LIST_HEAD (tmp);
604
605 set_freezable();
606 timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10);
607 for (;;) {
608 timeleft = schedule_timeout_interruptible(timeleft);
609
610 try_to_freeze();
611 if (kthread_should_stop() && list_empty(&mp->m_sync_list))
612 break;
613
614 spin_lock(&mp->m_sync_lock);
615
616
617
618
619
620 if (!timeleft || list_empty(&mp->m_sync_list)) {
621 if (!timeleft)
622 timeleft = xfs_syncd_centisecs *
623 msecs_to_jiffies(10);
624 INIT_LIST_HEAD(&mp->m_sync_work.w_list);
625 list_add_tail(&mp->m_sync_work.w_list,
626 &mp->m_sync_list);
627 }
628 list_for_each_entry_safe(work, n, &mp->m_sync_list, w_list)
629 list_move(&work->w_list, &tmp);
630 spin_unlock(&mp->m_sync_lock);
631
632 list_for_each_entry_safe(work, n, &tmp, w_list) {
633 (*work->w_syncer)(mp, work->w_data);
634 list_del(&work->w_list);
635 if (work == &mp->m_sync_work)
636 continue;
637 if (work->w_completion)
638 complete(work->w_completion);
639 kmem_free(work);
640 }
641 }
642
643 return 0;
644}
645
646int
647xfs_syncd_init(
648 struct xfs_mount *mp)
649{
650 mp->m_sync_work.w_syncer = xfs_sync_worker;
651 mp->m_sync_work.w_mount = mp;
652 mp->m_sync_work.w_completion = NULL;
653 mp->m_sync_task = kthread_run(xfssyncd, mp, "xfssyncd");
654 if (IS_ERR(mp->m_sync_task))
655 return -PTR_ERR(mp->m_sync_task);
656 return 0;
657}
658
659void
660xfs_syncd_stop(
661 struct xfs_mount *mp)
662{
663 kthread_stop(mp->m_sync_task);
664}
665
666int
667xfs_reclaim_inode(
668 xfs_inode_t *ip,
669 int locked,
670 int sync_mode)
671{
672 xfs_perag_t *pag = xfs_get_perag(ip->i_mount, ip->i_ino);
673
674
675
676
677
678
679 write_lock(&pag->pag_ici_lock);
680 spin_lock(&ip->i_flags_lock);
681 if (__xfs_iflags_test(ip, XFS_IRECLAIM) ||
682 !__xfs_iflags_test(ip, XFS_IRECLAIMABLE)) {
683 spin_unlock(&ip->i_flags_lock);
684 write_unlock(&pag->pag_ici_lock);
685 if (locked) {
686 xfs_ifunlock(ip);
687 xfs_iunlock(ip, XFS_ILOCK_EXCL);
688 }
689 return -EAGAIN;
690 }
691 __xfs_iflags_set(ip, XFS_IRECLAIM);
692 spin_unlock(&ip->i_flags_lock);
693 write_unlock(&pag->pag_ici_lock);
694 xfs_put_perag(ip->i_mount, pag);
695
696
697
698
699
700
701
702
703
704
705
706
707 if (!locked) {
708 xfs_ilock(ip, XFS_ILOCK_EXCL);
709 xfs_iflock(ip);
710 }
711
712
713
714
715
716 if (!is_bad_inode(VFS_I(ip)) && xfs_iflush(ip, sync_mode) == 0) {
717
718 xfs_iflock(ip);
719 xfs_ifunlock(ip);
720 }
721
722 xfs_iunlock(ip, XFS_ILOCK_EXCL);
723 xfs_ireclaim(ip);
724 return 0;
725}
726
727void
728__xfs_inode_set_reclaim_tag(
729 struct xfs_perag *pag,
730 struct xfs_inode *ip)
731{
732 radix_tree_tag_set(&pag->pag_ici_root,
733 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
734 XFS_ICI_RECLAIM_TAG);
735}
736
737
738
739
740
741
742void
743xfs_inode_set_reclaim_tag(
744 xfs_inode_t *ip)
745{
746 xfs_mount_t *mp = ip->i_mount;
747 xfs_perag_t *pag = xfs_get_perag(mp, ip->i_ino);
748
749 read_lock(&pag->pag_ici_lock);
750 spin_lock(&ip->i_flags_lock);
751 __xfs_inode_set_reclaim_tag(pag, ip);
752 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
753 spin_unlock(&ip->i_flags_lock);
754 read_unlock(&pag->pag_ici_lock);
755 xfs_put_perag(mp, pag);
756}
757
758void
759__xfs_inode_clear_reclaim_tag(
760 xfs_mount_t *mp,
761 xfs_perag_t *pag,
762 xfs_inode_t *ip)
763{
764 radix_tree_tag_clear(&pag->pag_ici_root,
765 XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
766}
767
768STATIC int
769xfs_reclaim_inode_now(
770 struct xfs_inode *ip,
771 struct xfs_perag *pag,
772 int flags)
773{
774
775 if (xfs_iflags_test(ip, XFS_IRECLAIM)) {
776 read_unlock(&pag->pag_ici_lock);
777 return 0;
778 }
779 read_unlock(&pag->pag_ici_lock);
780
781 return xfs_reclaim_inode(ip, 0, flags);
782}
783
784int
785xfs_reclaim_inodes(
786 xfs_mount_t *mp,
787 int mode)
788{
789 return xfs_inode_ag_iterator(mp, xfs_reclaim_inode_now, mode,
790 XFS_ICI_RECLAIM_TAG);
791}
792