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