xfs: pass the goal of the incore inode walk to xfs_inode_walk()
[linux-2.6-microblaze.git] / fs / xfs / xfs_icache.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6 #include "xfs.h"
7 #include "xfs_fs.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_sb.h"
13 #include "xfs_mount.h"
14 #include "xfs_inode.h"
15 #include "xfs_trans.h"
16 #include "xfs_trans_priv.h"
17 #include "xfs_inode_item.h"
18 #include "xfs_quota.h"
19 #include "xfs_trace.h"
20 #include "xfs_icache.h"
21 #include "xfs_bmap_util.h"
22 #include "xfs_dquot_item.h"
23 #include "xfs_dquot.h"
24 #include "xfs_reflink.h"
25 #include "xfs_ialloc.h"
26
27 #include <linux/iversion.h>
28
29 /* Radix tree tags for incore inode tree. */
30
31 /* inode is to be reclaimed */
32 #define XFS_ICI_RECLAIM_TAG     0
33 /* Inode has speculative preallocations (posteof or cow) to clean. */
34 #define XFS_ICI_BLOCKGC_TAG     1
35
36 /*
37  * The goal for walking incore inodes.  These can correspond with incore inode
38  * radix tree tags when convenient.  Avoid existing XFS_IWALK namespace.
39  */
40 enum xfs_icwalk_goal {
41         /* Goals that are not related to tags; these must be < 0. */
42         XFS_ICWALK_DQRELE       = -1,
43
44         /* Goals directly associated with tagged inodes. */
45         XFS_ICWALK_BLOCKGC      = XFS_ICI_BLOCKGC_TAG,
46 };
47
48 #define XFS_ICWALK_NULL_TAG     (-1U)
49
50 /* Compute the inode radix tree tag for this goal. */
51 static inline unsigned int
52 xfs_icwalk_tag(enum xfs_icwalk_goal goal)
53 {
54         return goal < 0 ? XFS_ICWALK_NULL_TAG : goal;
55 }
56
57 static int xfs_icwalk(struct xfs_mount *mp, int iter_flags,
58                 int (*execute)(struct xfs_inode *ip, void *args),
59                 void *args, enum xfs_icwalk_goal goal);
60 static int xfs_icwalk_ag(struct xfs_perag *pag, int iter_flags,
61                 int (*execute)(struct xfs_inode *ip, void *args),
62                 void *args, enum xfs_icwalk_goal goal);
63
64 /*
65  * Private inode cache walk flags for struct xfs_eofblocks.  Must not coincide
66  * with XFS_EOF_FLAGS_*.
67  */
68 #define XFS_ICWALK_FLAG_DROP_UDQUOT     (1U << 31)
69 #define XFS_ICWALK_FLAG_DROP_GDQUOT     (1U << 30)
70 #define XFS_ICWALK_FLAG_DROP_PDQUOT     (1U << 29)
71
72 #define XFS_ICWALK_PRIVATE_FLAGS        (XFS_ICWALK_FLAG_DROP_UDQUOT | \
73                                          XFS_ICWALK_FLAG_DROP_GDQUOT | \
74                                          XFS_ICWALK_FLAG_DROP_PDQUOT)
75
76 /*
77  * Allocate and initialise an xfs_inode.
78  */
79 struct xfs_inode *
80 xfs_inode_alloc(
81         struct xfs_mount        *mp,
82         xfs_ino_t               ino)
83 {
84         struct xfs_inode        *ip;
85
86         /*
87          * XXX: If this didn't occur in transactions, we could drop GFP_NOFAIL
88          * and return NULL here on ENOMEM.
89          */
90         ip = kmem_cache_alloc(xfs_inode_zone, GFP_KERNEL | __GFP_NOFAIL);
91
92         if (inode_init_always(mp->m_super, VFS_I(ip))) {
93                 kmem_cache_free(xfs_inode_zone, ip);
94                 return NULL;
95         }
96
97         /* VFS doesn't initialise i_mode! */
98         VFS_I(ip)->i_mode = 0;
99
100         XFS_STATS_INC(mp, vn_active);
101         ASSERT(atomic_read(&ip->i_pincount) == 0);
102         ASSERT(ip->i_ino == 0);
103
104         /* initialise the xfs inode */
105         ip->i_ino = ino;
106         ip->i_mount = mp;
107         memset(&ip->i_imap, 0, sizeof(struct xfs_imap));
108         ip->i_afp = NULL;
109         ip->i_cowfp = NULL;
110         memset(&ip->i_df, 0, sizeof(ip->i_df));
111         ip->i_flags = 0;
112         ip->i_delayed_blks = 0;
113         ip->i_diflags2 = mp->m_ino_geo.new_diflags2;
114         ip->i_nblocks = 0;
115         ip->i_forkoff = 0;
116         ip->i_sick = 0;
117         ip->i_checked = 0;
118         INIT_WORK(&ip->i_ioend_work, xfs_end_io);
119         INIT_LIST_HEAD(&ip->i_ioend_list);
120         spin_lock_init(&ip->i_ioend_lock);
121
122         return ip;
123 }
124
125 STATIC void
126 xfs_inode_free_callback(
127         struct rcu_head         *head)
128 {
129         struct inode            *inode = container_of(head, struct inode, i_rcu);
130         struct xfs_inode        *ip = XFS_I(inode);
131
132         switch (VFS_I(ip)->i_mode & S_IFMT) {
133         case S_IFREG:
134         case S_IFDIR:
135         case S_IFLNK:
136                 xfs_idestroy_fork(&ip->i_df);
137                 break;
138         }
139
140         if (ip->i_afp) {
141                 xfs_idestroy_fork(ip->i_afp);
142                 kmem_cache_free(xfs_ifork_zone, ip->i_afp);
143         }
144         if (ip->i_cowfp) {
145                 xfs_idestroy_fork(ip->i_cowfp);
146                 kmem_cache_free(xfs_ifork_zone, ip->i_cowfp);
147         }
148         if (ip->i_itemp) {
149                 ASSERT(!test_bit(XFS_LI_IN_AIL,
150                                  &ip->i_itemp->ili_item.li_flags));
151                 xfs_inode_item_destroy(ip);
152                 ip->i_itemp = NULL;
153         }
154
155         kmem_cache_free(xfs_inode_zone, ip);
156 }
157
158 static void
159 __xfs_inode_free(
160         struct xfs_inode        *ip)
161 {
162         /* asserts to verify all state is correct here */
163         ASSERT(atomic_read(&ip->i_pincount) == 0);
164         ASSERT(!ip->i_itemp || list_empty(&ip->i_itemp->ili_item.li_bio_list));
165         XFS_STATS_DEC(ip->i_mount, vn_active);
166
167         call_rcu(&VFS_I(ip)->i_rcu, xfs_inode_free_callback);
168 }
169
170 void
171 xfs_inode_free(
172         struct xfs_inode        *ip)
173 {
174         ASSERT(!xfs_iflags_test(ip, XFS_IFLUSHING));
175
176         /*
177          * Because we use RCU freeing we need to ensure the inode always
178          * appears to be reclaimed with an invalid inode number when in the
179          * free state. The ip->i_flags_lock provides the barrier against lookup
180          * races.
181          */
182         spin_lock(&ip->i_flags_lock);
183         ip->i_flags = XFS_IRECLAIM;
184         ip->i_ino = 0;
185         spin_unlock(&ip->i_flags_lock);
186
187         __xfs_inode_free(ip);
188 }
189
190 /*
191  * Queue background inode reclaim work if there are reclaimable inodes and there
192  * isn't reclaim work already scheduled or in progress.
193  */
194 static void
195 xfs_reclaim_work_queue(
196         struct xfs_mount        *mp)
197 {
198
199         rcu_read_lock();
200         if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_RECLAIM_TAG)) {
201                 queue_delayed_work(mp->m_reclaim_workqueue, &mp->m_reclaim_work,
202                         msecs_to_jiffies(xfs_syncd_centisecs / 6 * 10));
203         }
204         rcu_read_unlock();
205 }
206
207 static void
208 xfs_perag_set_reclaim_tag(
209         struct xfs_perag        *pag)
210 {
211         struct xfs_mount        *mp = pag->pag_mount;
212
213         lockdep_assert_held(&pag->pag_ici_lock);
214         if (pag->pag_ici_reclaimable++)
215                 return;
216
217         /* propagate the reclaim tag up into the perag radix tree */
218         spin_lock(&mp->m_perag_lock);
219         radix_tree_tag_set(&mp->m_perag_tree, pag->pag_agno,
220                            XFS_ICI_RECLAIM_TAG);
221         spin_unlock(&mp->m_perag_lock);
222
223         /* schedule periodic background inode reclaim */
224         xfs_reclaim_work_queue(mp);
225
226         trace_xfs_perag_set_reclaim(mp, pag->pag_agno, -1, _RET_IP_);
227 }
228
229 static void
230 xfs_perag_clear_reclaim_tag(
231         struct xfs_perag        *pag)
232 {
233         struct xfs_mount        *mp = pag->pag_mount;
234
235         lockdep_assert_held(&pag->pag_ici_lock);
236         if (--pag->pag_ici_reclaimable)
237                 return;
238
239         /* clear the reclaim tag from the perag radix tree */
240         spin_lock(&mp->m_perag_lock);
241         radix_tree_tag_clear(&mp->m_perag_tree, pag->pag_agno,
242                              XFS_ICI_RECLAIM_TAG);
243         spin_unlock(&mp->m_perag_lock);
244         trace_xfs_perag_clear_reclaim(mp, pag->pag_agno, -1, _RET_IP_);
245 }
246
247
248 /*
249  * We set the inode flag atomically with the radix tree tag.
250  * Once we get tag lookups on the radix tree, this inode flag
251  * can go away.
252  */
253 void
254 xfs_inode_set_reclaim_tag(
255         struct xfs_inode        *ip)
256 {
257         struct xfs_mount        *mp = ip->i_mount;
258         struct xfs_perag        *pag;
259
260         pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
261         spin_lock(&pag->pag_ici_lock);
262         spin_lock(&ip->i_flags_lock);
263
264         radix_tree_tag_set(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ip->i_ino),
265                            XFS_ICI_RECLAIM_TAG);
266         xfs_perag_set_reclaim_tag(pag);
267         __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
268
269         spin_unlock(&ip->i_flags_lock);
270         spin_unlock(&pag->pag_ici_lock);
271         xfs_perag_put(pag);
272 }
273
274 STATIC void
275 xfs_inode_clear_reclaim_tag(
276         struct xfs_perag        *pag,
277         xfs_ino_t               ino)
278 {
279         radix_tree_tag_clear(&pag->pag_ici_root,
280                              XFS_INO_TO_AGINO(pag->pag_mount, ino),
281                              XFS_ICI_RECLAIM_TAG);
282         xfs_perag_clear_reclaim_tag(pag);
283 }
284
285 static void
286 xfs_inew_wait(
287         struct xfs_inode        *ip)
288 {
289         wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_INEW_BIT);
290         DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_INEW_BIT);
291
292         do {
293                 prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
294                 if (!xfs_iflags_test(ip, XFS_INEW))
295                         break;
296                 schedule();
297         } while (true);
298         finish_wait(wq, &wait.wq_entry);
299 }
300
301 /*
302  * When we recycle a reclaimable inode, we need to re-initialise the VFS inode
303  * part of the structure. This is made more complex by the fact we store
304  * information about the on-disk values in the VFS inode and so we can't just
305  * overwrite the values unconditionally. Hence we save the parameters we
306  * need to retain across reinitialisation, and rewrite them into the VFS inode
307  * after reinitialisation even if it fails.
308  */
309 static int
310 xfs_reinit_inode(
311         struct xfs_mount        *mp,
312         struct inode            *inode)
313 {
314         int             error;
315         uint32_t        nlink = inode->i_nlink;
316         uint32_t        generation = inode->i_generation;
317         uint64_t        version = inode_peek_iversion(inode);
318         umode_t         mode = inode->i_mode;
319         dev_t           dev = inode->i_rdev;
320         kuid_t          uid = inode->i_uid;
321         kgid_t          gid = inode->i_gid;
322
323         error = inode_init_always(mp->m_super, inode);
324
325         set_nlink(inode, nlink);
326         inode->i_generation = generation;
327         inode_set_iversion_queried(inode, version);
328         inode->i_mode = mode;
329         inode->i_rdev = dev;
330         inode->i_uid = uid;
331         inode->i_gid = gid;
332         return error;
333 }
334
335 /*
336  * If we are allocating a new inode, then check what was returned is
337  * actually a free, empty inode. If we are not allocating an inode,
338  * then check we didn't find a free inode.
339  *
340  * Returns:
341  *      0               if the inode free state matches the lookup context
342  *      -ENOENT         if the inode is free and we are not allocating
343  *      -EFSCORRUPTED   if there is any state mismatch at all
344  */
345 static int
346 xfs_iget_check_free_state(
347         struct xfs_inode        *ip,
348         int                     flags)
349 {
350         if (flags & XFS_IGET_CREATE) {
351                 /* should be a free inode */
352                 if (VFS_I(ip)->i_mode != 0) {
353                         xfs_warn(ip->i_mount,
354 "Corruption detected! Free inode 0x%llx not marked free! (mode 0x%x)",
355                                 ip->i_ino, VFS_I(ip)->i_mode);
356                         return -EFSCORRUPTED;
357                 }
358
359                 if (ip->i_nblocks != 0) {
360                         xfs_warn(ip->i_mount,
361 "Corruption detected! Free inode 0x%llx has blocks allocated!",
362                                 ip->i_ino);
363                         return -EFSCORRUPTED;
364                 }
365                 return 0;
366         }
367
368         /* should be an allocated inode */
369         if (VFS_I(ip)->i_mode == 0)
370                 return -ENOENT;
371
372         return 0;
373 }
374
375 /*
376  * Check the validity of the inode we just found it the cache
377  */
378 static int
379 xfs_iget_cache_hit(
380         struct xfs_perag        *pag,
381         struct xfs_inode        *ip,
382         xfs_ino_t               ino,
383         int                     flags,
384         int                     lock_flags) __releases(RCU)
385 {
386         struct inode            *inode = VFS_I(ip);
387         struct xfs_mount        *mp = ip->i_mount;
388         int                     error;
389
390         /*
391          * check for re-use of an inode within an RCU grace period due to the
392          * radix tree nodes not being updated yet. We monitor for this by
393          * setting the inode number to zero before freeing the inode structure.
394          * If the inode has been reallocated and set up, then the inode number
395          * will not match, so check for that, too.
396          */
397         spin_lock(&ip->i_flags_lock);
398         if (ip->i_ino != ino) {
399                 trace_xfs_iget_skip(ip);
400                 XFS_STATS_INC(mp, xs_ig_frecycle);
401                 error = -EAGAIN;
402                 goto out_error;
403         }
404
405
406         /*
407          * If we are racing with another cache hit that is currently
408          * instantiating this inode or currently recycling it out of
409          * reclaimabe state, wait for the initialisation to complete
410          * before continuing.
411          *
412          * XXX(hch): eventually we should do something equivalent to
413          *           wait_on_inode to wait for these flags to be cleared
414          *           instead of polling for it.
415          */
416         if (ip->i_flags & (XFS_INEW|XFS_IRECLAIM)) {
417                 trace_xfs_iget_skip(ip);
418                 XFS_STATS_INC(mp, xs_ig_frecycle);
419                 error = -EAGAIN;
420                 goto out_error;
421         }
422
423         /*
424          * Check the inode free state is valid. This also detects lookup
425          * racing with unlinks.
426          */
427         error = xfs_iget_check_free_state(ip, flags);
428         if (error)
429                 goto out_error;
430
431         /*
432          * If IRECLAIMABLE is set, we've torn down the VFS inode already.
433          * Need to carefully get it back into useable state.
434          */
435         if (ip->i_flags & XFS_IRECLAIMABLE) {
436                 trace_xfs_iget_reclaim(ip);
437
438                 if (flags & XFS_IGET_INCORE) {
439                         error = -EAGAIN;
440                         goto out_error;
441                 }
442
443                 /*
444                  * We need to set XFS_IRECLAIM to prevent xfs_reclaim_inode
445                  * from stomping over us while we recycle the inode.  We can't
446                  * clear the radix tree reclaimable tag yet as it requires
447                  * pag_ici_lock to be held exclusive.
448                  */
449                 ip->i_flags |= XFS_IRECLAIM;
450
451                 spin_unlock(&ip->i_flags_lock);
452                 rcu_read_unlock();
453
454                 ASSERT(!rwsem_is_locked(&inode->i_rwsem));
455                 error = xfs_reinit_inode(mp, inode);
456                 if (error) {
457                         bool wake;
458                         /*
459                          * Re-initializing the inode failed, and we are in deep
460                          * trouble.  Try to re-add it to the reclaim list.
461                          */
462                         rcu_read_lock();
463                         spin_lock(&ip->i_flags_lock);
464                         wake = !!__xfs_iflags_test(ip, XFS_INEW);
465                         ip->i_flags &= ~(XFS_INEW | XFS_IRECLAIM);
466                         if (wake)
467                                 wake_up_bit(&ip->i_flags, __XFS_INEW_BIT);
468                         ASSERT(ip->i_flags & XFS_IRECLAIMABLE);
469                         trace_xfs_iget_reclaim_fail(ip);
470                         goto out_error;
471                 }
472
473                 spin_lock(&pag->pag_ici_lock);
474                 spin_lock(&ip->i_flags_lock);
475
476                 /*
477                  * Clear the per-lifetime state in the inode as we are now
478                  * effectively a new inode and need to return to the initial
479                  * state before reuse occurs.
480                  */
481                 ip->i_flags &= ~XFS_IRECLAIM_RESET_FLAGS;
482                 ip->i_flags |= XFS_INEW;
483                 xfs_inode_clear_reclaim_tag(pag, ip->i_ino);
484                 inode->i_state = I_NEW;
485                 ip->i_sick = 0;
486                 ip->i_checked = 0;
487
488                 spin_unlock(&ip->i_flags_lock);
489                 spin_unlock(&pag->pag_ici_lock);
490         } else {
491                 /* If the VFS inode is being torn down, pause and try again. */
492                 if (!igrab(inode)) {
493                         trace_xfs_iget_skip(ip);
494                         error = -EAGAIN;
495                         goto out_error;
496                 }
497
498                 /* We've got a live one. */
499                 spin_unlock(&ip->i_flags_lock);
500                 rcu_read_unlock();
501                 trace_xfs_iget_hit(ip);
502         }
503
504         if (lock_flags != 0)
505                 xfs_ilock(ip, lock_flags);
506
507         if (!(flags & XFS_IGET_INCORE))
508                 xfs_iflags_clear(ip, XFS_ISTALE);
509         XFS_STATS_INC(mp, xs_ig_found);
510
511         return 0;
512
513 out_error:
514         spin_unlock(&ip->i_flags_lock);
515         rcu_read_unlock();
516         return error;
517 }
518
519
520 static int
521 xfs_iget_cache_miss(
522         struct xfs_mount        *mp,
523         struct xfs_perag        *pag,
524         xfs_trans_t             *tp,
525         xfs_ino_t               ino,
526         struct xfs_inode        **ipp,
527         int                     flags,
528         int                     lock_flags)
529 {
530         struct xfs_inode        *ip;
531         int                     error;
532         xfs_agino_t             agino = XFS_INO_TO_AGINO(mp, ino);
533         int                     iflags;
534
535         ip = xfs_inode_alloc(mp, ino);
536         if (!ip)
537                 return -ENOMEM;
538
539         error = xfs_imap(mp, tp, ip->i_ino, &ip->i_imap, flags);
540         if (error)
541                 goto out_destroy;
542
543         /*
544          * For version 5 superblocks, if we are initialising a new inode and we
545          * are not utilising the XFS_MOUNT_IKEEP inode cluster mode, we can
546          * simply build the new inode core with a random generation number.
547          *
548          * For version 4 (and older) superblocks, log recovery is dependent on
549          * the i_flushiter field being initialised from the current on-disk
550          * value and hence we must also read the inode off disk even when
551          * initializing new inodes.
552          */
553         if (xfs_sb_version_has_v3inode(&mp->m_sb) &&
554             (flags & XFS_IGET_CREATE) && !(mp->m_flags & XFS_MOUNT_IKEEP)) {
555                 VFS_I(ip)->i_generation = prandom_u32();
556         } else {
557                 struct xfs_buf          *bp;
558
559                 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &bp);
560                 if (error)
561                         goto out_destroy;
562
563                 error = xfs_inode_from_disk(ip,
564                                 xfs_buf_offset(bp, ip->i_imap.im_boffset));
565                 if (!error)
566                         xfs_buf_set_ref(bp, XFS_INO_REF);
567                 xfs_trans_brelse(tp, bp);
568
569                 if (error)
570                         goto out_destroy;
571         }
572
573         trace_xfs_iget_miss(ip);
574
575         /*
576          * Check the inode free state is valid. This also detects lookup
577          * racing with unlinks.
578          */
579         error = xfs_iget_check_free_state(ip, flags);
580         if (error)
581                 goto out_destroy;
582
583         /*
584          * Preload the radix tree so we can insert safely under the
585          * write spinlock. Note that we cannot sleep inside the preload
586          * region. Since we can be called from transaction context, don't
587          * recurse into the file system.
588          */
589         if (radix_tree_preload(GFP_NOFS)) {
590                 error = -EAGAIN;
591                 goto out_destroy;
592         }
593
594         /*
595          * Because the inode hasn't been added to the radix-tree yet it can't
596          * be found by another thread, so we can do the non-sleeping lock here.
597          */
598         if (lock_flags) {
599                 if (!xfs_ilock_nowait(ip, lock_flags))
600                         BUG();
601         }
602
603         /*
604          * These values must be set before inserting the inode into the radix
605          * tree as the moment it is inserted a concurrent lookup (allowed by the
606          * RCU locking mechanism) can find it and that lookup must see that this
607          * is an inode currently under construction (i.e. that XFS_INEW is set).
608          * The ip->i_flags_lock that protects the XFS_INEW flag forms the
609          * memory barrier that ensures this detection works correctly at lookup
610          * time.
611          */
612         iflags = XFS_INEW;
613         if (flags & XFS_IGET_DONTCACHE)
614                 d_mark_dontcache(VFS_I(ip));
615         ip->i_udquot = NULL;
616         ip->i_gdquot = NULL;
617         ip->i_pdquot = NULL;
618         xfs_iflags_set(ip, iflags);
619
620         /* insert the new inode */
621         spin_lock(&pag->pag_ici_lock);
622         error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
623         if (unlikely(error)) {
624                 WARN_ON(error != -EEXIST);
625                 XFS_STATS_INC(mp, xs_ig_dup);
626                 error = -EAGAIN;
627                 goto out_preload_end;
628         }
629         spin_unlock(&pag->pag_ici_lock);
630         radix_tree_preload_end();
631
632         *ipp = ip;
633         return 0;
634
635 out_preload_end:
636         spin_unlock(&pag->pag_ici_lock);
637         radix_tree_preload_end();
638         if (lock_flags)
639                 xfs_iunlock(ip, lock_flags);
640 out_destroy:
641         __destroy_inode(VFS_I(ip));
642         xfs_inode_free(ip);
643         return error;
644 }
645
646 /*
647  * Look up an inode by number in the given file system.  The inode is looked up
648  * in the cache held in each AG.  If the inode is found in the cache, initialise
649  * the vfs inode if necessary.
650  *
651  * If it is not in core, read it in from the file system's device, add it to the
652  * cache and initialise the vfs inode.
653  *
654  * The inode is locked according to the value of the lock_flags parameter.
655  * Inode lookup is only done during metadata operations and not as part of the
656  * data IO path. Hence we only allow locking of the XFS_ILOCK during lookup.
657  */
658 int
659 xfs_iget(
660         struct xfs_mount        *mp,
661         struct xfs_trans        *tp,
662         xfs_ino_t               ino,
663         uint                    flags,
664         uint                    lock_flags,
665         struct xfs_inode        **ipp)
666 {
667         struct xfs_inode        *ip;
668         struct xfs_perag        *pag;
669         xfs_agino_t             agino;
670         int                     error;
671
672         ASSERT((lock_flags & (XFS_IOLOCK_EXCL | XFS_IOLOCK_SHARED)) == 0);
673
674         /* reject inode numbers outside existing AGs */
675         if (!ino || XFS_INO_TO_AGNO(mp, ino) >= mp->m_sb.sb_agcount)
676                 return -EINVAL;
677
678         XFS_STATS_INC(mp, xs_ig_attempts);
679
680         /* get the perag structure and ensure that it's inode capable */
681         pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ino));
682         agino = XFS_INO_TO_AGINO(mp, ino);
683
684 again:
685         error = 0;
686         rcu_read_lock();
687         ip = radix_tree_lookup(&pag->pag_ici_root, agino);
688
689         if (ip) {
690                 error = xfs_iget_cache_hit(pag, ip, ino, flags, lock_flags);
691                 if (error)
692                         goto out_error_or_again;
693         } else {
694                 rcu_read_unlock();
695                 if (flags & XFS_IGET_INCORE) {
696                         error = -ENODATA;
697                         goto out_error_or_again;
698                 }
699                 XFS_STATS_INC(mp, xs_ig_missed);
700
701                 error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip,
702                                                         flags, lock_flags);
703                 if (error)
704                         goto out_error_or_again;
705         }
706         xfs_perag_put(pag);
707
708         *ipp = ip;
709
710         /*
711          * If we have a real type for an on-disk inode, we can setup the inode
712          * now.  If it's a new inode being created, xfs_ialloc will handle it.
713          */
714         if (xfs_iflags_test(ip, XFS_INEW) && VFS_I(ip)->i_mode != 0)
715                 xfs_setup_existing_inode(ip);
716         return 0;
717
718 out_error_or_again:
719         if (!(flags & XFS_IGET_INCORE) && error == -EAGAIN) {
720                 delay(1);
721                 goto again;
722         }
723         xfs_perag_put(pag);
724         return error;
725 }
726
727 /*
728  * "Is this a cached inode that's also allocated?"
729  *
730  * Look up an inode by number in the given file system.  If the inode is
731  * in cache and isn't in purgatory, return 1 if the inode is allocated
732  * and 0 if it is not.  For all other cases (not in cache, being torn
733  * down, etc.), return a negative error code.
734  *
735  * The caller has to prevent inode allocation and freeing activity,
736  * presumably by locking the AGI buffer.   This is to ensure that an
737  * inode cannot transition from allocated to freed until the caller is
738  * ready to allow that.  If the inode is in an intermediate state (new,
739  * reclaimable, or being reclaimed), -EAGAIN will be returned; if the
740  * inode is not in the cache, -ENOENT will be returned.  The caller must
741  * deal with these scenarios appropriately.
742  *
743  * This is a specialized use case for the online scrubber; if you're
744  * reading this, you probably want xfs_iget.
745  */
746 int
747 xfs_icache_inode_is_allocated(
748         struct xfs_mount        *mp,
749         struct xfs_trans        *tp,
750         xfs_ino_t               ino,
751         bool                    *inuse)
752 {
753         struct xfs_inode        *ip;
754         int                     error;
755
756         error = xfs_iget(mp, tp, ino, XFS_IGET_INCORE, 0, &ip);
757         if (error)
758                 return error;
759
760         *inuse = !!(VFS_I(ip)->i_mode);
761         xfs_irele(ip);
762         return 0;
763 }
764
765 /*
766  * The inode lookup is done in batches to keep the amount of lock traffic and
767  * radix tree lookups to a minimum. The batch size is a trade off between
768  * lookup reduction and stack usage. This is in the reclaim path, so we can't
769  * be too greedy.
770  *
771  * XXX: This will be moved closer to xfs_icwalk* once we get rid of the
772  * separate reclaim walk functions.
773  */
774 #define XFS_LOOKUP_BATCH        32
775
776 #ifdef CONFIG_XFS_QUOTA
777 /* Drop this inode's dquots. */
778 static int
779 xfs_dqrele_inode(
780         struct xfs_inode        *ip,
781         void                    *priv)
782 {
783         struct xfs_eofblocks    *eofb = priv;
784
785         xfs_ilock(ip, XFS_ILOCK_EXCL);
786         if (eofb->eof_flags & XFS_ICWALK_FLAG_DROP_UDQUOT) {
787                 xfs_qm_dqrele(ip->i_udquot);
788                 ip->i_udquot = NULL;
789         }
790         if (eofb->eof_flags & XFS_ICWALK_FLAG_DROP_GDQUOT) {
791                 xfs_qm_dqrele(ip->i_gdquot);
792                 ip->i_gdquot = NULL;
793         }
794         if (eofb->eof_flags & XFS_ICWALK_FLAG_DROP_PDQUOT) {
795                 xfs_qm_dqrele(ip->i_pdquot);
796                 ip->i_pdquot = NULL;
797         }
798         xfs_iunlock(ip, XFS_ILOCK_EXCL);
799         return 0;
800 }
801
802 /*
803  * Detach all dquots from incore inodes if we can.  The caller must already
804  * have dropped the relevant XFS_[UGP]QUOTA_ACTIVE flags so that dquots will
805  * not get reattached.
806  */
807 int
808 xfs_dqrele_all_inodes(
809         struct xfs_mount        *mp,
810         unsigned int            qflags)
811 {
812         struct xfs_eofblocks    eofb = { .eof_flags = 0 };
813
814         if (qflags & XFS_UQUOTA_ACCT)
815                 eofb.eof_flags |= XFS_ICWALK_FLAG_DROP_UDQUOT;
816         if (qflags & XFS_GQUOTA_ACCT)
817                 eofb.eof_flags |= XFS_ICWALK_FLAG_DROP_GDQUOT;
818         if (qflags & XFS_PQUOTA_ACCT)
819                 eofb.eof_flags |= XFS_ICWALK_FLAG_DROP_PDQUOT;
820
821         return xfs_icwalk(mp, XFS_INODE_WALK_INEW_WAIT, xfs_dqrele_inode,
822                         &eofb, XFS_ICWALK_DQRELE);
823 }
824 #endif /* CONFIG_XFS_QUOTA */
825
826 /*
827  * Grab the inode for reclaim exclusively.
828  *
829  * We have found this inode via a lookup under RCU, so the inode may have
830  * already been freed, or it may be in the process of being recycled by
831  * xfs_iget(). In both cases, the inode will have XFS_IRECLAIM set. If the inode
832  * has been fully recycled by the time we get the i_flags_lock, XFS_IRECLAIMABLE
833  * will not be set. Hence we need to check for both these flag conditions to
834  * avoid inodes that are no longer reclaim candidates.
835  *
836  * Note: checking for other state flags here, under the i_flags_lock or not, is
837  * racy and should be avoided. Those races should be resolved only after we have
838  * ensured that we are able to reclaim this inode and the world can see that we
839  * are going to reclaim it.
840  *
841  * Return true if we grabbed it, false otherwise.
842  */
843 static bool
844 xfs_reclaim_inode_grab(
845         struct xfs_inode        *ip)
846 {
847         ASSERT(rcu_read_lock_held());
848
849         spin_lock(&ip->i_flags_lock);
850         if (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) ||
851             __xfs_iflags_test(ip, XFS_IRECLAIM)) {
852                 /* not a reclaim candidate. */
853                 spin_unlock(&ip->i_flags_lock);
854                 return false;
855         }
856         __xfs_iflags_set(ip, XFS_IRECLAIM);
857         spin_unlock(&ip->i_flags_lock);
858         return true;
859 }
860
861 /*
862  * Inode reclaim is non-blocking, so the default action if progress cannot be
863  * made is to "requeue" the inode for reclaim by unlocking it and clearing the
864  * XFS_IRECLAIM flag.  If we are in a shutdown state, we don't care about
865  * blocking anymore and hence we can wait for the inode to be able to reclaim
866  * it.
867  *
868  * We do no IO here - if callers require inodes to be cleaned they must push the
869  * AIL first to trigger writeback of dirty inodes.  This enables writeback to be
870  * done in the background in a non-blocking manner, and enables memory reclaim
871  * to make progress without blocking.
872  */
873 static void
874 xfs_reclaim_inode(
875         struct xfs_inode        *ip,
876         struct xfs_perag        *pag)
877 {
878         xfs_ino_t               ino = ip->i_ino; /* for radix_tree_delete */
879
880         if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL))
881                 goto out;
882         if (xfs_iflags_test_and_set(ip, XFS_IFLUSHING))
883                 goto out_iunlock;
884
885         if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
886                 xfs_iunpin_wait(ip);
887                 xfs_iflush_abort(ip);
888                 goto reclaim;
889         }
890         if (xfs_ipincount(ip))
891                 goto out_clear_flush;
892         if (!xfs_inode_clean(ip))
893                 goto out_clear_flush;
894
895         xfs_iflags_clear(ip, XFS_IFLUSHING);
896 reclaim:
897
898         /*
899          * Because we use RCU freeing we need to ensure the inode always appears
900          * to be reclaimed with an invalid inode number when in the free state.
901          * We do this as early as possible under the ILOCK so that
902          * xfs_iflush_cluster() and xfs_ifree_cluster() can be guaranteed to
903          * detect races with us here. By doing this, we guarantee that once
904          * xfs_iflush_cluster() or xfs_ifree_cluster() has locked XFS_ILOCK that
905          * it will see either a valid inode that will serialise correctly, or it
906          * will see an invalid inode that it can skip.
907          */
908         spin_lock(&ip->i_flags_lock);
909         ip->i_flags = XFS_IRECLAIM;
910         ip->i_ino = 0;
911         spin_unlock(&ip->i_flags_lock);
912
913         xfs_iunlock(ip, XFS_ILOCK_EXCL);
914
915         XFS_STATS_INC(ip->i_mount, xs_ig_reclaims);
916         /*
917          * Remove the inode from the per-AG radix tree.
918          *
919          * Because radix_tree_delete won't complain even if the item was never
920          * added to the tree assert that it's been there before to catch
921          * problems with the inode life time early on.
922          */
923         spin_lock(&pag->pag_ici_lock);
924         if (!radix_tree_delete(&pag->pag_ici_root,
925                                 XFS_INO_TO_AGINO(ip->i_mount, ino)))
926                 ASSERT(0);
927         xfs_perag_clear_reclaim_tag(pag);
928         spin_unlock(&pag->pag_ici_lock);
929
930         /*
931          * Here we do an (almost) spurious inode lock in order to coordinate
932          * with inode cache radix tree lookups.  This is because the lookup
933          * can reference the inodes in the cache without taking references.
934          *
935          * We make that OK here by ensuring that we wait until the inode is
936          * unlocked after the lookup before we go ahead and free it.
937          */
938         xfs_ilock(ip, XFS_ILOCK_EXCL);
939         ASSERT(!ip->i_udquot && !ip->i_gdquot && !ip->i_pdquot);
940         xfs_iunlock(ip, XFS_ILOCK_EXCL);
941         ASSERT(xfs_inode_clean(ip));
942
943         __xfs_inode_free(ip);
944         return;
945
946 out_clear_flush:
947         xfs_iflags_clear(ip, XFS_IFLUSHING);
948 out_iunlock:
949         xfs_iunlock(ip, XFS_ILOCK_EXCL);
950 out:
951         xfs_iflags_clear(ip, XFS_IRECLAIM);
952 }
953
954 /*
955  * Walk the AGs and reclaim the inodes in them. Even if the filesystem is
956  * corrupted, we still want to try to reclaim all the inodes. If we don't,
957  * then a shut down during filesystem unmount reclaim walk leak all the
958  * unreclaimed inodes.
959  *
960  * Returns non-zero if any AGs or inodes were skipped in the reclaim pass
961  * so that callers that want to block until all dirty inodes are written back
962  * and reclaimed can sanely loop.
963  */
964 static void
965 xfs_reclaim_inodes_ag(
966         struct xfs_mount        *mp,
967         int                     *nr_to_scan)
968 {
969         struct xfs_perag        *pag;
970         xfs_agnumber_t          ag = 0;
971
972         while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
973                 unsigned long   first_index = 0;
974                 int             done = 0;
975                 int             nr_found = 0;
976
977                 ag = pag->pag_agno + 1;
978
979                 first_index = READ_ONCE(pag->pag_ici_reclaim_cursor);
980                 do {
981                         struct xfs_inode *batch[XFS_LOOKUP_BATCH];
982                         int     i;
983
984                         rcu_read_lock();
985                         nr_found = radix_tree_gang_lookup_tag(
986                                         &pag->pag_ici_root,
987                                         (void **)batch, first_index,
988                                         XFS_LOOKUP_BATCH,
989                                         XFS_ICI_RECLAIM_TAG);
990                         if (!nr_found) {
991                                 done = 1;
992                                 rcu_read_unlock();
993                                 break;
994                         }
995
996                         /*
997                          * Grab the inodes before we drop the lock. if we found
998                          * nothing, nr == 0 and the loop will be skipped.
999                          */
1000                         for (i = 0; i < nr_found; i++) {
1001                                 struct xfs_inode *ip = batch[i];
1002
1003                                 if (done || !xfs_reclaim_inode_grab(ip))
1004                                         batch[i] = NULL;
1005
1006                                 /*
1007                                  * Update the index for the next lookup. Catch
1008                                  * overflows into the next AG range which can
1009                                  * occur if we have inodes in the last block of
1010                                  * the AG and we are currently pointing to the
1011                                  * last inode.
1012                                  *
1013                                  * Because we may see inodes that are from the
1014                                  * wrong AG due to RCU freeing and
1015                                  * reallocation, only update the index if it
1016                                  * lies in this AG. It was a race that lead us
1017                                  * to see this inode, so another lookup from
1018                                  * the same index will not find it again.
1019                                  */
1020                                 if (XFS_INO_TO_AGNO(mp, ip->i_ino) !=
1021                                                                 pag->pag_agno)
1022                                         continue;
1023                                 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
1024                                 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
1025                                         done = 1;
1026                         }
1027
1028                         /* unlock now we've grabbed the inodes. */
1029                         rcu_read_unlock();
1030
1031                         for (i = 0; i < nr_found; i++) {
1032                                 if (batch[i])
1033                                         xfs_reclaim_inode(batch[i], pag);
1034                         }
1035
1036                         *nr_to_scan -= XFS_LOOKUP_BATCH;
1037                         cond_resched();
1038                 } while (nr_found && !done && *nr_to_scan > 0);
1039
1040                 if (done)
1041                         first_index = 0;
1042                 WRITE_ONCE(pag->pag_ici_reclaim_cursor, first_index);
1043                 xfs_perag_put(pag);
1044         }
1045 }
1046
1047 void
1048 xfs_reclaim_inodes(
1049         struct xfs_mount        *mp)
1050 {
1051         int             nr_to_scan = INT_MAX;
1052
1053         while (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_RECLAIM_TAG)) {
1054                 xfs_ail_push_all_sync(mp->m_ail);
1055                 xfs_reclaim_inodes_ag(mp, &nr_to_scan);
1056         }
1057 }
1058
1059 /*
1060  * The shrinker infrastructure determines how many inodes we should scan for
1061  * reclaim. We want as many clean inodes ready to reclaim as possible, so we
1062  * push the AIL here. We also want to proactively free up memory if we can to
1063  * minimise the amount of work memory reclaim has to do so we kick the
1064  * background reclaim if it isn't already scheduled.
1065  */
1066 long
1067 xfs_reclaim_inodes_nr(
1068         struct xfs_mount        *mp,
1069         int                     nr_to_scan)
1070 {
1071         /* kick background reclaimer and push the AIL */
1072         xfs_reclaim_work_queue(mp);
1073         xfs_ail_push_all(mp->m_ail);
1074
1075         xfs_reclaim_inodes_ag(mp, &nr_to_scan);
1076         return 0;
1077 }
1078
1079 /*
1080  * Return the number of reclaimable inodes in the filesystem for
1081  * the shrinker to determine how much to reclaim.
1082  */
1083 int
1084 xfs_reclaim_inodes_count(
1085         struct xfs_mount        *mp)
1086 {
1087         struct xfs_perag        *pag;
1088         xfs_agnumber_t          ag = 0;
1089         int                     reclaimable = 0;
1090
1091         while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
1092                 ag = pag->pag_agno + 1;
1093                 reclaimable += pag->pag_ici_reclaimable;
1094                 xfs_perag_put(pag);
1095         }
1096         return reclaimable;
1097 }
1098
1099 STATIC bool
1100 xfs_inode_match_id(
1101         struct xfs_inode        *ip,
1102         struct xfs_eofblocks    *eofb)
1103 {
1104         if ((eofb->eof_flags & XFS_EOF_FLAGS_UID) &&
1105             !uid_eq(VFS_I(ip)->i_uid, eofb->eof_uid))
1106                 return false;
1107
1108         if ((eofb->eof_flags & XFS_EOF_FLAGS_GID) &&
1109             !gid_eq(VFS_I(ip)->i_gid, eofb->eof_gid))
1110                 return false;
1111
1112         if ((eofb->eof_flags & XFS_EOF_FLAGS_PRID) &&
1113             ip->i_projid != eofb->eof_prid)
1114                 return false;
1115
1116         return true;
1117 }
1118
1119 /*
1120  * A union-based inode filtering algorithm. Process the inode if any of the
1121  * criteria match. This is for global/internal scans only.
1122  */
1123 STATIC bool
1124 xfs_inode_match_id_union(
1125         struct xfs_inode        *ip,
1126         struct xfs_eofblocks    *eofb)
1127 {
1128         if ((eofb->eof_flags & XFS_EOF_FLAGS_UID) &&
1129             uid_eq(VFS_I(ip)->i_uid, eofb->eof_uid))
1130                 return true;
1131
1132         if ((eofb->eof_flags & XFS_EOF_FLAGS_GID) &&
1133             gid_eq(VFS_I(ip)->i_gid, eofb->eof_gid))
1134                 return true;
1135
1136         if ((eofb->eof_flags & XFS_EOF_FLAGS_PRID) &&
1137             ip->i_projid == eofb->eof_prid)
1138                 return true;
1139
1140         return false;
1141 }
1142
1143 /*
1144  * Is this inode @ip eligible for eof/cow block reclamation, given some
1145  * filtering parameters @eofb?  The inode is eligible if @eofb is null or
1146  * if the predicate functions match.
1147  */
1148 static bool
1149 xfs_inode_matches_eofb(
1150         struct xfs_inode        *ip,
1151         struct xfs_eofblocks    *eofb)
1152 {
1153         bool                    match;
1154
1155         if (!eofb)
1156                 return true;
1157
1158         if (eofb->eof_flags & XFS_EOF_FLAGS_UNION)
1159                 match = xfs_inode_match_id_union(ip, eofb);
1160         else
1161                 match = xfs_inode_match_id(ip, eofb);
1162         if (!match)
1163                 return false;
1164
1165         /* skip the inode if the file size is too small */
1166         if ((eofb->eof_flags & XFS_EOF_FLAGS_MINFILESIZE) &&
1167             XFS_ISIZE(ip) < eofb->eof_min_file_size)
1168                 return false;
1169
1170         return true;
1171 }
1172
1173 /*
1174  * This is a fast pass over the inode cache to try to get reclaim moving on as
1175  * many inodes as possible in a short period of time. It kicks itself every few
1176  * seconds, as well as being kicked by the inode cache shrinker when memory
1177  * goes low.
1178  */
1179 void
1180 xfs_reclaim_worker(
1181         struct work_struct *work)
1182 {
1183         struct xfs_mount *mp = container_of(to_delayed_work(work),
1184                                         struct xfs_mount, m_reclaim_work);
1185         int             nr_to_scan = INT_MAX;
1186
1187         xfs_reclaim_inodes_ag(mp, &nr_to_scan);
1188         xfs_reclaim_work_queue(mp);
1189 }
1190
1191 STATIC int
1192 xfs_inode_free_eofblocks(
1193         struct xfs_inode        *ip,
1194         void                    *args,
1195         unsigned int            *lockflags)
1196 {
1197         struct xfs_eofblocks    *eofb = args;
1198         bool                    wait;
1199
1200         wait = eofb && (eofb->eof_flags & XFS_EOF_FLAGS_SYNC);
1201
1202         if (!xfs_iflags_test(ip, XFS_IEOFBLOCKS))
1203                 return 0;
1204
1205         /*
1206          * If the mapping is dirty the operation can block and wait for some
1207          * time. Unless we are waiting, skip it.
1208          */
1209         if (!wait && mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_DIRTY))
1210                 return 0;
1211
1212         if (!xfs_inode_matches_eofb(ip, eofb))
1213                 return 0;
1214
1215         /*
1216          * If the caller is waiting, return -EAGAIN to keep the background
1217          * scanner moving and revisit the inode in a subsequent pass.
1218          */
1219         if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
1220                 if (wait)
1221                         return -EAGAIN;
1222                 return 0;
1223         }
1224         *lockflags |= XFS_IOLOCK_EXCL;
1225
1226         if (xfs_can_free_eofblocks(ip, false))
1227                 return xfs_free_eofblocks(ip);
1228
1229         /* inode could be preallocated or append-only */
1230         trace_xfs_inode_free_eofblocks_invalid(ip);
1231         xfs_inode_clear_eofblocks_tag(ip);
1232         return 0;
1233 }
1234
1235 /*
1236  * Background scanning to trim preallocated space. This is queued based on the
1237  * 'speculative_prealloc_lifetime' tunable (5m by default).
1238  */
1239 static inline void
1240 xfs_blockgc_queue(
1241         struct xfs_perag        *pag)
1242 {
1243         rcu_read_lock();
1244         if (radix_tree_tagged(&pag->pag_ici_root, XFS_ICI_BLOCKGC_TAG))
1245                 queue_delayed_work(pag->pag_mount->m_gc_workqueue,
1246                                    &pag->pag_blockgc_work,
1247                                    msecs_to_jiffies(xfs_blockgc_secs * 1000));
1248         rcu_read_unlock();
1249 }
1250
1251 static void
1252 xfs_blockgc_set_iflag(
1253         struct xfs_inode        *ip,
1254         unsigned long           iflag)
1255 {
1256         struct xfs_mount        *mp = ip->i_mount;
1257         struct xfs_perag        *pag;
1258         int                     tagged;
1259
1260         ASSERT((iflag & ~(XFS_IEOFBLOCKS | XFS_ICOWBLOCKS)) == 0);
1261
1262         /*
1263          * Don't bother locking the AG and looking up in the radix trees
1264          * if we already know that we have the tag set.
1265          */
1266         if (ip->i_flags & iflag)
1267                 return;
1268         spin_lock(&ip->i_flags_lock);
1269         ip->i_flags |= iflag;
1270         spin_unlock(&ip->i_flags_lock);
1271
1272         pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
1273         spin_lock(&pag->pag_ici_lock);
1274
1275         tagged = radix_tree_tagged(&pag->pag_ici_root, XFS_ICI_BLOCKGC_TAG);
1276         radix_tree_tag_set(&pag->pag_ici_root,
1277                            XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
1278                            XFS_ICI_BLOCKGC_TAG);
1279         if (!tagged) {
1280                 /* propagate the blockgc tag up into the perag radix tree */
1281                 spin_lock(&ip->i_mount->m_perag_lock);
1282                 radix_tree_tag_set(&ip->i_mount->m_perag_tree,
1283                                    XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
1284                                    XFS_ICI_BLOCKGC_TAG);
1285                 spin_unlock(&ip->i_mount->m_perag_lock);
1286
1287                 /* kick off background trimming */
1288                 xfs_blockgc_queue(pag);
1289
1290                 trace_xfs_perag_set_blockgc(ip->i_mount, pag->pag_agno, -1,
1291                                 _RET_IP_);
1292         }
1293
1294         spin_unlock(&pag->pag_ici_lock);
1295         xfs_perag_put(pag);
1296 }
1297
1298 void
1299 xfs_inode_set_eofblocks_tag(
1300         xfs_inode_t     *ip)
1301 {
1302         trace_xfs_inode_set_eofblocks_tag(ip);
1303         return xfs_blockgc_set_iflag(ip, XFS_IEOFBLOCKS);
1304 }
1305
1306 static void
1307 xfs_blockgc_clear_iflag(
1308         struct xfs_inode        *ip,
1309         unsigned long           iflag)
1310 {
1311         struct xfs_mount        *mp = ip->i_mount;
1312         struct xfs_perag        *pag;
1313         bool                    clear_tag;
1314
1315         ASSERT((iflag & ~(XFS_IEOFBLOCKS | XFS_ICOWBLOCKS)) == 0);
1316
1317         spin_lock(&ip->i_flags_lock);
1318         ip->i_flags &= ~iflag;
1319         clear_tag = (ip->i_flags & (XFS_IEOFBLOCKS | XFS_ICOWBLOCKS)) == 0;
1320         spin_unlock(&ip->i_flags_lock);
1321
1322         if (!clear_tag)
1323                 return;
1324
1325         pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
1326         spin_lock(&pag->pag_ici_lock);
1327
1328         radix_tree_tag_clear(&pag->pag_ici_root,
1329                              XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
1330                              XFS_ICI_BLOCKGC_TAG);
1331         if (!radix_tree_tagged(&pag->pag_ici_root, XFS_ICI_BLOCKGC_TAG)) {
1332                 /* clear the blockgc tag from the perag radix tree */
1333                 spin_lock(&ip->i_mount->m_perag_lock);
1334                 radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
1335                                      XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
1336                                      XFS_ICI_BLOCKGC_TAG);
1337                 spin_unlock(&ip->i_mount->m_perag_lock);
1338                 trace_xfs_perag_clear_blockgc(ip->i_mount, pag->pag_agno, -1,
1339                                 _RET_IP_);
1340         }
1341
1342         spin_unlock(&pag->pag_ici_lock);
1343         xfs_perag_put(pag);
1344 }
1345
1346 void
1347 xfs_inode_clear_eofblocks_tag(
1348         xfs_inode_t     *ip)
1349 {
1350         trace_xfs_inode_clear_eofblocks_tag(ip);
1351         return xfs_blockgc_clear_iflag(ip, XFS_IEOFBLOCKS);
1352 }
1353
1354 /*
1355  * Set ourselves up to free CoW blocks from this file.  If it's already clean
1356  * then we can bail out quickly, but otherwise we must back off if the file
1357  * is undergoing some kind of write.
1358  */
1359 static bool
1360 xfs_prep_free_cowblocks(
1361         struct xfs_inode        *ip)
1362 {
1363         /*
1364          * Just clear the tag if we have an empty cow fork or none at all. It's
1365          * possible the inode was fully unshared since it was originally tagged.
1366          */
1367         if (!xfs_inode_has_cow_data(ip)) {
1368                 trace_xfs_inode_free_cowblocks_invalid(ip);
1369                 xfs_inode_clear_cowblocks_tag(ip);
1370                 return false;
1371         }
1372
1373         /*
1374          * If the mapping is dirty or under writeback we cannot touch the
1375          * CoW fork.  Leave it alone if we're in the midst of a directio.
1376          */
1377         if ((VFS_I(ip)->i_state & I_DIRTY_PAGES) ||
1378             mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_DIRTY) ||
1379             mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_WRITEBACK) ||
1380             atomic_read(&VFS_I(ip)->i_dio_count))
1381                 return false;
1382
1383         return true;
1384 }
1385
1386 /*
1387  * Automatic CoW Reservation Freeing
1388  *
1389  * These functions automatically garbage collect leftover CoW reservations
1390  * that were made on behalf of a cowextsize hint when we start to run out
1391  * of quota or when the reservations sit around for too long.  If the file
1392  * has dirty pages or is undergoing writeback, its CoW reservations will
1393  * be retained.
1394  *
1395  * The actual garbage collection piggybacks off the same code that runs
1396  * the speculative EOF preallocation garbage collector.
1397  */
1398 STATIC int
1399 xfs_inode_free_cowblocks(
1400         struct xfs_inode        *ip,
1401         void                    *args,
1402         unsigned int            *lockflags)
1403 {
1404         struct xfs_eofblocks    *eofb = args;
1405         bool                    wait;
1406         int                     ret = 0;
1407
1408         wait = eofb && (eofb->eof_flags & XFS_EOF_FLAGS_SYNC);
1409
1410         if (!xfs_iflags_test(ip, XFS_ICOWBLOCKS))
1411                 return 0;
1412
1413         if (!xfs_prep_free_cowblocks(ip))
1414                 return 0;
1415
1416         if (!xfs_inode_matches_eofb(ip, eofb))
1417                 return 0;
1418
1419         /*
1420          * If the caller is waiting, return -EAGAIN to keep the background
1421          * scanner moving and revisit the inode in a subsequent pass.
1422          */
1423         if (!(*lockflags & XFS_IOLOCK_EXCL) &&
1424             !xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
1425                 if (wait)
1426                         return -EAGAIN;
1427                 return 0;
1428         }
1429         *lockflags |= XFS_IOLOCK_EXCL;
1430
1431         if (!xfs_ilock_nowait(ip, XFS_MMAPLOCK_EXCL)) {
1432                 if (wait)
1433                         return -EAGAIN;
1434                 return 0;
1435         }
1436         *lockflags |= XFS_MMAPLOCK_EXCL;
1437
1438         /*
1439          * Check again, nobody else should be able to dirty blocks or change
1440          * the reflink iflag now that we have the first two locks held.
1441          */
1442         if (xfs_prep_free_cowblocks(ip))
1443                 ret = xfs_reflink_cancel_cow_range(ip, 0, NULLFILEOFF, false);
1444         return ret;
1445 }
1446
1447 void
1448 xfs_inode_set_cowblocks_tag(
1449         xfs_inode_t     *ip)
1450 {
1451         trace_xfs_inode_set_cowblocks_tag(ip);
1452         return xfs_blockgc_set_iflag(ip, XFS_ICOWBLOCKS);
1453 }
1454
1455 void
1456 xfs_inode_clear_cowblocks_tag(
1457         xfs_inode_t     *ip)
1458 {
1459         trace_xfs_inode_clear_cowblocks_tag(ip);
1460         return xfs_blockgc_clear_iflag(ip, XFS_ICOWBLOCKS);
1461 }
1462
1463 #define for_each_perag_tag(mp, next_agno, pag, tag) \
1464         for ((next_agno) = 0, (pag) = xfs_perag_get_tag((mp), 0, (tag)); \
1465                 (pag) != NULL; \
1466                 (next_agno) = (pag)->pag_agno + 1, \
1467                 xfs_perag_put(pag), \
1468                 (pag) = xfs_perag_get_tag((mp), (next_agno), (tag)))
1469
1470
1471 /* Disable post-EOF and CoW block auto-reclamation. */
1472 void
1473 xfs_blockgc_stop(
1474         struct xfs_mount        *mp)
1475 {
1476         struct xfs_perag        *pag;
1477         xfs_agnumber_t          agno;
1478
1479         for_each_perag_tag(mp, agno, pag, XFS_ICI_BLOCKGC_TAG)
1480                 cancel_delayed_work_sync(&pag->pag_blockgc_work);
1481 }
1482
1483 /* Enable post-EOF and CoW block auto-reclamation. */
1484 void
1485 xfs_blockgc_start(
1486         struct xfs_mount        *mp)
1487 {
1488         struct xfs_perag        *pag;
1489         xfs_agnumber_t          agno;
1490
1491         for_each_perag_tag(mp, agno, pag, XFS_ICI_BLOCKGC_TAG)
1492                 xfs_blockgc_queue(pag);
1493 }
1494
1495 /*
1496  * Decide if the given @ip is eligible to be a part of the inode walk, and
1497  * grab it if so.  Returns true if it's ready to go or false if we should just
1498  * ignore it.
1499  */
1500 static bool
1501 xfs_inode_walk_ag_grab(
1502         struct xfs_inode        *ip,
1503         int                     flags)
1504 {
1505         struct inode            *inode = VFS_I(ip);
1506         bool                    newinos = !!(flags & XFS_INODE_WALK_INEW_WAIT);
1507
1508         ASSERT(rcu_read_lock_held());
1509
1510         /* Check for stale RCU freed inode */
1511         spin_lock(&ip->i_flags_lock);
1512         if (!ip->i_ino)
1513                 goto out_unlock_noent;
1514
1515         /* avoid new or reclaimable inodes. Leave for reclaim code to flush */
1516         if ((!newinos && __xfs_iflags_test(ip, XFS_INEW)) ||
1517             __xfs_iflags_test(ip, XFS_IRECLAIMABLE | XFS_IRECLAIM))
1518                 goto out_unlock_noent;
1519         spin_unlock(&ip->i_flags_lock);
1520
1521         /* nothing to sync during shutdown */
1522         if (XFS_FORCED_SHUTDOWN(ip->i_mount))
1523                 return false;
1524
1525         /* If we can't grab the inode, it must on it's way to reclaim. */
1526         if (!igrab(inode))
1527                 return false;
1528
1529         /* inode is valid */
1530         return true;
1531
1532 out_unlock_noent:
1533         spin_unlock(&ip->i_flags_lock);
1534         return false;
1535 }
1536
1537 /* Scan one incore inode for block preallocations that we can remove. */
1538 static int
1539 xfs_blockgc_scan_inode(
1540         struct xfs_inode        *ip,
1541         void                    *args)
1542 {
1543         unsigned int            lockflags = 0;
1544         int                     error;
1545
1546         error = xfs_inode_free_eofblocks(ip, args, &lockflags);
1547         if (error)
1548                 goto unlock;
1549
1550         error = xfs_inode_free_cowblocks(ip, args, &lockflags);
1551 unlock:
1552         if (lockflags)
1553                 xfs_iunlock(ip, lockflags);
1554         return error;
1555 }
1556
1557 /* Background worker that trims preallocated space. */
1558 void
1559 xfs_blockgc_worker(
1560         struct work_struct      *work)
1561 {
1562         struct xfs_perag        *pag = container_of(to_delayed_work(work),
1563                                         struct xfs_perag, pag_blockgc_work);
1564         struct xfs_mount        *mp = pag->pag_mount;
1565         int                     error;
1566
1567         if (!sb_start_write_trylock(mp->m_super))
1568                 return;
1569         error = xfs_icwalk_ag(pag, 0, xfs_blockgc_scan_inode, NULL,
1570                         XFS_ICWALK_BLOCKGC);
1571         if (error)
1572                 xfs_info(mp, "AG %u preallocation gc worker failed, err=%d",
1573                                 pag->pag_agno, error);
1574         sb_end_write(mp->m_super);
1575         xfs_blockgc_queue(pag);
1576 }
1577
1578 /*
1579  * Try to free space in the filesystem by purging eofblocks and cowblocks.
1580  */
1581 int
1582 xfs_blockgc_free_space(
1583         struct xfs_mount        *mp,
1584         struct xfs_eofblocks    *eofb)
1585 {
1586         trace_xfs_blockgc_free_space(mp, eofb, _RET_IP_);
1587
1588         return xfs_icwalk(mp, 0, xfs_blockgc_scan_inode, eofb,
1589                         XFS_ICWALK_BLOCKGC);
1590 }
1591
1592 /*
1593  * Run cow/eofblocks scans on the supplied dquots.  We don't know exactly which
1594  * quota caused an allocation failure, so we make a best effort by including
1595  * each quota under low free space conditions (less than 1% free space) in the
1596  * scan.
1597  *
1598  * Callers must not hold any inode's ILOCK.  If requesting a synchronous scan
1599  * (XFS_EOF_FLAGS_SYNC), the caller also must not hold any inode's IOLOCK or
1600  * MMAPLOCK.
1601  */
1602 int
1603 xfs_blockgc_free_dquots(
1604         struct xfs_mount        *mp,
1605         struct xfs_dquot        *udqp,
1606         struct xfs_dquot        *gdqp,
1607         struct xfs_dquot        *pdqp,
1608         unsigned int            eof_flags)
1609 {
1610         struct xfs_eofblocks    eofb = {0};
1611         bool                    do_work = false;
1612
1613         if (!udqp && !gdqp && !pdqp)
1614                 return 0;
1615
1616         /*
1617          * Run a scan to free blocks using the union filter to cover all
1618          * applicable quotas in a single scan.
1619          */
1620         eofb.eof_flags = XFS_EOF_FLAGS_UNION | eof_flags;
1621
1622         if (XFS_IS_UQUOTA_ENFORCED(mp) && udqp && xfs_dquot_lowsp(udqp)) {
1623                 eofb.eof_uid = make_kuid(mp->m_super->s_user_ns, udqp->q_id);
1624                 eofb.eof_flags |= XFS_EOF_FLAGS_UID;
1625                 do_work = true;
1626         }
1627
1628         if (XFS_IS_UQUOTA_ENFORCED(mp) && gdqp && xfs_dquot_lowsp(gdqp)) {
1629                 eofb.eof_gid = make_kgid(mp->m_super->s_user_ns, gdqp->q_id);
1630                 eofb.eof_flags |= XFS_EOF_FLAGS_GID;
1631                 do_work = true;
1632         }
1633
1634         if (XFS_IS_PQUOTA_ENFORCED(mp) && pdqp && xfs_dquot_lowsp(pdqp)) {
1635                 eofb.eof_prid = pdqp->q_id;
1636                 eofb.eof_flags |= XFS_EOF_FLAGS_PRID;
1637                 do_work = true;
1638         }
1639
1640         if (!do_work)
1641                 return 0;
1642
1643         return xfs_blockgc_free_space(mp, &eofb);
1644 }
1645
1646 /* Run cow/eofblocks scans on the quotas attached to the inode. */
1647 int
1648 xfs_blockgc_free_quota(
1649         struct xfs_inode        *ip,
1650         unsigned int            eof_flags)
1651 {
1652         return xfs_blockgc_free_dquots(ip->i_mount,
1653                         xfs_inode_dquot(ip, XFS_DQTYPE_USER),
1654                         xfs_inode_dquot(ip, XFS_DQTYPE_GROUP),
1655                         xfs_inode_dquot(ip, XFS_DQTYPE_PROJ), eof_flags);
1656 }
1657
1658 /* XFS Inode Cache Walking Code */
1659
1660 /*
1661  * For a given per-AG structure @pag, grab, @execute, and rele all incore
1662  * inodes with the given radix tree @tag.
1663  */
1664 static int
1665 xfs_icwalk_ag(
1666         struct xfs_perag        *pag,
1667         int                     iter_flags,
1668         int                     (*execute)(struct xfs_inode *ip, void *args),
1669         void                    *args,
1670         enum xfs_icwalk_goal    goal)
1671 {
1672         struct xfs_mount        *mp = pag->pag_mount;
1673         uint32_t                first_index;
1674         int                     last_error = 0;
1675         int                     skipped;
1676         bool                    done;
1677         int                     nr_found;
1678
1679 restart:
1680         done = false;
1681         skipped = 0;
1682         first_index = 0;
1683         nr_found = 0;
1684         do {
1685                 struct xfs_inode *batch[XFS_LOOKUP_BATCH];
1686                 unsigned int    tag = xfs_icwalk_tag(goal);
1687                 int             error = 0;
1688                 int             i;
1689
1690                 rcu_read_lock();
1691
1692                 if (tag == XFS_ICWALK_NULL_TAG)
1693                         nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
1694                                         (void **)batch, first_index,
1695                                         XFS_LOOKUP_BATCH);
1696                 else
1697                         nr_found = radix_tree_gang_lookup_tag(
1698                                         &pag->pag_ici_root,
1699                                         (void **) batch, first_index,
1700                                         XFS_LOOKUP_BATCH, tag);
1701
1702                 if (!nr_found) {
1703                         rcu_read_unlock();
1704                         break;
1705                 }
1706
1707                 /*
1708                  * Grab the inodes before we drop the lock. if we found
1709                  * nothing, nr == 0 and the loop will be skipped.
1710                  */
1711                 for (i = 0; i < nr_found; i++) {
1712                         struct xfs_inode *ip = batch[i];
1713
1714                         if (done || !xfs_inode_walk_ag_grab(ip, iter_flags))
1715                                 batch[i] = NULL;
1716
1717                         /*
1718                          * Update the index for the next lookup. Catch
1719                          * overflows into the next AG range which can occur if
1720                          * we have inodes in the last block of the AG and we
1721                          * are currently pointing to the last inode.
1722                          *
1723                          * Because we may see inodes that are from the wrong AG
1724                          * due to RCU freeing and reallocation, only update the
1725                          * index if it lies in this AG. It was a race that lead
1726                          * us to see this inode, so another lookup from the
1727                          * same index will not find it again.
1728                          */
1729                         if (XFS_INO_TO_AGNO(mp, ip->i_ino) != pag->pag_agno)
1730                                 continue;
1731                         first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
1732                         if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
1733                                 done = true;
1734                 }
1735
1736                 /* unlock now we've grabbed the inodes. */
1737                 rcu_read_unlock();
1738
1739                 for (i = 0; i < nr_found; i++) {
1740                         if (!batch[i])
1741                                 continue;
1742                         if ((iter_flags & XFS_INODE_WALK_INEW_WAIT) &&
1743                             xfs_iflags_test(batch[i], XFS_INEW))
1744                                 xfs_inew_wait(batch[i]);
1745                         error = execute(batch[i], args);
1746                         xfs_irele(batch[i]);
1747                         if (error == -EAGAIN) {
1748                                 skipped++;
1749                                 continue;
1750                         }
1751                         if (error && last_error != -EFSCORRUPTED)
1752                                 last_error = error;
1753                 }
1754
1755                 /* bail out if the filesystem is corrupted.  */
1756                 if (error == -EFSCORRUPTED)
1757                         break;
1758
1759                 cond_resched();
1760
1761         } while (nr_found && !done);
1762
1763         if (skipped) {
1764                 delay(1);
1765                 goto restart;
1766         }
1767         return last_error;
1768 }
1769
1770 /* Fetch the next (possibly tagged) per-AG structure. */
1771 static inline struct xfs_perag *
1772 xfs_icwalk_get_perag(
1773         struct xfs_mount        *mp,
1774         xfs_agnumber_t          agno,
1775         enum xfs_icwalk_goal    goal)
1776 {
1777         unsigned int            tag = xfs_icwalk_tag(goal);
1778
1779         if (tag == XFS_ICWALK_NULL_TAG)
1780                 return xfs_perag_get(mp, agno);
1781         return xfs_perag_get_tag(mp, agno, tag);
1782 }
1783
1784 /*
1785  * Call the @execute function on all incore inodes matching the radix tree
1786  * @tag.
1787  */
1788 static int
1789 xfs_icwalk(
1790         struct xfs_mount        *mp,
1791         int                     iter_flags,
1792         int                     (*execute)(struct xfs_inode *ip, void *args),
1793         void                    *args,
1794         enum xfs_icwalk_goal    goal)
1795 {
1796         struct xfs_perag        *pag;
1797         int                     error = 0;
1798         int                     last_error = 0;
1799         xfs_agnumber_t          agno = 0;
1800
1801         while ((pag = xfs_icwalk_get_perag(mp, agno, goal))) {
1802                 agno = pag->pag_agno + 1;
1803                 error = xfs_icwalk_ag(pag, iter_flags, execute, args, goal);
1804                 xfs_perag_put(pag);
1805                 if (error) {
1806                         last_error = error;
1807                         if (error == -EFSCORRUPTED)
1808                                 break;
1809                 }
1810         }
1811         return last_error;
1812         BUILD_BUG_ON(XFS_ICWALK_PRIVATE_FLAGS & XFS_EOF_FLAGS_VALID);
1813 }