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