blob: af3275965c77dd111b6e046f5a8aa2c46186f153 [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"
Dave Chinner1a387d32010-08-24 11:46:31 +100037#include "xfs_fsops.h"
David Chinnerfe4fa4b2008-10-30 17:06:08 +110038
David Chinnera167b172008-10-30 17:06:18 +110039#include <linux/kthread.h>
40#include <linux/freezer.h>
41
Dave Chinnerc6d09b62011-04-08 12:45:07 +100042struct workqueue_struct *xfs_syncd_wq; /* sync workqueue */
43
Dave Chinner78ae5252010-09-28 12:28:19 +100044/*
45 * The inode lookup is done in batches to keep the amount of lock traffic and
46 * radix tree lookups to a minimum. The batch size is a trade off between
47 * lookup reduction and stack usage. This is in the reclaim path, so we can't
48 * be too greedy.
49 */
50#define XFS_LOOKUP_BATCH 32
51
Dave Chinnere13de952010-09-28 12:28:06 +100052STATIC int
53xfs_inode_ag_walk_grab(
54 struct xfs_inode *ip)
55{
56 struct inode *inode = VFS_I(ip);
57
Dave Chinner1a3e8f32010-12-17 17:29:43 +110058 ASSERT(rcu_read_lock_held());
59
60 /*
61 * check for stale RCU freed inode
62 *
63 * If the inode has been reallocated, it doesn't matter if it's not in
64 * the AG we are walking - we are walking for writeback, so if it
65 * passes all the "valid inode" checks and is dirty, then we'll write
66 * it back anyway. If it has been reallocated and still being
67 * initialised, the XFS_INEW check below will catch it.
68 */
69 spin_lock(&ip->i_flags_lock);
70 if (!ip->i_ino)
71 goto out_unlock_noent;
72
73 /* avoid new or reclaimable inodes. Leave for reclaim code to flush */
74 if (__xfs_iflags_test(ip, XFS_INEW | XFS_IRECLAIMABLE | XFS_IRECLAIM))
75 goto out_unlock_noent;
76 spin_unlock(&ip->i_flags_lock);
77
Dave Chinnere13de952010-09-28 12:28:06 +100078 /* nothing to sync during shutdown */
79 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
80 return EFSCORRUPTED;
81
Dave Chinnere13de952010-09-28 12:28:06 +100082 /* If we can't grab the inode, it must on it's way to reclaim. */
83 if (!igrab(inode))
84 return ENOENT;
85
86 if (is_bad_inode(inode)) {
87 IRELE(ip);
88 return ENOENT;
89 }
90
91 /* inode is valid */
92 return 0;
Dave Chinner1a3e8f32010-12-17 17:29:43 +110093
94out_unlock_noent:
95 spin_unlock(&ip->i_flags_lock);
96 return ENOENT;
Dave Chinnere13de952010-09-28 12:28:06 +100097}
98
Dave Chinner75f3cb12009-06-08 15:35:14 +020099STATIC int
100xfs_inode_ag_walk(
101 struct xfs_mount *mp,
Dave Chinner5017e972010-01-11 11:47:40 +0000102 struct xfs_perag *pag,
Dave Chinner75f3cb12009-06-08 15:35:14 +0200103 int (*execute)(struct xfs_inode *ip,
104 struct xfs_perag *pag, int flags),
Dave Chinner65d0f202010-09-24 18:40:15 +1000105 int flags)
Dave Chinner75f3cb12009-06-08 15:35:14 +0200106{
Dave Chinner75f3cb12009-06-08 15:35:14 +0200107 uint32_t first_index;
108 int last_error = 0;
109 int skipped;
Dave Chinner65d0f202010-09-24 18:40:15 +1000110 int done;
Dave Chinner78ae5252010-09-28 12:28:19 +1000111 int nr_found;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200112
113restart:
Dave Chinner65d0f202010-09-24 18:40:15 +1000114 done = 0;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200115 skipped = 0;
116 first_index = 0;
Dave Chinner78ae5252010-09-28 12:28:19 +1000117 nr_found = 0;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200118 do {
Dave Chinner78ae5252010-09-28 12:28:19 +1000119 struct xfs_inode *batch[XFS_LOOKUP_BATCH];
Dave Chinner75f3cb12009-06-08 15:35:14 +0200120 int error = 0;
Dave Chinner78ae5252010-09-28 12:28:19 +1000121 int i;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200122
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100123 rcu_read_lock();
Dave Chinner65d0f202010-09-24 18:40:15 +1000124 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
Dave Chinner78ae5252010-09-28 12:28:19 +1000125 (void **)batch, first_index,
126 XFS_LOOKUP_BATCH);
Dave Chinner65d0f202010-09-24 18:40:15 +1000127 if (!nr_found) {
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100128 rcu_read_unlock();
Dave Chinner75f3cb12009-06-08 15:35:14 +0200129 break;
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000130 }
Dave Chinner75f3cb12009-06-08 15:35:14 +0200131
Dave Chinner65d0f202010-09-24 18:40:15 +1000132 /*
Dave Chinner78ae5252010-09-28 12:28:19 +1000133 * Grab the inodes before we drop the lock. if we found
134 * nothing, nr == 0 and the loop will be skipped.
Dave Chinner65d0f202010-09-24 18:40:15 +1000135 */
Dave Chinner78ae5252010-09-28 12:28:19 +1000136 for (i = 0; i < nr_found; i++) {
137 struct xfs_inode *ip = batch[i];
Dave Chinner65d0f202010-09-24 18:40:15 +1000138
Dave Chinner78ae5252010-09-28 12:28:19 +1000139 if (done || xfs_inode_ag_walk_grab(ip))
140 batch[i] = NULL;
141
142 /*
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100143 * Update the index for the next lookup. Catch
144 * overflows into the next AG range which can occur if
145 * we have inodes in the last block of the AG and we
146 * are currently pointing to the last inode.
147 *
148 * Because we may see inodes that are from the wrong AG
149 * due to RCU freeing and reallocation, only update the
150 * index if it lies in this AG. It was a race that lead
151 * us to see this inode, so another lookup from the
152 * same index will not find it again.
Dave Chinner78ae5252010-09-28 12:28:19 +1000153 */
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100154 if (XFS_INO_TO_AGNO(mp, ip->i_ino) != pag->pag_agno)
155 continue;
Dave Chinner78ae5252010-09-28 12:28:19 +1000156 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
157 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
158 done = 1;
Dave Chinnere13de952010-09-28 12:28:06 +1000159 }
Dave Chinner78ae5252010-09-28 12:28:19 +1000160
161 /* unlock now we've grabbed the inodes. */
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100162 rcu_read_unlock();
Dave Chinnere13de952010-09-28 12:28:06 +1000163
Dave Chinner78ae5252010-09-28 12:28:19 +1000164 for (i = 0; i < nr_found; i++) {
165 if (!batch[i])
166 continue;
167 error = execute(batch[i], pag, flags);
168 IRELE(batch[i]);
169 if (error == EAGAIN) {
170 skipped++;
171 continue;
172 }
173 if (error && last_error != EFSCORRUPTED)
174 last_error = error;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200175 }
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000176
177 /* bail out if the filesystem is corrupted. */
Dave Chinner75f3cb12009-06-08 15:35:14 +0200178 if (error == EFSCORRUPTED)
179 break;
180
Dave Chinner78ae5252010-09-28 12:28:19 +1000181 } while (nr_found && !done);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200182
183 if (skipped) {
184 delay(1);
185 goto restart;
186 }
Dave Chinner75f3cb12009-06-08 15:35:14 +0200187 return last_error;
188}
189
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200190int
Dave Chinner75f3cb12009-06-08 15:35:14 +0200191xfs_inode_ag_iterator(
192 struct xfs_mount *mp,
193 int (*execute)(struct xfs_inode *ip,
194 struct xfs_perag *pag, int flags),
Dave Chinner65d0f202010-09-24 18:40:15 +1000195 int flags)
Dave Chinner75f3cb12009-06-08 15:35:14 +0200196{
Dave Chinner16fd5362010-07-20 09:43:39 +1000197 struct xfs_perag *pag;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200198 int error = 0;
199 int last_error = 0;
200 xfs_agnumber_t ag;
201
Dave Chinner16fd5362010-07-20 09:43:39 +1000202 ag = 0;
Dave Chinner65d0f202010-09-24 18:40:15 +1000203 while ((pag = xfs_perag_get(mp, ag))) {
204 ag = pag->pag_agno + 1;
205 error = xfs_inode_ag_walk(mp, pag, execute, flags);
Dave Chinner5017e972010-01-11 11:47:40 +0000206 xfs_perag_put(pag);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200207 if (error) {
208 last_error = error;
209 if (error == EFSCORRUPTED)
210 break;
211 }
212 }
213 return XFS_ERROR(last_error);
214}
215
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200216STATIC int
217xfs_sync_inode_data(
218 struct xfs_inode *ip,
Dave Chinner75f3cb12009-06-08 15:35:14 +0200219 struct xfs_perag *pag,
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200220 int flags)
221{
222 struct inode *inode = VFS_I(ip);
223 struct address_space *mapping = inode->i_mapping;
224 int error = 0;
225
226 if (!mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
227 goto out_wait;
228
229 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED)) {
230 if (flags & SYNC_TRYLOCK)
231 goto out_wait;
232 xfs_ilock(ip, XFS_IOLOCK_SHARED);
233 }
234
235 error = xfs_flush_pages(ip, 0, -1, (flags & SYNC_WAIT) ?
Christoph Hellwig0cadda12010-01-19 09:56:44 +0000236 0 : XBF_ASYNC, FI_NONE);
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200237 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
238
239 out_wait:
Christoph Hellwigb0710cc2009-06-08 15:37:11 +0200240 if (flags & SYNC_WAIT)
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200241 xfs_ioend_wait(ip);
242 return error;
243}
244
Christoph Hellwig845b6d02009-06-08 15:35:05 +0200245STATIC int
246xfs_sync_inode_attr(
247 struct xfs_inode *ip,
Dave Chinner75f3cb12009-06-08 15:35:14 +0200248 struct xfs_perag *pag,
Christoph Hellwig845b6d02009-06-08 15:35:05 +0200249 int flags)
250{
251 int error = 0;
252
253 xfs_ilock(ip, XFS_ILOCK_SHARED);
254 if (xfs_inode_clean(ip))
255 goto out_unlock;
256 if (!xfs_iflock_nowait(ip)) {
257 if (!(flags & SYNC_WAIT))
258 goto out_unlock;
259 xfs_iflock(ip);
260 }
261
262 if (xfs_inode_clean(ip)) {
263 xfs_ifunlock(ip);
264 goto out_unlock;
265 }
266
Dave Chinnerc8543632010-02-06 12:39:36 +1100267 error = xfs_iflush(ip, flags);
Christoph Hellwig845b6d02009-06-08 15:35:05 +0200268
269 out_unlock:
270 xfs_iunlock(ip, XFS_ILOCK_SHARED);
271 return error;
272}
273
Christoph Hellwig075fe102009-06-08 15:35:48 +0200274/*
275 * Write out pagecache data for the whole filesystem.
276 */
Christoph Hellwig64c86142010-06-24 11:45:34 +1000277STATIC int
Christoph Hellwig075fe102009-06-08 15:35:48 +0200278xfs_sync_data(
279 struct xfs_mount *mp,
280 int flags)
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100281{
Christoph Hellwig075fe102009-06-08 15:35:48 +0200282 int error;
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100283
Christoph Hellwigb0710cc2009-06-08 15:37:11 +0200284 ASSERT((flags & ~(SYNC_TRYLOCK|SYNC_WAIT)) == 0);
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100285
Dave Chinner65d0f202010-09-24 18:40:15 +1000286 error = xfs_inode_ag_iterator(mp, xfs_sync_inode_data, flags);
Christoph Hellwig075fe102009-06-08 15:35:48 +0200287 if (error)
288 return XFS_ERROR(error);
David Chinnere9f1c6e2008-10-30 17:15:50 +1100289
Christoph Hellwiga14a3482010-01-19 09:56:46 +0000290 xfs_log_force(mp, (flags & SYNC_WAIT) ? XFS_LOG_SYNC : 0);
Christoph Hellwig075fe102009-06-08 15:35:48 +0200291 return 0;
292}
David Chinnere9f1c6e2008-10-30 17:15:50 +1100293
Christoph Hellwig075fe102009-06-08 15:35:48 +0200294/*
295 * Write out inode metadata (attributes) for the whole filesystem.
296 */
Christoph Hellwig64c86142010-06-24 11:45:34 +1000297STATIC int
Christoph Hellwig075fe102009-06-08 15:35:48 +0200298xfs_sync_attr(
299 struct xfs_mount *mp,
300 int flags)
301{
302 ASSERT((flags & ~SYNC_WAIT) == 0);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200303
Dave Chinner65d0f202010-09-24 18:40:15 +1000304 return xfs_inode_ag_iterator(mp, xfs_sync_inode_attr, flags);
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100305}
306
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100307STATIC int
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100308xfs_sync_fsdata(
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000309 struct xfs_mount *mp)
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100310{
311 struct xfs_buf *bp;
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100312
313 /*
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000314 * If the buffer is pinned then push on the log so we won't get stuck
315 * waiting in the write for someone, maybe ourselves, to flush the log.
316 *
317 * Even though we just pushed the log above, we did not have the
318 * superblock buffer locked at that point so it can become pinned in
319 * between there and here.
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100320 */
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000321 bp = xfs_getsb(mp, 0);
322 if (XFS_BUF_ISPINNED(bp))
323 xfs_log_force(mp, 0);
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100324
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000325 return xfs_bwrite(mp, bp);
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100326}
327
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100328/*
David Chinnera4e4c4f2008-10-30 17:16:11 +1100329 * When remounting a filesystem read-only or freezing the filesystem, we have
330 * two phases to execute. This first phase is syncing the data before we
331 * quiesce the filesystem, and the second is flushing all the inodes out after
332 * we've waited for all the transactions created by the first phase to
333 * complete. The second phase ensures that the inodes are written to their
334 * location on disk rather than just existing in transactions in the log. This
335 * means after a quiesce there is no log replay required to write the inodes to
336 * disk (this is the main difference between a sync and a quiesce).
337 */
338/*
339 * First stage of freeze - no writers will make progress now we are here,
David Chinnere9f1c6e2008-10-30 17:15:50 +1100340 * so we flush delwri and delalloc buffers here, then wait for all I/O to
341 * complete. Data is frozen at that point. Metadata is not frozen,
David Chinnera4e4c4f2008-10-30 17:16:11 +1100342 * transactions can still occur here so don't bother flushing the buftarg
343 * because it'll just get dirty again.
David Chinnere9f1c6e2008-10-30 17:15:50 +1100344 */
345int
346xfs_quiesce_data(
347 struct xfs_mount *mp)
348{
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000349 int error, error2 = 0;
David Chinnere9f1c6e2008-10-30 17:15:50 +1100350
351 /* push non-blocking */
Christoph Hellwig075fe102009-06-08 15:35:48 +0200352 xfs_sync_data(mp, 0);
Christoph Hellwig8b5403a2009-06-08 15:37:16 +0200353 xfs_qm_sync(mp, SYNC_TRYLOCK);
David Chinnere9f1c6e2008-10-30 17:15:50 +1100354
Dave Chinnerc90b07e2009-10-06 20:29:27 +0000355 /* push and block till complete */
Christoph Hellwigb0710cc2009-06-08 15:37:11 +0200356 xfs_sync_data(mp, SYNC_WAIT);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200357 xfs_qm_sync(mp, SYNC_WAIT);
David Chinnere9f1c6e2008-10-30 17:15:50 +1100358
David Chinnera4e4c4f2008-10-30 17:16:11 +1100359 /* write superblock and hoover up shutdown errors */
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000360 error = xfs_sync_fsdata(mp);
361
362 /* make sure all delwri buffers are written out */
363 xfs_flush_buftarg(mp->m_ddev_targp, 1);
364
365 /* mark the log as covered if needed */
366 if (xfs_log_need_covered(mp))
Dave Chinnerc58efdb2011-01-04 04:49:29 +0000367 error2 = xfs_fs_log_dummy(mp);
David Chinnere9f1c6e2008-10-30 17:15:50 +1100368
David Chinnera4e4c4f2008-10-30 17:16:11 +1100369 /* flush data-only devices */
David Chinnere9f1c6e2008-10-30 17:15:50 +1100370 if (mp->m_rtdev_targp)
371 XFS_bflush(mp->m_rtdev_targp);
372
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000373 return error ? error : error2;
David Chinnere9f1c6e2008-10-30 17:15:50 +1100374}
375
David Chinner76bf1052008-10-30 17:16:21 +1100376STATIC void
377xfs_quiesce_fs(
378 struct xfs_mount *mp)
379{
380 int count = 0, pincount;
381
Dave Chinnerc8543632010-02-06 12:39:36 +1100382 xfs_reclaim_inodes(mp, 0);
David Chinner76bf1052008-10-30 17:16:21 +1100383 xfs_flush_buftarg(mp->m_ddev_targp, 0);
David Chinner76bf1052008-10-30 17:16:21 +1100384
385 /*
386 * This loop must run at least twice. The first instance of the loop
387 * will flush most meta data but that will generate more meta data
388 * (typically directory updates). Which then must be flushed and
Dave Chinnerc8543632010-02-06 12:39:36 +1100389 * logged before we can write the unmount record. We also so sync
390 * reclaim of inodes to catch any that the above delwri flush skipped.
David Chinner76bf1052008-10-30 17:16:21 +1100391 */
392 do {
Dave Chinnerc8543632010-02-06 12:39:36 +1100393 xfs_reclaim_inodes(mp, SYNC_WAIT);
Christoph Hellwig075fe102009-06-08 15:35:48 +0200394 xfs_sync_attr(mp, SYNC_WAIT);
David Chinner76bf1052008-10-30 17:16:21 +1100395 pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1);
396 if (!pincount) {
397 delay(50);
398 count++;
399 }
400 } while (count < 2);
401}
402
403/*
404 * Second stage of a quiesce. The data is already synced, now we have to take
405 * care of the metadata. New transactions are already blocked, so we need to
406 * wait for any remaining transactions to drain out before proceding.
407 */
408void
409xfs_quiesce_attr(
410 struct xfs_mount *mp)
411{
412 int error = 0;
413
414 /* wait for all modifications to complete */
415 while (atomic_read(&mp->m_active_trans) > 0)
416 delay(100);
417
418 /* flush inodes and push all remaining buffers out to disk */
419 xfs_quiesce_fs(mp);
420
Felix Blyakher5e106572009-01-22 21:34:05 -0600421 /*
422 * Just warn here till VFS can correctly support
423 * read-only remount without racing.
424 */
425 WARN_ON(atomic_read(&mp->m_active_trans) != 0);
David Chinner76bf1052008-10-30 17:16:21 +1100426
427 /* Push the superblock and write an unmount record */
428 error = xfs_log_sbcount(mp, 1);
429 if (error)
Dave Chinner4f107002011-03-07 10:00:35 +1100430 xfs_warn(mp, "xfs_attr_quiesce: failed to log sb changes. "
David Chinner76bf1052008-10-30 17:16:21 +1100431 "Frozen image may not be consistent.");
432 xfs_log_unmount_write(mp);
433 xfs_unmountfs_writesb(mp);
434}
435
Dave Chinnerc6d09b62011-04-08 12:45:07 +1000436static void
437xfs_syncd_queue_sync(
438 struct xfs_mount *mp)
439{
440 queue_delayed_work(xfs_syncd_wq, &mp->m_sync_work,
441 msecs_to_jiffies(xfs_syncd_centisecs * 10));
442}
443
444/*
445 * Every sync period we need to unpin all items, reclaim inodes and sync
446 * disk quotas. We might need to cover the log to indicate that the
447 * filesystem is idle and not frozen.
448 */
449STATIC void
450xfs_sync_worker(
451 struct work_struct *work)
452{
453 struct xfs_mount *mp = container_of(to_delayed_work(work),
454 struct xfs_mount, m_sync_work);
455 int error;
456
457 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
458 /* dgc: errors ignored here */
459 if (mp->m_super->s_frozen == SB_UNFROZEN &&
460 xfs_log_need_covered(mp))
461 error = xfs_fs_log_dummy(mp);
462 else
463 xfs_log_force(mp, 0);
464 xfs_reclaim_inodes(mp, 0);
465 error = xfs_qm_sync(mp, SYNC_TRYLOCK);
466 }
467
468 /* queue us up again */
469 xfs_syncd_queue_sync(mp);
470}
471
Dave Chinner89e4cb52011-04-08 12:45:07 +1000472/*
473 * Flush delayed allocate data, attempting to free up reserved space
474 * from existing allocations. At this point a new allocation attempt
475 * has failed with ENOSPC and we are in the process of scratching our
476 * heads, looking about for more room.
477 *
478 * Queue a new data flush if there isn't one already in progress and
479 * wait for completion of the flush. This means that we only ever have one
480 * inode flush in progress no matter how many ENOSPC events are occurring and
481 * so will prevent the system from bogging down due to every concurrent
482 * ENOSPC event scanning all the active inodes in the system for writeback.
483 */
484void
485xfs_flush_inodes(
486 struct xfs_inode *ip)
487{
488 struct xfs_mount *mp = ip->i_mount;
489
490 queue_work(xfs_syncd_wq, &mp->m_flush_work);
491 flush_work_sync(&mp->m_flush_work);
492}
493
494STATIC void
495xfs_flush_worker(
496 struct work_struct *work)
497{
498 struct xfs_mount *mp = container_of(work,
499 struct xfs_mount, m_flush_work);
500
501 xfs_sync_data(mp, SYNC_TRYLOCK);
502 xfs_sync_data(mp, SYNC_TRYLOCK | SYNC_WAIT);
503}
504
David Chinnera167b172008-10-30 17:06:18 +1100505int
506xfs_syncd_init(
507 struct xfs_mount *mp)
508{
Dave Chinner89e4cb52011-04-08 12:45:07 +1000509 INIT_WORK(&mp->m_flush_work, xfs_flush_worker);
Dave Chinnerc6d09b62011-04-08 12:45:07 +1000510 INIT_DELAYED_WORK(&mp->m_sync_work, xfs_sync_worker);
511 xfs_syncd_queue_sync(mp);
512
David Chinnera167b172008-10-30 17:06:18 +1100513 return 0;
514}
515
516void
517xfs_syncd_stop(
518 struct xfs_mount *mp)
519{
Dave Chinnerc6d09b62011-04-08 12:45:07 +1000520 cancel_delayed_work_sync(&mp->m_sync_work);
Dave Chinner89e4cb52011-04-08 12:45:07 +1000521 cancel_work_sync(&mp->m_flush_work);
David Chinnera167b172008-10-30 17:06:18 +1100522}
523
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400524void
525__xfs_inode_set_reclaim_tag(
526 struct xfs_perag *pag,
527 struct xfs_inode *ip)
528{
529 radix_tree_tag_set(&pag->pag_ici_root,
530 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
531 XFS_ICI_RECLAIM_TAG);
Dave Chinner16fd5362010-07-20 09:43:39 +1000532
533 if (!pag->pag_ici_reclaimable) {
534 /* propagate the reclaim tag up into the perag radix tree */
535 spin_lock(&ip->i_mount->m_perag_lock);
536 radix_tree_tag_set(&ip->i_mount->m_perag_tree,
537 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
538 XFS_ICI_RECLAIM_TAG);
539 spin_unlock(&ip->i_mount->m_perag_lock);
540 trace_xfs_perag_set_reclaim(ip->i_mount, pag->pag_agno,
541 -1, _RET_IP_);
542 }
Dave Chinner9bf729c2010-04-29 09:55:50 +1000543 pag->pag_ici_reclaimable++;
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400544}
545
David Chinner11654512008-10-30 17:37:49 +1100546/*
547 * We set the inode flag atomically with the radix tree tag.
548 * Once we get tag lookups on the radix tree, this inode flag
549 * can go away.
550 */
David Chinner396beb82008-10-30 17:37:26 +1100551void
552xfs_inode_set_reclaim_tag(
553 xfs_inode_t *ip)
554{
Dave Chinner5017e972010-01-11 11:47:40 +0000555 struct xfs_mount *mp = ip->i_mount;
556 struct xfs_perag *pag;
David Chinner396beb82008-10-30 17:37:26 +1100557
Dave Chinner5017e972010-01-11 11:47:40 +0000558 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
Dave Chinner1a427ab2010-12-16 17:08:41 +1100559 spin_lock(&pag->pag_ici_lock);
David Chinner396beb82008-10-30 17:37:26 +1100560 spin_lock(&ip->i_flags_lock);
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400561 __xfs_inode_set_reclaim_tag(pag, ip);
David Chinner11654512008-10-30 17:37:49 +1100562 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
David Chinner396beb82008-10-30 17:37:26 +1100563 spin_unlock(&ip->i_flags_lock);
Dave Chinner1a427ab2010-12-16 17:08:41 +1100564 spin_unlock(&pag->pag_ici_lock);
Dave Chinner5017e972010-01-11 11:47:40 +0000565 xfs_perag_put(pag);
David Chinner396beb82008-10-30 17:37:26 +1100566}
567
Johannes Weiner081003f2010-10-01 07:43:54 +0000568STATIC void
569__xfs_inode_clear_reclaim(
David Chinner396beb82008-10-30 17:37:26 +1100570 xfs_perag_t *pag,
571 xfs_inode_t *ip)
572{
Dave Chinner9bf729c2010-04-29 09:55:50 +1000573 pag->pag_ici_reclaimable--;
Dave Chinner16fd5362010-07-20 09:43:39 +1000574 if (!pag->pag_ici_reclaimable) {
575 /* clear the reclaim tag from the perag radix tree */
576 spin_lock(&ip->i_mount->m_perag_lock);
577 radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
578 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
579 XFS_ICI_RECLAIM_TAG);
580 spin_unlock(&ip->i_mount->m_perag_lock);
581 trace_xfs_perag_clear_reclaim(ip->i_mount, pag->pag_agno,
582 -1, _RET_IP_);
583 }
David Chinner396beb82008-10-30 17:37:26 +1100584}
585
Johannes Weiner081003f2010-10-01 07:43:54 +0000586void
587__xfs_inode_clear_reclaim_tag(
588 xfs_mount_t *mp,
589 xfs_perag_t *pag,
590 xfs_inode_t *ip)
591{
592 radix_tree_tag_clear(&pag->pag_ici_root,
593 XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
594 __xfs_inode_clear_reclaim(pag, ip);
595}
596
Dave Chinner777df5a2010-02-06 12:37:26 +1100597/*
Dave Chinnere3a20c02010-09-24 19:51:50 +1000598 * Grab the inode for reclaim exclusively.
599 * Return 0 if we grabbed it, non-zero otherwise.
600 */
601STATIC int
602xfs_reclaim_inode_grab(
603 struct xfs_inode *ip,
604 int flags)
605{
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100606 ASSERT(rcu_read_lock_held());
607
608 /* quick check for stale RCU freed inode */
609 if (!ip->i_ino)
610 return 1;
Dave Chinnere3a20c02010-09-24 19:51:50 +1000611
612 /*
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100613 * do some unlocked checks first to avoid unnecessary lock traffic.
Dave Chinnere3a20c02010-09-24 19:51:50 +1000614 * The first is a flush lock check, the second is a already in reclaim
615 * check. Only do these checks if we are not going to block on locks.
616 */
617 if ((flags & SYNC_TRYLOCK) &&
618 (!ip->i_flush.done || __xfs_iflags_test(ip, XFS_IRECLAIM))) {
619 return 1;
620 }
621
622 /*
623 * The radix tree lock here protects a thread in xfs_iget from racing
624 * with us starting reclaim on the inode. Once we have the
625 * XFS_IRECLAIM flag set it will not touch us.
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100626 *
627 * Due to RCU lookup, we may find inodes that have been freed and only
628 * have XFS_IRECLAIM set. Indeed, we may see reallocated inodes that
629 * aren't candidates for reclaim at all, so we must check the
630 * XFS_IRECLAIMABLE is set first before proceeding to reclaim.
Dave Chinnere3a20c02010-09-24 19:51:50 +1000631 */
632 spin_lock(&ip->i_flags_lock);
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100633 if (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) ||
634 __xfs_iflags_test(ip, XFS_IRECLAIM)) {
635 /* not a reclaim candidate. */
Dave Chinnere3a20c02010-09-24 19:51:50 +1000636 spin_unlock(&ip->i_flags_lock);
637 return 1;
638 }
639 __xfs_iflags_set(ip, XFS_IRECLAIM);
640 spin_unlock(&ip->i_flags_lock);
641 return 0;
642}
643
644/*
Dave Chinner777df5a2010-02-06 12:37:26 +1100645 * Inodes in different states need to be treated differently, and the return
646 * value of xfs_iflush is not sufficient to get this right. The following table
647 * lists the inode states and the reclaim actions necessary for non-blocking
648 * reclaim:
649 *
650 *
651 * inode state iflush ret required action
652 * --------------- ---------- ---------------
653 * bad - reclaim
654 * shutdown EIO unpin and reclaim
655 * clean, unpinned 0 reclaim
656 * stale, unpinned 0 reclaim
Dave Chinnerc8543632010-02-06 12:39:36 +1100657 * clean, pinned(*) 0 requeue
658 * stale, pinned EAGAIN requeue
659 * dirty, delwri ok 0 requeue
660 * dirty, delwri blocked EAGAIN requeue
661 * dirty, sync flush 0 reclaim
Dave Chinner777df5a2010-02-06 12:37:26 +1100662 *
663 * (*) dgc: I don't think the clean, pinned state is possible but it gets
664 * handled anyway given the order of checks implemented.
665 *
Dave Chinnerc8543632010-02-06 12:39:36 +1100666 * As can be seen from the table, the return value of xfs_iflush() is not
667 * sufficient to correctly decide the reclaim action here. The checks in
668 * xfs_iflush() might look like duplicates, but they are not.
669 *
670 * Also, because we get the flush lock first, we know that any inode that has
671 * been flushed delwri has had the flush completed by the time we check that
672 * the inode is clean. The clean inode check needs to be done before flushing
673 * the inode delwri otherwise we would loop forever requeuing clean inodes as
674 * we cannot tell apart a successful delwri flush and a clean inode from the
675 * return value of xfs_iflush().
676 *
677 * Note that because the inode is flushed delayed write by background
678 * writeback, the flush lock may already be held here and waiting on it can
679 * result in very long latencies. Hence for sync reclaims, where we wait on the
680 * flush lock, the caller should push out delayed write inodes first before
681 * trying to reclaim them to minimise the amount of time spent waiting. For
682 * background relaim, we just requeue the inode for the next pass.
683 *
Dave Chinner777df5a2010-02-06 12:37:26 +1100684 * Hence the order of actions after gaining the locks should be:
685 * bad => reclaim
686 * shutdown => unpin and reclaim
Dave Chinnerc8543632010-02-06 12:39:36 +1100687 * pinned, delwri => requeue
688 * pinned, sync => unpin
Dave Chinner777df5a2010-02-06 12:37:26 +1100689 * stale => reclaim
690 * clean => reclaim
Dave Chinnerc8543632010-02-06 12:39:36 +1100691 * dirty, delwri => flush and requeue
692 * dirty, sync => flush, wait and reclaim
Dave Chinner777df5a2010-02-06 12:37:26 +1100693 */
Dave Chinner75f3cb12009-06-08 15:35:14 +0200694STATIC int
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000695xfs_reclaim_inode(
Dave Chinner75f3cb12009-06-08 15:35:14 +0200696 struct xfs_inode *ip,
697 struct xfs_perag *pag,
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000698 int sync_mode)
David Chinner7a3be022008-10-30 17:37:37 +1100699{
Dave Chinner1bfd8d02011-03-26 09:13:55 +1100700 int error;
Dave Chinner777df5a2010-02-06 12:37:26 +1100701
Dave Chinner1bfd8d02011-03-26 09:13:55 +1100702restart:
703 error = 0;
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000704 xfs_ilock(ip, XFS_ILOCK_EXCL);
Dave Chinnerc8543632010-02-06 12:39:36 +1100705 if (!xfs_iflock_nowait(ip)) {
706 if (!(sync_mode & SYNC_WAIT))
707 goto out;
708 xfs_iflock(ip);
709 }
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000710
Dave Chinner777df5a2010-02-06 12:37:26 +1100711 if (is_bad_inode(VFS_I(ip)))
712 goto reclaim;
713 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
714 xfs_iunpin_wait(ip);
715 goto reclaim;
716 }
Dave Chinnerc8543632010-02-06 12:39:36 +1100717 if (xfs_ipincount(ip)) {
718 if (!(sync_mode & SYNC_WAIT)) {
719 xfs_ifunlock(ip);
720 goto out;
721 }
Dave Chinner777df5a2010-02-06 12:37:26 +1100722 xfs_iunpin_wait(ip);
Dave Chinnerc8543632010-02-06 12:39:36 +1100723 }
Dave Chinner777df5a2010-02-06 12:37:26 +1100724 if (xfs_iflags_test(ip, XFS_ISTALE))
725 goto reclaim;
726 if (xfs_inode_clean(ip))
727 goto reclaim;
728
Dave Chinner1bfd8d02011-03-26 09:13:55 +1100729 /*
730 * Now we have an inode that needs flushing.
731 *
732 * We do a nonblocking flush here even if we are doing a SYNC_WAIT
733 * reclaim as we can deadlock with inode cluster removal.
734 * xfs_ifree_cluster() can lock the inode buffer before it locks the
735 * ip->i_lock, and we are doing the exact opposite here. As a result,
736 * doing a blocking xfs_itobp() to get the cluster buffer will result
737 * in an ABBA deadlock with xfs_ifree_cluster().
738 *
739 * As xfs_ifree_cluser() must gather all inodes that are active in the
740 * cache to mark them stale, if we hit this case we don't actually want
741 * to do IO here - we want the inode marked stale so we can simply
742 * reclaim it. Hence if we get an EAGAIN error on a SYNC_WAIT flush,
743 * just unlock the inode, back off and try again. Hopefully the next
744 * pass through will see the stale flag set on the inode.
745 */
746 error = xfs_iflush(ip, SYNC_TRYLOCK | sync_mode);
Dave Chinnerc8543632010-02-06 12:39:36 +1100747 if (sync_mode & SYNC_WAIT) {
Dave Chinner1bfd8d02011-03-26 09:13:55 +1100748 if (error == EAGAIN) {
749 xfs_iunlock(ip, XFS_ILOCK_EXCL);
750 /* backoff longer than in xfs_ifree_cluster */
751 delay(2);
752 goto restart;
753 }
Dave Chinnerc8543632010-02-06 12:39:36 +1100754 xfs_iflock(ip);
755 goto reclaim;
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000756 }
757
Dave Chinnerc8543632010-02-06 12:39:36 +1100758 /*
759 * When we have to flush an inode but don't have SYNC_WAIT set, we
760 * flush the inode out using a delwri buffer and wait for the next
761 * call into reclaim to find it in a clean state instead of waiting for
762 * it now. We also don't return errors here - if the error is transient
763 * then the next reclaim pass will flush the inode, and if the error
Dave Chinnerf1d486a2010-04-13 15:06:45 +1000764 * is permanent then the next sync reclaim will reclaim the inode and
Dave Chinnerc8543632010-02-06 12:39:36 +1100765 * pass on the error.
766 */
Dave Chinnerf1d486a2010-04-13 15:06:45 +1000767 if (error && error != EAGAIN && !XFS_FORCED_SHUTDOWN(ip->i_mount)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100768 xfs_warn(ip->i_mount,
Dave Chinnerc8543632010-02-06 12:39:36 +1100769 "inode 0x%llx background reclaim flush failed with %d",
770 (long long)ip->i_ino, error);
771 }
772out:
773 xfs_iflags_clear(ip, XFS_IRECLAIM);
774 xfs_iunlock(ip, XFS_ILOCK_EXCL);
775 /*
776 * We could return EAGAIN here to make reclaim rescan the inode tree in
777 * a short while. However, this just burns CPU time scanning the tree
778 * waiting for IO to complete and xfssyncd never goes back to the idle
779 * state. Instead, return 0 to let the next scheduled background reclaim
780 * attempt to reclaim the inode again.
781 */
782 return 0;
783
Dave Chinner777df5a2010-02-06 12:37:26 +1100784reclaim:
785 xfs_ifunlock(ip);
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000786 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner2f11fea2010-07-20 17:53:25 +1000787
788 XFS_STATS_INC(xs_ig_reclaims);
789 /*
790 * Remove the inode from the per-AG radix tree.
791 *
792 * Because radix_tree_delete won't complain even if the item was never
793 * added to the tree assert that it's been there before to catch
794 * problems with the inode life time early on.
795 */
Dave Chinner1a427ab2010-12-16 17:08:41 +1100796 spin_lock(&pag->pag_ici_lock);
Dave Chinner2f11fea2010-07-20 17:53:25 +1000797 if (!radix_tree_delete(&pag->pag_ici_root,
798 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino)))
799 ASSERT(0);
Johannes Weiner081003f2010-10-01 07:43:54 +0000800 __xfs_inode_clear_reclaim(pag, ip);
Dave Chinner1a427ab2010-12-16 17:08:41 +1100801 spin_unlock(&pag->pag_ici_lock);
Dave Chinner2f11fea2010-07-20 17:53:25 +1000802
803 /*
804 * Here we do an (almost) spurious inode lock in order to coordinate
805 * with inode cache radix tree lookups. This is because the lookup
806 * can reference the inodes in the cache without taking references.
807 *
808 * We make that OK here by ensuring that we wait until the inode is
809 * unlocked after the lookup before we go ahead and free it. We get
810 * both the ilock and the iolock because the code may need to drop the
811 * ilock one but will still hold the iolock.
812 */
813 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
814 xfs_qm_dqdetach(ip);
815 xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
816
817 xfs_inode_free(ip);
Dave Chinnerc8543632010-02-06 12:39:36 +1100818 return error;
819
David Chinner7a3be022008-10-30 17:37:37 +1100820}
821
Dave Chinner65d0f202010-09-24 18:40:15 +1000822/*
823 * Walk the AGs and reclaim the inodes in them. Even if the filesystem is
824 * corrupted, we still want to try to reclaim all the inodes. If we don't,
825 * then a shut down during filesystem unmount reclaim walk leak all the
826 * unreclaimed inodes.
827 */
828int
829xfs_reclaim_inodes_ag(
830 struct xfs_mount *mp,
831 int flags,
832 int *nr_to_scan)
833{
834 struct xfs_perag *pag;
835 int error = 0;
836 int last_error = 0;
837 xfs_agnumber_t ag;
Dave Chinner69b491c2010-09-27 11:09:51 +1000838 int trylock = flags & SYNC_TRYLOCK;
839 int skipped;
Dave Chinner65d0f202010-09-24 18:40:15 +1000840
Dave Chinner69b491c2010-09-27 11:09:51 +1000841restart:
Dave Chinner65d0f202010-09-24 18:40:15 +1000842 ag = 0;
Dave Chinner69b491c2010-09-27 11:09:51 +1000843 skipped = 0;
Dave Chinner65d0f202010-09-24 18:40:15 +1000844 while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
845 unsigned long first_index = 0;
846 int done = 0;
Dave Chinnere3a20c02010-09-24 19:51:50 +1000847 int nr_found = 0;
Dave Chinner65d0f202010-09-24 18:40:15 +1000848
849 ag = pag->pag_agno + 1;
850
Dave Chinner69b491c2010-09-27 11:09:51 +1000851 if (trylock) {
852 if (!mutex_trylock(&pag->pag_ici_reclaim_lock)) {
853 skipped++;
Dave Chinnerf83282a2010-11-08 08:55:04 +0000854 xfs_perag_put(pag);
Dave Chinner69b491c2010-09-27 11:09:51 +1000855 continue;
856 }
857 first_index = pag->pag_ici_reclaim_cursor;
858 } else
859 mutex_lock(&pag->pag_ici_reclaim_lock);
860
Dave Chinner65d0f202010-09-24 18:40:15 +1000861 do {
Dave Chinnere3a20c02010-09-24 19:51:50 +1000862 struct xfs_inode *batch[XFS_LOOKUP_BATCH];
863 int i;
Dave Chinner65d0f202010-09-24 18:40:15 +1000864
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100865 rcu_read_lock();
Dave Chinnere3a20c02010-09-24 19:51:50 +1000866 nr_found = radix_tree_gang_lookup_tag(
867 &pag->pag_ici_root,
868 (void **)batch, first_index,
869 XFS_LOOKUP_BATCH,
Dave Chinner65d0f202010-09-24 18:40:15 +1000870 XFS_ICI_RECLAIM_TAG);
871 if (!nr_found) {
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100872 rcu_read_unlock();
Dave Chinner65d0f202010-09-24 18:40:15 +1000873 break;
874 }
875
876 /*
Dave Chinnere3a20c02010-09-24 19:51:50 +1000877 * Grab the inodes before we drop the lock. if we found
878 * nothing, nr == 0 and the loop will be skipped.
Dave Chinner65d0f202010-09-24 18:40:15 +1000879 */
Dave Chinnere3a20c02010-09-24 19:51:50 +1000880 for (i = 0; i < nr_found; i++) {
881 struct xfs_inode *ip = batch[i];
Dave Chinner65d0f202010-09-24 18:40:15 +1000882
Dave Chinnere3a20c02010-09-24 19:51:50 +1000883 if (done || xfs_reclaim_inode_grab(ip, flags))
884 batch[i] = NULL;
Dave Chinner65d0f202010-09-24 18:40:15 +1000885
Dave Chinnere3a20c02010-09-24 19:51:50 +1000886 /*
887 * Update the index for the next lookup. Catch
888 * overflows into the next AG range which can
889 * occur if we have inodes in the last block of
890 * the AG and we are currently pointing to the
891 * last inode.
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100892 *
893 * Because we may see inodes that are from the
894 * wrong AG due to RCU freeing and
895 * reallocation, only update the index if it
896 * lies in this AG. It was a race that lead us
897 * to see this inode, so another lookup from
898 * the same index will not find it again.
Dave Chinnere3a20c02010-09-24 19:51:50 +1000899 */
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100900 if (XFS_INO_TO_AGNO(mp, ip->i_ino) !=
901 pag->pag_agno)
902 continue;
Dave Chinnere3a20c02010-09-24 19:51:50 +1000903 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
904 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
905 done = 1;
906 }
907
908 /* unlock now we've grabbed the inodes. */
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100909 rcu_read_unlock();
Dave Chinnere3a20c02010-09-24 19:51:50 +1000910
911 for (i = 0; i < nr_found; i++) {
912 if (!batch[i])
913 continue;
914 error = xfs_reclaim_inode(batch[i], pag, flags);
915 if (error && last_error != EFSCORRUPTED)
916 last_error = error;
917 }
918
919 *nr_to_scan -= XFS_LOOKUP_BATCH;
920
921 } while (nr_found && !done && *nr_to_scan > 0);
Dave Chinner65d0f202010-09-24 18:40:15 +1000922
Dave Chinner69b491c2010-09-27 11:09:51 +1000923 if (trylock && !done)
924 pag->pag_ici_reclaim_cursor = first_index;
925 else
926 pag->pag_ici_reclaim_cursor = 0;
927 mutex_unlock(&pag->pag_ici_reclaim_lock);
Dave Chinner65d0f202010-09-24 18:40:15 +1000928 xfs_perag_put(pag);
929 }
Dave Chinner69b491c2010-09-27 11:09:51 +1000930
931 /*
932 * if we skipped any AG, and we still have scan count remaining, do
933 * another pass this time using blocking reclaim semantics (i.e
934 * waiting on the reclaim locks and ignoring the reclaim cursors). This
935 * ensure that when we get more reclaimers than AGs we block rather
936 * than spin trying to execute reclaim.
937 */
938 if (trylock && skipped && *nr_to_scan > 0) {
939 trylock = 0;
940 goto restart;
941 }
Dave Chinner65d0f202010-09-24 18:40:15 +1000942 return XFS_ERROR(last_error);
943}
944
David Chinnerfce08f22008-10-30 17:37:03 +1100945int
David Chinner1dc33182008-10-30 17:37:15 +1100946xfs_reclaim_inodes(
David Chinnerfce08f22008-10-30 17:37:03 +1100947 xfs_mount_t *mp,
David Chinnerfce08f22008-10-30 17:37:03 +1100948 int mode)
949{
Dave Chinner65d0f202010-09-24 18:40:15 +1000950 int nr_to_scan = INT_MAX;
951
952 return xfs_reclaim_inodes_ag(mp, mode, &nr_to_scan);
Dave Chinner9bf729c2010-04-29 09:55:50 +1000953}
954
955/*
956 * Shrinker infrastructure.
Dave Chinner9bf729c2010-04-29 09:55:50 +1000957 */
Dave Chinner9bf729c2010-04-29 09:55:50 +1000958static int
959xfs_reclaim_inode_shrink(
Dave Chinner7f8275d2010-07-19 14:56:17 +1000960 struct shrinker *shrink,
Dave Chinner9bf729c2010-04-29 09:55:50 +1000961 int nr_to_scan,
962 gfp_t gfp_mask)
963{
964 struct xfs_mount *mp;
965 struct xfs_perag *pag;
966 xfs_agnumber_t ag;
Dave Chinner16fd5362010-07-20 09:43:39 +1000967 int reclaimable;
Dave Chinner9bf729c2010-04-29 09:55:50 +1000968
Dave Chinner70e60ce2010-07-20 08:07:02 +1000969 mp = container_of(shrink, struct xfs_mount, m_inode_shrink);
Dave Chinner9bf729c2010-04-29 09:55:50 +1000970 if (nr_to_scan) {
971 if (!(gfp_mask & __GFP_FS))
972 return -1;
973
Dave Chinnere3a20c02010-09-24 19:51:50 +1000974 xfs_reclaim_inodes_ag(mp, SYNC_TRYLOCK, &nr_to_scan);
Dave Chinner65d0f202010-09-24 18:40:15 +1000975 /* terminate if we don't exhaust the scan */
Dave Chinner70e60ce2010-07-20 08:07:02 +1000976 if (nr_to_scan > 0)
977 return -1;
978 }
Dave Chinner9bf729c2010-04-29 09:55:50 +1000979
Dave Chinner16fd5362010-07-20 09:43:39 +1000980 reclaimable = 0;
981 ag = 0;
Dave Chinner65d0f202010-09-24 18:40:15 +1000982 while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
983 ag = pag->pag_agno + 1;
Dave Chinner70e60ce2010-07-20 08:07:02 +1000984 reclaimable += pag->pag_ici_reclaimable;
985 xfs_perag_put(pag);
Dave Chinner9bf729c2010-04-29 09:55:50 +1000986 }
Dave Chinner9bf729c2010-04-29 09:55:50 +1000987 return reclaimable;
988}
989
Dave Chinner9bf729c2010-04-29 09:55:50 +1000990void
991xfs_inode_shrinker_register(
992 struct xfs_mount *mp)
993{
Dave Chinner70e60ce2010-07-20 08:07:02 +1000994 mp->m_inode_shrink.shrink = xfs_reclaim_inode_shrink;
995 mp->m_inode_shrink.seeks = DEFAULT_SEEKS;
996 register_shrinker(&mp->m_inode_shrink);
Dave Chinner9bf729c2010-04-29 09:55:50 +1000997}
998
999void
1000xfs_inode_shrinker_unregister(
1001 struct xfs_mount *mp)
1002{
Dave Chinner70e60ce2010-07-20 08:07:02 +10001003 unregister_shrinker(&mp->m_inode_shrink);
David Chinnerfce08f22008-10-30 17:37:03 +11001004}