xref: /linux/fs/xfs/xfs_icache.c (revision 1d39d02a1535658962f9370312be7b2d634946a5)
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_mount.h"
13 #include "xfs_inode.h"
14 #include "xfs_trans.h"
15 #include "xfs_trans_priv.h"
16 #include "xfs_inode_item.h"
17 #include "xfs_quota.h"
18 #include "xfs_trace.h"
19 #include "xfs_icache.h"
20 #include "xfs_bmap_util.h"
21 #include "xfs_dquot_item.h"
22 #include "xfs_dquot.h"
23 #include "xfs_reflink.h"
24 #include "xfs_ialloc.h"
25 #include "xfs_ag.h"
26 #include "xfs_log_priv.h"
27 #include "xfs_health.h"
28 
29 #include <linux/iversion.h>
30 
31 /* Radix tree tags for incore inode tree. */
32 
33 /* inode is to be reclaimed */
34 #define XFS_ICI_RECLAIM_TAG	0
35 /* Inode has speculative preallocations (posteof or cow) to clean. */
36 #define XFS_ICI_BLOCKGC_TAG	1
37 
38 /*
39  * The goal for walking incore inodes.  These can correspond with incore inode
40  * radix tree tags when convenient.  Avoid existing XFS_IWALK namespace.
41  */
42 enum xfs_icwalk_goal {
43 	/* Goals directly associated with tagged inodes. */
44 	XFS_ICWALK_BLOCKGC	= XFS_ICI_BLOCKGC_TAG,
45 	XFS_ICWALK_RECLAIM	= XFS_ICI_RECLAIM_TAG,
46 };
47 
48 static int xfs_icwalk(struct xfs_mount *mp,
49 		enum xfs_icwalk_goal goal, struct xfs_icwalk *icw);
50 static int xfs_icwalk_ag(struct xfs_perag *pag,
51 		enum xfs_icwalk_goal goal, struct xfs_icwalk *icw);
52 
53 /*
54  * Private inode cache walk flags for struct xfs_icwalk.  Must not
55  * coincide with XFS_ICWALK_FLAGS_VALID.
56  */
57 
58 /* Stop scanning after icw_scan_limit inodes. */
59 #define XFS_ICWALK_FLAG_SCAN_LIMIT	(1U << 28)
60 
61 #define XFS_ICWALK_FLAG_RECLAIM_SICK	(1U << 27)
62 #define XFS_ICWALK_FLAG_UNION		(1U << 26) /* union filter algorithm */
63 
64 #define XFS_ICWALK_PRIVATE_FLAGS	(XFS_ICWALK_FLAG_SCAN_LIMIT | \
65 					 XFS_ICWALK_FLAG_RECLAIM_SICK | \
66 					 XFS_ICWALK_FLAG_UNION)
67 
68 /* Marks for the perag xarray */
69 #define XFS_PERAG_RECLAIM_MARK	XA_MARK_0
70 #define XFS_PERAG_BLOCKGC_MARK	XA_MARK_1
71 
72 static inline xa_mark_t ici_tag_to_mark(unsigned int tag)
73 {
74 	if (tag == XFS_ICI_RECLAIM_TAG)
75 		return XFS_PERAG_RECLAIM_MARK;
76 	ASSERT(tag == XFS_ICI_BLOCKGC_TAG);
77 	return XFS_PERAG_BLOCKGC_MARK;
78 }
79 
80 /*
81  * Allocate and initialise an xfs_inode.
82  */
83 struct xfs_inode *
84 xfs_inode_alloc(
85 	struct xfs_mount	*mp,
86 	xfs_ino_t		ino)
87 {
88 	struct xfs_inode	*ip;
89 
90 	/*
91 	 * XXX: If this didn't occur in transactions, we could drop GFP_NOFAIL
92 	 * and return NULL here on ENOMEM.
93 	 */
94 	ip = alloc_inode_sb(mp->m_super, xfs_inode_cache, GFP_KERNEL | __GFP_NOFAIL);
95 
96 	if (inode_init_always(mp->m_super, VFS_I(ip))) {
97 		kmem_cache_free(xfs_inode_cache, ip);
98 		return NULL;
99 	}
100 
101 	/* VFS doesn't initialise i_mode! */
102 	VFS_I(ip)->i_mode = 0;
103 	mapping_set_folio_min_order(VFS_I(ip)->i_mapping,
104 				    M_IGEO(mp)->min_folio_order);
105 
106 	XFS_STATS_INC(mp, vn_active);
107 	ASSERT(atomic_read(&ip->i_pincount) == 0);
108 	ASSERT(ip->i_ino == 0);
109 
110 	/* initialise the xfs inode */
111 	ip->i_ino = ino;
112 	ip->i_mount = mp;
113 	memset(&ip->i_imap, 0, sizeof(struct xfs_imap));
114 	ip->i_cowfp = NULL;
115 	memset(&ip->i_af, 0, sizeof(ip->i_af));
116 	ip->i_af.if_format = XFS_DINODE_FMT_EXTENTS;
117 	memset(&ip->i_df, 0, sizeof(ip->i_df));
118 	ip->i_flags = 0;
119 	ip->i_delayed_blks = 0;
120 	ip->i_diflags2 = mp->m_ino_geo.new_diflags2;
121 	ip->i_nblocks = 0;
122 	ip->i_forkoff = 0;
123 	ip->i_sick = 0;
124 	ip->i_checked = 0;
125 	INIT_WORK(&ip->i_ioend_work, xfs_end_io);
126 	INIT_LIST_HEAD(&ip->i_ioend_list);
127 	spin_lock_init(&ip->i_ioend_lock);
128 	ip->i_next_unlinked = NULLAGINO;
129 	ip->i_prev_unlinked = 0;
130 
131 	return ip;
132 }
133 
134 STATIC void
135 xfs_inode_free_callback(
136 	struct rcu_head		*head)
137 {
138 	struct inode		*inode = container_of(head, struct inode, i_rcu);
139 	struct xfs_inode	*ip = XFS_I(inode);
140 
141 	switch (VFS_I(ip)->i_mode & S_IFMT) {
142 	case S_IFREG:
143 	case S_IFDIR:
144 	case S_IFLNK:
145 		xfs_idestroy_fork(&ip->i_df);
146 		break;
147 	}
148 
149 	xfs_ifork_zap_attr(ip);
150 
151 	if (ip->i_cowfp) {
152 		xfs_idestroy_fork(ip->i_cowfp);
153 		kmem_cache_free(xfs_ifork_cache, ip->i_cowfp);
154 	}
155 	if (ip->i_itemp) {
156 		ASSERT(!test_bit(XFS_LI_IN_AIL,
157 				 &ip->i_itemp->ili_item.li_flags));
158 		xfs_inode_item_destroy(ip);
159 		ip->i_itemp = NULL;
160 	}
161 
162 	kmem_cache_free(xfs_inode_cache, ip);
163 }
164 
165 static void
166 __xfs_inode_free(
167 	struct xfs_inode	*ip)
168 {
169 	/* asserts to verify all state is correct here */
170 	ASSERT(atomic_read(&ip->i_pincount) == 0);
171 	ASSERT(!ip->i_itemp || list_empty(&ip->i_itemp->ili_item.li_bio_list));
172 	XFS_STATS_DEC(ip->i_mount, vn_active);
173 
174 	call_rcu(&VFS_I(ip)->i_rcu, xfs_inode_free_callback);
175 }
176 
177 void
178 xfs_inode_free(
179 	struct xfs_inode	*ip)
180 {
181 	ASSERT(!xfs_iflags_test(ip, XFS_IFLUSHING));
182 
183 	/*
184 	 * Because we use RCU freeing we need to ensure the inode always
185 	 * appears to be reclaimed with an invalid inode number when in the
186 	 * free state. The ip->i_flags_lock provides the barrier against lookup
187 	 * races.
188 	 */
189 	spin_lock(&ip->i_flags_lock);
190 	ip->i_flags = XFS_IRECLAIM;
191 	ip->i_ino = 0;
192 	spin_unlock(&ip->i_flags_lock);
193 
194 	__xfs_inode_free(ip);
195 }
196 
197 /*
198  * Queue background inode reclaim work if there are reclaimable inodes and there
199  * isn't reclaim work already scheduled or in progress.
200  */
201 static void
202 xfs_reclaim_work_queue(
203 	struct xfs_mount        *mp)
204 {
205 
206 	rcu_read_lock();
207 	if (xa_marked(&mp->m_perags, XFS_PERAG_RECLAIM_MARK)) {
208 		queue_delayed_work(mp->m_reclaim_workqueue, &mp->m_reclaim_work,
209 			msecs_to_jiffies(xfs_syncd_centisecs / 6 * 10));
210 	}
211 	rcu_read_unlock();
212 }
213 
214 /*
215  * Background scanning to trim preallocated space. This is queued based on the
216  * 'speculative_prealloc_lifetime' tunable (5m by default).
217  */
218 static inline void
219 xfs_blockgc_queue(
220 	struct xfs_perag	*pag)
221 {
222 	struct xfs_mount	*mp = pag->pag_mount;
223 
224 	if (!xfs_is_blockgc_enabled(mp))
225 		return;
226 
227 	rcu_read_lock();
228 	if (radix_tree_tagged(&pag->pag_ici_root, XFS_ICI_BLOCKGC_TAG))
229 		queue_delayed_work(pag->pag_mount->m_blockgc_wq,
230 				   &pag->pag_blockgc_work,
231 				   msecs_to_jiffies(xfs_blockgc_secs * 1000));
232 	rcu_read_unlock();
233 }
234 
235 /* Set a tag on both the AG incore inode tree and the AG radix tree. */
236 static void
237 xfs_perag_set_inode_tag(
238 	struct xfs_perag	*pag,
239 	xfs_agino_t		agino,
240 	unsigned int		tag)
241 {
242 	struct xfs_mount	*mp = pag->pag_mount;
243 	bool			was_tagged;
244 
245 	lockdep_assert_held(&pag->pag_ici_lock);
246 
247 	was_tagged = radix_tree_tagged(&pag->pag_ici_root, tag);
248 	radix_tree_tag_set(&pag->pag_ici_root, agino, tag);
249 
250 	if (tag == XFS_ICI_RECLAIM_TAG)
251 		pag->pag_ici_reclaimable++;
252 
253 	if (was_tagged)
254 		return;
255 
256 	/* propagate the tag up into the perag radix tree */
257 	xa_set_mark(&mp->m_perags, pag->pag_agno, ici_tag_to_mark(tag));
258 
259 	/* start background work */
260 	switch (tag) {
261 	case XFS_ICI_RECLAIM_TAG:
262 		xfs_reclaim_work_queue(mp);
263 		break;
264 	case XFS_ICI_BLOCKGC_TAG:
265 		xfs_blockgc_queue(pag);
266 		break;
267 	}
268 
269 	trace_xfs_perag_set_inode_tag(pag, _RET_IP_);
270 }
271 
272 /* Clear a tag on both the AG incore inode tree and the AG radix tree. */
273 static void
274 xfs_perag_clear_inode_tag(
275 	struct xfs_perag	*pag,
276 	xfs_agino_t		agino,
277 	unsigned int		tag)
278 {
279 	struct xfs_mount	*mp = pag->pag_mount;
280 
281 	lockdep_assert_held(&pag->pag_ici_lock);
282 
283 	/*
284 	 * Reclaim can signal (with a null agino) that it cleared its own tag
285 	 * by removing the inode from the radix tree.
286 	 */
287 	if (agino != NULLAGINO)
288 		radix_tree_tag_clear(&pag->pag_ici_root, agino, tag);
289 	else
290 		ASSERT(tag == XFS_ICI_RECLAIM_TAG);
291 
292 	if (tag == XFS_ICI_RECLAIM_TAG)
293 		pag->pag_ici_reclaimable--;
294 
295 	if (radix_tree_tagged(&pag->pag_ici_root, tag))
296 		return;
297 
298 	/* clear the tag from the perag radix tree */
299 	xa_clear_mark(&mp->m_perags, pag->pag_agno, ici_tag_to_mark(tag));
300 
301 	trace_xfs_perag_clear_inode_tag(pag, _RET_IP_);
302 }
303 
304 /*
305  * Find the next AG after @pag, or the first AG if @pag is NULL.
306  */
307 static struct xfs_perag *
308 xfs_perag_grab_next_tag(
309 	struct xfs_mount	*mp,
310 	struct xfs_perag	*pag,
311 	int			tag)
312 {
313 	unsigned long		index = 0;
314 
315 	if (pag) {
316 		index = pag->pag_agno + 1;
317 		xfs_perag_rele(pag);
318 	}
319 
320 	rcu_read_lock();
321 	pag = xa_find(&mp->m_perags, &index, ULONG_MAX, ici_tag_to_mark(tag));
322 	if (pag) {
323 		trace_xfs_perag_grab_next_tag(pag, _RET_IP_);
324 		if (!atomic_inc_not_zero(&pag->pag_active_ref))
325 			pag = NULL;
326 	}
327 	rcu_read_unlock();
328 	return pag;
329 }
330 
331 /*
332  * When we recycle a reclaimable inode, we need to re-initialise the VFS inode
333  * part of the structure. This is made more complex by the fact we store
334  * information about the on-disk values in the VFS inode and so we can't just
335  * overwrite the values unconditionally. Hence we save the parameters we
336  * need to retain across reinitialisation, and rewrite them into the VFS inode
337  * after reinitialisation even if it fails.
338  */
339 static int
340 xfs_reinit_inode(
341 	struct xfs_mount	*mp,
342 	struct inode		*inode)
343 {
344 	int			error;
345 	uint32_t		nlink = inode->i_nlink;
346 	uint32_t		generation = inode->i_generation;
347 	uint64_t		version = inode_peek_iversion(inode);
348 	umode_t			mode = inode->i_mode;
349 	dev_t			dev = inode->i_rdev;
350 	kuid_t			uid = inode->i_uid;
351 	kgid_t			gid = inode->i_gid;
352 	unsigned long		state = inode->i_state;
353 
354 	error = inode_init_always(mp->m_super, inode);
355 
356 	set_nlink(inode, nlink);
357 	inode->i_generation = generation;
358 	inode_set_iversion_queried(inode, version);
359 	inode->i_mode = mode;
360 	inode->i_rdev = dev;
361 	inode->i_uid = uid;
362 	inode->i_gid = gid;
363 	inode->i_state = state;
364 	mapping_set_folio_min_order(inode->i_mapping,
365 				    M_IGEO(mp)->min_folio_order);
366 	return error;
367 }
368 
369 /*
370  * Carefully nudge an inode whose VFS state has been torn down back into a
371  * usable state.  Drops the i_flags_lock and the rcu read lock.
372  */
373 static int
374 xfs_iget_recycle(
375 	struct xfs_perag	*pag,
376 	struct xfs_inode	*ip) __releases(&ip->i_flags_lock)
377 {
378 	struct xfs_mount	*mp = ip->i_mount;
379 	struct inode		*inode = VFS_I(ip);
380 	int			error;
381 
382 	trace_xfs_iget_recycle(ip);
383 
384 	if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL))
385 		return -EAGAIN;
386 
387 	/*
388 	 * We need to make it look like the inode is being reclaimed to prevent
389 	 * the actual reclaim workers from stomping over us while we recycle
390 	 * the inode.  We can't clear the radix tree tag yet as it requires
391 	 * pag_ici_lock to be held exclusive.
392 	 */
393 	ip->i_flags |= XFS_IRECLAIM;
394 
395 	spin_unlock(&ip->i_flags_lock);
396 	rcu_read_unlock();
397 
398 	ASSERT(!rwsem_is_locked(&inode->i_rwsem));
399 	error = xfs_reinit_inode(mp, inode);
400 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
401 	if (error) {
402 		/*
403 		 * Re-initializing the inode failed, and we are in deep
404 		 * trouble.  Try to re-add it to the reclaim list.
405 		 */
406 		rcu_read_lock();
407 		spin_lock(&ip->i_flags_lock);
408 		ip->i_flags &= ~(XFS_INEW | XFS_IRECLAIM);
409 		ASSERT(ip->i_flags & XFS_IRECLAIMABLE);
410 		spin_unlock(&ip->i_flags_lock);
411 		rcu_read_unlock();
412 
413 		trace_xfs_iget_recycle_fail(ip);
414 		return error;
415 	}
416 
417 	spin_lock(&pag->pag_ici_lock);
418 	spin_lock(&ip->i_flags_lock);
419 
420 	/*
421 	 * Clear the per-lifetime state in the inode as we are now effectively
422 	 * a new inode and need to return to the initial state before reuse
423 	 * occurs.
424 	 */
425 	ip->i_flags &= ~XFS_IRECLAIM_RESET_FLAGS;
426 	ip->i_flags |= XFS_INEW;
427 	xfs_perag_clear_inode_tag(pag, XFS_INO_TO_AGINO(mp, ip->i_ino),
428 			XFS_ICI_RECLAIM_TAG);
429 	inode->i_state = I_NEW;
430 	spin_unlock(&ip->i_flags_lock);
431 	spin_unlock(&pag->pag_ici_lock);
432 
433 	return 0;
434 }
435 
436 /*
437  * If we are allocating a new inode, then check what was returned is
438  * actually a free, empty inode. If we are not allocating an inode,
439  * then check we didn't find a free inode.
440  *
441  * Returns:
442  *	0		if the inode free state matches the lookup context
443  *	-ENOENT		if the inode is free and we are not allocating
444  *	-EFSCORRUPTED	if there is any state mismatch at all
445  */
446 static int
447 xfs_iget_check_free_state(
448 	struct xfs_inode	*ip,
449 	int			flags)
450 {
451 	if (flags & XFS_IGET_CREATE) {
452 		/* should be a free inode */
453 		if (VFS_I(ip)->i_mode != 0) {
454 			xfs_warn(ip->i_mount,
455 "Corruption detected! Free inode 0x%llx not marked free! (mode 0x%x)",
456 				ip->i_ino, VFS_I(ip)->i_mode);
457 			xfs_agno_mark_sick(ip->i_mount,
458 					XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
459 					XFS_SICK_AG_INOBT);
460 			return -EFSCORRUPTED;
461 		}
462 
463 		if (ip->i_nblocks != 0) {
464 			xfs_warn(ip->i_mount,
465 "Corruption detected! Free inode 0x%llx has blocks allocated!",
466 				ip->i_ino);
467 			xfs_agno_mark_sick(ip->i_mount,
468 					XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
469 					XFS_SICK_AG_INOBT);
470 			return -EFSCORRUPTED;
471 		}
472 		return 0;
473 	}
474 
475 	/* should be an allocated inode */
476 	if (VFS_I(ip)->i_mode == 0)
477 		return -ENOENT;
478 
479 	return 0;
480 }
481 
482 /* Make all pending inactivation work start immediately. */
483 static bool
484 xfs_inodegc_queue_all(
485 	struct xfs_mount	*mp)
486 {
487 	struct xfs_inodegc	*gc;
488 	int			cpu;
489 	bool			ret = false;
490 
491 	for_each_cpu(cpu, &mp->m_inodegc_cpumask) {
492 		gc = per_cpu_ptr(mp->m_inodegc, cpu);
493 		if (!llist_empty(&gc->list)) {
494 			mod_delayed_work_on(cpu, mp->m_inodegc_wq, &gc->work, 0);
495 			ret = true;
496 		}
497 	}
498 
499 	return ret;
500 }
501 
502 /* Wait for all queued work and collect errors */
503 static int
504 xfs_inodegc_wait_all(
505 	struct xfs_mount	*mp)
506 {
507 	int			cpu;
508 	int			error = 0;
509 
510 	flush_workqueue(mp->m_inodegc_wq);
511 	for_each_cpu(cpu, &mp->m_inodegc_cpumask) {
512 		struct xfs_inodegc	*gc;
513 
514 		gc = per_cpu_ptr(mp->m_inodegc, cpu);
515 		if (gc->error && !error)
516 			error = gc->error;
517 		gc->error = 0;
518 	}
519 
520 	return error;
521 }
522 
523 /*
524  * Check the validity of the inode we just found it the cache
525  */
526 static int
527 xfs_iget_cache_hit(
528 	struct xfs_perag	*pag,
529 	struct xfs_inode	*ip,
530 	xfs_ino_t		ino,
531 	int			flags,
532 	int			lock_flags) __releases(RCU)
533 {
534 	struct inode		*inode = VFS_I(ip);
535 	struct xfs_mount	*mp = ip->i_mount;
536 	int			error;
537 
538 	/*
539 	 * check for re-use of an inode within an RCU grace period due to the
540 	 * radix tree nodes not being updated yet. We monitor for this by
541 	 * setting the inode number to zero before freeing the inode structure.
542 	 * If the inode has been reallocated and set up, then the inode number
543 	 * will not match, so check for that, too.
544 	 */
545 	spin_lock(&ip->i_flags_lock);
546 	if (ip->i_ino != ino)
547 		goto out_skip;
548 
549 	/*
550 	 * If we are racing with another cache hit that is currently
551 	 * instantiating this inode or currently recycling it out of
552 	 * reclaimable state, wait for the initialisation to complete
553 	 * before continuing.
554 	 *
555 	 * If we're racing with the inactivation worker we also want to wait.
556 	 * If we're creating a new file, it's possible that the worker
557 	 * previously marked the inode as free on disk but hasn't finished
558 	 * updating the incore state yet.  The AGI buffer will be dirty and
559 	 * locked to the icreate transaction, so a synchronous push of the
560 	 * inodegc workers would result in deadlock.  For a regular iget, the
561 	 * worker is running already, so we might as well wait.
562 	 *
563 	 * XXX(hch): eventually we should do something equivalent to
564 	 *	     wait_on_inode to wait for these flags to be cleared
565 	 *	     instead of polling for it.
566 	 */
567 	if (ip->i_flags & (XFS_INEW | XFS_IRECLAIM | XFS_INACTIVATING))
568 		goto out_skip;
569 
570 	if (ip->i_flags & XFS_NEED_INACTIVE) {
571 		/* Unlinked inodes cannot be re-grabbed. */
572 		if (VFS_I(ip)->i_nlink == 0) {
573 			error = -ENOENT;
574 			goto out_error;
575 		}
576 		goto out_inodegc_flush;
577 	}
578 
579 	/*
580 	 * Check the inode free state is valid. This also detects lookup
581 	 * racing with unlinks.
582 	 */
583 	error = xfs_iget_check_free_state(ip, flags);
584 	if (error)
585 		goto out_error;
586 
587 	/* Skip inodes that have no vfs state. */
588 	if ((flags & XFS_IGET_INCORE) &&
589 	    (ip->i_flags & XFS_IRECLAIMABLE))
590 		goto out_skip;
591 
592 	/* The inode fits the selection criteria; process it. */
593 	if (ip->i_flags & XFS_IRECLAIMABLE) {
594 		/* Drops i_flags_lock and RCU read lock. */
595 		error = xfs_iget_recycle(pag, ip);
596 		if (error == -EAGAIN)
597 			goto out_skip;
598 		if (error)
599 			return error;
600 	} else {
601 		/* If the VFS inode is being torn down, pause and try again. */
602 		if (!igrab(inode))
603 			goto out_skip;
604 
605 		/* We've got a live one. */
606 		spin_unlock(&ip->i_flags_lock);
607 		rcu_read_unlock();
608 		trace_xfs_iget_hit(ip);
609 	}
610 
611 	if (lock_flags != 0)
612 		xfs_ilock(ip, lock_flags);
613 
614 	if (!(flags & XFS_IGET_INCORE))
615 		xfs_iflags_clear(ip, XFS_ISTALE);
616 	XFS_STATS_INC(mp, xs_ig_found);
617 
618 	return 0;
619 
620 out_skip:
621 	trace_xfs_iget_skip(ip);
622 	XFS_STATS_INC(mp, xs_ig_frecycle);
623 	error = -EAGAIN;
624 out_error:
625 	spin_unlock(&ip->i_flags_lock);
626 	rcu_read_unlock();
627 	return error;
628 
629 out_inodegc_flush:
630 	spin_unlock(&ip->i_flags_lock);
631 	rcu_read_unlock();
632 	/*
633 	 * Do not wait for the workers, because the caller could hold an AGI
634 	 * buffer lock.  We're just going to sleep in a loop anyway.
635 	 */
636 	if (xfs_is_inodegc_enabled(mp))
637 		xfs_inodegc_queue_all(mp);
638 	return -EAGAIN;
639 }
640 
641 static int
642 xfs_iget_cache_miss(
643 	struct xfs_mount	*mp,
644 	struct xfs_perag	*pag,
645 	xfs_trans_t		*tp,
646 	xfs_ino_t		ino,
647 	struct xfs_inode	**ipp,
648 	int			flags,
649 	int			lock_flags)
650 {
651 	struct xfs_inode	*ip;
652 	int			error;
653 	xfs_agino_t		agino = XFS_INO_TO_AGINO(mp, ino);
654 
655 	ip = xfs_inode_alloc(mp, ino);
656 	if (!ip)
657 		return -ENOMEM;
658 
659 	error = xfs_imap(pag, tp, ip->i_ino, &ip->i_imap, flags);
660 	if (error)
661 		goto out_destroy;
662 
663 	/*
664 	 * For version 5 superblocks, if we are initialising a new inode and we
665 	 * are not utilising the XFS_FEAT_IKEEP inode cluster mode, we can
666 	 * simply build the new inode core with a random generation number.
667 	 *
668 	 * For version 4 (and older) superblocks, log recovery is dependent on
669 	 * the i_flushiter field being initialised from the current on-disk
670 	 * value and hence we must also read the inode off disk even when
671 	 * initializing new inodes.
672 	 */
673 	if (xfs_has_v3inodes(mp) &&
674 	    (flags & XFS_IGET_CREATE) && !xfs_has_ikeep(mp)) {
675 		VFS_I(ip)->i_generation = get_random_u32();
676 	} else {
677 		struct xfs_buf		*bp;
678 
679 		error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &bp);
680 		if (error)
681 			goto out_destroy;
682 
683 		error = xfs_inode_from_disk(ip,
684 				xfs_buf_offset(bp, ip->i_imap.im_boffset));
685 		if (!error)
686 			xfs_buf_set_ref(bp, XFS_INO_REF);
687 		else
688 			xfs_inode_mark_sick(ip, XFS_SICK_INO_CORE);
689 		xfs_trans_brelse(tp, bp);
690 
691 		if (error)
692 			goto out_destroy;
693 	}
694 
695 	trace_xfs_iget_miss(ip);
696 
697 	/*
698 	 * Check the inode free state is valid. This also detects lookup
699 	 * racing with unlinks.
700 	 */
701 	error = xfs_iget_check_free_state(ip, flags);
702 	if (error)
703 		goto out_destroy;
704 
705 	/*
706 	 * Preload the radix tree so we can insert safely under the
707 	 * write spinlock. Note that we cannot sleep inside the preload
708 	 * region.
709 	 */
710 	if (radix_tree_preload(GFP_KERNEL | __GFP_NOLOCKDEP)) {
711 		error = -EAGAIN;
712 		goto out_destroy;
713 	}
714 
715 	/*
716 	 * Because the inode hasn't been added to the radix-tree yet it can't
717 	 * be found by another thread, so we can do the non-sleeping lock here.
718 	 */
719 	if (lock_flags) {
720 		if (!xfs_ilock_nowait(ip, lock_flags))
721 			BUG();
722 	}
723 
724 	/*
725 	 * These values must be set before inserting the inode into the radix
726 	 * tree as the moment it is inserted a concurrent lookup (allowed by the
727 	 * RCU locking mechanism) can find it and that lookup must see that this
728 	 * is an inode currently under construction (i.e. that XFS_INEW is set).
729 	 * The ip->i_flags_lock that protects the XFS_INEW flag forms the
730 	 * memory barrier that ensures this detection works correctly at lookup
731 	 * time.
732 	 */
733 	if (flags & XFS_IGET_DONTCACHE)
734 		d_mark_dontcache(VFS_I(ip));
735 	ip->i_udquot = NULL;
736 	ip->i_gdquot = NULL;
737 	ip->i_pdquot = NULL;
738 	xfs_iflags_set(ip, XFS_INEW);
739 
740 	/* insert the new inode */
741 	spin_lock(&pag->pag_ici_lock);
742 	error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
743 	if (unlikely(error)) {
744 		WARN_ON(error != -EEXIST);
745 		XFS_STATS_INC(mp, xs_ig_dup);
746 		error = -EAGAIN;
747 		goto out_preload_end;
748 	}
749 	spin_unlock(&pag->pag_ici_lock);
750 	radix_tree_preload_end();
751 
752 	*ipp = ip;
753 	return 0;
754 
755 out_preload_end:
756 	spin_unlock(&pag->pag_ici_lock);
757 	radix_tree_preload_end();
758 	if (lock_flags)
759 		xfs_iunlock(ip, lock_flags);
760 out_destroy:
761 	__destroy_inode(VFS_I(ip));
762 	xfs_inode_free(ip);
763 	return error;
764 }
765 
766 /*
767  * Look up an inode by number in the given file system.  The inode is looked up
768  * in the cache held in each AG.  If the inode is found in the cache, initialise
769  * the vfs inode if necessary.
770  *
771  * If it is not in core, read it in from the file system's device, add it to the
772  * cache and initialise the vfs inode.
773  *
774  * The inode is locked according to the value of the lock_flags parameter.
775  * Inode lookup is only done during metadata operations and not as part of the
776  * data IO path. Hence we only allow locking of the XFS_ILOCK during lookup.
777  */
778 int
779 xfs_iget(
780 	struct xfs_mount	*mp,
781 	struct xfs_trans	*tp,
782 	xfs_ino_t		ino,
783 	uint			flags,
784 	uint			lock_flags,
785 	struct xfs_inode	**ipp)
786 {
787 	struct xfs_inode	*ip;
788 	struct xfs_perag	*pag;
789 	xfs_agino_t		agino;
790 	int			error;
791 
792 	ASSERT((lock_flags & (XFS_IOLOCK_EXCL | XFS_IOLOCK_SHARED)) == 0);
793 
794 	/* reject inode numbers outside existing AGs */
795 	if (!xfs_verify_ino(mp, ino))
796 		return -EINVAL;
797 
798 	XFS_STATS_INC(mp, xs_ig_attempts);
799 
800 	/* get the perag structure and ensure that it's inode capable */
801 	pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ino));
802 	agino = XFS_INO_TO_AGINO(mp, ino);
803 
804 again:
805 	error = 0;
806 	rcu_read_lock();
807 	ip = radix_tree_lookup(&pag->pag_ici_root, agino);
808 
809 	if (ip) {
810 		error = xfs_iget_cache_hit(pag, ip, ino, flags, lock_flags);
811 		if (error)
812 			goto out_error_or_again;
813 	} else {
814 		rcu_read_unlock();
815 		if (flags & XFS_IGET_INCORE) {
816 			error = -ENODATA;
817 			goto out_error_or_again;
818 		}
819 		XFS_STATS_INC(mp, xs_ig_missed);
820 
821 		error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip,
822 							flags, lock_flags);
823 		if (error)
824 			goto out_error_or_again;
825 	}
826 	xfs_perag_put(pag);
827 
828 	*ipp = ip;
829 
830 	/*
831 	 * If we have a real type for an on-disk inode, we can setup the inode
832 	 * now.	 If it's a new inode being created, xfs_init_new_inode will
833 	 * handle it.
834 	 */
835 	if (xfs_iflags_test(ip, XFS_INEW) && VFS_I(ip)->i_mode != 0)
836 		xfs_setup_existing_inode(ip);
837 	return 0;
838 
839 out_error_or_again:
840 	if (!(flags & (XFS_IGET_INCORE | XFS_IGET_NORETRY)) &&
841 	    error == -EAGAIN) {
842 		delay(1);
843 		goto again;
844 	}
845 	xfs_perag_put(pag);
846 	return error;
847 }
848 
849 /*
850  * Grab the inode for reclaim exclusively.
851  *
852  * We have found this inode via a lookup under RCU, so the inode may have
853  * already been freed, or it may be in the process of being recycled by
854  * xfs_iget(). In both cases, the inode will have XFS_IRECLAIM set. If the inode
855  * has been fully recycled by the time we get the i_flags_lock, XFS_IRECLAIMABLE
856  * will not be set. Hence we need to check for both these flag conditions to
857  * avoid inodes that are no longer reclaim candidates.
858  *
859  * Note: checking for other state flags here, under the i_flags_lock or not, is
860  * racy and should be avoided. Those races should be resolved only after we have
861  * ensured that we are able to reclaim this inode and the world can see that we
862  * are going to reclaim it.
863  *
864  * Return true if we grabbed it, false otherwise.
865  */
866 static bool
867 xfs_reclaim_igrab(
868 	struct xfs_inode	*ip,
869 	struct xfs_icwalk	*icw)
870 {
871 	ASSERT(rcu_read_lock_held());
872 
873 	spin_lock(&ip->i_flags_lock);
874 	if (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) ||
875 	    __xfs_iflags_test(ip, XFS_IRECLAIM)) {
876 		/* not a reclaim candidate. */
877 		spin_unlock(&ip->i_flags_lock);
878 		return false;
879 	}
880 
881 	/* Don't reclaim a sick inode unless the caller asked for it. */
882 	if (ip->i_sick &&
883 	    (!icw || !(icw->icw_flags & XFS_ICWALK_FLAG_RECLAIM_SICK))) {
884 		spin_unlock(&ip->i_flags_lock);
885 		return false;
886 	}
887 
888 	__xfs_iflags_set(ip, XFS_IRECLAIM);
889 	spin_unlock(&ip->i_flags_lock);
890 	return true;
891 }
892 
893 /*
894  * Inode reclaim is non-blocking, so the default action if progress cannot be
895  * made is to "requeue" the inode for reclaim by unlocking it and clearing the
896  * XFS_IRECLAIM flag.  If we are in a shutdown state, we don't care about
897  * blocking anymore and hence we can wait for the inode to be able to reclaim
898  * it.
899  *
900  * We do no IO here - if callers require inodes to be cleaned they must push the
901  * AIL first to trigger writeback of dirty inodes.  This enables writeback to be
902  * done in the background in a non-blocking manner, and enables memory reclaim
903  * to make progress without blocking.
904  */
905 static void
906 xfs_reclaim_inode(
907 	struct xfs_inode	*ip,
908 	struct xfs_perag	*pag)
909 {
910 	xfs_ino_t		ino = ip->i_ino; /* for radix_tree_delete */
911 
912 	if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL))
913 		goto out;
914 	if (xfs_iflags_test_and_set(ip, XFS_IFLUSHING))
915 		goto out_iunlock;
916 
917 	/*
918 	 * Check for log shutdown because aborting the inode can move the log
919 	 * tail and corrupt in memory state. This is fine if the log is shut
920 	 * down, but if the log is still active and only the mount is shut down
921 	 * then the in-memory log tail movement caused by the abort can be
922 	 * incorrectly propagated to disk.
923 	 */
924 	if (xlog_is_shutdown(ip->i_mount->m_log)) {
925 		xfs_iunpin_wait(ip);
926 		xfs_iflush_shutdown_abort(ip);
927 		goto reclaim;
928 	}
929 	if (xfs_ipincount(ip))
930 		goto out_clear_flush;
931 	if (!xfs_inode_clean(ip))
932 		goto out_clear_flush;
933 
934 	xfs_iflags_clear(ip, XFS_IFLUSHING);
935 reclaim:
936 	trace_xfs_inode_reclaiming(ip);
937 
938 	/*
939 	 * Because we use RCU freeing we need to ensure the inode always appears
940 	 * to be reclaimed with an invalid inode number when in the free state.
941 	 * We do this as early as possible under the ILOCK so that
942 	 * xfs_iflush_cluster() and xfs_ifree_cluster() can be guaranteed to
943 	 * detect races with us here. By doing this, we guarantee that once
944 	 * xfs_iflush_cluster() or xfs_ifree_cluster() has locked XFS_ILOCK that
945 	 * it will see either a valid inode that will serialise correctly, or it
946 	 * will see an invalid inode that it can skip.
947 	 */
948 	spin_lock(&ip->i_flags_lock);
949 	ip->i_flags = XFS_IRECLAIM;
950 	ip->i_ino = 0;
951 	ip->i_sick = 0;
952 	ip->i_checked = 0;
953 	spin_unlock(&ip->i_flags_lock);
954 
955 	ASSERT(!ip->i_itemp || ip->i_itemp->ili_item.li_buf == NULL);
956 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
957 
958 	XFS_STATS_INC(ip->i_mount, xs_ig_reclaims);
959 	/*
960 	 * Remove the inode from the per-AG radix tree.
961 	 *
962 	 * Because radix_tree_delete won't complain even if the item was never
963 	 * added to the tree assert that it's been there before to catch
964 	 * problems with the inode life time early on.
965 	 */
966 	spin_lock(&pag->pag_ici_lock);
967 	if (!radix_tree_delete(&pag->pag_ici_root,
968 				XFS_INO_TO_AGINO(ip->i_mount, ino)))
969 		ASSERT(0);
970 	xfs_perag_clear_inode_tag(pag, NULLAGINO, XFS_ICI_RECLAIM_TAG);
971 	spin_unlock(&pag->pag_ici_lock);
972 
973 	/*
974 	 * Here we do an (almost) spurious inode lock in order to coordinate
975 	 * with inode cache radix tree lookups.  This is because the lookup
976 	 * can reference the inodes in the cache without taking references.
977 	 *
978 	 * We make that OK here by ensuring that we wait until the inode is
979 	 * unlocked after the lookup before we go ahead and free it.
980 	 */
981 	xfs_ilock(ip, XFS_ILOCK_EXCL);
982 	ASSERT(!ip->i_udquot && !ip->i_gdquot && !ip->i_pdquot);
983 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
984 	ASSERT(xfs_inode_clean(ip));
985 
986 	__xfs_inode_free(ip);
987 	return;
988 
989 out_clear_flush:
990 	xfs_iflags_clear(ip, XFS_IFLUSHING);
991 out_iunlock:
992 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
993 out:
994 	xfs_iflags_clear(ip, XFS_IRECLAIM);
995 }
996 
997 /* Reclaim sick inodes if we're unmounting or the fs went down. */
998 static inline bool
999 xfs_want_reclaim_sick(
1000 	struct xfs_mount	*mp)
1001 {
1002 	return xfs_is_unmounting(mp) || xfs_has_norecovery(mp) ||
1003 	       xfs_is_shutdown(mp);
1004 }
1005 
1006 void
1007 xfs_reclaim_inodes(
1008 	struct xfs_mount	*mp)
1009 {
1010 	struct xfs_icwalk	icw = {
1011 		.icw_flags	= 0,
1012 	};
1013 
1014 	if (xfs_want_reclaim_sick(mp))
1015 		icw.icw_flags |= XFS_ICWALK_FLAG_RECLAIM_SICK;
1016 
1017 	while (xa_marked(&mp->m_perags, XFS_PERAG_RECLAIM_MARK)) {
1018 		xfs_ail_push_all_sync(mp->m_ail);
1019 		xfs_icwalk(mp, XFS_ICWALK_RECLAIM, &icw);
1020 	}
1021 }
1022 
1023 /*
1024  * The shrinker infrastructure determines how many inodes we should scan for
1025  * reclaim. We want as many clean inodes ready to reclaim as possible, so we
1026  * push the AIL here. We also want to proactively free up memory if we can to
1027  * minimise the amount of work memory reclaim has to do so we kick the
1028  * background reclaim if it isn't already scheduled.
1029  */
1030 long
1031 xfs_reclaim_inodes_nr(
1032 	struct xfs_mount	*mp,
1033 	unsigned long		nr_to_scan)
1034 {
1035 	struct xfs_icwalk	icw = {
1036 		.icw_flags	= XFS_ICWALK_FLAG_SCAN_LIMIT,
1037 		.icw_scan_limit	= min_t(unsigned long, LONG_MAX, nr_to_scan),
1038 	};
1039 
1040 	if (xfs_want_reclaim_sick(mp))
1041 		icw.icw_flags |= XFS_ICWALK_FLAG_RECLAIM_SICK;
1042 
1043 	/* kick background reclaimer and push the AIL */
1044 	xfs_reclaim_work_queue(mp);
1045 	xfs_ail_push_all(mp->m_ail);
1046 
1047 	xfs_icwalk(mp, XFS_ICWALK_RECLAIM, &icw);
1048 	return 0;
1049 }
1050 
1051 /*
1052  * Return the number of reclaimable inodes in the filesystem for
1053  * the shrinker to determine how much to reclaim.
1054  */
1055 long
1056 xfs_reclaim_inodes_count(
1057 	struct xfs_mount	*mp)
1058 {
1059 	XA_STATE		(xas, &mp->m_perags, 0);
1060 	long			reclaimable = 0;
1061 	struct xfs_perag	*pag;
1062 
1063 	rcu_read_lock();
1064 	xas_for_each_marked(&xas, pag, ULONG_MAX, XFS_PERAG_RECLAIM_MARK) {
1065 		trace_xfs_reclaim_inodes_count(pag, _THIS_IP_);
1066 		reclaimable += pag->pag_ici_reclaimable;
1067 	}
1068 	rcu_read_unlock();
1069 
1070 	return reclaimable;
1071 }
1072 
1073 STATIC bool
1074 xfs_icwalk_match_id(
1075 	struct xfs_inode	*ip,
1076 	struct xfs_icwalk	*icw)
1077 {
1078 	if ((icw->icw_flags & XFS_ICWALK_FLAG_UID) &&
1079 	    !uid_eq(VFS_I(ip)->i_uid, icw->icw_uid))
1080 		return false;
1081 
1082 	if ((icw->icw_flags & XFS_ICWALK_FLAG_GID) &&
1083 	    !gid_eq(VFS_I(ip)->i_gid, icw->icw_gid))
1084 		return false;
1085 
1086 	if ((icw->icw_flags & XFS_ICWALK_FLAG_PRID) &&
1087 	    ip->i_projid != icw->icw_prid)
1088 		return false;
1089 
1090 	return true;
1091 }
1092 
1093 /*
1094  * A union-based inode filtering algorithm. Process the inode if any of the
1095  * criteria match. This is for global/internal scans only.
1096  */
1097 STATIC bool
1098 xfs_icwalk_match_id_union(
1099 	struct xfs_inode	*ip,
1100 	struct xfs_icwalk	*icw)
1101 {
1102 	if ((icw->icw_flags & XFS_ICWALK_FLAG_UID) &&
1103 	    uid_eq(VFS_I(ip)->i_uid, icw->icw_uid))
1104 		return true;
1105 
1106 	if ((icw->icw_flags & XFS_ICWALK_FLAG_GID) &&
1107 	    gid_eq(VFS_I(ip)->i_gid, icw->icw_gid))
1108 		return true;
1109 
1110 	if ((icw->icw_flags & XFS_ICWALK_FLAG_PRID) &&
1111 	    ip->i_projid == icw->icw_prid)
1112 		return true;
1113 
1114 	return false;
1115 }
1116 
1117 /*
1118  * Is this inode @ip eligible for eof/cow block reclamation, given some
1119  * filtering parameters @icw?  The inode is eligible if @icw is null or
1120  * if the predicate functions match.
1121  */
1122 static bool
1123 xfs_icwalk_match(
1124 	struct xfs_inode	*ip,
1125 	struct xfs_icwalk	*icw)
1126 {
1127 	bool			match;
1128 
1129 	if (!icw)
1130 		return true;
1131 
1132 	if (icw->icw_flags & XFS_ICWALK_FLAG_UNION)
1133 		match = xfs_icwalk_match_id_union(ip, icw);
1134 	else
1135 		match = xfs_icwalk_match_id(ip, icw);
1136 	if (!match)
1137 		return false;
1138 
1139 	/* skip the inode if the file size is too small */
1140 	if ((icw->icw_flags & XFS_ICWALK_FLAG_MINFILESIZE) &&
1141 	    XFS_ISIZE(ip) < icw->icw_min_file_size)
1142 		return false;
1143 
1144 	return true;
1145 }
1146 
1147 /*
1148  * This is a fast pass over the inode cache to try to get reclaim moving on as
1149  * many inodes as possible in a short period of time. It kicks itself every few
1150  * seconds, as well as being kicked by the inode cache shrinker when memory
1151  * goes low.
1152  */
1153 void
1154 xfs_reclaim_worker(
1155 	struct work_struct *work)
1156 {
1157 	struct xfs_mount *mp = container_of(to_delayed_work(work),
1158 					struct xfs_mount, m_reclaim_work);
1159 
1160 	xfs_icwalk(mp, XFS_ICWALK_RECLAIM, NULL);
1161 	xfs_reclaim_work_queue(mp);
1162 }
1163 
1164 STATIC int
1165 xfs_inode_free_eofblocks(
1166 	struct xfs_inode	*ip,
1167 	struct xfs_icwalk	*icw,
1168 	unsigned int		*lockflags)
1169 {
1170 	bool			wait;
1171 
1172 	wait = icw && (icw->icw_flags & XFS_ICWALK_FLAG_SYNC);
1173 
1174 	if (!xfs_iflags_test(ip, XFS_IEOFBLOCKS))
1175 		return 0;
1176 
1177 	/*
1178 	 * If the mapping is dirty the operation can block and wait for some
1179 	 * time. Unless we are waiting, skip it.
1180 	 */
1181 	if (!wait && mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_DIRTY))
1182 		return 0;
1183 
1184 	if (!xfs_icwalk_match(ip, icw))
1185 		return 0;
1186 
1187 	/*
1188 	 * If the caller is waiting, return -EAGAIN to keep the background
1189 	 * scanner moving and revisit the inode in a subsequent pass.
1190 	 */
1191 	if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
1192 		if (wait)
1193 			return -EAGAIN;
1194 		return 0;
1195 	}
1196 	*lockflags |= XFS_IOLOCK_EXCL;
1197 
1198 	if (xfs_can_free_eofblocks(ip))
1199 		return xfs_free_eofblocks(ip);
1200 
1201 	/* inode could be preallocated */
1202 	trace_xfs_inode_free_eofblocks_invalid(ip);
1203 	xfs_inode_clear_eofblocks_tag(ip);
1204 	return 0;
1205 }
1206 
1207 static void
1208 xfs_blockgc_set_iflag(
1209 	struct xfs_inode	*ip,
1210 	unsigned long		iflag)
1211 {
1212 	struct xfs_mount	*mp = ip->i_mount;
1213 	struct xfs_perag	*pag;
1214 
1215 	ASSERT((iflag & ~(XFS_IEOFBLOCKS | XFS_ICOWBLOCKS)) == 0);
1216 
1217 	/*
1218 	 * Don't bother locking the AG and looking up in the radix trees
1219 	 * if we already know that we have the tag set.
1220 	 */
1221 	if (ip->i_flags & iflag)
1222 		return;
1223 	spin_lock(&ip->i_flags_lock);
1224 	ip->i_flags |= iflag;
1225 	spin_unlock(&ip->i_flags_lock);
1226 
1227 	pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
1228 	spin_lock(&pag->pag_ici_lock);
1229 
1230 	xfs_perag_set_inode_tag(pag, XFS_INO_TO_AGINO(mp, ip->i_ino),
1231 			XFS_ICI_BLOCKGC_TAG);
1232 
1233 	spin_unlock(&pag->pag_ici_lock);
1234 	xfs_perag_put(pag);
1235 }
1236 
1237 void
1238 xfs_inode_set_eofblocks_tag(
1239 	xfs_inode_t	*ip)
1240 {
1241 	trace_xfs_inode_set_eofblocks_tag(ip);
1242 	return xfs_blockgc_set_iflag(ip, XFS_IEOFBLOCKS);
1243 }
1244 
1245 static void
1246 xfs_blockgc_clear_iflag(
1247 	struct xfs_inode	*ip,
1248 	unsigned long		iflag)
1249 {
1250 	struct xfs_mount	*mp = ip->i_mount;
1251 	struct xfs_perag	*pag;
1252 	bool			clear_tag;
1253 
1254 	ASSERT((iflag & ~(XFS_IEOFBLOCKS | XFS_ICOWBLOCKS)) == 0);
1255 
1256 	spin_lock(&ip->i_flags_lock);
1257 	ip->i_flags &= ~iflag;
1258 	clear_tag = (ip->i_flags & (XFS_IEOFBLOCKS | XFS_ICOWBLOCKS)) == 0;
1259 	spin_unlock(&ip->i_flags_lock);
1260 
1261 	if (!clear_tag)
1262 		return;
1263 
1264 	pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
1265 	spin_lock(&pag->pag_ici_lock);
1266 
1267 	xfs_perag_clear_inode_tag(pag, XFS_INO_TO_AGINO(mp, ip->i_ino),
1268 			XFS_ICI_BLOCKGC_TAG);
1269 
1270 	spin_unlock(&pag->pag_ici_lock);
1271 	xfs_perag_put(pag);
1272 }
1273 
1274 void
1275 xfs_inode_clear_eofblocks_tag(
1276 	xfs_inode_t	*ip)
1277 {
1278 	trace_xfs_inode_clear_eofblocks_tag(ip);
1279 	return xfs_blockgc_clear_iflag(ip, XFS_IEOFBLOCKS);
1280 }
1281 
1282 /*
1283  * Set ourselves up to free CoW blocks from this file.  If it's already clean
1284  * then we can bail out quickly, but otherwise we must back off if the file
1285  * is undergoing some kind of write.
1286  */
1287 static bool
1288 xfs_prep_free_cowblocks(
1289 	struct xfs_inode	*ip)
1290 {
1291 	/*
1292 	 * Just clear the tag if we have an empty cow fork or none at all. It's
1293 	 * possible the inode was fully unshared since it was originally tagged.
1294 	 */
1295 	if (!xfs_inode_has_cow_data(ip)) {
1296 		trace_xfs_inode_free_cowblocks_invalid(ip);
1297 		xfs_inode_clear_cowblocks_tag(ip);
1298 		return false;
1299 	}
1300 
1301 	/*
1302 	 * If the mapping is dirty or under writeback we cannot touch the
1303 	 * CoW fork.  Leave it alone if we're in the midst of a directio.
1304 	 */
1305 	if ((VFS_I(ip)->i_state & I_DIRTY_PAGES) ||
1306 	    mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_DIRTY) ||
1307 	    mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_WRITEBACK) ||
1308 	    atomic_read(&VFS_I(ip)->i_dio_count))
1309 		return false;
1310 
1311 	return true;
1312 }
1313 
1314 /*
1315  * Automatic CoW Reservation Freeing
1316  *
1317  * These functions automatically garbage collect leftover CoW reservations
1318  * that were made on behalf of a cowextsize hint when we start to run out
1319  * of quota or when the reservations sit around for too long.  If the file
1320  * has dirty pages or is undergoing writeback, its CoW reservations will
1321  * be retained.
1322  *
1323  * The actual garbage collection piggybacks off the same code that runs
1324  * the speculative EOF preallocation garbage collector.
1325  */
1326 STATIC int
1327 xfs_inode_free_cowblocks(
1328 	struct xfs_inode	*ip,
1329 	struct xfs_icwalk	*icw,
1330 	unsigned int		*lockflags)
1331 {
1332 	bool			wait;
1333 	int			ret = 0;
1334 
1335 	wait = icw && (icw->icw_flags & XFS_ICWALK_FLAG_SYNC);
1336 
1337 	if (!xfs_iflags_test(ip, XFS_ICOWBLOCKS))
1338 		return 0;
1339 
1340 	if (!xfs_prep_free_cowblocks(ip))
1341 		return 0;
1342 
1343 	if (!xfs_icwalk_match(ip, icw))
1344 		return 0;
1345 
1346 	/*
1347 	 * If the caller is waiting, return -EAGAIN to keep the background
1348 	 * scanner moving and revisit the inode in a subsequent pass.
1349 	 */
1350 	if (!(*lockflags & XFS_IOLOCK_EXCL) &&
1351 	    !xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
1352 		if (wait)
1353 			return -EAGAIN;
1354 		return 0;
1355 	}
1356 	*lockflags |= XFS_IOLOCK_EXCL;
1357 
1358 	if (!xfs_ilock_nowait(ip, XFS_MMAPLOCK_EXCL)) {
1359 		if (wait)
1360 			return -EAGAIN;
1361 		return 0;
1362 	}
1363 	*lockflags |= XFS_MMAPLOCK_EXCL;
1364 
1365 	/*
1366 	 * Check again, nobody else should be able to dirty blocks or change
1367 	 * the reflink iflag now that we have the first two locks held.
1368 	 */
1369 	if (xfs_prep_free_cowblocks(ip))
1370 		ret = xfs_reflink_cancel_cow_range(ip, 0, NULLFILEOFF, false);
1371 	return ret;
1372 }
1373 
1374 void
1375 xfs_inode_set_cowblocks_tag(
1376 	xfs_inode_t	*ip)
1377 {
1378 	trace_xfs_inode_set_cowblocks_tag(ip);
1379 	return xfs_blockgc_set_iflag(ip, XFS_ICOWBLOCKS);
1380 }
1381 
1382 void
1383 xfs_inode_clear_cowblocks_tag(
1384 	xfs_inode_t	*ip)
1385 {
1386 	trace_xfs_inode_clear_cowblocks_tag(ip);
1387 	return xfs_blockgc_clear_iflag(ip, XFS_ICOWBLOCKS);
1388 }
1389 
1390 /* Disable post-EOF and CoW block auto-reclamation. */
1391 void
1392 xfs_blockgc_stop(
1393 	struct xfs_mount	*mp)
1394 {
1395 	struct xfs_perag	*pag;
1396 	xfs_agnumber_t		agno;
1397 
1398 	if (!xfs_clear_blockgc_enabled(mp))
1399 		return;
1400 
1401 	for_each_perag(mp, agno, pag)
1402 		cancel_delayed_work_sync(&pag->pag_blockgc_work);
1403 	trace_xfs_blockgc_stop(mp, __return_address);
1404 }
1405 
1406 /* Enable post-EOF and CoW block auto-reclamation. */
1407 void
1408 xfs_blockgc_start(
1409 	struct xfs_mount	*mp)
1410 {
1411 	struct xfs_perag	*pag = NULL;
1412 
1413 	if (xfs_set_blockgc_enabled(mp))
1414 		return;
1415 
1416 	trace_xfs_blockgc_start(mp, __return_address);
1417 	while ((pag = xfs_perag_grab_next_tag(mp, pag, XFS_ICI_BLOCKGC_TAG)))
1418 		xfs_blockgc_queue(pag);
1419 }
1420 
1421 /* Don't try to run block gc on an inode that's in any of these states. */
1422 #define XFS_BLOCKGC_NOGRAB_IFLAGS	(XFS_INEW | \
1423 					 XFS_NEED_INACTIVE | \
1424 					 XFS_INACTIVATING | \
1425 					 XFS_IRECLAIMABLE | \
1426 					 XFS_IRECLAIM)
1427 /*
1428  * Decide if the given @ip is eligible for garbage collection of speculative
1429  * preallocations, and grab it if so.  Returns true if it's ready to go or
1430  * false if we should just ignore it.
1431  */
1432 static bool
1433 xfs_blockgc_igrab(
1434 	struct xfs_inode	*ip)
1435 {
1436 	struct inode		*inode = VFS_I(ip);
1437 
1438 	ASSERT(rcu_read_lock_held());
1439 
1440 	/* Check for stale RCU freed inode */
1441 	spin_lock(&ip->i_flags_lock);
1442 	if (!ip->i_ino)
1443 		goto out_unlock_noent;
1444 
1445 	if (ip->i_flags & XFS_BLOCKGC_NOGRAB_IFLAGS)
1446 		goto out_unlock_noent;
1447 	spin_unlock(&ip->i_flags_lock);
1448 
1449 	/* nothing to sync during shutdown */
1450 	if (xfs_is_shutdown(ip->i_mount))
1451 		return false;
1452 
1453 	/* If we can't grab the inode, it must on it's way to reclaim. */
1454 	if (!igrab(inode))
1455 		return false;
1456 
1457 	/* inode is valid */
1458 	return true;
1459 
1460 out_unlock_noent:
1461 	spin_unlock(&ip->i_flags_lock);
1462 	return false;
1463 }
1464 
1465 /* Scan one incore inode for block preallocations that we can remove. */
1466 static int
1467 xfs_blockgc_scan_inode(
1468 	struct xfs_inode	*ip,
1469 	struct xfs_icwalk	*icw)
1470 {
1471 	unsigned int		lockflags = 0;
1472 	int			error;
1473 
1474 	error = xfs_inode_free_eofblocks(ip, icw, &lockflags);
1475 	if (error)
1476 		goto unlock;
1477 
1478 	error = xfs_inode_free_cowblocks(ip, icw, &lockflags);
1479 unlock:
1480 	if (lockflags)
1481 		xfs_iunlock(ip, lockflags);
1482 	xfs_irele(ip);
1483 	return error;
1484 }
1485 
1486 /* Background worker that trims preallocated space. */
1487 void
1488 xfs_blockgc_worker(
1489 	struct work_struct	*work)
1490 {
1491 	struct xfs_perag	*pag = container_of(to_delayed_work(work),
1492 					struct xfs_perag, pag_blockgc_work);
1493 	struct xfs_mount	*mp = pag->pag_mount;
1494 	int			error;
1495 
1496 	trace_xfs_blockgc_worker(mp, __return_address);
1497 
1498 	error = xfs_icwalk_ag(pag, XFS_ICWALK_BLOCKGC, NULL);
1499 	if (error)
1500 		xfs_info(mp, "AG %u preallocation gc worker failed, err=%d",
1501 				pag->pag_agno, error);
1502 	xfs_blockgc_queue(pag);
1503 }
1504 
1505 /*
1506  * Try to free space in the filesystem by purging inactive inodes, eofblocks
1507  * and cowblocks.
1508  */
1509 int
1510 xfs_blockgc_free_space(
1511 	struct xfs_mount	*mp,
1512 	struct xfs_icwalk	*icw)
1513 {
1514 	int			error;
1515 
1516 	trace_xfs_blockgc_free_space(mp, icw, _RET_IP_);
1517 
1518 	error = xfs_icwalk(mp, XFS_ICWALK_BLOCKGC, icw);
1519 	if (error)
1520 		return error;
1521 
1522 	return xfs_inodegc_flush(mp);
1523 }
1524 
1525 /*
1526  * Reclaim all the free space that we can by scheduling the background blockgc
1527  * and inodegc workers immediately and waiting for them all to clear.
1528  */
1529 int
1530 xfs_blockgc_flush_all(
1531 	struct xfs_mount	*mp)
1532 {
1533 	struct xfs_perag	*pag = NULL;
1534 
1535 	trace_xfs_blockgc_flush_all(mp, __return_address);
1536 
1537 	/*
1538 	 * For each blockgc worker, move its queue time up to now.  If it wasn't
1539 	 * queued, it will not be requeued.  Then flush whatever is left.
1540 	 */
1541 	while ((pag = xfs_perag_grab_next_tag(mp, pag, XFS_ICI_BLOCKGC_TAG)))
1542 		mod_delayed_work(pag->pag_mount->m_blockgc_wq,
1543 				&pag->pag_blockgc_work, 0);
1544 
1545 	while ((pag = xfs_perag_grab_next_tag(mp, pag, XFS_ICI_BLOCKGC_TAG)))
1546 		flush_delayed_work(&pag->pag_blockgc_work);
1547 
1548 	return xfs_inodegc_flush(mp);
1549 }
1550 
1551 /*
1552  * Run cow/eofblocks scans on the supplied dquots.  We don't know exactly which
1553  * quota caused an allocation failure, so we make a best effort by including
1554  * each quota under low free space conditions (less than 1% free space) in the
1555  * scan.
1556  *
1557  * Callers must not hold any inode's ILOCK.  If requesting a synchronous scan
1558  * (XFS_ICWALK_FLAG_SYNC), the caller also must not hold any inode's IOLOCK or
1559  * MMAPLOCK.
1560  */
1561 int
1562 xfs_blockgc_free_dquots(
1563 	struct xfs_mount	*mp,
1564 	struct xfs_dquot	*udqp,
1565 	struct xfs_dquot	*gdqp,
1566 	struct xfs_dquot	*pdqp,
1567 	unsigned int		iwalk_flags)
1568 {
1569 	struct xfs_icwalk	icw = {0};
1570 	bool			do_work = false;
1571 
1572 	if (!udqp && !gdqp && !pdqp)
1573 		return 0;
1574 
1575 	/*
1576 	 * Run a scan to free blocks using the union filter to cover all
1577 	 * applicable quotas in a single scan.
1578 	 */
1579 	icw.icw_flags = XFS_ICWALK_FLAG_UNION | iwalk_flags;
1580 
1581 	if (XFS_IS_UQUOTA_ENFORCED(mp) && udqp && xfs_dquot_lowsp(udqp)) {
1582 		icw.icw_uid = make_kuid(mp->m_super->s_user_ns, udqp->q_id);
1583 		icw.icw_flags |= XFS_ICWALK_FLAG_UID;
1584 		do_work = true;
1585 	}
1586 
1587 	if (XFS_IS_UQUOTA_ENFORCED(mp) && gdqp && xfs_dquot_lowsp(gdqp)) {
1588 		icw.icw_gid = make_kgid(mp->m_super->s_user_ns, gdqp->q_id);
1589 		icw.icw_flags |= XFS_ICWALK_FLAG_GID;
1590 		do_work = true;
1591 	}
1592 
1593 	if (XFS_IS_PQUOTA_ENFORCED(mp) && pdqp && xfs_dquot_lowsp(pdqp)) {
1594 		icw.icw_prid = pdqp->q_id;
1595 		icw.icw_flags |= XFS_ICWALK_FLAG_PRID;
1596 		do_work = true;
1597 	}
1598 
1599 	if (!do_work)
1600 		return 0;
1601 
1602 	return xfs_blockgc_free_space(mp, &icw);
1603 }
1604 
1605 /* Run cow/eofblocks scans on the quotas attached to the inode. */
1606 int
1607 xfs_blockgc_free_quota(
1608 	struct xfs_inode	*ip,
1609 	unsigned int		iwalk_flags)
1610 {
1611 	return xfs_blockgc_free_dquots(ip->i_mount,
1612 			xfs_inode_dquot(ip, XFS_DQTYPE_USER),
1613 			xfs_inode_dquot(ip, XFS_DQTYPE_GROUP),
1614 			xfs_inode_dquot(ip, XFS_DQTYPE_PROJ), iwalk_flags);
1615 }
1616 
1617 /* XFS Inode Cache Walking Code */
1618 
1619 /*
1620  * The inode lookup is done in batches to keep the amount of lock traffic and
1621  * radix tree lookups to a minimum. The batch size is a trade off between
1622  * lookup reduction and stack usage. This is in the reclaim path, so we can't
1623  * be too greedy.
1624  */
1625 #define XFS_LOOKUP_BATCH	32
1626 
1627 
1628 /*
1629  * Decide if we want to grab this inode in anticipation of doing work towards
1630  * the goal.
1631  */
1632 static inline bool
1633 xfs_icwalk_igrab(
1634 	enum xfs_icwalk_goal	goal,
1635 	struct xfs_inode	*ip,
1636 	struct xfs_icwalk	*icw)
1637 {
1638 	switch (goal) {
1639 	case XFS_ICWALK_BLOCKGC:
1640 		return xfs_blockgc_igrab(ip);
1641 	case XFS_ICWALK_RECLAIM:
1642 		return xfs_reclaim_igrab(ip, icw);
1643 	default:
1644 		return false;
1645 	}
1646 }
1647 
1648 /*
1649  * Process an inode.  Each processing function must handle any state changes
1650  * made by the icwalk igrab function.  Return -EAGAIN to skip an inode.
1651  */
1652 static inline int
1653 xfs_icwalk_process_inode(
1654 	enum xfs_icwalk_goal	goal,
1655 	struct xfs_inode	*ip,
1656 	struct xfs_perag	*pag,
1657 	struct xfs_icwalk	*icw)
1658 {
1659 	int			error = 0;
1660 
1661 	switch (goal) {
1662 	case XFS_ICWALK_BLOCKGC:
1663 		error = xfs_blockgc_scan_inode(ip, icw);
1664 		break;
1665 	case XFS_ICWALK_RECLAIM:
1666 		xfs_reclaim_inode(ip, pag);
1667 		break;
1668 	}
1669 	return error;
1670 }
1671 
1672 /*
1673  * For a given per-AG structure @pag and a goal, grab qualifying inodes and
1674  * process them in some manner.
1675  */
1676 static int
1677 xfs_icwalk_ag(
1678 	struct xfs_perag	*pag,
1679 	enum xfs_icwalk_goal	goal,
1680 	struct xfs_icwalk	*icw)
1681 {
1682 	struct xfs_mount	*mp = pag->pag_mount;
1683 	uint32_t		first_index;
1684 	int			last_error = 0;
1685 	int			skipped;
1686 	bool			done;
1687 	int			nr_found;
1688 
1689 restart:
1690 	done = false;
1691 	skipped = 0;
1692 	if (goal == XFS_ICWALK_RECLAIM)
1693 		first_index = READ_ONCE(pag->pag_ici_reclaim_cursor);
1694 	else
1695 		first_index = 0;
1696 	nr_found = 0;
1697 	do {
1698 		struct xfs_inode *batch[XFS_LOOKUP_BATCH];
1699 		int		error = 0;
1700 		int		i;
1701 
1702 		rcu_read_lock();
1703 
1704 		nr_found = radix_tree_gang_lookup_tag(&pag->pag_ici_root,
1705 				(void **) batch, first_index,
1706 				XFS_LOOKUP_BATCH, goal);
1707 		if (!nr_found) {
1708 			done = true;
1709 			rcu_read_unlock();
1710 			break;
1711 		}
1712 
1713 		/*
1714 		 * Grab the inodes before we drop the lock. if we found
1715 		 * nothing, nr == 0 and the loop will be skipped.
1716 		 */
1717 		for (i = 0; i < nr_found; i++) {
1718 			struct xfs_inode *ip = batch[i];
1719 
1720 			if (done || !xfs_icwalk_igrab(goal, ip, icw))
1721 				batch[i] = NULL;
1722 
1723 			/*
1724 			 * Update the index for the next lookup. Catch
1725 			 * overflows into the next AG range which can occur if
1726 			 * we have inodes in the last block of the AG and we
1727 			 * are currently pointing to the last inode.
1728 			 *
1729 			 * Because we may see inodes that are from the wrong AG
1730 			 * due to RCU freeing and reallocation, only update the
1731 			 * index if it lies in this AG. It was a race that lead
1732 			 * us to see this inode, so another lookup from the
1733 			 * same index will not find it again.
1734 			 */
1735 			if (XFS_INO_TO_AGNO(mp, ip->i_ino) != pag->pag_agno)
1736 				continue;
1737 			first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
1738 			if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
1739 				done = true;
1740 		}
1741 
1742 		/* unlock now we've grabbed the inodes. */
1743 		rcu_read_unlock();
1744 
1745 		for (i = 0; i < nr_found; i++) {
1746 			if (!batch[i])
1747 				continue;
1748 			error = xfs_icwalk_process_inode(goal, batch[i], pag,
1749 					icw);
1750 			if (error == -EAGAIN) {
1751 				skipped++;
1752 				continue;
1753 			}
1754 			if (error && last_error != -EFSCORRUPTED)
1755 				last_error = error;
1756 		}
1757 
1758 		/* bail out if the filesystem is corrupted.  */
1759 		if (error == -EFSCORRUPTED)
1760 			break;
1761 
1762 		cond_resched();
1763 
1764 		if (icw && (icw->icw_flags & XFS_ICWALK_FLAG_SCAN_LIMIT)) {
1765 			icw->icw_scan_limit -= XFS_LOOKUP_BATCH;
1766 			if (icw->icw_scan_limit <= 0)
1767 				break;
1768 		}
1769 	} while (nr_found && !done);
1770 
1771 	if (goal == XFS_ICWALK_RECLAIM) {
1772 		if (done)
1773 			first_index = 0;
1774 		WRITE_ONCE(pag->pag_ici_reclaim_cursor, first_index);
1775 	}
1776 
1777 	if (skipped) {
1778 		delay(1);
1779 		goto restart;
1780 	}
1781 	return last_error;
1782 }
1783 
1784 /* Walk all incore inodes to achieve a given goal. */
1785 static int
1786 xfs_icwalk(
1787 	struct xfs_mount	*mp,
1788 	enum xfs_icwalk_goal	goal,
1789 	struct xfs_icwalk	*icw)
1790 {
1791 	struct xfs_perag	*pag = NULL;
1792 	int			error = 0;
1793 	int			last_error = 0;
1794 
1795 	while ((pag = xfs_perag_grab_next_tag(mp, pag, goal))) {
1796 		error = xfs_icwalk_ag(pag, goal, icw);
1797 		if (error) {
1798 			last_error = error;
1799 			if (error == -EFSCORRUPTED) {
1800 				xfs_perag_rele(pag);
1801 				break;
1802 			}
1803 		}
1804 	}
1805 	return last_error;
1806 	BUILD_BUG_ON(XFS_ICWALK_PRIVATE_FLAGS & XFS_ICWALK_FLAGS_VALID);
1807 }
1808 
1809 #ifdef DEBUG
1810 static void
1811 xfs_check_delalloc(
1812 	struct xfs_inode	*ip,
1813 	int			whichfork)
1814 {
1815 	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, whichfork);
1816 	struct xfs_bmbt_irec	got;
1817 	struct xfs_iext_cursor	icur;
1818 
1819 	if (!ifp || !xfs_iext_lookup_extent(ip, ifp, 0, &icur, &got))
1820 		return;
1821 	do {
1822 		if (isnullstartblock(got.br_startblock)) {
1823 			xfs_warn(ip->i_mount,
1824 	"ino %llx %s fork has delalloc extent at [0x%llx:0x%llx]",
1825 				ip->i_ino,
1826 				whichfork == XFS_DATA_FORK ? "data" : "cow",
1827 				got.br_startoff, got.br_blockcount);
1828 		}
1829 	} while (xfs_iext_next_extent(ifp, &icur, &got));
1830 }
1831 #else
1832 #define xfs_check_delalloc(ip, whichfork)	do { } while (0)
1833 #endif
1834 
1835 /* Schedule the inode for reclaim. */
1836 static void
1837 xfs_inodegc_set_reclaimable(
1838 	struct xfs_inode	*ip)
1839 {
1840 	struct xfs_mount	*mp = ip->i_mount;
1841 	struct xfs_perag	*pag;
1842 
1843 	if (!xfs_is_shutdown(mp) && ip->i_delayed_blks) {
1844 		xfs_check_delalloc(ip, XFS_DATA_FORK);
1845 		xfs_check_delalloc(ip, XFS_COW_FORK);
1846 		ASSERT(0);
1847 	}
1848 
1849 	pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
1850 	spin_lock(&pag->pag_ici_lock);
1851 	spin_lock(&ip->i_flags_lock);
1852 
1853 	trace_xfs_inode_set_reclaimable(ip);
1854 	ip->i_flags &= ~(XFS_NEED_INACTIVE | XFS_INACTIVATING);
1855 	ip->i_flags |= XFS_IRECLAIMABLE;
1856 	xfs_perag_set_inode_tag(pag, XFS_INO_TO_AGINO(mp, ip->i_ino),
1857 			XFS_ICI_RECLAIM_TAG);
1858 
1859 	spin_unlock(&ip->i_flags_lock);
1860 	spin_unlock(&pag->pag_ici_lock);
1861 	xfs_perag_put(pag);
1862 }
1863 
1864 /*
1865  * Free all speculative preallocations and possibly even the inode itself.
1866  * This is the last chance to make changes to an otherwise unreferenced file
1867  * before incore reclamation happens.
1868  */
1869 static int
1870 xfs_inodegc_inactivate(
1871 	struct xfs_inode	*ip)
1872 {
1873 	int			error;
1874 
1875 	trace_xfs_inode_inactivating(ip);
1876 	error = xfs_inactive(ip);
1877 	xfs_inodegc_set_reclaimable(ip);
1878 	return error;
1879 
1880 }
1881 
1882 void
1883 xfs_inodegc_worker(
1884 	struct work_struct	*work)
1885 {
1886 	struct xfs_inodegc	*gc = container_of(to_delayed_work(work),
1887 						struct xfs_inodegc, work);
1888 	struct llist_node	*node = llist_del_all(&gc->list);
1889 	struct xfs_inode	*ip, *n;
1890 	struct xfs_mount	*mp = gc->mp;
1891 	unsigned int		nofs_flag;
1892 
1893 	/*
1894 	 * Clear the cpu mask bit and ensure that we have seen the latest
1895 	 * update of the gc structure associated with this CPU. This matches
1896 	 * with the release semantics used when setting the cpumask bit in
1897 	 * xfs_inodegc_queue.
1898 	 */
1899 	cpumask_clear_cpu(gc->cpu, &mp->m_inodegc_cpumask);
1900 	smp_mb__after_atomic();
1901 
1902 	WRITE_ONCE(gc->items, 0);
1903 
1904 	if (!node)
1905 		return;
1906 
1907 	/*
1908 	 * We can allocate memory here while doing writeback on behalf of
1909 	 * memory reclaim.  To avoid memory allocation deadlocks set the
1910 	 * task-wide nofs context for the following operations.
1911 	 */
1912 	nofs_flag = memalloc_nofs_save();
1913 
1914 	ip = llist_entry(node, struct xfs_inode, i_gclist);
1915 	trace_xfs_inodegc_worker(mp, READ_ONCE(gc->shrinker_hits));
1916 
1917 	WRITE_ONCE(gc->shrinker_hits, 0);
1918 	llist_for_each_entry_safe(ip, n, node, i_gclist) {
1919 		int	error;
1920 
1921 		xfs_iflags_set(ip, XFS_INACTIVATING);
1922 		error = xfs_inodegc_inactivate(ip);
1923 		if (error && !gc->error)
1924 			gc->error = error;
1925 	}
1926 
1927 	memalloc_nofs_restore(nofs_flag);
1928 }
1929 
1930 /*
1931  * Expedite all pending inodegc work to run immediately. This does not wait for
1932  * completion of the work.
1933  */
1934 void
1935 xfs_inodegc_push(
1936 	struct xfs_mount	*mp)
1937 {
1938 	if (!xfs_is_inodegc_enabled(mp))
1939 		return;
1940 	trace_xfs_inodegc_push(mp, __return_address);
1941 	xfs_inodegc_queue_all(mp);
1942 }
1943 
1944 /*
1945  * Force all currently queued inode inactivation work to run immediately and
1946  * wait for the work to finish.
1947  */
1948 int
1949 xfs_inodegc_flush(
1950 	struct xfs_mount	*mp)
1951 {
1952 	xfs_inodegc_push(mp);
1953 	trace_xfs_inodegc_flush(mp, __return_address);
1954 	return xfs_inodegc_wait_all(mp);
1955 }
1956 
1957 /*
1958  * Flush all the pending work and then disable the inode inactivation background
1959  * workers and wait for them to stop.  Caller must hold sb->s_umount to
1960  * coordinate changes in the inodegc_enabled state.
1961  */
1962 void
1963 xfs_inodegc_stop(
1964 	struct xfs_mount	*mp)
1965 {
1966 	bool			rerun;
1967 
1968 	if (!xfs_clear_inodegc_enabled(mp))
1969 		return;
1970 
1971 	/*
1972 	 * Drain all pending inodegc work, including inodes that could be
1973 	 * queued by racing xfs_inodegc_queue or xfs_inodegc_shrinker_scan
1974 	 * threads that sample the inodegc state just prior to us clearing it.
1975 	 * The inodegc flag state prevents new threads from queuing more
1976 	 * inodes, so we queue pending work items and flush the workqueue until
1977 	 * all inodegc lists are empty.  IOWs, we cannot use drain_workqueue
1978 	 * here because it does not allow other unserialized mechanisms to
1979 	 * reschedule inodegc work while this draining is in progress.
1980 	 */
1981 	xfs_inodegc_queue_all(mp);
1982 	do {
1983 		flush_workqueue(mp->m_inodegc_wq);
1984 		rerun = xfs_inodegc_queue_all(mp);
1985 	} while (rerun);
1986 
1987 	trace_xfs_inodegc_stop(mp, __return_address);
1988 }
1989 
1990 /*
1991  * Enable the inode inactivation background workers and schedule deferred inode
1992  * inactivation work if there is any.  Caller must hold sb->s_umount to
1993  * coordinate changes in the inodegc_enabled state.
1994  */
1995 void
1996 xfs_inodegc_start(
1997 	struct xfs_mount	*mp)
1998 {
1999 	if (xfs_set_inodegc_enabled(mp))
2000 		return;
2001 
2002 	trace_xfs_inodegc_start(mp, __return_address);
2003 	xfs_inodegc_queue_all(mp);
2004 }
2005 
2006 #ifdef CONFIG_XFS_RT
2007 static inline bool
2008 xfs_inodegc_want_queue_rt_file(
2009 	struct xfs_inode	*ip)
2010 {
2011 	struct xfs_mount	*mp = ip->i_mount;
2012 
2013 	if (!XFS_IS_REALTIME_INODE(ip))
2014 		return false;
2015 
2016 	if (__percpu_counter_compare(&mp->m_frextents,
2017 				mp->m_low_rtexts[XFS_LOWSP_5_PCNT],
2018 				XFS_FDBLOCKS_BATCH) < 0)
2019 		return true;
2020 
2021 	return false;
2022 }
2023 #else
2024 # define xfs_inodegc_want_queue_rt_file(ip)	(false)
2025 #endif /* CONFIG_XFS_RT */
2026 
2027 /*
2028  * Schedule the inactivation worker when:
2029  *
2030  *  - We've accumulated more than one inode cluster buffer's worth of inodes.
2031  *  - There is less than 5% free space left.
2032  *  - Any of the quotas for this inode are near an enforcement limit.
2033  */
2034 static inline bool
2035 xfs_inodegc_want_queue_work(
2036 	struct xfs_inode	*ip,
2037 	unsigned int		items)
2038 {
2039 	struct xfs_mount	*mp = ip->i_mount;
2040 
2041 	if (items > mp->m_ino_geo.inodes_per_cluster)
2042 		return true;
2043 
2044 	if (__percpu_counter_compare(&mp->m_fdblocks,
2045 				mp->m_low_space[XFS_LOWSP_5_PCNT],
2046 				XFS_FDBLOCKS_BATCH) < 0)
2047 		return true;
2048 
2049 	if (xfs_inodegc_want_queue_rt_file(ip))
2050 		return true;
2051 
2052 	if (xfs_inode_near_dquot_enforcement(ip, XFS_DQTYPE_USER))
2053 		return true;
2054 
2055 	if (xfs_inode_near_dquot_enforcement(ip, XFS_DQTYPE_GROUP))
2056 		return true;
2057 
2058 	if (xfs_inode_near_dquot_enforcement(ip, XFS_DQTYPE_PROJ))
2059 		return true;
2060 
2061 	return false;
2062 }
2063 
2064 /*
2065  * Upper bound on the number of inodes in each AG that can be queued for
2066  * inactivation at any given time, to avoid monopolizing the workqueue.
2067  */
2068 #define XFS_INODEGC_MAX_BACKLOG		(4 * XFS_INODES_PER_CHUNK)
2069 
2070 /*
2071  * Make the frontend wait for inactivations when:
2072  *
2073  *  - Memory shrinkers queued the inactivation worker and it hasn't finished.
2074  *  - The queue depth exceeds the maximum allowable percpu backlog.
2075  *
2076  * Note: If we are in a NOFS context here (e.g. current thread is running a
2077  * transaction) the we don't want to block here as inodegc progress may require
2078  * filesystem resources we hold to make progress and that could result in a
2079  * deadlock. Hence we skip out of here if we are in a scoped NOFS context.
2080  */
2081 static inline bool
2082 xfs_inodegc_want_flush_work(
2083 	struct xfs_inode	*ip,
2084 	unsigned int		items,
2085 	unsigned int		shrinker_hits)
2086 {
2087 	if (current->flags & PF_MEMALLOC_NOFS)
2088 		return false;
2089 
2090 	if (shrinker_hits > 0)
2091 		return true;
2092 
2093 	if (items > XFS_INODEGC_MAX_BACKLOG)
2094 		return true;
2095 
2096 	return false;
2097 }
2098 
2099 /*
2100  * Queue a background inactivation worker if there are inodes that need to be
2101  * inactivated and higher level xfs code hasn't disabled the background
2102  * workers.
2103  */
2104 static void
2105 xfs_inodegc_queue(
2106 	struct xfs_inode	*ip)
2107 {
2108 	struct xfs_mount	*mp = ip->i_mount;
2109 	struct xfs_inodegc	*gc;
2110 	int			items;
2111 	unsigned int		shrinker_hits;
2112 	unsigned int		cpu_nr;
2113 	unsigned long		queue_delay = 1;
2114 
2115 	trace_xfs_inode_set_need_inactive(ip);
2116 	spin_lock(&ip->i_flags_lock);
2117 	ip->i_flags |= XFS_NEED_INACTIVE;
2118 	spin_unlock(&ip->i_flags_lock);
2119 
2120 	cpu_nr = get_cpu();
2121 	gc = this_cpu_ptr(mp->m_inodegc);
2122 	llist_add(&ip->i_gclist, &gc->list);
2123 	items = READ_ONCE(gc->items);
2124 	WRITE_ONCE(gc->items, items + 1);
2125 	shrinker_hits = READ_ONCE(gc->shrinker_hits);
2126 
2127 	/*
2128 	 * Ensure the list add is always seen by anyone who finds the cpumask
2129 	 * bit set. This effectively gives the cpumask bit set operation
2130 	 * release ordering semantics.
2131 	 */
2132 	smp_mb__before_atomic();
2133 	if (!cpumask_test_cpu(cpu_nr, &mp->m_inodegc_cpumask))
2134 		cpumask_test_and_set_cpu(cpu_nr, &mp->m_inodegc_cpumask);
2135 
2136 	/*
2137 	 * We queue the work while holding the current CPU so that the work
2138 	 * is scheduled to run on this CPU.
2139 	 */
2140 	if (!xfs_is_inodegc_enabled(mp)) {
2141 		put_cpu();
2142 		return;
2143 	}
2144 
2145 	if (xfs_inodegc_want_queue_work(ip, items))
2146 		queue_delay = 0;
2147 
2148 	trace_xfs_inodegc_queue(mp, __return_address);
2149 	mod_delayed_work_on(current_cpu(), mp->m_inodegc_wq, &gc->work,
2150 			queue_delay);
2151 	put_cpu();
2152 
2153 	if (xfs_inodegc_want_flush_work(ip, items, shrinker_hits)) {
2154 		trace_xfs_inodegc_throttle(mp, __return_address);
2155 		flush_delayed_work(&gc->work);
2156 	}
2157 }
2158 
2159 /*
2160  * We set the inode flag atomically with the radix tree tag.  Once we get tag
2161  * lookups on the radix tree, this inode flag can go away.
2162  *
2163  * We always use background reclaim here because even if the inode is clean, it
2164  * still may be under IO and hence we have wait for IO completion to occur
2165  * before we can reclaim the inode. The background reclaim path handles this
2166  * more efficiently than we can here, so simply let background reclaim tear down
2167  * all inodes.
2168  */
2169 void
2170 xfs_inode_mark_reclaimable(
2171 	struct xfs_inode	*ip)
2172 {
2173 	struct xfs_mount	*mp = ip->i_mount;
2174 	bool			need_inactive;
2175 
2176 	XFS_STATS_INC(mp, vn_reclaim);
2177 
2178 	/*
2179 	 * We should never get here with any of the reclaim flags already set.
2180 	 */
2181 	ASSERT_ALWAYS(!xfs_iflags_test(ip, XFS_ALL_IRECLAIM_FLAGS));
2182 
2183 	need_inactive = xfs_inode_needs_inactive(ip);
2184 	if (need_inactive) {
2185 		xfs_inodegc_queue(ip);
2186 		return;
2187 	}
2188 
2189 	/* Going straight to reclaim, so drop the dquots. */
2190 	xfs_qm_dqdetach(ip);
2191 	xfs_inodegc_set_reclaimable(ip);
2192 }
2193 
2194 /*
2195  * Register a phony shrinker so that we can run background inodegc sooner when
2196  * there's memory pressure.  Inactivation does not itself free any memory but
2197  * it does make inodes reclaimable, which eventually frees memory.
2198  *
2199  * The count function, seek value, and batch value are crafted to trigger the
2200  * scan function during the second round of scanning.  Hopefully this means
2201  * that we reclaimed enough memory that initiating metadata transactions won't
2202  * make things worse.
2203  */
2204 #define XFS_INODEGC_SHRINKER_COUNT	(1UL << DEF_PRIORITY)
2205 #define XFS_INODEGC_SHRINKER_BATCH	((XFS_INODEGC_SHRINKER_COUNT / 2) + 1)
2206 
2207 static unsigned long
2208 xfs_inodegc_shrinker_count(
2209 	struct shrinker		*shrink,
2210 	struct shrink_control	*sc)
2211 {
2212 	struct xfs_mount	*mp = shrink->private_data;
2213 	struct xfs_inodegc	*gc;
2214 	int			cpu;
2215 
2216 	if (!xfs_is_inodegc_enabled(mp))
2217 		return 0;
2218 
2219 	for_each_cpu(cpu, &mp->m_inodegc_cpumask) {
2220 		gc = per_cpu_ptr(mp->m_inodegc, cpu);
2221 		if (!llist_empty(&gc->list))
2222 			return XFS_INODEGC_SHRINKER_COUNT;
2223 	}
2224 
2225 	return 0;
2226 }
2227 
2228 static unsigned long
2229 xfs_inodegc_shrinker_scan(
2230 	struct shrinker		*shrink,
2231 	struct shrink_control	*sc)
2232 {
2233 	struct xfs_mount	*mp = shrink->private_data;
2234 	struct xfs_inodegc	*gc;
2235 	int			cpu;
2236 	bool			no_items = true;
2237 
2238 	if (!xfs_is_inodegc_enabled(mp))
2239 		return SHRINK_STOP;
2240 
2241 	trace_xfs_inodegc_shrinker_scan(mp, sc, __return_address);
2242 
2243 	for_each_cpu(cpu, &mp->m_inodegc_cpumask) {
2244 		gc = per_cpu_ptr(mp->m_inodegc, cpu);
2245 		if (!llist_empty(&gc->list)) {
2246 			unsigned int	h = READ_ONCE(gc->shrinker_hits);
2247 
2248 			WRITE_ONCE(gc->shrinker_hits, h + 1);
2249 			mod_delayed_work_on(cpu, mp->m_inodegc_wq, &gc->work, 0);
2250 			no_items = false;
2251 		}
2252 	}
2253 
2254 	/*
2255 	 * If there are no inodes to inactivate, we don't want the shrinker
2256 	 * to think there's deferred work to call us back about.
2257 	 */
2258 	if (no_items)
2259 		return LONG_MAX;
2260 
2261 	return SHRINK_STOP;
2262 }
2263 
2264 /* Register a shrinker so we can accelerate inodegc and throttle queuing. */
2265 int
2266 xfs_inodegc_register_shrinker(
2267 	struct xfs_mount	*mp)
2268 {
2269 	mp->m_inodegc_shrinker = shrinker_alloc(SHRINKER_NONSLAB,
2270 						"xfs-inodegc:%s",
2271 						mp->m_super->s_id);
2272 	if (!mp->m_inodegc_shrinker)
2273 		return -ENOMEM;
2274 
2275 	mp->m_inodegc_shrinker->count_objects = xfs_inodegc_shrinker_count;
2276 	mp->m_inodegc_shrinker->scan_objects = xfs_inodegc_shrinker_scan;
2277 	mp->m_inodegc_shrinker->seeks = 0;
2278 	mp->m_inodegc_shrinker->batch = XFS_INODEGC_SHRINKER_BATCH;
2279 	mp->m_inodegc_shrinker->private_data = mp;
2280 
2281 	shrinker_register(mp->m_inodegc_shrinker);
2282 
2283 	return 0;
2284 }
2285