initial commit with v2.6.32.60
[linux-2.6.32.60-moxart.git] / fs / xfs / linux-2.6 / xfs_sync.c
blob71c14dde0690ee34aaeaceefe804b67a42ba0b62
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"
48 #include <linux/kthread.h>
49 #include <linux/freezer.h>
52 STATIC xfs_inode_t *
53 xfs_inode_ag_lookup(
54 struct xfs_mount *mp,
55 struct xfs_perag *pag,
56 uint32_t *first_index,
57 int tag)
59 int nr_found;
60 struct xfs_inode *ip;
63 * use a gang lookup to find the next inode in the tree
64 * as the tree is sparse and a gang lookup walks to find
65 * the number of objects requested.
67 if (tag == XFS_ICI_NO_TAG) {
68 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
69 (void **)&ip, *first_index, 1);
70 } else {
71 nr_found = radix_tree_gang_lookup_tag(&pag->pag_ici_root,
72 (void **)&ip, *first_index, 1, tag);
74 if (!nr_found)
75 return NULL;
78 * Update the index for the next lookup. Catch overflows
79 * into the next AG range which can occur if we have inodes
80 * in the last block of the AG and we are currently
81 * pointing to the last inode.
83 *first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
84 if (*first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
85 return NULL;
86 return ip;
89 STATIC int
90 xfs_inode_ag_walk(
91 struct xfs_mount *mp,
92 xfs_agnumber_t ag,
93 int (*execute)(struct xfs_inode *ip,
94 struct xfs_perag *pag, int flags),
95 int flags,
96 int tag,
97 int exclusive,
98 int *nr_to_scan)
100 struct xfs_perag *pag = &mp->m_perag[ag];
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;
145 xfs_put_perag(mp, pag);
146 return last_error;
150 xfs_inode_ag_iterator(
151 struct xfs_mount *mp,
152 int (*execute)(struct xfs_inode *ip,
153 struct xfs_perag *pag, int flags),
154 int flags,
155 int tag,
156 int exclusive,
157 int *nr_to_scan)
159 int error = 0;
160 int last_error = 0;
161 xfs_agnumber_t ag;
162 int nr;
164 nr = nr_to_scan ? *nr_to_scan : INT_MAX;
165 for (ag = 0; ag < mp->m_sb.sb_agcount; ag++) {
166 if (!mp->m_perag[ag].pag_ici_init)
167 continue;
168 error = xfs_inode_ag_walk(mp, ag, execute, flags, tag,
169 exclusive, &nr);
170 if (error) {
171 last_error = error;
172 if (error == EFSCORRUPTED)
173 break;
175 if (nr <= 0)
176 break;
178 if (nr_to_scan)
179 *nr_to_scan = nr;
180 return XFS_ERROR(last_error);
183 /* must be called with pag_ici_lock held and releases it */
185 xfs_sync_inode_valid(
186 struct xfs_inode *ip,
187 struct xfs_perag *pag)
189 struct inode *inode = VFS_I(ip);
190 int error = EFSCORRUPTED;
192 /* nothing to sync during shutdown */
193 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
194 goto out_unlock;
196 /* avoid new or reclaimable inodes. Leave for reclaim code to flush */
197 error = ENOENT;
198 if (xfs_iflags_test(ip, XFS_INEW | XFS_IRECLAIMABLE | XFS_IRECLAIM))
199 goto out_unlock;
201 /* If we can't grab the inode, it must on it's way to reclaim. */
202 if (!igrab(inode))
203 goto out_unlock;
205 if (is_bad_inode(inode)) {
206 IRELE(ip);
207 goto out_unlock;
210 /* inode is valid */
211 error = 0;
212 out_unlock:
213 read_unlock(&pag->pag_ici_lock);
214 return error;
217 STATIC int
218 xfs_sync_inode_data(
219 struct xfs_inode *ip,
220 struct xfs_perag *pag,
221 int flags)
223 struct inode *inode = VFS_I(ip);
224 struct address_space *mapping = inode->i_mapping;
225 int error = 0;
227 error = xfs_sync_inode_valid(ip, pag);
228 if (error)
229 return error;
231 if (!mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
232 goto out_wait;
234 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED)) {
235 if (flags & SYNC_TRYLOCK)
236 goto out_wait;
237 xfs_ilock(ip, XFS_IOLOCK_SHARED);
240 error = xfs_flush_pages(ip, 0, -1, (flags & SYNC_WAIT) ?
241 0 : XFS_B_ASYNC, FI_NONE);
242 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
244 out_wait:
245 if (flags & SYNC_WAIT)
246 xfs_ioend_wait(ip);
247 IRELE(ip);
248 return error;
251 STATIC int
252 xfs_sync_inode_attr(
253 struct xfs_inode *ip,
254 struct xfs_perag *pag,
255 int flags)
257 int error = 0;
259 error = xfs_sync_inode_valid(ip, pag);
260 if (error)
261 return error;
263 xfs_ilock(ip, XFS_ILOCK_SHARED);
264 if (xfs_inode_clean(ip))
265 goto out_unlock;
266 if (!xfs_iflock_nowait(ip)) {
267 if (!(flags & SYNC_WAIT))
268 goto out_unlock;
269 xfs_iflock(ip);
272 if (xfs_inode_clean(ip)) {
273 xfs_ifunlock(ip);
274 goto out_unlock;
277 error = xfs_iflush(ip, (flags & SYNC_WAIT) ?
278 XFS_IFLUSH_SYNC : XFS_IFLUSH_DELWRI);
280 out_unlock:
281 xfs_iunlock(ip, XFS_ILOCK_SHARED);
282 IRELE(ip);
283 return error;
287 * Write out pagecache data for the whole filesystem.
290 xfs_sync_data(
291 struct xfs_mount *mp,
292 int flags)
294 int error;
296 ASSERT((flags & ~(SYNC_TRYLOCK|SYNC_WAIT)) == 0);
298 error = xfs_inode_ag_iterator(mp, xfs_sync_inode_data, flags,
299 XFS_ICI_NO_TAG, 0, NULL);
300 if (error)
301 return XFS_ERROR(error);
303 xfs_log_force(mp, 0,
304 (flags & SYNC_WAIT) ?
305 XFS_LOG_FORCE | XFS_LOG_SYNC :
306 XFS_LOG_FORCE);
307 return 0;
311 * Write out inode metadata (attributes) for the whole filesystem.
314 xfs_sync_attr(
315 struct xfs_mount *mp,
316 int flags)
318 ASSERT((flags & ~SYNC_WAIT) == 0);
320 return xfs_inode_ag_iterator(mp, xfs_sync_inode_attr, flags,
321 XFS_ICI_NO_TAG, 0, NULL);
324 STATIC int
325 xfs_commit_dummy_trans(
326 struct xfs_mount *mp,
327 uint flags)
329 struct xfs_inode *ip = mp->m_rootip;
330 struct xfs_trans *tp;
331 int error;
332 int log_flags = XFS_LOG_FORCE;
334 if (flags & SYNC_WAIT)
335 log_flags |= XFS_LOG_SYNC;
338 * Put a dummy transaction in the log to tell recovery
339 * that all others are OK.
341 tp = xfs_trans_alloc(mp, XFS_TRANS_DUMMY1);
342 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
343 if (error) {
344 xfs_trans_cancel(tp, 0);
345 return error;
348 xfs_ilock(ip, XFS_ILOCK_EXCL);
350 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
351 xfs_trans_ihold(tp, ip);
352 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
353 error = xfs_trans_commit(tp, 0);
354 xfs_iunlock(ip, XFS_ILOCK_EXCL);
356 /* the log force ensures this transaction is pushed to disk */
357 xfs_log_force(mp, 0, log_flags);
358 return error;
362 xfs_sync_fsdata(
363 struct xfs_mount *mp,
364 int flags)
366 struct xfs_buf *bp;
367 struct xfs_buf_log_item *bip;
368 int error = 0;
371 * If this is xfssyncd() then only sync the superblock if we can
372 * lock it without sleeping and it is not pinned.
374 if (flags & SYNC_TRYLOCK) {
375 ASSERT(!(flags & SYNC_WAIT));
377 bp = xfs_getsb(mp, XFS_BUF_TRYLOCK);
378 if (!bp)
379 goto out;
381 bip = XFS_BUF_FSPRIVATE(bp, struct xfs_buf_log_item *);
382 if (!bip || !xfs_buf_item_dirty(bip) || XFS_BUF_ISPINNED(bp))
383 goto out_brelse;
384 } else {
385 bp = xfs_getsb(mp, 0);
388 * If the buffer is pinned then push on the log so we won't
389 * get stuck waiting in the write for someone, maybe
390 * ourselves, to flush the log.
392 * Even though we just pushed the log above, we did not have
393 * the superblock buffer locked at that point so it can
394 * become pinned in between there and here.
396 if (XFS_BUF_ISPINNED(bp))
397 xfs_log_force(mp, 0, XFS_LOG_FORCE);
401 if (flags & SYNC_WAIT)
402 XFS_BUF_UNASYNC(bp);
403 else
404 XFS_BUF_ASYNC(bp);
406 error = xfs_bwrite(mp, bp);
407 if (error)
408 return error;
411 * If this is a data integrity sync make sure all pending buffers
412 * are flushed out for the log coverage check below.
414 if (flags & SYNC_WAIT)
415 xfs_flush_buftarg(mp->m_ddev_targp, 1);
417 if (xfs_log_need_covered(mp))
418 error = xfs_commit_dummy_trans(mp, flags);
419 return error;
421 out_brelse:
422 xfs_buf_relse(bp);
423 out:
424 return error;
428 * When remounting a filesystem read-only or freezing the filesystem, we have
429 * two phases to execute. This first phase is syncing the data before we
430 * quiesce the filesystem, and the second is flushing all the inodes out after
431 * we've waited for all the transactions created by the first phase to
432 * complete. The second phase ensures that the inodes are written to their
433 * location on disk rather than just existing in transactions in the log. This
434 * means after a quiesce there is no log replay required to write the inodes to
435 * disk (this is the main difference between a sync and a quiesce).
438 * First stage of freeze - no writers will make progress now we are here,
439 * so we flush delwri and delalloc buffers here, then wait for all I/O to
440 * complete. Data is frozen at that point. Metadata is not frozen,
441 * transactions can still occur here so don't bother flushing the buftarg
442 * because it'll just get dirty again.
445 xfs_quiesce_data(
446 struct xfs_mount *mp)
448 int error;
450 /* push non-blocking */
451 xfs_sync_data(mp, 0);
452 xfs_qm_sync(mp, SYNC_TRYLOCK);
454 /* push and block till complete */
455 xfs_sync_data(mp, SYNC_WAIT);
456 xfs_qm_sync(mp, SYNC_WAIT);
458 /* drop inode references pinned by filestreams */
459 xfs_filestream_flush(mp);
461 /* write superblock and hoover up shutdown errors */
462 error = xfs_sync_fsdata(mp, SYNC_WAIT);
464 /* flush data-only devices */
465 if (mp->m_rtdev_targp)
466 XFS_bflush(mp->m_rtdev_targp);
468 return error;
471 STATIC void
472 xfs_quiesce_fs(
473 struct xfs_mount *mp)
475 int count = 0, pincount;
477 xfs_flush_buftarg(mp->m_ddev_targp, 0);
478 xfs_reclaim_inodes(mp, XFS_IFLUSH_DELWRI_ELSE_ASYNC);
481 * This loop must run at least twice. The first instance of the loop
482 * will flush most meta data but that will generate more meta data
483 * (typically directory updates). Which then must be flushed and
484 * logged before we can write the unmount record.
486 do {
487 xfs_sync_attr(mp, SYNC_WAIT);
488 pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1);
489 if (!pincount) {
490 delay(50);
491 count++;
493 } while (count < 2);
497 * Second stage of a quiesce. The data is already synced, now we have to take
498 * care of the metadata. New transactions are already blocked, so we need to
499 * wait for any remaining transactions to drain out before proceding.
501 void
502 xfs_quiesce_attr(
503 struct xfs_mount *mp)
505 int error = 0;
507 /* wait for all modifications to complete */
508 while (atomic_read(&mp->m_active_trans) > 0)
509 delay(100);
511 /* flush inodes and push all remaining buffers out to disk */
512 xfs_quiesce_fs(mp);
515 * Just warn here till VFS can correctly support
516 * read-only remount without racing.
518 WARN_ON(atomic_read(&mp->m_active_trans) != 0);
520 /* Push the superblock and write an unmount record */
521 error = xfs_log_sbcount(mp, 1);
522 if (error)
523 xfs_fs_cmn_err(CE_WARN, mp,
524 "xfs_attr_quiesce: failed to log sb changes. "
525 "Frozen image may not be consistent.");
526 xfs_log_unmount_write(mp);
527 xfs_unmountfs_writesb(mp);
531 * Enqueue a work item to be picked up by the vfs xfssyncd thread.
532 * Doing this has two advantages:
533 * - It saves on stack space, which is tight in certain situations
534 * - It can be used (with care) as a mechanism to avoid deadlocks.
535 * Flushing while allocating in a full filesystem requires both.
537 STATIC void
538 xfs_syncd_queue_work(
539 struct xfs_mount *mp,
540 void *data,
541 void (*syncer)(struct xfs_mount *, void *),
542 struct completion *completion)
544 struct xfs_sync_work *work;
546 work = kmem_alloc(sizeof(struct xfs_sync_work), KM_SLEEP);
547 INIT_LIST_HEAD(&work->w_list);
548 work->w_syncer = syncer;
549 work->w_data = data;
550 work->w_mount = mp;
551 work->w_completion = completion;
552 spin_lock(&mp->m_sync_lock);
553 list_add_tail(&work->w_list, &mp->m_sync_list);
554 spin_unlock(&mp->m_sync_lock);
555 wake_up_process(mp->m_sync_task);
559 * Flush delayed allocate data, attempting to free up reserved space
560 * from existing allocations. At this point a new allocation attempt
561 * has failed with ENOSPC and we are in the process of scratching our
562 * heads, looking about for more room...
564 STATIC void
565 xfs_flush_inodes_work(
566 struct xfs_mount *mp,
567 void *arg)
569 struct inode *inode = arg;
570 xfs_sync_data(mp, SYNC_TRYLOCK);
571 xfs_sync_data(mp, SYNC_TRYLOCK | SYNC_WAIT);
572 iput(inode);
575 void
576 xfs_flush_inodes(
577 xfs_inode_t *ip)
579 struct inode *inode = VFS_I(ip);
580 DECLARE_COMPLETION_ONSTACK(completion);
582 igrab(inode);
583 xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_inodes_work, &completion);
584 wait_for_completion(&completion);
585 xfs_log_force(ip->i_mount, (xfs_lsn_t)0, XFS_LOG_FORCE|XFS_LOG_SYNC);
589 * Every sync period we need to unpin all items, reclaim inodes, sync
590 * quota and write out the superblock. We might need to cover the log
591 * to indicate it is idle.
593 STATIC void
594 xfs_sync_worker(
595 struct xfs_mount *mp,
596 void *unused)
598 int error;
600 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
601 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
602 xfs_reclaim_inodes(mp, XFS_IFLUSH_DELWRI_ELSE_ASYNC);
603 /* dgc: errors ignored here */
604 error = xfs_qm_sync(mp, SYNC_TRYLOCK);
605 error = xfs_sync_fsdata(mp, SYNC_TRYLOCK);
607 mp->m_sync_seq++;
608 wake_up(&mp->m_wait_single_sync_task);
611 STATIC int
612 xfssyncd(
613 void *arg)
615 struct xfs_mount *mp = arg;
616 long timeleft;
617 xfs_sync_work_t *work, *n;
618 LIST_HEAD (tmp);
620 set_freezable();
621 timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10);
622 for (;;) {
623 timeleft = schedule_timeout_interruptible(timeleft);
624 /* swsusp */
625 try_to_freeze();
626 if (kthread_should_stop() && list_empty(&mp->m_sync_list))
627 break;
629 spin_lock(&mp->m_sync_lock);
631 * We can get woken by laptop mode, to do a sync -
632 * that's the (only!) case where the list would be
633 * empty with time remaining.
635 if (!timeleft || list_empty(&mp->m_sync_list)) {
636 if (!timeleft)
637 timeleft = xfs_syncd_centisecs *
638 msecs_to_jiffies(10);
639 INIT_LIST_HEAD(&mp->m_sync_work.w_list);
640 list_add_tail(&mp->m_sync_work.w_list,
641 &mp->m_sync_list);
643 list_for_each_entry_safe(work, n, &mp->m_sync_list, w_list)
644 list_move(&work->w_list, &tmp);
645 spin_unlock(&mp->m_sync_lock);
647 list_for_each_entry_safe(work, n, &tmp, w_list) {
648 (*work->w_syncer)(mp, work->w_data);
649 list_del(&work->w_list);
650 if (work == &mp->m_sync_work)
651 continue;
652 if (work->w_completion)
653 complete(work->w_completion);
654 kmem_free(work);
658 return 0;
662 xfs_syncd_init(
663 struct xfs_mount *mp)
665 mp->m_sync_work.w_syncer = xfs_sync_worker;
666 mp->m_sync_work.w_mount = mp;
667 mp->m_sync_work.w_completion = NULL;
668 mp->m_sync_task = kthread_run(xfssyncd, mp, "xfssyncd");
669 if (IS_ERR(mp->m_sync_task))
670 return -PTR_ERR(mp->m_sync_task);
671 return 0;
674 void
675 xfs_syncd_stop(
676 struct xfs_mount *mp)
678 kthread_stop(mp->m_sync_task);
681 void
682 __xfs_inode_set_reclaim_tag(
683 struct xfs_perag *pag,
684 struct xfs_inode *ip)
686 radix_tree_tag_set(&pag->pag_ici_root,
687 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
688 XFS_ICI_RECLAIM_TAG);
689 pag->pag_ici_reclaimable++;
693 * We set the inode flag atomically with the radix tree tag.
694 * Once we get tag lookups on the radix tree, this inode flag
695 * can go away.
697 void
698 xfs_inode_set_reclaim_tag(
699 xfs_inode_t *ip)
701 xfs_mount_t *mp = ip->i_mount;
702 xfs_perag_t *pag = xfs_get_perag(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_put_perag(mp, pag);
713 void
714 __xfs_inode_clear_reclaim(
715 xfs_perag_t *pag,
716 xfs_inode_t *ip)
718 pag->pag_ici_reclaimable--;
721 void
722 __xfs_inode_clear_reclaim_tag(
723 xfs_mount_t *mp,
724 xfs_perag_t *pag,
725 xfs_inode_t *ip)
727 radix_tree_tag_clear(&pag->pag_ici_root,
728 XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
729 __xfs_inode_clear_reclaim(pag, ip);
732 STATIC int
733 xfs_reclaim_inode(
734 struct xfs_inode *ip,
735 struct xfs_perag *pag,
736 int sync_mode)
739 * The radix tree lock here protects a thread in xfs_iget from racing
740 * with us starting reclaim on the inode. Once we have the
741 * XFS_IRECLAIM flag set it will not touch us.
743 spin_lock(&ip->i_flags_lock);
744 ASSERT_ALWAYS(__xfs_iflags_test(ip, XFS_IRECLAIMABLE));
745 if (__xfs_iflags_test(ip, XFS_IRECLAIM)) {
746 /* ignore as it is already under reclaim */
747 spin_unlock(&ip->i_flags_lock);
748 write_unlock(&pag->pag_ici_lock);
749 return 0;
751 __xfs_iflags_set(ip, XFS_IRECLAIM);
752 spin_unlock(&ip->i_flags_lock);
753 write_unlock(&pag->pag_ici_lock);
756 * If the inode is still dirty, then flush it out. If the inode
757 * is not in the AIL, then it will be OK to flush it delwri as
758 * long as xfs_iflush() does not keep any references to the inode.
759 * We leave that decision up to xfs_iflush() since it has the
760 * knowledge of whether it's OK to simply do a delwri flush of
761 * the inode or whether we need to wait until the inode is
762 * pulled from the AIL.
763 * We get the flush lock regardless, though, just to make sure
764 * we don't free it while it is being flushed.
766 xfs_ilock(ip, XFS_ILOCK_EXCL);
767 xfs_iflock(ip);
770 * In the case of a forced shutdown we rely on xfs_iflush() to
771 * wait for the inode to be unpinned before returning an error.
773 if (!is_bad_inode(VFS_I(ip)) && xfs_iflush(ip, sync_mode) == 0) {
774 /* synchronize with xfs_iflush_done */
775 xfs_iflock(ip);
776 xfs_ifunlock(ip);
779 xfs_iunlock(ip, XFS_ILOCK_EXCL);
780 xfs_ireclaim(ip);
781 return 0;
785 xfs_reclaim_inodes(
786 xfs_mount_t *mp,
787 int mode)
789 return xfs_inode_ag_iterator(mp, xfs_reclaim_inode, mode,
790 XFS_ICI_RECLAIM_TAG, 1, NULL);
794 * Shrinker infrastructure.
796 * This is all far more complex than it needs to be. It adds a global list of
797 * mounts because the shrinkers can only call a global context. We need to make
798 * the shrinkers pass a context to avoid the need for global state.
800 static LIST_HEAD(xfs_mount_list);
801 static struct rw_semaphore xfs_mount_list_lock;
803 static int
804 xfs_reclaim_inode_shrink(
805 int nr_to_scan,
806 gfp_t gfp_mask)
808 struct xfs_mount *mp;
809 xfs_agnumber_t ag;
810 int reclaimable = 0;
812 if (nr_to_scan) {
813 if (!(gfp_mask & __GFP_FS))
814 return -1;
816 down_read(&xfs_mount_list_lock);
817 list_for_each_entry(mp, &xfs_mount_list, m_mplist) {
818 xfs_inode_ag_iterator(mp, xfs_reclaim_inode, 0,
819 XFS_ICI_RECLAIM_TAG, 1, &nr_to_scan);
820 if (nr_to_scan <= 0)
821 break;
823 up_read(&xfs_mount_list_lock);
826 down_read(&xfs_mount_list_lock);
827 list_for_each_entry(mp, &xfs_mount_list, m_mplist) {
828 for (ag = 0; ag < mp->m_sb.sb_agcount; ag++) {
830 if (!mp->m_perag[ag].pag_ici_init)
831 continue;
832 reclaimable += mp->m_perag[ag].pag_ici_reclaimable;
835 up_read(&xfs_mount_list_lock);
836 return reclaimable;
839 static struct shrinker xfs_inode_shrinker = {
840 .shrink = xfs_reclaim_inode_shrink,
841 .seeks = DEFAULT_SEEKS,
844 void __init
845 xfs_inode_shrinker_init(void)
847 init_rwsem(&xfs_mount_list_lock);
848 register_shrinker(&xfs_inode_shrinker);
851 void
852 xfs_inode_shrinker_destroy(void)
854 ASSERT(list_empty(&xfs_mount_list));
855 unregister_shrinker(&xfs_inode_shrinker);
858 void
859 xfs_inode_shrinker_register(
860 struct xfs_mount *mp)
862 down_write(&xfs_mount_list_lock);
863 list_add_tail(&mp->m_mplist, &xfs_mount_list);
864 up_write(&xfs_mount_list_lock);
867 void
868 xfs_inode_shrinker_unregister(
869 struct xfs_mount *mp)
871 down_write(&xfs_mount_list_lock);
872 list_del(&mp->m_mplist);
873 up_write(&xfs_mount_list_lock);