blob: a51a07c3a70cfa8b5514add3dae137402bbc56b6 [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"
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"
Christoph Hellwig7d095252009-06-08 15:33:32 +020046#include "xfs_quota.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000047#include "xfs_trace.h"
David Chinnerfe4fa4b2008-10-30 17:06:08 +110048
David Chinnera167b172008-10-30 17:06:18 +110049#include <linux/kthread.h>
50#include <linux/freezer.h>
51
Dave Chinner5a34d5c2009-06-08 15:35:03 +020052
Dave Chinner75f3cb12009-06-08 15:35:14 +020053STATIC xfs_inode_t *
54xfs_inode_ag_lookup(
55 struct xfs_mount *mp,
56 struct xfs_perag *pag,
57 uint32_t *first_index,
58 int tag)
59{
60 int nr_found;
61 struct xfs_inode *ip;
62
63 /*
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.
67 */
Dave Chinner75f3cb12009-06-08 15:35:14 +020068 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)
Dave Chinnerc8e20be2010-01-10 23:51:45 +000076 return NULL;
Dave Chinner75f3cb12009-06-08 15:35:14 +020077
78 /*
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.
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))
Dave Chinnerc8e20be2010-01-10 23:51:45 +000086 return NULL;
Dave Chinner75f3cb12009-06-08 15:35:14 +020087 return ip;
Dave Chinner75f3cb12009-06-08 15:35:14 +020088}
89
90STATIC int
91xfs_inode_ag_walk(
92 struct xfs_mount *mp,
Dave Chinner5017e972010-01-11 11:47:40 +000093 struct xfs_perag *pag,
Dave Chinner75f3cb12009-06-08 15:35:14 +020094 int (*execute)(struct xfs_inode *ip,
95 struct xfs_perag *pag, int flags),
96 int flags,
Dave Chinnerc8e20be2010-01-10 23:51:45 +000097 int tag,
Dave Chinner9bf729c2010-04-29 09:55:50 +100098 int exclusive,
99 int *nr_to_scan)
Dave Chinner75f3cb12009-06-08 15:35:14 +0200100{
Dave Chinner75f3cb12009-06-08 15:35:14 +0200101 uint32_t first_index;
102 int last_error = 0;
103 int skipped;
104
105restart:
106 skipped = 0;
107 first_index = 0;
108 do {
109 int error = 0;
110 xfs_inode_t *ip;
111
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000112 if (exclusive)
113 write_lock(&pag->pag_ici_lock);
114 else
115 read_lock(&pag->pag_ici_lock);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200116 ip = xfs_inode_ag_lookup(mp, pag, &first_index, tag);
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000117 if (!ip) {
118 if (exclusive)
119 write_unlock(&pag->pag_ici_lock);
120 else
121 read_unlock(&pag->pag_ici_lock);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200122 break;
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000123 }
Dave Chinner75f3cb12009-06-08 15:35:14 +0200124
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000125 /* execute releases pag->pag_ici_lock */
Dave Chinner75f3cb12009-06-08 15:35:14 +0200126 error = execute(ip, pag, flags);
127 if (error == EAGAIN) {
128 skipped++;
129 continue;
130 }
131 if (error)
132 last_error = error;
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000133
134 /* bail out if the filesystem is corrupted. */
Dave Chinner75f3cb12009-06-08 15:35:14 +0200135 if (error == EFSCORRUPTED)
136 break;
137
Dave Chinner9bf729c2010-04-29 09:55:50 +1000138 } while ((*nr_to_scan)--);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200139
140 if (skipped) {
141 delay(1);
142 goto restart;
143 }
Dave Chinner75f3cb12009-06-08 15:35:14 +0200144 return last_error;
145}
146
Dave Chinner16fd5362010-07-20 09:43:39 +1000147/*
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.
150 */
151static struct xfs_perag *
152xfs_inode_ag_iter_next_pag(
153 struct xfs_mount *mp,
154 xfs_agnumber_t *first,
155 int tag)
156{
157 struct xfs_perag *pag = NULL;
158
159 if (tag == XFS_ICI_RECLAIM_TAG) {
160 int found;
161 int ref;
162
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;
169 }
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)++;
178 }
179 return pag;
180}
181
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200182int
Dave Chinner75f3cb12009-06-08 15:35:14 +0200183xfs_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,
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000188 int tag,
Dave Chinner9bf729c2010-04-29 09:55:50 +1000189 int exclusive,
190 int *nr_to_scan)
Dave Chinner75f3cb12009-06-08 15:35:14 +0200191{
Dave Chinner16fd5362010-07-20 09:43:39 +1000192 struct xfs_perag *pag;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200193 int error = 0;
194 int last_error = 0;
195 xfs_agnumber_t ag;
Dave Chinner9bf729c2010-04-29 09:55:50 +1000196 int nr;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200197
Dave Chinner9bf729c2010-04-29 09:55:50 +1000198 nr = nr_to_scan ? *nr_to_scan : INT_MAX;
Dave Chinner16fd5362010-07-20 09:43:39 +1000199 ag = 0;
200 while ((pag = xfs_inode_ag_iter_next_pag(mp, &ag, tag))) {
Dave Chinner5017e972010-01-11 11:47:40 +0000201 error = xfs_inode_ag_walk(mp, pag, execute, flags, tag,
Dave Chinner9bf729c2010-04-29 09:55:50 +1000202 exclusive, &nr);
Dave Chinner5017e972010-01-11 11:47:40 +0000203 xfs_perag_put(pag);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200204 if (error) {
205 last_error = error;
206 if (error == EFSCORRUPTED)
207 break;
208 }
Dave Chinner9bf729c2010-04-29 09:55:50 +1000209 if (nr <= 0)
210 break;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200211 }
Dave Chinner9bf729c2010-04-29 09:55:50 +1000212 if (nr_to_scan)
213 *nr_to_scan = nr;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200214 return XFS_ERROR(last_error);
215}
216
Dave Chinner1da8eec2009-06-08 15:35:07 +0200217/* must be called with pag_ici_lock held and releases it */
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200218int
Dave Chinner1da8eec2009-06-08 15:35:07 +0200219xfs_sync_inode_valid(
220 struct xfs_inode *ip,
221 struct xfs_perag *pag)
222{
223 struct inode *inode = VFS_I(ip);
Dave Chinner018027b2010-01-10 23:51:46 +0000224 int error = EFSCORRUPTED;
Dave Chinner1da8eec2009-06-08 15:35:07 +0200225
226 /* nothing to sync during shutdown */
Dave Chinner018027b2010-01-10 23:51:46 +0000227 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
228 goto out_unlock;
Dave Chinner1da8eec2009-06-08 15:35:07 +0200229
Dave Chinner018027b2010-01-10 23:51:46 +0000230 /* 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;
Dave Chinner1da8eec2009-06-08 15:35:07 +0200234
Dave Chinner018027b2010-01-10 23:51:46 +0000235 /* If we can't grab the inode, it must on it's way to reclaim. */
236 if (!igrab(inode))
237 goto out_unlock;
238
239 if (is_bad_inode(inode)) {
Dave Chinner1da8eec2009-06-08 15:35:07 +0200240 IRELE(ip);
Dave Chinner018027b2010-01-10 23:51:46 +0000241 goto out_unlock;
Dave Chinner1da8eec2009-06-08 15:35:07 +0200242 }
243
Dave Chinner018027b2010-01-10 23:51:46 +0000244 /* inode is valid */
245 error = 0;
246out_unlock:
247 read_unlock(&pag->pag_ici_lock);
248 return error;
Dave Chinner1da8eec2009-06-08 15:35:07 +0200249}
250
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200251STATIC int
252xfs_sync_inode_data(
253 struct xfs_inode *ip,
Dave Chinner75f3cb12009-06-08 15:35:14 +0200254 struct xfs_perag *pag,
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200255 int flags)
256{
257 struct inode *inode = VFS_I(ip);
258 struct address_space *mapping = inode->i_mapping;
259 int error = 0;
260
Dave Chinner75f3cb12009-06-08 15:35:14 +0200261 error = xfs_sync_inode_valid(ip, pag);
262 if (error)
263 return error;
264
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200265 if (!mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
266 goto out_wait;
267
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);
272 }
273
274 error = xfs_flush_pages(ip, 0, -1, (flags & SYNC_WAIT) ?
Christoph Hellwig0cadda12010-01-19 09:56:44 +0000275 0 : XBF_ASYNC, FI_NONE);
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200276 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
277
278 out_wait:
Christoph Hellwigb0710cc2009-06-08 15:37:11 +0200279 if (flags & SYNC_WAIT)
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200280 xfs_ioend_wait(ip);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200281 IRELE(ip);
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200282 return error;
283}
284
Christoph Hellwig845b6d02009-06-08 15:35:05 +0200285STATIC int
286xfs_sync_inode_attr(
287 struct xfs_inode *ip,
Dave Chinner75f3cb12009-06-08 15:35:14 +0200288 struct xfs_perag *pag,
Christoph Hellwig845b6d02009-06-08 15:35:05 +0200289 int flags)
290{
291 int error = 0;
292
Dave Chinner75f3cb12009-06-08 15:35:14 +0200293 error = xfs_sync_inode_valid(ip, pag);
294 if (error)
295 return error;
296
Christoph Hellwig845b6d02009-06-08 15:35:05 +0200297 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);
304 }
305
306 if (xfs_inode_clean(ip)) {
307 xfs_ifunlock(ip);
308 goto out_unlock;
309 }
310
Dave Chinnerc8543632010-02-06 12:39:36 +1100311 error = xfs_iflush(ip, flags);
Christoph Hellwig845b6d02009-06-08 15:35:05 +0200312
313 out_unlock:
314 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200315 IRELE(ip);
Christoph Hellwig845b6d02009-06-08 15:35:05 +0200316 return error;
317}
318
Christoph Hellwig075fe102009-06-08 15:35:48 +0200319/*
320 * Write out pagecache data for the whole filesystem.
321 */
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100322int
Christoph Hellwig075fe102009-06-08 15:35:48 +0200323xfs_sync_data(
324 struct xfs_mount *mp,
325 int flags)
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100326{
Christoph Hellwig075fe102009-06-08 15:35:48 +0200327 int error;
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100328
Christoph Hellwigb0710cc2009-06-08 15:37:11 +0200329 ASSERT((flags & ~(SYNC_TRYLOCK|SYNC_WAIT)) == 0);
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100330
Christoph Hellwig075fe102009-06-08 15:35:48 +0200331 error = xfs_inode_ag_iterator(mp, xfs_sync_inode_data, flags,
Dave Chinner9bf729c2010-04-29 09:55:50 +1000332 XFS_ICI_NO_TAG, 0, NULL);
Christoph Hellwig075fe102009-06-08 15:35:48 +0200333 if (error)
334 return XFS_ERROR(error);
David Chinnere9f1c6e2008-10-30 17:15:50 +1100335
Christoph Hellwiga14a3482010-01-19 09:56:46 +0000336 xfs_log_force(mp, (flags & SYNC_WAIT) ? XFS_LOG_SYNC : 0);
Christoph Hellwig075fe102009-06-08 15:35:48 +0200337 return 0;
338}
David Chinnere9f1c6e2008-10-30 17:15:50 +1100339
Christoph Hellwig075fe102009-06-08 15:35:48 +0200340/*
341 * Write out inode metadata (attributes) for the whole filesystem.
342 */
343int
344xfs_sync_attr(
345 struct xfs_mount *mp,
346 int flags)
347{
348 ASSERT((flags & ~SYNC_WAIT) == 0);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200349
Christoph Hellwig075fe102009-06-08 15:35:48 +0200350 return xfs_inode_ag_iterator(mp, xfs_sync_inode_attr, flags,
Dave Chinner9bf729c2010-04-29 09:55:50 +1000351 XFS_ICI_NO_TAG, 0, NULL);
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100352}
353
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100354STATIC int
355xfs_commit_dummy_trans(
356 struct xfs_mount *mp,
Dave Chinnerdce50652009-10-06 20:29:30 +0000357 uint flags)
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100358{
359 struct xfs_inode *ip = mp->m_rootip;
360 struct xfs_trans *tp;
361 int error;
362
363 /*
364 * Put a dummy transaction in the log to tell recovery
365 * that all others are OK.
366 */
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;
372 }
373
374 xfs_ilock(ip, XFS_ILOCK_EXCL);
375
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);
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100379 error = xfs_trans_commit(tp, 0);
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100380 xfs_iunlock(ip, XFS_ILOCK_EXCL);
381
Dave Chinnerdce50652009-10-06 20:29:30 +0000382 /* the log force ensures this transaction is pushed to disk */
Christoph Hellwiga14a3482010-01-19 09:56:46 +0000383 xfs_log_force(mp, (flags & SYNC_WAIT) ? XFS_LOG_SYNC : 0);
Dave Chinnerdce50652009-10-06 20:29:30 +0000384 return error;
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100385}
386
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000387STATIC int
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100388xfs_sync_fsdata(
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000389 struct xfs_mount *mp)
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100390{
391 struct xfs_buf *bp;
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100392
393 /*
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000394 * 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.
396 *
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.
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100400 */
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000401 bp = xfs_getsb(mp, 0);
402 if (XFS_BUF_ISPINNED(bp))
403 xfs_log_force(mp, 0);
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100404
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000405 return xfs_bwrite(mp, bp);
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100406}
407
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100408/*
David Chinnera4e4c4f2008-10-30 17:16:11 +1100409 * 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).
417 */
418/*
419 * First stage of freeze - no writers will make progress now we are here,
David Chinnere9f1c6e2008-10-30 17:15:50 +1100420 * 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,
David Chinnera4e4c4f2008-10-30 17:16:11 +1100422 * transactions can still occur here so don't bother flushing the buftarg
423 * because it'll just get dirty again.
David Chinnere9f1c6e2008-10-30 17:15:50 +1100424 */
425int
426xfs_quiesce_data(
427 struct xfs_mount *mp)
428{
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000429 int error, error2 = 0;
David Chinnere9f1c6e2008-10-30 17:15:50 +1100430
431 /* push non-blocking */
Christoph Hellwig075fe102009-06-08 15:35:48 +0200432 xfs_sync_data(mp, 0);
Christoph Hellwig8b5403a2009-06-08 15:37:16 +0200433 xfs_qm_sync(mp, SYNC_TRYLOCK);
David Chinnere9f1c6e2008-10-30 17:15:50 +1100434
Dave Chinnerc90b07e2009-10-06 20:29:27 +0000435 /* push and block till complete */
Christoph Hellwigb0710cc2009-06-08 15:37:11 +0200436 xfs_sync_data(mp, SYNC_WAIT);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200437 xfs_qm_sync(mp, SYNC_WAIT);
David Chinnere9f1c6e2008-10-30 17:15:50 +1100438
David Chinnera4e4c4f2008-10-30 17:16:11 +1100439 /* write superblock and hoover up shutdown errors */
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000440 error = xfs_sync_fsdata(mp);
441
442 /* make sure all delwri buffers are written out */
443 xfs_flush_buftarg(mp->m_ddev_targp, 1);
444
445 /* mark the log as covered if needed */
446 if (xfs_log_need_covered(mp))
447 error2 = xfs_commit_dummy_trans(mp, SYNC_WAIT);
David Chinnere9f1c6e2008-10-30 17:15:50 +1100448
David Chinnera4e4c4f2008-10-30 17:16:11 +1100449 /* flush data-only devices */
David Chinnere9f1c6e2008-10-30 17:15:50 +1100450 if (mp->m_rtdev_targp)
451 XFS_bflush(mp->m_rtdev_targp);
452
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000453 return error ? error : error2;
David Chinnere9f1c6e2008-10-30 17:15:50 +1100454}
455
David Chinner76bf1052008-10-30 17:16:21 +1100456STATIC void
457xfs_quiesce_fs(
458 struct xfs_mount *mp)
459{
460 int count = 0, pincount;
461
Dave Chinnerc8543632010-02-06 12:39:36 +1100462 xfs_reclaim_inodes(mp, 0);
David Chinner76bf1052008-10-30 17:16:21 +1100463 xfs_flush_buftarg(mp->m_ddev_targp, 0);
David Chinner76bf1052008-10-30 17:16:21 +1100464
465 /*
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
Dave Chinnerc8543632010-02-06 12:39:36 +1100469 * 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.
David Chinner76bf1052008-10-30 17:16:21 +1100471 */
472 do {
Dave Chinnerc8543632010-02-06 12:39:36 +1100473 xfs_reclaim_inodes(mp, SYNC_WAIT);
Christoph Hellwig075fe102009-06-08 15:35:48 +0200474 xfs_sync_attr(mp, SYNC_WAIT);
David Chinner76bf1052008-10-30 17:16:21 +1100475 pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1);
476 if (!pincount) {
477 delay(50);
478 count++;
479 }
480 } while (count < 2);
481}
482
483/*
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.
487 */
488void
489xfs_quiesce_attr(
490 struct xfs_mount *mp)
491{
492 int error = 0;
493
494 /* wait for all modifications to complete */
495 while (atomic_read(&mp->m_active_trans) > 0)
496 delay(100);
497
498 /* flush inodes and push all remaining buffers out to disk */
499 xfs_quiesce_fs(mp);
500
Felix Blyakher5e106572009-01-22 21:34:05 -0600501 /*
502 * Just warn here till VFS can correctly support
503 * read-only remount without racing.
504 */
505 WARN_ON(atomic_read(&mp->m_active_trans) != 0);
David Chinner76bf1052008-10-30 17:16:21 +1100506
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);
515}
516
David Chinnere9f1c6e2008-10-30 17:15:50 +1100517/*
David Chinnera167b172008-10-30 17:06:18 +1100518 * 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.
523 */
524STATIC void
525xfs_syncd_queue_work(
526 struct xfs_mount *mp,
527 void *data,
Dave Chinnere43afd72009-04-06 18:47:27 +0200528 void (*syncer)(struct xfs_mount *, void *),
529 struct completion *completion)
David Chinnera167b172008-10-30 17:06:18 +1100530{
Dave Chinnera8d770d2009-04-06 18:44:54 +0200531 struct xfs_sync_work *work;
David Chinnera167b172008-10-30 17:06:18 +1100532
Dave Chinnera8d770d2009-04-06 18:44:54 +0200533 work = kmem_alloc(sizeof(struct xfs_sync_work), KM_SLEEP);
David Chinnera167b172008-10-30 17:06:18 +1100534 INIT_LIST_HEAD(&work->w_list);
535 work->w_syncer = syncer;
536 work->w_data = data;
537 work->w_mount = mp;
Dave Chinnere43afd72009-04-06 18:47:27 +0200538 work->w_completion = completion;
David Chinnera167b172008-10-30 17:06:18 +1100539 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);
543}
544
545/*
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...
550 */
551STATIC void
Dave Chinnera8d770d2009-04-06 18:44:54 +0200552xfs_flush_inodes_work(
David Chinnera167b172008-10-30 17:06:18 +1100553 struct xfs_mount *mp,
554 void *arg)
555{
556 struct inode *inode = arg;
Christoph Hellwig075fe102009-06-08 15:35:48 +0200557 xfs_sync_data(mp, SYNC_TRYLOCK);
Christoph Hellwigb0710cc2009-06-08 15:37:11 +0200558 xfs_sync_data(mp, SYNC_TRYLOCK | SYNC_WAIT);
David Chinnera167b172008-10-30 17:06:18 +1100559 iput(inode);
560}
561
562void
Dave Chinnera8d770d2009-04-06 18:44:54 +0200563xfs_flush_inodes(
David Chinnera167b172008-10-30 17:06:18 +1100564 xfs_inode_t *ip)
565{
566 struct inode *inode = VFS_I(ip);
Dave Chinnere43afd72009-04-06 18:47:27 +0200567 DECLARE_COMPLETION_ONSTACK(completion);
David Chinnera167b172008-10-30 17:06:18 +1100568
569 igrab(inode);
Dave Chinnere43afd72009-04-06 18:47:27 +0200570 xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_inodes_work, &completion);
571 wait_for_completion(&completion);
Christoph Hellwiga14a3482010-01-19 09:56:46 +0000572 xfs_log_force(ip->i_mount, XFS_LOG_SYNC);
David Chinnera167b172008-10-30 17:06:18 +1100573}
574
David Chinneraacaa882008-10-30 17:15:29 +1100575/*
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000576 * 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.
David Chinneraacaa882008-10-30 17:15:29 +1100579 */
David Chinnera167b172008-10-30 17:06:18 +1100580STATIC void
581xfs_sync_worker(
582 struct xfs_mount *mp,
583 void *unused)
584{
585 int error;
586
David Chinneraacaa882008-10-30 17:15:29 +1100587 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
Christoph Hellwiga14a3482010-01-19 09:56:46 +0000588 xfs_log_force(mp, 0);
Dave Chinnerc8543632010-02-06 12:39:36 +1100589 xfs_reclaim_inodes(mp, 0);
David Chinneraacaa882008-10-30 17:15:29 +1100590 /* dgc: errors ignored here */
Christoph Hellwig8b5403a2009-06-08 15:37:16 +0200591 error = xfs_qm_sync(mp, SYNC_TRYLOCK);
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000592 if (xfs_log_need_covered(mp))
593 error = xfs_commit_dummy_trans(mp, 0);
David Chinneraacaa882008-10-30 17:15:29 +1100594 }
David Chinnera167b172008-10-30 17:06:18 +1100595 mp->m_sync_seq++;
596 wake_up(&mp->m_wait_single_sync_task);
597}
598
599STATIC int
600xfssyncd(
601 void *arg)
602{
603 struct xfs_mount *mp = arg;
604 long timeleft;
Dave Chinnera8d770d2009-04-06 18:44:54 +0200605 xfs_sync_work_t *work, *n;
David Chinnera167b172008-10-30 17:06:18 +1100606 LIST_HEAD (tmp);
607
608 set_freezable();
609 timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10);
610 for (;;) {
Dave Chinner20f6b2c2010-03-04 01:46:23 +0000611 if (list_empty(&mp->m_sync_list))
612 timeleft = schedule_timeout_interruptible(timeleft);
David Chinnera167b172008-10-30 17:06:18 +1100613 /* swsusp */
614 try_to_freeze();
615 if (kthread_should_stop() && list_empty(&mp->m_sync_list))
616 break;
617
618 spin_lock(&mp->m_sync_lock);
619 /*
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.
623 */
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);
631 }
Dave Chinner20f6b2c2010-03-04 01:46:23 +0000632 list_splice_init(&mp->m_sync_list, &tmp);
David Chinnera167b172008-10-30 17:06:18 +1100633 spin_unlock(&mp->m_sync_lock);
634
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;
Dave Chinnere43afd72009-04-06 18:47:27 +0200640 if (work->w_completion)
641 complete(work->w_completion);
David Chinnera167b172008-10-30 17:06:18 +1100642 kmem_free(work);
643 }
644 }
645
646 return 0;
647}
648
649int
650xfs_syncd_init(
651 struct xfs_mount *mp)
652{
653 mp->m_sync_work.w_syncer = xfs_sync_worker;
654 mp->m_sync_work.w_mount = mp;
Dave Chinnere43afd72009-04-06 18:47:27 +0200655 mp->m_sync_work.w_completion = NULL;
Jan Engelhardte2a07812010-03-23 09:52:55 +1100656 mp->m_sync_task = kthread_run(xfssyncd, mp, "xfssyncd/%s", mp->m_fsname);
David Chinnera167b172008-10-30 17:06:18 +1100657 if (IS_ERR(mp->m_sync_task))
658 return -PTR_ERR(mp->m_sync_task);
659 return 0;
660}
661
662void
663xfs_syncd_stop(
664 struct xfs_mount *mp)
665{
666 kthread_stop(mp->m_sync_task);
667}
668
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400669void
670__xfs_inode_set_reclaim_tag(
671 struct xfs_perag *pag,
672 struct xfs_inode *ip)
673{
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);
Dave Chinner16fd5362010-07-20 09:43:39 +1000677
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_);
687 }
Dave Chinner9bf729c2010-04-29 09:55:50 +1000688 pag->pag_ici_reclaimable++;
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400689}
690
David Chinner11654512008-10-30 17:37:49 +1100691/*
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.
695 */
David Chinner396beb82008-10-30 17:37:26 +1100696void
697xfs_inode_set_reclaim_tag(
698 xfs_inode_t *ip)
699{
Dave Chinner5017e972010-01-11 11:47:40 +0000700 struct xfs_mount *mp = ip->i_mount;
701 struct xfs_perag *pag;
David Chinner396beb82008-10-30 17:37:26 +1100702
Dave Chinner5017e972010-01-11 11:47:40 +0000703 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
Christoph Hellwigf1f724e2010-03-01 11:30:31 +0000704 write_lock(&pag->pag_ici_lock);
David Chinner396beb82008-10-30 17:37:26 +1100705 spin_lock(&ip->i_flags_lock);
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400706 __xfs_inode_set_reclaim_tag(pag, ip);
David Chinner11654512008-10-30 17:37:49 +1100707 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
David Chinner396beb82008-10-30 17:37:26 +1100708 spin_unlock(&ip->i_flags_lock);
Christoph Hellwigf1f724e2010-03-01 11:30:31 +0000709 write_unlock(&pag->pag_ici_lock);
Dave Chinner5017e972010-01-11 11:47:40 +0000710 xfs_perag_put(pag);
David Chinner396beb82008-10-30 17:37:26 +1100711}
712
713void
714__xfs_inode_clear_reclaim_tag(
715 xfs_mount_t *mp,
716 xfs_perag_t *pag,
717 xfs_inode_t *ip)
718{
719 radix_tree_tag_clear(&pag->pag_ici_root,
720 XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
Dave Chinner9bf729c2010-04-29 09:55:50 +1000721 pag->pag_ici_reclaimable--;
Dave Chinner16fd5362010-07-20 09:43:39 +1000722 if (!pag->pag_ici_reclaimable) {
723 /* clear the reclaim tag from the perag radix tree */
724 spin_lock(&ip->i_mount->m_perag_lock);
725 radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
726 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
727 XFS_ICI_RECLAIM_TAG);
728 spin_unlock(&ip->i_mount->m_perag_lock);
729 trace_xfs_perag_clear_reclaim(ip->i_mount, pag->pag_agno,
730 -1, _RET_IP_);
731 }
David Chinner396beb82008-10-30 17:37:26 +1100732}
733
Dave Chinner777df5a2010-02-06 12:37:26 +1100734/*
735 * Inodes in different states need to be treated differently, and the return
736 * value of xfs_iflush is not sufficient to get this right. The following table
737 * lists the inode states and the reclaim actions necessary for non-blocking
738 * reclaim:
739 *
740 *
741 * inode state iflush ret required action
742 * --------------- ---------- ---------------
743 * bad - reclaim
744 * shutdown EIO unpin and reclaim
745 * clean, unpinned 0 reclaim
746 * stale, unpinned 0 reclaim
Dave Chinnerc8543632010-02-06 12:39:36 +1100747 * clean, pinned(*) 0 requeue
748 * stale, pinned EAGAIN requeue
749 * dirty, delwri ok 0 requeue
750 * dirty, delwri blocked EAGAIN requeue
751 * dirty, sync flush 0 reclaim
Dave Chinner777df5a2010-02-06 12:37:26 +1100752 *
753 * (*) dgc: I don't think the clean, pinned state is possible but it gets
754 * handled anyway given the order of checks implemented.
755 *
Dave Chinnerc8543632010-02-06 12:39:36 +1100756 * As can be seen from the table, the return value of xfs_iflush() is not
757 * sufficient to correctly decide the reclaim action here. The checks in
758 * xfs_iflush() might look like duplicates, but they are not.
759 *
760 * Also, because we get the flush lock first, we know that any inode that has
761 * been flushed delwri has had the flush completed by the time we check that
762 * the inode is clean. The clean inode check needs to be done before flushing
763 * the inode delwri otherwise we would loop forever requeuing clean inodes as
764 * we cannot tell apart a successful delwri flush and a clean inode from the
765 * return value of xfs_iflush().
766 *
767 * Note that because the inode is flushed delayed write by background
768 * writeback, the flush lock may already be held here and waiting on it can
769 * result in very long latencies. Hence for sync reclaims, where we wait on the
770 * flush lock, the caller should push out delayed write inodes first before
771 * trying to reclaim them to minimise the amount of time spent waiting. For
772 * background relaim, we just requeue the inode for the next pass.
773 *
Dave Chinner777df5a2010-02-06 12:37:26 +1100774 * Hence the order of actions after gaining the locks should be:
775 * bad => reclaim
776 * shutdown => unpin and reclaim
Dave Chinnerc8543632010-02-06 12:39:36 +1100777 * pinned, delwri => requeue
778 * pinned, sync => unpin
Dave Chinner777df5a2010-02-06 12:37:26 +1100779 * stale => reclaim
780 * clean => reclaim
Dave Chinnerc8543632010-02-06 12:39:36 +1100781 * dirty, delwri => flush and requeue
782 * dirty, sync => flush, wait and reclaim
Dave Chinner777df5a2010-02-06 12:37:26 +1100783 */
Dave Chinner75f3cb12009-06-08 15:35:14 +0200784STATIC int
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000785xfs_reclaim_inode(
Dave Chinner75f3cb12009-06-08 15:35:14 +0200786 struct xfs_inode *ip,
787 struct xfs_perag *pag,
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000788 int sync_mode)
David Chinner7a3be022008-10-30 17:37:37 +1100789{
Dave Chinnerc8543632010-02-06 12:39:36 +1100790 int error = 0;
Dave Chinner777df5a2010-02-06 12:37:26 +1100791
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000792 /*
793 * The radix tree lock here protects a thread in xfs_iget from racing
794 * with us starting reclaim on the inode. Once we have the
795 * XFS_IRECLAIM flag set it will not touch us.
796 */
797 spin_lock(&ip->i_flags_lock);
798 ASSERT_ALWAYS(__xfs_iflags_test(ip, XFS_IRECLAIMABLE));
799 if (__xfs_iflags_test(ip, XFS_IRECLAIM)) {
800 /* ignore as it is already under reclaim */
801 spin_unlock(&ip->i_flags_lock);
802 write_unlock(&pag->pag_ici_lock);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200803 return 0;
David Chinner7a3be022008-10-30 17:37:37 +1100804 }
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000805 __xfs_iflags_set(ip, XFS_IRECLAIM);
806 spin_unlock(&ip->i_flags_lock);
807 write_unlock(&pag->pag_ici_lock);
David Chinner7a3be022008-10-30 17:37:37 +1100808
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000809 xfs_ilock(ip, XFS_ILOCK_EXCL);
Dave Chinnerc8543632010-02-06 12:39:36 +1100810 if (!xfs_iflock_nowait(ip)) {
811 if (!(sync_mode & SYNC_WAIT))
812 goto out;
813 xfs_iflock(ip);
814 }
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000815
Dave Chinner777df5a2010-02-06 12:37:26 +1100816 if (is_bad_inode(VFS_I(ip)))
817 goto reclaim;
818 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
819 xfs_iunpin_wait(ip);
820 goto reclaim;
821 }
Dave Chinnerc8543632010-02-06 12:39:36 +1100822 if (xfs_ipincount(ip)) {
823 if (!(sync_mode & SYNC_WAIT)) {
824 xfs_ifunlock(ip);
825 goto out;
826 }
Dave Chinner777df5a2010-02-06 12:37:26 +1100827 xfs_iunpin_wait(ip);
Dave Chinnerc8543632010-02-06 12:39:36 +1100828 }
Dave Chinner777df5a2010-02-06 12:37:26 +1100829 if (xfs_iflags_test(ip, XFS_ISTALE))
830 goto reclaim;
831 if (xfs_inode_clean(ip))
832 goto reclaim;
833
834 /* Now we have an inode that needs flushing */
835 error = xfs_iflush(ip, sync_mode);
Dave Chinnerc8543632010-02-06 12:39:36 +1100836 if (sync_mode & SYNC_WAIT) {
837 xfs_iflock(ip);
838 goto reclaim;
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000839 }
840
Dave Chinnerc8543632010-02-06 12:39:36 +1100841 /*
842 * When we have to flush an inode but don't have SYNC_WAIT set, we
843 * flush the inode out using a delwri buffer and wait for the next
844 * call into reclaim to find it in a clean state instead of waiting for
845 * it now. We also don't return errors here - if the error is transient
846 * then the next reclaim pass will flush the inode, and if the error
Dave Chinnerf1d486a2010-04-13 15:06:45 +1000847 * is permanent then the next sync reclaim will reclaim the inode and
Dave Chinnerc8543632010-02-06 12:39:36 +1100848 * pass on the error.
849 */
Dave Chinnerf1d486a2010-04-13 15:06:45 +1000850 if (error && error != EAGAIN && !XFS_FORCED_SHUTDOWN(ip->i_mount)) {
Dave Chinnerc8543632010-02-06 12:39:36 +1100851 xfs_fs_cmn_err(CE_WARN, ip->i_mount,
852 "inode 0x%llx background reclaim flush failed with %d",
853 (long long)ip->i_ino, error);
854 }
855out:
856 xfs_iflags_clear(ip, XFS_IRECLAIM);
857 xfs_iunlock(ip, XFS_ILOCK_EXCL);
858 /*
859 * We could return EAGAIN here to make reclaim rescan the inode tree in
860 * a short while. However, this just burns CPU time scanning the tree
861 * waiting for IO to complete and xfssyncd never goes back to the idle
862 * state. Instead, return 0 to let the next scheduled background reclaim
863 * attempt to reclaim the inode again.
864 */
865 return 0;
866
Dave Chinner777df5a2010-02-06 12:37:26 +1100867reclaim:
868 xfs_ifunlock(ip);
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000869 xfs_iunlock(ip, XFS_ILOCK_EXCL);
870 xfs_ireclaim(ip);
Dave Chinnerc8543632010-02-06 12:39:36 +1100871 return error;
872
David Chinner7a3be022008-10-30 17:37:37 +1100873}
874
David Chinnerfce08f22008-10-30 17:37:03 +1100875int
David Chinner1dc33182008-10-30 17:37:15 +1100876xfs_reclaim_inodes(
David Chinnerfce08f22008-10-30 17:37:03 +1100877 xfs_mount_t *mp,
David Chinnerfce08f22008-10-30 17:37:03 +1100878 int mode)
879{
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000880 return xfs_inode_ag_iterator(mp, xfs_reclaim_inode, mode,
Dave Chinner9bf729c2010-04-29 09:55:50 +1000881 XFS_ICI_RECLAIM_TAG, 1, NULL);
882}
883
884/*
885 * Shrinker infrastructure.
Dave Chinner9bf729c2010-04-29 09:55:50 +1000886 */
Dave Chinner9bf729c2010-04-29 09:55:50 +1000887static int
888xfs_reclaim_inode_shrink(
Dave Chinner7f8275d2010-07-19 14:56:17 +1000889 struct shrinker *shrink,
Dave Chinner9bf729c2010-04-29 09:55:50 +1000890 int nr_to_scan,
891 gfp_t gfp_mask)
892{
893 struct xfs_mount *mp;
894 struct xfs_perag *pag;
895 xfs_agnumber_t ag;
Dave Chinner16fd5362010-07-20 09:43:39 +1000896 int reclaimable;
Dave Chinner9bf729c2010-04-29 09:55:50 +1000897
Dave Chinner70e60ce2010-07-20 08:07:02 +1000898 mp = container_of(shrink, struct xfs_mount, m_inode_shrink);
Dave Chinner9bf729c2010-04-29 09:55:50 +1000899 if (nr_to_scan) {
900 if (!(gfp_mask & __GFP_FS))
901 return -1;
902
Dave Chinner70e60ce2010-07-20 08:07:02 +1000903 xfs_inode_ag_iterator(mp, xfs_reclaim_inode, 0,
Dave Chinner9bf729c2010-04-29 09:55:50 +1000904 XFS_ICI_RECLAIM_TAG, 1, &nr_to_scan);
Dave Chinner70e60ce2010-07-20 08:07:02 +1000905 /* if we don't exhaust the scan, don't bother coming back */
906 if (nr_to_scan > 0)
907 return -1;
908 }
Dave Chinner9bf729c2010-04-29 09:55:50 +1000909
Dave Chinner16fd5362010-07-20 09:43:39 +1000910 reclaimable = 0;
911 ag = 0;
912 while ((pag = xfs_inode_ag_iter_next_pag(mp, &ag,
913 XFS_ICI_RECLAIM_TAG))) {
Dave Chinner70e60ce2010-07-20 08:07:02 +1000914 reclaimable += pag->pag_ici_reclaimable;
915 xfs_perag_put(pag);
Dave Chinner9bf729c2010-04-29 09:55:50 +1000916 }
Dave Chinner9bf729c2010-04-29 09:55:50 +1000917 return reclaimable;
918}
919
Dave Chinner9bf729c2010-04-29 09:55:50 +1000920void
921xfs_inode_shrinker_register(
922 struct xfs_mount *mp)
923{
Dave Chinner70e60ce2010-07-20 08:07:02 +1000924 mp->m_inode_shrink.shrink = xfs_reclaim_inode_shrink;
925 mp->m_inode_shrink.seeks = DEFAULT_SEEKS;
926 register_shrinker(&mp->m_inode_shrink);
Dave Chinner9bf729c2010-04-29 09:55:50 +1000927}
928
929void
930xfs_inode_shrinker_unregister(
931 struct xfs_mount *mp)
932{
Dave Chinner70e60ce2010-07-20 08:07:02 +1000933 unregister_shrinker(&mp->m_inode_shrink);
David Chinnerfce08f22008-10-30 17:37:03 +1100934}