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