blob: 29cc9886a3cb6e7fb14b1f4097f898e322f39c3b [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) {
371 /*
372 * Re-initializing the inode failed, and we are in deep
373 * trouble. Try to re-add it to the reclaim list.
374 */
375 rcu_read_lock();
376 spin_lock(&ip->i_flags_lock);
377
378 ip->i_flags &= ~(XFS_INEW | XFS_IRECLAIM);
379 ASSERT(ip->i_flags & XFS_IRECLAIMABLE);
380 trace_xfs_iget_reclaim_fail(ip);
381 goto out_error;
382 }
383
384 spin_lock(&pag->pag_ici_lock);
385 spin_lock(&ip->i_flags_lock);
386
387 /*
388 * Clear the per-lifetime state in the inode as we are now
389 * effectively a new inode and need to return to the initial
390 * state before reuse occurs.
391 */
392 ip->i_flags &= ~XFS_IRECLAIM_RESET_FLAGS;
393 ip->i_flags |= XFS_INEW;
Dave Chinner545c0882016-05-18 14:11:41 +1000394 xfs_inode_clear_reclaim_tag(pag, ip->i_ino);
Dave Chinner33479e02012-10-08 21:56:11 +1100395 inode->i_state = I_NEW;
396
397 ASSERT(!rwsem_is_locked(&ip->i_iolock.mr_lock));
398 mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino);
399
400 spin_unlock(&ip->i_flags_lock);
401 spin_unlock(&pag->pag_ici_lock);
402 } else {
403 /* If the VFS inode is being torn down, pause and try again. */
404 if (!igrab(inode)) {
405 trace_xfs_iget_skip(ip);
Dave Chinner24513372014-06-25 14:58:08 +1000406 error = -EAGAIN;
Dave Chinner33479e02012-10-08 21:56:11 +1100407 goto out_error;
408 }
409
410 /* We've got a live one. */
411 spin_unlock(&ip->i_flags_lock);
412 rcu_read_unlock();
413 trace_xfs_iget_hit(ip);
414 }
415
416 if (lock_flags != 0)
417 xfs_ilock(ip, lock_flags);
418
419 xfs_iflags_clear(ip, XFS_ISTALE | XFS_IDONTCACHE);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100420 XFS_STATS_INC(mp, xs_ig_found);
Dave Chinner33479e02012-10-08 21:56:11 +1100421
422 return 0;
423
424out_error:
425 spin_unlock(&ip->i_flags_lock);
426 rcu_read_unlock();
427 return error;
428}
429
430
431static int
432xfs_iget_cache_miss(
433 struct xfs_mount *mp,
434 struct xfs_perag *pag,
435 xfs_trans_t *tp,
436 xfs_ino_t ino,
437 struct xfs_inode **ipp,
438 int flags,
439 int lock_flags)
440{
441 struct xfs_inode *ip;
442 int error;
443 xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino);
444 int iflags;
445
446 ip = xfs_inode_alloc(mp, ino);
447 if (!ip)
Dave Chinner24513372014-06-25 14:58:08 +1000448 return -ENOMEM;
Dave Chinner33479e02012-10-08 21:56:11 +1100449
450 error = xfs_iread(mp, tp, ip, flags);
451 if (error)
452 goto out_destroy;
453
454 trace_xfs_iget_miss(ip);
455
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100456 if ((VFS_I(ip)->i_mode == 0) && !(flags & XFS_IGET_CREATE)) {
Dave Chinner24513372014-06-25 14:58:08 +1000457 error = -ENOENT;
Dave Chinner33479e02012-10-08 21:56:11 +1100458 goto out_destroy;
459 }
460
461 /*
462 * Preload the radix tree so we can insert safely under the
463 * write spinlock. Note that we cannot sleep inside the preload
464 * region. Since we can be called from transaction context, don't
465 * recurse into the file system.
466 */
467 if (radix_tree_preload(GFP_NOFS)) {
Dave Chinner24513372014-06-25 14:58:08 +1000468 error = -EAGAIN;
Dave Chinner33479e02012-10-08 21:56:11 +1100469 goto out_destroy;
470 }
471
472 /*
473 * Because the inode hasn't been added to the radix-tree yet it can't
474 * be found by another thread, so we can do the non-sleeping lock here.
475 */
476 if (lock_flags) {
477 if (!xfs_ilock_nowait(ip, lock_flags))
478 BUG();
479 }
480
481 /*
482 * These values must be set before inserting the inode into the radix
483 * tree as the moment it is inserted a concurrent lookup (allowed by the
484 * RCU locking mechanism) can find it and that lookup must see that this
485 * is an inode currently under construction (i.e. that XFS_INEW is set).
486 * The ip->i_flags_lock that protects the XFS_INEW flag forms the
487 * memory barrier that ensures this detection works correctly at lookup
488 * time.
489 */
490 iflags = XFS_INEW;
491 if (flags & XFS_IGET_DONTCACHE)
492 iflags |= XFS_IDONTCACHE;
Chandra Seetharaman113a5682013-06-27 17:25:07 -0500493 ip->i_udquot = NULL;
494 ip->i_gdquot = NULL;
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500495 ip->i_pdquot = NULL;
Dave Chinner33479e02012-10-08 21:56:11 +1100496 xfs_iflags_set(ip, iflags);
497
498 /* insert the new inode */
499 spin_lock(&pag->pag_ici_lock);
500 error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
501 if (unlikely(error)) {
502 WARN_ON(error != -EEXIST);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100503 XFS_STATS_INC(mp, xs_ig_dup);
Dave Chinner24513372014-06-25 14:58:08 +1000504 error = -EAGAIN;
Dave Chinner33479e02012-10-08 21:56:11 +1100505 goto out_preload_end;
506 }
507 spin_unlock(&pag->pag_ici_lock);
508 radix_tree_preload_end();
509
510 *ipp = ip;
511 return 0;
512
513out_preload_end:
514 spin_unlock(&pag->pag_ici_lock);
515 radix_tree_preload_end();
516 if (lock_flags)
517 xfs_iunlock(ip, lock_flags);
518out_destroy:
519 __destroy_inode(VFS_I(ip));
520 xfs_inode_free(ip);
521 return error;
522}
523
524/*
525 * Look up an inode by number in the given file system.
526 * The inode is looked up in the cache held in each AG.
527 * If the inode is found in the cache, initialise the vfs inode
528 * if necessary.
529 *
530 * If it is not in core, read it in from the file system's device,
531 * add it to the cache and initialise the vfs inode.
532 *
533 * The inode is locked according to the value of the lock_flags parameter.
534 * This flag parameter indicates how and if the inode's IO lock and inode lock
535 * should be taken.
536 *
537 * mp -- the mount point structure for the current file system. It points
538 * to the inode hash table.
539 * tp -- a pointer to the current transaction if there is one. This is
540 * simply passed through to the xfs_iread() call.
541 * ino -- the number of the inode desired. This is the unique identifier
542 * within the file system for the inode being requested.
543 * lock_flags -- flags indicating how to lock the inode. See the comment
544 * for xfs_ilock() for a list of valid values.
545 */
546int
547xfs_iget(
548 xfs_mount_t *mp,
549 xfs_trans_t *tp,
550 xfs_ino_t ino,
551 uint flags,
552 uint lock_flags,
553 xfs_inode_t **ipp)
554{
555 xfs_inode_t *ip;
556 int error;
557 xfs_perag_t *pag;
558 xfs_agino_t agino;
559
560 /*
561 * xfs_reclaim_inode() uses the ILOCK to ensure an inode
562 * doesn't get freed while it's being referenced during a
563 * radix tree traversal here. It assumes this function
564 * aqcuires only the ILOCK (and therefore it has no need to
565 * involve the IOLOCK in this synchronization).
566 */
567 ASSERT((lock_flags & (XFS_IOLOCK_EXCL | XFS_IOLOCK_SHARED)) == 0);
568
569 /* reject inode numbers outside existing AGs */
570 if (!ino || XFS_INO_TO_AGNO(mp, ino) >= mp->m_sb.sb_agcount)
Dave Chinner24513372014-06-25 14:58:08 +1000571 return -EINVAL;
Dave Chinner33479e02012-10-08 21:56:11 +1100572
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100573 XFS_STATS_INC(mp, xs_ig_attempts);
Lucas Stach8774cf82015-08-28 14:50:56 +1000574
Dave Chinner33479e02012-10-08 21:56:11 +1100575 /* get the perag structure and ensure that it's inode capable */
576 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ino));
577 agino = XFS_INO_TO_AGINO(mp, ino);
578
579again:
580 error = 0;
581 rcu_read_lock();
582 ip = radix_tree_lookup(&pag->pag_ici_root, agino);
583
584 if (ip) {
585 error = xfs_iget_cache_hit(pag, ip, ino, flags, lock_flags);
586 if (error)
587 goto out_error_or_again;
588 } else {
589 rcu_read_unlock();
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100590 XFS_STATS_INC(mp, xs_ig_missed);
Dave Chinner33479e02012-10-08 21:56:11 +1100591
592 error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip,
593 flags, lock_flags);
594 if (error)
595 goto out_error_or_again;
596 }
597 xfs_perag_put(pag);
598
599 *ipp = ip;
600
601 /*
Dave Chinner58c90472015-02-23 22:38:08 +1100602 * If we have a real type for an on-disk inode, we can setup the inode
Dave Chinner33479e02012-10-08 21:56:11 +1100603 * now. If it's a new inode being created, xfs_ialloc will handle it.
604 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100605 if (xfs_iflags_test(ip, XFS_INEW) && VFS_I(ip)->i_mode != 0)
Dave Chinner58c90472015-02-23 22:38:08 +1100606 xfs_setup_existing_inode(ip);
Dave Chinner33479e02012-10-08 21:56:11 +1100607 return 0;
608
609out_error_or_again:
Dave Chinner24513372014-06-25 14:58:08 +1000610 if (error == -EAGAIN) {
Dave Chinner33479e02012-10-08 21:56:11 +1100611 delay(1);
612 goto again;
613 }
614 xfs_perag_put(pag);
615 return error;
616}
617
Dave Chinner78ae5252010-09-28 12:28:19 +1000618/*
619 * The inode lookup is done in batches to keep the amount of lock traffic and
620 * radix tree lookups to a minimum. The batch size is a trade off between
621 * lookup reduction and stack usage. This is in the reclaim path, so we can't
622 * be too greedy.
623 */
624#define XFS_LOOKUP_BATCH 32
625
Dave Chinnere13de952010-09-28 12:28:06 +1000626STATIC int
627xfs_inode_ag_walk_grab(
628 struct xfs_inode *ip)
629{
630 struct inode *inode = VFS_I(ip);
631
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100632 ASSERT(rcu_read_lock_held());
633
634 /*
635 * check for stale RCU freed inode
636 *
637 * If the inode has been reallocated, it doesn't matter if it's not in
638 * the AG we are walking - we are walking for writeback, so if it
639 * passes all the "valid inode" checks and is dirty, then we'll write
640 * it back anyway. If it has been reallocated and still being
641 * initialised, the XFS_INEW check below will catch it.
642 */
643 spin_lock(&ip->i_flags_lock);
644 if (!ip->i_ino)
645 goto out_unlock_noent;
646
647 /* avoid new or reclaimable inodes. Leave for reclaim code to flush */
648 if (__xfs_iflags_test(ip, XFS_INEW | XFS_IRECLAIMABLE | XFS_IRECLAIM))
649 goto out_unlock_noent;
650 spin_unlock(&ip->i_flags_lock);
651
Dave Chinnere13de952010-09-28 12:28:06 +1000652 /* nothing to sync during shutdown */
653 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
Dave Chinner24513372014-06-25 14:58:08 +1000654 return -EFSCORRUPTED;
Dave Chinnere13de952010-09-28 12:28:06 +1000655
Dave Chinnere13de952010-09-28 12:28:06 +1000656 /* If we can't grab the inode, it must on it's way to reclaim. */
657 if (!igrab(inode))
Dave Chinner24513372014-06-25 14:58:08 +1000658 return -ENOENT;
Dave Chinnere13de952010-09-28 12:28:06 +1000659
Dave Chinnere13de952010-09-28 12:28:06 +1000660 /* inode is valid */
661 return 0;
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100662
663out_unlock_noent:
664 spin_unlock(&ip->i_flags_lock);
Dave Chinner24513372014-06-25 14:58:08 +1000665 return -ENOENT;
Dave Chinnere13de952010-09-28 12:28:06 +1000666}
667
Dave Chinner75f3cb12009-06-08 15:35:14 +0200668STATIC int
669xfs_inode_ag_walk(
670 struct xfs_mount *mp,
Dave Chinner5017e972010-01-11 11:47:40 +0000671 struct xfs_perag *pag,
Eric Sandeene0094002014-04-14 19:04:19 +1000672 int (*execute)(struct xfs_inode *ip, int flags,
Brian Fostera454f742012-11-06 09:50:39 -0500673 void *args),
674 int flags,
675 void *args,
676 int tag)
Dave Chinner75f3cb12009-06-08 15:35:14 +0200677{
Dave Chinner75f3cb12009-06-08 15:35:14 +0200678 uint32_t first_index;
679 int last_error = 0;
680 int skipped;
Dave Chinner65d0f202010-09-24 18:40:15 +1000681 int done;
Dave Chinner78ae5252010-09-28 12:28:19 +1000682 int nr_found;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200683
684restart:
Dave Chinner65d0f202010-09-24 18:40:15 +1000685 done = 0;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200686 skipped = 0;
687 first_index = 0;
Dave Chinner78ae5252010-09-28 12:28:19 +1000688 nr_found = 0;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200689 do {
Dave Chinner78ae5252010-09-28 12:28:19 +1000690 struct xfs_inode *batch[XFS_LOOKUP_BATCH];
Dave Chinner75f3cb12009-06-08 15:35:14 +0200691 int error = 0;
Dave Chinner78ae5252010-09-28 12:28:19 +1000692 int i;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200693
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100694 rcu_read_lock();
Brian Fostera454f742012-11-06 09:50:39 -0500695
696 if (tag == -1)
697 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
Dave Chinner78ae5252010-09-28 12:28:19 +1000698 (void **)batch, first_index,
699 XFS_LOOKUP_BATCH);
Brian Fostera454f742012-11-06 09:50:39 -0500700 else
701 nr_found = radix_tree_gang_lookup_tag(
702 &pag->pag_ici_root,
703 (void **) batch, first_index,
704 XFS_LOOKUP_BATCH, tag);
705
Dave Chinner65d0f202010-09-24 18:40:15 +1000706 if (!nr_found) {
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100707 rcu_read_unlock();
Dave Chinner75f3cb12009-06-08 15:35:14 +0200708 break;
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000709 }
Dave Chinner75f3cb12009-06-08 15:35:14 +0200710
Dave Chinner65d0f202010-09-24 18:40:15 +1000711 /*
Dave Chinner78ae5252010-09-28 12:28:19 +1000712 * Grab the inodes before we drop the lock. if we found
713 * nothing, nr == 0 and the loop will be skipped.
Dave Chinner65d0f202010-09-24 18:40:15 +1000714 */
Dave Chinner78ae5252010-09-28 12:28:19 +1000715 for (i = 0; i < nr_found; i++) {
716 struct xfs_inode *ip = batch[i];
Dave Chinner65d0f202010-09-24 18:40:15 +1000717
Dave Chinner78ae5252010-09-28 12:28:19 +1000718 if (done || xfs_inode_ag_walk_grab(ip))
719 batch[i] = NULL;
720
721 /*
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100722 * Update the index for the next lookup. Catch
723 * overflows into the next AG range which can occur if
724 * we have inodes in the last block of the AG and we
725 * are currently pointing to the last inode.
726 *
727 * Because we may see inodes that are from the wrong AG
728 * due to RCU freeing and reallocation, only update the
729 * index if it lies in this AG. It was a race that lead
730 * us to see this inode, so another lookup from the
731 * same index will not find it again.
Dave Chinner78ae5252010-09-28 12:28:19 +1000732 */
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100733 if (XFS_INO_TO_AGNO(mp, ip->i_ino) != pag->pag_agno)
734 continue;
Dave Chinner78ae5252010-09-28 12:28:19 +1000735 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
736 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
737 done = 1;
Dave Chinnere13de952010-09-28 12:28:06 +1000738 }
Dave Chinner78ae5252010-09-28 12:28:19 +1000739
740 /* unlock now we've grabbed the inodes. */
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100741 rcu_read_unlock();
Dave Chinnere13de952010-09-28 12:28:06 +1000742
Dave Chinner78ae5252010-09-28 12:28:19 +1000743 for (i = 0; i < nr_found; i++) {
744 if (!batch[i])
745 continue;
Eric Sandeene0094002014-04-14 19:04:19 +1000746 error = execute(batch[i], flags, args);
Dave Chinner78ae5252010-09-28 12:28:19 +1000747 IRELE(batch[i]);
Dave Chinner24513372014-06-25 14:58:08 +1000748 if (error == -EAGAIN) {
Dave Chinner78ae5252010-09-28 12:28:19 +1000749 skipped++;
750 continue;
751 }
Dave Chinner24513372014-06-25 14:58:08 +1000752 if (error && last_error != -EFSCORRUPTED)
Dave Chinner78ae5252010-09-28 12:28:19 +1000753 last_error = error;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200754 }
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000755
756 /* bail out if the filesystem is corrupted. */
Dave Chinner24513372014-06-25 14:58:08 +1000757 if (error == -EFSCORRUPTED)
Dave Chinner75f3cb12009-06-08 15:35:14 +0200758 break;
759
Dave Chinner8daaa832011-07-08 14:14:46 +1000760 cond_resched();
761
Dave Chinner78ae5252010-09-28 12:28:19 +1000762 } while (nr_found && !done);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200763
764 if (skipped) {
765 delay(1);
766 goto restart;
767 }
Dave Chinner75f3cb12009-06-08 15:35:14 +0200768 return last_error;
769}
770
Brian Foster579b62f2012-11-06 09:50:47 -0500771/*
772 * Background scanning to trim post-EOF preallocated space. This is queued
Dwight Engenb9fe5052013-08-15 14:08:02 -0400773 * based on the 'speculative_prealloc_lifetime' tunable (5m by default).
Brian Foster579b62f2012-11-06 09:50:47 -0500774 */
Brian Fosterfa5a4f52016-06-21 11:53:28 +1000775void
Brian Foster579b62f2012-11-06 09:50:47 -0500776xfs_queue_eofblocks(
777 struct xfs_mount *mp)
778{
779 rcu_read_lock();
780 if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_EOFBLOCKS_TAG))
781 queue_delayed_work(mp->m_eofblocks_workqueue,
782 &mp->m_eofblocks_work,
783 msecs_to_jiffies(xfs_eofb_secs * 1000));
784 rcu_read_unlock();
785}
786
787void
788xfs_eofblocks_worker(
789 struct work_struct *work)
790{
791 struct xfs_mount *mp = container_of(to_delayed_work(work),
792 struct xfs_mount, m_eofblocks_work);
793 xfs_icache_free_eofblocks(mp, NULL);
794 xfs_queue_eofblocks(mp);
795}
796
Darrick J. Wong83104d42016-10-03 09:11:46 -0700797/*
798 * Background scanning to trim preallocated CoW space. This is queued
799 * based on the 'speculative_cow_prealloc_lifetime' tunable (5m by default).
800 * (We'll just piggyback on the post-EOF prealloc space workqueue.)
801 */
802STATIC void
803xfs_queue_cowblocks(
804 struct xfs_mount *mp)
805{
806 rcu_read_lock();
807 if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_COWBLOCKS_TAG))
808 queue_delayed_work(mp->m_eofblocks_workqueue,
809 &mp->m_cowblocks_work,
810 msecs_to_jiffies(xfs_cowb_secs * 1000));
811 rcu_read_unlock();
812}
813
814void
815xfs_cowblocks_worker(
816 struct work_struct *work)
817{
818 struct xfs_mount *mp = container_of(to_delayed_work(work),
819 struct xfs_mount, m_cowblocks_work);
820 xfs_icache_free_cowblocks(mp, NULL);
821 xfs_queue_cowblocks(mp);
822}
823
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200824int
Dave Chinner75f3cb12009-06-08 15:35:14 +0200825xfs_inode_ag_iterator(
826 struct xfs_mount *mp,
Eric Sandeene0094002014-04-14 19:04:19 +1000827 int (*execute)(struct xfs_inode *ip, int flags,
Brian Fostera454f742012-11-06 09:50:39 -0500828 void *args),
829 int flags,
830 void *args)
Dave Chinner75f3cb12009-06-08 15:35:14 +0200831{
Dave Chinner16fd5362010-07-20 09:43:39 +1000832 struct xfs_perag *pag;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200833 int error = 0;
834 int last_error = 0;
835 xfs_agnumber_t ag;
836
Dave Chinner16fd5362010-07-20 09:43:39 +1000837 ag = 0;
Dave Chinner65d0f202010-09-24 18:40:15 +1000838 while ((pag = xfs_perag_get(mp, ag))) {
839 ag = pag->pag_agno + 1;
Brian Fostera454f742012-11-06 09:50:39 -0500840 error = xfs_inode_ag_walk(mp, pag, execute, flags, args, -1);
841 xfs_perag_put(pag);
842 if (error) {
843 last_error = error;
Dave Chinner24513372014-06-25 14:58:08 +1000844 if (error == -EFSCORRUPTED)
Brian Fostera454f742012-11-06 09:50:39 -0500845 break;
846 }
847 }
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000848 return last_error;
Brian Fostera454f742012-11-06 09:50:39 -0500849}
850
851int
852xfs_inode_ag_iterator_tag(
853 struct xfs_mount *mp,
Eric Sandeene0094002014-04-14 19:04:19 +1000854 int (*execute)(struct xfs_inode *ip, int flags,
Brian Fostera454f742012-11-06 09:50:39 -0500855 void *args),
856 int flags,
857 void *args,
858 int tag)
859{
860 struct xfs_perag *pag;
861 int error = 0;
862 int last_error = 0;
863 xfs_agnumber_t ag;
864
865 ag = 0;
866 while ((pag = xfs_perag_get_tag(mp, ag, tag))) {
867 ag = pag->pag_agno + 1;
868 error = xfs_inode_ag_walk(mp, pag, execute, flags, args, tag);
Dave Chinner5017e972010-01-11 11:47:40 +0000869 xfs_perag_put(pag);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200870 if (error) {
871 last_error = error;
Dave Chinner24513372014-06-25 14:58:08 +1000872 if (error == -EFSCORRUPTED)
Dave Chinner75f3cb12009-06-08 15:35:14 +0200873 break;
874 }
875 }
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000876 return last_error;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200877}
878
David Chinner76bf1052008-10-30 17:16:21 +1100879/*
Dave Chinnere3a20c02010-09-24 19:51:50 +1000880 * Grab the inode for reclaim exclusively.
881 * Return 0 if we grabbed it, non-zero otherwise.
882 */
883STATIC int
884xfs_reclaim_inode_grab(
885 struct xfs_inode *ip,
886 int flags)
887{
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100888 ASSERT(rcu_read_lock_held());
889
890 /* quick check for stale RCU freed inode */
891 if (!ip->i_ino)
892 return 1;
Dave Chinnere3a20c02010-09-24 19:51:50 +1000893
894 /*
Christoph Hellwig474fce02011-12-18 20:00:09 +0000895 * If we are asked for non-blocking operation, do unlocked checks to
896 * see if the inode already is being flushed or in reclaim to avoid
897 * lock traffic.
Dave Chinnere3a20c02010-09-24 19:51:50 +1000898 */
899 if ((flags & SYNC_TRYLOCK) &&
Christoph Hellwig474fce02011-12-18 20:00:09 +0000900 __xfs_iflags_test(ip, XFS_IFLOCK | XFS_IRECLAIM))
Dave Chinnere3a20c02010-09-24 19:51:50 +1000901 return 1;
Dave Chinnere3a20c02010-09-24 19:51:50 +1000902
903 /*
904 * The radix tree lock here protects a thread in xfs_iget from racing
905 * with us starting reclaim on the inode. Once we have the
906 * XFS_IRECLAIM flag set it will not touch us.
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100907 *
908 * Due to RCU lookup, we may find inodes that have been freed and only
909 * have XFS_IRECLAIM set. Indeed, we may see reallocated inodes that
910 * aren't candidates for reclaim at all, so we must check the
911 * XFS_IRECLAIMABLE is set first before proceeding to reclaim.
Dave Chinnere3a20c02010-09-24 19:51:50 +1000912 */
913 spin_lock(&ip->i_flags_lock);
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100914 if (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) ||
915 __xfs_iflags_test(ip, XFS_IRECLAIM)) {
916 /* not a reclaim candidate. */
Dave Chinnere3a20c02010-09-24 19:51:50 +1000917 spin_unlock(&ip->i_flags_lock);
918 return 1;
919 }
920 __xfs_iflags_set(ip, XFS_IRECLAIM);
921 spin_unlock(&ip->i_flags_lock);
922 return 0;
923}
924
925/*
Christoph Hellwig8a480882012-04-23 15:58:35 +1000926 * Inodes in different states need to be treated differently. The following
927 * table lists the inode states and the reclaim actions necessary:
Dave Chinner777df5a2010-02-06 12:37:26 +1100928 *
929 * inode state iflush ret required action
930 * --------------- ---------- ---------------
931 * bad - reclaim
932 * shutdown EIO unpin and reclaim
933 * clean, unpinned 0 reclaim
934 * stale, unpinned 0 reclaim
Dave Chinnerc8543632010-02-06 12:39:36 +1100935 * clean, pinned(*) 0 requeue
936 * stale, pinned EAGAIN requeue
Christoph Hellwig8a480882012-04-23 15:58:35 +1000937 * dirty, async - requeue
938 * dirty, sync 0 reclaim
Dave Chinner777df5a2010-02-06 12:37:26 +1100939 *
940 * (*) dgc: I don't think the clean, pinned state is possible but it gets
941 * handled anyway given the order of checks implemented.
942 *
Dave Chinnerc8543632010-02-06 12:39:36 +1100943 * Also, because we get the flush lock first, we know that any inode that has
944 * been flushed delwri has had the flush completed by the time we check that
Christoph Hellwig8a480882012-04-23 15:58:35 +1000945 * the inode is clean.
Dave Chinnerc8543632010-02-06 12:39:36 +1100946 *
Christoph Hellwig8a480882012-04-23 15:58:35 +1000947 * Note that because the inode is flushed delayed write by AIL pushing, the
948 * flush lock may already be held here and waiting on it can result in very
949 * long latencies. Hence for sync reclaims, where we wait on the flush lock,
950 * the caller should push the AIL first before trying to reclaim inodes to
951 * minimise the amount of time spent waiting. For background relaim, we only
952 * bother to reclaim clean inodes anyway.
Dave Chinnerc8543632010-02-06 12:39:36 +1100953 *
Dave Chinner777df5a2010-02-06 12:37:26 +1100954 * Hence the order of actions after gaining the locks should be:
955 * bad => reclaim
956 * shutdown => unpin and reclaim
Christoph Hellwig8a480882012-04-23 15:58:35 +1000957 * pinned, async => requeue
Dave Chinnerc8543632010-02-06 12:39:36 +1100958 * pinned, sync => unpin
Dave Chinner777df5a2010-02-06 12:37:26 +1100959 * stale => reclaim
960 * clean => reclaim
Christoph Hellwig8a480882012-04-23 15:58:35 +1000961 * dirty, async => requeue
Dave Chinnerc8543632010-02-06 12:39:36 +1100962 * dirty, sync => flush, wait and reclaim
Dave Chinner777df5a2010-02-06 12:37:26 +1100963 */
Dave Chinner75f3cb12009-06-08 15:35:14 +0200964STATIC int
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000965xfs_reclaim_inode(
Dave Chinner75f3cb12009-06-08 15:35:14 +0200966 struct xfs_inode *ip,
967 struct xfs_perag *pag,
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000968 int sync_mode)
David Chinner7a3be022008-10-30 17:37:37 +1100969{
Christoph Hellwig4c468192012-04-23 15:58:36 +1000970 struct xfs_buf *bp = NULL;
Dave Chinner8a17d7d2016-05-18 14:09:12 +1000971 xfs_ino_t ino = ip->i_ino; /* for radix_tree_delete */
Christoph Hellwig4c468192012-04-23 15:58:36 +1000972 int error;
Dave Chinner777df5a2010-02-06 12:37:26 +1100973
Dave Chinner1bfd8d02011-03-26 09:13:55 +1100974restart:
975 error = 0;
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000976 xfs_ilock(ip, XFS_ILOCK_EXCL);
Dave Chinnerc8543632010-02-06 12:39:36 +1100977 if (!xfs_iflock_nowait(ip)) {
978 if (!(sync_mode & SYNC_WAIT))
979 goto out;
980 xfs_iflock(ip);
981 }
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000982
Dave Chinner777df5a2010-02-06 12:37:26 +1100983 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
984 xfs_iunpin_wait(ip);
Brian Fosterb49ef752017-01-09 16:38:38 +0100985 /* xfs_iflush_abort() drops the flush lock */
Dave Chinner04913fd2012-04-23 15:58:41 +1000986 xfs_iflush_abort(ip, false);
Dave Chinner777df5a2010-02-06 12:37:26 +1100987 goto reclaim;
988 }
Dave Chinnerc8543632010-02-06 12:39:36 +1100989 if (xfs_ipincount(ip)) {
Christoph Hellwig8a480882012-04-23 15:58:35 +1000990 if (!(sync_mode & SYNC_WAIT))
991 goto out_ifunlock;
Dave Chinner777df5a2010-02-06 12:37:26 +1100992 xfs_iunpin_wait(ip);
Dave Chinnerc8543632010-02-06 12:39:36 +1100993 }
Brian Fosterb49ef752017-01-09 16:38:38 +0100994 if (xfs_iflags_test(ip, XFS_ISTALE) || xfs_inode_clean(ip)) {
995 xfs_ifunlock(ip);
Dave Chinner777df5a2010-02-06 12:37:26 +1100996 goto reclaim;
Brian Fosterb49ef752017-01-09 16:38:38 +0100997 }
Dave Chinner777df5a2010-02-06 12:37:26 +1100998
Dave Chinner1bfd8d02011-03-26 09:13:55 +1100999 /*
Christoph Hellwig8a480882012-04-23 15:58:35 +10001000 * Never flush out dirty data during non-blocking reclaim, as it would
1001 * just contend with AIL pushing trying to do the same job.
1002 */
1003 if (!(sync_mode & SYNC_WAIT))
1004 goto out_ifunlock;
1005
1006 /*
Dave Chinner1bfd8d02011-03-26 09:13:55 +11001007 * Now we have an inode that needs flushing.
1008 *
Christoph Hellwig4c468192012-04-23 15:58:36 +10001009 * Note that xfs_iflush will never block on the inode buffer lock, as
Dave Chinner1bfd8d02011-03-26 09:13:55 +11001010 * xfs_ifree_cluster() can lock the inode buffer before it locks the
Christoph Hellwig4c468192012-04-23 15:58:36 +10001011 * ip->i_lock, and we are doing the exact opposite here. As a result,
Christoph Hellwig475ee412012-07-03 12:21:22 -04001012 * doing a blocking xfs_imap_to_bp() to get the cluster buffer would
1013 * result in an ABBA deadlock with xfs_ifree_cluster().
Dave Chinner1bfd8d02011-03-26 09:13:55 +11001014 *
1015 * As xfs_ifree_cluser() must gather all inodes that are active in the
1016 * cache to mark them stale, if we hit this case we don't actually want
1017 * to do IO here - we want the inode marked stale so we can simply
Christoph Hellwig4c468192012-04-23 15:58:36 +10001018 * reclaim it. Hence if we get an EAGAIN error here, just unlock the
1019 * inode, back off and try again. Hopefully the next pass through will
1020 * see the stale flag set on the inode.
Dave Chinner1bfd8d02011-03-26 09:13:55 +11001021 */
Christoph Hellwig4c468192012-04-23 15:58:36 +10001022 error = xfs_iflush(ip, &bp);
Dave Chinner24513372014-06-25 14:58:08 +10001023 if (error == -EAGAIN) {
Christoph Hellwig8a480882012-04-23 15:58:35 +10001024 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1025 /* backoff longer than in xfs_ifree_cluster */
1026 delay(2);
1027 goto restart;
Dave Chinnerc8e20be2010-01-10 23:51:45 +00001028 }
Dave Chinnerc8543632010-02-06 12:39:36 +11001029
Christoph Hellwig4c468192012-04-23 15:58:36 +10001030 if (!error) {
1031 error = xfs_bwrite(bp);
1032 xfs_buf_relse(bp);
1033 }
1034
Dave Chinner777df5a2010-02-06 12:37:26 +11001035reclaim:
Brian Fosterb49ef752017-01-09 16:38:38 +01001036 ASSERT(!xfs_isiflocked(ip));
1037
Dave Chinner8a17d7d2016-05-18 14:09:12 +10001038 /*
1039 * Because we use RCU freeing we need to ensure the inode always appears
1040 * to be reclaimed with an invalid inode number when in the free state.
Brian Fosterb49ef752017-01-09 16:38:38 +01001041 * We do this as early as possible under the ILOCK so that
1042 * xfs_iflush_cluster() can be guaranteed to detect races with us here.
1043 * By doing this, we guarantee that once xfs_iflush_cluster has locked
1044 * XFS_ILOCK that it will see either a valid, flushable inode that will
1045 * serialise correctly, or it will see a clean (and invalid) inode that
1046 * it can skip.
Dave Chinner8a17d7d2016-05-18 14:09:12 +10001047 */
1048 spin_lock(&ip->i_flags_lock);
1049 ip->i_flags = XFS_IRECLAIM;
1050 ip->i_ino = 0;
1051 spin_unlock(&ip->i_flags_lock);
1052
Dave Chinnerc8e20be2010-01-10 23:51:45 +00001053 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner2f11fea2010-07-20 17:53:25 +10001054
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001055 XFS_STATS_INC(ip->i_mount, xs_ig_reclaims);
Dave Chinner2f11fea2010-07-20 17:53:25 +10001056 /*
1057 * Remove the inode from the per-AG radix tree.
1058 *
1059 * Because radix_tree_delete won't complain even if the item was never
1060 * added to the tree assert that it's been there before to catch
1061 * problems with the inode life time early on.
1062 */
Dave Chinner1a427ab2010-12-16 17:08:41 +11001063 spin_lock(&pag->pag_ici_lock);
Dave Chinner2f11fea2010-07-20 17:53:25 +10001064 if (!radix_tree_delete(&pag->pag_ici_root,
Dave Chinner8a17d7d2016-05-18 14:09:12 +10001065 XFS_INO_TO_AGINO(ip->i_mount, ino)))
Dave Chinner2f11fea2010-07-20 17:53:25 +10001066 ASSERT(0);
Dave Chinner545c0882016-05-18 14:11:41 +10001067 xfs_perag_clear_reclaim_tag(pag);
Dave Chinner1a427ab2010-12-16 17:08:41 +11001068 spin_unlock(&pag->pag_ici_lock);
Dave Chinner2f11fea2010-07-20 17:53:25 +10001069
1070 /*
1071 * Here we do an (almost) spurious inode lock in order to coordinate
1072 * with inode cache radix tree lookups. This is because the lookup
1073 * can reference the inodes in the cache without taking references.
1074 *
1075 * We make that OK here by ensuring that we wait until the inode is
Alex Elderad637a12012-02-16 22:01:00 +00001076 * unlocked after the lookup before we go ahead and free it.
Dave Chinner2f11fea2010-07-20 17:53:25 +10001077 */
Alex Elderad637a12012-02-16 22:01:00 +00001078 xfs_ilock(ip, XFS_ILOCK_EXCL);
Dave Chinner2f11fea2010-07-20 17:53:25 +10001079 xfs_qm_dqdetach(ip);
Alex Elderad637a12012-02-16 22:01:00 +00001080 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner2f11fea2010-07-20 17:53:25 +10001081
Dave Chinner8a17d7d2016-05-18 14:09:12 +10001082 __xfs_inode_free(ip);
Alex Elderad637a12012-02-16 22:01:00 +00001083 return error;
Christoph Hellwig8a480882012-04-23 15:58:35 +10001084
1085out_ifunlock:
1086 xfs_ifunlock(ip);
1087out:
1088 xfs_iflags_clear(ip, XFS_IRECLAIM);
1089 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1090 /*
Dave Chinner24513372014-06-25 14:58:08 +10001091 * We could return -EAGAIN here to make reclaim rescan the inode tree in
Christoph Hellwig8a480882012-04-23 15:58:35 +10001092 * a short while. However, this just burns CPU time scanning the tree
Dave Chinner58896082012-10-08 21:56:05 +11001093 * waiting for IO to complete and the reclaim work never goes back to
1094 * the idle state. Instead, return 0 to let the next scheduled
1095 * background reclaim attempt to reclaim the inode again.
Christoph Hellwig8a480882012-04-23 15:58:35 +10001096 */
1097 return 0;
David Chinner7a3be022008-10-30 17:37:37 +11001098}
1099
Dave Chinner65d0f202010-09-24 18:40:15 +10001100/*
1101 * Walk the AGs and reclaim the inodes in them. Even if the filesystem is
1102 * corrupted, we still want to try to reclaim all the inodes. If we don't,
1103 * then a shut down during filesystem unmount reclaim walk leak all the
1104 * unreclaimed inodes.
1105 */
Dave Chinner33479e02012-10-08 21:56:11 +11001106STATIC int
Dave Chinner65d0f202010-09-24 18:40:15 +10001107xfs_reclaim_inodes_ag(
1108 struct xfs_mount *mp,
1109 int flags,
1110 int *nr_to_scan)
1111{
1112 struct xfs_perag *pag;
1113 int error = 0;
1114 int last_error = 0;
1115 xfs_agnumber_t ag;
Dave Chinner69b491c2010-09-27 11:09:51 +10001116 int trylock = flags & SYNC_TRYLOCK;
1117 int skipped;
Dave Chinner65d0f202010-09-24 18:40:15 +10001118
Dave Chinner69b491c2010-09-27 11:09:51 +10001119restart:
Dave Chinner65d0f202010-09-24 18:40:15 +10001120 ag = 0;
Dave Chinner69b491c2010-09-27 11:09:51 +10001121 skipped = 0;
Dave Chinner65d0f202010-09-24 18:40:15 +10001122 while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
1123 unsigned long first_index = 0;
1124 int done = 0;
Dave Chinnere3a20c02010-09-24 19:51:50 +10001125 int nr_found = 0;
Dave Chinner65d0f202010-09-24 18:40:15 +10001126
1127 ag = pag->pag_agno + 1;
1128
Dave Chinner69b491c2010-09-27 11:09:51 +10001129 if (trylock) {
1130 if (!mutex_trylock(&pag->pag_ici_reclaim_lock)) {
1131 skipped++;
Dave Chinnerf83282a2010-11-08 08:55:04 +00001132 xfs_perag_put(pag);
Dave Chinner69b491c2010-09-27 11:09:51 +10001133 continue;
1134 }
1135 first_index = pag->pag_ici_reclaim_cursor;
1136 } else
1137 mutex_lock(&pag->pag_ici_reclaim_lock);
1138
Dave Chinner65d0f202010-09-24 18:40:15 +10001139 do {
Dave Chinnere3a20c02010-09-24 19:51:50 +10001140 struct xfs_inode *batch[XFS_LOOKUP_BATCH];
1141 int i;
Dave Chinner65d0f202010-09-24 18:40:15 +10001142
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001143 rcu_read_lock();
Dave Chinnere3a20c02010-09-24 19:51:50 +10001144 nr_found = radix_tree_gang_lookup_tag(
1145 &pag->pag_ici_root,
1146 (void **)batch, first_index,
1147 XFS_LOOKUP_BATCH,
Dave Chinner65d0f202010-09-24 18:40:15 +10001148 XFS_ICI_RECLAIM_TAG);
1149 if (!nr_found) {
Dave Chinnerb2232212011-05-06 02:54:04 +00001150 done = 1;
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001151 rcu_read_unlock();
Dave Chinner65d0f202010-09-24 18:40:15 +10001152 break;
1153 }
1154
1155 /*
Dave Chinnere3a20c02010-09-24 19:51:50 +10001156 * Grab the inodes before we drop the lock. if we found
1157 * nothing, nr == 0 and the loop will be skipped.
Dave Chinner65d0f202010-09-24 18:40:15 +10001158 */
Dave Chinnere3a20c02010-09-24 19:51:50 +10001159 for (i = 0; i < nr_found; i++) {
1160 struct xfs_inode *ip = batch[i];
Dave Chinner65d0f202010-09-24 18:40:15 +10001161
Dave Chinnere3a20c02010-09-24 19:51:50 +10001162 if (done || xfs_reclaim_inode_grab(ip, flags))
1163 batch[i] = NULL;
Dave Chinner65d0f202010-09-24 18:40:15 +10001164
Dave Chinnere3a20c02010-09-24 19:51:50 +10001165 /*
1166 * Update the index for the next lookup. Catch
1167 * overflows into the next AG range which can
1168 * occur if we have inodes in the last block of
1169 * the AG and we are currently pointing to the
1170 * last inode.
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001171 *
1172 * Because we may see inodes that are from the
1173 * wrong AG due to RCU freeing and
1174 * reallocation, only update the index if it
1175 * lies in this AG. It was a race that lead us
1176 * to see this inode, so another lookup from
1177 * the same index will not find it again.
Dave Chinnere3a20c02010-09-24 19:51:50 +10001178 */
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001179 if (XFS_INO_TO_AGNO(mp, ip->i_ino) !=
1180 pag->pag_agno)
1181 continue;
Dave Chinnere3a20c02010-09-24 19:51:50 +10001182 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
1183 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
1184 done = 1;
1185 }
1186
1187 /* unlock now we've grabbed the inodes. */
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001188 rcu_read_unlock();
Dave Chinnere3a20c02010-09-24 19:51:50 +10001189
1190 for (i = 0; i < nr_found; i++) {
1191 if (!batch[i])
1192 continue;
1193 error = xfs_reclaim_inode(batch[i], pag, flags);
Dave Chinner24513372014-06-25 14:58:08 +10001194 if (error && last_error != -EFSCORRUPTED)
Dave Chinnere3a20c02010-09-24 19:51:50 +10001195 last_error = error;
1196 }
1197
1198 *nr_to_scan -= XFS_LOOKUP_BATCH;
1199
Dave Chinner8daaa832011-07-08 14:14:46 +10001200 cond_resched();
1201
Dave Chinnere3a20c02010-09-24 19:51:50 +10001202 } while (nr_found && !done && *nr_to_scan > 0);
Dave Chinner65d0f202010-09-24 18:40:15 +10001203
Dave Chinner69b491c2010-09-27 11:09:51 +10001204 if (trylock && !done)
1205 pag->pag_ici_reclaim_cursor = first_index;
1206 else
1207 pag->pag_ici_reclaim_cursor = 0;
1208 mutex_unlock(&pag->pag_ici_reclaim_lock);
Dave Chinner65d0f202010-09-24 18:40:15 +10001209 xfs_perag_put(pag);
1210 }
Dave Chinner69b491c2010-09-27 11:09:51 +10001211
1212 /*
1213 * if we skipped any AG, and we still have scan count remaining, do
1214 * another pass this time using blocking reclaim semantics (i.e
1215 * waiting on the reclaim locks and ignoring the reclaim cursors). This
1216 * ensure that when we get more reclaimers than AGs we block rather
1217 * than spin trying to execute reclaim.
1218 */
Dave Chinner8daaa832011-07-08 14:14:46 +10001219 if (skipped && (flags & SYNC_WAIT) && *nr_to_scan > 0) {
Dave Chinner69b491c2010-09-27 11:09:51 +10001220 trylock = 0;
1221 goto restart;
1222 }
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001223 return last_error;
Dave Chinner65d0f202010-09-24 18:40:15 +10001224}
1225
David Chinnerfce08f22008-10-30 17:37:03 +11001226int
David Chinner1dc33182008-10-30 17:37:15 +11001227xfs_reclaim_inodes(
David Chinnerfce08f22008-10-30 17:37:03 +11001228 xfs_mount_t *mp,
David Chinnerfce08f22008-10-30 17:37:03 +11001229 int mode)
1230{
Dave Chinner65d0f202010-09-24 18:40:15 +10001231 int nr_to_scan = INT_MAX;
1232
1233 return xfs_reclaim_inodes_ag(mp, mode, &nr_to_scan);
Dave Chinner9bf729c2010-04-29 09:55:50 +10001234}
1235
1236/*
Dave Chinner8daaa832011-07-08 14:14:46 +10001237 * Scan a certain number of inodes for reclaim.
Dave Chinnera7b339f2011-04-08 12:45:07 +10001238 *
1239 * When called we make sure that there is a background (fast) inode reclaim in
Dave Chinner8daaa832011-07-08 14:14:46 +10001240 * progress, while we will throttle the speed of reclaim via doing synchronous
Dave Chinnera7b339f2011-04-08 12:45:07 +10001241 * reclaim of inodes. That means if we come across dirty inodes, we wait for
1242 * them to be cleaned, which we hope will not be very long due to the
1243 * background walker having already kicked the IO off on those dirty inodes.
Dave Chinner9bf729c2010-04-29 09:55:50 +10001244 */
Dave Chinner0a234c62013-08-28 10:17:57 +10001245long
Dave Chinner8daaa832011-07-08 14:14:46 +10001246xfs_reclaim_inodes_nr(
1247 struct xfs_mount *mp,
1248 int nr_to_scan)
Dave Chinner9bf729c2010-04-29 09:55:50 +10001249{
Dave Chinner8daaa832011-07-08 14:14:46 +10001250 /* kick background reclaimer and push the AIL */
Dave Chinner58896082012-10-08 21:56:05 +11001251 xfs_reclaim_work_queue(mp);
Dave Chinner8daaa832011-07-08 14:14:46 +10001252 xfs_ail_push_all(mp->m_ail);
Dave Chinner9bf729c2010-04-29 09:55:50 +10001253
Dave Chinner0a234c62013-08-28 10:17:57 +10001254 return xfs_reclaim_inodes_ag(mp, SYNC_TRYLOCK | SYNC_WAIT, &nr_to_scan);
Dave Chinner8daaa832011-07-08 14:14:46 +10001255}
Dave Chinnera7b339f2011-04-08 12:45:07 +10001256
Dave Chinner8daaa832011-07-08 14:14:46 +10001257/*
1258 * Return the number of reclaimable inodes in the filesystem for
1259 * the shrinker to determine how much to reclaim.
1260 */
1261int
1262xfs_reclaim_inodes_count(
1263 struct xfs_mount *mp)
1264{
1265 struct xfs_perag *pag;
1266 xfs_agnumber_t ag = 0;
1267 int reclaimable = 0;
Dave Chinner9bf729c2010-04-29 09:55:50 +10001268
Dave Chinner65d0f202010-09-24 18:40:15 +10001269 while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
1270 ag = pag->pag_agno + 1;
Dave Chinner70e60ce2010-07-20 08:07:02 +10001271 reclaimable += pag->pag_ici_reclaimable;
1272 xfs_perag_put(pag);
Dave Chinner9bf729c2010-04-29 09:55:50 +10001273 }
Dave Chinner9bf729c2010-04-29 09:55:50 +10001274 return reclaimable;
1275}
1276
Brian Foster41176a62012-11-06 09:50:42 -05001277STATIC int
Brian Foster3e3f9f52012-11-07 12:21:13 -05001278xfs_inode_match_id(
1279 struct xfs_inode *ip,
1280 struct xfs_eofblocks *eofb)
1281{
Dwight Engenb9fe5052013-08-15 14:08:02 -04001282 if ((eofb->eof_flags & XFS_EOF_FLAGS_UID) &&
1283 !uid_eq(VFS_I(ip)->i_uid, eofb->eof_uid))
Brian Foster1b556042012-11-06 09:50:45 -05001284 return 0;
Brian Foster3e3f9f52012-11-07 12:21:13 -05001285
Dwight Engenb9fe5052013-08-15 14:08:02 -04001286 if ((eofb->eof_flags & XFS_EOF_FLAGS_GID) &&
1287 !gid_eq(VFS_I(ip)->i_gid, eofb->eof_gid))
Brian Foster1b556042012-11-06 09:50:45 -05001288 return 0;
1289
Dwight Engenb9fe5052013-08-15 14:08:02 -04001290 if ((eofb->eof_flags & XFS_EOF_FLAGS_PRID) &&
Brian Foster1b556042012-11-06 09:50:45 -05001291 xfs_get_projid(ip) != eofb->eof_prid)
1292 return 0;
1293
1294 return 1;
Brian Foster3e3f9f52012-11-07 12:21:13 -05001295}
1296
Brian Fosterf4526392014-07-24 19:44:28 +10001297/*
1298 * A union-based inode filtering algorithm. Process the inode if any of the
1299 * criteria match. This is for global/internal scans only.
1300 */
1301STATIC int
1302xfs_inode_match_id_union(
1303 struct xfs_inode *ip,
1304 struct xfs_eofblocks *eofb)
1305{
1306 if ((eofb->eof_flags & XFS_EOF_FLAGS_UID) &&
1307 uid_eq(VFS_I(ip)->i_uid, eofb->eof_uid))
1308 return 1;
1309
1310 if ((eofb->eof_flags & XFS_EOF_FLAGS_GID) &&
1311 gid_eq(VFS_I(ip)->i_gid, eofb->eof_gid))
1312 return 1;
1313
1314 if ((eofb->eof_flags & XFS_EOF_FLAGS_PRID) &&
1315 xfs_get_projid(ip) == eofb->eof_prid)
1316 return 1;
1317
1318 return 0;
1319}
1320
Brian Foster3e3f9f52012-11-07 12:21:13 -05001321STATIC int
Brian Foster41176a62012-11-06 09:50:42 -05001322xfs_inode_free_eofblocks(
1323 struct xfs_inode *ip,
Brian Foster41176a62012-11-06 09:50:42 -05001324 int flags,
1325 void *args)
1326{
1327 int ret;
Brian Foster3e3f9f52012-11-07 12:21:13 -05001328 struct xfs_eofblocks *eofb = args;
Brian Foster5400da72014-07-24 19:40:22 +10001329 bool need_iolock = true;
Brian Fosterf4526392014-07-24 19:44:28 +10001330 int match;
Brian Foster5400da72014-07-24 19:40:22 +10001331
1332 ASSERT(!eofb || (eofb && eofb->eof_scan_owner != 0));
Brian Foster41176a62012-11-06 09:50:42 -05001333
1334 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;
Brian Foster5400da72014-07-24 19:40:22 +10001361
1362 /*
1363 * A scan owner implies we already hold the iolock. Skip it in
1364 * xfs_free_eofblocks() to avoid deadlock. This also eliminates
1365 * the possibility of EAGAIN being returned.
1366 */
1367 if (eofb->eof_scan_owner == ip->i_ino)
1368 need_iolock = false;
Brian Foster00ca79a2012-11-07 12:21:14 -05001369 }
Brian Foster3e3f9f52012-11-07 12:21:13 -05001370
Brian Foster5400da72014-07-24 19:40:22 +10001371 ret = xfs_free_eofblocks(ip->i_mount, ip, need_iolock);
Brian Foster41176a62012-11-06 09:50:42 -05001372
1373 /* don't revisit the inode if we're not waiting */
Dave Chinner24513372014-06-25 14:58:08 +10001374 if (ret == -EAGAIN && !(flags & SYNC_WAIT))
Brian Foster41176a62012-11-06 09:50:42 -05001375 ret = 0;
1376
1377 return ret;
1378}
1379
Darrick J. Wong83104d42016-10-03 09:11:46 -07001380static int
1381__xfs_icache_free_eofblocks(
Brian Foster41176a62012-11-06 09:50:42 -05001382 struct xfs_mount *mp,
Darrick J. Wong83104d42016-10-03 09:11:46 -07001383 struct xfs_eofblocks *eofb,
1384 int (*execute)(struct xfs_inode *ip, int flags,
1385 void *args),
1386 int tag)
Brian Foster41176a62012-11-06 09:50:42 -05001387{
Brian Foster8ca149d2012-11-07 12:21:12 -05001388 int flags = SYNC_TRYLOCK;
1389
1390 if (eofb && (eofb->eof_flags & XFS_EOF_FLAGS_SYNC))
1391 flags = SYNC_WAIT;
1392
Darrick J. Wong83104d42016-10-03 09:11:46 -07001393 return xfs_inode_ag_iterator_tag(mp, execute, flags,
1394 eofb, tag);
1395}
1396
1397int
1398xfs_icache_free_eofblocks(
1399 struct xfs_mount *mp,
1400 struct xfs_eofblocks *eofb)
1401{
1402 return __xfs_icache_free_eofblocks(mp, eofb, xfs_inode_free_eofblocks,
1403 XFS_ICI_EOFBLOCKS_TAG);
Brian Foster41176a62012-11-06 09:50:42 -05001404}
1405
Brian Fosterdc06f3982014-07-24 19:49:28 +10001406/*
1407 * Run eofblocks scans on the quotas applicable to the inode. For inodes with
1408 * multiple quotas, we don't know exactly which quota caused an allocation
1409 * failure. We make a best effort by including each quota under low free space
1410 * conditions (less than 1% free space) in the scan.
1411 */
Darrick J. Wong83104d42016-10-03 09:11:46 -07001412static int
1413__xfs_inode_free_quota_eofblocks(
1414 struct xfs_inode *ip,
1415 int (*execute)(struct xfs_mount *mp,
1416 struct xfs_eofblocks *eofb))
Brian Fosterdc06f3982014-07-24 19:49:28 +10001417{
1418 int scan = 0;
1419 struct xfs_eofblocks eofb = {0};
1420 struct xfs_dquot *dq;
1421
1422 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
1423
1424 /*
1425 * Set the scan owner to avoid a potential livelock. Otherwise, the scan
1426 * can repeatedly trylock on the inode we're currently processing. We
1427 * run a sync scan to increase effectiveness and use the union filter to
1428 * cover all applicable quotas in a single scan.
1429 */
1430 eofb.eof_scan_owner = ip->i_ino;
1431 eofb.eof_flags = XFS_EOF_FLAGS_UNION|XFS_EOF_FLAGS_SYNC;
1432
1433 if (XFS_IS_UQUOTA_ENFORCED(ip->i_mount)) {
1434 dq = xfs_inode_dquot(ip, XFS_DQ_USER);
1435 if (dq && xfs_dquot_lowsp(dq)) {
1436 eofb.eof_uid = VFS_I(ip)->i_uid;
1437 eofb.eof_flags |= XFS_EOF_FLAGS_UID;
1438 scan = 1;
1439 }
1440 }
1441
1442 if (XFS_IS_GQUOTA_ENFORCED(ip->i_mount)) {
1443 dq = xfs_inode_dquot(ip, XFS_DQ_GROUP);
1444 if (dq && xfs_dquot_lowsp(dq)) {
1445 eofb.eof_gid = VFS_I(ip)->i_gid;
1446 eofb.eof_flags |= XFS_EOF_FLAGS_GID;
1447 scan = 1;
1448 }
1449 }
1450
1451 if (scan)
Darrick J. Wong83104d42016-10-03 09:11:46 -07001452 execute(ip->i_mount, &eofb);
Brian Fosterdc06f3982014-07-24 19:49:28 +10001453
1454 return scan;
1455}
1456
Darrick J. Wong83104d42016-10-03 09:11:46 -07001457int
1458xfs_inode_free_quota_eofblocks(
1459 struct xfs_inode *ip)
1460{
1461 return __xfs_inode_free_quota_eofblocks(ip, xfs_icache_free_eofblocks);
1462}
1463
1464static void
1465__xfs_inode_set_eofblocks_tag(
1466 xfs_inode_t *ip,
1467 void (*execute)(struct xfs_mount *mp),
1468 void (*set_tp)(struct xfs_mount *mp, xfs_agnumber_t agno,
1469 int error, unsigned long caller_ip),
1470 int tag)
Brian Foster27b52862012-11-06 09:50:38 -05001471{
1472 struct xfs_mount *mp = ip->i_mount;
1473 struct xfs_perag *pag;
1474 int tagged;
1475
Christoph Hellwig85a6e762016-09-19 11:09:48 +10001476 /*
1477 * Don't bother locking the AG and looking up in the radix trees
1478 * if we already know that we have the tag set.
1479 */
1480 if (ip->i_flags & XFS_IEOFBLOCKS)
1481 return;
1482 spin_lock(&ip->i_flags_lock);
1483 ip->i_flags |= XFS_IEOFBLOCKS;
1484 spin_unlock(&ip->i_flags_lock);
1485
Brian Foster27b52862012-11-06 09:50:38 -05001486 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
1487 spin_lock(&pag->pag_ici_lock);
Brian Foster27b52862012-11-06 09:50:38 -05001488
Darrick J. Wong83104d42016-10-03 09:11:46 -07001489 tagged = radix_tree_tagged(&pag->pag_ici_root, tag);
Brian Foster27b52862012-11-06 09:50:38 -05001490 radix_tree_tag_set(&pag->pag_ici_root,
Darrick J. Wong83104d42016-10-03 09:11:46 -07001491 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino), tag);
Brian Foster27b52862012-11-06 09:50:38 -05001492 if (!tagged) {
1493 /* propagate the eofblocks tag up into the perag radix tree */
1494 spin_lock(&ip->i_mount->m_perag_lock);
1495 radix_tree_tag_set(&ip->i_mount->m_perag_tree,
1496 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
Darrick J. Wong83104d42016-10-03 09:11:46 -07001497 tag);
Brian Foster27b52862012-11-06 09:50:38 -05001498 spin_unlock(&ip->i_mount->m_perag_lock);
1499
Brian Foster579b62f2012-11-06 09:50:47 -05001500 /* kick off background trimming */
Darrick J. Wong83104d42016-10-03 09:11:46 -07001501 execute(ip->i_mount);
Brian Foster579b62f2012-11-06 09:50:47 -05001502
Darrick J. Wong83104d42016-10-03 09:11:46 -07001503 set_tp(ip->i_mount, pag->pag_agno, -1, _RET_IP_);
Brian Foster27b52862012-11-06 09:50:38 -05001504 }
1505
1506 spin_unlock(&pag->pag_ici_lock);
1507 xfs_perag_put(pag);
1508}
1509
1510void
Darrick J. Wong83104d42016-10-03 09:11:46 -07001511xfs_inode_set_eofblocks_tag(
Brian Foster27b52862012-11-06 09:50:38 -05001512 xfs_inode_t *ip)
1513{
Darrick J. Wong83104d42016-10-03 09:11:46 -07001514 trace_xfs_inode_set_eofblocks_tag(ip);
1515 return __xfs_inode_set_eofblocks_tag(ip, xfs_queue_eofblocks,
1516 trace_xfs_perag_set_eofblocks,
1517 XFS_ICI_EOFBLOCKS_TAG);
1518}
1519
1520static void
1521__xfs_inode_clear_eofblocks_tag(
1522 xfs_inode_t *ip,
1523 void (*clear_tp)(struct xfs_mount *mp, xfs_agnumber_t agno,
1524 int error, unsigned long caller_ip),
1525 int tag)
1526{
Brian Foster27b52862012-11-06 09:50:38 -05001527 struct xfs_mount *mp = ip->i_mount;
1528 struct xfs_perag *pag;
1529
Christoph Hellwig85a6e762016-09-19 11:09:48 +10001530 spin_lock(&ip->i_flags_lock);
1531 ip->i_flags &= ~XFS_IEOFBLOCKS;
1532 spin_unlock(&ip->i_flags_lock);
1533
Brian Foster27b52862012-11-06 09:50:38 -05001534 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
1535 spin_lock(&pag->pag_ici_lock);
Brian Foster27b52862012-11-06 09:50:38 -05001536
1537 radix_tree_tag_clear(&pag->pag_ici_root,
Darrick J. Wong83104d42016-10-03 09:11:46 -07001538 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino), tag);
1539 if (!radix_tree_tagged(&pag->pag_ici_root, tag)) {
Brian Foster27b52862012-11-06 09:50:38 -05001540 /* clear the eofblocks tag from the perag radix tree */
1541 spin_lock(&ip->i_mount->m_perag_lock);
1542 radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
1543 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
Darrick J. Wong83104d42016-10-03 09:11:46 -07001544 tag);
Brian Foster27b52862012-11-06 09:50:38 -05001545 spin_unlock(&ip->i_mount->m_perag_lock);
Darrick J. Wong83104d42016-10-03 09:11:46 -07001546 clear_tp(ip->i_mount, pag->pag_agno, -1, _RET_IP_);
Brian Foster27b52862012-11-06 09:50:38 -05001547 }
1548
1549 spin_unlock(&pag->pag_ici_lock);
1550 xfs_perag_put(pag);
1551}
1552
Darrick J. Wong83104d42016-10-03 09:11:46 -07001553void
1554xfs_inode_clear_eofblocks_tag(
1555 xfs_inode_t *ip)
1556{
1557 trace_xfs_inode_clear_eofblocks_tag(ip);
1558 return __xfs_inode_clear_eofblocks_tag(ip,
1559 trace_xfs_perag_clear_eofblocks, XFS_ICI_EOFBLOCKS_TAG);
1560}
1561
1562/*
1563 * Automatic CoW Reservation Freeing
1564 *
1565 * These functions automatically garbage collect leftover CoW reservations
1566 * that were made on behalf of a cowextsize hint when we start to run out
1567 * of quota or when the reservations sit around for too long. If the file
1568 * has dirty pages or is undergoing writeback, its CoW reservations will
1569 * be retained.
1570 *
1571 * The actual garbage collection piggybacks off the same code that runs
1572 * the speculative EOF preallocation garbage collector.
1573 */
1574STATIC int
1575xfs_inode_free_cowblocks(
1576 struct xfs_inode *ip,
1577 int flags,
1578 void *args)
1579{
1580 int ret;
1581 struct xfs_eofblocks *eofb = args;
1582 bool need_iolock = true;
1583 int match;
Brian Foster2f092422017-01-09 16:38:34 +01001584 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
Darrick J. Wong83104d42016-10-03 09:11:46 -07001585
1586 ASSERT(!eofb || (eofb && eofb->eof_scan_owner != 0));
1587
Brian Foster2f092422017-01-09 16:38:34 +01001588 /*
1589 * Just clear the tag if we have an empty cow fork or none at all. It's
1590 * possible the inode was fully unshared since it was originally tagged.
1591 */
1592 if (!xfs_is_reflink_inode(ip) || !ifp->if_bytes) {
Darrick J. Wong83104d42016-10-03 09:11:46 -07001593 trace_xfs_inode_free_cowblocks_invalid(ip);
1594 xfs_inode_clear_cowblocks_tag(ip);
1595 return 0;
1596 }
1597
1598 /*
1599 * If the mapping is dirty or under writeback we cannot touch the
1600 * CoW fork. Leave it alone if we're in the midst of a directio.
1601 */
Christoph Hellwig91192ae2017-01-09 16:39:02 +01001602 if ((VFS_I(ip)->i_state & I_DIRTY_PAGES) ||
1603 mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_DIRTY) ||
Darrick J. Wong83104d42016-10-03 09:11:46 -07001604 mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_WRITEBACK) ||
1605 atomic_read(&VFS_I(ip)->i_dio_count))
1606 return 0;
1607
1608 if (eofb) {
1609 if (eofb->eof_flags & XFS_EOF_FLAGS_UNION)
1610 match = xfs_inode_match_id_union(ip, eofb);
1611 else
1612 match = xfs_inode_match_id(ip, eofb);
1613 if (!match)
1614 return 0;
1615
1616 /* skip the inode if the file size is too small */
1617 if (eofb->eof_flags & XFS_EOF_FLAGS_MINFILESIZE &&
1618 XFS_ISIZE(ip) < eofb->eof_min_file_size)
1619 return 0;
1620
1621 /*
1622 * A scan owner implies we already hold the iolock. Skip it in
1623 * xfs_free_eofblocks() to avoid deadlock. This also eliminates
1624 * the possibility of EAGAIN being returned.
1625 */
1626 if (eofb->eof_scan_owner == ip->i_ino)
1627 need_iolock = false;
1628 }
1629
1630 /* Free the CoW blocks */
1631 if (need_iolock) {
1632 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1633 xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
1634 }
1635
1636 ret = xfs_reflink_cancel_cow_range(ip, 0, NULLFILEOFF);
1637
1638 if (need_iolock) {
1639 xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
1640 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1641 }
1642
1643 return ret;
1644}
1645
1646int
1647xfs_icache_free_cowblocks(
1648 struct xfs_mount *mp,
1649 struct xfs_eofblocks *eofb)
1650{
1651 return __xfs_icache_free_eofblocks(mp, eofb, xfs_inode_free_cowblocks,
1652 XFS_ICI_COWBLOCKS_TAG);
1653}
1654
1655int
1656xfs_inode_free_quota_cowblocks(
1657 struct xfs_inode *ip)
1658{
1659 return __xfs_inode_free_quota_eofblocks(ip, xfs_icache_free_cowblocks);
1660}
1661
1662void
1663xfs_inode_set_cowblocks_tag(
1664 xfs_inode_t *ip)
1665{
Brian Foster7b7381f2016-10-24 14:21:00 +11001666 trace_xfs_inode_set_cowblocks_tag(ip);
Darrick J. Wong83104d42016-10-03 09:11:46 -07001667 return __xfs_inode_set_eofblocks_tag(ip, xfs_queue_cowblocks,
Brian Foster7b7381f2016-10-24 14:21:00 +11001668 trace_xfs_perag_set_cowblocks,
Darrick J. Wong83104d42016-10-03 09:11:46 -07001669 XFS_ICI_COWBLOCKS_TAG);
1670}
1671
1672void
1673xfs_inode_clear_cowblocks_tag(
1674 xfs_inode_t *ip)
1675{
Brian Foster7b7381f2016-10-24 14:21:00 +11001676 trace_xfs_inode_clear_cowblocks_tag(ip);
Darrick J. Wong83104d42016-10-03 09:11:46 -07001677 return __xfs_inode_clear_eofblocks_tag(ip,
Brian Foster7b7381f2016-10-24 14:21:00 +11001678 trace_xfs_perag_clear_cowblocks, XFS_ICI_COWBLOCKS_TAG);
Darrick J. Wong83104d42016-10-03 09:11:46 -07001679}