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