xfs: merge xfs_ag.h into xfs_format.h
[firefly-linux-kernel-4.4.55.git] / fs / xfs / xfs_icache.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_format.h"
21 #include "xfs_log_format.h"
22 #include "xfs_trans_resv.h"
23 #include "xfs_inum.h"
24 #include "xfs_sb.h"
25 #include "xfs_mount.h"
26 #include "xfs_inode.h"
27 #include "xfs_error.h"
28 #include "xfs_trans.h"
29 #include "xfs_trans_priv.h"
30 #include "xfs_inode_item.h"
31 #include "xfs_quota.h"
32 #include "xfs_trace.h"
33 #include "xfs_icache.h"
34 #include "xfs_bmap_util.h"
35 #include "xfs_dquot_item.h"
36 #include "xfs_dquot.h"
37
38 #include <linux/kthread.h>
39 #include <linux/freezer.h>
40
41 STATIC void __xfs_inode_clear_reclaim_tag(struct xfs_mount *mp,
42                                 struct xfs_perag *pag, struct xfs_inode *ip);
43
44 /*
45  * Allocate and initialise an xfs_inode.
46  */
47 struct xfs_inode *
48 xfs_inode_alloc(
49         struct xfs_mount        *mp,
50         xfs_ino_t               ino)
51 {
52         struct xfs_inode        *ip;
53
54         /*
55          * if this didn't occur in transactions, we could use
56          * KM_MAYFAIL and return NULL here on ENOMEM. Set the
57          * code up to do this anyway.
58          */
59         ip = kmem_zone_alloc(xfs_inode_zone, KM_SLEEP);
60         if (!ip)
61                 return NULL;
62         if (inode_init_always(mp->m_super, VFS_I(ip))) {
63                 kmem_zone_free(xfs_inode_zone, ip);
64                 return NULL;
65         }
66
67         ASSERT(atomic_read(&ip->i_pincount) == 0);
68         ASSERT(!spin_is_locked(&ip->i_flags_lock));
69         ASSERT(!xfs_isiflocked(ip));
70         ASSERT(ip->i_ino == 0);
71
72         mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino);
73
74         /* initialise the xfs inode */
75         ip->i_ino = ino;
76         ip->i_mount = mp;
77         memset(&ip->i_imap, 0, sizeof(struct xfs_imap));
78         ip->i_afp = NULL;
79         memset(&ip->i_df, 0, sizeof(xfs_ifork_t));
80         ip->i_flags = 0;
81         ip->i_delayed_blks = 0;
82         memset(&ip->i_d, 0, sizeof(xfs_icdinode_t));
83
84         return ip;
85 }
86
87 STATIC void
88 xfs_inode_free_callback(
89         struct rcu_head         *head)
90 {
91         struct inode            *inode = container_of(head, struct inode, i_rcu);
92         struct xfs_inode        *ip = XFS_I(inode);
93
94         kmem_zone_free(xfs_inode_zone, ip);
95 }
96
97 void
98 xfs_inode_free(
99         struct xfs_inode        *ip)
100 {
101         switch (ip->i_d.di_mode & S_IFMT) {
102         case S_IFREG:
103         case S_IFDIR:
104         case S_IFLNK:
105                 xfs_idestroy_fork(ip, XFS_DATA_FORK);
106                 break;
107         }
108
109         if (ip->i_afp)
110                 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
111
112         if (ip->i_itemp) {
113                 ASSERT(!(ip->i_itemp->ili_item.li_flags & XFS_LI_IN_AIL));
114                 xfs_inode_item_destroy(ip);
115                 ip->i_itemp = NULL;
116         }
117
118         /*
119          * Because we use RCU freeing we need to ensure the inode always
120          * appears to be reclaimed with an invalid inode number when in the
121          * free state. The ip->i_flags_lock provides the barrier against lookup
122          * races.
123          */
124         spin_lock(&ip->i_flags_lock);
125         ip->i_flags = XFS_IRECLAIM;
126         ip->i_ino = 0;
127         spin_unlock(&ip->i_flags_lock);
128
129         /* asserts to verify all state is correct here */
130         ASSERT(atomic_read(&ip->i_pincount) == 0);
131         ASSERT(!xfs_isiflocked(ip));
132
133         call_rcu(&VFS_I(ip)->i_rcu, xfs_inode_free_callback);
134 }
135
136 /*
137  * Check the validity of the inode we just found it the cache
138  */
139 static int
140 xfs_iget_cache_hit(
141         struct xfs_perag        *pag,
142         struct xfs_inode        *ip,
143         xfs_ino_t               ino,
144         int                     flags,
145         int                     lock_flags) __releases(RCU)
146 {
147         struct inode            *inode = VFS_I(ip);
148         struct xfs_mount        *mp = ip->i_mount;
149         int                     error;
150
151         /*
152          * check for re-use of an inode within an RCU grace period due to the
153          * radix tree nodes not being updated yet. We monitor for this by
154          * setting the inode number to zero before freeing the inode structure.
155          * If the inode has been reallocated and set up, then the inode number
156          * will not match, so check for that, too.
157          */
158         spin_lock(&ip->i_flags_lock);
159         if (ip->i_ino != ino) {
160                 trace_xfs_iget_skip(ip);
161                 XFS_STATS_INC(xs_ig_frecycle);
162                 error = -EAGAIN;
163                 goto out_error;
164         }
165
166
167         /*
168          * If we are racing with another cache hit that is currently
169          * instantiating this inode or currently recycling it out of
170          * reclaimabe state, wait for the initialisation to complete
171          * before continuing.
172          *
173          * XXX(hch): eventually we should do something equivalent to
174          *           wait_on_inode to wait for these flags to be cleared
175          *           instead of polling for it.
176          */
177         if (ip->i_flags & (XFS_INEW|XFS_IRECLAIM)) {
178                 trace_xfs_iget_skip(ip);
179                 XFS_STATS_INC(xs_ig_frecycle);
180                 error = -EAGAIN;
181                 goto out_error;
182         }
183
184         /*
185          * If lookup is racing with unlink return an error immediately.
186          */
187         if (ip->i_d.di_mode == 0 && !(flags & XFS_IGET_CREATE)) {
188                 error = -ENOENT;
189                 goto out_error;
190         }
191
192         /*
193          * If IRECLAIMABLE is set, we've torn down the VFS inode already.
194          * Need to carefully get it back into useable state.
195          */
196         if (ip->i_flags & XFS_IRECLAIMABLE) {
197                 trace_xfs_iget_reclaim(ip);
198
199                 /*
200                  * We need to set XFS_IRECLAIM to prevent xfs_reclaim_inode
201                  * from stomping over us while we recycle the inode.  We can't
202                  * clear the radix tree reclaimable tag yet as it requires
203                  * pag_ici_lock to be held exclusive.
204                  */
205                 ip->i_flags |= XFS_IRECLAIM;
206
207                 spin_unlock(&ip->i_flags_lock);
208                 rcu_read_unlock();
209
210                 error = inode_init_always(mp->m_super, inode);
211                 if (error) {
212                         /*
213                          * Re-initializing the inode failed, and we are in deep
214                          * trouble.  Try to re-add it to the reclaim list.
215                          */
216                         rcu_read_lock();
217                         spin_lock(&ip->i_flags_lock);
218
219                         ip->i_flags &= ~(XFS_INEW | XFS_IRECLAIM);
220                         ASSERT(ip->i_flags & XFS_IRECLAIMABLE);
221                         trace_xfs_iget_reclaim_fail(ip);
222                         goto out_error;
223                 }
224
225                 spin_lock(&pag->pag_ici_lock);
226                 spin_lock(&ip->i_flags_lock);
227
228                 /*
229                  * Clear the per-lifetime state in the inode as we are now
230                  * effectively a new inode and need to return to the initial
231                  * state before reuse occurs.
232                  */
233                 ip->i_flags &= ~XFS_IRECLAIM_RESET_FLAGS;
234                 ip->i_flags |= XFS_INEW;
235                 __xfs_inode_clear_reclaim_tag(mp, pag, ip);
236                 inode->i_state = I_NEW;
237
238                 ASSERT(!rwsem_is_locked(&ip->i_iolock.mr_lock));
239                 mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino);
240
241                 spin_unlock(&ip->i_flags_lock);
242                 spin_unlock(&pag->pag_ici_lock);
243         } else {
244                 /* If the VFS inode is being torn down, pause and try again. */
245                 if (!igrab(inode)) {
246                         trace_xfs_iget_skip(ip);
247                         error = -EAGAIN;
248                         goto out_error;
249                 }
250
251                 /* We've got a live one. */
252                 spin_unlock(&ip->i_flags_lock);
253                 rcu_read_unlock();
254                 trace_xfs_iget_hit(ip);
255         }
256
257         if (lock_flags != 0)
258                 xfs_ilock(ip, lock_flags);
259
260         xfs_iflags_clear(ip, XFS_ISTALE | XFS_IDONTCACHE);
261         XFS_STATS_INC(xs_ig_found);
262
263         return 0;
264
265 out_error:
266         spin_unlock(&ip->i_flags_lock);
267         rcu_read_unlock();
268         return error;
269 }
270
271
272 static int
273 xfs_iget_cache_miss(
274         struct xfs_mount        *mp,
275         struct xfs_perag        *pag,
276         xfs_trans_t             *tp,
277         xfs_ino_t               ino,
278         struct xfs_inode        **ipp,
279         int                     flags,
280         int                     lock_flags)
281 {
282         struct xfs_inode        *ip;
283         int                     error;
284         xfs_agino_t             agino = XFS_INO_TO_AGINO(mp, ino);
285         int                     iflags;
286
287         ip = xfs_inode_alloc(mp, ino);
288         if (!ip)
289                 return -ENOMEM;
290
291         error = xfs_iread(mp, tp, ip, flags);
292         if (error)
293                 goto out_destroy;
294
295         trace_xfs_iget_miss(ip);
296
297         if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
298                 error = -ENOENT;
299                 goto out_destroy;
300         }
301
302         /*
303          * Preload the radix tree so we can insert safely under the
304          * write spinlock. Note that we cannot sleep inside the preload
305          * region. Since we can be called from transaction context, don't
306          * recurse into the file system.
307          */
308         if (radix_tree_preload(GFP_NOFS)) {
309                 error = -EAGAIN;
310                 goto out_destroy;
311         }
312
313         /*
314          * Because the inode hasn't been added to the radix-tree yet it can't
315          * be found by another thread, so we can do the non-sleeping lock here.
316          */
317         if (lock_flags) {
318                 if (!xfs_ilock_nowait(ip, lock_flags))
319                         BUG();
320         }
321
322         /*
323          * These values must be set before inserting the inode into the radix
324          * tree as the moment it is inserted a concurrent lookup (allowed by the
325          * RCU locking mechanism) can find it and that lookup must see that this
326          * is an inode currently under construction (i.e. that XFS_INEW is set).
327          * The ip->i_flags_lock that protects the XFS_INEW flag forms the
328          * memory barrier that ensures this detection works correctly at lookup
329          * time.
330          */
331         iflags = XFS_INEW;
332         if (flags & XFS_IGET_DONTCACHE)
333                 iflags |= XFS_IDONTCACHE;
334         ip->i_udquot = NULL;
335         ip->i_gdquot = NULL;
336         ip->i_pdquot = NULL;
337         xfs_iflags_set(ip, iflags);
338
339         /* insert the new inode */
340         spin_lock(&pag->pag_ici_lock);
341         error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
342         if (unlikely(error)) {
343                 WARN_ON(error != -EEXIST);
344                 XFS_STATS_INC(xs_ig_dup);
345                 error = -EAGAIN;
346                 goto out_preload_end;
347         }
348         spin_unlock(&pag->pag_ici_lock);
349         radix_tree_preload_end();
350
351         *ipp = ip;
352         return 0;
353
354 out_preload_end:
355         spin_unlock(&pag->pag_ici_lock);
356         radix_tree_preload_end();
357         if (lock_flags)
358                 xfs_iunlock(ip, lock_flags);
359 out_destroy:
360         __destroy_inode(VFS_I(ip));
361         xfs_inode_free(ip);
362         return error;
363 }
364
365 /*
366  * Look up an inode by number in the given file system.
367  * The inode is looked up in the cache held in each AG.
368  * If the inode is found in the cache, initialise the vfs inode
369  * if necessary.
370  *
371  * If it is not in core, read it in from the file system's device,
372  * add it to the cache and initialise the vfs inode.
373  *
374  * The inode is locked according to the value of the lock_flags parameter.
375  * This flag parameter indicates how and if the inode's IO lock and inode lock
376  * should be taken.
377  *
378  * mp -- the mount point structure for the current file system.  It points
379  *       to the inode hash table.
380  * tp -- a pointer to the current transaction if there is one.  This is
381  *       simply passed through to the xfs_iread() call.
382  * ino -- the number of the inode desired.  This is the unique identifier
383  *        within the file system for the inode being requested.
384  * lock_flags -- flags indicating how to lock the inode.  See the comment
385  *               for xfs_ilock() for a list of valid values.
386  */
387 int
388 xfs_iget(
389         xfs_mount_t     *mp,
390         xfs_trans_t     *tp,
391         xfs_ino_t       ino,
392         uint            flags,
393         uint            lock_flags,
394         xfs_inode_t     **ipp)
395 {
396         xfs_inode_t     *ip;
397         int             error;
398         xfs_perag_t     *pag;
399         xfs_agino_t     agino;
400
401         /*
402          * xfs_reclaim_inode() uses the ILOCK to ensure an inode
403          * doesn't get freed while it's being referenced during a
404          * radix tree traversal here.  It assumes this function
405          * aqcuires only the ILOCK (and therefore it has no need to
406          * involve the IOLOCK in this synchronization).
407          */
408         ASSERT((lock_flags & (XFS_IOLOCK_EXCL | XFS_IOLOCK_SHARED)) == 0);
409
410         /* reject inode numbers outside existing AGs */
411         if (!ino || XFS_INO_TO_AGNO(mp, ino) >= mp->m_sb.sb_agcount)
412                 return -EINVAL;
413
414         /* get the perag structure and ensure that it's inode capable */
415         pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ino));
416         agino = XFS_INO_TO_AGINO(mp, ino);
417
418 again:
419         error = 0;
420         rcu_read_lock();
421         ip = radix_tree_lookup(&pag->pag_ici_root, agino);
422
423         if (ip) {
424                 error = xfs_iget_cache_hit(pag, ip, ino, flags, lock_flags);
425                 if (error)
426                         goto out_error_or_again;
427         } else {
428                 rcu_read_unlock();
429                 XFS_STATS_INC(xs_ig_missed);
430
431                 error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip,
432                                                         flags, lock_flags);
433                 if (error)
434                         goto out_error_or_again;
435         }
436         xfs_perag_put(pag);
437
438         *ipp = ip;
439
440         /*
441          * If we have a real type for an on-disk inode, we can set ops(&unlock)
442          * now.  If it's a new inode being created, xfs_ialloc will handle it.
443          */
444         if (xfs_iflags_test(ip, XFS_INEW) && ip->i_d.di_mode != 0)
445                 xfs_setup_inode(ip);
446         return 0;
447
448 out_error_or_again:
449         if (error == -EAGAIN) {
450                 delay(1);
451                 goto again;
452         }
453         xfs_perag_put(pag);
454         return error;
455 }
456
457 /*
458  * The inode lookup is done in batches to keep the amount of lock traffic and
459  * radix tree lookups to a minimum. The batch size is a trade off between
460  * lookup reduction and stack usage. This is in the reclaim path, so we can't
461  * be too greedy.
462  */
463 #define XFS_LOOKUP_BATCH        32
464
465 STATIC int
466 xfs_inode_ag_walk_grab(
467         struct xfs_inode        *ip)
468 {
469         struct inode            *inode = VFS_I(ip);
470
471         ASSERT(rcu_read_lock_held());
472
473         /*
474          * check for stale RCU freed inode
475          *
476          * If the inode has been reallocated, it doesn't matter if it's not in
477          * the AG we are walking - we are walking for writeback, so if it
478          * passes all the "valid inode" checks and is dirty, then we'll write
479          * it back anyway.  If it has been reallocated and still being
480          * initialised, the XFS_INEW check below will catch it.
481          */
482         spin_lock(&ip->i_flags_lock);
483         if (!ip->i_ino)
484                 goto out_unlock_noent;
485
486         /* avoid new or reclaimable inodes. Leave for reclaim code to flush */
487         if (__xfs_iflags_test(ip, XFS_INEW | XFS_IRECLAIMABLE | XFS_IRECLAIM))
488                 goto out_unlock_noent;
489         spin_unlock(&ip->i_flags_lock);
490
491         /* nothing to sync during shutdown */
492         if (XFS_FORCED_SHUTDOWN(ip->i_mount))
493                 return -EFSCORRUPTED;
494
495         /* If we can't grab the inode, it must on it's way to reclaim. */
496         if (!igrab(inode))
497                 return -ENOENT;
498
499         /* inode is valid */
500         return 0;
501
502 out_unlock_noent:
503         spin_unlock(&ip->i_flags_lock);
504         return -ENOENT;
505 }
506
507 STATIC int
508 xfs_inode_ag_walk(
509         struct xfs_mount        *mp,
510         struct xfs_perag        *pag,
511         int                     (*execute)(struct xfs_inode *ip, int flags,
512                                            void *args),
513         int                     flags,
514         void                    *args,
515         int                     tag)
516 {
517         uint32_t                first_index;
518         int                     last_error = 0;
519         int                     skipped;
520         int                     done;
521         int                     nr_found;
522
523 restart:
524         done = 0;
525         skipped = 0;
526         first_index = 0;
527         nr_found = 0;
528         do {
529                 struct xfs_inode *batch[XFS_LOOKUP_BATCH];
530                 int             error = 0;
531                 int             i;
532
533                 rcu_read_lock();
534
535                 if (tag == -1)
536                         nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
537                                         (void **)batch, first_index,
538                                         XFS_LOOKUP_BATCH);
539                 else
540                         nr_found = radix_tree_gang_lookup_tag(
541                                         &pag->pag_ici_root,
542                                         (void **) batch, first_index,
543                                         XFS_LOOKUP_BATCH, tag);
544
545                 if (!nr_found) {
546                         rcu_read_unlock();
547                         break;
548                 }
549
550                 /*
551                  * Grab the inodes before we drop the lock. if we found
552                  * nothing, nr == 0 and the loop will be skipped.
553                  */
554                 for (i = 0; i < nr_found; i++) {
555                         struct xfs_inode *ip = batch[i];
556
557                         if (done || xfs_inode_ag_walk_grab(ip))
558                                 batch[i] = NULL;
559
560                         /*
561                          * Update the index for the next lookup. Catch
562                          * overflows into the next AG range which can occur if
563                          * we have inodes in the last block of the AG and we
564                          * are currently pointing to the last inode.
565                          *
566                          * Because we may see inodes that are from the wrong AG
567                          * due to RCU freeing and reallocation, only update the
568                          * index if it lies in this AG. It was a race that lead
569                          * us to see this inode, so another lookup from the
570                          * same index will not find it again.
571                          */
572                         if (XFS_INO_TO_AGNO(mp, ip->i_ino) != pag->pag_agno)
573                                 continue;
574                         first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
575                         if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
576                                 done = 1;
577                 }
578
579                 /* unlock now we've grabbed the inodes. */
580                 rcu_read_unlock();
581
582                 for (i = 0; i < nr_found; i++) {
583                         if (!batch[i])
584                                 continue;
585                         error = execute(batch[i], flags, args);
586                         IRELE(batch[i]);
587                         if (error == -EAGAIN) {
588                                 skipped++;
589                                 continue;
590                         }
591                         if (error && last_error != -EFSCORRUPTED)
592                                 last_error = error;
593                 }
594
595                 /* bail out if the filesystem is corrupted.  */
596                 if (error == -EFSCORRUPTED)
597                         break;
598
599                 cond_resched();
600
601         } while (nr_found && !done);
602
603         if (skipped) {
604                 delay(1);
605                 goto restart;
606         }
607         return last_error;
608 }
609
610 /*
611  * Background scanning to trim post-EOF preallocated space. This is queued
612  * based on the 'speculative_prealloc_lifetime' tunable (5m by default).
613  */
614 STATIC void
615 xfs_queue_eofblocks(
616         struct xfs_mount *mp)
617 {
618         rcu_read_lock();
619         if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_EOFBLOCKS_TAG))
620                 queue_delayed_work(mp->m_eofblocks_workqueue,
621                                    &mp->m_eofblocks_work,
622                                    msecs_to_jiffies(xfs_eofb_secs * 1000));
623         rcu_read_unlock();
624 }
625
626 void
627 xfs_eofblocks_worker(
628         struct work_struct *work)
629 {
630         struct xfs_mount *mp = container_of(to_delayed_work(work),
631                                 struct xfs_mount, m_eofblocks_work);
632         xfs_icache_free_eofblocks(mp, NULL);
633         xfs_queue_eofblocks(mp);
634 }
635
636 int
637 xfs_inode_ag_iterator(
638         struct xfs_mount        *mp,
639         int                     (*execute)(struct xfs_inode *ip, int flags,
640                                            void *args),
641         int                     flags,
642         void                    *args)
643 {
644         struct xfs_perag        *pag;
645         int                     error = 0;
646         int                     last_error = 0;
647         xfs_agnumber_t          ag;
648
649         ag = 0;
650         while ((pag = xfs_perag_get(mp, ag))) {
651                 ag = pag->pag_agno + 1;
652                 error = xfs_inode_ag_walk(mp, pag, execute, flags, args, -1);
653                 xfs_perag_put(pag);
654                 if (error) {
655                         last_error = error;
656                         if (error == -EFSCORRUPTED)
657                                 break;
658                 }
659         }
660         return last_error;
661 }
662
663 int
664 xfs_inode_ag_iterator_tag(
665         struct xfs_mount        *mp,
666         int                     (*execute)(struct xfs_inode *ip, int flags,
667                                            void *args),
668         int                     flags,
669         void                    *args,
670         int                     tag)
671 {
672         struct xfs_perag        *pag;
673         int                     error = 0;
674         int                     last_error = 0;
675         xfs_agnumber_t          ag;
676
677         ag = 0;
678         while ((pag = xfs_perag_get_tag(mp, ag, tag))) {
679                 ag = pag->pag_agno + 1;
680                 error = xfs_inode_ag_walk(mp, pag, execute, flags, args, tag);
681                 xfs_perag_put(pag);
682                 if (error) {
683                         last_error = error;
684                         if (error == -EFSCORRUPTED)
685                                 break;
686                 }
687         }
688         return last_error;
689 }
690
691 /*
692  * Queue a new inode reclaim pass if there are reclaimable inodes and there
693  * isn't a reclaim pass already in progress. By default it runs every 5s based
694  * on the xfs periodic sync default of 30s. Perhaps this should have it's own
695  * tunable, but that can be done if this method proves to be ineffective or too
696  * aggressive.
697  */
698 static void
699 xfs_reclaim_work_queue(
700         struct xfs_mount        *mp)
701 {
702
703         rcu_read_lock();
704         if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_RECLAIM_TAG)) {
705                 queue_delayed_work(mp->m_reclaim_workqueue, &mp->m_reclaim_work,
706                         msecs_to_jiffies(xfs_syncd_centisecs / 6 * 10));
707         }
708         rcu_read_unlock();
709 }
710
711 /*
712  * This is a fast pass over the inode cache to try to get reclaim moving on as
713  * many inodes as possible in a short period of time. It kicks itself every few
714  * seconds, as well as being kicked by the inode cache shrinker when memory
715  * goes low. It scans as quickly as possible avoiding locked inodes or those
716  * already being flushed, and once done schedules a future pass.
717  */
718 void
719 xfs_reclaim_worker(
720         struct work_struct *work)
721 {
722         struct xfs_mount *mp = container_of(to_delayed_work(work),
723                                         struct xfs_mount, m_reclaim_work);
724
725         xfs_reclaim_inodes(mp, SYNC_TRYLOCK);
726         xfs_reclaim_work_queue(mp);
727 }
728
729 static void
730 __xfs_inode_set_reclaim_tag(
731         struct xfs_perag        *pag,
732         struct xfs_inode        *ip)
733 {
734         radix_tree_tag_set(&pag->pag_ici_root,
735                            XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
736                            XFS_ICI_RECLAIM_TAG);
737
738         if (!pag->pag_ici_reclaimable) {
739                 /* propagate the reclaim tag up into the perag radix tree */
740                 spin_lock(&ip->i_mount->m_perag_lock);
741                 radix_tree_tag_set(&ip->i_mount->m_perag_tree,
742                                 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
743                                 XFS_ICI_RECLAIM_TAG);
744                 spin_unlock(&ip->i_mount->m_perag_lock);
745
746                 /* schedule periodic background inode reclaim */
747                 xfs_reclaim_work_queue(ip->i_mount);
748
749                 trace_xfs_perag_set_reclaim(ip->i_mount, pag->pag_agno,
750                                                         -1, _RET_IP_);
751         }
752         pag->pag_ici_reclaimable++;
753 }
754
755 /*
756  * We set the inode flag atomically with the radix tree tag.
757  * Once we get tag lookups on the radix tree, this inode flag
758  * can go away.
759  */
760 void
761 xfs_inode_set_reclaim_tag(
762         xfs_inode_t     *ip)
763 {
764         struct xfs_mount *mp = ip->i_mount;
765         struct xfs_perag *pag;
766
767         pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
768         spin_lock(&pag->pag_ici_lock);
769         spin_lock(&ip->i_flags_lock);
770         __xfs_inode_set_reclaim_tag(pag, ip);
771         __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
772         spin_unlock(&ip->i_flags_lock);
773         spin_unlock(&pag->pag_ici_lock);
774         xfs_perag_put(pag);
775 }
776
777 STATIC void
778 __xfs_inode_clear_reclaim(
779         xfs_perag_t     *pag,
780         xfs_inode_t     *ip)
781 {
782         pag->pag_ici_reclaimable--;
783         if (!pag->pag_ici_reclaimable) {
784                 /* clear the reclaim tag from the perag radix tree */
785                 spin_lock(&ip->i_mount->m_perag_lock);
786                 radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
787                                 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
788                                 XFS_ICI_RECLAIM_TAG);
789                 spin_unlock(&ip->i_mount->m_perag_lock);
790                 trace_xfs_perag_clear_reclaim(ip->i_mount, pag->pag_agno,
791                                                         -1, _RET_IP_);
792         }
793 }
794
795 STATIC void
796 __xfs_inode_clear_reclaim_tag(
797         xfs_mount_t     *mp,
798         xfs_perag_t     *pag,
799         xfs_inode_t     *ip)
800 {
801         radix_tree_tag_clear(&pag->pag_ici_root,
802                         XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
803         __xfs_inode_clear_reclaim(pag, ip);
804 }
805
806 /*
807  * Grab the inode for reclaim exclusively.
808  * Return 0 if we grabbed it, non-zero otherwise.
809  */
810 STATIC int
811 xfs_reclaim_inode_grab(
812         struct xfs_inode        *ip,
813         int                     flags)
814 {
815         ASSERT(rcu_read_lock_held());
816
817         /* quick check for stale RCU freed inode */
818         if (!ip->i_ino)
819                 return 1;
820
821         /*
822          * If we are asked for non-blocking operation, do unlocked checks to
823          * see if the inode already is being flushed or in reclaim to avoid
824          * lock traffic.
825          */
826         if ((flags & SYNC_TRYLOCK) &&
827             __xfs_iflags_test(ip, XFS_IFLOCK | XFS_IRECLAIM))
828                 return 1;
829
830         /*
831          * The radix tree lock here protects a thread in xfs_iget from racing
832          * with us starting reclaim on the inode.  Once we have the
833          * XFS_IRECLAIM flag set it will not touch us.
834          *
835          * Due to RCU lookup, we may find inodes that have been freed and only
836          * have XFS_IRECLAIM set.  Indeed, we may see reallocated inodes that
837          * aren't candidates for reclaim at all, so we must check the
838          * XFS_IRECLAIMABLE is set first before proceeding to reclaim.
839          */
840         spin_lock(&ip->i_flags_lock);
841         if (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) ||
842             __xfs_iflags_test(ip, XFS_IRECLAIM)) {
843                 /* not a reclaim candidate. */
844                 spin_unlock(&ip->i_flags_lock);
845                 return 1;
846         }
847         __xfs_iflags_set(ip, XFS_IRECLAIM);
848         spin_unlock(&ip->i_flags_lock);
849         return 0;
850 }
851
852 /*
853  * Inodes in different states need to be treated differently. The following
854  * table lists the inode states and the reclaim actions necessary:
855  *
856  *      inode state          iflush ret         required action
857  *      ---------------      ----------         ---------------
858  *      bad                     -               reclaim
859  *      shutdown                EIO             unpin and reclaim
860  *      clean, unpinned         0               reclaim
861  *      stale, unpinned         0               reclaim
862  *      clean, pinned(*)        0               requeue
863  *      stale, pinned           EAGAIN          requeue
864  *      dirty, async            -               requeue
865  *      dirty, sync             0               reclaim
866  *
867  * (*) dgc: I don't think the clean, pinned state is possible but it gets
868  * handled anyway given the order of checks implemented.
869  *
870  * Also, because we get the flush lock first, we know that any inode that has
871  * been flushed delwri has had the flush completed by the time we check that
872  * the inode is clean.
873  *
874  * Note that because the inode is flushed delayed write by AIL pushing, the
875  * flush lock may already be held here and waiting on it can result in very
876  * long latencies.  Hence for sync reclaims, where we wait on the flush lock,
877  * the caller should push the AIL first before trying to reclaim inodes to
878  * minimise the amount of time spent waiting.  For background relaim, we only
879  * bother to reclaim clean inodes anyway.
880  *
881  * Hence the order of actions after gaining the locks should be:
882  *      bad             => reclaim
883  *      shutdown        => unpin and reclaim
884  *      pinned, async   => requeue
885  *      pinned, sync    => unpin
886  *      stale           => reclaim
887  *      clean           => reclaim
888  *      dirty, async    => requeue
889  *      dirty, sync     => flush, wait and reclaim
890  */
891 STATIC int
892 xfs_reclaim_inode(
893         struct xfs_inode        *ip,
894         struct xfs_perag        *pag,
895         int                     sync_mode)
896 {
897         struct xfs_buf          *bp = NULL;
898         int                     error;
899
900 restart:
901         error = 0;
902         xfs_ilock(ip, XFS_ILOCK_EXCL);
903         if (!xfs_iflock_nowait(ip)) {
904                 if (!(sync_mode & SYNC_WAIT))
905                         goto out;
906                 xfs_iflock(ip);
907         }
908
909         if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
910                 xfs_iunpin_wait(ip);
911                 xfs_iflush_abort(ip, false);
912                 goto reclaim;
913         }
914         if (xfs_ipincount(ip)) {
915                 if (!(sync_mode & SYNC_WAIT))
916                         goto out_ifunlock;
917                 xfs_iunpin_wait(ip);
918         }
919         if (xfs_iflags_test(ip, XFS_ISTALE))
920                 goto reclaim;
921         if (xfs_inode_clean(ip))
922                 goto reclaim;
923
924         /*
925          * Never flush out dirty data during non-blocking reclaim, as it would
926          * just contend with AIL pushing trying to do the same job.
927          */
928         if (!(sync_mode & SYNC_WAIT))
929                 goto out_ifunlock;
930
931         /*
932          * Now we have an inode that needs flushing.
933          *
934          * Note that xfs_iflush will never block on the inode buffer lock, as
935          * xfs_ifree_cluster() can lock the inode buffer before it locks the
936          * ip->i_lock, and we are doing the exact opposite here.  As a result,
937          * doing a blocking xfs_imap_to_bp() to get the cluster buffer would
938          * result in an ABBA deadlock with xfs_ifree_cluster().
939          *
940          * As xfs_ifree_cluser() must gather all inodes that are active in the
941          * cache to mark them stale, if we hit this case we don't actually want
942          * to do IO here - we want the inode marked stale so we can simply
943          * reclaim it.  Hence if we get an EAGAIN error here,  just unlock the
944          * inode, back off and try again.  Hopefully the next pass through will
945          * see the stale flag set on the inode.
946          */
947         error = xfs_iflush(ip, &bp);
948         if (error == -EAGAIN) {
949                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
950                 /* backoff longer than in xfs_ifree_cluster */
951                 delay(2);
952                 goto restart;
953         }
954
955         if (!error) {
956                 error = xfs_bwrite(bp);
957                 xfs_buf_relse(bp);
958         }
959
960         xfs_iflock(ip);
961 reclaim:
962         xfs_ifunlock(ip);
963         xfs_iunlock(ip, XFS_ILOCK_EXCL);
964
965         XFS_STATS_INC(xs_ig_reclaims);
966         /*
967          * Remove the inode from the per-AG radix tree.
968          *
969          * Because radix_tree_delete won't complain even if the item was never
970          * added to the tree assert that it's been there before to catch
971          * problems with the inode life time early on.
972          */
973         spin_lock(&pag->pag_ici_lock);
974         if (!radix_tree_delete(&pag->pag_ici_root,
975                                 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino)))
976                 ASSERT(0);
977         __xfs_inode_clear_reclaim(pag, ip);
978         spin_unlock(&pag->pag_ici_lock);
979
980         /*
981          * Here we do an (almost) spurious inode lock in order to coordinate
982          * with inode cache radix tree lookups.  This is because the lookup
983          * can reference the inodes in the cache without taking references.
984          *
985          * We make that OK here by ensuring that we wait until the inode is
986          * unlocked after the lookup before we go ahead and free it.
987          */
988         xfs_ilock(ip, XFS_ILOCK_EXCL);
989         xfs_qm_dqdetach(ip);
990         xfs_iunlock(ip, XFS_ILOCK_EXCL);
991
992         xfs_inode_free(ip);
993         return error;
994
995 out_ifunlock:
996         xfs_ifunlock(ip);
997 out:
998         xfs_iflags_clear(ip, XFS_IRECLAIM);
999         xfs_iunlock(ip, XFS_ILOCK_EXCL);
1000         /*
1001          * We could return -EAGAIN here to make reclaim rescan the inode tree in
1002          * a short while. However, this just burns CPU time scanning the tree
1003          * waiting for IO to complete and the reclaim work never goes back to
1004          * the idle state. Instead, return 0 to let the next scheduled
1005          * background reclaim attempt to reclaim the inode again.
1006          */
1007         return 0;
1008 }
1009
1010 /*
1011  * Walk the AGs and reclaim the inodes in them. Even if the filesystem is
1012  * corrupted, we still want to try to reclaim all the inodes. If we don't,
1013  * then a shut down during filesystem unmount reclaim walk leak all the
1014  * unreclaimed inodes.
1015  */
1016 STATIC int
1017 xfs_reclaim_inodes_ag(
1018         struct xfs_mount        *mp,
1019         int                     flags,
1020         int                     *nr_to_scan)
1021 {
1022         struct xfs_perag        *pag;
1023         int                     error = 0;
1024         int                     last_error = 0;
1025         xfs_agnumber_t          ag;
1026         int                     trylock = flags & SYNC_TRYLOCK;
1027         int                     skipped;
1028
1029 restart:
1030         ag = 0;
1031         skipped = 0;
1032         while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
1033                 unsigned long   first_index = 0;
1034                 int             done = 0;
1035                 int             nr_found = 0;
1036
1037                 ag = pag->pag_agno + 1;
1038
1039                 if (trylock) {
1040                         if (!mutex_trylock(&pag->pag_ici_reclaim_lock)) {
1041                                 skipped++;
1042                                 xfs_perag_put(pag);
1043                                 continue;
1044                         }
1045                         first_index = pag->pag_ici_reclaim_cursor;
1046                 } else
1047                         mutex_lock(&pag->pag_ici_reclaim_lock);
1048
1049                 do {
1050                         struct xfs_inode *batch[XFS_LOOKUP_BATCH];
1051                         int     i;
1052
1053                         rcu_read_lock();
1054                         nr_found = radix_tree_gang_lookup_tag(
1055                                         &pag->pag_ici_root,
1056                                         (void **)batch, first_index,
1057                                         XFS_LOOKUP_BATCH,
1058                                         XFS_ICI_RECLAIM_TAG);
1059                         if (!nr_found) {
1060                                 done = 1;
1061                                 rcu_read_unlock();
1062                                 break;
1063                         }
1064
1065                         /*
1066                          * Grab the inodes before we drop the lock. if we found
1067                          * nothing, nr == 0 and the loop will be skipped.
1068                          */
1069                         for (i = 0; i < nr_found; i++) {
1070                                 struct xfs_inode *ip = batch[i];
1071
1072                                 if (done || xfs_reclaim_inode_grab(ip, flags))
1073                                         batch[i] = NULL;
1074
1075                                 /*
1076                                  * Update the index for the next lookup. Catch
1077                                  * overflows into the next AG range which can
1078                                  * occur if we have inodes in the last block of
1079                                  * the AG and we are currently pointing to the
1080                                  * last inode.
1081                                  *
1082                                  * Because we may see inodes that are from the
1083                                  * wrong AG due to RCU freeing and
1084                                  * reallocation, only update the index if it
1085                                  * lies in this AG. It was a race that lead us
1086                                  * to see this inode, so another lookup from
1087                                  * the same index will not find it again.
1088                                  */
1089                                 if (XFS_INO_TO_AGNO(mp, ip->i_ino) !=
1090                                                                 pag->pag_agno)
1091                                         continue;
1092                                 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
1093                                 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
1094                                         done = 1;
1095                         }
1096
1097                         /* unlock now we've grabbed the inodes. */
1098                         rcu_read_unlock();
1099
1100                         for (i = 0; i < nr_found; i++) {
1101                                 if (!batch[i])
1102                                         continue;
1103                                 error = xfs_reclaim_inode(batch[i], pag, flags);
1104                                 if (error && last_error != -EFSCORRUPTED)
1105                                         last_error = error;
1106                         }
1107
1108                         *nr_to_scan -= XFS_LOOKUP_BATCH;
1109
1110                         cond_resched();
1111
1112                 } while (nr_found && !done && *nr_to_scan > 0);
1113
1114                 if (trylock && !done)
1115                         pag->pag_ici_reclaim_cursor = first_index;
1116                 else
1117                         pag->pag_ici_reclaim_cursor = 0;
1118                 mutex_unlock(&pag->pag_ici_reclaim_lock);
1119                 xfs_perag_put(pag);
1120         }
1121
1122         /*
1123          * if we skipped any AG, and we still have scan count remaining, do
1124          * another pass this time using blocking reclaim semantics (i.e
1125          * waiting on the reclaim locks and ignoring the reclaim cursors). This
1126          * ensure that when we get more reclaimers than AGs we block rather
1127          * than spin trying to execute reclaim.
1128          */
1129         if (skipped && (flags & SYNC_WAIT) && *nr_to_scan > 0) {
1130                 trylock = 0;
1131                 goto restart;
1132         }
1133         return last_error;
1134 }
1135
1136 int
1137 xfs_reclaim_inodes(
1138         xfs_mount_t     *mp,
1139         int             mode)
1140 {
1141         int             nr_to_scan = INT_MAX;
1142
1143         return xfs_reclaim_inodes_ag(mp, mode, &nr_to_scan);
1144 }
1145
1146 /*
1147  * Scan a certain number of inodes for reclaim.
1148  *
1149  * When called we make sure that there is a background (fast) inode reclaim in
1150  * progress, while we will throttle the speed of reclaim via doing synchronous
1151  * reclaim of inodes. That means if we come across dirty inodes, we wait for
1152  * them to be cleaned, which we hope will not be very long due to the
1153  * background walker having already kicked the IO off on those dirty inodes.
1154  */
1155 long
1156 xfs_reclaim_inodes_nr(
1157         struct xfs_mount        *mp,
1158         int                     nr_to_scan)
1159 {
1160         /* kick background reclaimer and push the AIL */
1161         xfs_reclaim_work_queue(mp);
1162         xfs_ail_push_all(mp->m_ail);
1163
1164         return xfs_reclaim_inodes_ag(mp, SYNC_TRYLOCK | SYNC_WAIT, &nr_to_scan);
1165 }
1166
1167 /*
1168  * Return the number of reclaimable inodes in the filesystem for
1169  * the shrinker to determine how much to reclaim.
1170  */
1171 int
1172 xfs_reclaim_inodes_count(
1173         struct xfs_mount        *mp)
1174 {
1175         struct xfs_perag        *pag;
1176         xfs_agnumber_t          ag = 0;
1177         int                     reclaimable = 0;
1178
1179         while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
1180                 ag = pag->pag_agno + 1;
1181                 reclaimable += pag->pag_ici_reclaimable;
1182                 xfs_perag_put(pag);
1183         }
1184         return reclaimable;
1185 }
1186
1187 STATIC int
1188 xfs_inode_match_id(
1189         struct xfs_inode        *ip,
1190         struct xfs_eofblocks    *eofb)
1191 {
1192         if ((eofb->eof_flags & XFS_EOF_FLAGS_UID) &&
1193             !uid_eq(VFS_I(ip)->i_uid, eofb->eof_uid))
1194                 return 0;
1195
1196         if ((eofb->eof_flags & XFS_EOF_FLAGS_GID) &&
1197             !gid_eq(VFS_I(ip)->i_gid, eofb->eof_gid))
1198                 return 0;
1199
1200         if ((eofb->eof_flags & XFS_EOF_FLAGS_PRID) &&
1201             xfs_get_projid(ip) != eofb->eof_prid)
1202                 return 0;
1203
1204         return 1;
1205 }
1206
1207 /*
1208  * A union-based inode filtering algorithm. Process the inode if any of the
1209  * criteria match. This is for global/internal scans only.
1210  */
1211 STATIC int
1212 xfs_inode_match_id_union(
1213         struct xfs_inode        *ip,
1214         struct xfs_eofblocks    *eofb)
1215 {
1216         if ((eofb->eof_flags & XFS_EOF_FLAGS_UID) &&
1217             uid_eq(VFS_I(ip)->i_uid, eofb->eof_uid))
1218                 return 1;
1219
1220         if ((eofb->eof_flags & XFS_EOF_FLAGS_GID) &&
1221             gid_eq(VFS_I(ip)->i_gid, eofb->eof_gid))
1222                 return 1;
1223
1224         if ((eofb->eof_flags & XFS_EOF_FLAGS_PRID) &&
1225             xfs_get_projid(ip) == eofb->eof_prid)
1226                 return 1;
1227
1228         return 0;
1229 }
1230
1231 STATIC int
1232 xfs_inode_free_eofblocks(
1233         struct xfs_inode        *ip,
1234         int                     flags,
1235         void                    *args)
1236 {
1237         int ret;
1238         struct xfs_eofblocks *eofb = args;
1239         bool need_iolock = true;
1240         int match;
1241
1242         ASSERT(!eofb || (eofb && eofb->eof_scan_owner != 0));
1243
1244         if (!xfs_can_free_eofblocks(ip, false)) {
1245                 /* inode could be preallocated or append-only */
1246                 trace_xfs_inode_free_eofblocks_invalid(ip);
1247                 xfs_inode_clear_eofblocks_tag(ip);
1248                 return 0;
1249         }
1250
1251         /*
1252          * If the mapping is dirty the operation can block and wait for some
1253          * time. Unless we are waiting, skip it.
1254          */
1255         if (!(flags & SYNC_WAIT) &&
1256             mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_DIRTY))
1257                 return 0;
1258
1259         if (eofb) {
1260                 if (eofb->eof_flags & XFS_EOF_FLAGS_UNION)
1261                         match = xfs_inode_match_id_union(ip, eofb);
1262                 else
1263                         match = xfs_inode_match_id(ip, eofb);
1264                 if (!match)
1265                         return 0;
1266
1267                 /* skip the inode if the file size is too small */
1268                 if (eofb->eof_flags & XFS_EOF_FLAGS_MINFILESIZE &&
1269                     XFS_ISIZE(ip) < eofb->eof_min_file_size)
1270                         return 0;
1271
1272                 /*
1273                  * A scan owner implies we already hold the iolock. Skip it in
1274                  * xfs_free_eofblocks() to avoid deadlock. This also eliminates
1275                  * the possibility of EAGAIN being returned.
1276                  */
1277                 if (eofb->eof_scan_owner == ip->i_ino)
1278                         need_iolock = false;
1279         }
1280
1281         ret = xfs_free_eofblocks(ip->i_mount, ip, need_iolock);
1282
1283         /* don't revisit the inode if we're not waiting */
1284         if (ret == -EAGAIN && !(flags & SYNC_WAIT))
1285                 ret = 0;
1286
1287         return ret;
1288 }
1289
1290 int
1291 xfs_icache_free_eofblocks(
1292         struct xfs_mount        *mp,
1293         struct xfs_eofblocks    *eofb)
1294 {
1295         int flags = SYNC_TRYLOCK;
1296
1297         if (eofb && (eofb->eof_flags & XFS_EOF_FLAGS_SYNC))
1298                 flags = SYNC_WAIT;
1299
1300         return xfs_inode_ag_iterator_tag(mp, xfs_inode_free_eofblocks, flags,
1301                                          eofb, XFS_ICI_EOFBLOCKS_TAG);
1302 }
1303
1304 /*
1305  * Run eofblocks scans on the quotas applicable to the inode. For inodes with
1306  * multiple quotas, we don't know exactly which quota caused an allocation
1307  * failure. We make a best effort by including each quota under low free space
1308  * conditions (less than 1% free space) in the scan.
1309  */
1310 int
1311 xfs_inode_free_quota_eofblocks(
1312         struct xfs_inode *ip)
1313 {
1314         int scan = 0;
1315         struct xfs_eofblocks eofb = {0};
1316         struct xfs_dquot *dq;
1317
1318         ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
1319
1320         /*
1321          * Set the scan owner to avoid a potential livelock. Otherwise, the scan
1322          * can repeatedly trylock on the inode we're currently processing. We
1323          * run a sync scan to increase effectiveness and use the union filter to
1324          * cover all applicable quotas in a single scan.
1325          */
1326         eofb.eof_scan_owner = ip->i_ino;
1327         eofb.eof_flags = XFS_EOF_FLAGS_UNION|XFS_EOF_FLAGS_SYNC;
1328
1329         if (XFS_IS_UQUOTA_ENFORCED(ip->i_mount)) {
1330                 dq = xfs_inode_dquot(ip, XFS_DQ_USER);
1331                 if (dq && xfs_dquot_lowsp(dq)) {
1332                         eofb.eof_uid = VFS_I(ip)->i_uid;
1333                         eofb.eof_flags |= XFS_EOF_FLAGS_UID;
1334                         scan = 1;
1335                 }
1336         }
1337
1338         if (XFS_IS_GQUOTA_ENFORCED(ip->i_mount)) {
1339                 dq = xfs_inode_dquot(ip, XFS_DQ_GROUP);
1340                 if (dq && xfs_dquot_lowsp(dq)) {
1341                         eofb.eof_gid = VFS_I(ip)->i_gid;
1342                         eofb.eof_flags |= XFS_EOF_FLAGS_GID;
1343                         scan = 1;
1344                 }
1345         }
1346
1347         if (scan)
1348                 xfs_icache_free_eofblocks(ip->i_mount, &eofb);
1349
1350         return scan;
1351 }
1352
1353 void
1354 xfs_inode_set_eofblocks_tag(
1355         xfs_inode_t     *ip)
1356 {
1357         struct xfs_mount *mp = ip->i_mount;
1358         struct xfs_perag *pag;
1359         int tagged;
1360
1361         pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
1362         spin_lock(&pag->pag_ici_lock);
1363         trace_xfs_inode_set_eofblocks_tag(ip);
1364
1365         tagged = radix_tree_tagged(&pag->pag_ici_root,
1366                                    XFS_ICI_EOFBLOCKS_TAG);
1367         radix_tree_tag_set(&pag->pag_ici_root,
1368                            XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
1369                            XFS_ICI_EOFBLOCKS_TAG);
1370         if (!tagged) {
1371                 /* propagate the eofblocks tag up into the perag radix tree */
1372                 spin_lock(&ip->i_mount->m_perag_lock);
1373                 radix_tree_tag_set(&ip->i_mount->m_perag_tree,
1374                                    XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
1375                                    XFS_ICI_EOFBLOCKS_TAG);
1376                 spin_unlock(&ip->i_mount->m_perag_lock);
1377
1378                 /* kick off background trimming */
1379                 xfs_queue_eofblocks(ip->i_mount);
1380
1381                 trace_xfs_perag_set_eofblocks(ip->i_mount, pag->pag_agno,
1382                                               -1, _RET_IP_);
1383         }
1384
1385         spin_unlock(&pag->pag_ici_lock);
1386         xfs_perag_put(pag);
1387 }
1388
1389 void
1390 xfs_inode_clear_eofblocks_tag(
1391         xfs_inode_t     *ip)
1392 {
1393         struct xfs_mount *mp = ip->i_mount;
1394         struct xfs_perag *pag;
1395
1396         pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
1397         spin_lock(&pag->pag_ici_lock);
1398         trace_xfs_inode_clear_eofblocks_tag(ip);
1399
1400         radix_tree_tag_clear(&pag->pag_ici_root,
1401                              XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
1402                              XFS_ICI_EOFBLOCKS_TAG);
1403         if (!radix_tree_tagged(&pag->pag_ici_root, XFS_ICI_EOFBLOCKS_TAG)) {
1404                 /* clear the eofblocks tag from the perag radix tree */
1405                 spin_lock(&ip->i_mount->m_perag_lock);
1406                 radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
1407                                      XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
1408                                      XFS_ICI_EOFBLOCKS_TAG);
1409                 spin_unlock(&ip->i_mount->m_perag_lock);
1410                 trace_xfs_perag_clear_eofblocks(ip->i_mount, pag->pag_agno,
1411                                                -1, _RET_IP_);
1412         }
1413
1414         spin_unlock(&pag->pag_ici_lock);
1415         xfs_perag_put(pag);
1416 }
1417