xfs: split inode AG walking into separate code for reclaim
[linux-block.git] / fs / xfs / linux-2.6 / xfs_sync.c
CommitLineData
fe4fa4b8
DC
1/*
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"
fe4fa4b8
DC
27#include "xfs_mount.h"
28#include "xfs_bmap_btree.h"
fe4fa4b8
DC
29#include "xfs_inode.h"
30#include "xfs_dinode.h"
31#include "xfs_error.h"
fe4fa4b8
DC
32#include "xfs_filestream.h"
33#include "xfs_vnodeops.h"
fe4fa4b8 34#include "xfs_inode_item.h"
7d095257 35#include "xfs_quota.h"
0b1b213f 36#include "xfs_trace.h"
1a387d3b 37#include "xfs_fsops.h"
fe4fa4b8 38
a167b17e
DC
39#include <linux/kthread.h>
40#include <linux/freezer.h>
41
5a34d5cd 42
75f3cb13
DC
43STATIC int
44xfs_inode_ag_walk(
45 struct xfs_mount *mp,
5017e97d 46 struct xfs_perag *pag,
75f3cb13
DC
47 int (*execute)(struct xfs_inode *ip,
48 struct xfs_perag *pag, int flags),
65d0f205 49 int flags)
75f3cb13 50{
75f3cb13
DC
51 uint32_t first_index;
52 int last_error = 0;
53 int skipped;
65d0f205 54 int done;
75f3cb13
DC
55
56restart:
65d0f205 57 done = 0;
75f3cb13
DC
58 skipped = 0;
59 first_index = 0;
60 do {
61 int error = 0;
65d0f205 62 int nr_found;
75f3cb13
DC
63 xfs_inode_t *ip;
64
65d0f205
DC
65 read_lock(&pag->pag_ici_lock);
66 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
67 (void **)&ip, first_index, 1);
68 if (!nr_found) {
69 read_unlock(&pag->pag_ici_lock);
75f3cb13 70 break;
c8e20be0 71 }
75f3cb13 72
65d0f205
DC
73 /*
74 * Update the index for the next lookup. Catch overflows
75 * into the next AG range which can occur if we have inodes
76 * in the last block of the AG and we are currently
77 * pointing to the last inode.
78 */
79 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
80 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
81 done = 1;
82
c8e20be0 83 /* execute releases pag->pag_ici_lock */
75f3cb13
DC
84 error = execute(ip, pag, flags);
85 if (error == EAGAIN) {
86 skipped++;
87 continue;
88 }
89 if (error)
90 last_error = error;
c8e20be0
DC
91
92 /* bail out if the filesystem is corrupted. */
75f3cb13
DC
93 if (error == EFSCORRUPTED)
94 break;
95
65d0f205 96 } while (!done);
75f3cb13
DC
97
98 if (skipped) {
99 delay(1);
100 goto restart;
101 }
75f3cb13
DC
102 return last_error;
103}
104
fe588ed3 105int
75f3cb13
DC
106xfs_inode_ag_iterator(
107 struct xfs_mount *mp,
108 int (*execute)(struct xfs_inode *ip,
109 struct xfs_perag *pag, int flags),
65d0f205 110 int flags)
75f3cb13 111{
16fd5367 112 struct xfs_perag *pag;
75f3cb13
DC
113 int error = 0;
114 int last_error = 0;
115 xfs_agnumber_t ag;
116
16fd5367 117 ag = 0;
65d0f205
DC
118 while ((pag = xfs_perag_get(mp, ag))) {
119 ag = pag->pag_agno + 1;
120 error = xfs_inode_ag_walk(mp, pag, execute, flags);
5017e97d 121 xfs_perag_put(pag);
75f3cb13
DC
122 if (error) {
123 last_error = error;
124 if (error == EFSCORRUPTED)
125 break;
126 }
127 }
128 return XFS_ERROR(last_error);
129}
130
1da8eeca 131/* must be called with pag_ici_lock held and releases it */
fe588ed3 132int
1da8eeca
DC
133xfs_sync_inode_valid(
134 struct xfs_inode *ip,
135 struct xfs_perag *pag)
136{
137 struct inode *inode = VFS_I(ip);
018027be 138 int error = EFSCORRUPTED;
1da8eeca
DC
139
140 /* nothing to sync during shutdown */
018027be
DC
141 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
142 goto out_unlock;
1da8eeca 143
018027be
DC
144 /* avoid new or reclaimable inodes. Leave for reclaim code to flush */
145 error = ENOENT;
146 if (xfs_iflags_test(ip, XFS_INEW | XFS_IRECLAIMABLE | XFS_IRECLAIM))
147 goto out_unlock;
1da8eeca 148
018027be
DC
149 /* If we can't grab the inode, it must on it's way to reclaim. */
150 if (!igrab(inode))
151 goto out_unlock;
152
153 if (is_bad_inode(inode)) {
1da8eeca 154 IRELE(ip);
018027be 155 goto out_unlock;
1da8eeca
DC
156 }
157
018027be
DC
158 /* inode is valid */
159 error = 0;
160out_unlock:
161 read_unlock(&pag->pag_ici_lock);
162 return error;
1da8eeca
DC
163}
164
5a34d5cd
DC
165STATIC int
166xfs_sync_inode_data(
167 struct xfs_inode *ip,
75f3cb13 168 struct xfs_perag *pag,
5a34d5cd
DC
169 int flags)
170{
171 struct inode *inode = VFS_I(ip);
172 struct address_space *mapping = inode->i_mapping;
173 int error = 0;
174
75f3cb13
DC
175 error = xfs_sync_inode_valid(ip, pag);
176 if (error)
177 return error;
178
5a34d5cd
DC
179 if (!mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
180 goto out_wait;
181
182 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED)) {
183 if (flags & SYNC_TRYLOCK)
184 goto out_wait;
185 xfs_ilock(ip, XFS_IOLOCK_SHARED);
186 }
187
188 error = xfs_flush_pages(ip, 0, -1, (flags & SYNC_WAIT) ?
0cadda1c 189 0 : XBF_ASYNC, FI_NONE);
5a34d5cd
DC
190 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
191
192 out_wait:
b0710ccc 193 if (flags & SYNC_WAIT)
5a34d5cd 194 xfs_ioend_wait(ip);
75f3cb13 195 IRELE(ip);
5a34d5cd
DC
196 return error;
197}
198
845b6d0c
CH
199STATIC int
200xfs_sync_inode_attr(
201 struct xfs_inode *ip,
75f3cb13 202 struct xfs_perag *pag,
845b6d0c
CH
203 int flags)
204{
205 int error = 0;
206
75f3cb13
DC
207 error = xfs_sync_inode_valid(ip, pag);
208 if (error)
209 return error;
210
845b6d0c
CH
211 xfs_ilock(ip, XFS_ILOCK_SHARED);
212 if (xfs_inode_clean(ip))
213 goto out_unlock;
214 if (!xfs_iflock_nowait(ip)) {
215 if (!(flags & SYNC_WAIT))
216 goto out_unlock;
217 xfs_iflock(ip);
218 }
219
220 if (xfs_inode_clean(ip)) {
221 xfs_ifunlock(ip);
222 goto out_unlock;
223 }
224
c854363e 225 error = xfs_iflush(ip, flags);
845b6d0c
CH
226
227 out_unlock:
228 xfs_iunlock(ip, XFS_ILOCK_SHARED);
75f3cb13 229 IRELE(ip);
845b6d0c
CH
230 return error;
231}
232
075fe102
CH
233/*
234 * Write out pagecache data for the whole filesystem.
235 */
64c86149 236STATIC int
075fe102
CH
237xfs_sync_data(
238 struct xfs_mount *mp,
239 int flags)
683a8970 240{
075fe102 241 int error;
fe4fa4b8 242
b0710ccc 243 ASSERT((flags & ~(SYNC_TRYLOCK|SYNC_WAIT)) == 0);
fe4fa4b8 244
65d0f205 245 error = xfs_inode_ag_iterator(mp, xfs_sync_inode_data, flags);
075fe102
CH
246 if (error)
247 return XFS_ERROR(error);
e9f1c6ee 248
a14a348b 249 xfs_log_force(mp, (flags & SYNC_WAIT) ? XFS_LOG_SYNC : 0);
075fe102
CH
250 return 0;
251}
e9f1c6ee 252
075fe102
CH
253/*
254 * Write out inode metadata (attributes) for the whole filesystem.
255 */
64c86149 256STATIC int
075fe102
CH
257xfs_sync_attr(
258 struct xfs_mount *mp,
259 int flags)
260{
261 ASSERT((flags & ~SYNC_WAIT) == 0);
75f3cb13 262
65d0f205 263 return xfs_inode_ag_iterator(mp, xfs_sync_inode_attr, flags);
fe4fa4b8
DC
264}
265
5d77c0dc 266STATIC int
2af75df7 267xfs_sync_fsdata(
df308bcf 268 struct xfs_mount *mp)
2af75df7
CH
269{
270 struct xfs_buf *bp;
2af75df7
CH
271
272 /*
df308bcf
CH
273 * If the buffer is pinned then push on the log so we won't get stuck
274 * waiting in the write for someone, maybe ourselves, to flush the log.
275 *
276 * Even though we just pushed the log above, we did not have the
277 * superblock buffer locked at that point so it can become pinned in
278 * between there and here.
2af75df7 279 */
df308bcf
CH
280 bp = xfs_getsb(mp, 0);
281 if (XFS_BUF_ISPINNED(bp))
282 xfs_log_force(mp, 0);
2af75df7 283
df308bcf 284 return xfs_bwrite(mp, bp);
e9f1c6ee
DC
285}
286
287/*
a4e4c4f4
DC
288 * When remounting a filesystem read-only or freezing the filesystem, we have
289 * two phases to execute. This first phase is syncing the data before we
290 * quiesce the filesystem, and the second is flushing all the inodes out after
291 * we've waited for all the transactions created by the first phase to
292 * complete. The second phase ensures that the inodes are written to their
293 * location on disk rather than just existing in transactions in the log. This
294 * means after a quiesce there is no log replay required to write the inodes to
295 * disk (this is the main difference between a sync and a quiesce).
296 */
297/*
298 * First stage of freeze - no writers will make progress now we are here,
e9f1c6ee
DC
299 * so we flush delwri and delalloc buffers here, then wait for all I/O to
300 * complete. Data is frozen at that point. Metadata is not frozen,
a4e4c4f4
DC
301 * transactions can still occur here so don't bother flushing the buftarg
302 * because it'll just get dirty again.
e9f1c6ee
DC
303 */
304int
305xfs_quiesce_data(
306 struct xfs_mount *mp)
307{
df308bcf 308 int error, error2 = 0;
e9f1c6ee
DC
309
310 /* push non-blocking */
075fe102 311 xfs_sync_data(mp, 0);
8b5403a6 312 xfs_qm_sync(mp, SYNC_TRYLOCK);
e9f1c6ee 313
c90b07e8 314 /* push and block till complete */
b0710ccc 315 xfs_sync_data(mp, SYNC_WAIT);
7d095257 316 xfs_qm_sync(mp, SYNC_WAIT);
e9f1c6ee 317
a4e4c4f4 318 /* write superblock and hoover up shutdown errors */
df308bcf
CH
319 error = xfs_sync_fsdata(mp);
320
321 /* make sure all delwri buffers are written out */
322 xfs_flush_buftarg(mp->m_ddev_targp, 1);
323
324 /* mark the log as covered if needed */
325 if (xfs_log_need_covered(mp))
1a387d3b 326 error2 = xfs_fs_log_dummy(mp, SYNC_WAIT);
e9f1c6ee 327
a4e4c4f4 328 /* flush data-only devices */
e9f1c6ee
DC
329 if (mp->m_rtdev_targp)
330 XFS_bflush(mp->m_rtdev_targp);
331
df308bcf 332 return error ? error : error2;
2af75df7
CH
333}
334
76bf105c
DC
335STATIC void
336xfs_quiesce_fs(
337 struct xfs_mount *mp)
338{
339 int count = 0, pincount;
340
c854363e 341 xfs_reclaim_inodes(mp, 0);
76bf105c 342 xfs_flush_buftarg(mp->m_ddev_targp, 0);
76bf105c
DC
343
344 /*
345 * This loop must run at least twice. The first instance of the loop
346 * will flush most meta data but that will generate more meta data
347 * (typically directory updates). Which then must be flushed and
c854363e
DC
348 * logged before we can write the unmount record. We also so sync
349 * reclaim of inodes to catch any that the above delwri flush skipped.
76bf105c
DC
350 */
351 do {
c854363e 352 xfs_reclaim_inodes(mp, SYNC_WAIT);
075fe102 353 xfs_sync_attr(mp, SYNC_WAIT);
76bf105c
DC
354 pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1);
355 if (!pincount) {
356 delay(50);
357 count++;
358 }
359 } while (count < 2);
360}
361
362/*
363 * Second stage of a quiesce. The data is already synced, now we have to take
364 * care of the metadata. New transactions are already blocked, so we need to
365 * wait for any remaining transactions to drain out before proceding.
366 */
367void
368xfs_quiesce_attr(
369 struct xfs_mount *mp)
370{
371 int error = 0;
372
373 /* wait for all modifications to complete */
374 while (atomic_read(&mp->m_active_trans) > 0)
375 delay(100);
376
377 /* flush inodes and push all remaining buffers out to disk */
378 xfs_quiesce_fs(mp);
379
5e106572
FB
380 /*
381 * Just warn here till VFS can correctly support
382 * read-only remount without racing.
383 */
384 WARN_ON(atomic_read(&mp->m_active_trans) != 0);
76bf105c
DC
385
386 /* Push the superblock and write an unmount record */
387 error = xfs_log_sbcount(mp, 1);
388 if (error)
389 xfs_fs_cmn_err(CE_WARN, mp,
390 "xfs_attr_quiesce: failed to log sb changes. "
391 "Frozen image may not be consistent.");
392 xfs_log_unmount_write(mp);
393 xfs_unmountfs_writesb(mp);
394}
395
a167b17e
DC
396/*
397 * Enqueue a work item to be picked up by the vfs xfssyncd thread.
398 * Doing this has two advantages:
399 * - It saves on stack space, which is tight in certain situations
400 * - It can be used (with care) as a mechanism to avoid deadlocks.
401 * Flushing while allocating in a full filesystem requires both.
402 */
403STATIC void
404xfs_syncd_queue_work(
405 struct xfs_mount *mp,
406 void *data,
e43afd72
DC
407 void (*syncer)(struct xfs_mount *, void *),
408 struct completion *completion)
a167b17e 409{
a8d770d9 410 struct xfs_sync_work *work;
a167b17e 411
a8d770d9 412 work = kmem_alloc(sizeof(struct xfs_sync_work), KM_SLEEP);
a167b17e
DC
413 INIT_LIST_HEAD(&work->w_list);
414 work->w_syncer = syncer;
415 work->w_data = data;
416 work->w_mount = mp;
e43afd72 417 work->w_completion = completion;
a167b17e
DC
418 spin_lock(&mp->m_sync_lock);
419 list_add_tail(&work->w_list, &mp->m_sync_list);
420 spin_unlock(&mp->m_sync_lock);
421 wake_up_process(mp->m_sync_task);
422}
423
424/*
425 * Flush delayed allocate data, attempting to free up reserved space
426 * from existing allocations. At this point a new allocation attempt
427 * has failed with ENOSPC and we are in the process of scratching our
428 * heads, looking about for more room...
429 */
430STATIC void
a8d770d9 431xfs_flush_inodes_work(
a167b17e
DC
432 struct xfs_mount *mp,
433 void *arg)
434{
435 struct inode *inode = arg;
075fe102 436 xfs_sync_data(mp, SYNC_TRYLOCK);
b0710ccc 437 xfs_sync_data(mp, SYNC_TRYLOCK | SYNC_WAIT);
a167b17e
DC
438 iput(inode);
439}
440
441void
a8d770d9 442xfs_flush_inodes(
a167b17e
DC
443 xfs_inode_t *ip)
444{
445 struct inode *inode = VFS_I(ip);
e43afd72 446 DECLARE_COMPLETION_ONSTACK(completion);
a167b17e
DC
447
448 igrab(inode);
e43afd72
DC
449 xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_inodes_work, &completion);
450 wait_for_completion(&completion);
a14a348b 451 xfs_log_force(ip->i_mount, XFS_LOG_SYNC);
a167b17e
DC
452}
453
aacaa880 454/*
df308bcf
CH
455 * Every sync period we need to unpin all items, reclaim inodes and sync
456 * disk quotas. We might need to cover the log to indicate that the
1a387d3b 457 * filesystem is idle and not frozen.
aacaa880 458 */
a167b17e
DC
459STATIC void
460xfs_sync_worker(
461 struct xfs_mount *mp,
462 void *unused)
463{
464 int error;
465
aacaa880 466 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
a14a348b 467 xfs_log_force(mp, 0);
c854363e 468 xfs_reclaim_inodes(mp, 0);
aacaa880 469 /* dgc: errors ignored here */
8b5403a6 470 error = xfs_qm_sync(mp, SYNC_TRYLOCK);
1a387d3b
DC
471 if (mp->m_super->s_frozen == SB_UNFROZEN &&
472 xfs_log_need_covered(mp))
473 error = xfs_fs_log_dummy(mp, 0);
aacaa880 474 }
a167b17e
DC
475 mp->m_sync_seq++;
476 wake_up(&mp->m_wait_single_sync_task);
477}
478
479STATIC int
480xfssyncd(
481 void *arg)
482{
483 struct xfs_mount *mp = arg;
484 long timeleft;
a8d770d9 485 xfs_sync_work_t *work, *n;
a167b17e
DC
486 LIST_HEAD (tmp);
487
488 set_freezable();
489 timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10);
490 for (;;) {
20f6b2c7
DC
491 if (list_empty(&mp->m_sync_list))
492 timeleft = schedule_timeout_interruptible(timeleft);
a167b17e
DC
493 /* swsusp */
494 try_to_freeze();
495 if (kthread_should_stop() && list_empty(&mp->m_sync_list))
496 break;
497
498 spin_lock(&mp->m_sync_lock);
499 /*
500 * We can get woken by laptop mode, to do a sync -
501 * that's the (only!) case where the list would be
502 * empty with time remaining.
503 */
504 if (!timeleft || list_empty(&mp->m_sync_list)) {
505 if (!timeleft)
506 timeleft = xfs_syncd_centisecs *
507 msecs_to_jiffies(10);
508 INIT_LIST_HEAD(&mp->m_sync_work.w_list);
509 list_add_tail(&mp->m_sync_work.w_list,
510 &mp->m_sync_list);
511 }
20f6b2c7 512 list_splice_init(&mp->m_sync_list, &tmp);
a167b17e
DC
513 spin_unlock(&mp->m_sync_lock);
514
515 list_for_each_entry_safe(work, n, &tmp, w_list) {
516 (*work->w_syncer)(mp, work->w_data);
517 list_del(&work->w_list);
518 if (work == &mp->m_sync_work)
519 continue;
e43afd72
DC
520 if (work->w_completion)
521 complete(work->w_completion);
a167b17e
DC
522 kmem_free(work);
523 }
524 }
525
526 return 0;
527}
528
529int
530xfs_syncd_init(
531 struct xfs_mount *mp)
532{
533 mp->m_sync_work.w_syncer = xfs_sync_worker;
534 mp->m_sync_work.w_mount = mp;
e43afd72 535 mp->m_sync_work.w_completion = NULL;
e2a07812 536 mp->m_sync_task = kthread_run(xfssyncd, mp, "xfssyncd/%s", mp->m_fsname);
a167b17e
DC
537 if (IS_ERR(mp->m_sync_task))
538 return -PTR_ERR(mp->m_sync_task);
539 return 0;
540}
541
542void
543xfs_syncd_stop(
544 struct xfs_mount *mp)
545{
546 kthread_stop(mp->m_sync_task);
547}
548
bc990f5c
CH
549void
550__xfs_inode_set_reclaim_tag(
551 struct xfs_perag *pag,
552 struct xfs_inode *ip)
553{
554 radix_tree_tag_set(&pag->pag_ici_root,
555 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
556 XFS_ICI_RECLAIM_TAG);
16fd5367
DC
557
558 if (!pag->pag_ici_reclaimable) {
559 /* propagate the reclaim tag up into the perag radix tree */
560 spin_lock(&ip->i_mount->m_perag_lock);
561 radix_tree_tag_set(&ip->i_mount->m_perag_tree,
562 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
563 XFS_ICI_RECLAIM_TAG);
564 spin_unlock(&ip->i_mount->m_perag_lock);
565 trace_xfs_perag_set_reclaim(ip->i_mount, pag->pag_agno,
566 -1, _RET_IP_);
567 }
9bf729c0 568 pag->pag_ici_reclaimable++;
bc990f5c
CH
569}
570
11654513
DC
571/*
572 * We set the inode flag atomically with the radix tree tag.
573 * Once we get tag lookups on the radix tree, this inode flag
574 * can go away.
575 */
396beb85
DC
576void
577xfs_inode_set_reclaim_tag(
578 xfs_inode_t *ip)
579{
5017e97d
DC
580 struct xfs_mount *mp = ip->i_mount;
581 struct xfs_perag *pag;
396beb85 582
5017e97d 583 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
f1f724e4 584 write_lock(&pag->pag_ici_lock);
396beb85 585 spin_lock(&ip->i_flags_lock);
bc990f5c 586 __xfs_inode_set_reclaim_tag(pag, ip);
11654513 587 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
396beb85 588 spin_unlock(&ip->i_flags_lock);
f1f724e4 589 write_unlock(&pag->pag_ici_lock);
5017e97d 590 xfs_perag_put(pag);
396beb85
DC
591}
592
081003ff
JW
593STATIC void
594__xfs_inode_clear_reclaim(
396beb85
DC
595 xfs_perag_t *pag,
596 xfs_inode_t *ip)
597{
9bf729c0 598 pag->pag_ici_reclaimable--;
16fd5367
DC
599 if (!pag->pag_ici_reclaimable) {
600 /* clear the reclaim tag from the perag radix tree */
601 spin_lock(&ip->i_mount->m_perag_lock);
602 radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
603 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
604 XFS_ICI_RECLAIM_TAG);
605 spin_unlock(&ip->i_mount->m_perag_lock);
606 trace_xfs_perag_clear_reclaim(ip->i_mount, pag->pag_agno,
607 -1, _RET_IP_);
608 }
396beb85
DC
609}
610
081003ff
JW
611void
612__xfs_inode_clear_reclaim_tag(
613 xfs_mount_t *mp,
614 xfs_perag_t *pag,
615 xfs_inode_t *ip)
616{
617 radix_tree_tag_clear(&pag->pag_ici_root,
618 XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
619 __xfs_inode_clear_reclaim(pag, ip);
620}
621
777df5af
DC
622/*
623 * Inodes in different states need to be treated differently, and the return
624 * value of xfs_iflush is not sufficient to get this right. The following table
625 * lists the inode states and the reclaim actions necessary for non-blocking
626 * reclaim:
627 *
628 *
629 * inode state iflush ret required action
630 * --------------- ---------- ---------------
631 * bad - reclaim
632 * shutdown EIO unpin and reclaim
633 * clean, unpinned 0 reclaim
634 * stale, unpinned 0 reclaim
c854363e
DC
635 * clean, pinned(*) 0 requeue
636 * stale, pinned EAGAIN requeue
637 * dirty, delwri ok 0 requeue
638 * dirty, delwri blocked EAGAIN requeue
639 * dirty, sync flush 0 reclaim
777df5af
DC
640 *
641 * (*) dgc: I don't think the clean, pinned state is possible but it gets
642 * handled anyway given the order of checks implemented.
643 *
c854363e
DC
644 * As can be seen from the table, the return value of xfs_iflush() is not
645 * sufficient to correctly decide the reclaim action here. The checks in
646 * xfs_iflush() might look like duplicates, but they are not.
647 *
648 * Also, because we get the flush lock first, we know that any inode that has
649 * been flushed delwri has had the flush completed by the time we check that
650 * the inode is clean. The clean inode check needs to be done before flushing
651 * the inode delwri otherwise we would loop forever requeuing clean inodes as
652 * we cannot tell apart a successful delwri flush and a clean inode from the
653 * return value of xfs_iflush().
654 *
655 * Note that because the inode is flushed delayed write by background
656 * writeback, the flush lock may already be held here and waiting on it can
657 * result in very long latencies. Hence for sync reclaims, where we wait on the
658 * flush lock, the caller should push out delayed write inodes first before
659 * trying to reclaim them to minimise the amount of time spent waiting. For
660 * background relaim, we just requeue the inode for the next pass.
661 *
777df5af
DC
662 * Hence the order of actions after gaining the locks should be:
663 * bad => reclaim
664 * shutdown => unpin and reclaim
c854363e
DC
665 * pinned, delwri => requeue
666 * pinned, sync => unpin
777df5af
DC
667 * stale => reclaim
668 * clean => reclaim
c854363e
DC
669 * dirty, delwri => flush and requeue
670 * dirty, sync => flush, wait and reclaim
777df5af 671 */
75f3cb13 672STATIC int
c8e20be0 673xfs_reclaim_inode(
75f3cb13
DC
674 struct xfs_inode *ip,
675 struct xfs_perag *pag,
c8e20be0 676 int sync_mode)
fce08f2f 677{
c854363e 678 int error = 0;
777df5af 679
c8e20be0
DC
680 /*
681 * The radix tree lock here protects a thread in xfs_iget from racing
682 * with us starting reclaim on the inode. Once we have the
683 * XFS_IRECLAIM flag set it will not touch us.
684 */
685 spin_lock(&ip->i_flags_lock);
686 ASSERT_ALWAYS(__xfs_iflags_test(ip, XFS_IRECLAIMABLE));
687 if (__xfs_iflags_test(ip, XFS_IRECLAIM)) {
688 /* ignore as it is already under reclaim */
689 spin_unlock(&ip->i_flags_lock);
690 write_unlock(&pag->pag_ici_lock);
75f3cb13 691 return 0;
fce08f2f 692 }
c8e20be0
DC
693 __xfs_iflags_set(ip, XFS_IRECLAIM);
694 spin_unlock(&ip->i_flags_lock);
695 write_unlock(&pag->pag_ici_lock);
696
c8e20be0 697 xfs_ilock(ip, XFS_ILOCK_EXCL);
c854363e
DC
698 if (!xfs_iflock_nowait(ip)) {
699 if (!(sync_mode & SYNC_WAIT))
700 goto out;
701 xfs_iflock(ip);
702 }
7a3be02b 703
777df5af
DC
704 if (is_bad_inode(VFS_I(ip)))
705 goto reclaim;
706 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
707 xfs_iunpin_wait(ip);
708 goto reclaim;
709 }
c854363e
DC
710 if (xfs_ipincount(ip)) {
711 if (!(sync_mode & SYNC_WAIT)) {
712 xfs_ifunlock(ip);
713 goto out;
714 }
777df5af 715 xfs_iunpin_wait(ip);
c854363e 716 }
777df5af
DC
717 if (xfs_iflags_test(ip, XFS_ISTALE))
718 goto reclaim;
719 if (xfs_inode_clean(ip))
720 goto reclaim;
721
722 /* Now we have an inode that needs flushing */
723 error = xfs_iflush(ip, sync_mode);
c854363e
DC
724 if (sync_mode & SYNC_WAIT) {
725 xfs_iflock(ip);
726 goto reclaim;
c8e20be0
DC
727 }
728
c854363e
DC
729 /*
730 * When we have to flush an inode but don't have SYNC_WAIT set, we
731 * flush the inode out using a delwri buffer and wait for the next
732 * call into reclaim to find it in a clean state instead of waiting for
733 * it now. We also don't return errors here - if the error is transient
734 * then the next reclaim pass will flush the inode, and if the error
f1d486a3 735 * is permanent then the next sync reclaim will reclaim the inode and
c854363e
DC
736 * pass on the error.
737 */
f1d486a3 738 if (error && error != EAGAIN && !XFS_FORCED_SHUTDOWN(ip->i_mount)) {
c854363e
DC
739 xfs_fs_cmn_err(CE_WARN, ip->i_mount,
740 "inode 0x%llx background reclaim flush failed with %d",
741 (long long)ip->i_ino, error);
742 }
743out:
744 xfs_iflags_clear(ip, XFS_IRECLAIM);
745 xfs_iunlock(ip, XFS_ILOCK_EXCL);
746 /*
747 * We could return EAGAIN here to make reclaim rescan the inode tree in
748 * a short while. However, this just burns CPU time scanning the tree
749 * waiting for IO to complete and xfssyncd never goes back to the idle
750 * state. Instead, return 0 to let the next scheduled background reclaim
751 * attempt to reclaim the inode again.
752 */
753 return 0;
754
777df5af
DC
755reclaim:
756 xfs_ifunlock(ip);
c8e20be0 757 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2f11feab
DC
758
759 XFS_STATS_INC(xs_ig_reclaims);
760 /*
761 * Remove the inode from the per-AG radix tree.
762 *
763 * Because radix_tree_delete won't complain even if the item was never
764 * added to the tree assert that it's been there before to catch
765 * problems with the inode life time early on.
766 */
767 write_lock(&pag->pag_ici_lock);
768 if (!radix_tree_delete(&pag->pag_ici_root,
769 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino)))
770 ASSERT(0);
081003ff 771 __xfs_inode_clear_reclaim(pag, ip);
2f11feab
DC
772 write_unlock(&pag->pag_ici_lock);
773
774 /*
775 * Here we do an (almost) spurious inode lock in order to coordinate
776 * with inode cache radix tree lookups. This is because the lookup
777 * can reference the inodes in the cache without taking references.
778 *
779 * We make that OK here by ensuring that we wait until the inode is
780 * unlocked after the lookup before we go ahead and free it. We get
781 * both the ilock and the iolock because the code may need to drop the
782 * ilock one but will still hold the iolock.
783 */
784 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
785 xfs_qm_dqdetach(ip);
786 xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
787
788 xfs_inode_free(ip);
c854363e
DC
789 return error;
790
7a3be02b
DC
791}
792
65d0f205
DC
793/*
794 * Walk the AGs and reclaim the inodes in them. Even if the filesystem is
795 * corrupted, we still want to try to reclaim all the inodes. If we don't,
796 * then a shut down during filesystem unmount reclaim walk leak all the
797 * unreclaimed inodes.
798 */
799int
800xfs_reclaim_inodes_ag(
801 struct xfs_mount *mp,
802 int flags,
803 int *nr_to_scan)
804{
805 struct xfs_perag *pag;
806 int error = 0;
807 int last_error = 0;
808 xfs_agnumber_t ag;
809
810 ag = 0;
811 while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
812 unsigned long first_index = 0;
813 int done = 0;
814
815 ag = pag->pag_agno + 1;
816
817 do {
818 struct xfs_inode *ip;
819 int nr_found;
820
821 write_lock(&pag->pag_ici_lock);
822 nr_found = radix_tree_gang_lookup_tag(&pag->pag_ici_root,
823 (void **)&ip, first_index, 1,
824 XFS_ICI_RECLAIM_TAG);
825 if (!nr_found) {
826 write_unlock(&pag->pag_ici_lock);
827 break;
828 }
829
830 /*
831 * Update the index for the next lookup. Catch overflows
832 * into the next AG range which can occur if we have inodes
833 * in the last block of the AG and we are currently
834 * pointing to the last inode.
835 */
836 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
837 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
838 done = 1;
839
840 error = xfs_reclaim_inode(ip, pag, flags);
841 if (error && last_error != EFSCORRUPTED)
842 last_error = error;
843
844 } while (!done && (*nr_to_scan)--);
845
846 xfs_perag_put(pag);
847 }
848 return XFS_ERROR(last_error);
849}
850
7a3be02b
DC
851int
852xfs_reclaim_inodes(
853 xfs_mount_t *mp,
7a3be02b
DC
854 int mode)
855{
65d0f205
DC
856 int nr_to_scan = INT_MAX;
857
858 return xfs_reclaim_inodes_ag(mp, mode, &nr_to_scan);
9bf729c0
DC
859}
860
861/*
862 * Shrinker infrastructure.
9bf729c0 863 */
9bf729c0
DC
864static int
865xfs_reclaim_inode_shrink(
7f8275d0 866 struct shrinker *shrink,
9bf729c0
DC
867 int nr_to_scan,
868 gfp_t gfp_mask)
869{
870 struct xfs_mount *mp;
871 struct xfs_perag *pag;
872 xfs_agnumber_t ag;
16fd5367 873 int reclaimable;
9bf729c0 874
70e60ce7 875 mp = container_of(shrink, struct xfs_mount, m_inode_shrink);
9bf729c0
DC
876 if (nr_to_scan) {
877 if (!(gfp_mask & __GFP_FS))
878 return -1;
879
65d0f205
DC
880 xfs_reclaim_inodes_ag(mp, 0, &nr_to_scan);
881 /* terminate if we don't exhaust the scan */
70e60ce7
DC
882 if (nr_to_scan > 0)
883 return -1;
884 }
9bf729c0 885
16fd5367
DC
886 reclaimable = 0;
887 ag = 0;
65d0f205
DC
888 while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
889 ag = pag->pag_agno + 1;
70e60ce7
DC
890 reclaimable += pag->pag_ici_reclaimable;
891 xfs_perag_put(pag);
9bf729c0 892 }
9bf729c0
DC
893 return reclaimable;
894}
895
9bf729c0
DC
896void
897xfs_inode_shrinker_register(
898 struct xfs_mount *mp)
899{
70e60ce7
DC
900 mp->m_inode_shrink.shrink = xfs_reclaim_inode_shrink;
901 mp->m_inode_shrink.seeks = DEFAULT_SEEKS;
902 register_shrinker(&mp->m_inode_shrink);
9bf729c0
DC
903}
904
905void
906xfs_inode_shrinker_unregister(
907 struct xfs_mount *mp)
908{
70e60ce7 909 unregister_shrinker(&mp->m_inode_shrink);
fce08f2f 910}