blob: 850b4198bf60361ff1bfe19e75e9a95b51a721df [file] [log] [blame]
David Chinnerfe4fa4b2008-10-30 17:06:08 +11001/*
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
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.
8 *
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.
13 *
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
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"
David Chinnerfe4fa4b2008-10-30 17:06:08 +110027#include "xfs_mount.h"
28#include "xfs_bmap_btree.h"
David Chinnerfe4fa4b2008-10-30 17:06:08 +110029#include "xfs_inode.h"
30#include "xfs_dinode.h"
31#include "xfs_error.h"
David Chinnerfe4fa4b2008-10-30 17:06:08 +110032#include "xfs_filestream.h"
33#include "xfs_vnodeops.h"
David Chinnerfe4fa4b2008-10-30 17:06:08 +110034#include "xfs_inode_item.h"
Christoph Hellwig7d095252009-06-08 15:33:32 +020035#include "xfs_quota.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000036#include "xfs_trace.h"
David Chinnerfe4fa4b2008-10-30 17:06:08 +110037
David Chinnera167b172008-10-30 17:06:18 +110038#include <linux/kthread.h>
39#include <linux/freezer.h>
40
Dave Chinner5a34d5c2009-06-08 15:35:03 +020041
Dave Chinner75f3cb12009-06-08 15:35:14 +020042STATIC xfs_inode_t *
43xfs_inode_ag_lookup(
44 struct xfs_mount *mp,
45 struct xfs_perag *pag,
46 uint32_t *first_index,
47 int tag)
48{
49 int nr_found;
50 struct xfs_inode *ip;
51
52 /*
53 * use a gang lookup to find the next inode in the tree
54 * as the tree is sparse and a gang lookup walks to find
55 * the number of objects requested.
56 */
Dave Chinner75f3cb12009-06-08 15:35:14 +020057 if (tag == XFS_ICI_NO_TAG) {
58 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
59 (void **)&ip, *first_index, 1);
60 } else {
61 nr_found = radix_tree_gang_lookup_tag(&pag->pag_ici_root,
62 (void **)&ip, *first_index, 1, tag);
63 }
64 if (!nr_found)
Dave Chinnerc8e20be2010-01-10 23:51:45 +000065 return NULL;
Dave Chinner75f3cb12009-06-08 15:35:14 +020066
67 /*
68 * Update the index for the next lookup. Catch overflows
69 * into the next AG range which can occur if we have inodes
70 * in the last block of the AG and we are currently
71 * pointing to the last inode.
72 */
73 *first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
74 if (*first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
Dave Chinnerc8e20be2010-01-10 23:51:45 +000075 return NULL;
Dave Chinner75f3cb12009-06-08 15:35:14 +020076 return ip;
Dave Chinner75f3cb12009-06-08 15:35:14 +020077}
78
79STATIC int
80xfs_inode_ag_walk(
81 struct xfs_mount *mp,
Dave Chinner5017e972010-01-11 11:47:40 +000082 struct xfs_perag *pag,
Dave Chinner75f3cb12009-06-08 15:35:14 +020083 int (*execute)(struct xfs_inode *ip,
84 struct xfs_perag *pag, int flags),
85 int flags,
Dave Chinnerc8e20be2010-01-10 23:51:45 +000086 int tag,
Dave Chinner9bf729c2010-04-29 09:55:50 +100087 int exclusive,
88 int *nr_to_scan)
Dave Chinner75f3cb12009-06-08 15:35:14 +020089{
Dave Chinner75f3cb12009-06-08 15:35:14 +020090 uint32_t first_index;
91 int last_error = 0;
92 int skipped;
93
94restart:
95 skipped = 0;
96 first_index = 0;
97 do {
98 int error = 0;
99 xfs_inode_t *ip;
100
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000101 if (exclusive)
102 write_lock(&pag->pag_ici_lock);
103 else
104 read_lock(&pag->pag_ici_lock);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200105 ip = xfs_inode_ag_lookup(mp, pag, &first_index, tag);
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000106 if (!ip) {
107 if (exclusive)
108 write_unlock(&pag->pag_ici_lock);
109 else
110 read_unlock(&pag->pag_ici_lock);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200111 break;
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000112 }
Dave Chinner75f3cb12009-06-08 15:35:14 +0200113
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000114 /* execute releases pag->pag_ici_lock */
Dave Chinner75f3cb12009-06-08 15:35:14 +0200115 error = execute(ip, pag, flags);
116 if (error == EAGAIN) {
117 skipped++;
118 continue;
119 }
120 if (error)
121 last_error = error;
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000122
123 /* bail out if the filesystem is corrupted. */
Dave Chinner75f3cb12009-06-08 15:35:14 +0200124 if (error == EFSCORRUPTED)
125 break;
126
Dave Chinner9bf729c2010-04-29 09:55:50 +1000127 } while ((*nr_to_scan)--);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200128
129 if (skipped) {
130 delay(1);
131 goto restart;
132 }
Dave Chinner75f3cb12009-06-08 15:35:14 +0200133 return last_error;
134}
135
Dave Chinner16fd5362010-07-20 09:43:39 +1000136/*
137 * Select the next per-ag structure to iterate during the walk. The reclaim
138 * walk is optimised only to walk AGs with reclaimable inodes in them.
139 */
140static struct xfs_perag *
141xfs_inode_ag_iter_next_pag(
142 struct xfs_mount *mp,
143 xfs_agnumber_t *first,
144 int tag)
145{
146 struct xfs_perag *pag = NULL;
147
148 if (tag == XFS_ICI_RECLAIM_TAG) {
149 int found;
150 int ref;
151
152 spin_lock(&mp->m_perag_lock);
153 found = radix_tree_gang_lookup_tag(&mp->m_perag_tree,
154 (void **)&pag, *first, 1, tag);
155 if (found <= 0) {
156 spin_unlock(&mp->m_perag_lock);
157 return NULL;
158 }
159 *first = pag->pag_agno + 1;
160 /* open coded pag reference increment */
161 ref = atomic_inc_return(&pag->pag_ref);
162 spin_unlock(&mp->m_perag_lock);
163 trace_xfs_perag_get_reclaim(mp, pag->pag_agno, ref, _RET_IP_);
164 } else {
165 pag = xfs_perag_get(mp, *first);
166 (*first)++;
167 }
168 return pag;
169}
170
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200171int
Dave Chinner75f3cb12009-06-08 15:35:14 +0200172xfs_inode_ag_iterator(
173 struct xfs_mount *mp,
174 int (*execute)(struct xfs_inode *ip,
175 struct xfs_perag *pag, int flags),
176 int flags,
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000177 int tag,
Dave Chinner9bf729c2010-04-29 09:55:50 +1000178 int exclusive,
179 int *nr_to_scan)
Dave Chinner75f3cb12009-06-08 15:35:14 +0200180{
Dave Chinner16fd5362010-07-20 09:43:39 +1000181 struct xfs_perag *pag;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200182 int error = 0;
183 int last_error = 0;
184 xfs_agnumber_t ag;
Dave Chinner9bf729c2010-04-29 09:55:50 +1000185 int nr;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200186
Dave Chinner9bf729c2010-04-29 09:55:50 +1000187 nr = nr_to_scan ? *nr_to_scan : INT_MAX;
Dave Chinner16fd5362010-07-20 09:43:39 +1000188 ag = 0;
189 while ((pag = xfs_inode_ag_iter_next_pag(mp, &ag, tag))) {
Dave Chinner5017e972010-01-11 11:47:40 +0000190 error = xfs_inode_ag_walk(mp, pag, execute, flags, tag,
Dave Chinner9bf729c2010-04-29 09:55:50 +1000191 exclusive, &nr);
Dave Chinner5017e972010-01-11 11:47:40 +0000192 xfs_perag_put(pag);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200193 if (error) {
194 last_error = error;
195 if (error == EFSCORRUPTED)
196 break;
197 }
Dave Chinner9bf729c2010-04-29 09:55:50 +1000198 if (nr <= 0)
199 break;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200200 }
Dave Chinner9bf729c2010-04-29 09:55:50 +1000201 if (nr_to_scan)
202 *nr_to_scan = nr;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200203 return XFS_ERROR(last_error);
204}
205
Dave Chinner1da8eec2009-06-08 15:35:07 +0200206/* must be called with pag_ici_lock held and releases it */
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200207int
Dave Chinner1da8eec2009-06-08 15:35:07 +0200208xfs_sync_inode_valid(
209 struct xfs_inode *ip,
210 struct xfs_perag *pag)
211{
212 struct inode *inode = VFS_I(ip);
Dave Chinner018027b2010-01-10 23:51:46 +0000213 int error = EFSCORRUPTED;
Dave Chinner1da8eec2009-06-08 15:35:07 +0200214
215 /* nothing to sync during shutdown */
Dave Chinner018027b2010-01-10 23:51:46 +0000216 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
217 goto out_unlock;
Dave Chinner1da8eec2009-06-08 15:35:07 +0200218
Dave Chinner018027b2010-01-10 23:51:46 +0000219 /* avoid new or reclaimable inodes. Leave for reclaim code to flush */
220 error = ENOENT;
221 if (xfs_iflags_test(ip, XFS_INEW | XFS_IRECLAIMABLE | XFS_IRECLAIM))
222 goto out_unlock;
Dave Chinner1da8eec2009-06-08 15:35:07 +0200223
Dave Chinner018027b2010-01-10 23:51:46 +0000224 /* If we can't grab the inode, it must on it's way to reclaim. */
225 if (!igrab(inode))
226 goto out_unlock;
227
228 if (is_bad_inode(inode)) {
Dave Chinner1da8eec2009-06-08 15:35:07 +0200229 IRELE(ip);
Dave Chinner018027b2010-01-10 23:51:46 +0000230 goto out_unlock;
Dave Chinner1da8eec2009-06-08 15:35:07 +0200231 }
232
Dave Chinner018027b2010-01-10 23:51:46 +0000233 /* inode is valid */
234 error = 0;
235out_unlock:
236 read_unlock(&pag->pag_ici_lock);
237 return error;
Dave Chinner1da8eec2009-06-08 15:35:07 +0200238}
239
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200240STATIC int
241xfs_sync_inode_data(
242 struct xfs_inode *ip,
Dave Chinner75f3cb12009-06-08 15:35:14 +0200243 struct xfs_perag *pag,
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200244 int flags)
245{
246 struct inode *inode = VFS_I(ip);
247 struct address_space *mapping = inode->i_mapping;
248 int error = 0;
249
Dave Chinner75f3cb12009-06-08 15:35:14 +0200250 error = xfs_sync_inode_valid(ip, pag);
251 if (error)
252 return error;
253
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200254 if (!mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
255 goto out_wait;
256
257 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED)) {
258 if (flags & SYNC_TRYLOCK)
259 goto out_wait;
260 xfs_ilock(ip, XFS_IOLOCK_SHARED);
261 }
262
263 error = xfs_flush_pages(ip, 0, -1, (flags & SYNC_WAIT) ?
Christoph Hellwig0cadda12010-01-19 09:56:44 +0000264 0 : XBF_ASYNC, FI_NONE);
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200265 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
266
267 out_wait:
Christoph Hellwigb0710cc2009-06-08 15:37:11 +0200268 if (flags & SYNC_WAIT)
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200269 xfs_ioend_wait(ip);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200270 IRELE(ip);
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200271 return error;
272}
273
Christoph Hellwig845b6d02009-06-08 15:35:05 +0200274STATIC int
275xfs_sync_inode_attr(
276 struct xfs_inode *ip,
Dave Chinner75f3cb12009-06-08 15:35:14 +0200277 struct xfs_perag *pag,
Christoph Hellwig845b6d02009-06-08 15:35:05 +0200278 int flags)
279{
280 int error = 0;
281
Dave Chinner75f3cb12009-06-08 15:35:14 +0200282 error = xfs_sync_inode_valid(ip, pag);
283 if (error)
284 return error;
285
Christoph Hellwig845b6d02009-06-08 15:35:05 +0200286 xfs_ilock(ip, XFS_ILOCK_SHARED);
287 if (xfs_inode_clean(ip))
288 goto out_unlock;
289 if (!xfs_iflock_nowait(ip)) {
290 if (!(flags & SYNC_WAIT))
291 goto out_unlock;
292 xfs_iflock(ip);
293 }
294
295 if (xfs_inode_clean(ip)) {
296 xfs_ifunlock(ip);
297 goto out_unlock;
298 }
299
Dave Chinnerc8543632010-02-06 12:39:36 +1100300 error = xfs_iflush(ip, flags);
Christoph Hellwig845b6d02009-06-08 15:35:05 +0200301
302 out_unlock:
303 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200304 IRELE(ip);
Christoph Hellwig845b6d02009-06-08 15:35:05 +0200305 return error;
306}
307
Christoph Hellwig075fe102009-06-08 15:35:48 +0200308/*
309 * Write out pagecache data for the whole filesystem.
310 */
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100311int
Christoph Hellwig075fe102009-06-08 15:35:48 +0200312xfs_sync_data(
313 struct xfs_mount *mp,
314 int flags)
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100315{
Christoph Hellwig075fe102009-06-08 15:35:48 +0200316 int error;
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100317
Christoph Hellwigb0710cc2009-06-08 15:37:11 +0200318 ASSERT((flags & ~(SYNC_TRYLOCK|SYNC_WAIT)) == 0);
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100319
Christoph Hellwig075fe102009-06-08 15:35:48 +0200320 error = xfs_inode_ag_iterator(mp, xfs_sync_inode_data, flags,
Dave Chinner9bf729c2010-04-29 09:55:50 +1000321 XFS_ICI_NO_TAG, 0, NULL);
Christoph Hellwig075fe102009-06-08 15:35:48 +0200322 if (error)
323 return XFS_ERROR(error);
David Chinnere9f1c6e2008-10-30 17:15:50 +1100324
Christoph Hellwiga14a3482010-01-19 09:56:46 +0000325 xfs_log_force(mp, (flags & SYNC_WAIT) ? XFS_LOG_SYNC : 0);
Christoph Hellwig075fe102009-06-08 15:35:48 +0200326 return 0;
327}
David Chinnere9f1c6e2008-10-30 17:15:50 +1100328
Christoph Hellwig075fe102009-06-08 15:35:48 +0200329/*
330 * Write out inode metadata (attributes) for the whole filesystem.
331 */
332int
333xfs_sync_attr(
334 struct xfs_mount *mp,
335 int flags)
336{
337 ASSERT((flags & ~SYNC_WAIT) == 0);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200338
Christoph Hellwig075fe102009-06-08 15:35:48 +0200339 return xfs_inode_ag_iterator(mp, xfs_sync_inode_attr, flags,
Dave Chinner9bf729c2010-04-29 09:55:50 +1000340 XFS_ICI_NO_TAG, 0, NULL);
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100341}
342
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100343STATIC int
344xfs_commit_dummy_trans(
345 struct xfs_mount *mp,
Dave Chinnerdce50652009-10-06 20:29:30 +0000346 uint flags)
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100347{
348 struct xfs_inode *ip = mp->m_rootip;
349 struct xfs_trans *tp;
350 int error;
351
352 /*
353 * Put a dummy transaction in the log to tell recovery
354 * that all others are OK.
355 */
356 tp = xfs_trans_alloc(mp, XFS_TRANS_DUMMY1);
357 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
358 if (error) {
359 xfs_trans_cancel(tp, 0);
360 return error;
361 }
362
363 xfs_ilock(ip, XFS_ILOCK_EXCL);
364
365 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
366 xfs_trans_ihold(tp, ip);
367 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100368 error = xfs_trans_commit(tp, 0);
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100369 xfs_iunlock(ip, XFS_ILOCK_EXCL);
370
Dave Chinnerdce50652009-10-06 20:29:30 +0000371 /* the log force ensures this transaction is pushed to disk */
Christoph Hellwiga14a3482010-01-19 09:56:46 +0000372 xfs_log_force(mp, (flags & SYNC_WAIT) ? XFS_LOG_SYNC : 0);
Dave Chinnerdce50652009-10-06 20:29:30 +0000373 return error;
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100374}
375
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000376STATIC int
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100377xfs_sync_fsdata(
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000378 struct xfs_mount *mp)
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100379{
380 struct xfs_buf *bp;
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100381
382 /*
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000383 * If the buffer is pinned then push on the log so we won't get stuck
384 * waiting in the write for someone, maybe ourselves, to flush the log.
385 *
386 * Even though we just pushed the log above, we did not have the
387 * superblock buffer locked at that point so it can become pinned in
388 * between there and here.
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100389 */
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000390 bp = xfs_getsb(mp, 0);
391 if (XFS_BUF_ISPINNED(bp))
392 xfs_log_force(mp, 0);
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100393
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000394 return xfs_bwrite(mp, bp);
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100395}
396
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100397/*
David Chinnera4e4c4f2008-10-30 17:16:11 +1100398 * When remounting a filesystem read-only or freezing the filesystem, we have
399 * two phases to execute. This first phase is syncing the data before we
400 * quiesce the filesystem, and the second is flushing all the inodes out after
401 * we've waited for all the transactions created by the first phase to
402 * complete. The second phase ensures that the inodes are written to their
403 * location on disk rather than just existing in transactions in the log. This
404 * means after a quiesce there is no log replay required to write the inodes to
405 * disk (this is the main difference between a sync and a quiesce).
406 */
407/*
408 * First stage of freeze - no writers will make progress now we are here,
David Chinnere9f1c6e2008-10-30 17:15:50 +1100409 * so we flush delwri and delalloc buffers here, then wait for all I/O to
410 * complete. Data is frozen at that point. Metadata is not frozen,
David Chinnera4e4c4f2008-10-30 17:16:11 +1100411 * transactions can still occur here so don't bother flushing the buftarg
412 * because it'll just get dirty again.
David Chinnere9f1c6e2008-10-30 17:15:50 +1100413 */
414int
415xfs_quiesce_data(
416 struct xfs_mount *mp)
417{
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000418 int error, error2 = 0;
David Chinnere9f1c6e2008-10-30 17:15:50 +1100419
420 /* push non-blocking */
Christoph Hellwig075fe102009-06-08 15:35:48 +0200421 xfs_sync_data(mp, 0);
Christoph Hellwig8b5403a2009-06-08 15:37:16 +0200422 xfs_qm_sync(mp, SYNC_TRYLOCK);
David Chinnere9f1c6e2008-10-30 17:15:50 +1100423
Dave Chinnerc90b07e2009-10-06 20:29:27 +0000424 /* push and block till complete */
Christoph Hellwigb0710cc2009-06-08 15:37:11 +0200425 xfs_sync_data(mp, SYNC_WAIT);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200426 xfs_qm_sync(mp, SYNC_WAIT);
David Chinnere9f1c6e2008-10-30 17:15:50 +1100427
David Chinnera4e4c4f2008-10-30 17:16:11 +1100428 /* write superblock and hoover up shutdown errors */
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000429 error = xfs_sync_fsdata(mp);
430
431 /* make sure all delwri buffers are written out */
432 xfs_flush_buftarg(mp->m_ddev_targp, 1);
433
434 /* mark the log as covered if needed */
435 if (xfs_log_need_covered(mp))
436 error2 = xfs_commit_dummy_trans(mp, SYNC_WAIT);
David Chinnere9f1c6e2008-10-30 17:15:50 +1100437
David Chinnera4e4c4f2008-10-30 17:16:11 +1100438 /* flush data-only devices */
David Chinnere9f1c6e2008-10-30 17:15:50 +1100439 if (mp->m_rtdev_targp)
440 XFS_bflush(mp->m_rtdev_targp);
441
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000442 return error ? error : error2;
David Chinnere9f1c6e2008-10-30 17:15:50 +1100443}
444
David Chinner76bf1052008-10-30 17:16:21 +1100445STATIC void
446xfs_quiesce_fs(
447 struct xfs_mount *mp)
448{
449 int count = 0, pincount;
450
Dave Chinnerc8543632010-02-06 12:39:36 +1100451 xfs_reclaim_inodes(mp, 0);
David Chinner76bf1052008-10-30 17:16:21 +1100452 xfs_flush_buftarg(mp->m_ddev_targp, 0);
David Chinner76bf1052008-10-30 17:16:21 +1100453
454 /*
455 * This loop must run at least twice. The first instance of the loop
456 * will flush most meta data but that will generate more meta data
457 * (typically directory updates). Which then must be flushed and
Dave Chinnerc8543632010-02-06 12:39:36 +1100458 * logged before we can write the unmount record. We also so sync
459 * reclaim of inodes to catch any that the above delwri flush skipped.
David Chinner76bf1052008-10-30 17:16:21 +1100460 */
461 do {
Dave Chinnerc8543632010-02-06 12:39:36 +1100462 xfs_reclaim_inodes(mp, SYNC_WAIT);
Christoph Hellwig075fe102009-06-08 15:35:48 +0200463 xfs_sync_attr(mp, SYNC_WAIT);
David Chinner76bf1052008-10-30 17:16:21 +1100464 pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1);
465 if (!pincount) {
466 delay(50);
467 count++;
468 }
469 } while (count < 2);
470}
471
472/*
473 * Second stage of a quiesce. The data is already synced, now we have to take
474 * care of the metadata. New transactions are already blocked, so we need to
475 * wait for any remaining transactions to drain out before proceding.
476 */
477void
478xfs_quiesce_attr(
479 struct xfs_mount *mp)
480{
481 int error = 0;
482
483 /* wait for all modifications to complete */
484 while (atomic_read(&mp->m_active_trans) > 0)
485 delay(100);
486
487 /* flush inodes and push all remaining buffers out to disk */
488 xfs_quiesce_fs(mp);
489
Felix Blyakher5e106572009-01-22 21:34:05 -0600490 /*
491 * Just warn here till VFS can correctly support
492 * read-only remount without racing.
493 */
494 WARN_ON(atomic_read(&mp->m_active_trans) != 0);
David Chinner76bf1052008-10-30 17:16:21 +1100495
496 /* Push the superblock and write an unmount record */
497 error = xfs_log_sbcount(mp, 1);
498 if (error)
499 xfs_fs_cmn_err(CE_WARN, mp,
500 "xfs_attr_quiesce: failed to log sb changes. "
501 "Frozen image may not be consistent.");
502 xfs_log_unmount_write(mp);
503 xfs_unmountfs_writesb(mp);
504}
505
David Chinnere9f1c6e2008-10-30 17:15:50 +1100506/*
David Chinnera167b172008-10-30 17:06:18 +1100507 * Enqueue a work item to be picked up by the vfs xfssyncd thread.
508 * Doing this has two advantages:
509 * - It saves on stack space, which is tight in certain situations
510 * - It can be used (with care) as a mechanism to avoid deadlocks.
511 * Flushing while allocating in a full filesystem requires both.
512 */
513STATIC void
514xfs_syncd_queue_work(
515 struct xfs_mount *mp,
516 void *data,
Dave Chinnere43afd72009-04-06 18:47:27 +0200517 void (*syncer)(struct xfs_mount *, void *),
518 struct completion *completion)
David Chinnera167b172008-10-30 17:06:18 +1100519{
Dave Chinnera8d770d2009-04-06 18:44:54 +0200520 struct xfs_sync_work *work;
David Chinnera167b172008-10-30 17:06:18 +1100521
Dave Chinnera8d770d2009-04-06 18:44:54 +0200522 work = kmem_alloc(sizeof(struct xfs_sync_work), KM_SLEEP);
David Chinnera167b172008-10-30 17:06:18 +1100523 INIT_LIST_HEAD(&work->w_list);
524 work->w_syncer = syncer;
525 work->w_data = data;
526 work->w_mount = mp;
Dave Chinnere43afd72009-04-06 18:47:27 +0200527 work->w_completion = completion;
David Chinnera167b172008-10-30 17:06:18 +1100528 spin_lock(&mp->m_sync_lock);
529 list_add_tail(&work->w_list, &mp->m_sync_list);
530 spin_unlock(&mp->m_sync_lock);
531 wake_up_process(mp->m_sync_task);
532}
533
534/*
535 * Flush delayed allocate data, attempting to free up reserved space
536 * from existing allocations. At this point a new allocation attempt
537 * has failed with ENOSPC and we are in the process of scratching our
538 * heads, looking about for more room...
539 */
540STATIC void
Dave Chinnera8d770d2009-04-06 18:44:54 +0200541xfs_flush_inodes_work(
David Chinnera167b172008-10-30 17:06:18 +1100542 struct xfs_mount *mp,
543 void *arg)
544{
545 struct inode *inode = arg;
Christoph Hellwig075fe102009-06-08 15:35:48 +0200546 xfs_sync_data(mp, SYNC_TRYLOCK);
Christoph Hellwigb0710cc2009-06-08 15:37:11 +0200547 xfs_sync_data(mp, SYNC_TRYLOCK | SYNC_WAIT);
David Chinnera167b172008-10-30 17:06:18 +1100548 iput(inode);
549}
550
551void
Dave Chinnera8d770d2009-04-06 18:44:54 +0200552xfs_flush_inodes(
David Chinnera167b172008-10-30 17:06:18 +1100553 xfs_inode_t *ip)
554{
555 struct inode *inode = VFS_I(ip);
Dave Chinnere43afd72009-04-06 18:47:27 +0200556 DECLARE_COMPLETION_ONSTACK(completion);
David Chinnera167b172008-10-30 17:06:18 +1100557
558 igrab(inode);
Dave Chinnere43afd72009-04-06 18:47:27 +0200559 xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_inodes_work, &completion);
560 wait_for_completion(&completion);
Christoph Hellwiga14a3482010-01-19 09:56:46 +0000561 xfs_log_force(ip->i_mount, XFS_LOG_SYNC);
David Chinnera167b172008-10-30 17:06:18 +1100562}
563
David Chinneraacaa882008-10-30 17:15:29 +1100564/*
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000565 * Every sync period we need to unpin all items, reclaim inodes and sync
566 * disk quotas. We might need to cover the log to indicate that the
567 * filesystem is idle.
David Chinneraacaa882008-10-30 17:15:29 +1100568 */
David Chinnera167b172008-10-30 17:06:18 +1100569STATIC void
570xfs_sync_worker(
571 struct xfs_mount *mp,
572 void *unused)
573{
574 int error;
575
David Chinneraacaa882008-10-30 17:15:29 +1100576 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
Christoph Hellwiga14a3482010-01-19 09:56:46 +0000577 xfs_log_force(mp, 0);
Dave Chinnerc8543632010-02-06 12:39:36 +1100578 xfs_reclaim_inodes(mp, 0);
David Chinneraacaa882008-10-30 17:15:29 +1100579 /* dgc: errors ignored here */
Christoph Hellwig8b5403a2009-06-08 15:37:16 +0200580 error = xfs_qm_sync(mp, SYNC_TRYLOCK);
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000581 if (xfs_log_need_covered(mp))
582 error = xfs_commit_dummy_trans(mp, 0);
David Chinneraacaa882008-10-30 17:15:29 +1100583 }
David Chinnera167b172008-10-30 17:06:18 +1100584 mp->m_sync_seq++;
585 wake_up(&mp->m_wait_single_sync_task);
586}
587
588STATIC int
589xfssyncd(
590 void *arg)
591{
592 struct xfs_mount *mp = arg;
593 long timeleft;
Dave Chinnera8d770d2009-04-06 18:44:54 +0200594 xfs_sync_work_t *work, *n;
David Chinnera167b172008-10-30 17:06:18 +1100595 LIST_HEAD (tmp);
596
597 set_freezable();
598 timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10);
599 for (;;) {
Dave Chinner20f6b2c2010-03-04 01:46:23 +0000600 if (list_empty(&mp->m_sync_list))
601 timeleft = schedule_timeout_interruptible(timeleft);
David Chinnera167b172008-10-30 17:06:18 +1100602 /* swsusp */
603 try_to_freeze();
604 if (kthread_should_stop() && list_empty(&mp->m_sync_list))
605 break;
606
607 spin_lock(&mp->m_sync_lock);
608 /*
609 * We can get woken by laptop mode, to do a sync -
610 * that's the (only!) case where the list would be
611 * empty with time remaining.
612 */
613 if (!timeleft || list_empty(&mp->m_sync_list)) {
614 if (!timeleft)
615 timeleft = xfs_syncd_centisecs *
616 msecs_to_jiffies(10);
617 INIT_LIST_HEAD(&mp->m_sync_work.w_list);
618 list_add_tail(&mp->m_sync_work.w_list,
619 &mp->m_sync_list);
620 }
Dave Chinner20f6b2c2010-03-04 01:46:23 +0000621 list_splice_init(&mp->m_sync_list, &tmp);
David Chinnera167b172008-10-30 17:06:18 +1100622 spin_unlock(&mp->m_sync_lock);
623
624 list_for_each_entry_safe(work, n, &tmp, w_list) {
625 (*work->w_syncer)(mp, work->w_data);
626 list_del(&work->w_list);
627 if (work == &mp->m_sync_work)
628 continue;
Dave Chinnere43afd72009-04-06 18:47:27 +0200629 if (work->w_completion)
630 complete(work->w_completion);
David Chinnera167b172008-10-30 17:06:18 +1100631 kmem_free(work);
632 }
633 }
634
635 return 0;
636}
637
638int
639xfs_syncd_init(
640 struct xfs_mount *mp)
641{
642 mp->m_sync_work.w_syncer = xfs_sync_worker;
643 mp->m_sync_work.w_mount = mp;
Dave Chinnere43afd72009-04-06 18:47:27 +0200644 mp->m_sync_work.w_completion = NULL;
Jan Engelhardte2a07812010-03-23 09:52:55 +1100645 mp->m_sync_task = kthread_run(xfssyncd, mp, "xfssyncd/%s", mp->m_fsname);
David Chinnera167b172008-10-30 17:06:18 +1100646 if (IS_ERR(mp->m_sync_task))
647 return -PTR_ERR(mp->m_sync_task);
648 return 0;
649}
650
651void
652xfs_syncd_stop(
653 struct xfs_mount *mp)
654{
655 kthread_stop(mp->m_sync_task);
656}
657
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400658void
659__xfs_inode_set_reclaim_tag(
660 struct xfs_perag *pag,
661 struct xfs_inode *ip)
662{
663 radix_tree_tag_set(&pag->pag_ici_root,
664 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
665 XFS_ICI_RECLAIM_TAG);
Dave Chinner16fd5362010-07-20 09:43:39 +1000666
667 if (!pag->pag_ici_reclaimable) {
668 /* propagate the reclaim tag up into the perag radix tree */
669 spin_lock(&ip->i_mount->m_perag_lock);
670 radix_tree_tag_set(&ip->i_mount->m_perag_tree,
671 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
672 XFS_ICI_RECLAIM_TAG);
673 spin_unlock(&ip->i_mount->m_perag_lock);
674 trace_xfs_perag_set_reclaim(ip->i_mount, pag->pag_agno,
675 -1, _RET_IP_);
676 }
Dave Chinner9bf729c2010-04-29 09:55:50 +1000677 pag->pag_ici_reclaimable++;
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400678}
679
David Chinner11654512008-10-30 17:37:49 +1100680/*
681 * We set the inode flag atomically with the radix tree tag.
682 * Once we get tag lookups on the radix tree, this inode flag
683 * can go away.
684 */
David Chinner396beb82008-10-30 17:37:26 +1100685void
686xfs_inode_set_reclaim_tag(
687 xfs_inode_t *ip)
688{
Dave Chinner5017e972010-01-11 11:47:40 +0000689 struct xfs_mount *mp = ip->i_mount;
690 struct xfs_perag *pag;
David Chinner396beb82008-10-30 17:37:26 +1100691
Dave Chinner5017e972010-01-11 11:47:40 +0000692 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
Christoph Hellwigf1f724e2010-03-01 11:30:31 +0000693 write_lock(&pag->pag_ici_lock);
David Chinner396beb82008-10-30 17:37:26 +1100694 spin_lock(&ip->i_flags_lock);
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400695 __xfs_inode_set_reclaim_tag(pag, ip);
David Chinner11654512008-10-30 17:37:49 +1100696 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
David Chinner396beb82008-10-30 17:37:26 +1100697 spin_unlock(&ip->i_flags_lock);
Christoph Hellwigf1f724e2010-03-01 11:30:31 +0000698 write_unlock(&pag->pag_ici_lock);
Dave Chinner5017e972010-01-11 11:47:40 +0000699 xfs_perag_put(pag);
David Chinner396beb82008-10-30 17:37:26 +1100700}
701
702void
703__xfs_inode_clear_reclaim_tag(
704 xfs_mount_t *mp,
705 xfs_perag_t *pag,
706 xfs_inode_t *ip)
707{
708 radix_tree_tag_clear(&pag->pag_ici_root,
709 XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
Dave Chinner9bf729c2010-04-29 09:55:50 +1000710 pag->pag_ici_reclaimable--;
Dave Chinner16fd5362010-07-20 09:43:39 +1000711 if (!pag->pag_ici_reclaimable) {
712 /* clear the reclaim tag from the perag radix tree */
713 spin_lock(&ip->i_mount->m_perag_lock);
714 radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
715 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
716 XFS_ICI_RECLAIM_TAG);
717 spin_unlock(&ip->i_mount->m_perag_lock);
718 trace_xfs_perag_clear_reclaim(ip->i_mount, pag->pag_agno,
719 -1, _RET_IP_);
720 }
David Chinner396beb82008-10-30 17:37:26 +1100721}
722
Dave Chinner777df5a2010-02-06 12:37:26 +1100723/*
724 * Inodes in different states need to be treated differently, and the return
725 * value of xfs_iflush is not sufficient to get this right. The following table
726 * lists the inode states and the reclaim actions necessary for non-blocking
727 * reclaim:
728 *
729 *
730 * inode state iflush ret required action
731 * --------------- ---------- ---------------
732 * bad - reclaim
733 * shutdown EIO unpin and reclaim
734 * clean, unpinned 0 reclaim
735 * stale, unpinned 0 reclaim
Dave Chinnerc8543632010-02-06 12:39:36 +1100736 * clean, pinned(*) 0 requeue
737 * stale, pinned EAGAIN requeue
738 * dirty, delwri ok 0 requeue
739 * dirty, delwri blocked EAGAIN requeue
740 * dirty, sync flush 0 reclaim
Dave Chinner777df5a2010-02-06 12:37:26 +1100741 *
742 * (*) dgc: I don't think the clean, pinned state is possible but it gets
743 * handled anyway given the order of checks implemented.
744 *
Dave Chinnerc8543632010-02-06 12:39:36 +1100745 * As can be seen from the table, the return value of xfs_iflush() is not
746 * sufficient to correctly decide the reclaim action here. The checks in
747 * xfs_iflush() might look like duplicates, but they are not.
748 *
749 * Also, because we get the flush lock first, we know that any inode that has
750 * been flushed delwri has had the flush completed by the time we check that
751 * the inode is clean. The clean inode check needs to be done before flushing
752 * the inode delwri otherwise we would loop forever requeuing clean inodes as
753 * we cannot tell apart a successful delwri flush and a clean inode from the
754 * return value of xfs_iflush().
755 *
756 * Note that because the inode is flushed delayed write by background
757 * writeback, the flush lock may already be held here and waiting on it can
758 * result in very long latencies. Hence for sync reclaims, where we wait on the
759 * flush lock, the caller should push out delayed write inodes first before
760 * trying to reclaim them to minimise the amount of time spent waiting. For
761 * background relaim, we just requeue the inode for the next pass.
762 *
Dave Chinner777df5a2010-02-06 12:37:26 +1100763 * Hence the order of actions after gaining the locks should be:
764 * bad => reclaim
765 * shutdown => unpin and reclaim
Dave Chinnerc8543632010-02-06 12:39:36 +1100766 * pinned, delwri => requeue
767 * pinned, sync => unpin
Dave Chinner777df5a2010-02-06 12:37:26 +1100768 * stale => reclaim
769 * clean => reclaim
Dave Chinnerc8543632010-02-06 12:39:36 +1100770 * dirty, delwri => flush and requeue
771 * dirty, sync => flush, wait and reclaim
Dave Chinner777df5a2010-02-06 12:37:26 +1100772 */
Dave Chinner75f3cb12009-06-08 15:35:14 +0200773STATIC int
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000774xfs_reclaim_inode(
Dave Chinner75f3cb12009-06-08 15:35:14 +0200775 struct xfs_inode *ip,
776 struct xfs_perag *pag,
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000777 int sync_mode)
David Chinner7a3be022008-10-30 17:37:37 +1100778{
Dave Chinnerc8543632010-02-06 12:39:36 +1100779 int error = 0;
Dave Chinner777df5a2010-02-06 12:37:26 +1100780
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000781 /*
782 * The radix tree lock here protects a thread in xfs_iget from racing
783 * with us starting reclaim on the inode. Once we have the
784 * XFS_IRECLAIM flag set it will not touch us.
785 */
786 spin_lock(&ip->i_flags_lock);
787 ASSERT_ALWAYS(__xfs_iflags_test(ip, XFS_IRECLAIMABLE));
788 if (__xfs_iflags_test(ip, XFS_IRECLAIM)) {
789 /* ignore as it is already under reclaim */
790 spin_unlock(&ip->i_flags_lock);
791 write_unlock(&pag->pag_ici_lock);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200792 return 0;
David Chinner7a3be022008-10-30 17:37:37 +1100793 }
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000794 __xfs_iflags_set(ip, XFS_IRECLAIM);
795 spin_unlock(&ip->i_flags_lock);
796 write_unlock(&pag->pag_ici_lock);
David Chinner7a3be022008-10-30 17:37:37 +1100797
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000798 xfs_ilock(ip, XFS_ILOCK_EXCL);
Dave Chinnerc8543632010-02-06 12:39:36 +1100799 if (!xfs_iflock_nowait(ip)) {
800 if (!(sync_mode & SYNC_WAIT))
801 goto out;
802 xfs_iflock(ip);
803 }
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000804
Dave Chinner777df5a2010-02-06 12:37:26 +1100805 if (is_bad_inode(VFS_I(ip)))
806 goto reclaim;
807 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
808 xfs_iunpin_wait(ip);
809 goto reclaim;
810 }
Dave Chinnerc8543632010-02-06 12:39:36 +1100811 if (xfs_ipincount(ip)) {
812 if (!(sync_mode & SYNC_WAIT)) {
813 xfs_ifunlock(ip);
814 goto out;
815 }
Dave Chinner777df5a2010-02-06 12:37:26 +1100816 xfs_iunpin_wait(ip);
Dave Chinnerc8543632010-02-06 12:39:36 +1100817 }
Dave Chinner777df5a2010-02-06 12:37:26 +1100818 if (xfs_iflags_test(ip, XFS_ISTALE))
819 goto reclaim;
820 if (xfs_inode_clean(ip))
821 goto reclaim;
822
823 /* Now we have an inode that needs flushing */
824 error = xfs_iflush(ip, sync_mode);
Dave Chinnerc8543632010-02-06 12:39:36 +1100825 if (sync_mode & SYNC_WAIT) {
826 xfs_iflock(ip);
827 goto reclaim;
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000828 }
829
Dave Chinnerc8543632010-02-06 12:39:36 +1100830 /*
831 * When we have to flush an inode but don't have SYNC_WAIT set, we
832 * flush the inode out using a delwri buffer and wait for the next
833 * call into reclaim to find it in a clean state instead of waiting for
834 * it now. We also don't return errors here - if the error is transient
835 * then the next reclaim pass will flush the inode, and if the error
Dave Chinnerf1d486a2010-04-13 15:06:45 +1000836 * is permanent then the next sync reclaim will reclaim the inode and
Dave Chinnerc8543632010-02-06 12:39:36 +1100837 * pass on the error.
838 */
Dave Chinnerf1d486a2010-04-13 15:06:45 +1000839 if (error && error != EAGAIN && !XFS_FORCED_SHUTDOWN(ip->i_mount)) {
Dave Chinnerc8543632010-02-06 12:39:36 +1100840 xfs_fs_cmn_err(CE_WARN, ip->i_mount,
841 "inode 0x%llx background reclaim flush failed with %d",
842 (long long)ip->i_ino, error);
843 }
844out:
845 xfs_iflags_clear(ip, XFS_IRECLAIM);
846 xfs_iunlock(ip, XFS_ILOCK_EXCL);
847 /*
848 * We could return EAGAIN here to make reclaim rescan the inode tree in
849 * a short while. However, this just burns CPU time scanning the tree
850 * waiting for IO to complete and xfssyncd never goes back to the idle
851 * state. Instead, return 0 to let the next scheduled background reclaim
852 * attempt to reclaim the inode again.
853 */
854 return 0;
855
Dave Chinner777df5a2010-02-06 12:37:26 +1100856reclaim:
857 xfs_ifunlock(ip);
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000858 xfs_iunlock(ip, XFS_ILOCK_EXCL);
859 xfs_ireclaim(ip);
Dave Chinnerc8543632010-02-06 12:39:36 +1100860 return error;
861
David Chinner7a3be022008-10-30 17:37:37 +1100862}
863
David Chinnerfce08f22008-10-30 17:37:03 +1100864int
David Chinner1dc33182008-10-30 17:37:15 +1100865xfs_reclaim_inodes(
David Chinnerfce08f22008-10-30 17:37:03 +1100866 xfs_mount_t *mp,
David Chinnerfce08f22008-10-30 17:37:03 +1100867 int mode)
868{
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000869 return xfs_inode_ag_iterator(mp, xfs_reclaim_inode, mode,
Dave Chinner9bf729c2010-04-29 09:55:50 +1000870 XFS_ICI_RECLAIM_TAG, 1, NULL);
871}
872
873/*
874 * Shrinker infrastructure.
Dave Chinner9bf729c2010-04-29 09:55:50 +1000875 */
Dave Chinner9bf729c2010-04-29 09:55:50 +1000876static int
877xfs_reclaim_inode_shrink(
Dave Chinner7f8275d2010-07-19 14:56:17 +1000878 struct shrinker *shrink,
Dave Chinner9bf729c2010-04-29 09:55:50 +1000879 int nr_to_scan,
880 gfp_t gfp_mask)
881{
882 struct xfs_mount *mp;
883 struct xfs_perag *pag;
884 xfs_agnumber_t ag;
Dave Chinner16fd5362010-07-20 09:43:39 +1000885 int reclaimable;
Dave Chinner9bf729c2010-04-29 09:55:50 +1000886
Dave Chinner70e60ce2010-07-20 08:07:02 +1000887 mp = container_of(shrink, struct xfs_mount, m_inode_shrink);
Dave Chinner9bf729c2010-04-29 09:55:50 +1000888 if (nr_to_scan) {
889 if (!(gfp_mask & __GFP_FS))
890 return -1;
891
Dave Chinner70e60ce2010-07-20 08:07:02 +1000892 xfs_inode_ag_iterator(mp, xfs_reclaim_inode, 0,
Dave Chinner9bf729c2010-04-29 09:55:50 +1000893 XFS_ICI_RECLAIM_TAG, 1, &nr_to_scan);
Dave Chinner70e60ce2010-07-20 08:07:02 +1000894 /* if we don't exhaust the scan, don't bother coming back */
895 if (nr_to_scan > 0)
896 return -1;
897 }
Dave Chinner9bf729c2010-04-29 09:55:50 +1000898
Dave Chinner16fd5362010-07-20 09:43:39 +1000899 reclaimable = 0;
900 ag = 0;
901 while ((pag = xfs_inode_ag_iter_next_pag(mp, &ag,
902 XFS_ICI_RECLAIM_TAG))) {
Dave Chinner70e60ce2010-07-20 08:07:02 +1000903 reclaimable += pag->pag_ici_reclaimable;
904 xfs_perag_put(pag);
Dave Chinner9bf729c2010-04-29 09:55:50 +1000905 }
Dave Chinner9bf729c2010-04-29 09:55:50 +1000906 return reclaimable;
907}
908
Dave Chinner9bf729c2010-04-29 09:55:50 +1000909void
910xfs_inode_shrinker_register(
911 struct xfs_mount *mp)
912{
Dave Chinner70e60ce2010-07-20 08:07:02 +1000913 mp->m_inode_shrink.shrink = xfs_reclaim_inode_shrink;
914 mp->m_inode_shrink.seeks = DEFAULT_SEEKS;
915 register_shrinker(&mp->m_inode_shrink);
Dave Chinner9bf729c2010-04-29 09:55:50 +1000916}
917
918void
919xfs_inode_shrinker_unregister(
920 struct xfs_mount *mp)
921{
Dave Chinner70e60ce2010-07-20 08:07:02 +1000922 unregister_shrinker(&mp->m_inode_shrink);
David Chinnerfce08f22008-10-30 17:37:03 +1100923}