blob: 027cf8ba2235e278d54e593bee9c96f7f67d1194 [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
267/*
Dave Chinner50997472016-02-09 16:54:58 +1100268 * When we recycle a reclaimable inode, we need to re-initialise the VFS inode
269 * part of the structure. This is made more complex by the fact we store
270 * information about the on-disk values in the VFS inode and so we can't just
Dave Chinner83e06f22016-02-09 16:54:58 +1100271 * overwrite the values unconditionally. Hence we save the parameters we
Dave Chinner50997472016-02-09 16:54:58 +1100272 * need to retain across reinitialisation, and rewrite them into the VFS inode
Dave Chinner83e06f22016-02-09 16:54:58 +1100273 * after reinitialisation even if it fails.
Dave Chinner50997472016-02-09 16:54:58 +1100274 */
275static int
276xfs_reinit_inode(
277 struct xfs_mount *mp,
278 struct inode *inode)
279{
280 int error;
Dave Chinner54d7b5c2016-02-09 16:54:58 +1100281 uint32_t nlink = inode->i_nlink;
Dave Chinner9e9a2672016-02-09 16:54:58 +1100282 uint32_t generation = inode->i_generation;
Dave Chinner83e06f22016-02-09 16:54:58 +1100283 uint64_t version = inode->i_version;
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100284 umode_t mode = inode->i_mode;
Dave Chinner50997472016-02-09 16:54:58 +1100285
286 error = inode_init_always(mp->m_super, inode);
287
Dave Chinner54d7b5c2016-02-09 16:54:58 +1100288 set_nlink(inode, nlink);
Dave Chinner9e9a2672016-02-09 16:54:58 +1100289 inode->i_generation = generation;
Dave Chinner83e06f22016-02-09 16:54:58 +1100290 inode->i_version = version;
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100291 inode->i_mode = mode;
Dave Chinner50997472016-02-09 16:54:58 +1100292 return error;
293}
294
295/*
Dave Chinner33479e02012-10-08 21:56:11 +1100296 * Check the validity of the inode we just found it the cache
297 */
298static int
299xfs_iget_cache_hit(
300 struct xfs_perag *pag,
301 struct xfs_inode *ip,
302 xfs_ino_t ino,
303 int flags,
304 int lock_flags) __releases(RCU)
305{
306 struct inode *inode = VFS_I(ip);
307 struct xfs_mount *mp = ip->i_mount;
308 int error;
309
310 /*
311 * check for re-use of an inode within an RCU grace period due to the
312 * radix tree nodes not being updated yet. We monitor for this by
313 * setting the inode number to zero before freeing the inode structure.
314 * If the inode has been reallocated and set up, then the inode number
315 * will not match, so check for that, too.
316 */
317 spin_lock(&ip->i_flags_lock);
318 if (ip->i_ino != ino) {
319 trace_xfs_iget_skip(ip);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100320 XFS_STATS_INC(mp, xs_ig_frecycle);
Dave Chinner24513372014-06-25 14:58:08 +1000321 error = -EAGAIN;
Dave Chinner33479e02012-10-08 21:56:11 +1100322 goto out_error;
323 }
324
325
326 /*
327 * If we are racing with another cache hit that is currently
328 * instantiating this inode or currently recycling it out of
329 * reclaimabe state, wait for the initialisation to complete
330 * before continuing.
331 *
332 * XXX(hch): eventually we should do something equivalent to
333 * wait_on_inode to wait for these flags to be cleared
334 * instead of polling for it.
335 */
336 if (ip->i_flags & (XFS_INEW|XFS_IRECLAIM)) {
337 trace_xfs_iget_skip(ip);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100338 XFS_STATS_INC(mp, xs_ig_frecycle);
Dave Chinner24513372014-06-25 14:58:08 +1000339 error = -EAGAIN;
Dave Chinner33479e02012-10-08 21:56:11 +1100340 goto out_error;
341 }
342
343 /*
344 * If lookup is racing with unlink return an error immediately.
345 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100346 if (VFS_I(ip)->i_mode == 0 && !(flags & XFS_IGET_CREATE)) {
Dave Chinner24513372014-06-25 14:58:08 +1000347 error = -ENOENT;
Dave Chinner33479e02012-10-08 21:56:11 +1100348 goto out_error;
349 }
350
351 /*
352 * If IRECLAIMABLE is set, we've torn down the VFS inode already.
353 * Need to carefully get it back into useable state.
354 */
355 if (ip->i_flags & XFS_IRECLAIMABLE) {
356 trace_xfs_iget_reclaim(ip);
357
358 /*
359 * We need to set XFS_IRECLAIM to prevent xfs_reclaim_inode
360 * from stomping over us while we recycle the inode. We can't
361 * clear the radix tree reclaimable tag yet as it requires
362 * pag_ici_lock to be held exclusive.
363 */
364 ip->i_flags |= XFS_IRECLAIM;
365
366 spin_unlock(&ip->i_flags_lock);
367 rcu_read_unlock();
368
Dave Chinner50997472016-02-09 16:54:58 +1100369 error = xfs_reinit_inode(mp, inode);
Dave Chinner33479e02012-10-08 21:56:11 +1100370 if (error) {
Brian Fostere86b6162017-04-26 08:30:39 -0700371 bool wake;
Dave Chinner33479e02012-10-08 21:56:11 +1100372 /*
373 * Re-initializing the inode failed, and we are in deep
374 * trouble. Try to re-add it to the reclaim list.
375 */
376 rcu_read_lock();
377 spin_lock(&ip->i_flags_lock);
Brian Fostere86b6162017-04-26 08:30:39 -0700378 wake = !!__xfs_iflags_test(ip, XFS_INEW);
Dave Chinner33479e02012-10-08 21:56:11 +1100379 ip->i_flags &= ~(XFS_INEW | XFS_IRECLAIM);
Brian Fostere86b6162017-04-26 08:30:39 -0700380 if (wake)
381 wake_up_bit(&ip->i_flags, __XFS_INEW_BIT);
Dave Chinner33479e02012-10-08 21:56:11 +1100382 ASSERT(ip->i_flags & XFS_IRECLAIMABLE);
383 trace_xfs_iget_reclaim_fail(ip);
384 goto out_error;
385 }
386
387 spin_lock(&pag->pag_ici_lock);
388 spin_lock(&ip->i_flags_lock);
389
390 /*
391 * Clear the per-lifetime state in the inode as we are now
392 * effectively a new inode and need to return to the initial
393 * state before reuse occurs.
394 */
395 ip->i_flags &= ~XFS_IRECLAIM_RESET_FLAGS;
396 ip->i_flags |= XFS_INEW;
Dave Chinner545c0882016-05-18 14:11:41 +1000397 xfs_inode_clear_reclaim_tag(pag, ip->i_ino);
Dave Chinner33479e02012-10-08 21:56:11 +1100398 inode->i_state = I_NEW;
399
400 ASSERT(!rwsem_is_locked(&ip->i_iolock.mr_lock));
401 mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino);
402
403 spin_unlock(&ip->i_flags_lock);
404 spin_unlock(&pag->pag_ici_lock);
405 } else {
406 /* If the VFS inode is being torn down, pause and try again. */
407 if (!igrab(inode)) {
408 trace_xfs_iget_skip(ip);
Dave Chinner24513372014-06-25 14:58:08 +1000409 error = -EAGAIN;
Dave Chinner33479e02012-10-08 21:56:11 +1100410 goto out_error;
411 }
412
413 /* We've got a live one. */
414 spin_unlock(&ip->i_flags_lock);
415 rcu_read_unlock();
416 trace_xfs_iget_hit(ip);
417 }
418
419 if (lock_flags != 0)
420 xfs_ilock(ip, lock_flags);
421
422 xfs_iflags_clear(ip, XFS_ISTALE | XFS_IDONTCACHE);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100423 XFS_STATS_INC(mp, xs_ig_found);
Dave Chinner33479e02012-10-08 21:56:11 +1100424
425 return 0;
426
427out_error:
428 spin_unlock(&ip->i_flags_lock);
429 rcu_read_unlock();
430 return error;
431}
432
433
434static int
435xfs_iget_cache_miss(
436 struct xfs_mount *mp,
437 struct xfs_perag *pag,
438 xfs_trans_t *tp,
439 xfs_ino_t ino,
440 struct xfs_inode **ipp,
441 int flags,
442 int lock_flags)
443{
444 struct xfs_inode *ip;
445 int error;
446 xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino);
447 int iflags;
448
449 ip = xfs_inode_alloc(mp, ino);
450 if (!ip)
Dave Chinner24513372014-06-25 14:58:08 +1000451 return -ENOMEM;
Dave Chinner33479e02012-10-08 21:56:11 +1100452
453 error = xfs_iread(mp, tp, ip, flags);
454 if (error)
455 goto out_destroy;
456
457 trace_xfs_iget_miss(ip);
458
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100459 if ((VFS_I(ip)->i_mode == 0) && !(flags & XFS_IGET_CREATE)) {
Dave Chinner24513372014-06-25 14:58:08 +1000460 error = -ENOENT;
Dave Chinner33479e02012-10-08 21:56:11 +1100461 goto out_destroy;
462 }
463
464 /*
465 * Preload the radix tree so we can insert safely under the
466 * write spinlock. Note that we cannot sleep inside the preload
467 * region. Since we can be called from transaction context, don't
468 * recurse into the file system.
469 */
470 if (radix_tree_preload(GFP_NOFS)) {
Dave Chinner24513372014-06-25 14:58:08 +1000471 error = -EAGAIN;
Dave Chinner33479e02012-10-08 21:56:11 +1100472 goto out_destroy;
473 }
474
475 /*
476 * Because the inode hasn't been added to the radix-tree yet it can't
477 * be found by another thread, so we can do the non-sleeping lock here.
478 */
479 if (lock_flags) {
480 if (!xfs_ilock_nowait(ip, lock_flags))
481 BUG();
482 }
483
484 /*
485 * These values must be set before inserting the inode into the radix
486 * tree as the moment it is inserted a concurrent lookup (allowed by the
487 * RCU locking mechanism) can find it and that lookup must see that this
488 * is an inode currently under construction (i.e. that XFS_INEW is set).
489 * The ip->i_flags_lock that protects the XFS_INEW flag forms the
490 * memory barrier that ensures this detection works correctly at lookup
491 * time.
492 */
493 iflags = XFS_INEW;
494 if (flags & XFS_IGET_DONTCACHE)
495 iflags |= XFS_IDONTCACHE;
Chandra Seetharaman113a5682013-06-27 17:25:07 -0500496 ip->i_udquot = NULL;
497 ip->i_gdquot = NULL;
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500498 ip->i_pdquot = NULL;
Dave Chinner33479e02012-10-08 21:56:11 +1100499 xfs_iflags_set(ip, iflags);
500
501 /* insert the new inode */
502 spin_lock(&pag->pag_ici_lock);
503 error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
504 if (unlikely(error)) {
505 WARN_ON(error != -EEXIST);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100506 XFS_STATS_INC(mp, xs_ig_dup);
Dave Chinner24513372014-06-25 14:58:08 +1000507 error = -EAGAIN;
Dave Chinner33479e02012-10-08 21:56:11 +1100508 goto out_preload_end;
509 }
510 spin_unlock(&pag->pag_ici_lock);
511 radix_tree_preload_end();
512
513 *ipp = ip;
514 return 0;
515
516out_preload_end:
517 spin_unlock(&pag->pag_ici_lock);
518 radix_tree_preload_end();
519 if (lock_flags)
520 xfs_iunlock(ip, lock_flags);
521out_destroy:
522 __destroy_inode(VFS_I(ip));
523 xfs_inode_free(ip);
524 return error;
525}
526
527/*
528 * Look up an inode by number in the given file system.
529 * The inode is looked up in the cache held in each AG.
530 * If the inode is found in the cache, initialise the vfs inode
531 * if necessary.
532 *
533 * If it is not in core, read it in from the file system's device,
534 * add it to the cache and initialise the vfs inode.
535 *
536 * The inode is locked according to the value of the lock_flags parameter.
537 * This flag parameter indicates how and if the inode's IO lock and inode lock
538 * should be taken.
539 *
540 * mp -- the mount point structure for the current file system. It points
541 * to the inode hash table.
542 * tp -- a pointer to the current transaction if there is one. This is
543 * simply passed through to the xfs_iread() call.
544 * ino -- the number of the inode desired. This is the unique identifier
545 * within the file system for the inode being requested.
546 * lock_flags -- flags indicating how to lock the inode. See the comment
547 * for xfs_ilock() for a list of valid values.
548 */
549int
550xfs_iget(
551 xfs_mount_t *mp,
552 xfs_trans_t *tp,
553 xfs_ino_t ino,
554 uint flags,
555 uint lock_flags,
556 xfs_inode_t **ipp)
557{
558 xfs_inode_t *ip;
559 int error;
560 xfs_perag_t *pag;
561 xfs_agino_t agino;
562
563 /*
564 * xfs_reclaim_inode() uses the ILOCK to ensure an inode
565 * doesn't get freed while it's being referenced during a
566 * radix tree traversal here. It assumes this function
567 * aqcuires only the ILOCK (and therefore it has no need to
568 * involve the IOLOCK in this synchronization).
569 */
570 ASSERT((lock_flags & (XFS_IOLOCK_EXCL | XFS_IOLOCK_SHARED)) == 0);
571
572 /* reject inode numbers outside existing AGs */
573 if (!ino || XFS_INO_TO_AGNO(mp, ino) >= mp->m_sb.sb_agcount)
Dave Chinner24513372014-06-25 14:58:08 +1000574 return -EINVAL;
Dave Chinner33479e02012-10-08 21:56:11 +1100575
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100576 XFS_STATS_INC(mp, xs_ig_attempts);
Lucas Stach8774cf82015-08-28 14:50:56 +1000577
Dave Chinner33479e02012-10-08 21:56:11 +1100578 /* get the perag structure and ensure that it's inode capable */
579 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ino));
580 agino = XFS_INO_TO_AGINO(mp, ino);
581
582again:
583 error = 0;
584 rcu_read_lock();
585 ip = radix_tree_lookup(&pag->pag_ici_root, agino);
586
587 if (ip) {
588 error = xfs_iget_cache_hit(pag, ip, ino, flags, lock_flags);
589 if (error)
590 goto out_error_or_again;
591 } else {
592 rcu_read_unlock();
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100593 XFS_STATS_INC(mp, xs_ig_missed);
Dave Chinner33479e02012-10-08 21:56:11 +1100594
595 error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip,
596 flags, lock_flags);
597 if (error)
598 goto out_error_or_again;
599 }
600 xfs_perag_put(pag);
601
602 *ipp = ip;
603
604 /*
Dave Chinner58c90472015-02-23 22:38:08 +1100605 * If we have a real type for an on-disk inode, we can setup the inode
Dave Chinner33479e02012-10-08 21:56:11 +1100606 * now. If it's a new inode being created, xfs_ialloc will handle it.
607 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100608 if (xfs_iflags_test(ip, XFS_INEW) && VFS_I(ip)->i_mode != 0)
Dave Chinner58c90472015-02-23 22:38:08 +1100609 xfs_setup_existing_inode(ip);
Dave Chinner33479e02012-10-08 21:56:11 +1100610 return 0;
611
612out_error_or_again:
Dave Chinner24513372014-06-25 14:58:08 +1000613 if (error == -EAGAIN) {
Dave Chinner33479e02012-10-08 21:56:11 +1100614 delay(1);
615 goto again;
616 }
617 xfs_perag_put(pag);
618 return error;
619}
620
Dave Chinner78ae5252010-09-28 12:28:19 +1000621/*
622 * The inode lookup is done in batches to keep the amount of lock traffic and
623 * radix tree lookups to a minimum. The batch size is a trade off between
624 * lookup reduction and stack usage. This is in the reclaim path, so we can't
625 * be too greedy.
626 */
627#define XFS_LOOKUP_BATCH 32
628
Dave Chinnere13de952010-09-28 12:28:06 +1000629STATIC int
630xfs_inode_ag_walk_grab(
631 struct xfs_inode *ip)
632{
633 struct inode *inode = VFS_I(ip);
634
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100635 ASSERT(rcu_read_lock_held());
636
637 /*
638 * check for stale RCU freed inode
639 *
640 * If the inode has been reallocated, it doesn't matter if it's not in
641 * the AG we are walking - we are walking for writeback, so if it
642 * passes all the "valid inode" checks and is dirty, then we'll write
643 * it back anyway. If it has been reallocated and still being
644 * initialised, the XFS_INEW check below will catch it.
645 */
646 spin_lock(&ip->i_flags_lock);
647 if (!ip->i_ino)
648 goto out_unlock_noent;
649
650 /* avoid new or reclaimable inodes. Leave for reclaim code to flush */
651 if (__xfs_iflags_test(ip, XFS_INEW | XFS_IRECLAIMABLE | XFS_IRECLAIM))
652 goto out_unlock_noent;
653 spin_unlock(&ip->i_flags_lock);
654
Dave Chinnere13de952010-09-28 12:28:06 +1000655 /* nothing to sync during shutdown */
656 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
Dave Chinner24513372014-06-25 14:58:08 +1000657 return -EFSCORRUPTED;
Dave Chinnere13de952010-09-28 12:28:06 +1000658
Dave Chinnere13de952010-09-28 12:28:06 +1000659 /* If we can't grab the inode, it must on it's way to reclaim. */
660 if (!igrab(inode))
Dave Chinner24513372014-06-25 14:58:08 +1000661 return -ENOENT;
Dave Chinnere13de952010-09-28 12:28:06 +1000662
Dave Chinnere13de952010-09-28 12:28:06 +1000663 /* inode is valid */
664 return 0;
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100665
666out_unlock_noent:
667 spin_unlock(&ip->i_flags_lock);
Dave Chinner24513372014-06-25 14:58:08 +1000668 return -ENOENT;
Dave Chinnere13de952010-09-28 12:28:06 +1000669}
670
Dave Chinner75f3cb12009-06-08 15:35:14 +0200671STATIC int
672xfs_inode_ag_walk(
673 struct xfs_mount *mp,
Dave Chinner5017e972010-01-11 11:47:40 +0000674 struct xfs_perag *pag,
Eric Sandeene0094002014-04-14 19:04:19 +1000675 int (*execute)(struct xfs_inode *ip, int flags,
Brian Fostera454f742012-11-06 09:50:39 -0500676 void *args),
677 int flags,
678 void *args,
679 int tag)
Dave Chinner75f3cb12009-06-08 15:35:14 +0200680{
Dave Chinner75f3cb12009-06-08 15:35:14 +0200681 uint32_t first_index;
682 int last_error = 0;
683 int skipped;
Dave Chinner65d0f202010-09-24 18:40:15 +1000684 int done;
Dave Chinner78ae5252010-09-28 12:28:19 +1000685 int nr_found;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200686
687restart:
Dave Chinner65d0f202010-09-24 18:40:15 +1000688 done = 0;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200689 skipped = 0;
690 first_index = 0;
Dave Chinner78ae5252010-09-28 12:28:19 +1000691 nr_found = 0;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200692 do {
Dave Chinner78ae5252010-09-28 12:28:19 +1000693 struct xfs_inode *batch[XFS_LOOKUP_BATCH];
Dave Chinner75f3cb12009-06-08 15:35:14 +0200694 int error = 0;
Dave Chinner78ae5252010-09-28 12:28:19 +1000695 int i;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200696
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100697 rcu_read_lock();
Brian Fostera454f742012-11-06 09:50:39 -0500698
699 if (tag == -1)
700 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
Dave Chinner78ae5252010-09-28 12:28:19 +1000701 (void **)batch, first_index,
702 XFS_LOOKUP_BATCH);
Brian Fostera454f742012-11-06 09:50:39 -0500703 else
704 nr_found = radix_tree_gang_lookup_tag(
705 &pag->pag_ici_root,
706 (void **) batch, first_index,
707 XFS_LOOKUP_BATCH, tag);
708
Dave Chinner65d0f202010-09-24 18:40:15 +1000709 if (!nr_found) {
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100710 rcu_read_unlock();
Dave Chinner75f3cb12009-06-08 15:35:14 +0200711 break;
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000712 }
Dave Chinner75f3cb12009-06-08 15:35:14 +0200713
Dave Chinner65d0f202010-09-24 18:40:15 +1000714 /*
Dave Chinner78ae5252010-09-28 12:28:19 +1000715 * Grab the inodes before we drop the lock. if we found
716 * nothing, nr == 0 and the loop will be skipped.
Dave Chinner65d0f202010-09-24 18:40:15 +1000717 */
Dave Chinner78ae5252010-09-28 12:28:19 +1000718 for (i = 0; i < nr_found; i++) {
719 struct xfs_inode *ip = batch[i];
Dave Chinner65d0f202010-09-24 18:40:15 +1000720
Dave Chinner78ae5252010-09-28 12:28:19 +1000721 if (done || xfs_inode_ag_walk_grab(ip))
722 batch[i] = NULL;
723
724 /*
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100725 * Update the index for the next lookup. Catch
726 * overflows into the next AG range which can occur if
727 * we have inodes in the last block of the AG and we
728 * are currently pointing to the last inode.
729 *
730 * Because we may see inodes that are from the wrong AG
731 * due to RCU freeing and reallocation, only update the
732 * index if it lies in this AG. It was a race that lead
733 * us to see this inode, so another lookup from the
734 * same index will not find it again.
Dave Chinner78ae5252010-09-28 12:28:19 +1000735 */
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100736 if (XFS_INO_TO_AGNO(mp, ip->i_ino) != pag->pag_agno)
737 continue;
Dave Chinner78ae5252010-09-28 12:28:19 +1000738 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
739 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
740 done = 1;
Dave Chinnere13de952010-09-28 12:28:06 +1000741 }
Dave Chinner78ae5252010-09-28 12:28:19 +1000742
743 /* unlock now we've grabbed the inodes. */
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100744 rcu_read_unlock();
Dave Chinnere13de952010-09-28 12:28:06 +1000745
Dave Chinner78ae5252010-09-28 12:28:19 +1000746 for (i = 0; i < nr_found; i++) {
747 if (!batch[i])
748 continue;
Eric Sandeene0094002014-04-14 19:04:19 +1000749 error = execute(batch[i], flags, args);
Dave Chinner78ae5252010-09-28 12:28:19 +1000750 IRELE(batch[i]);
Dave Chinner24513372014-06-25 14:58:08 +1000751 if (error == -EAGAIN) {
Dave Chinner78ae5252010-09-28 12:28:19 +1000752 skipped++;
753 continue;
754 }
Dave Chinner24513372014-06-25 14:58:08 +1000755 if (error && last_error != -EFSCORRUPTED)
Dave Chinner78ae5252010-09-28 12:28:19 +1000756 last_error = error;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200757 }
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000758
759 /* bail out if the filesystem is corrupted. */
Dave Chinner24513372014-06-25 14:58:08 +1000760 if (error == -EFSCORRUPTED)
Dave Chinner75f3cb12009-06-08 15:35:14 +0200761 break;
762
Dave Chinner8daaa832011-07-08 14:14:46 +1000763 cond_resched();
764
Dave Chinner78ae5252010-09-28 12:28:19 +1000765 } while (nr_found && !done);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200766
767 if (skipped) {
768 delay(1);
769 goto restart;
770 }
Dave Chinner75f3cb12009-06-08 15:35:14 +0200771 return last_error;
772}
773
Brian Foster579b62f2012-11-06 09:50:47 -0500774/*
775 * Background scanning to trim post-EOF preallocated space. This is queued
Dwight Engenb9fe5052013-08-15 14:08:02 -0400776 * based on the 'speculative_prealloc_lifetime' tunable (5m by default).
Brian Foster579b62f2012-11-06 09:50:47 -0500777 */
Brian Fosterfa5a4f52016-06-21 11:53:28 +1000778void
Brian Foster579b62f2012-11-06 09:50:47 -0500779xfs_queue_eofblocks(
780 struct xfs_mount *mp)
781{
782 rcu_read_lock();
783 if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_EOFBLOCKS_TAG))
784 queue_delayed_work(mp->m_eofblocks_workqueue,
785 &mp->m_eofblocks_work,
786 msecs_to_jiffies(xfs_eofb_secs * 1000));
787 rcu_read_unlock();
788}
789
790void
791xfs_eofblocks_worker(
792 struct work_struct *work)
793{
794 struct xfs_mount *mp = container_of(to_delayed_work(work),
795 struct xfs_mount, m_eofblocks_work);
796 xfs_icache_free_eofblocks(mp, NULL);
797 xfs_queue_eofblocks(mp);
798}
799
Darrick J. Wong83104d42016-10-03 09:11:46 -0700800/*
801 * Background scanning to trim preallocated CoW space. This is queued
802 * based on the 'speculative_cow_prealloc_lifetime' tunable (5m by default).
803 * (We'll just piggyback on the post-EOF prealloc space workqueue.)
804 */
805STATIC void
806xfs_queue_cowblocks(
807 struct xfs_mount *mp)
808{
809 rcu_read_lock();
810 if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_COWBLOCKS_TAG))
811 queue_delayed_work(mp->m_eofblocks_workqueue,
812 &mp->m_cowblocks_work,
813 msecs_to_jiffies(xfs_cowb_secs * 1000));
814 rcu_read_unlock();
815}
816
817void
818xfs_cowblocks_worker(
819 struct work_struct *work)
820{
821 struct xfs_mount *mp = container_of(to_delayed_work(work),
822 struct xfs_mount, m_cowblocks_work);
823 xfs_icache_free_cowblocks(mp, NULL);
824 xfs_queue_cowblocks(mp);
825}
826
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200827int
Dave Chinner75f3cb12009-06-08 15:35:14 +0200828xfs_inode_ag_iterator(
829 struct xfs_mount *mp,
Eric Sandeene0094002014-04-14 19:04:19 +1000830 int (*execute)(struct xfs_inode *ip, int flags,
Brian Fostera454f742012-11-06 09:50:39 -0500831 void *args),
832 int flags,
833 void *args)
Dave Chinner75f3cb12009-06-08 15:35:14 +0200834{
Dave Chinner16fd5362010-07-20 09:43:39 +1000835 struct xfs_perag *pag;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200836 int error = 0;
837 int last_error = 0;
838 xfs_agnumber_t ag;
839
Dave Chinner16fd5362010-07-20 09:43:39 +1000840 ag = 0;
Dave Chinner65d0f202010-09-24 18:40:15 +1000841 while ((pag = xfs_perag_get(mp, ag))) {
842 ag = pag->pag_agno + 1;
Brian Fostera454f742012-11-06 09:50:39 -0500843 error = xfs_inode_ag_walk(mp, pag, execute, flags, args, -1);
844 xfs_perag_put(pag);
845 if (error) {
846 last_error = error;
Dave Chinner24513372014-06-25 14:58:08 +1000847 if (error == -EFSCORRUPTED)
Brian Fostera454f742012-11-06 09:50:39 -0500848 break;
849 }
850 }
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000851 return last_error;
Brian Fostera454f742012-11-06 09:50:39 -0500852}
853
854int
855xfs_inode_ag_iterator_tag(
856 struct xfs_mount *mp,
Eric Sandeene0094002014-04-14 19:04:19 +1000857 int (*execute)(struct xfs_inode *ip, int flags,
Brian Fostera454f742012-11-06 09:50:39 -0500858 void *args),
859 int flags,
860 void *args,
861 int tag)
862{
863 struct xfs_perag *pag;
864 int error = 0;
865 int last_error = 0;
866 xfs_agnumber_t ag;
867
868 ag = 0;
869 while ((pag = xfs_perag_get_tag(mp, ag, tag))) {
870 ag = pag->pag_agno + 1;
871 error = xfs_inode_ag_walk(mp, pag, execute, flags, args, tag);
Dave Chinner5017e972010-01-11 11:47:40 +0000872 xfs_perag_put(pag);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200873 if (error) {
874 last_error = error;
Dave Chinner24513372014-06-25 14:58:08 +1000875 if (error == -EFSCORRUPTED)
Dave Chinner75f3cb12009-06-08 15:35:14 +0200876 break;
877 }
878 }
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000879 return last_error;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200880}
881
David Chinner76bf1052008-10-30 17:16:21 +1100882/*
Dave Chinnere3a20c02010-09-24 19:51:50 +1000883 * Grab the inode for reclaim exclusively.
884 * Return 0 if we grabbed it, non-zero otherwise.
885 */
886STATIC int
887xfs_reclaim_inode_grab(
888 struct xfs_inode *ip,
889 int flags)
890{
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100891 ASSERT(rcu_read_lock_held());
892
893 /* quick check for stale RCU freed inode */
894 if (!ip->i_ino)
895 return 1;
Dave Chinnere3a20c02010-09-24 19:51:50 +1000896
897 /*
Christoph Hellwig474fce02011-12-18 20:00:09 +0000898 * If we are asked for non-blocking operation, do unlocked checks to
899 * see if the inode already is being flushed or in reclaim to avoid
900 * lock traffic.
Dave Chinnere3a20c02010-09-24 19:51:50 +1000901 */
902 if ((flags & SYNC_TRYLOCK) &&
Christoph Hellwig474fce02011-12-18 20:00:09 +0000903 __xfs_iflags_test(ip, XFS_IFLOCK | XFS_IRECLAIM))
Dave Chinnere3a20c02010-09-24 19:51:50 +1000904 return 1;
Dave Chinnere3a20c02010-09-24 19:51:50 +1000905
906 /*
907 * The radix tree lock here protects a thread in xfs_iget from racing
908 * with us starting reclaim on the inode. Once we have the
909 * XFS_IRECLAIM flag set it will not touch us.
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100910 *
911 * Due to RCU lookup, we may find inodes that have been freed and only
912 * have XFS_IRECLAIM set. Indeed, we may see reallocated inodes that
913 * aren't candidates for reclaim at all, so we must check the
914 * XFS_IRECLAIMABLE is set first before proceeding to reclaim.
Dave Chinnere3a20c02010-09-24 19:51:50 +1000915 */
916 spin_lock(&ip->i_flags_lock);
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100917 if (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) ||
918 __xfs_iflags_test(ip, XFS_IRECLAIM)) {
919 /* not a reclaim candidate. */
Dave Chinnere3a20c02010-09-24 19:51:50 +1000920 spin_unlock(&ip->i_flags_lock);
921 return 1;
922 }
923 __xfs_iflags_set(ip, XFS_IRECLAIM);
924 spin_unlock(&ip->i_flags_lock);
925 return 0;
926}
927
928/*
Christoph Hellwig8a480882012-04-23 15:58:35 +1000929 * Inodes in different states need to be treated differently. The following
930 * table lists the inode states and the reclaim actions necessary:
Dave Chinner777df5a2010-02-06 12:37:26 +1100931 *
932 * inode state iflush ret required action
933 * --------------- ---------- ---------------
934 * bad - reclaim
935 * shutdown EIO unpin and reclaim
936 * clean, unpinned 0 reclaim
937 * stale, unpinned 0 reclaim
Dave Chinnerc8543632010-02-06 12:39:36 +1100938 * clean, pinned(*) 0 requeue
939 * stale, pinned EAGAIN requeue
Christoph Hellwig8a480882012-04-23 15:58:35 +1000940 * dirty, async - requeue
941 * dirty, sync 0 reclaim
Dave Chinner777df5a2010-02-06 12:37:26 +1100942 *
943 * (*) dgc: I don't think the clean, pinned state is possible but it gets
944 * handled anyway given the order of checks implemented.
945 *
Dave Chinnerc8543632010-02-06 12:39:36 +1100946 * Also, because we get the flush lock first, we know that any inode that has
947 * been flushed delwri has had the flush completed by the time we check that
Christoph Hellwig8a480882012-04-23 15:58:35 +1000948 * the inode is clean.
Dave Chinnerc8543632010-02-06 12:39:36 +1100949 *
Christoph Hellwig8a480882012-04-23 15:58:35 +1000950 * Note that because the inode is flushed delayed write by AIL pushing, the
951 * flush lock may already be held here and waiting on it can result in very
952 * long latencies. Hence for sync reclaims, where we wait on the flush lock,
953 * the caller should push the AIL first before trying to reclaim inodes to
954 * minimise the amount of time spent waiting. For background relaim, we only
955 * bother to reclaim clean inodes anyway.
Dave Chinnerc8543632010-02-06 12:39:36 +1100956 *
Dave Chinner777df5a2010-02-06 12:37:26 +1100957 * Hence the order of actions after gaining the locks should be:
958 * bad => reclaim
959 * shutdown => unpin and reclaim
Christoph Hellwig8a480882012-04-23 15:58:35 +1000960 * pinned, async => requeue
Dave Chinnerc8543632010-02-06 12:39:36 +1100961 * pinned, sync => unpin
Dave Chinner777df5a2010-02-06 12:37:26 +1100962 * stale => reclaim
963 * clean => reclaim
Christoph Hellwig8a480882012-04-23 15:58:35 +1000964 * dirty, async => requeue
Dave Chinnerc8543632010-02-06 12:39:36 +1100965 * dirty, sync => flush, wait and reclaim
Dave Chinner777df5a2010-02-06 12:37:26 +1100966 */
Dave Chinner75f3cb12009-06-08 15:35:14 +0200967STATIC int
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000968xfs_reclaim_inode(
Dave Chinner75f3cb12009-06-08 15:35:14 +0200969 struct xfs_inode *ip,
970 struct xfs_perag *pag,
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000971 int sync_mode)
David Chinner7a3be022008-10-30 17:37:37 +1100972{
Christoph Hellwig4c468192012-04-23 15:58:36 +1000973 struct xfs_buf *bp = NULL;
Dave Chinner8a17d7d2016-05-18 14:09:12 +1000974 xfs_ino_t ino = ip->i_ino; /* for radix_tree_delete */
Christoph Hellwig4c468192012-04-23 15:58:36 +1000975 int error;
Dave Chinner777df5a2010-02-06 12:37:26 +1100976
Dave Chinner1bfd8d02011-03-26 09:13:55 +1100977restart:
978 error = 0;
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000979 xfs_ilock(ip, XFS_ILOCK_EXCL);
Dave Chinnerc8543632010-02-06 12:39:36 +1100980 if (!xfs_iflock_nowait(ip)) {
981 if (!(sync_mode & SYNC_WAIT))
982 goto out;
983 xfs_iflock(ip);
984 }
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000985
Dave Chinner777df5a2010-02-06 12:37:26 +1100986 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
987 xfs_iunpin_wait(ip);
Brian Fosterb49ef752017-01-09 16:38:38 +0100988 /* xfs_iflush_abort() drops the flush lock */
Dave Chinner04913fd2012-04-23 15:58:41 +1000989 xfs_iflush_abort(ip, false);
Dave Chinner777df5a2010-02-06 12:37:26 +1100990 goto reclaim;
991 }
Dave Chinnerc8543632010-02-06 12:39:36 +1100992 if (xfs_ipincount(ip)) {
Christoph Hellwig8a480882012-04-23 15:58:35 +1000993 if (!(sync_mode & SYNC_WAIT))
994 goto out_ifunlock;
Dave Chinner777df5a2010-02-06 12:37:26 +1100995 xfs_iunpin_wait(ip);
Dave Chinnerc8543632010-02-06 12:39:36 +1100996 }
Brian Fosterb49ef752017-01-09 16:38:38 +0100997 if (xfs_iflags_test(ip, XFS_ISTALE) || xfs_inode_clean(ip)) {
998 xfs_ifunlock(ip);
Dave Chinner777df5a2010-02-06 12:37:26 +1100999 goto reclaim;
Brian Fosterb49ef752017-01-09 16:38:38 +01001000 }
Dave Chinner777df5a2010-02-06 12:37:26 +11001001
Dave Chinner1bfd8d02011-03-26 09:13:55 +11001002 /*
Christoph Hellwig8a480882012-04-23 15:58:35 +10001003 * Never flush out dirty data during non-blocking reclaim, as it would
1004 * just contend with AIL pushing trying to do the same job.
1005 */
1006 if (!(sync_mode & SYNC_WAIT))
1007 goto out_ifunlock;
1008
1009 /*
Dave Chinner1bfd8d02011-03-26 09:13:55 +11001010 * Now we have an inode that needs flushing.
1011 *
Christoph Hellwig4c468192012-04-23 15:58:36 +10001012 * Note that xfs_iflush will never block on the inode buffer lock, as
Dave Chinner1bfd8d02011-03-26 09:13:55 +11001013 * xfs_ifree_cluster() can lock the inode buffer before it locks the
Christoph Hellwig4c468192012-04-23 15:58:36 +10001014 * ip->i_lock, and we are doing the exact opposite here. As a result,
Christoph Hellwig475ee412012-07-03 12:21:22 -04001015 * doing a blocking xfs_imap_to_bp() to get the cluster buffer would
1016 * result in an ABBA deadlock with xfs_ifree_cluster().
Dave Chinner1bfd8d02011-03-26 09:13:55 +11001017 *
1018 * As xfs_ifree_cluser() must gather all inodes that are active in the
1019 * cache to mark them stale, if we hit this case we don't actually want
1020 * to do IO here - we want the inode marked stale so we can simply
Christoph Hellwig4c468192012-04-23 15:58:36 +10001021 * reclaim it. Hence if we get an EAGAIN error here, just unlock the
1022 * inode, back off and try again. Hopefully the next pass through will
1023 * see the stale flag set on the inode.
Dave Chinner1bfd8d02011-03-26 09:13:55 +11001024 */
Christoph Hellwig4c468192012-04-23 15:58:36 +10001025 error = xfs_iflush(ip, &bp);
Dave Chinner24513372014-06-25 14:58:08 +10001026 if (error == -EAGAIN) {
Christoph Hellwig8a480882012-04-23 15:58:35 +10001027 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1028 /* backoff longer than in xfs_ifree_cluster */
1029 delay(2);
1030 goto restart;
Dave Chinnerc8e20be2010-01-10 23:51:45 +00001031 }
Dave Chinnerc8543632010-02-06 12:39:36 +11001032
Christoph Hellwig4c468192012-04-23 15:58:36 +10001033 if (!error) {
1034 error = xfs_bwrite(bp);
1035 xfs_buf_relse(bp);
1036 }
1037
Dave Chinner777df5a2010-02-06 12:37:26 +11001038reclaim:
Brian Fosterb49ef752017-01-09 16:38:38 +01001039 ASSERT(!xfs_isiflocked(ip));
1040
Dave Chinner8a17d7d2016-05-18 14:09:12 +10001041 /*
1042 * Because we use RCU freeing we need to ensure the inode always appears
1043 * to be reclaimed with an invalid inode number when in the free state.
Brian Fosterb49ef752017-01-09 16:38:38 +01001044 * We do this as early as possible under the ILOCK so that
1045 * xfs_iflush_cluster() can be guaranteed to detect races with us here.
1046 * By doing this, we guarantee that once xfs_iflush_cluster has locked
1047 * XFS_ILOCK that it will see either a valid, flushable inode that will
1048 * serialise correctly, or it will see a clean (and invalid) inode that
1049 * it can skip.
Dave Chinner8a17d7d2016-05-18 14:09:12 +10001050 */
1051 spin_lock(&ip->i_flags_lock);
1052 ip->i_flags = XFS_IRECLAIM;
1053 ip->i_ino = 0;
1054 spin_unlock(&ip->i_flags_lock);
1055
Dave Chinnerc8e20be2010-01-10 23:51:45 +00001056 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner2f11fea2010-07-20 17:53:25 +10001057
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001058 XFS_STATS_INC(ip->i_mount, xs_ig_reclaims);
Dave Chinner2f11fea2010-07-20 17:53:25 +10001059 /*
1060 * Remove the inode from the per-AG radix tree.
1061 *
1062 * Because radix_tree_delete won't complain even if the item was never
1063 * added to the tree assert that it's been there before to catch
1064 * problems with the inode life time early on.
1065 */
Dave Chinner1a427ab2010-12-16 17:08:41 +11001066 spin_lock(&pag->pag_ici_lock);
Dave Chinner2f11fea2010-07-20 17:53:25 +10001067 if (!radix_tree_delete(&pag->pag_ici_root,
Dave Chinner8a17d7d2016-05-18 14:09:12 +10001068 XFS_INO_TO_AGINO(ip->i_mount, ino)))
Dave Chinner2f11fea2010-07-20 17:53:25 +10001069 ASSERT(0);
Dave Chinner545c0882016-05-18 14:11:41 +10001070 xfs_perag_clear_reclaim_tag(pag);
Dave Chinner1a427ab2010-12-16 17:08:41 +11001071 spin_unlock(&pag->pag_ici_lock);
Dave Chinner2f11fea2010-07-20 17:53:25 +10001072
1073 /*
1074 * Here we do an (almost) spurious inode lock in order to coordinate
1075 * with inode cache radix tree lookups. This is because the lookup
1076 * can reference the inodes in the cache without taking references.
1077 *
1078 * We make that OK here by ensuring that we wait until the inode is
Alex Elderad637a12012-02-16 22:01:00 +00001079 * unlocked after the lookup before we go ahead and free it.
Dave Chinner2f11fea2010-07-20 17:53:25 +10001080 */
Alex Elderad637a12012-02-16 22:01:00 +00001081 xfs_ilock(ip, XFS_ILOCK_EXCL);
Dave Chinner2f11fea2010-07-20 17:53:25 +10001082 xfs_qm_dqdetach(ip);
Alex Elderad637a12012-02-16 22:01:00 +00001083 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner2f11fea2010-07-20 17:53:25 +10001084
Dave Chinner8a17d7d2016-05-18 14:09:12 +10001085 __xfs_inode_free(ip);
Alex Elderad637a12012-02-16 22:01:00 +00001086 return error;
Christoph Hellwig8a480882012-04-23 15:58:35 +10001087
1088out_ifunlock:
1089 xfs_ifunlock(ip);
1090out:
1091 xfs_iflags_clear(ip, XFS_IRECLAIM);
1092 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1093 /*
Dave Chinner24513372014-06-25 14:58:08 +10001094 * We could return -EAGAIN here to make reclaim rescan the inode tree in
Christoph Hellwig8a480882012-04-23 15:58:35 +10001095 * a short while. However, this just burns CPU time scanning the tree
Dave Chinner58896082012-10-08 21:56:05 +11001096 * waiting for IO to complete and the reclaim work never goes back to
1097 * the idle state. Instead, return 0 to let the next scheduled
1098 * background reclaim attempt to reclaim the inode again.
Christoph Hellwig8a480882012-04-23 15:58:35 +10001099 */
1100 return 0;
David Chinner7a3be022008-10-30 17:37:37 +11001101}
1102
Dave Chinner65d0f202010-09-24 18:40:15 +10001103/*
1104 * Walk the AGs and reclaim the inodes in them. Even if the filesystem is
1105 * corrupted, we still want to try to reclaim all the inodes. If we don't,
1106 * then a shut down during filesystem unmount reclaim walk leak all the
1107 * unreclaimed inodes.
1108 */
Dave Chinner33479e02012-10-08 21:56:11 +11001109STATIC int
Dave Chinner65d0f202010-09-24 18:40:15 +10001110xfs_reclaim_inodes_ag(
1111 struct xfs_mount *mp,
1112 int flags,
1113 int *nr_to_scan)
1114{
1115 struct xfs_perag *pag;
1116 int error = 0;
1117 int last_error = 0;
1118 xfs_agnumber_t ag;
Dave Chinner69b491c2010-09-27 11:09:51 +10001119 int trylock = flags & SYNC_TRYLOCK;
1120 int skipped;
Dave Chinner65d0f202010-09-24 18:40:15 +10001121
Dave Chinner69b491c2010-09-27 11:09:51 +10001122restart:
Dave Chinner65d0f202010-09-24 18:40:15 +10001123 ag = 0;
Dave Chinner69b491c2010-09-27 11:09:51 +10001124 skipped = 0;
Dave Chinner65d0f202010-09-24 18:40:15 +10001125 while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
1126 unsigned long first_index = 0;
1127 int done = 0;
Dave Chinnere3a20c02010-09-24 19:51:50 +10001128 int nr_found = 0;
Dave Chinner65d0f202010-09-24 18:40:15 +10001129
1130 ag = pag->pag_agno + 1;
1131
Dave Chinner69b491c2010-09-27 11:09:51 +10001132 if (trylock) {
1133 if (!mutex_trylock(&pag->pag_ici_reclaim_lock)) {
1134 skipped++;
Dave Chinnerf83282a2010-11-08 08:55:04 +00001135 xfs_perag_put(pag);
Dave Chinner69b491c2010-09-27 11:09:51 +10001136 continue;
1137 }
1138 first_index = pag->pag_ici_reclaim_cursor;
1139 } else
1140 mutex_lock(&pag->pag_ici_reclaim_lock);
1141
Dave Chinner65d0f202010-09-24 18:40:15 +10001142 do {
Dave Chinnere3a20c02010-09-24 19:51:50 +10001143 struct xfs_inode *batch[XFS_LOOKUP_BATCH];
1144 int i;
Dave Chinner65d0f202010-09-24 18:40:15 +10001145
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001146 rcu_read_lock();
Dave Chinnere3a20c02010-09-24 19:51:50 +10001147 nr_found = radix_tree_gang_lookup_tag(
1148 &pag->pag_ici_root,
1149 (void **)batch, first_index,
1150 XFS_LOOKUP_BATCH,
Dave Chinner65d0f202010-09-24 18:40:15 +10001151 XFS_ICI_RECLAIM_TAG);
1152 if (!nr_found) {
Dave Chinnerb2232212011-05-06 02:54:04 +00001153 done = 1;
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001154 rcu_read_unlock();
Dave Chinner65d0f202010-09-24 18:40:15 +10001155 break;
1156 }
1157
1158 /*
Dave Chinnere3a20c02010-09-24 19:51:50 +10001159 * Grab the inodes before we drop the lock. if we found
1160 * nothing, nr == 0 and the loop will be skipped.
Dave Chinner65d0f202010-09-24 18:40:15 +10001161 */
Dave Chinnere3a20c02010-09-24 19:51:50 +10001162 for (i = 0; i < nr_found; i++) {
1163 struct xfs_inode *ip = batch[i];
Dave Chinner65d0f202010-09-24 18:40:15 +10001164
Dave Chinnere3a20c02010-09-24 19:51:50 +10001165 if (done || xfs_reclaim_inode_grab(ip, flags))
1166 batch[i] = NULL;
Dave Chinner65d0f202010-09-24 18:40:15 +10001167
Dave Chinnere3a20c02010-09-24 19:51:50 +10001168 /*
1169 * Update the index for the next lookup. Catch
1170 * overflows into the next AG range which can
1171 * occur if we have inodes in the last block of
1172 * the AG and we are currently pointing to the
1173 * last inode.
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001174 *
1175 * Because we may see inodes that are from the
1176 * wrong AG due to RCU freeing and
1177 * reallocation, only update the index if it
1178 * lies in this AG. It was a race that lead us
1179 * to see this inode, so another lookup from
1180 * the same index will not find it again.
Dave Chinnere3a20c02010-09-24 19:51:50 +10001181 */
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001182 if (XFS_INO_TO_AGNO(mp, ip->i_ino) !=
1183 pag->pag_agno)
1184 continue;
Dave Chinnere3a20c02010-09-24 19:51:50 +10001185 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
1186 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
1187 done = 1;
1188 }
1189
1190 /* unlock now we've grabbed the inodes. */
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001191 rcu_read_unlock();
Dave Chinnere3a20c02010-09-24 19:51:50 +10001192
1193 for (i = 0; i < nr_found; i++) {
1194 if (!batch[i])
1195 continue;
1196 error = xfs_reclaim_inode(batch[i], pag, flags);
Dave Chinner24513372014-06-25 14:58:08 +10001197 if (error && last_error != -EFSCORRUPTED)
Dave Chinnere3a20c02010-09-24 19:51:50 +10001198 last_error = error;
1199 }
1200
1201 *nr_to_scan -= XFS_LOOKUP_BATCH;
1202
Dave Chinner8daaa832011-07-08 14:14:46 +10001203 cond_resched();
1204
Dave Chinnere3a20c02010-09-24 19:51:50 +10001205 } while (nr_found && !done && *nr_to_scan > 0);
Dave Chinner65d0f202010-09-24 18:40:15 +10001206
Dave Chinner69b491c2010-09-27 11:09:51 +10001207 if (trylock && !done)
1208 pag->pag_ici_reclaim_cursor = first_index;
1209 else
1210 pag->pag_ici_reclaim_cursor = 0;
1211 mutex_unlock(&pag->pag_ici_reclaim_lock);
Dave Chinner65d0f202010-09-24 18:40:15 +10001212 xfs_perag_put(pag);
1213 }
Dave Chinner69b491c2010-09-27 11:09:51 +10001214
1215 /*
1216 * if we skipped any AG, and we still have scan count remaining, do
1217 * another pass this time using blocking reclaim semantics (i.e
1218 * waiting on the reclaim locks and ignoring the reclaim cursors). This
1219 * ensure that when we get more reclaimers than AGs we block rather
1220 * than spin trying to execute reclaim.
1221 */
Dave Chinner8daaa832011-07-08 14:14:46 +10001222 if (skipped && (flags & SYNC_WAIT) && *nr_to_scan > 0) {
Dave Chinner69b491c2010-09-27 11:09:51 +10001223 trylock = 0;
1224 goto restart;
1225 }
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001226 return last_error;
Dave Chinner65d0f202010-09-24 18:40:15 +10001227}
1228
David Chinnerfce08f22008-10-30 17:37:03 +11001229int
David Chinner1dc33182008-10-30 17:37:15 +11001230xfs_reclaim_inodes(
David Chinnerfce08f22008-10-30 17:37:03 +11001231 xfs_mount_t *mp,
David Chinnerfce08f22008-10-30 17:37:03 +11001232 int mode)
1233{
Dave Chinner65d0f202010-09-24 18:40:15 +10001234 int nr_to_scan = INT_MAX;
1235
1236 return xfs_reclaim_inodes_ag(mp, mode, &nr_to_scan);
Dave Chinner9bf729c2010-04-29 09:55:50 +10001237}
1238
1239/*
Dave Chinner8daaa832011-07-08 14:14:46 +10001240 * Scan a certain number of inodes for reclaim.
Dave Chinnera7b339f2011-04-08 12:45:07 +10001241 *
1242 * When called we make sure that there is a background (fast) inode reclaim in
Dave Chinner8daaa832011-07-08 14:14:46 +10001243 * progress, while we will throttle the speed of reclaim via doing synchronous
Dave Chinnera7b339f2011-04-08 12:45:07 +10001244 * reclaim of inodes. That means if we come across dirty inodes, we wait for
1245 * them to be cleaned, which we hope will not be very long due to the
1246 * background walker having already kicked the IO off on those dirty inodes.
Dave Chinner9bf729c2010-04-29 09:55:50 +10001247 */
Dave Chinner0a234c62013-08-28 10:17:57 +10001248long
Dave Chinner8daaa832011-07-08 14:14:46 +10001249xfs_reclaim_inodes_nr(
1250 struct xfs_mount *mp,
1251 int nr_to_scan)
Dave Chinner9bf729c2010-04-29 09:55:50 +10001252{
Dave Chinner8daaa832011-07-08 14:14:46 +10001253 /* kick background reclaimer and push the AIL */
Dave Chinner58896082012-10-08 21:56:05 +11001254 xfs_reclaim_work_queue(mp);
Dave Chinner8daaa832011-07-08 14:14:46 +10001255 xfs_ail_push_all(mp->m_ail);
Dave Chinner9bf729c2010-04-29 09:55:50 +10001256
Dave Chinner0a234c62013-08-28 10:17:57 +10001257 return xfs_reclaim_inodes_ag(mp, SYNC_TRYLOCK | SYNC_WAIT, &nr_to_scan);
Dave Chinner8daaa832011-07-08 14:14:46 +10001258}
Dave Chinnera7b339f2011-04-08 12:45:07 +10001259
Dave Chinner8daaa832011-07-08 14:14:46 +10001260/*
1261 * Return the number of reclaimable inodes in the filesystem for
1262 * the shrinker to determine how much to reclaim.
1263 */
1264int
1265xfs_reclaim_inodes_count(
1266 struct xfs_mount *mp)
1267{
1268 struct xfs_perag *pag;
1269 xfs_agnumber_t ag = 0;
1270 int reclaimable = 0;
Dave Chinner9bf729c2010-04-29 09:55:50 +10001271
Dave Chinner65d0f202010-09-24 18:40:15 +10001272 while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
1273 ag = pag->pag_agno + 1;
Dave Chinner70e60ce2010-07-20 08:07:02 +10001274 reclaimable += pag->pag_ici_reclaimable;
1275 xfs_perag_put(pag);
Dave Chinner9bf729c2010-04-29 09:55:50 +10001276 }
Dave Chinner9bf729c2010-04-29 09:55:50 +10001277 return reclaimable;
1278}
1279
Brian Foster41176a62012-11-06 09:50:42 -05001280STATIC int
Brian Foster3e3f9f52012-11-07 12:21:13 -05001281xfs_inode_match_id(
1282 struct xfs_inode *ip,
1283 struct xfs_eofblocks *eofb)
1284{
Dwight Engenb9fe5052013-08-15 14:08:02 -04001285 if ((eofb->eof_flags & XFS_EOF_FLAGS_UID) &&
1286 !uid_eq(VFS_I(ip)->i_uid, eofb->eof_uid))
Brian Foster1b556042012-11-06 09:50:45 -05001287 return 0;
Brian Foster3e3f9f52012-11-07 12:21:13 -05001288
Dwight Engenb9fe5052013-08-15 14:08:02 -04001289 if ((eofb->eof_flags & XFS_EOF_FLAGS_GID) &&
1290 !gid_eq(VFS_I(ip)->i_gid, eofb->eof_gid))
Brian Foster1b556042012-11-06 09:50:45 -05001291 return 0;
1292
Dwight Engenb9fe5052013-08-15 14:08:02 -04001293 if ((eofb->eof_flags & XFS_EOF_FLAGS_PRID) &&
Brian Foster1b556042012-11-06 09:50:45 -05001294 xfs_get_projid(ip) != eofb->eof_prid)
1295 return 0;
1296
1297 return 1;
Brian Foster3e3f9f52012-11-07 12:21:13 -05001298}
1299
Brian Fosterf4526392014-07-24 19:44:28 +10001300/*
1301 * A union-based inode filtering algorithm. Process the inode if any of the
1302 * criteria match. This is for global/internal scans only.
1303 */
1304STATIC int
1305xfs_inode_match_id_union(
1306 struct xfs_inode *ip,
1307 struct xfs_eofblocks *eofb)
1308{
1309 if ((eofb->eof_flags & XFS_EOF_FLAGS_UID) &&
1310 uid_eq(VFS_I(ip)->i_uid, eofb->eof_uid))
1311 return 1;
1312
1313 if ((eofb->eof_flags & XFS_EOF_FLAGS_GID) &&
1314 gid_eq(VFS_I(ip)->i_gid, eofb->eof_gid))
1315 return 1;
1316
1317 if ((eofb->eof_flags & XFS_EOF_FLAGS_PRID) &&
1318 xfs_get_projid(ip) == eofb->eof_prid)
1319 return 1;
1320
1321 return 0;
1322}
1323
Brian Foster3e3f9f52012-11-07 12:21:13 -05001324STATIC int
Brian Foster41176a62012-11-06 09:50:42 -05001325xfs_inode_free_eofblocks(
1326 struct xfs_inode *ip,
Brian Foster41176a62012-11-06 09:50:42 -05001327 int flags,
1328 void *args)
1329{
Brian Foster798b1dc2017-01-27 23:22:55 -08001330 int ret = 0;
Brian Foster3e3f9f52012-11-07 12:21:13 -05001331 struct xfs_eofblocks *eofb = args;
Brian Fosterf4526392014-07-24 19:44:28 +10001332 int match;
Brian Foster5400da72014-07-24 19:40:22 +10001333
Brian Foster41176a62012-11-06 09:50:42 -05001334 if (!xfs_can_free_eofblocks(ip, false)) {
1335 /* inode could be preallocated or append-only */
1336 trace_xfs_inode_free_eofblocks_invalid(ip);
1337 xfs_inode_clear_eofblocks_tag(ip);
1338 return 0;
1339 }
1340
1341 /*
1342 * If the mapping is dirty the operation can block and wait for some
1343 * time. Unless we are waiting, skip it.
1344 */
1345 if (!(flags & SYNC_WAIT) &&
1346 mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_DIRTY))
1347 return 0;
1348
Brian Foster00ca79a2012-11-07 12:21:14 -05001349 if (eofb) {
Brian Fosterf4526392014-07-24 19:44:28 +10001350 if (eofb->eof_flags & XFS_EOF_FLAGS_UNION)
1351 match = xfs_inode_match_id_union(ip, eofb);
1352 else
1353 match = xfs_inode_match_id(ip, eofb);
1354 if (!match)
Brian Foster00ca79a2012-11-07 12:21:14 -05001355 return 0;
1356
1357 /* skip the inode if the file size is too small */
1358 if (eofb->eof_flags & XFS_EOF_FLAGS_MINFILESIZE &&
1359 XFS_ISIZE(ip) < eofb->eof_min_file_size)
1360 return 0;
1361 }
Brian Foster3e3f9f52012-11-07 12:21:13 -05001362
Brian Foster798b1dc2017-01-27 23:22:55 -08001363 /*
1364 * If the caller is waiting, return -EAGAIN to keep the background
1365 * scanner moving and revisit the inode in a subsequent pass.
1366 */
Brian Foster4d725d72017-01-27 23:22:56 -08001367 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
Brian Foster798b1dc2017-01-27 23:22:55 -08001368 if (flags & SYNC_WAIT)
1369 ret = -EAGAIN;
1370 return ret;
1371 }
1372 ret = xfs_free_eofblocks(ip);
Brian Foster4d725d72017-01-27 23:22:56 -08001373 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Brian Foster41176a62012-11-06 09:50:42 -05001374
1375 return ret;
1376}
1377
Darrick J. Wong83104d42016-10-03 09:11:46 -07001378static int
1379__xfs_icache_free_eofblocks(
Brian Foster41176a62012-11-06 09:50:42 -05001380 struct xfs_mount *mp,
Darrick J. Wong83104d42016-10-03 09:11:46 -07001381 struct xfs_eofblocks *eofb,
1382 int (*execute)(struct xfs_inode *ip, int flags,
1383 void *args),
1384 int tag)
Brian Foster41176a62012-11-06 09:50:42 -05001385{
Brian Foster8ca149d2012-11-07 12:21:12 -05001386 int flags = SYNC_TRYLOCK;
1387
1388 if (eofb && (eofb->eof_flags & XFS_EOF_FLAGS_SYNC))
1389 flags = SYNC_WAIT;
1390
Darrick J. Wong83104d42016-10-03 09:11:46 -07001391 return xfs_inode_ag_iterator_tag(mp, execute, flags,
1392 eofb, tag);
1393}
1394
1395int
1396xfs_icache_free_eofblocks(
1397 struct xfs_mount *mp,
1398 struct xfs_eofblocks *eofb)
1399{
1400 return __xfs_icache_free_eofblocks(mp, eofb, xfs_inode_free_eofblocks,
1401 XFS_ICI_EOFBLOCKS_TAG);
Brian Foster41176a62012-11-06 09:50:42 -05001402}
1403
Brian Fosterdc06f3982014-07-24 19:49:28 +10001404/*
1405 * Run eofblocks scans on the quotas applicable to the inode. For inodes with
1406 * multiple quotas, we don't know exactly which quota caused an allocation
1407 * failure. We make a best effort by including each quota under low free space
1408 * conditions (less than 1% free space) in the scan.
1409 */
Darrick J. Wong83104d42016-10-03 09:11:46 -07001410static int
1411__xfs_inode_free_quota_eofblocks(
1412 struct xfs_inode *ip,
1413 int (*execute)(struct xfs_mount *mp,
1414 struct xfs_eofblocks *eofb))
Brian Fosterdc06f3982014-07-24 19:49:28 +10001415{
1416 int scan = 0;
1417 struct xfs_eofblocks eofb = {0};
1418 struct xfs_dquot *dq;
1419
Brian Fosterdc06f3982014-07-24 19:49:28 +10001420 /*
Brian Foster4d725d72017-01-27 23:22:56 -08001421 * Run a sync scan to increase effectiveness and use the union filter to
Brian Fosterdc06f3982014-07-24 19:49:28 +10001422 * cover all applicable quotas in a single scan.
1423 */
Brian Fosterdc06f3982014-07-24 19:49:28 +10001424 eofb.eof_flags = XFS_EOF_FLAGS_UNION|XFS_EOF_FLAGS_SYNC;
1425
1426 if (XFS_IS_UQUOTA_ENFORCED(ip->i_mount)) {
1427 dq = xfs_inode_dquot(ip, XFS_DQ_USER);
1428 if (dq && xfs_dquot_lowsp(dq)) {
1429 eofb.eof_uid = VFS_I(ip)->i_uid;
1430 eofb.eof_flags |= XFS_EOF_FLAGS_UID;
1431 scan = 1;
1432 }
1433 }
1434
1435 if (XFS_IS_GQUOTA_ENFORCED(ip->i_mount)) {
1436 dq = xfs_inode_dquot(ip, XFS_DQ_GROUP);
1437 if (dq && xfs_dquot_lowsp(dq)) {
1438 eofb.eof_gid = VFS_I(ip)->i_gid;
1439 eofb.eof_flags |= XFS_EOF_FLAGS_GID;
1440 scan = 1;
1441 }
1442 }
1443
1444 if (scan)
Darrick J. Wong83104d42016-10-03 09:11:46 -07001445 execute(ip->i_mount, &eofb);
Brian Fosterdc06f3982014-07-24 19:49:28 +10001446
1447 return scan;
1448}
1449
Darrick J. Wong83104d42016-10-03 09:11:46 -07001450int
1451xfs_inode_free_quota_eofblocks(
1452 struct xfs_inode *ip)
1453{
1454 return __xfs_inode_free_quota_eofblocks(ip, xfs_icache_free_eofblocks);
1455}
1456
1457static void
1458__xfs_inode_set_eofblocks_tag(
1459 xfs_inode_t *ip,
1460 void (*execute)(struct xfs_mount *mp),
1461 void (*set_tp)(struct xfs_mount *mp, xfs_agnumber_t agno,
1462 int error, unsigned long caller_ip),
1463 int tag)
Brian Foster27b52862012-11-06 09:50:38 -05001464{
1465 struct xfs_mount *mp = ip->i_mount;
1466 struct xfs_perag *pag;
1467 int tagged;
1468
Christoph Hellwig85a6e762016-09-19 11:09:48 +10001469 /*
1470 * Don't bother locking the AG and looking up in the radix trees
1471 * if we already know that we have the tag set.
1472 */
1473 if (ip->i_flags & XFS_IEOFBLOCKS)
1474 return;
1475 spin_lock(&ip->i_flags_lock);
1476 ip->i_flags |= XFS_IEOFBLOCKS;
1477 spin_unlock(&ip->i_flags_lock);
1478
Brian Foster27b52862012-11-06 09:50:38 -05001479 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
1480 spin_lock(&pag->pag_ici_lock);
Brian Foster27b52862012-11-06 09:50:38 -05001481
Darrick J. Wong83104d42016-10-03 09:11:46 -07001482 tagged = radix_tree_tagged(&pag->pag_ici_root, tag);
Brian Foster27b52862012-11-06 09:50:38 -05001483 radix_tree_tag_set(&pag->pag_ici_root,
Darrick J. Wong83104d42016-10-03 09:11:46 -07001484 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino), tag);
Brian Foster27b52862012-11-06 09:50:38 -05001485 if (!tagged) {
1486 /* propagate the eofblocks tag up into the perag radix tree */
1487 spin_lock(&ip->i_mount->m_perag_lock);
1488 radix_tree_tag_set(&ip->i_mount->m_perag_tree,
1489 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
Darrick J. Wong83104d42016-10-03 09:11:46 -07001490 tag);
Brian Foster27b52862012-11-06 09:50:38 -05001491 spin_unlock(&ip->i_mount->m_perag_lock);
1492
Brian Foster579b62f2012-11-06 09:50:47 -05001493 /* kick off background trimming */
Darrick J. Wong83104d42016-10-03 09:11:46 -07001494 execute(ip->i_mount);
Brian Foster579b62f2012-11-06 09:50:47 -05001495
Darrick J. Wong83104d42016-10-03 09:11:46 -07001496 set_tp(ip->i_mount, pag->pag_agno, -1, _RET_IP_);
Brian Foster27b52862012-11-06 09:50:38 -05001497 }
1498
1499 spin_unlock(&pag->pag_ici_lock);
1500 xfs_perag_put(pag);
1501}
1502
1503void
Darrick J. Wong83104d42016-10-03 09:11:46 -07001504xfs_inode_set_eofblocks_tag(
Brian Foster27b52862012-11-06 09:50:38 -05001505 xfs_inode_t *ip)
1506{
Darrick J. Wong83104d42016-10-03 09:11:46 -07001507 trace_xfs_inode_set_eofblocks_tag(ip);
1508 return __xfs_inode_set_eofblocks_tag(ip, xfs_queue_eofblocks,
1509 trace_xfs_perag_set_eofblocks,
1510 XFS_ICI_EOFBLOCKS_TAG);
1511}
1512
1513static void
1514__xfs_inode_clear_eofblocks_tag(
1515 xfs_inode_t *ip,
1516 void (*clear_tp)(struct xfs_mount *mp, xfs_agnumber_t agno,
1517 int error, unsigned long caller_ip),
1518 int tag)
1519{
Brian Foster27b52862012-11-06 09:50:38 -05001520 struct xfs_mount *mp = ip->i_mount;
1521 struct xfs_perag *pag;
1522
Christoph Hellwig85a6e762016-09-19 11:09:48 +10001523 spin_lock(&ip->i_flags_lock);
1524 ip->i_flags &= ~XFS_IEOFBLOCKS;
1525 spin_unlock(&ip->i_flags_lock);
1526
Brian Foster27b52862012-11-06 09:50:38 -05001527 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
1528 spin_lock(&pag->pag_ici_lock);
Brian Foster27b52862012-11-06 09:50:38 -05001529
1530 radix_tree_tag_clear(&pag->pag_ici_root,
Darrick J. Wong83104d42016-10-03 09:11:46 -07001531 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino), tag);
1532 if (!radix_tree_tagged(&pag->pag_ici_root, tag)) {
Brian Foster27b52862012-11-06 09:50:38 -05001533 /* clear the eofblocks tag from the perag radix tree */
1534 spin_lock(&ip->i_mount->m_perag_lock);
1535 radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
1536 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
Darrick J. Wong83104d42016-10-03 09:11:46 -07001537 tag);
Brian Foster27b52862012-11-06 09:50:38 -05001538 spin_unlock(&ip->i_mount->m_perag_lock);
Darrick J. Wong83104d42016-10-03 09:11:46 -07001539 clear_tp(ip->i_mount, pag->pag_agno, -1, _RET_IP_);
Brian Foster27b52862012-11-06 09:50:38 -05001540 }
1541
1542 spin_unlock(&pag->pag_ici_lock);
1543 xfs_perag_put(pag);
1544}
1545
Darrick J. Wong83104d42016-10-03 09:11:46 -07001546void
1547xfs_inode_clear_eofblocks_tag(
1548 xfs_inode_t *ip)
1549{
1550 trace_xfs_inode_clear_eofblocks_tag(ip);
1551 return __xfs_inode_clear_eofblocks_tag(ip,
1552 trace_xfs_perag_clear_eofblocks, XFS_ICI_EOFBLOCKS_TAG);
1553}
1554
1555/*
1556 * Automatic CoW Reservation Freeing
1557 *
1558 * These functions automatically garbage collect leftover CoW reservations
1559 * that were made on behalf of a cowextsize hint when we start to run out
1560 * of quota or when the reservations sit around for too long. If the file
1561 * has dirty pages or is undergoing writeback, its CoW reservations will
1562 * be retained.
1563 *
1564 * The actual garbage collection piggybacks off the same code that runs
1565 * the speculative EOF preallocation garbage collector.
1566 */
1567STATIC int
1568xfs_inode_free_cowblocks(
1569 struct xfs_inode *ip,
1570 int flags,
1571 void *args)
1572{
1573 int ret;
1574 struct xfs_eofblocks *eofb = args;
Darrick J. Wong83104d42016-10-03 09:11:46 -07001575 int match;
Brian Foster2f092422017-01-09 16:38:34 +01001576 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
Darrick J. Wong83104d42016-10-03 09:11:46 -07001577
Brian Foster2f092422017-01-09 16:38:34 +01001578 /*
1579 * Just clear the tag if we have an empty cow fork or none at all. It's
1580 * possible the inode was fully unshared since it was originally tagged.
1581 */
1582 if (!xfs_is_reflink_inode(ip) || !ifp->if_bytes) {
Darrick J. Wong83104d42016-10-03 09:11:46 -07001583 trace_xfs_inode_free_cowblocks_invalid(ip);
1584 xfs_inode_clear_cowblocks_tag(ip);
1585 return 0;
1586 }
1587
1588 /*
1589 * If the mapping is dirty or under writeback we cannot touch the
1590 * CoW fork. Leave it alone if we're in the midst of a directio.
1591 */
Christoph Hellwig91192ae2017-01-09 16:39:02 +01001592 if ((VFS_I(ip)->i_state & I_DIRTY_PAGES) ||
1593 mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_DIRTY) ||
Darrick J. Wong83104d42016-10-03 09:11:46 -07001594 mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_WRITEBACK) ||
1595 atomic_read(&VFS_I(ip)->i_dio_count))
1596 return 0;
1597
1598 if (eofb) {
1599 if (eofb->eof_flags & XFS_EOF_FLAGS_UNION)
1600 match = xfs_inode_match_id_union(ip, eofb);
1601 else
1602 match = xfs_inode_match_id(ip, eofb);
1603 if (!match)
1604 return 0;
1605
1606 /* skip the inode if the file size is too small */
1607 if (eofb->eof_flags & XFS_EOF_FLAGS_MINFILESIZE &&
1608 XFS_ISIZE(ip) < eofb->eof_min_file_size)
1609 return 0;
Darrick J. Wong83104d42016-10-03 09:11:46 -07001610 }
1611
1612 /* Free the CoW blocks */
Brian Foster4d725d72017-01-27 23:22:56 -08001613 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1614 xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
Darrick J. Wong83104d42016-10-03 09:11:46 -07001615
Christoph Hellwig3b83a022017-03-07 16:45:58 -08001616 ret = xfs_reflink_cancel_cow_range(ip, 0, NULLFILEOFF, false);
Darrick J. Wong83104d42016-10-03 09:11:46 -07001617
Brian Foster4d725d72017-01-27 23:22:56 -08001618 xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
1619 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Darrick J. Wong83104d42016-10-03 09:11:46 -07001620
1621 return ret;
1622}
1623
1624int
1625xfs_icache_free_cowblocks(
1626 struct xfs_mount *mp,
1627 struct xfs_eofblocks *eofb)
1628{
1629 return __xfs_icache_free_eofblocks(mp, eofb, xfs_inode_free_cowblocks,
1630 XFS_ICI_COWBLOCKS_TAG);
1631}
1632
1633int
1634xfs_inode_free_quota_cowblocks(
1635 struct xfs_inode *ip)
1636{
1637 return __xfs_inode_free_quota_eofblocks(ip, xfs_icache_free_cowblocks);
1638}
1639
1640void
1641xfs_inode_set_cowblocks_tag(
1642 xfs_inode_t *ip)
1643{
Brian Foster7b7381f2016-10-24 14:21:00 +11001644 trace_xfs_inode_set_cowblocks_tag(ip);
Darrick J. Wong83104d42016-10-03 09:11:46 -07001645 return __xfs_inode_set_eofblocks_tag(ip, xfs_queue_cowblocks,
Brian Foster7b7381f2016-10-24 14:21:00 +11001646 trace_xfs_perag_set_cowblocks,
Darrick J. Wong83104d42016-10-03 09:11:46 -07001647 XFS_ICI_COWBLOCKS_TAG);
1648}
1649
1650void
1651xfs_inode_clear_cowblocks_tag(
1652 xfs_inode_t *ip)
1653{
Brian Foster7b7381f2016-10-24 14:21:00 +11001654 trace_xfs_inode_clear_cowblocks_tag(ip);
Darrick J. Wong83104d42016-10-03 09:11:46 -07001655 return __xfs_inode_clear_eofblocks_tag(ip,
Brian Foster7b7381f2016-10-24 14:21:00 +11001656 trace_xfs_perag_clear_cowblocks, XFS_ICI_COWBLOCKS_TAG);
Darrick J. Wong83104d42016-10-03 09:11:46 -07001657}