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