blob: 74304b6ce84bbf2b5df0b913b5148ef66e2717a2 [file] [log] [blame]
David Chinnerfe4fa4b2008-10-30 17:06:08 +11001/*
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18#include "xfs.h"
19#include "xfs_fs.h"
Dave Chinner6ca1c902013-08-12 20:49:26 +100020#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110021#include "xfs_log_format.h"
22#include "xfs_trans_resv.h"
David Chinnerfe4fa4b2008-10-30 17:06:08 +110023#include "xfs_sb.h"
David Chinnerfe4fa4b2008-10-30 17:06:08 +110024#include "xfs_mount.h"
David Chinnerfe4fa4b2008-10-30 17:06:08 +110025#include "xfs_inode.h"
David Chinnerfe4fa4b2008-10-30 17:06:08 +110026#include "xfs_error.h"
Dave Chinner239880e2013-10-23 10:50:10 +110027#include "xfs_trans.h"
28#include "xfs_trans_priv.h"
David Chinnerfe4fa4b2008-10-30 17:06:08 +110029#include "xfs_inode_item.h"
Christoph Hellwig7d095252009-06-08 15:33:32 +020030#include "xfs_quota.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000031#include "xfs_trace.h"
Dave Chinner6d8b79c2012-10-08 21:56:09 +110032#include "xfs_icache.h"
Dave Chinnerc24b5df2013-08-12 20:49:45 +100033#include "xfs_bmap_util.h"
Brian Fosterdc06f3982014-07-24 19:49:28 +100034#include "xfs_dquot_item.h"
35#include "xfs_dquot.h"
Darrick J. Wong83104d42016-10-03 09:11:46 -070036#include "xfs_reflink.h"
David Chinnerfe4fa4b2008-10-30 17:06:08 +110037
David Chinnera167b172008-10-30 17:06:18 +110038#include <linux/kthread.h>
39#include <linux/freezer.h>
40
Dave Chinner33479e02012-10-08 21:56:11 +110041/*
42 * Allocate and initialise an xfs_inode.
43 */
Dave Chinner638f44162013-08-30 10:23:45 +100044struct xfs_inode *
Dave Chinner33479e02012-10-08 21:56:11 +110045xfs_inode_alloc(
46 struct xfs_mount *mp,
47 xfs_ino_t ino)
48{
49 struct xfs_inode *ip;
50
51 /*
52 * if this didn't occur in transactions, we could use
53 * KM_MAYFAIL and return NULL here on ENOMEM. Set the
54 * code up to do this anyway.
55 */
56 ip = kmem_zone_alloc(xfs_inode_zone, KM_SLEEP);
57 if (!ip)
58 return NULL;
59 if (inode_init_always(mp->m_super, VFS_I(ip))) {
60 kmem_zone_free(xfs_inode_zone, ip);
61 return NULL;
62 }
63
Dave Chinnerc19b3b052016-02-09 16:54:58 +110064 /* VFS doesn't initialise i_mode! */
65 VFS_I(ip)->i_mode = 0;
66
Bill O'Donnellff6d6af2015-10-12 18:21:22 +110067 XFS_STATS_INC(mp, vn_active);
Dave Chinner33479e02012-10-08 21:56:11 +110068 ASSERT(atomic_read(&ip->i_pincount) == 0);
69 ASSERT(!spin_is_locked(&ip->i_flags_lock));
70 ASSERT(!xfs_isiflocked(ip));
71 ASSERT(ip->i_ino == 0);
72
73 mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino);
74
75 /* initialise the xfs inode */
76 ip->i_ino = ino;
77 ip->i_mount = mp;
78 memset(&ip->i_imap, 0, sizeof(struct xfs_imap));
79 ip->i_afp = NULL;
Darrick J. Wong3993bae2016-10-03 09:11:32 -070080 ip->i_cowfp = NULL;
81 ip->i_cnextents = 0;
82 ip->i_cformat = XFS_DINODE_FMT_EXTENTS;
Dave Chinner33479e02012-10-08 21:56:11 +110083 memset(&ip->i_df, 0, sizeof(xfs_ifork_t));
84 ip->i_flags = 0;
85 ip->i_delayed_blks = 0;
Dave Chinnerf8d55aa0522016-02-09 16:54:58 +110086 memset(&ip->i_d, 0, sizeof(ip->i_d));
Dave Chinner33479e02012-10-08 21:56:11 +110087
88 return ip;
89}
90
91STATIC void
92xfs_inode_free_callback(
93 struct rcu_head *head)
94{
95 struct inode *inode = container_of(head, struct inode, i_rcu);
96 struct xfs_inode *ip = XFS_I(inode);
97
Dave Chinnerc19b3b052016-02-09 16:54:58 +110098 switch (VFS_I(ip)->i_mode & S_IFMT) {
Dave Chinner33479e02012-10-08 21:56:11 +110099 case S_IFREG:
100 case S_IFDIR:
101 case S_IFLNK:
102 xfs_idestroy_fork(ip, XFS_DATA_FORK);
103 break;
104 }
105
106 if (ip->i_afp)
107 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
Darrick J. Wong3993bae2016-10-03 09:11:32 -0700108 if (ip->i_cowfp)
109 xfs_idestroy_fork(ip, XFS_COW_FORK);
Dave Chinner33479e02012-10-08 21:56:11 +1100110
111 if (ip->i_itemp) {
112 ASSERT(!(ip->i_itemp->ili_item.li_flags & XFS_LI_IN_AIL));
113 xfs_inode_item_destroy(ip);
114 ip->i_itemp = NULL;
115 }
116
Dave Chinner1f2dcfe2016-05-18 14:01:53 +1000117 kmem_zone_free(xfs_inode_zone, ip);
118}
119
Dave Chinner8a17d7d2016-05-18 14:09:12 +1000120static void
121__xfs_inode_free(
122 struct xfs_inode *ip)
123{
124 /* asserts to verify all state is correct here */
125 ASSERT(atomic_read(&ip->i_pincount) == 0);
Dave Chinner8a17d7d2016-05-18 14:09:12 +1000126 XFS_STATS_DEC(ip->i_mount, vn_active);
127
128 call_rcu(&VFS_I(ip)->i_rcu, xfs_inode_free_callback);
129}
130
Dave Chinner1f2dcfe2016-05-18 14:01:53 +1000131void
132xfs_inode_free(
133 struct xfs_inode *ip)
134{
Brian Fosterb49ef752017-01-09 16:38:38 +0100135 ASSERT(!xfs_isiflocked(ip));
136
Dave Chinner33479e02012-10-08 21:56:11 +1100137 /*
138 * Because we use RCU freeing we need to ensure the inode always
139 * appears to be reclaimed with an invalid inode number when in the
140 * free state. The ip->i_flags_lock provides the barrier against lookup
141 * races.
142 */
143 spin_lock(&ip->i_flags_lock);
144 ip->i_flags = XFS_IRECLAIM;
145 ip->i_ino = 0;
146 spin_unlock(&ip->i_flags_lock);
147
Dave Chinner8a17d7d2016-05-18 14:09:12 +1000148 __xfs_inode_free(ip);
Dave Chinner33479e02012-10-08 21:56:11 +1100149}
150
151/*
Dave Chinnerad438c42016-05-18 14:20:08 +1000152 * Queue a new inode reclaim pass if there are reclaimable inodes and there
153 * isn't a reclaim pass already in progress. By default it runs every 5s based
154 * on the xfs periodic sync default of 30s. Perhaps this should have it's own
155 * tunable, but that can be done if this method proves to be ineffective or too
156 * aggressive.
157 */
158static void
159xfs_reclaim_work_queue(
160 struct xfs_mount *mp)
161{
162
163 rcu_read_lock();
164 if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_RECLAIM_TAG)) {
165 queue_delayed_work(mp->m_reclaim_workqueue, &mp->m_reclaim_work,
166 msecs_to_jiffies(xfs_syncd_centisecs / 6 * 10));
167 }
168 rcu_read_unlock();
169}
170
171/*
172 * This is a fast pass over the inode cache to try to get reclaim moving on as
173 * many inodes as possible in a short period of time. It kicks itself every few
174 * seconds, as well as being kicked by the inode cache shrinker when memory
175 * goes low. It scans as quickly as possible avoiding locked inodes or those
176 * already being flushed, and once done schedules a future pass.
177 */
178void
179xfs_reclaim_worker(
180 struct work_struct *work)
181{
182 struct xfs_mount *mp = container_of(to_delayed_work(work),
183 struct xfs_mount, m_reclaim_work);
184
185 xfs_reclaim_inodes(mp, SYNC_TRYLOCK);
186 xfs_reclaim_work_queue(mp);
187}
188
189static void
190xfs_perag_set_reclaim_tag(
191 struct xfs_perag *pag)
192{
193 struct xfs_mount *mp = pag->pag_mount;
194
195 ASSERT(spin_is_locked(&pag->pag_ici_lock));
196 if (pag->pag_ici_reclaimable++)
197 return;
198
199 /* propagate the reclaim tag up into the perag radix tree */
200 spin_lock(&mp->m_perag_lock);
201 radix_tree_tag_set(&mp->m_perag_tree, pag->pag_agno,
202 XFS_ICI_RECLAIM_TAG);
203 spin_unlock(&mp->m_perag_lock);
204
205 /* schedule periodic background inode reclaim */
206 xfs_reclaim_work_queue(mp);
207
208 trace_xfs_perag_set_reclaim(mp, pag->pag_agno, -1, _RET_IP_);
209}
210
211static void
212xfs_perag_clear_reclaim_tag(
213 struct xfs_perag *pag)
214{
215 struct xfs_mount *mp = pag->pag_mount;
216
217 ASSERT(spin_is_locked(&pag->pag_ici_lock));
218 if (--pag->pag_ici_reclaimable)
219 return;
220
221 /* clear the reclaim tag from the perag radix tree */
222 spin_lock(&mp->m_perag_lock);
223 radix_tree_tag_clear(&mp->m_perag_tree, pag->pag_agno,
224 XFS_ICI_RECLAIM_TAG);
225 spin_unlock(&mp->m_perag_lock);
226 trace_xfs_perag_clear_reclaim(mp, pag->pag_agno, -1, _RET_IP_);
227}
228
229
230/*
231 * We set the inode flag atomically with the radix tree tag.
232 * Once we get tag lookups on the radix tree, this inode flag
233 * can go away.
234 */
235void
236xfs_inode_set_reclaim_tag(
237 struct xfs_inode *ip)
238{
239 struct xfs_mount *mp = ip->i_mount;
240 struct xfs_perag *pag;
241
242 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
243 spin_lock(&pag->pag_ici_lock);
244 spin_lock(&ip->i_flags_lock);
245
246 radix_tree_tag_set(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ip->i_ino),
247 XFS_ICI_RECLAIM_TAG);
248 xfs_perag_set_reclaim_tag(pag);
249 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
250
251 spin_unlock(&ip->i_flags_lock);
252 spin_unlock(&pag->pag_ici_lock);
253 xfs_perag_put(pag);
254}
255
256STATIC void
257xfs_inode_clear_reclaim_tag(
258 struct xfs_perag *pag,
259 xfs_ino_t ino)
260{
261 radix_tree_tag_clear(&pag->pag_ici_root,
262 XFS_INO_TO_AGINO(pag->pag_mount, ino),
263 XFS_ICI_RECLAIM_TAG);
264 xfs_perag_clear_reclaim_tag(pag);
265}
266
Brian Foster2ea882d2017-04-26 08:30:39 -0700267static void
268xfs_inew_wait(
269 struct xfs_inode *ip)
270{
271 wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_INEW_BIT);
272 DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_INEW_BIT);
273
274 do {
275 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
276 if (!xfs_iflags_test(ip, XFS_INEW))
277 break;
278 schedule();
279 } while (true);
280 finish_wait(wq, &wait.wait);
281}
282
Dave Chinnerad438c42016-05-18 14:20:08 +1000283/*
Dave Chinner50997472016-02-09 16:54:58 +1100284 * When we recycle a reclaimable inode, we need to re-initialise the VFS inode
285 * part of the structure. This is made more complex by the fact we store
286 * information about the on-disk values in the VFS inode and so we can't just
Dave Chinner83e06f22016-02-09 16:54:58 +1100287 * overwrite the values unconditionally. Hence we save the parameters we
Dave Chinner50997472016-02-09 16:54:58 +1100288 * need to retain across reinitialisation, and rewrite them into the VFS inode
Dave Chinner83e06f22016-02-09 16:54:58 +1100289 * after reinitialisation even if it fails.
Dave Chinner50997472016-02-09 16:54:58 +1100290 */
291static int
292xfs_reinit_inode(
293 struct xfs_mount *mp,
294 struct inode *inode)
295{
296 int error;
Dave Chinner54d7b5c2016-02-09 16:54:58 +1100297 uint32_t nlink = inode->i_nlink;
Dave Chinner9e9a2672016-02-09 16:54:58 +1100298 uint32_t generation = inode->i_generation;
Dave Chinner83e06f22016-02-09 16:54:58 +1100299 uint64_t version = inode->i_version;
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100300 umode_t mode = inode->i_mode;
Dave Chinner50997472016-02-09 16:54:58 +1100301
302 error = inode_init_always(mp->m_super, inode);
303
Dave Chinner54d7b5c2016-02-09 16:54:58 +1100304 set_nlink(inode, nlink);
Dave Chinner9e9a2672016-02-09 16:54:58 +1100305 inode->i_generation = generation;
Dave Chinner83e06f22016-02-09 16:54:58 +1100306 inode->i_version = version;
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100307 inode->i_mode = mode;
Dave Chinner50997472016-02-09 16:54:58 +1100308 return error;
309}
310
311/*
Dave Chinner33479e02012-10-08 21:56:11 +1100312 * Check the validity of the inode we just found it the cache
313 */
314static int
315xfs_iget_cache_hit(
316 struct xfs_perag *pag,
317 struct xfs_inode *ip,
318 xfs_ino_t ino,
319 int flags,
320 int lock_flags) __releases(RCU)
321{
322 struct inode *inode = VFS_I(ip);
323 struct xfs_mount *mp = ip->i_mount;
324 int error;
325
326 /*
327 * check for re-use of an inode within an RCU grace period due to the
328 * radix tree nodes not being updated yet. We monitor for this by
329 * setting the inode number to zero before freeing the inode structure.
330 * If the inode has been reallocated and set up, then the inode number
331 * will not match, so check for that, too.
332 */
333 spin_lock(&ip->i_flags_lock);
334 if (ip->i_ino != ino) {
335 trace_xfs_iget_skip(ip);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100336 XFS_STATS_INC(mp, xs_ig_frecycle);
Dave Chinner24513372014-06-25 14:58:08 +1000337 error = -EAGAIN;
Dave Chinner33479e02012-10-08 21:56:11 +1100338 goto out_error;
339 }
340
341
342 /*
343 * If we are racing with another cache hit that is currently
344 * instantiating this inode or currently recycling it out of
345 * reclaimabe state, wait for the initialisation to complete
346 * before continuing.
347 *
348 * XXX(hch): eventually we should do something equivalent to
349 * wait_on_inode to wait for these flags to be cleared
350 * instead of polling for it.
351 */
352 if (ip->i_flags & (XFS_INEW|XFS_IRECLAIM)) {
353 trace_xfs_iget_skip(ip);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100354 XFS_STATS_INC(mp, xs_ig_frecycle);
Dave Chinner24513372014-06-25 14:58:08 +1000355 error = -EAGAIN;
Dave Chinner33479e02012-10-08 21:56:11 +1100356 goto out_error;
357 }
358
359 /*
360 * If lookup is racing with unlink return an error immediately.
361 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100362 if (VFS_I(ip)->i_mode == 0 && !(flags & XFS_IGET_CREATE)) {
Dave Chinner24513372014-06-25 14:58:08 +1000363 error = -ENOENT;
Dave Chinner33479e02012-10-08 21:56:11 +1100364 goto out_error;
365 }
366
367 /*
368 * If IRECLAIMABLE is set, we've torn down the VFS inode already.
369 * Need to carefully get it back into useable state.
370 */
371 if (ip->i_flags & XFS_IRECLAIMABLE) {
372 trace_xfs_iget_reclaim(ip);
373
374 /*
375 * We need to set XFS_IRECLAIM to prevent xfs_reclaim_inode
376 * from stomping over us while we recycle the inode. We can't
377 * clear the radix tree reclaimable tag yet as it requires
378 * pag_ici_lock to be held exclusive.
379 */
380 ip->i_flags |= XFS_IRECLAIM;
381
382 spin_unlock(&ip->i_flags_lock);
383 rcu_read_unlock();
384
Dave Chinner50997472016-02-09 16:54:58 +1100385 error = xfs_reinit_inode(mp, inode);
Dave Chinner33479e02012-10-08 21:56:11 +1100386 if (error) {
Brian Fostere86b6162017-04-26 08:30:39 -0700387 bool wake;
Dave Chinner33479e02012-10-08 21:56:11 +1100388 /*
389 * Re-initializing the inode failed, and we are in deep
390 * trouble. Try to re-add it to the reclaim list.
391 */
392 rcu_read_lock();
393 spin_lock(&ip->i_flags_lock);
Brian Fostere86b6162017-04-26 08:30:39 -0700394 wake = !!__xfs_iflags_test(ip, XFS_INEW);
Dave Chinner33479e02012-10-08 21:56:11 +1100395 ip->i_flags &= ~(XFS_INEW | XFS_IRECLAIM);
Brian Fostere86b6162017-04-26 08:30:39 -0700396 if (wake)
397 wake_up_bit(&ip->i_flags, __XFS_INEW_BIT);
Dave Chinner33479e02012-10-08 21:56:11 +1100398 ASSERT(ip->i_flags & XFS_IRECLAIMABLE);
399 trace_xfs_iget_reclaim_fail(ip);
400 goto out_error;
401 }
402
403 spin_lock(&pag->pag_ici_lock);
404 spin_lock(&ip->i_flags_lock);
405
406 /*
407 * Clear the per-lifetime state in the inode as we are now
408 * effectively a new inode and need to return to the initial
409 * state before reuse occurs.
410 */
411 ip->i_flags &= ~XFS_IRECLAIM_RESET_FLAGS;
412 ip->i_flags |= XFS_INEW;
Dave Chinner545c0882016-05-18 14:11:41 +1000413 xfs_inode_clear_reclaim_tag(pag, ip->i_ino);
Dave Chinner33479e02012-10-08 21:56:11 +1100414 inode->i_state = I_NEW;
415
416 ASSERT(!rwsem_is_locked(&ip->i_iolock.mr_lock));
417 mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino);
418
419 spin_unlock(&ip->i_flags_lock);
420 spin_unlock(&pag->pag_ici_lock);
421 } else {
422 /* If the VFS inode is being torn down, pause and try again. */
423 if (!igrab(inode)) {
424 trace_xfs_iget_skip(ip);
Dave Chinner24513372014-06-25 14:58:08 +1000425 error = -EAGAIN;
Dave Chinner33479e02012-10-08 21:56:11 +1100426 goto out_error;
427 }
428
429 /* We've got a live one. */
430 spin_unlock(&ip->i_flags_lock);
431 rcu_read_unlock();
432 trace_xfs_iget_hit(ip);
433 }
434
435 if (lock_flags != 0)
436 xfs_ilock(ip, lock_flags);
437
438 xfs_iflags_clear(ip, XFS_ISTALE | XFS_IDONTCACHE);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100439 XFS_STATS_INC(mp, xs_ig_found);
Dave Chinner33479e02012-10-08 21:56:11 +1100440
441 return 0;
442
443out_error:
444 spin_unlock(&ip->i_flags_lock);
445 rcu_read_unlock();
446 return error;
447}
448
449
450static int
451xfs_iget_cache_miss(
452 struct xfs_mount *mp,
453 struct xfs_perag *pag,
454 xfs_trans_t *tp,
455 xfs_ino_t ino,
456 struct xfs_inode **ipp,
457 int flags,
458 int lock_flags)
459{
460 struct xfs_inode *ip;
461 int error;
462 xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino);
463 int iflags;
464
465 ip = xfs_inode_alloc(mp, ino);
466 if (!ip)
Dave Chinner24513372014-06-25 14:58:08 +1000467 return -ENOMEM;
Dave Chinner33479e02012-10-08 21:56:11 +1100468
469 error = xfs_iread(mp, tp, ip, flags);
470 if (error)
471 goto out_destroy;
472
473 trace_xfs_iget_miss(ip);
474
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100475 if ((VFS_I(ip)->i_mode == 0) && !(flags & XFS_IGET_CREATE)) {
Dave Chinner24513372014-06-25 14:58:08 +1000476 error = -ENOENT;
Dave Chinner33479e02012-10-08 21:56:11 +1100477 goto out_destroy;
478 }
479
480 /*
481 * Preload the radix tree so we can insert safely under the
482 * write spinlock. Note that we cannot sleep inside the preload
483 * region. Since we can be called from transaction context, don't
484 * recurse into the file system.
485 */
486 if (radix_tree_preload(GFP_NOFS)) {
Dave Chinner24513372014-06-25 14:58:08 +1000487 error = -EAGAIN;
Dave Chinner33479e02012-10-08 21:56:11 +1100488 goto out_destroy;
489 }
490
491 /*
492 * Because the inode hasn't been added to the radix-tree yet it can't
493 * be found by another thread, so we can do the non-sleeping lock here.
494 */
495 if (lock_flags) {
496 if (!xfs_ilock_nowait(ip, lock_flags))
497 BUG();
498 }
499
500 /*
501 * These values must be set before inserting the inode into the radix
502 * tree as the moment it is inserted a concurrent lookup (allowed by the
503 * RCU locking mechanism) can find it and that lookup must see that this
504 * is an inode currently under construction (i.e. that XFS_INEW is set).
505 * The ip->i_flags_lock that protects the XFS_INEW flag forms the
506 * memory barrier that ensures this detection works correctly at lookup
507 * time.
508 */
509 iflags = XFS_INEW;
510 if (flags & XFS_IGET_DONTCACHE)
511 iflags |= XFS_IDONTCACHE;
Chandra Seetharaman113a5682013-06-27 17:25:07 -0500512 ip->i_udquot = NULL;
513 ip->i_gdquot = NULL;
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500514 ip->i_pdquot = NULL;
Dave Chinner33479e02012-10-08 21:56:11 +1100515 xfs_iflags_set(ip, iflags);
516
517 /* insert the new inode */
518 spin_lock(&pag->pag_ici_lock);
519 error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
520 if (unlikely(error)) {
521 WARN_ON(error != -EEXIST);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100522 XFS_STATS_INC(mp, xs_ig_dup);
Dave Chinner24513372014-06-25 14:58:08 +1000523 error = -EAGAIN;
Dave Chinner33479e02012-10-08 21:56:11 +1100524 goto out_preload_end;
525 }
526 spin_unlock(&pag->pag_ici_lock);
527 radix_tree_preload_end();
528
529 *ipp = ip;
530 return 0;
531
532out_preload_end:
533 spin_unlock(&pag->pag_ici_lock);
534 radix_tree_preload_end();
535 if (lock_flags)
536 xfs_iunlock(ip, lock_flags);
537out_destroy:
538 __destroy_inode(VFS_I(ip));
539 xfs_inode_free(ip);
540 return error;
541}
542
543/*
544 * Look up an inode by number in the given file system.
545 * The inode is looked up in the cache held in each AG.
546 * If the inode is found in the cache, initialise the vfs inode
547 * if necessary.
548 *
549 * If it is not in core, read it in from the file system's device,
550 * add it to the cache and initialise the vfs inode.
551 *
552 * The inode is locked according to the value of the lock_flags parameter.
553 * This flag parameter indicates how and if the inode's IO lock and inode lock
554 * should be taken.
555 *
556 * mp -- the mount point structure for the current file system. It points
557 * to the inode hash table.
558 * tp -- a pointer to the current transaction if there is one. This is
559 * simply passed through to the xfs_iread() call.
560 * ino -- the number of the inode desired. This is the unique identifier
561 * within the file system for the inode being requested.
562 * lock_flags -- flags indicating how to lock the inode. See the comment
563 * for xfs_ilock() for a list of valid values.
564 */
565int
566xfs_iget(
567 xfs_mount_t *mp,
568 xfs_trans_t *tp,
569 xfs_ino_t ino,
570 uint flags,
571 uint lock_flags,
572 xfs_inode_t **ipp)
573{
574 xfs_inode_t *ip;
575 int error;
576 xfs_perag_t *pag;
577 xfs_agino_t agino;
578
579 /*
580 * xfs_reclaim_inode() uses the ILOCK to ensure an inode
581 * doesn't get freed while it's being referenced during a
582 * radix tree traversal here. It assumes this function
583 * aqcuires only the ILOCK (and therefore it has no need to
584 * involve the IOLOCK in this synchronization).
585 */
586 ASSERT((lock_flags & (XFS_IOLOCK_EXCL | XFS_IOLOCK_SHARED)) == 0);
587
588 /* reject inode numbers outside existing AGs */
589 if (!ino || XFS_INO_TO_AGNO(mp, ino) >= mp->m_sb.sb_agcount)
Dave Chinner24513372014-06-25 14:58:08 +1000590 return -EINVAL;
Dave Chinner33479e02012-10-08 21:56:11 +1100591
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100592 XFS_STATS_INC(mp, xs_ig_attempts);
Lucas Stach8774cf82015-08-28 14:50:56 +1000593
Dave Chinner33479e02012-10-08 21:56:11 +1100594 /* get the perag structure and ensure that it's inode capable */
595 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ino));
596 agino = XFS_INO_TO_AGINO(mp, ino);
597
598again:
599 error = 0;
600 rcu_read_lock();
601 ip = radix_tree_lookup(&pag->pag_ici_root, agino);
602
603 if (ip) {
604 error = xfs_iget_cache_hit(pag, ip, ino, flags, lock_flags);
605 if (error)
606 goto out_error_or_again;
607 } else {
608 rcu_read_unlock();
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100609 XFS_STATS_INC(mp, xs_ig_missed);
Dave Chinner33479e02012-10-08 21:56:11 +1100610
611 error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip,
612 flags, lock_flags);
613 if (error)
614 goto out_error_or_again;
615 }
616 xfs_perag_put(pag);
617
618 *ipp = ip;
619
620 /*
Dave Chinner58c90472015-02-23 22:38:08 +1100621 * If we have a real type for an on-disk inode, we can setup the inode
Dave Chinner33479e02012-10-08 21:56:11 +1100622 * now. If it's a new inode being created, xfs_ialloc will handle it.
623 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100624 if (xfs_iflags_test(ip, XFS_INEW) && VFS_I(ip)->i_mode != 0)
Dave Chinner58c90472015-02-23 22:38:08 +1100625 xfs_setup_existing_inode(ip);
Dave Chinner33479e02012-10-08 21:56:11 +1100626 return 0;
627
628out_error_or_again:
Dave Chinner24513372014-06-25 14:58:08 +1000629 if (error == -EAGAIN) {
Dave Chinner33479e02012-10-08 21:56:11 +1100630 delay(1);
631 goto again;
632 }
633 xfs_perag_put(pag);
634 return error;
635}
636
Dave Chinner78ae5252010-09-28 12:28:19 +1000637/*
638 * The inode lookup is done in batches to keep the amount of lock traffic and
639 * radix tree lookups to a minimum. The batch size is a trade off between
640 * lookup reduction and stack usage. This is in the reclaim path, so we can't
641 * be too greedy.
642 */
643#define XFS_LOOKUP_BATCH 32
644
Dave Chinnere13de952010-09-28 12:28:06 +1000645STATIC int
646xfs_inode_ag_walk_grab(
Brian Foster2ea882d2017-04-26 08:30:39 -0700647 struct xfs_inode *ip,
648 int flags)
Dave Chinnere13de952010-09-28 12:28:06 +1000649{
650 struct inode *inode = VFS_I(ip);
Brian Foster2ea882d2017-04-26 08:30:39 -0700651 bool newinos = !!(flags & XFS_AGITER_INEW_WAIT);
Dave Chinnere13de952010-09-28 12:28:06 +1000652
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100653 ASSERT(rcu_read_lock_held());
654
655 /*
656 * check for stale RCU freed inode
657 *
658 * If the inode has been reallocated, it doesn't matter if it's not in
659 * the AG we are walking - we are walking for writeback, so if it
660 * passes all the "valid inode" checks and is dirty, then we'll write
661 * it back anyway. If it has been reallocated and still being
662 * initialised, the XFS_INEW check below will catch it.
663 */
664 spin_lock(&ip->i_flags_lock);
665 if (!ip->i_ino)
666 goto out_unlock_noent;
667
668 /* avoid new or reclaimable inodes. Leave for reclaim code to flush */
Brian Foster2ea882d2017-04-26 08:30:39 -0700669 if ((!newinos && __xfs_iflags_test(ip, XFS_INEW)) ||
670 __xfs_iflags_test(ip, XFS_IRECLAIMABLE | XFS_IRECLAIM))
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100671 goto out_unlock_noent;
672 spin_unlock(&ip->i_flags_lock);
673
Dave Chinnere13de952010-09-28 12:28:06 +1000674 /* nothing to sync during shutdown */
675 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
Dave Chinner24513372014-06-25 14:58:08 +1000676 return -EFSCORRUPTED;
Dave Chinnere13de952010-09-28 12:28:06 +1000677
Dave Chinnere13de952010-09-28 12:28:06 +1000678 /* If we can't grab the inode, it must on it's way to reclaim. */
679 if (!igrab(inode))
Dave Chinner24513372014-06-25 14:58:08 +1000680 return -ENOENT;
Dave Chinnere13de952010-09-28 12:28:06 +1000681
Dave Chinnere13de952010-09-28 12:28:06 +1000682 /* inode is valid */
683 return 0;
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100684
685out_unlock_noent:
686 spin_unlock(&ip->i_flags_lock);
Dave Chinner24513372014-06-25 14:58:08 +1000687 return -ENOENT;
Dave Chinnere13de952010-09-28 12:28:06 +1000688}
689
Dave Chinner75f3cb12009-06-08 15:35:14 +0200690STATIC int
691xfs_inode_ag_walk(
692 struct xfs_mount *mp,
Dave Chinner5017e972010-01-11 11:47:40 +0000693 struct xfs_perag *pag,
Eric Sandeene0094002014-04-14 19:04:19 +1000694 int (*execute)(struct xfs_inode *ip, int flags,
Brian Fostera454f742012-11-06 09:50:39 -0500695 void *args),
696 int flags,
697 void *args,
Brian Foster2ea882d2017-04-26 08:30:39 -0700698 int tag,
699 int iter_flags)
Dave Chinner75f3cb12009-06-08 15:35:14 +0200700{
Dave Chinner75f3cb12009-06-08 15:35:14 +0200701 uint32_t first_index;
702 int last_error = 0;
703 int skipped;
Dave Chinner65d0f202010-09-24 18:40:15 +1000704 int done;
Dave Chinner78ae5252010-09-28 12:28:19 +1000705 int nr_found;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200706
707restart:
Dave Chinner65d0f202010-09-24 18:40:15 +1000708 done = 0;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200709 skipped = 0;
710 first_index = 0;
Dave Chinner78ae5252010-09-28 12:28:19 +1000711 nr_found = 0;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200712 do {
Dave Chinner78ae5252010-09-28 12:28:19 +1000713 struct xfs_inode *batch[XFS_LOOKUP_BATCH];
Dave Chinner75f3cb12009-06-08 15:35:14 +0200714 int error = 0;
Dave Chinner78ae5252010-09-28 12:28:19 +1000715 int i;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200716
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100717 rcu_read_lock();
Brian Fostera454f742012-11-06 09:50:39 -0500718
719 if (tag == -1)
720 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
Dave Chinner78ae5252010-09-28 12:28:19 +1000721 (void **)batch, first_index,
722 XFS_LOOKUP_BATCH);
Brian Fostera454f742012-11-06 09:50:39 -0500723 else
724 nr_found = radix_tree_gang_lookup_tag(
725 &pag->pag_ici_root,
726 (void **) batch, first_index,
727 XFS_LOOKUP_BATCH, tag);
728
Dave Chinner65d0f202010-09-24 18:40:15 +1000729 if (!nr_found) {
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100730 rcu_read_unlock();
Dave Chinner75f3cb12009-06-08 15:35:14 +0200731 break;
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000732 }
Dave Chinner75f3cb12009-06-08 15:35:14 +0200733
Dave Chinner65d0f202010-09-24 18:40:15 +1000734 /*
Dave Chinner78ae5252010-09-28 12:28:19 +1000735 * Grab the inodes before we drop the lock. if we found
736 * nothing, nr == 0 and the loop will be skipped.
Dave Chinner65d0f202010-09-24 18:40:15 +1000737 */
Dave Chinner78ae5252010-09-28 12:28:19 +1000738 for (i = 0; i < nr_found; i++) {
739 struct xfs_inode *ip = batch[i];
Dave Chinner65d0f202010-09-24 18:40:15 +1000740
Brian Foster2ea882d2017-04-26 08:30:39 -0700741 if (done || xfs_inode_ag_walk_grab(ip, iter_flags))
Dave Chinner78ae5252010-09-28 12:28:19 +1000742 batch[i] = NULL;
743
744 /*
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100745 * Update the index for the next lookup. Catch
746 * overflows into the next AG range which can occur if
747 * we have inodes in the last block of the AG and we
748 * are currently pointing to the last inode.
749 *
750 * Because we may see inodes that are from the wrong AG
751 * due to RCU freeing and reallocation, only update the
752 * index if it lies in this AG. It was a race that lead
753 * us to see this inode, so another lookup from the
754 * same index will not find it again.
Dave Chinner78ae5252010-09-28 12:28:19 +1000755 */
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100756 if (XFS_INO_TO_AGNO(mp, ip->i_ino) != pag->pag_agno)
757 continue;
Dave Chinner78ae5252010-09-28 12:28:19 +1000758 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
759 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
760 done = 1;
Dave Chinnere13de952010-09-28 12:28:06 +1000761 }
Dave Chinner78ae5252010-09-28 12:28:19 +1000762
763 /* unlock now we've grabbed the inodes. */
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100764 rcu_read_unlock();
Dave Chinnere13de952010-09-28 12:28:06 +1000765
Dave Chinner78ae5252010-09-28 12:28:19 +1000766 for (i = 0; i < nr_found; i++) {
767 if (!batch[i])
768 continue;
Brian Foster2ea882d2017-04-26 08:30:39 -0700769 if ((iter_flags & XFS_AGITER_INEW_WAIT) &&
770 xfs_iflags_test(batch[i], XFS_INEW))
771 xfs_inew_wait(batch[i]);
Eric Sandeene0094002014-04-14 19:04:19 +1000772 error = execute(batch[i], flags, args);
Dave Chinner78ae5252010-09-28 12:28:19 +1000773 IRELE(batch[i]);
Dave Chinner24513372014-06-25 14:58:08 +1000774 if (error == -EAGAIN) {
Dave Chinner78ae5252010-09-28 12:28:19 +1000775 skipped++;
776 continue;
777 }
Dave Chinner24513372014-06-25 14:58:08 +1000778 if (error && last_error != -EFSCORRUPTED)
Dave Chinner78ae5252010-09-28 12:28:19 +1000779 last_error = error;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200780 }
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000781
782 /* bail out if the filesystem is corrupted. */
Dave Chinner24513372014-06-25 14:58:08 +1000783 if (error == -EFSCORRUPTED)
Dave Chinner75f3cb12009-06-08 15:35:14 +0200784 break;
785
Dave Chinner8daaa832011-07-08 14:14:46 +1000786 cond_resched();
787
Dave Chinner78ae5252010-09-28 12:28:19 +1000788 } while (nr_found && !done);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200789
790 if (skipped) {
791 delay(1);
792 goto restart;
793 }
Dave Chinner75f3cb12009-06-08 15:35:14 +0200794 return last_error;
795}
796
Brian Foster579b62f2012-11-06 09:50:47 -0500797/*
798 * Background scanning to trim post-EOF preallocated space. This is queued
Dwight Engenb9fe5052013-08-15 14:08:02 -0400799 * based on the 'speculative_prealloc_lifetime' tunable (5m by default).
Brian Foster579b62f2012-11-06 09:50:47 -0500800 */
Brian Fosterfa5a4f52016-06-21 11:53:28 +1000801void
Brian Foster579b62f2012-11-06 09:50:47 -0500802xfs_queue_eofblocks(
803 struct xfs_mount *mp)
804{
805 rcu_read_lock();
806 if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_EOFBLOCKS_TAG))
807 queue_delayed_work(mp->m_eofblocks_workqueue,
808 &mp->m_eofblocks_work,
809 msecs_to_jiffies(xfs_eofb_secs * 1000));
810 rcu_read_unlock();
811}
812
813void
814xfs_eofblocks_worker(
815 struct work_struct *work)
816{
817 struct xfs_mount *mp = container_of(to_delayed_work(work),
818 struct xfs_mount, m_eofblocks_work);
819 xfs_icache_free_eofblocks(mp, NULL);
820 xfs_queue_eofblocks(mp);
821}
822
Darrick J. Wong83104d42016-10-03 09:11:46 -0700823/*
824 * Background scanning to trim preallocated CoW space. This is queued
825 * based on the 'speculative_cow_prealloc_lifetime' tunable (5m by default).
826 * (We'll just piggyback on the post-EOF prealloc space workqueue.)
827 */
828STATIC void
829xfs_queue_cowblocks(
830 struct xfs_mount *mp)
831{
832 rcu_read_lock();
833 if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_COWBLOCKS_TAG))
834 queue_delayed_work(mp->m_eofblocks_workqueue,
835 &mp->m_cowblocks_work,
836 msecs_to_jiffies(xfs_cowb_secs * 1000));
837 rcu_read_unlock();
838}
839
840void
841xfs_cowblocks_worker(
842 struct work_struct *work)
843{
844 struct xfs_mount *mp = container_of(to_delayed_work(work),
845 struct xfs_mount, m_cowblocks_work);
846 xfs_icache_free_cowblocks(mp, NULL);
847 xfs_queue_cowblocks(mp);
848}
849
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200850int
Brian Foster2ea882d2017-04-26 08:30:39 -0700851xfs_inode_ag_iterator_flags(
Dave Chinner75f3cb12009-06-08 15:35:14 +0200852 struct xfs_mount *mp,
Eric Sandeene0094002014-04-14 19:04:19 +1000853 int (*execute)(struct xfs_inode *ip, int flags,
Brian Fostera454f742012-11-06 09:50:39 -0500854 void *args),
855 int flags,
Brian Foster2ea882d2017-04-26 08:30:39 -0700856 void *args,
857 int iter_flags)
Dave Chinner75f3cb12009-06-08 15:35:14 +0200858{
Dave Chinner16fd5362010-07-20 09:43:39 +1000859 struct xfs_perag *pag;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200860 int error = 0;
861 int last_error = 0;
862 xfs_agnumber_t ag;
863
Dave Chinner16fd5362010-07-20 09:43:39 +1000864 ag = 0;
Dave Chinner65d0f202010-09-24 18:40:15 +1000865 while ((pag = xfs_perag_get(mp, ag))) {
866 ag = pag->pag_agno + 1;
Brian Foster2ea882d2017-04-26 08:30:39 -0700867 error = xfs_inode_ag_walk(mp, pag, execute, flags, args, -1,
868 iter_flags);
Brian Fostera454f742012-11-06 09:50:39 -0500869 xfs_perag_put(pag);
870 if (error) {
871 last_error = error;
Dave Chinner24513372014-06-25 14:58:08 +1000872 if (error == -EFSCORRUPTED)
Brian Fostera454f742012-11-06 09:50:39 -0500873 break;
874 }
875 }
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000876 return last_error;
Brian Fostera454f742012-11-06 09:50:39 -0500877}
878
879int
Brian Foster2ea882d2017-04-26 08:30:39 -0700880xfs_inode_ag_iterator(
881 struct xfs_mount *mp,
882 int (*execute)(struct xfs_inode *ip, int flags,
883 void *args),
884 int flags,
885 void *args)
886{
887 return xfs_inode_ag_iterator_flags(mp, execute, flags, args, 0);
888}
889
890int
Brian Fostera454f742012-11-06 09:50:39 -0500891xfs_inode_ag_iterator_tag(
892 struct xfs_mount *mp,
Eric Sandeene0094002014-04-14 19:04:19 +1000893 int (*execute)(struct xfs_inode *ip, int flags,
Brian Fostera454f742012-11-06 09:50:39 -0500894 void *args),
895 int flags,
896 void *args,
897 int tag)
898{
899 struct xfs_perag *pag;
900 int error = 0;
901 int last_error = 0;
902 xfs_agnumber_t ag;
903
904 ag = 0;
905 while ((pag = xfs_perag_get_tag(mp, ag, tag))) {
906 ag = pag->pag_agno + 1;
Brian Foster2ea882d2017-04-26 08:30:39 -0700907 error = xfs_inode_ag_walk(mp, pag, execute, flags, args, tag,
908 0);
Dave Chinner5017e972010-01-11 11:47:40 +0000909 xfs_perag_put(pag);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200910 if (error) {
911 last_error = error;
Dave Chinner24513372014-06-25 14:58:08 +1000912 if (error == -EFSCORRUPTED)
Dave Chinner75f3cb12009-06-08 15:35:14 +0200913 break;
914 }
915 }
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000916 return last_error;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200917}
918
David Chinner76bf1052008-10-30 17:16:21 +1100919/*
Dave Chinnere3a20c02010-09-24 19:51:50 +1000920 * Grab the inode for reclaim exclusively.
921 * Return 0 if we grabbed it, non-zero otherwise.
922 */
923STATIC int
924xfs_reclaim_inode_grab(
925 struct xfs_inode *ip,
926 int flags)
927{
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100928 ASSERT(rcu_read_lock_held());
929
930 /* quick check for stale RCU freed inode */
931 if (!ip->i_ino)
932 return 1;
Dave Chinnere3a20c02010-09-24 19:51:50 +1000933
934 /*
Christoph Hellwig474fce02011-12-18 20:00:09 +0000935 * If we are asked for non-blocking operation, do unlocked checks to
936 * see if the inode already is being flushed or in reclaim to avoid
937 * lock traffic.
Dave Chinnere3a20c02010-09-24 19:51:50 +1000938 */
939 if ((flags & SYNC_TRYLOCK) &&
Christoph Hellwig474fce02011-12-18 20:00:09 +0000940 __xfs_iflags_test(ip, XFS_IFLOCK | XFS_IRECLAIM))
Dave Chinnere3a20c02010-09-24 19:51:50 +1000941 return 1;
Dave Chinnere3a20c02010-09-24 19:51:50 +1000942
943 /*
944 * The radix tree lock here protects a thread in xfs_iget from racing
945 * with us starting reclaim on the inode. Once we have the
946 * XFS_IRECLAIM flag set it will not touch us.
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100947 *
948 * Due to RCU lookup, we may find inodes that have been freed and only
949 * have XFS_IRECLAIM set. Indeed, we may see reallocated inodes that
950 * aren't candidates for reclaim at all, so we must check the
951 * XFS_IRECLAIMABLE is set first before proceeding to reclaim.
Dave Chinnere3a20c02010-09-24 19:51:50 +1000952 */
953 spin_lock(&ip->i_flags_lock);
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100954 if (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) ||
955 __xfs_iflags_test(ip, XFS_IRECLAIM)) {
956 /* not a reclaim candidate. */
Dave Chinnere3a20c02010-09-24 19:51:50 +1000957 spin_unlock(&ip->i_flags_lock);
958 return 1;
959 }
960 __xfs_iflags_set(ip, XFS_IRECLAIM);
961 spin_unlock(&ip->i_flags_lock);
962 return 0;
963}
964
965/*
Christoph Hellwig8a480882012-04-23 15:58:35 +1000966 * Inodes in different states need to be treated differently. The following
967 * table lists the inode states and the reclaim actions necessary:
Dave Chinner777df5a2010-02-06 12:37:26 +1100968 *
969 * inode state iflush ret required action
970 * --------------- ---------- ---------------
971 * bad - reclaim
972 * shutdown EIO unpin and reclaim
973 * clean, unpinned 0 reclaim
974 * stale, unpinned 0 reclaim
Dave Chinnerc8543632010-02-06 12:39:36 +1100975 * clean, pinned(*) 0 requeue
976 * stale, pinned EAGAIN requeue
Christoph Hellwig8a480882012-04-23 15:58:35 +1000977 * dirty, async - requeue
978 * dirty, sync 0 reclaim
Dave Chinner777df5a2010-02-06 12:37:26 +1100979 *
980 * (*) dgc: I don't think the clean, pinned state is possible but it gets
981 * handled anyway given the order of checks implemented.
982 *
Dave Chinnerc8543632010-02-06 12:39:36 +1100983 * Also, because we get the flush lock first, we know that any inode that has
984 * been flushed delwri has had the flush completed by the time we check that
Christoph Hellwig8a480882012-04-23 15:58:35 +1000985 * the inode is clean.
Dave Chinnerc8543632010-02-06 12:39:36 +1100986 *
Christoph Hellwig8a480882012-04-23 15:58:35 +1000987 * Note that because the inode is flushed delayed write by AIL pushing, the
988 * flush lock may already be held here and waiting on it can result in very
989 * long latencies. Hence for sync reclaims, where we wait on the flush lock,
990 * the caller should push the AIL first before trying to reclaim inodes to
991 * minimise the amount of time spent waiting. For background relaim, we only
992 * bother to reclaim clean inodes anyway.
Dave Chinnerc8543632010-02-06 12:39:36 +1100993 *
Dave Chinner777df5a2010-02-06 12:37:26 +1100994 * Hence the order of actions after gaining the locks should be:
995 * bad => reclaim
996 * shutdown => unpin and reclaim
Christoph Hellwig8a480882012-04-23 15:58:35 +1000997 * pinned, async => requeue
Dave Chinnerc8543632010-02-06 12:39:36 +1100998 * pinned, sync => unpin
Dave Chinner777df5a2010-02-06 12:37:26 +1100999 * stale => reclaim
1000 * clean => reclaim
Christoph Hellwig8a480882012-04-23 15:58:35 +10001001 * dirty, async => requeue
Dave Chinnerc8543632010-02-06 12:39:36 +11001002 * dirty, sync => flush, wait and reclaim
Dave Chinner777df5a2010-02-06 12:37:26 +11001003 */
Dave Chinner75f3cb12009-06-08 15:35:14 +02001004STATIC int
Dave Chinnerc8e20be2010-01-10 23:51:45 +00001005xfs_reclaim_inode(
Dave Chinner75f3cb12009-06-08 15:35:14 +02001006 struct xfs_inode *ip,
1007 struct xfs_perag *pag,
Dave Chinnerc8e20be2010-01-10 23:51:45 +00001008 int sync_mode)
David Chinner7a3be022008-10-30 17:37:37 +11001009{
Christoph Hellwig4c468192012-04-23 15:58:36 +10001010 struct xfs_buf *bp = NULL;
Dave Chinner8a17d7d2016-05-18 14:09:12 +10001011 xfs_ino_t ino = ip->i_ino; /* for radix_tree_delete */
Christoph Hellwig4c468192012-04-23 15:58:36 +10001012 int error;
Dave Chinner777df5a2010-02-06 12:37:26 +11001013
Dave Chinner1bfd8d02011-03-26 09:13:55 +11001014restart:
1015 error = 0;
Dave Chinnerc8e20be2010-01-10 23:51:45 +00001016 xfs_ilock(ip, XFS_ILOCK_EXCL);
Dave Chinnerc8543632010-02-06 12:39:36 +11001017 if (!xfs_iflock_nowait(ip)) {
1018 if (!(sync_mode & SYNC_WAIT))
1019 goto out;
1020 xfs_iflock(ip);
1021 }
Dave Chinnerc8e20be2010-01-10 23:51:45 +00001022
Dave Chinner777df5a2010-02-06 12:37:26 +11001023 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
1024 xfs_iunpin_wait(ip);
Brian Fosterb49ef752017-01-09 16:38:38 +01001025 /* xfs_iflush_abort() drops the flush lock */
Dave Chinner04913fd2012-04-23 15:58:41 +10001026 xfs_iflush_abort(ip, false);
Dave Chinner777df5a2010-02-06 12:37:26 +11001027 goto reclaim;
1028 }
Dave Chinnerc8543632010-02-06 12:39:36 +11001029 if (xfs_ipincount(ip)) {
Christoph Hellwig8a480882012-04-23 15:58:35 +10001030 if (!(sync_mode & SYNC_WAIT))
1031 goto out_ifunlock;
Dave Chinner777df5a2010-02-06 12:37:26 +11001032 xfs_iunpin_wait(ip);
Dave Chinnerc8543632010-02-06 12:39:36 +11001033 }
Brian Fosterb49ef752017-01-09 16:38:38 +01001034 if (xfs_iflags_test(ip, XFS_ISTALE) || xfs_inode_clean(ip)) {
1035 xfs_ifunlock(ip);
Dave Chinner777df5a2010-02-06 12:37:26 +11001036 goto reclaim;
Brian Fosterb49ef752017-01-09 16:38:38 +01001037 }
Dave Chinner777df5a2010-02-06 12:37:26 +11001038
Dave Chinner1bfd8d02011-03-26 09:13:55 +11001039 /*
Christoph Hellwig8a480882012-04-23 15:58:35 +10001040 * Never flush out dirty data during non-blocking reclaim, as it would
1041 * just contend with AIL pushing trying to do the same job.
1042 */
1043 if (!(sync_mode & SYNC_WAIT))
1044 goto out_ifunlock;
1045
1046 /*
Dave Chinner1bfd8d02011-03-26 09:13:55 +11001047 * Now we have an inode that needs flushing.
1048 *
Christoph Hellwig4c468192012-04-23 15:58:36 +10001049 * Note that xfs_iflush will never block on the inode buffer lock, as
Dave Chinner1bfd8d02011-03-26 09:13:55 +11001050 * xfs_ifree_cluster() can lock the inode buffer before it locks the
Christoph Hellwig4c468192012-04-23 15:58:36 +10001051 * ip->i_lock, and we are doing the exact opposite here. As a result,
Christoph Hellwig475ee412012-07-03 12:21:22 -04001052 * doing a blocking xfs_imap_to_bp() to get the cluster buffer would
1053 * result in an ABBA deadlock with xfs_ifree_cluster().
Dave Chinner1bfd8d02011-03-26 09:13:55 +11001054 *
1055 * As xfs_ifree_cluser() must gather all inodes that are active in the
1056 * cache to mark them stale, if we hit this case we don't actually want
1057 * to do IO here - we want the inode marked stale so we can simply
Christoph Hellwig4c468192012-04-23 15:58:36 +10001058 * reclaim it. Hence if we get an EAGAIN error here, just unlock the
1059 * inode, back off and try again. Hopefully the next pass through will
1060 * see the stale flag set on the inode.
Dave Chinner1bfd8d02011-03-26 09:13:55 +11001061 */
Christoph Hellwig4c468192012-04-23 15:58:36 +10001062 error = xfs_iflush(ip, &bp);
Dave Chinner24513372014-06-25 14:58:08 +10001063 if (error == -EAGAIN) {
Christoph Hellwig8a480882012-04-23 15:58:35 +10001064 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1065 /* backoff longer than in xfs_ifree_cluster */
1066 delay(2);
1067 goto restart;
Dave Chinnerc8e20be2010-01-10 23:51:45 +00001068 }
Dave Chinnerc8543632010-02-06 12:39:36 +11001069
Christoph Hellwig4c468192012-04-23 15:58:36 +10001070 if (!error) {
1071 error = xfs_bwrite(bp);
1072 xfs_buf_relse(bp);
1073 }
1074
Dave Chinner777df5a2010-02-06 12:37:26 +11001075reclaim:
Brian Fosterb49ef752017-01-09 16:38:38 +01001076 ASSERT(!xfs_isiflocked(ip));
1077
Dave Chinner8a17d7d2016-05-18 14:09:12 +10001078 /*
1079 * Because we use RCU freeing we need to ensure the inode always appears
1080 * to be reclaimed with an invalid inode number when in the free state.
Brian Fosterb49ef752017-01-09 16:38:38 +01001081 * We do this as early as possible under the ILOCK so that
1082 * xfs_iflush_cluster() can be guaranteed to detect races with us here.
1083 * By doing this, we guarantee that once xfs_iflush_cluster has locked
1084 * XFS_ILOCK that it will see either a valid, flushable inode that will
1085 * serialise correctly, or it will see a clean (and invalid) inode that
1086 * it can skip.
Dave Chinner8a17d7d2016-05-18 14:09:12 +10001087 */
1088 spin_lock(&ip->i_flags_lock);
1089 ip->i_flags = XFS_IRECLAIM;
1090 ip->i_ino = 0;
1091 spin_unlock(&ip->i_flags_lock);
1092
Dave Chinnerc8e20be2010-01-10 23:51:45 +00001093 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner2f11fea2010-07-20 17:53:25 +10001094
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001095 XFS_STATS_INC(ip->i_mount, xs_ig_reclaims);
Dave Chinner2f11fea2010-07-20 17:53:25 +10001096 /*
1097 * Remove the inode from the per-AG radix tree.
1098 *
1099 * Because radix_tree_delete won't complain even if the item was never
1100 * added to the tree assert that it's been there before to catch
1101 * problems with the inode life time early on.
1102 */
Dave Chinner1a427ab2010-12-16 17:08:41 +11001103 spin_lock(&pag->pag_ici_lock);
Dave Chinner2f11fea2010-07-20 17:53:25 +10001104 if (!radix_tree_delete(&pag->pag_ici_root,
Dave Chinner8a17d7d2016-05-18 14:09:12 +10001105 XFS_INO_TO_AGINO(ip->i_mount, ino)))
Dave Chinner2f11fea2010-07-20 17:53:25 +10001106 ASSERT(0);
Dave Chinner545c0882016-05-18 14:11:41 +10001107 xfs_perag_clear_reclaim_tag(pag);
Dave Chinner1a427ab2010-12-16 17:08:41 +11001108 spin_unlock(&pag->pag_ici_lock);
Dave Chinner2f11fea2010-07-20 17:53:25 +10001109
1110 /*
1111 * Here we do an (almost) spurious inode lock in order to coordinate
1112 * with inode cache radix tree lookups. This is because the lookup
1113 * can reference the inodes in the cache without taking references.
1114 *
1115 * We make that OK here by ensuring that we wait until the inode is
Alex Elderad637a12012-02-16 22:01:00 +00001116 * unlocked after the lookup before we go ahead and free it.
Dave Chinner2f11fea2010-07-20 17:53:25 +10001117 */
Alex Elderad637a12012-02-16 22:01:00 +00001118 xfs_ilock(ip, XFS_ILOCK_EXCL);
Dave Chinner2f11fea2010-07-20 17:53:25 +10001119 xfs_qm_dqdetach(ip);
Alex Elderad637a12012-02-16 22:01:00 +00001120 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner2f11fea2010-07-20 17:53:25 +10001121
Dave Chinner8a17d7d2016-05-18 14:09:12 +10001122 __xfs_inode_free(ip);
Alex Elderad637a12012-02-16 22:01:00 +00001123 return error;
Christoph Hellwig8a480882012-04-23 15:58:35 +10001124
1125out_ifunlock:
1126 xfs_ifunlock(ip);
1127out:
1128 xfs_iflags_clear(ip, XFS_IRECLAIM);
1129 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1130 /*
Dave Chinner24513372014-06-25 14:58:08 +10001131 * We could return -EAGAIN here to make reclaim rescan the inode tree in
Christoph Hellwig8a480882012-04-23 15:58:35 +10001132 * a short while. However, this just burns CPU time scanning the tree
Dave Chinner58896082012-10-08 21:56:05 +11001133 * waiting for IO to complete and the reclaim work never goes back to
1134 * the idle state. Instead, return 0 to let the next scheduled
1135 * background reclaim attempt to reclaim the inode again.
Christoph Hellwig8a480882012-04-23 15:58:35 +10001136 */
1137 return 0;
David Chinner7a3be022008-10-30 17:37:37 +11001138}
1139
Dave Chinner65d0f202010-09-24 18:40:15 +10001140/*
1141 * Walk the AGs and reclaim the inodes in them. Even if the filesystem is
1142 * corrupted, we still want to try to reclaim all the inodes. If we don't,
1143 * then a shut down during filesystem unmount reclaim walk leak all the
1144 * unreclaimed inodes.
1145 */
Dave Chinner33479e02012-10-08 21:56:11 +11001146STATIC int
Dave Chinner65d0f202010-09-24 18:40:15 +10001147xfs_reclaim_inodes_ag(
1148 struct xfs_mount *mp,
1149 int flags,
1150 int *nr_to_scan)
1151{
1152 struct xfs_perag *pag;
1153 int error = 0;
1154 int last_error = 0;
1155 xfs_agnumber_t ag;
Dave Chinner69b491c2010-09-27 11:09:51 +10001156 int trylock = flags & SYNC_TRYLOCK;
1157 int skipped;
Dave Chinner65d0f202010-09-24 18:40:15 +10001158
Dave Chinner69b491c2010-09-27 11:09:51 +10001159restart:
Dave Chinner65d0f202010-09-24 18:40:15 +10001160 ag = 0;
Dave Chinner69b491c2010-09-27 11:09:51 +10001161 skipped = 0;
Dave Chinner65d0f202010-09-24 18:40:15 +10001162 while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
1163 unsigned long first_index = 0;
1164 int done = 0;
Dave Chinnere3a20c02010-09-24 19:51:50 +10001165 int nr_found = 0;
Dave Chinner65d0f202010-09-24 18:40:15 +10001166
1167 ag = pag->pag_agno + 1;
1168
Dave Chinner69b491c2010-09-27 11:09:51 +10001169 if (trylock) {
1170 if (!mutex_trylock(&pag->pag_ici_reclaim_lock)) {
1171 skipped++;
Dave Chinnerf83282a2010-11-08 08:55:04 +00001172 xfs_perag_put(pag);
Dave Chinner69b491c2010-09-27 11:09:51 +10001173 continue;
1174 }
1175 first_index = pag->pag_ici_reclaim_cursor;
1176 } else
1177 mutex_lock(&pag->pag_ici_reclaim_lock);
1178
Dave Chinner65d0f202010-09-24 18:40:15 +10001179 do {
Dave Chinnere3a20c02010-09-24 19:51:50 +10001180 struct xfs_inode *batch[XFS_LOOKUP_BATCH];
1181 int i;
Dave Chinner65d0f202010-09-24 18:40:15 +10001182
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001183 rcu_read_lock();
Dave Chinnere3a20c02010-09-24 19:51:50 +10001184 nr_found = radix_tree_gang_lookup_tag(
1185 &pag->pag_ici_root,
1186 (void **)batch, first_index,
1187 XFS_LOOKUP_BATCH,
Dave Chinner65d0f202010-09-24 18:40:15 +10001188 XFS_ICI_RECLAIM_TAG);
1189 if (!nr_found) {
Dave Chinnerb2232212011-05-06 02:54:04 +00001190 done = 1;
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001191 rcu_read_unlock();
Dave Chinner65d0f202010-09-24 18:40:15 +10001192 break;
1193 }
1194
1195 /*
Dave Chinnere3a20c02010-09-24 19:51:50 +10001196 * Grab the inodes before we drop the lock. if we found
1197 * nothing, nr == 0 and the loop will be skipped.
Dave Chinner65d0f202010-09-24 18:40:15 +10001198 */
Dave Chinnere3a20c02010-09-24 19:51:50 +10001199 for (i = 0; i < nr_found; i++) {
1200 struct xfs_inode *ip = batch[i];
Dave Chinner65d0f202010-09-24 18:40:15 +10001201
Dave Chinnere3a20c02010-09-24 19:51:50 +10001202 if (done || xfs_reclaim_inode_grab(ip, flags))
1203 batch[i] = NULL;
Dave Chinner65d0f202010-09-24 18:40:15 +10001204
Dave Chinnere3a20c02010-09-24 19:51:50 +10001205 /*
1206 * Update the index for the next lookup. Catch
1207 * overflows into the next AG range which can
1208 * occur if we have inodes in the last block of
1209 * the AG and we are currently pointing to the
1210 * last inode.
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001211 *
1212 * Because we may see inodes that are from the
1213 * wrong AG due to RCU freeing and
1214 * reallocation, only update the index if it
1215 * lies in this AG. It was a race that lead us
1216 * to see this inode, so another lookup from
1217 * the same index will not find it again.
Dave Chinnere3a20c02010-09-24 19:51:50 +10001218 */
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001219 if (XFS_INO_TO_AGNO(mp, ip->i_ino) !=
1220 pag->pag_agno)
1221 continue;
Dave Chinnere3a20c02010-09-24 19:51:50 +10001222 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
1223 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
1224 done = 1;
1225 }
1226
1227 /* unlock now we've grabbed the inodes. */
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001228 rcu_read_unlock();
Dave Chinnere3a20c02010-09-24 19:51:50 +10001229
1230 for (i = 0; i < nr_found; i++) {
1231 if (!batch[i])
1232 continue;
1233 error = xfs_reclaim_inode(batch[i], pag, flags);
Dave Chinner24513372014-06-25 14:58:08 +10001234 if (error && last_error != -EFSCORRUPTED)
Dave Chinnere3a20c02010-09-24 19:51:50 +10001235 last_error = error;
1236 }
1237
1238 *nr_to_scan -= XFS_LOOKUP_BATCH;
1239
Dave Chinner8daaa832011-07-08 14:14:46 +10001240 cond_resched();
1241
Dave Chinnere3a20c02010-09-24 19:51:50 +10001242 } while (nr_found && !done && *nr_to_scan > 0);
Dave Chinner65d0f202010-09-24 18:40:15 +10001243
Dave Chinner69b491c2010-09-27 11:09:51 +10001244 if (trylock && !done)
1245 pag->pag_ici_reclaim_cursor = first_index;
1246 else
1247 pag->pag_ici_reclaim_cursor = 0;
1248 mutex_unlock(&pag->pag_ici_reclaim_lock);
Dave Chinner65d0f202010-09-24 18:40:15 +10001249 xfs_perag_put(pag);
1250 }
Dave Chinner69b491c2010-09-27 11:09:51 +10001251
1252 /*
1253 * if we skipped any AG, and we still have scan count remaining, do
1254 * another pass this time using blocking reclaim semantics (i.e
1255 * waiting on the reclaim locks and ignoring the reclaim cursors). This
1256 * ensure that when we get more reclaimers than AGs we block rather
1257 * than spin trying to execute reclaim.
1258 */
Dave Chinner8daaa832011-07-08 14:14:46 +10001259 if (skipped && (flags & SYNC_WAIT) && *nr_to_scan > 0) {
Dave Chinner69b491c2010-09-27 11:09:51 +10001260 trylock = 0;
1261 goto restart;
1262 }
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001263 return last_error;
Dave Chinner65d0f202010-09-24 18:40:15 +10001264}
1265
David Chinnerfce08f22008-10-30 17:37:03 +11001266int
David Chinner1dc33182008-10-30 17:37:15 +11001267xfs_reclaim_inodes(
David Chinnerfce08f22008-10-30 17:37:03 +11001268 xfs_mount_t *mp,
David Chinnerfce08f22008-10-30 17:37:03 +11001269 int mode)
1270{
Dave Chinner65d0f202010-09-24 18:40:15 +10001271 int nr_to_scan = INT_MAX;
1272
1273 return xfs_reclaim_inodes_ag(mp, mode, &nr_to_scan);
Dave Chinner9bf729c2010-04-29 09:55:50 +10001274}
1275
1276/*
Dave Chinner8daaa832011-07-08 14:14:46 +10001277 * Scan a certain number of inodes for reclaim.
Dave Chinnera7b339f2011-04-08 12:45:07 +10001278 *
1279 * When called we make sure that there is a background (fast) inode reclaim in
Dave Chinner8daaa832011-07-08 14:14:46 +10001280 * progress, while we will throttle the speed of reclaim via doing synchronous
Dave Chinnera7b339f2011-04-08 12:45:07 +10001281 * reclaim of inodes. That means if we come across dirty inodes, we wait for
1282 * them to be cleaned, which we hope will not be very long due to the
1283 * background walker having already kicked the IO off on those dirty inodes.
Dave Chinner9bf729c2010-04-29 09:55:50 +10001284 */
Dave Chinner0a234c62013-08-28 10:17:57 +10001285long
Dave Chinner8daaa832011-07-08 14:14:46 +10001286xfs_reclaim_inodes_nr(
1287 struct xfs_mount *mp,
1288 int nr_to_scan)
Dave Chinner9bf729c2010-04-29 09:55:50 +10001289{
Dave Chinner8daaa832011-07-08 14:14:46 +10001290 /* kick background reclaimer and push the AIL */
Dave Chinner58896082012-10-08 21:56:05 +11001291 xfs_reclaim_work_queue(mp);
Dave Chinner8daaa832011-07-08 14:14:46 +10001292 xfs_ail_push_all(mp->m_ail);
Dave Chinner9bf729c2010-04-29 09:55:50 +10001293
Dave Chinner0a234c62013-08-28 10:17:57 +10001294 return xfs_reclaim_inodes_ag(mp, SYNC_TRYLOCK | SYNC_WAIT, &nr_to_scan);
Dave Chinner8daaa832011-07-08 14:14:46 +10001295}
Dave Chinnera7b339f2011-04-08 12:45:07 +10001296
Dave Chinner8daaa832011-07-08 14:14:46 +10001297/*
1298 * Return the number of reclaimable inodes in the filesystem for
1299 * the shrinker to determine how much to reclaim.
1300 */
1301int
1302xfs_reclaim_inodes_count(
1303 struct xfs_mount *mp)
1304{
1305 struct xfs_perag *pag;
1306 xfs_agnumber_t ag = 0;
1307 int reclaimable = 0;
Dave Chinner9bf729c2010-04-29 09:55:50 +10001308
Dave Chinner65d0f202010-09-24 18:40:15 +10001309 while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
1310 ag = pag->pag_agno + 1;
Dave Chinner70e60ce2010-07-20 08:07:02 +10001311 reclaimable += pag->pag_ici_reclaimable;
1312 xfs_perag_put(pag);
Dave Chinner9bf729c2010-04-29 09:55:50 +10001313 }
Dave Chinner9bf729c2010-04-29 09:55:50 +10001314 return reclaimable;
1315}
1316
Brian Foster41176a62012-11-06 09:50:42 -05001317STATIC int
Brian Foster3e3f9f52012-11-07 12:21:13 -05001318xfs_inode_match_id(
1319 struct xfs_inode *ip,
1320 struct xfs_eofblocks *eofb)
1321{
Dwight Engenb9fe5052013-08-15 14:08:02 -04001322 if ((eofb->eof_flags & XFS_EOF_FLAGS_UID) &&
1323 !uid_eq(VFS_I(ip)->i_uid, eofb->eof_uid))
Brian Foster1b556042012-11-06 09:50:45 -05001324 return 0;
Brian Foster3e3f9f52012-11-07 12:21:13 -05001325
Dwight Engenb9fe5052013-08-15 14:08:02 -04001326 if ((eofb->eof_flags & XFS_EOF_FLAGS_GID) &&
1327 !gid_eq(VFS_I(ip)->i_gid, eofb->eof_gid))
Brian Foster1b556042012-11-06 09:50:45 -05001328 return 0;
1329
Dwight Engenb9fe5052013-08-15 14:08:02 -04001330 if ((eofb->eof_flags & XFS_EOF_FLAGS_PRID) &&
Brian Foster1b556042012-11-06 09:50:45 -05001331 xfs_get_projid(ip) != eofb->eof_prid)
1332 return 0;
1333
1334 return 1;
Brian Foster3e3f9f52012-11-07 12:21:13 -05001335}
1336
Brian Fosterf4526392014-07-24 19:44:28 +10001337/*
1338 * A union-based inode filtering algorithm. Process the inode if any of the
1339 * criteria match. This is for global/internal scans only.
1340 */
1341STATIC int
1342xfs_inode_match_id_union(
1343 struct xfs_inode *ip,
1344 struct xfs_eofblocks *eofb)
1345{
1346 if ((eofb->eof_flags & XFS_EOF_FLAGS_UID) &&
1347 uid_eq(VFS_I(ip)->i_uid, eofb->eof_uid))
1348 return 1;
1349
1350 if ((eofb->eof_flags & XFS_EOF_FLAGS_GID) &&
1351 gid_eq(VFS_I(ip)->i_gid, eofb->eof_gid))
1352 return 1;
1353
1354 if ((eofb->eof_flags & XFS_EOF_FLAGS_PRID) &&
1355 xfs_get_projid(ip) == eofb->eof_prid)
1356 return 1;
1357
1358 return 0;
1359}
1360
Brian Foster3e3f9f52012-11-07 12:21:13 -05001361STATIC int
Brian Foster41176a62012-11-06 09:50:42 -05001362xfs_inode_free_eofblocks(
1363 struct xfs_inode *ip,
Brian Foster41176a62012-11-06 09:50:42 -05001364 int flags,
1365 void *args)
1366{
Brian Foster798b1dc2017-01-27 23:22:55 -08001367 int ret = 0;
Brian Foster3e3f9f52012-11-07 12:21:13 -05001368 struct xfs_eofblocks *eofb = args;
Brian Fosterf4526392014-07-24 19:44:28 +10001369 int match;
Brian Foster5400da72014-07-24 19:40:22 +10001370
Brian Foster41176a62012-11-06 09:50:42 -05001371 if (!xfs_can_free_eofblocks(ip, false)) {
1372 /* inode could be preallocated or append-only */
1373 trace_xfs_inode_free_eofblocks_invalid(ip);
1374 xfs_inode_clear_eofblocks_tag(ip);
1375 return 0;
1376 }
1377
1378 /*
1379 * If the mapping is dirty the operation can block and wait for some
1380 * time. Unless we are waiting, skip it.
1381 */
1382 if (!(flags & SYNC_WAIT) &&
1383 mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_DIRTY))
1384 return 0;
1385
Brian Foster00ca79a2012-11-07 12:21:14 -05001386 if (eofb) {
Brian Fosterf4526392014-07-24 19:44:28 +10001387 if (eofb->eof_flags & XFS_EOF_FLAGS_UNION)
1388 match = xfs_inode_match_id_union(ip, eofb);
1389 else
1390 match = xfs_inode_match_id(ip, eofb);
1391 if (!match)
Brian Foster00ca79a2012-11-07 12:21:14 -05001392 return 0;
1393
1394 /* skip the inode if the file size is too small */
1395 if (eofb->eof_flags & XFS_EOF_FLAGS_MINFILESIZE &&
1396 XFS_ISIZE(ip) < eofb->eof_min_file_size)
1397 return 0;
1398 }
Brian Foster3e3f9f52012-11-07 12:21:13 -05001399
Brian Foster798b1dc2017-01-27 23:22:55 -08001400 /*
1401 * If the caller is waiting, return -EAGAIN to keep the background
1402 * scanner moving and revisit the inode in a subsequent pass.
1403 */
Brian Foster4d725d72017-01-27 23:22:56 -08001404 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
Brian Foster798b1dc2017-01-27 23:22:55 -08001405 if (flags & SYNC_WAIT)
1406 ret = -EAGAIN;
1407 return ret;
1408 }
1409 ret = xfs_free_eofblocks(ip);
Brian Foster4d725d72017-01-27 23:22:56 -08001410 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Brian Foster41176a62012-11-06 09:50:42 -05001411
1412 return ret;
1413}
1414
Darrick J. Wong83104d42016-10-03 09:11:46 -07001415static int
1416__xfs_icache_free_eofblocks(
Brian Foster41176a62012-11-06 09:50:42 -05001417 struct xfs_mount *mp,
Darrick J. Wong83104d42016-10-03 09:11:46 -07001418 struct xfs_eofblocks *eofb,
1419 int (*execute)(struct xfs_inode *ip, int flags,
1420 void *args),
1421 int tag)
Brian Foster41176a62012-11-06 09:50:42 -05001422{
Brian Foster8ca149d2012-11-07 12:21:12 -05001423 int flags = SYNC_TRYLOCK;
1424
1425 if (eofb && (eofb->eof_flags & XFS_EOF_FLAGS_SYNC))
1426 flags = SYNC_WAIT;
1427
Darrick J. Wong83104d42016-10-03 09:11:46 -07001428 return xfs_inode_ag_iterator_tag(mp, execute, flags,
1429 eofb, tag);
1430}
1431
1432int
1433xfs_icache_free_eofblocks(
1434 struct xfs_mount *mp,
1435 struct xfs_eofblocks *eofb)
1436{
1437 return __xfs_icache_free_eofblocks(mp, eofb, xfs_inode_free_eofblocks,
1438 XFS_ICI_EOFBLOCKS_TAG);
Brian Foster41176a62012-11-06 09:50:42 -05001439}
1440
Brian Fosterdc06f3982014-07-24 19:49:28 +10001441/*
1442 * Run eofblocks scans on the quotas applicable to the inode. For inodes with
1443 * multiple quotas, we don't know exactly which quota caused an allocation
1444 * failure. We make a best effort by including each quota under low free space
1445 * conditions (less than 1% free space) in the scan.
1446 */
Darrick J. Wong83104d42016-10-03 09:11:46 -07001447static int
1448__xfs_inode_free_quota_eofblocks(
1449 struct xfs_inode *ip,
1450 int (*execute)(struct xfs_mount *mp,
1451 struct xfs_eofblocks *eofb))
Brian Fosterdc06f3982014-07-24 19:49:28 +10001452{
1453 int scan = 0;
1454 struct xfs_eofblocks eofb = {0};
1455 struct xfs_dquot *dq;
1456
Brian Fosterdc06f3982014-07-24 19:49:28 +10001457 /*
Brian Foster4d725d72017-01-27 23:22:56 -08001458 * Run a sync scan to increase effectiveness and use the union filter to
Brian Fosterdc06f3982014-07-24 19:49:28 +10001459 * cover all applicable quotas in a single scan.
1460 */
Brian Fosterdc06f3982014-07-24 19:49:28 +10001461 eofb.eof_flags = XFS_EOF_FLAGS_UNION|XFS_EOF_FLAGS_SYNC;
1462
1463 if (XFS_IS_UQUOTA_ENFORCED(ip->i_mount)) {
1464 dq = xfs_inode_dquot(ip, XFS_DQ_USER);
1465 if (dq && xfs_dquot_lowsp(dq)) {
1466 eofb.eof_uid = VFS_I(ip)->i_uid;
1467 eofb.eof_flags |= XFS_EOF_FLAGS_UID;
1468 scan = 1;
1469 }
1470 }
1471
1472 if (XFS_IS_GQUOTA_ENFORCED(ip->i_mount)) {
1473 dq = xfs_inode_dquot(ip, XFS_DQ_GROUP);
1474 if (dq && xfs_dquot_lowsp(dq)) {
1475 eofb.eof_gid = VFS_I(ip)->i_gid;
1476 eofb.eof_flags |= XFS_EOF_FLAGS_GID;
1477 scan = 1;
1478 }
1479 }
1480
1481 if (scan)
Darrick J. Wong83104d42016-10-03 09:11:46 -07001482 execute(ip->i_mount, &eofb);
Brian Fosterdc06f3982014-07-24 19:49:28 +10001483
1484 return scan;
1485}
1486
Darrick J. Wong83104d42016-10-03 09:11:46 -07001487int
1488xfs_inode_free_quota_eofblocks(
1489 struct xfs_inode *ip)
1490{
1491 return __xfs_inode_free_quota_eofblocks(ip, xfs_icache_free_eofblocks);
1492}
1493
1494static void
1495__xfs_inode_set_eofblocks_tag(
1496 xfs_inode_t *ip,
1497 void (*execute)(struct xfs_mount *mp),
1498 void (*set_tp)(struct xfs_mount *mp, xfs_agnumber_t agno,
1499 int error, unsigned long caller_ip),
1500 int tag)
Brian Foster27b52862012-11-06 09:50:38 -05001501{
1502 struct xfs_mount *mp = ip->i_mount;
1503 struct xfs_perag *pag;
1504 int tagged;
1505
Christoph Hellwig85a6e762016-09-19 11:09:48 +10001506 /*
1507 * Don't bother locking the AG and looking up in the radix trees
1508 * if we already know that we have the tag set.
1509 */
1510 if (ip->i_flags & XFS_IEOFBLOCKS)
1511 return;
1512 spin_lock(&ip->i_flags_lock);
1513 ip->i_flags |= XFS_IEOFBLOCKS;
1514 spin_unlock(&ip->i_flags_lock);
1515
Brian Foster27b52862012-11-06 09:50:38 -05001516 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
1517 spin_lock(&pag->pag_ici_lock);
Brian Foster27b52862012-11-06 09:50:38 -05001518
Darrick J. Wong83104d42016-10-03 09:11:46 -07001519 tagged = radix_tree_tagged(&pag->pag_ici_root, tag);
Brian Foster27b52862012-11-06 09:50:38 -05001520 radix_tree_tag_set(&pag->pag_ici_root,
Darrick J. Wong83104d42016-10-03 09:11:46 -07001521 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino), tag);
Brian Foster27b52862012-11-06 09:50:38 -05001522 if (!tagged) {
1523 /* propagate the eofblocks tag up into the perag radix tree */
1524 spin_lock(&ip->i_mount->m_perag_lock);
1525 radix_tree_tag_set(&ip->i_mount->m_perag_tree,
1526 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
Darrick J. Wong83104d42016-10-03 09:11:46 -07001527 tag);
Brian Foster27b52862012-11-06 09:50:38 -05001528 spin_unlock(&ip->i_mount->m_perag_lock);
1529
Brian Foster579b62f2012-11-06 09:50:47 -05001530 /* kick off background trimming */
Darrick J. Wong83104d42016-10-03 09:11:46 -07001531 execute(ip->i_mount);
Brian Foster579b62f2012-11-06 09:50:47 -05001532
Darrick J. Wong83104d42016-10-03 09:11:46 -07001533 set_tp(ip->i_mount, pag->pag_agno, -1, _RET_IP_);
Brian Foster27b52862012-11-06 09:50:38 -05001534 }
1535
1536 spin_unlock(&pag->pag_ici_lock);
1537 xfs_perag_put(pag);
1538}
1539
1540void
Darrick J. Wong83104d42016-10-03 09:11:46 -07001541xfs_inode_set_eofblocks_tag(
Brian Foster27b52862012-11-06 09:50:38 -05001542 xfs_inode_t *ip)
1543{
Darrick J. Wong83104d42016-10-03 09:11:46 -07001544 trace_xfs_inode_set_eofblocks_tag(ip);
1545 return __xfs_inode_set_eofblocks_tag(ip, xfs_queue_eofblocks,
1546 trace_xfs_perag_set_eofblocks,
1547 XFS_ICI_EOFBLOCKS_TAG);
1548}
1549
1550static void
1551__xfs_inode_clear_eofblocks_tag(
1552 xfs_inode_t *ip,
1553 void (*clear_tp)(struct xfs_mount *mp, xfs_agnumber_t agno,
1554 int error, unsigned long caller_ip),
1555 int tag)
1556{
Brian Foster27b52862012-11-06 09:50:38 -05001557 struct xfs_mount *mp = ip->i_mount;
1558 struct xfs_perag *pag;
1559
Christoph Hellwig85a6e762016-09-19 11:09:48 +10001560 spin_lock(&ip->i_flags_lock);
1561 ip->i_flags &= ~XFS_IEOFBLOCKS;
1562 spin_unlock(&ip->i_flags_lock);
1563
Brian Foster27b52862012-11-06 09:50:38 -05001564 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
1565 spin_lock(&pag->pag_ici_lock);
Brian Foster27b52862012-11-06 09:50:38 -05001566
1567 radix_tree_tag_clear(&pag->pag_ici_root,
Darrick J. Wong83104d42016-10-03 09:11:46 -07001568 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino), tag);
1569 if (!radix_tree_tagged(&pag->pag_ici_root, tag)) {
Brian Foster27b52862012-11-06 09:50:38 -05001570 /* clear the eofblocks tag from the perag radix tree */
1571 spin_lock(&ip->i_mount->m_perag_lock);
1572 radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
1573 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
Darrick J. Wong83104d42016-10-03 09:11:46 -07001574 tag);
Brian Foster27b52862012-11-06 09:50:38 -05001575 spin_unlock(&ip->i_mount->m_perag_lock);
Darrick J. Wong83104d42016-10-03 09:11:46 -07001576 clear_tp(ip->i_mount, pag->pag_agno, -1, _RET_IP_);
Brian Foster27b52862012-11-06 09:50:38 -05001577 }
1578
1579 spin_unlock(&pag->pag_ici_lock);
1580 xfs_perag_put(pag);
1581}
1582
Darrick J. Wong83104d42016-10-03 09:11:46 -07001583void
1584xfs_inode_clear_eofblocks_tag(
1585 xfs_inode_t *ip)
1586{
1587 trace_xfs_inode_clear_eofblocks_tag(ip);
1588 return __xfs_inode_clear_eofblocks_tag(ip,
1589 trace_xfs_perag_clear_eofblocks, XFS_ICI_EOFBLOCKS_TAG);
1590}
1591
1592/*
1593 * Automatic CoW Reservation Freeing
1594 *
1595 * These functions automatically garbage collect leftover CoW reservations
1596 * that were made on behalf of a cowextsize hint when we start to run out
1597 * of quota or when the reservations sit around for too long. If the file
1598 * has dirty pages or is undergoing writeback, its CoW reservations will
1599 * be retained.
1600 *
1601 * The actual garbage collection piggybacks off the same code that runs
1602 * the speculative EOF preallocation garbage collector.
1603 */
1604STATIC int
1605xfs_inode_free_cowblocks(
1606 struct xfs_inode *ip,
1607 int flags,
1608 void *args)
1609{
1610 int ret;
1611 struct xfs_eofblocks *eofb = args;
Darrick J. Wong83104d42016-10-03 09:11:46 -07001612 int match;
Brian Foster2f092422017-01-09 16:38:34 +01001613 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
Darrick J. Wong83104d42016-10-03 09:11:46 -07001614
Brian Foster2f092422017-01-09 16:38:34 +01001615 /*
1616 * Just clear the tag if we have an empty cow fork or none at all. It's
1617 * possible the inode was fully unshared since it was originally tagged.
1618 */
1619 if (!xfs_is_reflink_inode(ip) || !ifp->if_bytes) {
Darrick J. Wong83104d42016-10-03 09:11:46 -07001620 trace_xfs_inode_free_cowblocks_invalid(ip);
1621 xfs_inode_clear_cowblocks_tag(ip);
1622 return 0;
1623 }
1624
1625 /*
1626 * If the mapping is dirty or under writeback we cannot touch the
1627 * CoW fork. Leave it alone if we're in the midst of a directio.
1628 */
Christoph Hellwig91192ae2017-01-09 16:39:02 +01001629 if ((VFS_I(ip)->i_state & I_DIRTY_PAGES) ||
1630 mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_DIRTY) ||
Darrick J. Wong83104d42016-10-03 09:11:46 -07001631 mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_WRITEBACK) ||
1632 atomic_read(&VFS_I(ip)->i_dio_count))
1633 return 0;
1634
1635 if (eofb) {
1636 if (eofb->eof_flags & XFS_EOF_FLAGS_UNION)
1637 match = xfs_inode_match_id_union(ip, eofb);
1638 else
1639 match = xfs_inode_match_id(ip, eofb);
1640 if (!match)
1641 return 0;
1642
1643 /* skip the inode if the file size is too small */
1644 if (eofb->eof_flags & XFS_EOF_FLAGS_MINFILESIZE &&
1645 XFS_ISIZE(ip) < eofb->eof_min_file_size)
1646 return 0;
Darrick J. Wong83104d42016-10-03 09:11:46 -07001647 }
1648
1649 /* Free the CoW blocks */
Brian Foster4d725d72017-01-27 23:22:56 -08001650 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1651 xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
Darrick J. Wong83104d42016-10-03 09:11:46 -07001652
Christoph Hellwig3b83a022017-03-07 16:45:58 -08001653 ret = xfs_reflink_cancel_cow_range(ip, 0, NULLFILEOFF, false);
Darrick J. Wong83104d42016-10-03 09:11:46 -07001654
Brian Foster4d725d72017-01-27 23:22:56 -08001655 xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
1656 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Darrick J. Wong83104d42016-10-03 09:11:46 -07001657
1658 return ret;
1659}
1660
1661int
1662xfs_icache_free_cowblocks(
1663 struct xfs_mount *mp,
1664 struct xfs_eofblocks *eofb)
1665{
1666 return __xfs_icache_free_eofblocks(mp, eofb, xfs_inode_free_cowblocks,
1667 XFS_ICI_COWBLOCKS_TAG);
1668}
1669
1670int
1671xfs_inode_free_quota_cowblocks(
1672 struct xfs_inode *ip)
1673{
1674 return __xfs_inode_free_quota_eofblocks(ip, xfs_icache_free_cowblocks);
1675}
1676
1677void
1678xfs_inode_set_cowblocks_tag(
1679 xfs_inode_t *ip)
1680{
Brian Foster7b7381f2016-10-24 14:21:00 +11001681 trace_xfs_inode_set_cowblocks_tag(ip);
Darrick J. Wong83104d42016-10-03 09:11:46 -07001682 return __xfs_inode_set_eofblocks_tag(ip, xfs_queue_cowblocks,
Brian Foster7b7381f2016-10-24 14:21:00 +11001683 trace_xfs_perag_set_cowblocks,
Darrick J. Wong83104d42016-10-03 09:11:46 -07001684 XFS_ICI_COWBLOCKS_TAG);
1685}
1686
1687void
1688xfs_inode_clear_cowblocks_tag(
1689 xfs_inode_t *ip)
1690{
Brian Foster7b7381f2016-10-24 14:21:00 +11001691 trace_xfs_inode_clear_cowblocks_tag(ip);
Darrick J. Wong83104d42016-10-03 09:11:46 -07001692 return __xfs_inode_clear_eofblocks_tag(ip,
Brian Foster7b7381f2016-10-24 14:21:00 +11001693 trace_xfs_perag_clear_cowblocks, XFS_ICI_COWBLOCKS_TAG);
Darrick J. Wong83104d42016-10-03 09:11:46 -07001694}