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