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