xfs: split out inode walk inode grabbing
[pandora-kernel.git] / 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_mount.h"
28 #include "xfs_bmap_btree.h"
29 #include "xfs_inode.h"
30 #include "xfs_dinode.h"
31 #include "xfs_error.h"
32 #include "xfs_filestream.h"
33 #include "xfs_vnodeops.h"
34 #include "xfs_inode_item.h"
35 #include "xfs_quota.h"
36 #include "xfs_trace.h"
37 #include "xfs_fsops.h"
38
39 #include <linux/kthread.h>
40 #include <linux/freezer.h>
41
42 STATIC int
43 xfs_inode_ag_walk_grab(
44         struct xfs_inode        *ip)
45 {
46         struct inode            *inode = VFS_I(ip);
47
48         /* nothing to sync during shutdown */
49         if (XFS_FORCED_SHUTDOWN(ip->i_mount))
50                 return EFSCORRUPTED;
51
52         /* avoid new or reclaimable inodes. Leave for reclaim code to flush */
53         if (xfs_iflags_test(ip, XFS_INEW | XFS_IRECLAIMABLE | XFS_IRECLAIM))
54                 return ENOENT;
55
56         /* If we can't grab the inode, it must on it's way to reclaim. */
57         if (!igrab(inode))
58                 return ENOENT;
59
60         if (is_bad_inode(inode)) {
61                 IRELE(ip);
62                 return ENOENT;
63         }
64
65         /* inode is valid */
66         return 0;
67 }
68
69
70 STATIC int
71 xfs_inode_ag_walk(
72         struct xfs_mount        *mp,
73         struct xfs_perag        *pag,
74         int                     (*execute)(struct xfs_inode *ip,
75                                            struct xfs_perag *pag, int flags),
76         int                     flags)
77 {
78         uint32_t                first_index;
79         int                     last_error = 0;
80         int                     skipped;
81         int                     done;
82
83 restart:
84         done = 0;
85         skipped = 0;
86         first_index = 0;
87         do {
88                 int             error = 0;
89                 int             nr_found;
90                 xfs_inode_t     *ip;
91
92                 read_lock(&pag->pag_ici_lock);
93                 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
94                                 (void **)&ip, first_index, 1);
95                 if (!nr_found) {
96                         read_unlock(&pag->pag_ici_lock);
97                         break;
98                 }
99
100                 /*
101                  * Update the index for the next lookup. Catch overflows
102                  * into the next AG range which can occur if we have inodes
103                  * in the last block of the AG and we are currently
104                  * pointing to the last inode.
105                  */
106                 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
107                 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
108                         done = 1;
109
110                 if (xfs_inode_ag_walk_grab(ip)) {
111                         read_unlock(&pag->pag_ici_lock);
112                         continue;
113                 }
114                 read_unlock(&pag->pag_ici_lock);
115
116                 error = execute(ip, pag, flags);
117                 IRELE(ip);
118                 if (error == EAGAIN) {
119                         skipped++;
120                         continue;
121                 }
122                 if (error)
123                         last_error = error;
124
125                 /* bail out if the filesystem is corrupted.  */
126                 if (error == EFSCORRUPTED)
127                         break;
128
129         } while (!done);
130
131         if (skipped) {
132                 delay(1);
133                 goto restart;
134         }
135         return last_error;
136 }
137
138 int
139 xfs_inode_ag_iterator(
140         struct xfs_mount        *mp,
141         int                     (*execute)(struct xfs_inode *ip,
142                                            struct xfs_perag *pag, int flags),
143         int                     flags)
144 {
145         struct xfs_perag        *pag;
146         int                     error = 0;
147         int                     last_error = 0;
148         xfs_agnumber_t          ag;
149
150         ag = 0;
151         while ((pag = xfs_perag_get(mp, ag))) {
152                 ag = pag->pag_agno + 1;
153                 error = xfs_inode_ag_walk(mp, pag, execute, flags);
154                 xfs_perag_put(pag);
155                 if (error) {
156                         last_error = error;
157                         if (error == EFSCORRUPTED)
158                                 break;
159                 }
160         }
161         return XFS_ERROR(last_error);
162 }
163
164 STATIC int
165 xfs_sync_inode_data(
166         struct xfs_inode        *ip,
167         struct xfs_perag        *pag,
168         int                     flags)
169 {
170         struct inode            *inode = VFS_I(ip);
171         struct address_space *mapping = inode->i_mapping;
172         int                     error = 0;
173
174         if (!mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
175                 goto out_wait;
176
177         if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED)) {
178                 if (flags & SYNC_TRYLOCK)
179                         goto out_wait;
180                 xfs_ilock(ip, XFS_IOLOCK_SHARED);
181         }
182
183         error = xfs_flush_pages(ip, 0, -1, (flags & SYNC_WAIT) ?
184                                 0 : XBF_ASYNC, FI_NONE);
185         xfs_iunlock(ip, XFS_IOLOCK_SHARED);
186
187  out_wait:
188         if (flags & SYNC_WAIT)
189                 xfs_ioend_wait(ip);
190         return error;
191 }
192
193 STATIC int
194 xfs_sync_inode_attr(
195         struct xfs_inode        *ip,
196         struct xfs_perag        *pag,
197         int                     flags)
198 {
199         int                     error = 0;
200
201         xfs_ilock(ip, XFS_ILOCK_SHARED);
202         if (xfs_inode_clean(ip))
203                 goto out_unlock;
204         if (!xfs_iflock_nowait(ip)) {
205                 if (!(flags & SYNC_WAIT))
206                         goto out_unlock;
207                 xfs_iflock(ip);
208         }
209
210         if (xfs_inode_clean(ip)) {
211                 xfs_ifunlock(ip);
212                 goto out_unlock;
213         }
214
215         error = xfs_iflush(ip, flags);
216
217  out_unlock:
218         xfs_iunlock(ip, XFS_ILOCK_SHARED);
219         return error;
220 }
221
222 /*
223  * Write out pagecache data for the whole filesystem.
224  */
225 STATIC int
226 xfs_sync_data(
227         struct xfs_mount        *mp,
228         int                     flags)
229 {
230         int                     error;
231
232         ASSERT((flags & ~(SYNC_TRYLOCK|SYNC_WAIT)) == 0);
233
234         error = xfs_inode_ag_iterator(mp, xfs_sync_inode_data, flags);
235         if (error)
236                 return XFS_ERROR(error);
237
238         xfs_log_force(mp, (flags & SYNC_WAIT) ? XFS_LOG_SYNC : 0);
239         return 0;
240 }
241
242 /*
243  * Write out inode metadata (attributes) for the whole filesystem.
244  */
245 STATIC int
246 xfs_sync_attr(
247         struct xfs_mount        *mp,
248         int                     flags)
249 {
250         ASSERT((flags & ~SYNC_WAIT) == 0);
251
252         return xfs_inode_ag_iterator(mp, xfs_sync_inode_attr, flags);
253 }
254
255 STATIC int
256 xfs_sync_fsdata(
257         struct xfs_mount        *mp)
258 {
259         struct xfs_buf          *bp;
260
261         /*
262          * If the buffer is pinned then push on the log so we won't get stuck
263          * waiting in the write for someone, maybe ourselves, to flush the log.
264          *
265          * Even though we just pushed the log above, we did not have the
266          * superblock buffer locked at that point so it can become pinned in
267          * between there and here.
268          */
269         bp = xfs_getsb(mp, 0);
270         if (XFS_BUF_ISPINNED(bp))
271                 xfs_log_force(mp, 0);
272
273         return xfs_bwrite(mp, bp);
274 }
275
276 /*
277  * When remounting a filesystem read-only or freezing the filesystem, we have
278  * two phases to execute. This first phase is syncing the data before we
279  * quiesce the filesystem, and the second is flushing all the inodes out after
280  * we've waited for all the transactions created by the first phase to
281  * complete. The second phase ensures that the inodes are written to their
282  * location on disk rather than just existing in transactions in the log. This
283  * means after a quiesce there is no log replay required to write the inodes to
284  * disk (this is the main difference between a sync and a quiesce).
285  */
286 /*
287  * First stage of freeze - no writers will make progress now we are here,
288  * so we flush delwri and delalloc buffers here, then wait for all I/O to
289  * complete.  Data is frozen at that point. Metadata is not frozen,
290  * transactions can still occur here so don't bother flushing the buftarg
291  * because it'll just get dirty again.
292  */
293 int
294 xfs_quiesce_data(
295         struct xfs_mount        *mp)
296 {
297         int                     error, error2 = 0;
298
299         /* push non-blocking */
300         xfs_sync_data(mp, 0);
301         xfs_qm_sync(mp, SYNC_TRYLOCK);
302
303         /* push and block till complete */
304         xfs_sync_data(mp, SYNC_WAIT);
305         xfs_qm_sync(mp, SYNC_WAIT);
306
307         /* write superblock and hoover up shutdown errors */
308         error = xfs_sync_fsdata(mp);
309
310         /* make sure all delwri buffers are written out */
311         xfs_flush_buftarg(mp->m_ddev_targp, 1);
312
313         /* mark the log as covered if needed */
314         if (xfs_log_need_covered(mp))
315                 error2 = xfs_fs_log_dummy(mp, SYNC_WAIT);
316
317         /* flush data-only devices */
318         if (mp->m_rtdev_targp)
319                 XFS_bflush(mp->m_rtdev_targp);
320
321         return error ? error : error2;
322 }
323
324 STATIC void
325 xfs_quiesce_fs(
326         struct xfs_mount        *mp)
327 {
328         int     count = 0, pincount;
329
330         xfs_reclaim_inodes(mp, 0);
331         xfs_flush_buftarg(mp->m_ddev_targp, 0);
332
333         /*
334          * This loop must run at least twice.  The first instance of the loop
335          * will flush most meta data but that will generate more meta data
336          * (typically directory updates).  Which then must be flushed and
337          * logged before we can write the unmount record. We also so sync
338          * reclaim of inodes to catch any that the above delwri flush skipped.
339          */
340         do {
341                 xfs_reclaim_inodes(mp, SYNC_WAIT);
342                 xfs_sync_attr(mp, SYNC_WAIT);
343                 pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1);
344                 if (!pincount) {
345                         delay(50);
346                         count++;
347                 }
348         } while (count < 2);
349 }
350
351 /*
352  * Second stage of a quiesce. The data is already synced, now we have to take
353  * care of the metadata. New transactions are already blocked, so we need to
354  * wait for any remaining transactions to drain out before proceding.
355  */
356 void
357 xfs_quiesce_attr(
358         struct xfs_mount        *mp)
359 {
360         int     error = 0;
361
362         /* wait for all modifications to complete */
363         while (atomic_read(&mp->m_active_trans) > 0)
364                 delay(100);
365
366         /* flush inodes and push all remaining buffers out to disk */
367         xfs_quiesce_fs(mp);
368
369         /*
370          * Just warn here till VFS can correctly support
371          * read-only remount without racing.
372          */
373         WARN_ON(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         struct completion *completion)
398 {
399         struct xfs_sync_work *work;
400
401         work = kmem_alloc(sizeof(struct xfs_sync_work), KM_SLEEP);
402         INIT_LIST_HEAD(&work->w_list);
403         work->w_syncer = syncer;
404         work->w_data = data;
405         work->w_mount = mp;
406         work->w_completion = completion;
407         spin_lock(&mp->m_sync_lock);
408         list_add_tail(&work->w_list, &mp->m_sync_list);
409         spin_unlock(&mp->m_sync_lock);
410         wake_up_process(mp->m_sync_task);
411 }
412
413 /*
414  * Flush delayed allocate data, attempting to free up reserved space
415  * from existing allocations.  At this point a new allocation attempt
416  * has failed with ENOSPC and we are in the process of scratching our
417  * heads, looking about for more room...
418  */
419 STATIC void
420 xfs_flush_inodes_work(
421         struct xfs_mount *mp,
422         void            *arg)
423 {
424         struct inode    *inode = arg;
425         xfs_sync_data(mp, SYNC_TRYLOCK);
426         xfs_sync_data(mp, SYNC_TRYLOCK | SYNC_WAIT);
427         iput(inode);
428 }
429
430 void
431 xfs_flush_inodes(
432         xfs_inode_t     *ip)
433 {
434         struct inode    *inode = VFS_I(ip);
435         DECLARE_COMPLETION_ONSTACK(completion);
436
437         igrab(inode);
438         xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_inodes_work, &completion);
439         wait_for_completion(&completion);
440         xfs_log_force(ip->i_mount, XFS_LOG_SYNC);
441 }
442
443 /*
444  * Every sync period we need to unpin all items, reclaim inodes and sync
445  * disk quotas.  We might need to cover the log to indicate that the
446  * filesystem is idle and not frozen.
447  */
448 STATIC void
449 xfs_sync_worker(
450         struct xfs_mount *mp,
451         void            *unused)
452 {
453         int             error;
454
455         if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
456                 xfs_log_force(mp, 0);
457                 xfs_reclaim_inodes(mp, 0);
458                 /* dgc: errors ignored here */
459                 error = xfs_qm_sync(mp, SYNC_TRYLOCK);
460                 if (mp->m_super->s_frozen == SB_UNFROZEN &&
461                     xfs_log_need_covered(mp))
462                         error = xfs_fs_log_dummy(mp, 0);
463         }
464         mp->m_sync_seq++;
465         wake_up(&mp->m_wait_single_sync_task);
466 }
467
468 STATIC int
469 xfssyncd(
470         void                    *arg)
471 {
472         struct xfs_mount        *mp = arg;
473         long                    timeleft;
474         xfs_sync_work_t         *work, *n;
475         LIST_HEAD               (tmp);
476
477         set_freezable();
478         timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10);
479         for (;;) {
480                 if (list_empty(&mp->m_sync_list))
481                         timeleft = schedule_timeout_interruptible(timeleft);
482                 /* swsusp */
483                 try_to_freeze();
484                 if (kthread_should_stop() && list_empty(&mp->m_sync_list))
485                         break;
486
487                 spin_lock(&mp->m_sync_lock);
488                 /*
489                  * We can get woken by laptop mode, to do a sync -
490                  * that's the (only!) case where the list would be
491                  * empty with time remaining.
492                  */
493                 if (!timeleft || list_empty(&mp->m_sync_list)) {
494                         if (!timeleft)
495                                 timeleft = xfs_syncd_centisecs *
496                                                         msecs_to_jiffies(10);
497                         INIT_LIST_HEAD(&mp->m_sync_work.w_list);
498                         list_add_tail(&mp->m_sync_work.w_list,
499                                         &mp->m_sync_list);
500                 }
501                 list_splice_init(&mp->m_sync_list, &tmp);
502                 spin_unlock(&mp->m_sync_lock);
503
504                 list_for_each_entry_safe(work, n, &tmp, w_list) {
505                         (*work->w_syncer)(mp, work->w_data);
506                         list_del(&work->w_list);
507                         if (work == &mp->m_sync_work)
508                                 continue;
509                         if (work->w_completion)
510                                 complete(work->w_completion);
511                         kmem_free(work);
512                 }
513         }
514
515         return 0;
516 }
517
518 int
519 xfs_syncd_init(
520         struct xfs_mount        *mp)
521 {
522         mp->m_sync_work.w_syncer = xfs_sync_worker;
523         mp->m_sync_work.w_mount = mp;
524         mp->m_sync_work.w_completion = NULL;
525         mp->m_sync_task = kthread_run(xfssyncd, mp, "xfssyncd/%s", mp->m_fsname);
526         if (IS_ERR(mp->m_sync_task))
527                 return -PTR_ERR(mp->m_sync_task);
528         return 0;
529 }
530
531 void
532 xfs_syncd_stop(
533         struct xfs_mount        *mp)
534 {
535         kthread_stop(mp->m_sync_task);
536 }
537
538 void
539 __xfs_inode_set_reclaim_tag(
540         struct xfs_perag        *pag,
541         struct xfs_inode        *ip)
542 {
543         radix_tree_tag_set(&pag->pag_ici_root,
544                            XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
545                            XFS_ICI_RECLAIM_TAG);
546
547         if (!pag->pag_ici_reclaimable) {
548                 /* propagate the reclaim tag up into the perag radix tree */
549                 spin_lock(&ip->i_mount->m_perag_lock);
550                 radix_tree_tag_set(&ip->i_mount->m_perag_tree,
551                                 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
552                                 XFS_ICI_RECLAIM_TAG);
553                 spin_unlock(&ip->i_mount->m_perag_lock);
554                 trace_xfs_perag_set_reclaim(ip->i_mount, pag->pag_agno,
555                                                         -1, _RET_IP_);
556         }
557         pag->pag_ici_reclaimable++;
558 }
559
560 /*
561  * We set the inode flag atomically with the radix tree tag.
562  * Once we get tag lookups on the radix tree, this inode flag
563  * can go away.
564  */
565 void
566 xfs_inode_set_reclaim_tag(
567         xfs_inode_t     *ip)
568 {
569         struct xfs_mount *mp = ip->i_mount;
570         struct xfs_perag *pag;
571
572         pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
573         write_lock(&pag->pag_ici_lock);
574         spin_lock(&ip->i_flags_lock);
575         __xfs_inode_set_reclaim_tag(pag, ip);
576         __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
577         spin_unlock(&ip->i_flags_lock);
578         write_unlock(&pag->pag_ici_lock);
579         xfs_perag_put(pag);
580 }
581
582 STATIC void
583 __xfs_inode_clear_reclaim(
584         xfs_perag_t     *pag,
585         xfs_inode_t     *ip)
586 {
587         pag->pag_ici_reclaimable--;
588         if (!pag->pag_ici_reclaimable) {
589                 /* clear the reclaim tag from the perag radix tree */
590                 spin_lock(&ip->i_mount->m_perag_lock);
591                 radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
592                                 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
593                                 XFS_ICI_RECLAIM_TAG);
594                 spin_unlock(&ip->i_mount->m_perag_lock);
595                 trace_xfs_perag_clear_reclaim(ip->i_mount, pag->pag_agno,
596                                                         -1, _RET_IP_);
597         }
598 }
599
600 void
601 __xfs_inode_clear_reclaim_tag(
602         xfs_mount_t     *mp,
603         xfs_perag_t     *pag,
604         xfs_inode_t     *ip)
605 {
606         radix_tree_tag_clear(&pag->pag_ici_root,
607                         XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
608         __xfs_inode_clear_reclaim(pag, ip);
609 }
610
611 /*
612  * Inodes in different states need to be treated differently, and the return
613  * value of xfs_iflush is not sufficient to get this right. The following table
614  * lists the inode states and the reclaim actions necessary for non-blocking
615  * reclaim:
616  *
617  *
618  *      inode state          iflush ret         required action
619  *      ---------------      ----------         ---------------
620  *      bad                     -               reclaim
621  *      shutdown                EIO             unpin and reclaim
622  *      clean, unpinned         0               reclaim
623  *      stale, unpinned         0               reclaim
624  *      clean, pinned(*)        0               requeue
625  *      stale, pinned           EAGAIN          requeue
626  *      dirty, delwri ok        0               requeue
627  *      dirty, delwri blocked   EAGAIN          requeue
628  *      dirty, sync flush       0               reclaim
629  *
630  * (*) dgc: I don't think the clean, pinned state is possible but it gets
631  * handled anyway given the order of checks implemented.
632  *
633  * As can be seen from the table, the return value of xfs_iflush() is not
634  * sufficient to correctly decide the reclaim action here. The checks in
635  * xfs_iflush() might look like duplicates, but they are not.
636  *
637  * Also, because we get the flush lock first, we know that any inode that has
638  * been flushed delwri has had the flush completed by the time we check that
639  * the inode is clean. The clean inode check needs to be done before flushing
640  * the inode delwri otherwise we would loop forever requeuing clean inodes as
641  * we cannot tell apart a successful delwri flush and a clean inode from the
642  * return value of xfs_iflush().
643  *
644  * Note that because the inode is flushed delayed write by background
645  * writeback, the flush lock may already be held here and waiting on it can
646  * result in very long latencies. Hence for sync reclaims, where we wait on the
647  * flush lock, the caller should push out delayed write inodes first before
648  * trying to reclaim them to minimise the amount of time spent waiting. For
649  * background relaim, we just requeue the inode for the next pass.
650  *
651  * Hence the order of actions after gaining the locks should be:
652  *      bad             => reclaim
653  *      shutdown        => unpin and reclaim
654  *      pinned, delwri  => requeue
655  *      pinned, sync    => unpin
656  *      stale           => reclaim
657  *      clean           => reclaim
658  *      dirty, delwri   => flush and requeue
659  *      dirty, sync     => flush, wait and reclaim
660  */
661 STATIC int
662 xfs_reclaim_inode(
663         struct xfs_inode        *ip,
664         struct xfs_perag        *pag,
665         int                     sync_mode)
666 {
667         int     error = 0;
668
669         /*
670          * The radix tree lock here protects a thread in xfs_iget from racing
671          * with us starting reclaim on the inode.  Once we have the
672          * XFS_IRECLAIM flag set it will not touch us.
673          */
674         spin_lock(&ip->i_flags_lock);
675         ASSERT_ALWAYS(__xfs_iflags_test(ip, XFS_IRECLAIMABLE));
676         if (__xfs_iflags_test(ip, XFS_IRECLAIM)) {
677                 /* ignore as it is already under reclaim */
678                 spin_unlock(&ip->i_flags_lock);
679                 write_unlock(&pag->pag_ici_lock);
680                 return 0;
681         }
682         __xfs_iflags_set(ip, XFS_IRECLAIM);
683         spin_unlock(&ip->i_flags_lock);
684         write_unlock(&pag->pag_ici_lock);
685
686         xfs_ilock(ip, XFS_ILOCK_EXCL);
687         if (!xfs_iflock_nowait(ip)) {
688                 if (!(sync_mode & SYNC_WAIT))
689                         goto out;
690                 xfs_iflock(ip);
691         }
692
693         if (is_bad_inode(VFS_I(ip)))
694                 goto reclaim;
695         if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
696                 xfs_iunpin_wait(ip);
697                 goto reclaim;
698         }
699         if (xfs_ipincount(ip)) {
700                 if (!(sync_mode & SYNC_WAIT)) {
701                         xfs_ifunlock(ip);
702                         goto out;
703                 }
704                 xfs_iunpin_wait(ip);
705         }
706         if (xfs_iflags_test(ip, XFS_ISTALE))
707                 goto reclaim;
708         if (xfs_inode_clean(ip))
709                 goto reclaim;
710
711         /* Now we have an inode that needs flushing */
712         error = xfs_iflush(ip, sync_mode);
713         if (sync_mode & SYNC_WAIT) {
714                 xfs_iflock(ip);
715                 goto reclaim;
716         }
717
718         /*
719          * When we have to flush an inode but don't have SYNC_WAIT set, we
720          * flush the inode out using a delwri buffer and wait for the next
721          * call into reclaim to find it in a clean state instead of waiting for
722          * it now. We also don't return errors here - if the error is transient
723          * then the next reclaim pass will flush the inode, and if the error
724          * is permanent then the next sync reclaim will reclaim the inode and
725          * pass on the error.
726          */
727         if (error && error != EAGAIN && !XFS_FORCED_SHUTDOWN(ip->i_mount)) {
728                 xfs_fs_cmn_err(CE_WARN, ip->i_mount,
729                         "inode 0x%llx background reclaim flush failed with %d",
730                         (long long)ip->i_ino, error);
731         }
732 out:
733         xfs_iflags_clear(ip, XFS_IRECLAIM);
734         xfs_iunlock(ip, XFS_ILOCK_EXCL);
735         /*
736          * We could return EAGAIN here to make reclaim rescan the inode tree in
737          * a short while. However, this just burns CPU time scanning the tree
738          * waiting for IO to complete and xfssyncd never goes back to the idle
739          * state. Instead, return 0 to let the next scheduled background reclaim
740          * attempt to reclaim the inode again.
741          */
742         return 0;
743
744 reclaim:
745         xfs_ifunlock(ip);
746         xfs_iunlock(ip, XFS_ILOCK_EXCL);
747
748         XFS_STATS_INC(xs_ig_reclaims);
749         /*
750          * Remove the inode from the per-AG radix tree.
751          *
752          * Because radix_tree_delete won't complain even if the item was never
753          * added to the tree assert that it's been there before to catch
754          * problems with the inode life time early on.
755          */
756         write_lock(&pag->pag_ici_lock);
757         if (!radix_tree_delete(&pag->pag_ici_root,
758                                 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino)))
759                 ASSERT(0);
760         __xfs_inode_clear_reclaim(pag, ip);
761         write_unlock(&pag->pag_ici_lock);
762
763         /*
764          * Here we do an (almost) spurious inode lock in order to coordinate
765          * with inode cache radix tree lookups.  This is because the lookup
766          * can reference the inodes in the cache without taking references.
767          *
768          * We make that OK here by ensuring that we wait until the inode is
769          * unlocked after the lookup before we go ahead and free it.  We get
770          * both the ilock and the iolock because the code may need to drop the
771          * ilock one but will still hold the iolock.
772          */
773         xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
774         xfs_qm_dqdetach(ip);
775         xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
776
777         xfs_inode_free(ip);
778         return error;
779
780 }
781
782 /*
783  * Walk the AGs and reclaim the inodes in them. Even if the filesystem is
784  * corrupted, we still want to try to reclaim all the inodes. If we don't,
785  * then a shut down during filesystem unmount reclaim walk leak all the
786  * unreclaimed inodes.
787  */
788 int
789 xfs_reclaim_inodes_ag(
790         struct xfs_mount        *mp,
791         int                     flags,
792         int                     *nr_to_scan)
793 {
794         struct xfs_perag        *pag;
795         int                     error = 0;
796         int                     last_error = 0;
797         xfs_agnumber_t          ag;
798
799         ag = 0;
800         while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
801                 unsigned long   first_index = 0;
802                 int             done = 0;
803
804                 ag = pag->pag_agno + 1;
805
806                 do {
807                         struct xfs_inode *ip;
808                         int     nr_found;
809
810                         write_lock(&pag->pag_ici_lock);
811                         nr_found = radix_tree_gang_lookup_tag(&pag->pag_ici_root,
812                                         (void **)&ip, first_index, 1,
813                                         XFS_ICI_RECLAIM_TAG);
814                         if (!nr_found) {
815                                 write_unlock(&pag->pag_ici_lock);
816                                 break;
817                         }
818
819                         /*
820                          * Update the index for the next lookup. Catch overflows
821                          * into the next AG range which can occur if we have inodes
822                          * in the last block of the AG and we are currently
823                          * pointing to the last inode.
824                          */
825                         first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
826                         if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
827                                 done = 1;
828
829                         error = xfs_reclaim_inode(ip, pag, flags);
830                         if (error && last_error != EFSCORRUPTED)
831                                 last_error = error;
832
833                 } while (!done && (*nr_to_scan)--);
834
835                 xfs_perag_put(pag);
836         }
837         return XFS_ERROR(last_error);
838 }
839
840 int
841 xfs_reclaim_inodes(
842         xfs_mount_t     *mp,
843         int             mode)
844 {
845         int             nr_to_scan = INT_MAX;
846
847         return xfs_reclaim_inodes_ag(mp, mode, &nr_to_scan);
848 }
849
850 /*
851  * Shrinker infrastructure.
852  */
853 static int
854 xfs_reclaim_inode_shrink(
855         struct shrinker *shrink,
856         int             nr_to_scan,
857         gfp_t           gfp_mask)
858 {
859         struct xfs_mount *mp;
860         struct xfs_perag *pag;
861         xfs_agnumber_t  ag;
862         int             reclaimable;
863
864         mp = container_of(shrink, struct xfs_mount, m_inode_shrink);
865         if (nr_to_scan) {
866                 if (!(gfp_mask & __GFP_FS))
867                         return -1;
868
869                 xfs_reclaim_inodes_ag(mp, 0, &nr_to_scan);
870                 /* terminate if we don't exhaust the scan */
871                 if (nr_to_scan > 0)
872                         return -1;
873        }
874
875         reclaimable = 0;
876         ag = 0;
877         while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
878                 ag = pag->pag_agno + 1;
879                 reclaimable += pag->pag_ici_reclaimable;
880                 xfs_perag_put(pag);
881         }
882         return reclaimable;
883 }
884
885 void
886 xfs_inode_shrinker_register(
887         struct xfs_mount        *mp)
888 {
889         mp->m_inode_shrink.shrink = xfs_reclaim_inode_shrink;
890         mp->m_inode_shrink.seeks = DEFAULT_SEEKS;
891         register_shrinker(&mp->m_inode_shrink);
892 }
893
894 void
895 xfs_inode_shrinker_unregister(
896         struct xfs_mount        *mp)
897 {
898         unregister_shrinker(&mp->m_inode_shrink);
899 }