Release 2.6.35.11
[linux/fpc-iii.git] / fs / xfs / linux-2.6 / xfs_sync.c
blobf38037d8384656cd683afe81d98f8723d22d6b33
1 /*
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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 #include "xfs_trace.h"
49 #include <linux/kthread.h>
50 #include <linux/freezer.h>
53 STATIC xfs_inode_t *
54 xfs_inode_ag_lookup(
55 struct xfs_mount *mp,
56 struct xfs_perag *pag,
57 uint32_t *first_index,
58 int tag)
60 int nr_found;
61 struct xfs_inode *ip;
64 * use a gang lookup to find the next inode in the tree
65 * as the tree is sparse and a gang lookup walks to find
66 * the number of objects requested.
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);
75 if (!nr_found)
76 return NULL;
79 * Update the index for the next lookup. Catch overflows
80 * into the next AG range which can occur if we have inodes
81 * in the last block of the AG and we are currently
82 * pointing to the last inode.
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 return NULL;
87 return ip;
90 STATIC int
91 xfs_inode_ag_walk(
92 struct xfs_mount *mp,
93 struct xfs_perag *pag,
94 int (*execute)(struct xfs_inode *ip,
95 struct xfs_perag *pag, int flags),
96 int flags,
97 int tag,
98 int exclusive,
99 int *nr_to_scan)
101 uint32_t first_index;
102 int last_error = 0;
103 int skipped;
105 restart:
106 skipped = 0;
107 first_index = 0;
108 do {
109 int error = 0;
110 xfs_inode_t *ip;
112 if (exclusive)
113 write_lock(&pag->pag_ici_lock);
114 else
115 read_lock(&pag->pag_ici_lock);
116 ip = xfs_inode_ag_lookup(mp, pag, &first_index, tag);
117 if (!ip) {
118 if (exclusive)
119 write_unlock(&pag->pag_ici_lock);
120 else
121 read_unlock(&pag->pag_ici_lock);
122 break;
125 /* execute releases pag->pag_ici_lock */
126 error = execute(ip, pag, flags);
127 if (error == EAGAIN) {
128 skipped++;
129 continue;
131 if (error)
132 last_error = error;
134 /* bail out if the filesystem is corrupted. */
135 if (error == EFSCORRUPTED)
136 break;
138 } while ((*nr_to_scan)--);
140 if (skipped) {
141 delay(1);
142 goto restart;
144 return last_error;
148 * Select the next per-ag structure to iterate during the walk. The reclaim
149 * walk is optimised only to walk AGs with reclaimable inodes in them.
151 static struct xfs_perag *
152 xfs_inode_ag_iter_next_pag(
153 struct xfs_mount *mp,
154 xfs_agnumber_t *first,
155 int tag)
157 struct xfs_perag *pag = NULL;
159 if (tag == XFS_ICI_RECLAIM_TAG) {
160 int found;
161 int ref;
163 spin_lock(&mp->m_perag_lock);
164 found = radix_tree_gang_lookup_tag(&mp->m_perag_tree,
165 (void **)&pag, *first, 1, tag);
166 if (found <= 0) {
167 spin_unlock(&mp->m_perag_lock);
168 return NULL;
170 *first = pag->pag_agno + 1;
171 /* open coded pag reference increment */
172 ref = atomic_inc_return(&pag->pag_ref);
173 spin_unlock(&mp->m_perag_lock);
174 trace_xfs_perag_get_reclaim(mp, pag->pag_agno, ref, _RET_IP_);
175 } else {
176 pag = xfs_perag_get(mp, *first);
177 (*first)++;
179 return pag;
183 xfs_inode_ag_iterator(
184 struct xfs_mount *mp,
185 int (*execute)(struct xfs_inode *ip,
186 struct xfs_perag *pag, int flags),
187 int flags,
188 int tag,
189 int exclusive,
190 int *nr_to_scan)
192 struct xfs_perag *pag;
193 int error = 0;
194 int last_error = 0;
195 xfs_agnumber_t ag;
196 int nr;
198 nr = nr_to_scan ? *nr_to_scan : INT_MAX;
199 ag = 0;
200 while ((pag = xfs_inode_ag_iter_next_pag(mp, &ag, tag))) {
201 error = xfs_inode_ag_walk(mp, pag, execute, flags, tag,
202 exclusive, &nr);
203 xfs_perag_put(pag);
204 if (error) {
205 last_error = error;
206 if (error == EFSCORRUPTED)
207 break;
209 if (nr <= 0)
210 break;
212 if (nr_to_scan)
213 *nr_to_scan = nr;
214 return XFS_ERROR(last_error);
217 /* must be called with pag_ici_lock held and releases it */
219 xfs_sync_inode_valid(
220 struct xfs_inode *ip,
221 struct xfs_perag *pag)
223 struct inode *inode = VFS_I(ip);
224 int error = EFSCORRUPTED;
226 /* nothing to sync during shutdown */
227 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
228 goto out_unlock;
230 /* avoid new or reclaimable inodes. Leave for reclaim code to flush */
231 error = ENOENT;
232 if (xfs_iflags_test(ip, XFS_INEW | XFS_IRECLAIMABLE | XFS_IRECLAIM))
233 goto out_unlock;
235 /* If we can't grab the inode, it must on it's way to reclaim. */
236 if (!igrab(inode))
237 goto out_unlock;
239 if (is_bad_inode(inode)) {
240 IRELE(ip);
241 goto out_unlock;
244 /* inode is valid */
245 error = 0;
246 out_unlock:
247 read_unlock(&pag->pag_ici_lock);
248 return error;
251 STATIC int
252 xfs_sync_inode_data(
253 struct xfs_inode *ip,
254 struct xfs_perag *pag,
255 int flags)
257 struct inode *inode = VFS_I(ip);
258 struct address_space *mapping = inode->i_mapping;
259 int error = 0;
261 error = xfs_sync_inode_valid(ip, pag);
262 if (error)
263 return error;
265 if (!mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
266 goto out_wait;
268 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED)) {
269 if (flags & SYNC_TRYLOCK)
270 goto out_wait;
271 xfs_ilock(ip, XFS_IOLOCK_SHARED);
274 error = xfs_flush_pages(ip, 0, -1, (flags & SYNC_WAIT) ?
275 0 : XBF_ASYNC, FI_NONE);
276 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
278 out_wait:
279 if (flags & SYNC_WAIT)
280 xfs_ioend_wait(ip);
281 IRELE(ip);
282 return error;
285 STATIC int
286 xfs_sync_inode_attr(
287 struct xfs_inode *ip,
288 struct xfs_perag *pag,
289 int flags)
291 int error = 0;
293 error = xfs_sync_inode_valid(ip, pag);
294 if (error)
295 return error;
297 xfs_ilock(ip, XFS_ILOCK_SHARED);
298 if (xfs_inode_clean(ip))
299 goto out_unlock;
300 if (!xfs_iflock_nowait(ip)) {
301 if (!(flags & SYNC_WAIT))
302 goto out_unlock;
303 xfs_iflock(ip);
306 if (xfs_inode_clean(ip)) {
307 xfs_ifunlock(ip);
308 goto out_unlock;
311 error = xfs_iflush(ip, flags);
313 out_unlock:
314 xfs_iunlock(ip, XFS_ILOCK_SHARED);
315 IRELE(ip);
316 return error;
320 * Write out pagecache data for the whole filesystem.
323 xfs_sync_data(
324 struct xfs_mount *mp,
325 int flags)
327 int error;
329 ASSERT((flags & ~(SYNC_TRYLOCK|SYNC_WAIT)) == 0);
331 error = xfs_inode_ag_iterator(mp, xfs_sync_inode_data, flags,
332 XFS_ICI_NO_TAG, 0, NULL);
333 if (error)
334 return XFS_ERROR(error);
336 xfs_log_force(mp, (flags & SYNC_WAIT) ? XFS_LOG_SYNC : 0);
337 return 0;
341 * Write out inode metadata (attributes) for the whole filesystem.
344 xfs_sync_attr(
345 struct xfs_mount *mp,
346 int flags)
348 ASSERT((flags & ~SYNC_WAIT) == 0);
350 return xfs_inode_ag_iterator(mp, xfs_sync_inode_attr, flags,
351 XFS_ICI_NO_TAG, 0, NULL);
354 STATIC int
355 xfs_commit_dummy_trans(
356 struct xfs_mount *mp,
357 uint flags)
359 struct xfs_inode *ip = mp->m_rootip;
360 struct xfs_trans *tp;
361 int error;
364 * Put a dummy transaction in the log to tell recovery
365 * that all others are OK.
367 tp = xfs_trans_alloc(mp, XFS_TRANS_DUMMY1);
368 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
369 if (error) {
370 xfs_trans_cancel(tp, 0);
371 return error;
374 xfs_ilock(ip, XFS_ILOCK_EXCL);
376 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
377 xfs_trans_ihold(tp, ip);
378 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
379 error = xfs_trans_commit(tp, 0);
380 xfs_iunlock(ip, XFS_ILOCK_EXCL);
382 /* the log force ensures this transaction is pushed to disk */
383 xfs_log_force(mp, (flags & SYNC_WAIT) ? XFS_LOG_SYNC : 0);
384 return error;
387 STATIC int
388 xfs_sync_fsdata(
389 struct xfs_mount *mp)
391 struct xfs_buf *bp;
394 * If the buffer is pinned then push on the log so we won't get stuck
395 * waiting in the write for someone, maybe ourselves, to flush the log.
397 * Even though we just pushed the log above, we did not have the
398 * superblock buffer locked at that point so it can become pinned in
399 * between there and here.
401 bp = xfs_getsb(mp, 0);
402 if (XFS_BUF_ISPINNED(bp))
403 xfs_log_force(mp, 0);
405 return xfs_bwrite(mp, bp);
409 * When remounting a filesystem read-only or freezing the filesystem, we have
410 * two phases to execute. This first phase is syncing the data before we
411 * quiesce the filesystem, and the second is flushing all the inodes out after
412 * we've waited for all the transactions created by the first phase to
413 * complete. The second phase ensures that the inodes are written to their
414 * location on disk rather than just existing in transactions in the log. This
415 * means after a quiesce there is no log replay required to write the inodes to
416 * disk (this is the main difference between a sync and a quiesce).
419 * First stage of freeze - no writers will make progress now we are here,
420 * so we flush delwri and delalloc buffers here, then wait for all I/O to
421 * complete. Data is frozen at that point. Metadata is not frozen,
422 * transactions can still occur here so don't bother flushing the buftarg
423 * because it'll just get dirty again.
426 xfs_quiesce_data(
427 struct xfs_mount *mp)
429 int error, error2 = 0;
431 /* push non-blocking */
432 xfs_sync_data(mp, 0);
433 xfs_qm_sync(mp, SYNC_TRYLOCK);
435 /* push and block till complete */
436 xfs_sync_data(mp, SYNC_WAIT);
437 xfs_qm_sync(mp, SYNC_WAIT);
439 /* write superblock and hoover up shutdown errors */
440 error = xfs_sync_fsdata(mp);
442 /* make sure all delwri buffers are written out */
443 xfs_flush_buftarg(mp->m_ddev_targp, 1);
445 /* mark the log as covered if needed */
446 if (xfs_log_need_covered(mp))
447 error2 = xfs_commit_dummy_trans(mp, SYNC_WAIT);
449 /* flush data-only devices */
450 if (mp->m_rtdev_targp)
451 XFS_bflush(mp->m_rtdev_targp);
453 return error ? error : error2;
456 STATIC void
457 xfs_quiesce_fs(
458 struct xfs_mount *mp)
460 int count = 0, pincount;
462 xfs_reclaim_inodes(mp, 0);
463 xfs_flush_buftarg(mp->m_ddev_targp, 0);
466 * This loop must run at least twice. The first instance of the loop
467 * will flush most meta data but that will generate more meta data
468 * (typically directory updates). Which then must be flushed and
469 * logged before we can write the unmount record. We also so sync
470 * reclaim of inodes to catch any that the above delwri flush skipped.
472 do {
473 xfs_reclaim_inodes(mp, SYNC_WAIT);
474 xfs_sync_attr(mp, SYNC_WAIT);
475 pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1);
476 if (!pincount) {
477 delay(50);
478 count++;
480 } while (count < 2);
484 * Second stage of a quiesce. The data is already synced, now we have to take
485 * care of the metadata. New transactions are already blocked, so we need to
486 * wait for any remaining transactions to drain out before proceding.
488 void
489 xfs_quiesce_attr(
490 struct xfs_mount *mp)
492 int error = 0;
494 /* wait for all modifications to complete */
495 while (atomic_read(&mp->m_active_trans) > 0)
496 delay(100);
498 /* flush inodes and push all remaining buffers out to disk */
499 xfs_quiesce_fs(mp);
502 * Just warn here till VFS can correctly support
503 * read-only remount without racing.
505 WARN_ON(atomic_read(&mp->m_active_trans) != 0);
507 /* Push the superblock and write an unmount record */
508 error = xfs_log_sbcount(mp, 1);
509 if (error)
510 xfs_fs_cmn_err(CE_WARN, mp,
511 "xfs_attr_quiesce: failed to log sb changes. "
512 "Frozen image may not be consistent.");
513 xfs_log_unmount_write(mp);
514 xfs_unmountfs_writesb(mp);
518 * Enqueue a work item to be picked up by the vfs xfssyncd thread.
519 * Doing this has two advantages:
520 * - It saves on stack space, which is tight in certain situations
521 * - It can be used (with care) as a mechanism to avoid deadlocks.
522 * Flushing while allocating in a full filesystem requires both.
524 STATIC void
525 xfs_syncd_queue_work(
526 struct xfs_mount *mp,
527 void *data,
528 void (*syncer)(struct xfs_mount *, void *),
529 struct completion *completion)
531 struct xfs_sync_work *work;
533 work = kmem_alloc(sizeof(struct xfs_sync_work), KM_SLEEP);
534 INIT_LIST_HEAD(&work->w_list);
535 work->w_syncer = syncer;
536 work->w_data = data;
537 work->w_mount = mp;
538 work->w_completion = completion;
539 spin_lock(&mp->m_sync_lock);
540 list_add_tail(&work->w_list, &mp->m_sync_list);
541 spin_unlock(&mp->m_sync_lock);
542 wake_up_process(mp->m_sync_task);
546 * Flush delayed allocate data, attempting to free up reserved space
547 * from existing allocations. At this point a new allocation attempt
548 * has failed with ENOSPC and we are in the process of scratching our
549 * heads, looking about for more room...
551 STATIC void
552 xfs_flush_inodes_work(
553 struct xfs_mount *mp,
554 void *arg)
556 struct inode *inode = arg;
557 xfs_sync_data(mp, SYNC_TRYLOCK);
558 xfs_sync_data(mp, SYNC_TRYLOCK | SYNC_WAIT);
559 iput(inode);
562 void
563 xfs_flush_inodes(
564 xfs_inode_t *ip)
566 struct inode *inode = VFS_I(ip);
567 DECLARE_COMPLETION_ONSTACK(completion);
569 igrab(inode);
570 xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_inodes_work, &completion);
571 wait_for_completion(&completion);
572 xfs_log_force(ip->i_mount, XFS_LOG_SYNC);
576 * Every sync period we need to unpin all items, reclaim inodes and sync
577 * disk quotas. We might need to cover the log to indicate that the
578 * filesystem is idle.
580 STATIC void
581 xfs_sync_worker(
582 struct xfs_mount *mp,
583 void *unused)
585 int error;
587 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
588 xfs_log_force(mp, 0);
589 xfs_reclaim_inodes(mp, 0);
590 /* dgc: errors ignored here */
591 error = xfs_qm_sync(mp, SYNC_TRYLOCK);
592 if (xfs_log_need_covered(mp))
593 error = xfs_commit_dummy_trans(mp, 0);
595 mp->m_sync_seq++;
596 wake_up(&mp->m_wait_single_sync_task);
599 STATIC int
600 xfssyncd(
601 void *arg)
603 struct xfs_mount *mp = arg;
604 long timeleft;
605 xfs_sync_work_t *work, *n;
606 LIST_HEAD (tmp);
608 set_freezable();
609 timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10);
610 for (;;) {
611 if (list_empty(&mp->m_sync_list))
612 timeleft = schedule_timeout_interruptible(timeleft);
613 /* swsusp */
614 try_to_freeze();
615 if (kthread_should_stop() && list_empty(&mp->m_sync_list))
616 break;
618 spin_lock(&mp->m_sync_lock);
620 * We can get woken by laptop mode, to do a sync -
621 * that's the (only!) case where the list would be
622 * empty with time remaining.
624 if (!timeleft || list_empty(&mp->m_sync_list)) {
625 if (!timeleft)
626 timeleft = xfs_syncd_centisecs *
627 msecs_to_jiffies(10);
628 INIT_LIST_HEAD(&mp->m_sync_work.w_list);
629 list_add_tail(&mp->m_sync_work.w_list,
630 &mp->m_sync_list);
632 list_splice_init(&mp->m_sync_list, &tmp);
633 spin_unlock(&mp->m_sync_lock);
635 list_for_each_entry_safe(work, n, &tmp, w_list) {
636 (*work->w_syncer)(mp, work->w_data);
637 list_del(&work->w_list);
638 if (work == &mp->m_sync_work)
639 continue;
640 if (work->w_completion)
641 complete(work->w_completion);
642 kmem_free(work);
646 return 0;
650 xfs_syncd_init(
651 struct xfs_mount *mp)
653 mp->m_sync_work.w_syncer = xfs_sync_worker;
654 mp->m_sync_work.w_mount = mp;
655 mp->m_sync_work.w_completion = NULL;
656 mp->m_sync_task = kthread_run(xfssyncd, mp, "xfssyncd/%s", mp->m_fsname);
657 if (IS_ERR(mp->m_sync_task))
658 return -PTR_ERR(mp->m_sync_task);
659 return 0;
662 void
663 xfs_syncd_stop(
664 struct xfs_mount *mp)
666 kthread_stop(mp->m_sync_task);
669 void
670 __xfs_inode_set_reclaim_tag(
671 struct xfs_perag *pag,
672 struct xfs_inode *ip)
674 radix_tree_tag_set(&pag->pag_ici_root,
675 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
676 XFS_ICI_RECLAIM_TAG);
678 if (!pag->pag_ici_reclaimable) {
679 /* propagate the reclaim tag up into the perag radix tree */
680 spin_lock(&ip->i_mount->m_perag_lock);
681 radix_tree_tag_set(&ip->i_mount->m_perag_tree,
682 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
683 XFS_ICI_RECLAIM_TAG);
684 spin_unlock(&ip->i_mount->m_perag_lock);
685 trace_xfs_perag_set_reclaim(ip->i_mount, pag->pag_agno,
686 -1, _RET_IP_);
688 pag->pag_ici_reclaimable++;
692 * We set the inode flag atomically with the radix tree tag.
693 * Once we get tag lookups on the radix tree, this inode flag
694 * can go away.
696 void
697 xfs_inode_set_reclaim_tag(
698 xfs_inode_t *ip)
700 struct xfs_mount *mp = ip->i_mount;
701 struct xfs_perag *pag;
703 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
704 write_lock(&pag->pag_ici_lock);
705 spin_lock(&ip->i_flags_lock);
706 __xfs_inode_set_reclaim_tag(pag, ip);
707 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
708 spin_unlock(&ip->i_flags_lock);
709 write_unlock(&pag->pag_ici_lock);
710 xfs_perag_put(pag);
713 void
714 __xfs_inode_clear_reclaim(
715 xfs_perag_t *pag,
716 xfs_inode_t *ip)
718 pag->pag_ici_reclaimable--;
719 if (!pag->pag_ici_reclaimable) {
720 /* clear the reclaim tag from the perag radix tree */
721 spin_lock(&ip->i_mount->m_perag_lock);
722 radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
723 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
724 XFS_ICI_RECLAIM_TAG);
725 spin_unlock(&ip->i_mount->m_perag_lock);
726 trace_xfs_perag_clear_reclaim(ip->i_mount, pag->pag_agno,
727 -1, _RET_IP_);
731 void
732 __xfs_inode_clear_reclaim_tag(
733 xfs_mount_t *mp,
734 xfs_perag_t *pag,
735 xfs_inode_t *ip)
737 radix_tree_tag_clear(&pag->pag_ici_root,
738 XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
739 __xfs_inode_clear_reclaim(pag, ip);
743 * Inodes in different states need to be treated differently, and the return
744 * value of xfs_iflush is not sufficient to get this right. The following table
745 * lists the inode states and the reclaim actions necessary for non-blocking
746 * reclaim:
749 * inode state iflush ret required action
750 * --------------- ---------- ---------------
751 * bad - reclaim
752 * shutdown EIO unpin and reclaim
753 * clean, unpinned 0 reclaim
754 * stale, unpinned 0 reclaim
755 * clean, pinned(*) 0 requeue
756 * stale, pinned EAGAIN requeue
757 * dirty, delwri ok 0 requeue
758 * dirty, delwri blocked EAGAIN requeue
759 * dirty, sync flush 0 reclaim
761 * (*) dgc: I don't think the clean, pinned state is possible but it gets
762 * handled anyway given the order of checks implemented.
764 * As can be seen from the table, the return value of xfs_iflush() is not
765 * sufficient to correctly decide the reclaim action here. The checks in
766 * xfs_iflush() might look like duplicates, but they are not.
768 * Also, because we get the flush lock first, we know that any inode that has
769 * been flushed delwri has had the flush completed by the time we check that
770 * the inode is clean. The clean inode check needs to be done before flushing
771 * the inode delwri otherwise we would loop forever requeuing clean inodes as
772 * we cannot tell apart a successful delwri flush and a clean inode from the
773 * return value of xfs_iflush().
775 * Note that because the inode is flushed delayed write by background
776 * writeback, the flush lock may already be held here and waiting on it can
777 * result in very long latencies. Hence for sync reclaims, where we wait on the
778 * flush lock, the caller should push out delayed write inodes first before
779 * trying to reclaim them to minimise the amount of time spent waiting. For
780 * background relaim, we just requeue the inode for the next pass.
782 * Hence the order of actions after gaining the locks should be:
783 * bad => reclaim
784 * shutdown => unpin and reclaim
785 * pinned, delwri => requeue
786 * pinned, sync => unpin
787 * stale => reclaim
788 * clean => reclaim
789 * dirty, delwri => flush and requeue
790 * dirty, sync => flush, wait and reclaim
792 STATIC int
793 xfs_reclaim_inode(
794 struct xfs_inode *ip,
795 struct xfs_perag *pag,
796 int sync_mode)
798 int error = 0;
801 * The radix tree lock here protects a thread in xfs_iget from racing
802 * with us starting reclaim on the inode. Once we have the
803 * XFS_IRECLAIM flag set it will not touch us.
805 spin_lock(&ip->i_flags_lock);
806 ASSERT_ALWAYS(__xfs_iflags_test(ip, XFS_IRECLAIMABLE));
807 if (__xfs_iflags_test(ip, XFS_IRECLAIM)) {
808 /* ignore as it is already under reclaim */
809 spin_unlock(&ip->i_flags_lock);
810 write_unlock(&pag->pag_ici_lock);
811 return 0;
813 __xfs_iflags_set(ip, XFS_IRECLAIM);
814 spin_unlock(&ip->i_flags_lock);
815 write_unlock(&pag->pag_ici_lock);
817 xfs_ilock(ip, XFS_ILOCK_EXCL);
818 if (!xfs_iflock_nowait(ip)) {
819 if (!(sync_mode & SYNC_WAIT))
820 goto out;
821 xfs_iflock(ip);
824 if (is_bad_inode(VFS_I(ip)))
825 goto reclaim;
826 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
827 xfs_iunpin_wait(ip);
828 goto reclaim;
830 if (xfs_ipincount(ip)) {
831 if (!(sync_mode & SYNC_WAIT)) {
832 xfs_ifunlock(ip);
833 goto out;
835 xfs_iunpin_wait(ip);
837 if (xfs_iflags_test(ip, XFS_ISTALE))
838 goto reclaim;
839 if (xfs_inode_clean(ip))
840 goto reclaim;
842 /* Now we have an inode that needs flushing */
843 error = xfs_iflush(ip, sync_mode);
844 if (sync_mode & SYNC_WAIT) {
845 xfs_iflock(ip);
846 goto reclaim;
850 * When we have to flush an inode but don't have SYNC_WAIT set, we
851 * flush the inode out using a delwri buffer and wait for the next
852 * call into reclaim to find it in a clean state instead of waiting for
853 * it now. We also don't return errors here - if the error is transient
854 * then the next reclaim pass will flush the inode, and if the error
855 * is permanent then the next sync reclaim will reclaim the inode and
856 * pass on the error.
858 if (error && error != EAGAIN && !XFS_FORCED_SHUTDOWN(ip->i_mount)) {
859 xfs_fs_cmn_err(CE_WARN, ip->i_mount,
860 "inode 0x%llx background reclaim flush failed with %d",
861 (long long)ip->i_ino, error);
863 out:
864 xfs_iflags_clear(ip, XFS_IRECLAIM);
865 xfs_iunlock(ip, XFS_ILOCK_EXCL);
867 * We could return EAGAIN here to make reclaim rescan the inode tree in
868 * a short while. However, this just burns CPU time scanning the tree
869 * waiting for IO to complete and xfssyncd never goes back to the idle
870 * state. Instead, return 0 to let the next scheduled background reclaim
871 * attempt to reclaim the inode again.
873 return 0;
875 reclaim:
876 xfs_ifunlock(ip);
877 xfs_iunlock(ip, XFS_ILOCK_EXCL);
878 xfs_ireclaim(ip);
879 return error;
884 xfs_reclaim_inodes(
885 xfs_mount_t *mp,
886 int mode)
888 return xfs_inode_ag_iterator(mp, xfs_reclaim_inode, mode,
889 XFS_ICI_RECLAIM_TAG, 1, NULL);
893 * Shrinker infrastructure.
895 static int
896 xfs_reclaim_inode_shrink(
897 struct shrinker *shrink,
898 int nr_to_scan,
899 gfp_t gfp_mask)
901 struct xfs_mount *mp;
902 struct xfs_perag *pag;
903 xfs_agnumber_t ag;
904 int reclaimable;
906 mp = container_of(shrink, struct xfs_mount, m_inode_shrink);
907 if (nr_to_scan) {
908 if (!(gfp_mask & __GFP_FS))
909 return -1;
911 xfs_inode_ag_iterator(mp, xfs_reclaim_inode, 0,
912 XFS_ICI_RECLAIM_TAG, 1, &nr_to_scan);
913 /* if we don't exhaust the scan, don't bother coming back */
914 if (nr_to_scan > 0)
915 return -1;
918 reclaimable = 0;
919 ag = 0;
920 while ((pag = xfs_inode_ag_iter_next_pag(mp, &ag,
921 XFS_ICI_RECLAIM_TAG))) {
922 reclaimable += pag->pag_ici_reclaimable;
923 xfs_perag_put(pag);
925 return reclaimable;
928 void
929 xfs_inode_shrinker_register(
930 struct xfs_mount *mp)
932 mp->m_inode_shrink.shrink = xfs_reclaim_inode_shrink;
933 mp->m_inode_shrink.seeks = DEFAULT_SEEKS;
934 register_shrinker(&mp->m_inode_shrink);
937 void
938 xfs_inode_shrinker_unregister(
939 struct xfs_mount *mp)
941 unregister_shrinker(&mp->m_inode_shrink);