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