xfs: introduce background inode reclaim work
[deliverable/linux.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
c6d09b66
DC
42struct workqueue_struct *xfs_syncd_wq; /* sync workqueue */
43
78ae5256
DC
44/*
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
e13de955
DC
52STATIC int
53xfs_inode_ag_walk_grab(
54 struct xfs_inode *ip)
55{
56 struct inode *inode = VFS_I(ip);
57
1a3e8f3d
DC
58 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
e13de955
DC
78 /* nothing to sync during shutdown */
79 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
80 return EFSCORRUPTED;
81
e13de955
DC
82 /* 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;
1a3e8f3d
DC
93
94out_unlock_noent:
95 spin_unlock(&ip->i_flags_lock);
96 return ENOENT;
e13de955
DC
97}
98
75f3cb13
DC
99STATIC int
100xfs_inode_ag_walk(
101 struct xfs_mount *mp,
5017e97d 102 struct xfs_perag *pag,
75f3cb13
DC
103 int (*execute)(struct xfs_inode *ip,
104 struct xfs_perag *pag, int flags),
65d0f205 105 int flags)
75f3cb13 106{
75f3cb13
DC
107 uint32_t first_index;
108 int last_error = 0;
109 int skipped;
65d0f205 110 int done;
78ae5256 111 int nr_found;
75f3cb13
DC
112
113restart:
65d0f205 114 done = 0;
75f3cb13
DC
115 skipped = 0;
116 first_index = 0;
78ae5256 117 nr_found = 0;
75f3cb13 118 do {
78ae5256 119 struct xfs_inode *batch[XFS_LOOKUP_BATCH];
75f3cb13 120 int error = 0;
78ae5256 121 int i;
75f3cb13 122
1a3e8f3d 123 rcu_read_lock();
65d0f205 124 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
78ae5256
DC
125 (void **)batch, first_index,
126 XFS_LOOKUP_BATCH);
65d0f205 127 if (!nr_found) {
1a3e8f3d 128 rcu_read_unlock();
75f3cb13 129 break;
c8e20be0 130 }
75f3cb13 131
65d0f205 132 /*
78ae5256
DC
133 * Grab the inodes before we drop the lock. if we found
134 * nothing, nr == 0 and the loop will be skipped.
65d0f205 135 */
78ae5256
DC
136 for (i = 0; i < nr_found; i++) {
137 struct xfs_inode *ip = batch[i];
138
139 if (done || xfs_inode_ag_walk_grab(ip))
140 batch[i] = NULL;
141
142 /*
1a3e8f3d
DC
143 * 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.
78ae5256 153 */
1a3e8f3d
DC
154 if (XFS_INO_TO_AGNO(mp, ip->i_ino) != pag->pag_agno)
155 continue;
78ae5256
DC
156 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;
e13de955 159 }
78ae5256
DC
160
161 /* unlock now we've grabbed the inodes. */
1a3e8f3d 162 rcu_read_unlock();
e13de955 163
78ae5256
DC
164 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;
75f3cb13 175 }
c8e20be0
DC
176
177 /* bail out if the filesystem is corrupted. */
75f3cb13
DC
178 if (error == EFSCORRUPTED)
179 break;
180
78ae5256 181 } while (nr_found && !done);
75f3cb13
DC
182
183 if (skipped) {
184 delay(1);
185 goto restart;
186 }
75f3cb13
DC
187 return last_error;
188}
189
fe588ed3 190int
75f3cb13
DC
191xfs_inode_ag_iterator(
192 struct xfs_mount *mp,
193 int (*execute)(struct xfs_inode *ip,
194 struct xfs_perag *pag, int flags),
65d0f205 195 int flags)
75f3cb13 196{
16fd5367 197 struct xfs_perag *pag;
75f3cb13
DC
198 int error = 0;
199 int last_error = 0;
200 xfs_agnumber_t ag;
201
16fd5367 202 ag = 0;
65d0f205
DC
203 while ((pag = xfs_perag_get(mp, ag))) {
204 ag = pag->pag_agno + 1;
205 error = xfs_inode_ag_walk(mp, pag, execute, flags);
5017e97d 206 xfs_perag_put(pag);
75f3cb13
DC
207 if (error) {
208 last_error = error;
209 if (error == EFSCORRUPTED)
210 break;
211 }
212 }
213 return XFS_ERROR(last_error);
214}
215
5a34d5cd
DC
216STATIC int
217xfs_sync_inode_data(
218 struct xfs_inode *ip,
75f3cb13 219 struct xfs_perag *pag,
5a34d5cd
DC
220 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) ?
0cadda1c 236 0 : XBF_ASYNC, FI_NONE);
5a34d5cd
DC
237 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
238
239 out_wait:
b0710ccc 240 if (flags & SYNC_WAIT)
5a34d5cd
DC
241 xfs_ioend_wait(ip);
242 return error;
243}
244
845b6d0c
CH
245STATIC int
246xfs_sync_inode_attr(
247 struct xfs_inode *ip,
75f3cb13 248 struct xfs_perag *pag,
845b6d0c
CH
249 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
c854363e 267 error = xfs_iflush(ip, flags);
845b6d0c
CH
268
269 out_unlock:
270 xfs_iunlock(ip, XFS_ILOCK_SHARED);
271 return error;
272}
273
075fe102
CH
274/*
275 * Write out pagecache data for the whole filesystem.
276 */
64c86149 277STATIC int
075fe102
CH
278xfs_sync_data(
279 struct xfs_mount *mp,
280 int flags)
683a8970 281{
075fe102 282 int error;
fe4fa4b8 283
b0710ccc 284 ASSERT((flags & ~(SYNC_TRYLOCK|SYNC_WAIT)) == 0);
fe4fa4b8 285
65d0f205 286 error = xfs_inode_ag_iterator(mp, xfs_sync_inode_data, flags);
075fe102
CH
287 if (error)
288 return XFS_ERROR(error);
e9f1c6ee 289
a14a348b 290 xfs_log_force(mp, (flags & SYNC_WAIT) ? XFS_LOG_SYNC : 0);
075fe102
CH
291 return 0;
292}
e9f1c6ee 293
075fe102
CH
294/*
295 * Write out inode metadata (attributes) for the whole filesystem.
296 */
64c86149 297STATIC int
075fe102
CH
298xfs_sync_attr(
299 struct xfs_mount *mp,
300 int flags)
301{
302 ASSERT((flags & ~SYNC_WAIT) == 0);
75f3cb13 303
65d0f205 304 return xfs_inode_ag_iterator(mp, xfs_sync_inode_attr, flags);
fe4fa4b8
DC
305}
306
5d77c0dc 307STATIC int
2af75df7 308xfs_sync_fsdata(
df308bcf 309 struct xfs_mount *mp)
2af75df7
CH
310{
311 struct xfs_buf *bp;
2af75df7
CH
312
313 /*
df308bcf
CH
314 * 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.
2af75df7 320 */
df308bcf
CH
321 bp = xfs_getsb(mp, 0);
322 if (XFS_BUF_ISPINNED(bp))
323 xfs_log_force(mp, 0);
2af75df7 324
df308bcf 325 return xfs_bwrite(mp, bp);
e9f1c6ee
DC
326}
327
328/*
a4e4c4f4
DC
329 * 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,
e9f1c6ee
DC
340 * 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,
a4e4c4f4
DC
342 * transactions can still occur here so don't bother flushing the buftarg
343 * because it'll just get dirty again.
e9f1c6ee
DC
344 */
345int
346xfs_quiesce_data(
347 struct xfs_mount *mp)
348{
df308bcf 349 int error, error2 = 0;
e9f1c6ee
DC
350
351 /* push non-blocking */
075fe102 352 xfs_sync_data(mp, 0);
8b5403a6 353 xfs_qm_sync(mp, SYNC_TRYLOCK);
e9f1c6ee 354
c90b07e8 355 /* push and block till complete */
b0710ccc 356 xfs_sync_data(mp, SYNC_WAIT);
7d095257 357 xfs_qm_sync(mp, SYNC_WAIT);
e9f1c6ee 358
a4e4c4f4 359 /* write superblock and hoover up shutdown errors */
df308bcf
CH
360 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))
c58efdb4 367 error2 = xfs_fs_log_dummy(mp);
e9f1c6ee 368
a4e4c4f4 369 /* flush data-only devices */
e9f1c6ee
DC
370 if (mp->m_rtdev_targp)
371 XFS_bflush(mp->m_rtdev_targp);
372
df308bcf 373 return error ? error : error2;
2af75df7
CH
374}
375
76bf105c
DC
376STATIC void
377xfs_quiesce_fs(
378 struct xfs_mount *mp)
379{
380 int count = 0, pincount;
381
c854363e 382 xfs_reclaim_inodes(mp, 0);
76bf105c 383 xfs_flush_buftarg(mp->m_ddev_targp, 0);
76bf105c
DC
384
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
c854363e
DC
389 * 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.
76bf105c
DC
391 */
392 do {
c854363e 393 xfs_reclaim_inodes(mp, SYNC_WAIT);
075fe102 394 xfs_sync_attr(mp, SYNC_WAIT);
76bf105c
DC
395 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
5e106572
FB
421 /*
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);
76bf105c
DC
426
427 /* Push the superblock and write an unmount record */
428 error = xfs_log_sbcount(mp, 1);
429 if (error)
4f10700a 430 xfs_warn(mp, "xfs_attr_quiesce: failed to log sb changes. "
76bf105c
DC
431 "Frozen image may not be consistent.");
432 xfs_log_unmount_write(mp);
433 xfs_unmountfs_writesb(mp);
434}
435
c6d09b66
DC
436static 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);
c6d09b66
DC
464 error = xfs_qm_sync(mp, SYNC_TRYLOCK);
465 }
466
467 /* queue us up again */
468 xfs_syncd_queue_sync(mp);
469}
470
a7b339f1
DC
471/*
472 * Queue a new inode reclaim pass if there are reclaimable inodes and there
473 * isn't a reclaim pass already in progress. By default it runs every 5s based
474 * on the xfs syncd work default of 30s. Perhaps this should have it's own
475 * tunable, but that can be done if this method proves to be ineffective or too
476 * aggressive.
477 */
478static void
479xfs_syncd_queue_reclaim(
480 struct xfs_mount *mp)
481{
482
483 /*
484 * We can have inodes enter reclaim after we've shut down the syncd
485 * workqueue during unmount, so don't allow reclaim work to be queued
486 * during unmount.
487 */
488 if (!(mp->m_super->s_flags & MS_ACTIVE))
489 return;
490
491 rcu_read_lock();
492 if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_RECLAIM_TAG)) {
493 queue_delayed_work(xfs_syncd_wq, &mp->m_reclaim_work,
494 msecs_to_jiffies(xfs_syncd_centisecs / 6 * 10));
495 }
496 rcu_read_unlock();
497}
498
499/*
500 * This is a fast pass over the inode cache to try to get reclaim moving on as
501 * many inodes as possible in a short period of time. It kicks itself every few
502 * seconds, as well as being kicked by the inode cache shrinker when memory
503 * goes low. It scans as quickly as possible avoiding locked inodes or those
504 * already being flushed, and once done schedules a future pass.
505 */
506STATIC void
507xfs_reclaim_worker(
508 struct work_struct *work)
509{
510 struct xfs_mount *mp = container_of(to_delayed_work(work),
511 struct xfs_mount, m_reclaim_work);
512
513 xfs_reclaim_inodes(mp, SYNC_TRYLOCK);
514 xfs_syncd_queue_reclaim(mp);
515}
516
89e4cb55
DC
517/*
518 * Flush delayed allocate data, attempting to free up reserved space
519 * from existing allocations. At this point a new allocation attempt
520 * has failed with ENOSPC and we are in the process of scratching our
521 * heads, looking about for more room.
522 *
523 * Queue a new data flush if there isn't one already in progress and
524 * wait for completion of the flush. This means that we only ever have one
525 * inode flush in progress no matter how many ENOSPC events are occurring and
526 * so will prevent the system from bogging down due to every concurrent
527 * ENOSPC event scanning all the active inodes in the system for writeback.
528 */
529void
530xfs_flush_inodes(
531 struct xfs_inode *ip)
532{
533 struct xfs_mount *mp = ip->i_mount;
534
535 queue_work(xfs_syncd_wq, &mp->m_flush_work);
536 flush_work_sync(&mp->m_flush_work);
537}
538
539STATIC void
540xfs_flush_worker(
541 struct work_struct *work)
542{
543 struct xfs_mount *mp = container_of(work,
544 struct xfs_mount, m_flush_work);
545
546 xfs_sync_data(mp, SYNC_TRYLOCK);
547 xfs_sync_data(mp, SYNC_TRYLOCK | SYNC_WAIT);
548}
549
a167b17e
DC
550int
551xfs_syncd_init(
552 struct xfs_mount *mp)
553{
89e4cb55 554 INIT_WORK(&mp->m_flush_work, xfs_flush_worker);
c6d09b66 555 INIT_DELAYED_WORK(&mp->m_sync_work, xfs_sync_worker);
a7b339f1
DC
556 INIT_DELAYED_WORK(&mp->m_reclaim_work, xfs_reclaim_worker);
557
c6d09b66 558 xfs_syncd_queue_sync(mp);
a7b339f1 559 xfs_syncd_queue_reclaim(mp);
c6d09b66 560
a167b17e
DC
561 return 0;
562}
563
564void
565xfs_syncd_stop(
566 struct xfs_mount *mp)
567{
c6d09b66 568 cancel_delayed_work_sync(&mp->m_sync_work);
a7b339f1 569 cancel_delayed_work_sync(&mp->m_reclaim_work);
89e4cb55 570 cancel_work_sync(&mp->m_flush_work);
a167b17e
DC
571}
572
bc990f5c
CH
573void
574__xfs_inode_set_reclaim_tag(
575 struct xfs_perag *pag,
576 struct xfs_inode *ip)
577{
578 radix_tree_tag_set(&pag->pag_ici_root,
579 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
580 XFS_ICI_RECLAIM_TAG);
16fd5367
DC
581
582 if (!pag->pag_ici_reclaimable) {
583 /* propagate the reclaim tag up into the perag radix tree */
584 spin_lock(&ip->i_mount->m_perag_lock);
585 radix_tree_tag_set(&ip->i_mount->m_perag_tree,
586 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
587 XFS_ICI_RECLAIM_TAG);
588 spin_unlock(&ip->i_mount->m_perag_lock);
a7b339f1
DC
589
590 /* schedule periodic background inode reclaim */
591 xfs_syncd_queue_reclaim(ip->i_mount);
592
16fd5367
DC
593 trace_xfs_perag_set_reclaim(ip->i_mount, pag->pag_agno,
594 -1, _RET_IP_);
595 }
9bf729c0 596 pag->pag_ici_reclaimable++;
bc990f5c
CH
597}
598
11654513
DC
599/*
600 * We set the inode flag atomically with the radix tree tag.
601 * Once we get tag lookups on the radix tree, this inode flag
602 * can go away.
603 */
396beb85
DC
604void
605xfs_inode_set_reclaim_tag(
606 xfs_inode_t *ip)
607{
5017e97d
DC
608 struct xfs_mount *mp = ip->i_mount;
609 struct xfs_perag *pag;
396beb85 610
5017e97d 611 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
1a427ab0 612 spin_lock(&pag->pag_ici_lock);
396beb85 613 spin_lock(&ip->i_flags_lock);
bc990f5c 614 __xfs_inode_set_reclaim_tag(pag, ip);
11654513 615 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
396beb85 616 spin_unlock(&ip->i_flags_lock);
1a427ab0 617 spin_unlock(&pag->pag_ici_lock);
5017e97d 618 xfs_perag_put(pag);
396beb85
DC
619}
620
081003ff
JW
621STATIC void
622__xfs_inode_clear_reclaim(
396beb85
DC
623 xfs_perag_t *pag,
624 xfs_inode_t *ip)
625{
9bf729c0 626 pag->pag_ici_reclaimable--;
16fd5367
DC
627 if (!pag->pag_ici_reclaimable) {
628 /* clear the reclaim tag from the perag radix tree */
629 spin_lock(&ip->i_mount->m_perag_lock);
630 radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
631 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
632 XFS_ICI_RECLAIM_TAG);
633 spin_unlock(&ip->i_mount->m_perag_lock);
634 trace_xfs_perag_clear_reclaim(ip->i_mount, pag->pag_agno,
635 -1, _RET_IP_);
636 }
396beb85
DC
637}
638
081003ff
JW
639void
640__xfs_inode_clear_reclaim_tag(
641 xfs_mount_t *mp,
642 xfs_perag_t *pag,
643 xfs_inode_t *ip)
644{
645 radix_tree_tag_clear(&pag->pag_ici_root,
646 XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
647 __xfs_inode_clear_reclaim(pag, ip);
648}
649
e3a20c0b
DC
650/*
651 * Grab the inode for reclaim exclusively.
652 * Return 0 if we grabbed it, non-zero otherwise.
653 */
654STATIC int
655xfs_reclaim_inode_grab(
656 struct xfs_inode *ip,
657 int flags)
658{
1a3e8f3d
DC
659 ASSERT(rcu_read_lock_held());
660
661 /* quick check for stale RCU freed inode */
662 if (!ip->i_ino)
663 return 1;
e3a20c0b
DC
664
665 /*
1a3e8f3d 666 * do some unlocked checks first to avoid unnecessary lock traffic.
e3a20c0b
DC
667 * The first is a flush lock check, the second is a already in reclaim
668 * check. Only do these checks if we are not going to block on locks.
669 */
670 if ((flags & SYNC_TRYLOCK) &&
671 (!ip->i_flush.done || __xfs_iflags_test(ip, XFS_IRECLAIM))) {
672 return 1;
673 }
674
675 /*
676 * The radix tree lock here protects a thread in xfs_iget from racing
677 * with us starting reclaim on the inode. Once we have the
678 * XFS_IRECLAIM flag set it will not touch us.
1a3e8f3d
DC
679 *
680 * Due to RCU lookup, we may find inodes that have been freed and only
681 * have XFS_IRECLAIM set. Indeed, we may see reallocated inodes that
682 * aren't candidates for reclaim at all, so we must check the
683 * XFS_IRECLAIMABLE is set first before proceeding to reclaim.
e3a20c0b
DC
684 */
685 spin_lock(&ip->i_flags_lock);
1a3e8f3d
DC
686 if (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) ||
687 __xfs_iflags_test(ip, XFS_IRECLAIM)) {
688 /* not a reclaim candidate. */
e3a20c0b
DC
689 spin_unlock(&ip->i_flags_lock);
690 return 1;
691 }
692 __xfs_iflags_set(ip, XFS_IRECLAIM);
693 spin_unlock(&ip->i_flags_lock);
694 return 0;
695}
696
777df5af
DC
697/*
698 * Inodes in different states need to be treated differently, and the return
699 * value of xfs_iflush is not sufficient to get this right. The following table
700 * lists the inode states and the reclaim actions necessary for non-blocking
701 * reclaim:
702 *
703 *
704 * inode state iflush ret required action
705 * --------------- ---------- ---------------
706 * bad - reclaim
707 * shutdown EIO unpin and reclaim
708 * clean, unpinned 0 reclaim
709 * stale, unpinned 0 reclaim
c854363e
DC
710 * clean, pinned(*) 0 requeue
711 * stale, pinned EAGAIN requeue
712 * dirty, delwri ok 0 requeue
713 * dirty, delwri blocked EAGAIN requeue
714 * dirty, sync flush 0 reclaim
777df5af
DC
715 *
716 * (*) dgc: I don't think the clean, pinned state is possible but it gets
717 * handled anyway given the order of checks implemented.
718 *
c854363e
DC
719 * As can be seen from the table, the return value of xfs_iflush() is not
720 * sufficient to correctly decide the reclaim action here. The checks in
721 * xfs_iflush() might look like duplicates, but they are not.
722 *
723 * Also, because we get the flush lock first, we know that any inode that has
724 * been flushed delwri has had the flush completed by the time we check that
725 * the inode is clean. The clean inode check needs to be done before flushing
726 * the inode delwri otherwise we would loop forever requeuing clean inodes as
727 * we cannot tell apart a successful delwri flush and a clean inode from the
728 * return value of xfs_iflush().
729 *
730 * Note that because the inode is flushed delayed write by background
731 * writeback, the flush lock may already be held here and waiting on it can
732 * result in very long latencies. Hence for sync reclaims, where we wait on the
733 * flush lock, the caller should push out delayed write inodes first before
734 * trying to reclaim them to minimise the amount of time spent waiting. For
735 * background relaim, we just requeue the inode for the next pass.
736 *
777df5af
DC
737 * Hence the order of actions after gaining the locks should be:
738 * bad => reclaim
739 * shutdown => unpin and reclaim
c854363e
DC
740 * pinned, delwri => requeue
741 * pinned, sync => unpin
777df5af
DC
742 * stale => reclaim
743 * clean => reclaim
c854363e
DC
744 * dirty, delwri => flush and requeue
745 * dirty, sync => flush, wait and reclaim
777df5af 746 */
75f3cb13 747STATIC int
c8e20be0 748xfs_reclaim_inode(
75f3cb13
DC
749 struct xfs_inode *ip,
750 struct xfs_perag *pag,
c8e20be0 751 int sync_mode)
fce08f2f 752{
1bfd8d04 753 int error;
777df5af 754
1bfd8d04
DC
755restart:
756 error = 0;
c8e20be0 757 xfs_ilock(ip, XFS_ILOCK_EXCL);
c854363e
DC
758 if (!xfs_iflock_nowait(ip)) {
759 if (!(sync_mode & SYNC_WAIT))
760 goto out;
761 xfs_iflock(ip);
762 }
7a3be02b 763
777df5af
DC
764 if (is_bad_inode(VFS_I(ip)))
765 goto reclaim;
766 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
767 xfs_iunpin_wait(ip);
768 goto reclaim;
769 }
c854363e
DC
770 if (xfs_ipincount(ip)) {
771 if (!(sync_mode & SYNC_WAIT)) {
772 xfs_ifunlock(ip);
773 goto out;
774 }
777df5af 775 xfs_iunpin_wait(ip);
c854363e 776 }
777df5af
DC
777 if (xfs_iflags_test(ip, XFS_ISTALE))
778 goto reclaim;
779 if (xfs_inode_clean(ip))
780 goto reclaim;
781
1bfd8d04
DC
782 /*
783 * Now we have an inode that needs flushing.
784 *
785 * We do a nonblocking flush here even if we are doing a SYNC_WAIT
786 * reclaim as we can deadlock with inode cluster removal.
787 * xfs_ifree_cluster() can lock the inode buffer before it locks the
788 * ip->i_lock, and we are doing the exact opposite here. As a result,
789 * doing a blocking xfs_itobp() to get the cluster buffer will result
790 * in an ABBA deadlock with xfs_ifree_cluster().
791 *
792 * As xfs_ifree_cluser() must gather all inodes that are active in the
793 * cache to mark them stale, if we hit this case we don't actually want
794 * to do IO here - we want the inode marked stale so we can simply
795 * reclaim it. Hence if we get an EAGAIN error on a SYNC_WAIT flush,
796 * just unlock the inode, back off and try again. Hopefully the next
797 * pass through will see the stale flag set on the inode.
798 */
799 error = xfs_iflush(ip, SYNC_TRYLOCK | sync_mode);
c854363e 800 if (sync_mode & SYNC_WAIT) {
1bfd8d04
DC
801 if (error == EAGAIN) {
802 xfs_iunlock(ip, XFS_ILOCK_EXCL);
803 /* backoff longer than in xfs_ifree_cluster */
804 delay(2);
805 goto restart;
806 }
c854363e
DC
807 xfs_iflock(ip);
808 goto reclaim;
c8e20be0
DC
809 }
810
c854363e
DC
811 /*
812 * When we have to flush an inode but don't have SYNC_WAIT set, we
813 * flush the inode out using a delwri buffer and wait for the next
814 * call into reclaim to find it in a clean state instead of waiting for
815 * it now. We also don't return errors here - if the error is transient
816 * then the next reclaim pass will flush the inode, and if the error
f1d486a3 817 * is permanent then the next sync reclaim will reclaim the inode and
c854363e
DC
818 * pass on the error.
819 */
f1d486a3 820 if (error && error != EAGAIN && !XFS_FORCED_SHUTDOWN(ip->i_mount)) {
4f10700a 821 xfs_warn(ip->i_mount,
c854363e
DC
822 "inode 0x%llx background reclaim flush failed with %d",
823 (long long)ip->i_ino, error);
824 }
825out:
826 xfs_iflags_clear(ip, XFS_IRECLAIM);
827 xfs_iunlock(ip, XFS_ILOCK_EXCL);
828 /*
829 * We could return EAGAIN here to make reclaim rescan the inode tree in
830 * a short while. However, this just burns CPU time scanning the tree
831 * waiting for IO to complete and xfssyncd never goes back to the idle
832 * state. Instead, return 0 to let the next scheduled background reclaim
833 * attempt to reclaim the inode again.
834 */
835 return 0;
836
777df5af
DC
837reclaim:
838 xfs_ifunlock(ip);
c8e20be0 839 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2f11feab
DC
840
841 XFS_STATS_INC(xs_ig_reclaims);
842 /*
843 * Remove the inode from the per-AG radix tree.
844 *
845 * Because radix_tree_delete won't complain even if the item was never
846 * added to the tree assert that it's been there before to catch
847 * problems with the inode life time early on.
848 */
1a427ab0 849 spin_lock(&pag->pag_ici_lock);
2f11feab
DC
850 if (!radix_tree_delete(&pag->pag_ici_root,
851 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino)))
852 ASSERT(0);
081003ff 853 __xfs_inode_clear_reclaim(pag, ip);
1a427ab0 854 spin_unlock(&pag->pag_ici_lock);
2f11feab
DC
855
856 /*
857 * Here we do an (almost) spurious inode lock in order to coordinate
858 * with inode cache radix tree lookups. This is because the lookup
859 * can reference the inodes in the cache without taking references.
860 *
861 * We make that OK here by ensuring that we wait until the inode is
862 * unlocked after the lookup before we go ahead and free it. We get
863 * both the ilock and the iolock because the code may need to drop the
864 * ilock one but will still hold the iolock.
865 */
866 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
867 xfs_qm_dqdetach(ip);
868 xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
869
870 xfs_inode_free(ip);
c854363e
DC
871 return error;
872
7a3be02b
DC
873}
874
65d0f205
DC
875/*
876 * Walk the AGs and reclaim the inodes in them. Even if the filesystem is
877 * corrupted, we still want to try to reclaim all the inodes. If we don't,
878 * then a shut down during filesystem unmount reclaim walk leak all the
879 * unreclaimed inodes.
880 */
881int
882xfs_reclaim_inodes_ag(
883 struct xfs_mount *mp,
884 int flags,
885 int *nr_to_scan)
886{
887 struct xfs_perag *pag;
888 int error = 0;
889 int last_error = 0;
890 xfs_agnumber_t ag;
69b491c2
DC
891 int trylock = flags & SYNC_TRYLOCK;
892 int skipped;
65d0f205 893
69b491c2 894restart:
65d0f205 895 ag = 0;
69b491c2 896 skipped = 0;
65d0f205
DC
897 while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
898 unsigned long first_index = 0;
899 int done = 0;
e3a20c0b 900 int nr_found = 0;
65d0f205
DC
901
902 ag = pag->pag_agno + 1;
903
69b491c2
DC
904 if (trylock) {
905 if (!mutex_trylock(&pag->pag_ici_reclaim_lock)) {
906 skipped++;
f83282a8 907 xfs_perag_put(pag);
69b491c2
DC
908 continue;
909 }
910 first_index = pag->pag_ici_reclaim_cursor;
911 } else
912 mutex_lock(&pag->pag_ici_reclaim_lock);
913
65d0f205 914 do {
e3a20c0b
DC
915 struct xfs_inode *batch[XFS_LOOKUP_BATCH];
916 int i;
65d0f205 917
1a3e8f3d 918 rcu_read_lock();
e3a20c0b
DC
919 nr_found = radix_tree_gang_lookup_tag(
920 &pag->pag_ici_root,
921 (void **)batch, first_index,
922 XFS_LOOKUP_BATCH,
65d0f205
DC
923 XFS_ICI_RECLAIM_TAG);
924 if (!nr_found) {
1a3e8f3d 925 rcu_read_unlock();
65d0f205
DC
926 break;
927 }
928
929 /*
e3a20c0b
DC
930 * Grab the inodes before we drop the lock. if we found
931 * nothing, nr == 0 and the loop will be skipped.
65d0f205 932 */
e3a20c0b
DC
933 for (i = 0; i < nr_found; i++) {
934 struct xfs_inode *ip = batch[i];
935
936 if (done || xfs_reclaim_inode_grab(ip, flags))
937 batch[i] = NULL;
938
939 /*
940 * Update the index for the next lookup. Catch
941 * overflows into the next AG range which can
942 * occur if we have inodes in the last block of
943 * the AG and we are currently pointing to the
944 * last inode.
1a3e8f3d
DC
945 *
946 * Because we may see inodes that are from the
947 * wrong AG due to RCU freeing and
948 * reallocation, only update the index if it
949 * lies in this AG. It was a race that lead us
950 * to see this inode, so another lookup from
951 * the same index will not find it again.
e3a20c0b 952 */
1a3e8f3d
DC
953 if (XFS_INO_TO_AGNO(mp, ip->i_ino) !=
954 pag->pag_agno)
955 continue;
e3a20c0b
DC
956 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
957 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
958 done = 1;
959 }
65d0f205 960
e3a20c0b 961 /* unlock now we've grabbed the inodes. */
1a3e8f3d 962 rcu_read_unlock();
e3a20c0b
DC
963
964 for (i = 0; i < nr_found; i++) {
965 if (!batch[i])
966 continue;
967 error = xfs_reclaim_inode(batch[i], pag, flags);
968 if (error && last_error != EFSCORRUPTED)
969 last_error = error;
970 }
971
972 *nr_to_scan -= XFS_LOOKUP_BATCH;
65d0f205 973
e3a20c0b 974 } while (nr_found && !done && *nr_to_scan > 0);
65d0f205 975
69b491c2
DC
976 if (trylock && !done)
977 pag->pag_ici_reclaim_cursor = first_index;
978 else
979 pag->pag_ici_reclaim_cursor = 0;
980 mutex_unlock(&pag->pag_ici_reclaim_lock);
65d0f205
DC
981 xfs_perag_put(pag);
982 }
69b491c2
DC
983
984 /*
985 * if we skipped any AG, and we still have scan count remaining, do
986 * another pass this time using blocking reclaim semantics (i.e
987 * waiting on the reclaim locks and ignoring the reclaim cursors). This
988 * ensure that when we get more reclaimers than AGs we block rather
989 * than spin trying to execute reclaim.
990 */
991 if (trylock && skipped && *nr_to_scan > 0) {
992 trylock = 0;
993 goto restart;
994 }
65d0f205
DC
995 return XFS_ERROR(last_error);
996}
997
7a3be02b
DC
998int
999xfs_reclaim_inodes(
1000 xfs_mount_t *mp,
7a3be02b
DC
1001 int mode)
1002{
65d0f205
DC
1003 int nr_to_scan = INT_MAX;
1004
1005 return xfs_reclaim_inodes_ag(mp, mode, &nr_to_scan);
9bf729c0
DC
1006}
1007
1008/*
a7b339f1
DC
1009 * Inode cache shrinker.
1010 *
1011 * When called we make sure that there is a background (fast) inode reclaim in
1012 * progress, while we will throttle the speed of reclaim via doiing synchronous
1013 * reclaim of inodes. That means if we come across dirty inodes, we wait for
1014 * them to be cleaned, which we hope will not be very long due to the
1015 * background walker having already kicked the IO off on those dirty inodes.
9bf729c0 1016 */
9bf729c0
DC
1017static int
1018xfs_reclaim_inode_shrink(
7f8275d0 1019 struct shrinker *shrink,
9bf729c0
DC
1020 int nr_to_scan,
1021 gfp_t gfp_mask)
1022{
1023 struct xfs_mount *mp;
1024 struct xfs_perag *pag;
1025 xfs_agnumber_t ag;
16fd5367 1026 int reclaimable;
9bf729c0 1027
70e60ce7 1028 mp = container_of(shrink, struct xfs_mount, m_inode_shrink);
9bf729c0 1029 if (nr_to_scan) {
a7b339f1
DC
1030 /* kick background reclaimer */
1031 xfs_syncd_queue_reclaim(mp);
1032
9bf729c0
DC
1033 if (!(gfp_mask & __GFP_FS))
1034 return -1;
1035
a7b339f1
DC
1036 xfs_reclaim_inodes_ag(mp, SYNC_TRYLOCK | SYNC_WAIT,
1037 &nr_to_scan);
65d0f205 1038 /* terminate if we don't exhaust the scan */
70e60ce7
DC
1039 if (nr_to_scan > 0)
1040 return -1;
1041 }
9bf729c0 1042
16fd5367
DC
1043 reclaimable = 0;
1044 ag = 0;
65d0f205
DC
1045 while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
1046 ag = pag->pag_agno + 1;
70e60ce7
DC
1047 reclaimable += pag->pag_ici_reclaimable;
1048 xfs_perag_put(pag);
9bf729c0 1049 }
9bf729c0
DC
1050 return reclaimable;
1051}
1052
9bf729c0
DC
1053void
1054xfs_inode_shrinker_register(
1055 struct xfs_mount *mp)
1056{
70e60ce7
DC
1057 mp->m_inode_shrink.shrink = xfs_reclaim_inode_shrink;
1058 mp->m_inode_shrink.seeks = DEFAULT_SEEKS;
1059 register_shrinker(&mp->m_inode_shrink);
9bf729c0
DC
1060}
1061
1062void
1063xfs_inode_shrinker_unregister(
1064 struct xfs_mount *mp)
1065{
70e60ce7 1066 unregister_shrinker(&mp->m_inode_shrink);
fce08f2f 1067}
This page took 0.254225 seconds and 5 git commands to generate.