]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/xfs/linux-2.6/xfs_sync.c
xfs: reclaim inodes under a write lock
[net-next-2.6.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"
27#include "xfs_dir2.h"
28#include "xfs_dmapi.h"
29#include "xfs_mount.h"
30#include "xfs_bmap_btree.h"
31#include "xfs_alloc_btree.h"
32#include "xfs_ialloc_btree.h"
33#include "xfs_btree.h"
34#include "xfs_dir2_sf.h"
35#include "xfs_attr_sf.h"
36#include "xfs_inode.h"
37#include "xfs_dinode.h"
38#include "xfs_error.h"
39#include "xfs_mru_cache.h"
40#include "xfs_filestream.h"
41#include "xfs_vnodeops.h"
42#include "xfs_utils.h"
43#include "xfs_buf_item.h"
44#include "xfs_inode_item.h"
45#include "xfs_rw.h"
7d095257 46#include "xfs_quota.h"
0b1b213f 47#include "xfs_trace.h"
fe4fa4b8 48
a167b17e
DC
49#include <linux/kthread.h>
50#include <linux/freezer.h>
51
5a34d5cd 52
75f3cb13
DC
53STATIC xfs_inode_t *
54xfs_inode_ag_lookup(
55 struct xfs_mount *mp,
56 struct xfs_perag *pag,
57 uint32_t *first_index,
58 int tag)
59{
60 int nr_found;
61 struct xfs_inode *ip;
62
63 /*
64 * use a gang lookup to find the next inode in the tree
65 * as the tree is sparse and a gang lookup walks to find
66 * the number of objects requested.
67 */
75f3cb13
DC
68 if (tag == XFS_ICI_NO_TAG) {
69 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
70 (void **)&ip, *first_index, 1);
71 } else {
72 nr_found = radix_tree_gang_lookup_tag(&pag->pag_ici_root,
73 (void **)&ip, *first_index, 1, tag);
74 }
75 if (!nr_found)
c8e20be0 76 return NULL;
75f3cb13
DC
77
78 /*
79 * Update the index for the next lookup. Catch overflows
80 * into the next AG range which can occur if we have inodes
81 * in the last block of the AG and we are currently
82 * pointing to the last inode.
83 */
84 *first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
85 if (*first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
c8e20be0 86 return NULL;
75f3cb13 87 return ip;
75f3cb13
DC
88}
89
90STATIC int
91xfs_inode_ag_walk(
92 struct xfs_mount *mp,
93 xfs_agnumber_t ag,
94 int (*execute)(struct xfs_inode *ip,
95 struct xfs_perag *pag, int flags),
96 int flags,
c8e20be0
DC
97 int tag,
98 int exclusive)
75f3cb13
DC
99{
100 struct xfs_perag *pag = &mp->m_perag[ag];
101 uint32_t first_index;
102 int last_error = 0;
103 int skipped;
104
105restart:
106 skipped = 0;
107 first_index = 0;
108 do {
109 int error = 0;
110 xfs_inode_t *ip;
111
c8e20be0
DC
112 if (exclusive)
113 write_lock(&pag->pag_ici_lock);
114 else
115 read_lock(&pag->pag_ici_lock);
75f3cb13 116 ip = xfs_inode_ag_lookup(mp, pag, &first_index, tag);
c8e20be0
DC
117 if (!ip) {
118 if (exclusive)
119 write_unlock(&pag->pag_ici_lock);
120 else
121 read_unlock(&pag->pag_ici_lock);
75f3cb13 122 break;
c8e20be0 123 }
75f3cb13 124
c8e20be0 125 /* execute releases pag->pag_ici_lock */
75f3cb13
DC
126 error = execute(ip, pag, flags);
127 if (error == EAGAIN) {
128 skipped++;
129 continue;
130 }
131 if (error)
132 last_error = error;
c8e20be0
DC
133
134 /* bail out if the filesystem is corrupted. */
75f3cb13
DC
135 if (error == EFSCORRUPTED)
136 break;
137
138 } while (1);
139
140 if (skipped) {
141 delay(1);
142 goto restart;
143 }
144
145 xfs_put_perag(mp, pag);
146 return last_error;
147}
148
fe588ed3 149int
75f3cb13
DC
150xfs_inode_ag_iterator(
151 struct xfs_mount *mp,
152 int (*execute)(struct xfs_inode *ip,
153 struct xfs_perag *pag, int flags),
154 int flags,
c8e20be0
DC
155 int tag,
156 int exclusive)
75f3cb13
DC
157{
158 int error = 0;
159 int last_error = 0;
160 xfs_agnumber_t ag;
161
162 for (ag = 0; ag < mp->m_sb.sb_agcount; ag++) {
163 if (!mp->m_perag[ag].pag_ici_init)
164 continue;
c8e20be0
DC
165 error = xfs_inode_ag_walk(mp, ag, execute, flags, tag,
166 exclusive);
75f3cb13
DC
167 if (error) {
168 last_error = error;
169 if (error == EFSCORRUPTED)
170 break;
171 }
172 }
173 return XFS_ERROR(last_error);
174}
175
1da8eeca 176/* must be called with pag_ici_lock held and releases it */
fe588ed3 177int
1da8eeca
DC
178xfs_sync_inode_valid(
179 struct xfs_inode *ip,
180 struct xfs_perag *pag)
181{
182 struct inode *inode = VFS_I(ip);
183
184 /* nothing to sync during shutdown */
185 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
186 read_unlock(&pag->pag_ici_lock);
187 return EFSCORRUPTED;
188 }
189
c8e20be0 190 /* If we can't get a reference on the inode, it must be in reclaim. */
1da8eeca
DC
191 if (!igrab(inode)) {
192 read_unlock(&pag->pag_ici_lock);
193 return ENOENT;
194 }
195 read_unlock(&pag->pag_ici_lock);
196
197 if (is_bad_inode(inode) || xfs_iflags_test(ip, XFS_INEW)) {
198 IRELE(ip);
199 return ENOENT;
200 }
201
202 return 0;
203}
204
5a34d5cd
DC
205STATIC int
206xfs_sync_inode_data(
207 struct xfs_inode *ip,
75f3cb13 208 struct xfs_perag *pag,
5a34d5cd
DC
209 int flags)
210{
211 struct inode *inode = VFS_I(ip);
212 struct address_space *mapping = inode->i_mapping;
213 int error = 0;
214
75f3cb13
DC
215 error = xfs_sync_inode_valid(ip, pag);
216 if (error)
217 return error;
218
5a34d5cd
DC
219 if (!mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
220 goto out_wait;
221
222 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED)) {
223 if (flags & SYNC_TRYLOCK)
224 goto out_wait;
225 xfs_ilock(ip, XFS_IOLOCK_SHARED);
226 }
227
228 error = xfs_flush_pages(ip, 0, -1, (flags & SYNC_WAIT) ?
229 0 : XFS_B_ASYNC, FI_NONE);
230 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
231
232 out_wait:
b0710ccc 233 if (flags & SYNC_WAIT)
5a34d5cd 234 xfs_ioend_wait(ip);
75f3cb13 235 IRELE(ip);
5a34d5cd
DC
236 return error;
237}
238
845b6d0c
CH
239STATIC int
240xfs_sync_inode_attr(
241 struct xfs_inode *ip,
75f3cb13 242 struct xfs_perag *pag,
845b6d0c
CH
243 int flags)
244{
245 int error = 0;
246
75f3cb13
DC
247 error = xfs_sync_inode_valid(ip, pag);
248 if (error)
249 return error;
250
845b6d0c
CH
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
265 error = xfs_iflush(ip, (flags & SYNC_WAIT) ?
266 XFS_IFLUSH_SYNC : XFS_IFLUSH_DELWRI);
267
268 out_unlock:
269 xfs_iunlock(ip, XFS_ILOCK_SHARED);
75f3cb13 270 IRELE(ip);
845b6d0c
CH
271 return error;
272}
273
075fe102
CH
274/*
275 * Write out pagecache data for the whole filesystem.
276 */
683a8970 277int
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
075fe102 286 error = xfs_inode_ag_iterator(mp, xfs_sync_inode_data, flags,
c8e20be0 287 XFS_ICI_NO_TAG, 0);
075fe102
CH
288 if (error)
289 return XFS_ERROR(error);
e9f1c6ee 290
075fe102
CH
291 xfs_log_force(mp, 0,
292 (flags & SYNC_WAIT) ?
293 XFS_LOG_FORCE | XFS_LOG_SYNC :
294 XFS_LOG_FORCE);
295 return 0;
296}
e9f1c6ee 297
075fe102
CH
298/*
299 * Write out inode metadata (attributes) for the whole filesystem.
300 */
301int
302xfs_sync_attr(
303 struct xfs_mount *mp,
304 int flags)
305{
306 ASSERT((flags & ~SYNC_WAIT) == 0);
75f3cb13 307
075fe102 308 return xfs_inode_ag_iterator(mp, xfs_sync_inode_attr, flags,
c8e20be0 309 XFS_ICI_NO_TAG, 0);
fe4fa4b8
DC
310}
311
2af75df7
CH
312STATIC int
313xfs_commit_dummy_trans(
314 struct xfs_mount *mp,
dce5065a 315 uint flags)
2af75df7
CH
316{
317 struct xfs_inode *ip = mp->m_rootip;
318 struct xfs_trans *tp;
319 int error;
dce5065a
DC
320 int log_flags = XFS_LOG_FORCE;
321
322 if (flags & SYNC_WAIT)
323 log_flags |= XFS_LOG_SYNC;
2af75df7
CH
324
325 /*
326 * Put a dummy transaction in the log to tell recovery
327 * that all others are OK.
328 */
329 tp = xfs_trans_alloc(mp, XFS_TRANS_DUMMY1);
330 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
331 if (error) {
332 xfs_trans_cancel(tp, 0);
333 return error;
334 }
335
336 xfs_ilock(ip, XFS_ILOCK_EXCL);
337
338 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
339 xfs_trans_ihold(tp, ip);
340 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2af75df7 341 error = xfs_trans_commit(tp, 0);
2af75df7
CH
342 xfs_iunlock(ip, XFS_ILOCK_EXCL);
343
dce5065a 344 /* the log force ensures this transaction is pushed to disk */
2af75df7 345 xfs_log_force(mp, 0, log_flags);
dce5065a 346 return error;
2af75df7
CH
347}
348
e9f1c6ee 349int
2af75df7
CH
350xfs_sync_fsdata(
351 struct xfs_mount *mp,
352 int flags)
353{
354 struct xfs_buf *bp;
355 struct xfs_buf_log_item *bip;
356 int error = 0;
357
358 /*
359 * If this is xfssyncd() then only sync the superblock if we can
360 * lock it without sleeping and it is not pinned.
361 */
8b5403a6 362 if (flags & SYNC_TRYLOCK) {
2af75df7
CH
363 ASSERT(!(flags & SYNC_WAIT));
364
365 bp = xfs_getsb(mp, XFS_BUF_TRYLOCK);
366 if (!bp)
367 goto out;
368
369 bip = XFS_BUF_FSPRIVATE(bp, struct xfs_buf_log_item *);
370 if (!bip || !xfs_buf_item_dirty(bip) || XFS_BUF_ISPINNED(bp))
371 goto out_brelse;
372 } else {
373 bp = xfs_getsb(mp, 0);
374
375 /*
376 * If the buffer is pinned then push on the log so we won't
377 * get stuck waiting in the write for someone, maybe
378 * ourselves, to flush the log.
379 *
380 * Even though we just pushed the log above, we did not have
381 * the superblock buffer locked at that point so it can
382 * become pinned in between there and here.
383 */
384 if (XFS_BUF_ISPINNED(bp))
385 xfs_log_force(mp, 0, XFS_LOG_FORCE);
386 }
387
388
389 if (flags & SYNC_WAIT)
390 XFS_BUF_UNASYNC(bp);
391 else
392 XFS_BUF_ASYNC(bp);
393
dce5065a
DC
394 error = xfs_bwrite(mp, bp);
395 if (error)
396 return error;
397
398 /*
399 * If this is a data integrity sync make sure all pending buffers
400 * are flushed out for the log coverage check below.
401 */
402 if (flags & SYNC_WAIT)
403 xfs_flush_buftarg(mp->m_ddev_targp, 1);
404
405 if (xfs_log_need_covered(mp))
406 error = xfs_commit_dummy_trans(mp, flags);
407 return error;
2af75df7
CH
408
409 out_brelse:
410 xfs_buf_relse(bp);
411 out:
412 return error;
e9f1c6ee
DC
413}
414
415/*
a4e4c4f4
DC
416 * When remounting a filesystem read-only or freezing the filesystem, we have
417 * two phases to execute. This first phase is syncing the data before we
418 * quiesce the filesystem, and the second is flushing all the inodes out after
419 * we've waited for all the transactions created by the first phase to
420 * complete. The second phase ensures that the inodes are written to their
421 * location on disk rather than just existing in transactions in the log. This
422 * means after a quiesce there is no log replay required to write the inodes to
423 * disk (this is the main difference between a sync and a quiesce).
424 */
425/*
426 * First stage of freeze - no writers will make progress now we are here,
e9f1c6ee
DC
427 * so we flush delwri and delalloc buffers here, then wait for all I/O to
428 * complete. Data is frozen at that point. Metadata is not frozen,
a4e4c4f4
DC
429 * transactions can still occur here so don't bother flushing the buftarg
430 * because it'll just get dirty again.
e9f1c6ee
DC
431 */
432int
433xfs_quiesce_data(
434 struct xfs_mount *mp)
435{
436 int error;
437
438 /* push non-blocking */
075fe102 439 xfs_sync_data(mp, 0);
8b5403a6 440 xfs_qm_sync(mp, SYNC_TRYLOCK);
e9f1c6ee 441
c90b07e8 442 /* push and block till complete */
b0710ccc 443 xfs_sync_data(mp, SYNC_WAIT);
7d095257 444 xfs_qm_sync(mp, SYNC_WAIT);
e9f1c6ee 445
c90b07e8
DC
446 /* drop inode references pinned by filestreams */
447 xfs_filestream_flush(mp);
448
a4e4c4f4 449 /* write superblock and hoover up shutdown errors */
c90b07e8 450 error = xfs_sync_fsdata(mp, SYNC_WAIT);
e9f1c6ee 451
a4e4c4f4 452 /* flush data-only devices */
e9f1c6ee
DC
453 if (mp->m_rtdev_targp)
454 XFS_bflush(mp->m_rtdev_targp);
455
456 return error;
2af75df7
CH
457}
458
76bf105c
DC
459STATIC void
460xfs_quiesce_fs(
461 struct xfs_mount *mp)
462{
463 int count = 0, pincount;
464
465 xfs_flush_buftarg(mp->m_ddev_targp, 0);
abc10647 466 xfs_reclaim_inodes(mp, XFS_IFLUSH_DELWRI_ELSE_ASYNC);
76bf105c
DC
467
468 /*
469 * This loop must run at least twice. The first instance of the loop
470 * will flush most meta data but that will generate more meta data
471 * (typically directory updates). Which then must be flushed and
472 * logged before we can write the unmount record.
473 */
474 do {
075fe102 475 xfs_sync_attr(mp, SYNC_WAIT);
76bf105c
DC
476 pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1);
477 if (!pincount) {
478 delay(50);
479 count++;
480 }
481 } while (count < 2);
482}
483
484/*
485 * Second stage of a quiesce. The data is already synced, now we have to take
486 * care of the metadata. New transactions are already blocked, so we need to
487 * wait for any remaining transactions to drain out before proceding.
488 */
489void
490xfs_quiesce_attr(
491 struct xfs_mount *mp)
492{
493 int error = 0;
494
495 /* wait for all modifications to complete */
496 while (atomic_read(&mp->m_active_trans) > 0)
497 delay(100);
498
499 /* flush inodes and push all remaining buffers out to disk */
500 xfs_quiesce_fs(mp);
501
5e106572
FB
502 /*
503 * Just warn here till VFS can correctly support
504 * read-only remount without racing.
505 */
506 WARN_ON(atomic_read(&mp->m_active_trans) != 0);
76bf105c
DC
507
508 /* Push the superblock and write an unmount record */
509 error = xfs_log_sbcount(mp, 1);
510 if (error)
511 xfs_fs_cmn_err(CE_WARN, mp,
512 "xfs_attr_quiesce: failed to log sb changes. "
513 "Frozen image may not be consistent.");
514 xfs_log_unmount_write(mp);
515 xfs_unmountfs_writesb(mp);
516}
517
a167b17e
DC
518/*
519 * Enqueue a work item to be picked up by the vfs xfssyncd thread.
520 * Doing this has two advantages:
521 * - It saves on stack space, which is tight in certain situations
522 * - It can be used (with care) as a mechanism to avoid deadlocks.
523 * Flushing while allocating in a full filesystem requires both.
524 */
525STATIC void
526xfs_syncd_queue_work(
527 struct xfs_mount *mp,
528 void *data,
e43afd72
DC
529 void (*syncer)(struct xfs_mount *, void *),
530 struct completion *completion)
a167b17e 531{
a8d770d9 532 struct xfs_sync_work *work;
a167b17e 533
a8d770d9 534 work = kmem_alloc(sizeof(struct xfs_sync_work), KM_SLEEP);
a167b17e
DC
535 INIT_LIST_HEAD(&work->w_list);
536 work->w_syncer = syncer;
537 work->w_data = data;
538 work->w_mount = mp;
e43afd72 539 work->w_completion = completion;
a167b17e
DC
540 spin_lock(&mp->m_sync_lock);
541 list_add_tail(&work->w_list, &mp->m_sync_list);
542 spin_unlock(&mp->m_sync_lock);
543 wake_up_process(mp->m_sync_task);
544}
545
546/*
547 * Flush delayed allocate data, attempting to free up reserved space
548 * from existing allocations. At this point a new allocation attempt
549 * has failed with ENOSPC and we are in the process of scratching our
550 * heads, looking about for more room...
551 */
552STATIC void
a8d770d9 553xfs_flush_inodes_work(
a167b17e
DC
554 struct xfs_mount *mp,
555 void *arg)
556{
557 struct inode *inode = arg;
075fe102 558 xfs_sync_data(mp, SYNC_TRYLOCK);
b0710ccc 559 xfs_sync_data(mp, SYNC_TRYLOCK | SYNC_WAIT);
a167b17e
DC
560 iput(inode);
561}
562
563void
a8d770d9 564xfs_flush_inodes(
a167b17e
DC
565 xfs_inode_t *ip)
566{
567 struct inode *inode = VFS_I(ip);
e43afd72 568 DECLARE_COMPLETION_ONSTACK(completion);
a167b17e
DC
569
570 igrab(inode);
e43afd72
DC
571 xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_inodes_work, &completion);
572 wait_for_completion(&completion);
a167b17e
DC
573 xfs_log_force(ip->i_mount, (xfs_lsn_t)0, XFS_LOG_FORCE|XFS_LOG_SYNC);
574}
575
aacaa880
DC
576/*
577 * Every sync period we need to unpin all items, reclaim inodes, sync
578 * quota and write out the superblock. We might need to cover the log
579 * to indicate it is idle.
580 */
a167b17e
DC
581STATIC void
582xfs_sync_worker(
583 struct xfs_mount *mp,
584 void *unused)
585{
586 int error;
587
aacaa880
DC
588 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
589 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
abc10647 590 xfs_reclaim_inodes(mp, XFS_IFLUSH_DELWRI_ELSE_ASYNC);
aacaa880 591 /* dgc: errors ignored here */
8b5403a6
CH
592 error = xfs_qm_sync(mp, SYNC_TRYLOCK);
593 error = xfs_sync_fsdata(mp, SYNC_TRYLOCK);
aacaa880 594 }
a167b17e
DC
595 mp->m_sync_seq++;
596 wake_up(&mp->m_wait_single_sync_task);
597}
598
599STATIC int
600xfssyncd(
601 void *arg)
602{
603 struct xfs_mount *mp = arg;
604 long timeleft;
a8d770d9 605 xfs_sync_work_t *work, *n;
a167b17e
DC
606 LIST_HEAD (tmp);
607
608 set_freezable();
609 timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10);
610 for (;;) {
611 timeleft = schedule_timeout_interruptible(timeleft);
612 /* swsusp */
613 try_to_freeze();
614 if (kthread_should_stop() && list_empty(&mp->m_sync_list))
615 break;
616
617 spin_lock(&mp->m_sync_lock);
618 /*
619 * We can get woken by laptop mode, to do a sync -
620 * that's the (only!) case where the list would be
621 * empty with time remaining.
622 */
623 if (!timeleft || list_empty(&mp->m_sync_list)) {
624 if (!timeleft)
625 timeleft = xfs_syncd_centisecs *
626 msecs_to_jiffies(10);
627 INIT_LIST_HEAD(&mp->m_sync_work.w_list);
628 list_add_tail(&mp->m_sync_work.w_list,
629 &mp->m_sync_list);
630 }
631 list_for_each_entry_safe(work, n, &mp->m_sync_list, w_list)
632 list_move(&work->w_list, &tmp);
633 spin_unlock(&mp->m_sync_lock);
634
635 list_for_each_entry_safe(work, n, &tmp, w_list) {
636 (*work->w_syncer)(mp, work->w_data);
637 list_del(&work->w_list);
638 if (work == &mp->m_sync_work)
639 continue;
e43afd72
DC
640 if (work->w_completion)
641 complete(work->w_completion);
a167b17e
DC
642 kmem_free(work);
643 }
644 }
645
646 return 0;
647}
648
649int
650xfs_syncd_init(
651 struct xfs_mount *mp)
652{
653 mp->m_sync_work.w_syncer = xfs_sync_worker;
654 mp->m_sync_work.w_mount = mp;
e43afd72 655 mp->m_sync_work.w_completion = NULL;
a167b17e
DC
656 mp->m_sync_task = kthread_run(xfssyncd, mp, "xfssyncd");
657 if (IS_ERR(mp->m_sync_task))
658 return -PTR_ERR(mp->m_sync_task);
659 return 0;
660}
661
662void
663xfs_syncd_stop(
664 struct xfs_mount *mp)
665{
666 kthread_stop(mp->m_sync_task);
667}
668
bc990f5c
CH
669void
670__xfs_inode_set_reclaim_tag(
671 struct xfs_perag *pag,
672 struct xfs_inode *ip)
673{
674 radix_tree_tag_set(&pag->pag_ici_root,
675 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
676 XFS_ICI_RECLAIM_TAG);
677}
678
11654513
DC
679/*
680 * We set the inode flag atomically with the radix tree tag.
681 * Once we get tag lookups on the radix tree, this inode flag
682 * can go away.
683 */
396beb85
DC
684void
685xfs_inode_set_reclaim_tag(
686 xfs_inode_t *ip)
687{
688 xfs_mount_t *mp = ip->i_mount;
689 xfs_perag_t *pag = xfs_get_perag(mp, ip->i_ino);
690
691 read_lock(&pag->pag_ici_lock);
692 spin_lock(&ip->i_flags_lock);
bc990f5c 693 __xfs_inode_set_reclaim_tag(pag, ip);
11654513 694 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
396beb85
DC
695 spin_unlock(&ip->i_flags_lock);
696 read_unlock(&pag->pag_ici_lock);
697 xfs_put_perag(mp, pag);
698}
699
700void
701__xfs_inode_clear_reclaim_tag(
702 xfs_mount_t *mp,
703 xfs_perag_t *pag,
704 xfs_inode_t *ip)
705{
706 radix_tree_tag_clear(&pag->pag_ici_root,
707 XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
708}
709
75f3cb13 710STATIC int
c8e20be0 711xfs_reclaim_inode(
75f3cb13
DC
712 struct xfs_inode *ip,
713 struct xfs_perag *pag,
c8e20be0 714 int sync_mode)
fce08f2f 715{
c8e20be0
DC
716 /*
717 * The radix tree lock here protects a thread in xfs_iget from racing
718 * with us starting reclaim on the inode. Once we have the
719 * XFS_IRECLAIM flag set it will not touch us.
720 */
721 spin_lock(&ip->i_flags_lock);
722 ASSERT_ALWAYS(__xfs_iflags_test(ip, XFS_IRECLAIMABLE));
723 if (__xfs_iflags_test(ip, XFS_IRECLAIM)) {
724 /* ignore as it is already under reclaim */
725 spin_unlock(&ip->i_flags_lock);
726 write_unlock(&pag->pag_ici_lock);
75f3cb13 727 return 0;
fce08f2f 728 }
c8e20be0
DC
729 __xfs_iflags_set(ip, XFS_IRECLAIM);
730 spin_unlock(&ip->i_flags_lock);
731 write_unlock(&pag->pag_ici_lock);
732
733 /*
734 * If the inode is still dirty, then flush it out. If the inode
735 * is not in the AIL, then it will be OK to flush it delwri as
736 * long as xfs_iflush() does not keep any references to the inode.
737 * We leave that decision up to xfs_iflush() since it has the
738 * knowledge of whether it's OK to simply do a delwri flush of
739 * the inode or whether we need to wait until the inode is
740 * pulled from the AIL.
741 * We get the flush lock regardless, though, just to make sure
742 * we don't free it while it is being flushed.
743 */
744 xfs_ilock(ip, XFS_ILOCK_EXCL);
745 xfs_iflock(ip);
7a3be02b 746
c8e20be0
DC
747 /*
748 * In the case of a forced shutdown we rely on xfs_iflush() to
749 * wait for the inode to be unpinned before returning an error.
750 */
751 if (!is_bad_inode(VFS_I(ip)) && xfs_iflush(ip, sync_mode) == 0) {
752 /* synchronize with xfs_iflush_done */
753 xfs_iflock(ip);
754 xfs_ifunlock(ip);
755 }
756
757 xfs_iunlock(ip, XFS_ILOCK_EXCL);
758 xfs_ireclaim(ip);
759 return 0;
7a3be02b
DC
760}
761
762int
763xfs_reclaim_inodes(
764 xfs_mount_t *mp,
7a3be02b
DC
765 int mode)
766{
c8e20be0
DC
767 return xfs_inode_ag_iterator(mp, xfs_reclaim_inode, mode,
768 XFS_ICI_RECLAIM_TAG, 1);
fce08f2f 769}