xfs: move inode generation count to VFS inode
[deliverable/linux.git] / fs / xfs / xfs_icache.c
1 /*
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"
20 #include "xfs_format.h"
21 #include "xfs_log_format.h"
22 #include "xfs_trans_resv.h"
23 #include "xfs_sb.h"
24 #include "xfs_mount.h"
25 #include "xfs_inode.h"
26 #include "xfs_error.h"
27 #include "xfs_trans.h"
28 #include "xfs_trans_priv.h"
29 #include "xfs_inode_item.h"
30 #include "xfs_quota.h"
31 #include "xfs_trace.h"
32 #include "xfs_icache.h"
33 #include "xfs_bmap_util.h"
34 #include "xfs_dquot_item.h"
35 #include "xfs_dquot.h"
36
37 #include <linux/kthread.h>
38 #include <linux/freezer.h>
39
40 STATIC void __xfs_inode_clear_reclaim_tag(struct xfs_mount *mp,
41 struct xfs_perag *pag, struct xfs_inode *ip);
42
43 /*
44 * Allocate and initialise an xfs_inode.
45 */
46 struct xfs_inode *
47 xfs_inode_alloc(
48 struct xfs_mount *mp,
49 xfs_ino_t ino)
50 {
51 struct xfs_inode *ip;
52
53 /*
54 * if this didn't occur in transactions, we could use
55 * KM_MAYFAIL and return NULL here on ENOMEM. Set the
56 * code up to do this anyway.
57 */
58 ip = kmem_zone_alloc(xfs_inode_zone, KM_SLEEP);
59 if (!ip)
60 return NULL;
61 if (inode_init_always(mp->m_super, VFS_I(ip))) {
62 kmem_zone_free(xfs_inode_zone, ip);
63 return NULL;
64 }
65
66 XFS_STATS_INC(mp, vn_active);
67 ASSERT(atomic_read(&ip->i_pincount) == 0);
68 ASSERT(!spin_is_locked(&ip->i_flags_lock));
69 ASSERT(!xfs_isiflocked(ip));
70 ASSERT(ip->i_ino == 0);
71
72 mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino);
73
74 /* initialise the xfs inode */
75 ip->i_ino = ino;
76 ip->i_mount = mp;
77 memset(&ip->i_imap, 0, sizeof(struct xfs_imap));
78 ip->i_afp = NULL;
79 memset(&ip->i_df, 0, sizeof(xfs_ifork_t));
80 ip->i_flags = 0;
81 ip->i_delayed_blks = 0;
82 memset(&ip->i_d, 0, sizeof(ip->i_d));
83
84 return ip;
85 }
86
87 STATIC void
88 xfs_inode_free_callback(
89 struct rcu_head *head)
90 {
91 struct inode *inode = container_of(head, struct inode, i_rcu);
92 struct xfs_inode *ip = XFS_I(inode);
93
94 kmem_zone_free(xfs_inode_zone, ip);
95 }
96
97 void
98 xfs_inode_free(
99 struct xfs_inode *ip)
100 {
101 switch (ip->i_d.di_mode & S_IFMT) {
102 case S_IFREG:
103 case S_IFDIR:
104 case S_IFLNK:
105 xfs_idestroy_fork(ip, XFS_DATA_FORK);
106 break;
107 }
108
109 if (ip->i_afp)
110 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
111
112 if (ip->i_itemp) {
113 ASSERT(!(ip->i_itemp->ili_item.li_flags & XFS_LI_IN_AIL));
114 xfs_inode_item_destroy(ip);
115 ip->i_itemp = NULL;
116 }
117
118 /*
119 * Because we use RCU freeing we need to ensure the inode always
120 * appears to be reclaimed with an invalid inode number when in the
121 * free state. The ip->i_flags_lock provides the barrier against lookup
122 * races.
123 */
124 spin_lock(&ip->i_flags_lock);
125 ip->i_flags = XFS_IRECLAIM;
126 ip->i_ino = 0;
127 spin_unlock(&ip->i_flags_lock);
128
129 /* asserts to verify all state is correct here */
130 ASSERT(atomic_read(&ip->i_pincount) == 0);
131 ASSERT(!xfs_isiflocked(ip));
132 XFS_STATS_DEC(ip->i_mount, vn_active);
133
134 call_rcu(&VFS_I(ip)->i_rcu, xfs_inode_free_callback);
135 }
136
137 /*
138 * When we recycle a reclaimable inode, we need to re-initialise the VFS inode
139 * part of the structure. This is made more complex by the fact we store
140 * information about the on-disk values in the VFS inode and so we can't just
141 * overwrite it's values unconditionally. Hence we save the parameters we
142 * need to retain across reinitialisation, and rewrite them into the VFS inode
143 * after resetting it's state even if resetting fails.
144 */
145 static int
146 xfs_reinit_inode(
147 struct xfs_mount *mp,
148 struct inode *inode)
149 {
150 int error;
151 uint32_t nlink = inode->i_nlink;
152 uint32_t generation = inode->i_generation;
153
154 error = inode_init_always(mp->m_super, inode);
155
156 set_nlink(inode, nlink);
157 inode->i_generation = generation;
158 return error;
159 }
160
161 /*
162 * Check the validity of the inode we just found it the cache
163 */
164 static int
165 xfs_iget_cache_hit(
166 struct xfs_perag *pag,
167 struct xfs_inode *ip,
168 xfs_ino_t ino,
169 int flags,
170 int lock_flags) __releases(RCU)
171 {
172 struct inode *inode = VFS_I(ip);
173 struct xfs_mount *mp = ip->i_mount;
174 int error;
175
176 /*
177 * check for re-use of an inode within an RCU grace period due to the
178 * radix tree nodes not being updated yet. We monitor for this by
179 * setting the inode number to zero before freeing the inode structure.
180 * If the inode has been reallocated and set up, then the inode number
181 * will not match, so check for that, too.
182 */
183 spin_lock(&ip->i_flags_lock);
184 if (ip->i_ino != ino) {
185 trace_xfs_iget_skip(ip);
186 XFS_STATS_INC(mp, xs_ig_frecycle);
187 error = -EAGAIN;
188 goto out_error;
189 }
190
191
192 /*
193 * If we are racing with another cache hit that is currently
194 * instantiating this inode or currently recycling it out of
195 * reclaimabe state, wait for the initialisation to complete
196 * before continuing.
197 *
198 * XXX(hch): eventually we should do something equivalent to
199 * wait_on_inode to wait for these flags to be cleared
200 * instead of polling for it.
201 */
202 if (ip->i_flags & (XFS_INEW|XFS_IRECLAIM)) {
203 trace_xfs_iget_skip(ip);
204 XFS_STATS_INC(mp, xs_ig_frecycle);
205 error = -EAGAIN;
206 goto out_error;
207 }
208
209 /*
210 * If lookup is racing with unlink return an error immediately.
211 */
212 if (ip->i_d.di_mode == 0 && !(flags & XFS_IGET_CREATE)) {
213 error = -ENOENT;
214 goto out_error;
215 }
216
217 /*
218 * If IRECLAIMABLE is set, we've torn down the VFS inode already.
219 * Need to carefully get it back into useable state.
220 */
221 if (ip->i_flags & XFS_IRECLAIMABLE) {
222 trace_xfs_iget_reclaim(ip);
223
224 /*
225 * We need to set XFS_IRECLAIM to prevent xfs_reclaim_inode
226 * from stomping over us while we recycle the inode. We can't
227 * clear the radix tree reclaimable tag yet as it requires
228 * pag_ici_lock to be held exclusive.
229 */
230 ip->i_flags |= XFS_IRECLAIM;
231
232 spin_unlock(&ip->i_flags_lock);
233 rcu_read_unlock();
234
235 error = xfs_reinit_inode(mp, inode);
236 if (error) {
237 /*
238 * Re-initializing the inode failed, and we are in deep
239 * trouble. Try to re-add it to the reclaim list.
240 */
241 rcu_read_lock();
242 spin_lock(&ip->i_flags_lock);
243
244 ip->i_flags &= ~(XFS_INEW | XFS_IRECLAIM);
245 ASSERT(ip->i_flags & XFS_IRECLAIMABLE);
246 trace_xfs_iget_reclaim_fail(ip);
247 goto out_error;
248 }
249
250 spin_lock(&pag->pag_ici_lock);
251 spin_lock(&ip->i_flags_lock);
252
253 /*
254 * Clear the per-lifetime state in the inode as we are now
255 * effectively a new inode and need to return to the initial
256 * state before reuse occurs.
257 */
258 ip->i_flags &= ~XFS_IRECLAIM_RESET_FLAGS;
259 ip->i_flags |= XFS_INEW;
260 __xfs_inode_clear_reclaim_tag(mp, pag, ip);
261 inode->i_state = I_NEW;
262
263 ASSERT(!rwsem_is_locked(&ip->i_iolock.mr_lock));
264 mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino);
265
266 spin_unlock(&ip->i_flags_lock);
267 spin_unlock(&pag->pag_ici_lock);
268 } else {
269 /* If the VFS inode is being torn down, pause and try again. */
270 if (!igrab(inode)) {
271 trace_xfs_iget_skip(ip);
272 error = -EAGAIN;
273 goto out_error;
274 }
275
276 /* We've got a live one. */
277 spin_unlock(&ip->i_flags_lock);
278 rcu_read_unlock();
279 trace_xfs_iget_hit(ip);
280 }
281
282 if (lock_flags != 0)
283 xfs_ilock(ip, lock_flags);
284
285 xfs_iflags_clear(ip, XFS_ISTALE | XFS_IDONTCACHE);
286 XFS_STATS_INC(mp, xs_ig_found);
287
288 return 0;
289
290 out_error:
291 spin_unlock(&ip->i_flags_lock);
292 rcu_read_unlock();
293 return error;
294 }
295
296
297 static int
298 xfs_iget_cache_miss(
299 struct xfs_mount *mp,
300 struct xfs_perag *pag,
301 xfs_trans_t *tp,
302 xfs_ino_t ino,
303 struct xfs_inode **ipp,
304 int flags,
305 int lock_flags)
306 {
307 struct xfs_inode *ip;
308 int error;
309 xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino);
310 int iflags;
311
312 ip = xfs_inode_alloc(mp, ino);
313 if (!ip)
314 return -ENOMEM;
315
316 error = xfs_iread(mp, tp, ip, flags);
317 if (error)
318 goto out_destroy;
319
320 trace_xfs_iget_miss(ip);
321
322 if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
323 error = -ENOENT;
324 goto out_destroy;
325 }
326
327 /*
328 * Preload the radix tree so we can insert safely under the
329 * write spinlock. Note that we cannot sleep inside the preload
330 * region. Since we can be called from transaction context, don't
331 * recurse into the file system.
332 */
333 if (radix_tree_preload(GFP_NOFS)) {
334 error = -EAGAIN;
335 goto out_destroy;
336 }
337
338 /*
339 * Because the inode hasn't been added to the radix-tree yet it can't
340 * be found by another thread, so we can do the non-sleeping lock here.
341 */
342 if (lock_flags) {
343 if (!xfs_ilock_nowait(ip, lock_flags))
344 BUG();
345 }
346
347 /*
348 * These values must be set before inserting the inode into the radix
349 * tree as the moment it is inserted a concurrent lookup (allowed by the
350 * RCU locking mechanism) can find it and that lookup must see that this
351 * is an inode currently under construction (i.e. that XFS_INEW is set).
352 * The ip->i_flags_lock that protects the XFS_INEW flag forms the
353 * memory barrier that ensures this detection works correctly at lookup
354 * time.
355 */
356 iflags = XFS_INEW;
357 if (flags & XFS_IGET_DONTCACHE)
358 iflags |= XFS_IDONTCACHE;
359 ip->i_udquot = NULL;
360 ip->i_gdquot = NULL;
361 ip->i_pdquot = NULL;
362 xfs_iflags_set(ip, iflags);
363
364 /* insert the new inode */
365 spin_lock(&pag->pag_ici_lock);
366 error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
367 if (unlikely(error)) {
368 WARN_ON(error != -EEXIST);
369 XFS_STATS_INC(mp, xs_ig_dup);
370 error = -EAGAIN;
371 goto out_preload_end;
372 }
373 spin_unlock(&pag->pag_ici_lock);
374 radix_tree_preload_end();
375
376 *ipp = ip;
377 return 0;
378
379 out_preload_end:
380 spin_unlock(&pag->pag_ici_lock);
381 radix_tree_preload_end();
382 if (lock_flags)
383 xfs_iunlock(ip, lock_flags);
384 out_destroy:
385 __destroy_inode(VFS_I(ip));
386 xfs_inode_free(ip);
387 return error;
388 }
389
390 /*
391 * Look up an inode by number in the given file system.
392 * The inode is looked up in the cache held in each AG.
393 * If the inode is found in the cache, initialise the vfs inode
394 * if necessary.
395 *
396 * If it is not in core, read it in from the file system's device,
397 * add it to the cache and initialise the vfs inode.
398 *
399 * The inode is locked according to the value of the lock_flags parameter.
400 * This flag parameter indicates how and if the inode's IO lock and inode lock
401 * should be taken.
402 *
403 * mp -- the mount point structure for the current file system. It points
404 * to the inode hash table.
405 * tp -- a pointer to the current transaction if there is one. This is
406 * simply passed through to the xfs_iread() call.
407 * ino -- the number of the inode desired. This is the unique identifier
408 * within the file system for the inode being requested.
409 * lock_flags -- flags indicating how to lock the inode. See the comment
410 * for xfs_ilock() for a list of valid values.
411 */
412 int
413 xfs_iget(
414 xfs_mount_t *mp,
415 xfs_trans_t *tp,
416 xfs_ino_t ino,
417 uint flags,
418 uint lock_flags,
419 xfs_inode_t **ipp)
420 {
421 xfs_inode_t *ip;
422 int error;
423 xfs_perag_t *pag;
424 xfs_agino_t agino;
425
426 /*
427 * xfs_reclaim_inode() uses the ILOCK to ensure an inode
428 * doesn't get freed while it's being referenced during a
429 * radix tree traversal here. It assumes this function
430 * aqcuires only the ILOCK (and therefore it has no need to
431 * involve the IOLOCK in this synchronization).
432 */
433 ASSERT((lock_flags & (XFS_IOLOCK_EXCL | XFS_IOLOCK_SHARED)) == 0);
434
435 /* reject inode numbers outside existing AGs */
436 if (!ino || XFS_INO_TO_AGNO(mp, ino) >= mp->m_sb.sb_agcount)
437 return -EINVAL;
438
439 XFS_STATS_INC(mp, xs_ig_attempts);
440
441 /* get the perag structure and ensure that it's inode capable */
442 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ino));
443 agino = XFS_INO_TO_AGINO(mp, ino);
444
445 again:
446 error = 0;
447 rcu_read_lock();
448 ip = radix_tree_lookup(&pag->pag_ici_root, agino);
449
450 if (ip) {
451 error = xfs_iget_cache_hit(pag, ip, ino, flags, lock_flags);
452 if (error)
453 goto out_error_or_again;
454 } else {
455 rcu_read_unlock();
456 XFS_STATS_INC(mp, xs_ig_missed);
457
458 error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip,
459 flags, lock_flags);
460 if (error)
461 goto out_error_or_again;
462 }
463 xfs_perag_put(pag);
464
465 *ipp = ip;
466
467 /*
468 * If we have a real type for an on-disk inode, we can setup the inode
469 * now. If it's a new inode being created, xfs_ialloc will handle it.
470 */
471 if (xfs_iflags_test(ip, XFS_INEW) && ip->i_d.di_mode != 0)
472 xfs_setup_existing_inode(ip);
473 return 0;
474
475 out_error_or_again:
476 if (error == -EAGAIN) {
477 delay(1);
478 goto again;
479 }
480 xfs_perag_put(pag);
481 return error;
482 }
483
484 /*
485 * The inode lookup is done in batches to keep the amount of lock traffic and
486 * radix tree lookups to a minimum. The batch size is a trade off between
487 * lookup reduction and stack usage. This is in the reclaim path, so we can't
488 * be too greedy.
489 */
490 #define XFS_LOOKUP_BATCH 32
491
492 STATIC int
493 xfs_inode_ag_walk_grab(
494 struct xfs_inode *ip)
495 {
496 struct inode *inode = VFS_I(ip);
497
498 ASSERT(rcu_read_lock_held());
499
500 /*
501 * check for stale RCU freed inode
502 *
503 * If the inode has been reallocated, it doesn't matter if it's not in
504 * the AG we are walking - we are walking for writeback, so if it
505 * passes all the "valid inode" checks and is dirty, then we'll write
506 * it back anyway. If it has been reallocated and still being
507 * initialised, the XFS_INEW check below will catch it.
508 */
509 spin_lock(&ip->i_flags_lock);
510 if (!ip->i_ino)
511 goto out_unlock_noent;
512
513 /* avoid new or reclaimable inodes. Leave for reclaim code to flush */
514 if (__xfs_iflags_test(ip, XFS_INEW | XFS_IRECLAIMABLE | XFS_IRECLAIM))
515 goto out_unlock_noent;
516 spin_unlock(&ip->i_flags_lock);
517
518 /* nothing to sync during shutdown */
519 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
520 return -EFSCORRUPTED;
521
522 /* If we can't grab the inode, it must on it's way to reclaim. */
523 if (!igrab(inode))
524 return -ENOENT;
525
526 /* inode is valid */
527 return 0;
528
529 out_unlock_noent:
530 spin_unlock(&ip->i_flags_lock);
531 return -ENOENT;
532 }
533
534 STATIC int
535 xfs_inode_ag_walk(
536 struct xfs_mount *mp,
537 struct xfs_perag *pag,
538 int (*execute)(struct xfs_inode *ip, int flags,
539 void *args),
540 int flags,
541 void *args,
542 int tag)
543 {
544 uint32_t first_index;
545 int last_error = 0;
546 int skipped;
547 int done;
548 int nr_found;
549
550 restart:
551 done = 0;
552 skipped = 0;
553 first_index = 0;
554 nr_found = 0;
555 do {
556 struct xfs_inode *batch[XFS_LOOKUP_BATCH];
557 int error = 0;
558 int i;
559
560 rcu_read_lock();
561
562 if (tag == -1)
563 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
564 (void **)batch, first_index,
565 XFS_LOOKUP_BATCH);
566 else
567 nr_found = radix_tree_gang_lookup_tag(
568 &pag->pag_ici_root,
569 (void **) batch, first_index,
570 XFS_LOOKUP_BATCH, tag);
571
572 if (!nr_found) {
573 rcu_read_unlock();
574 break;
575 }
576
577 /*
578 * Grab the inodes before we drop the lock. if we found
579 * nothing, nr == 0 and the loop will be skipped.
580 */
581 for (i = 0; i < nr_found; i++) {
582 struct xfs_inode *ip = batch[i];
583
584 if (done || xfs_inode_ag_walk_grab(ip))
585 batch[i] = NULL;
586
587 /*
588 * Update the index for the next lookup. Catch
589 * overflows into the next AG range which can occur if
590 * we have inodes in the last block of the AG and we
591 * are currently pointing to the last inode.
592 *
593 * Because we may see inodes that are from the wrong AG
594 * due to RCU freeing and reallocation, only update the
595 * index if it lies in this AG. It was a race that lead
596 * us to see this inode, so another lookup from the
597 * same index will not find it again.
598 */
599 if (XFS_INO_TO_AGNO(mp, ip->i_ino) != pag->pag_agno)
600 continue;
601 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
602 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
603 done = 1;
604 }
605
606 /* unlock now we've grabbed the inodes. */
607 rcu_read_unlock();
608
609 for (i = 0; i < nr_found; i++) {
610 if (!batch[i])
611 continue;
612 error = execute(batch[i], flags, args);
613 IRELE(batch[i]);
614 if (error == -EAGAIN) {
615 skipped++;
616 continue;
617 }
618 if (error && last_error != -EFSCORRUPTED)
619 last_error = error;
620 }
621
622 /* bail out if the filesystem is corrupted. */
623 if (error == -EFSCORRUPTED)
624 break;
625
626 cond_resched();
627
628 } while (nr_found && !done);
629
630 if (skipped) {
631 delay(1);
632 goto restart;
633 }
634 return last_error;
635 }
636
637 /*
638 * Background scanning to trim post-EOF preallocated space. This is queued
639 * based on the 'speculative_prealloc_lifetime' tunable (5m by default).
640 */
641 STATIC void
642 xfs_queue_eofblocks(
643 struct xfs_mount *mp)
644 {
645 rcu_read_lock();
646 if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_EOFBLOCKS_TAG))
647 queue_delayed_work(mp->m_eofblocks_workqueue,
648 &mp->m_eofblocks_work,
649 msecs_to_jiffies(xfs_eofb_secs * 1000));
650 rcu_read_unlock();
651 }
652
653 void
654 xfs_eofblocks_worker(
655 struct work_struct *work)
656 {
657 struct xfs_mount *mp = container_of(to_delayed_work(work),
658 struct xfs_mount, m_eofblocks_work);
659 xfs_icache_free_eofblocks(mp, NULL);
660 xfs_queue_eofblocks(mp);
661 }
662
663 int
664 xfs_inode_ag_iterator(
665 struct xfs_mount *mp,
666 int (*execute)(struct xfs_inode *ip, int flags,
667 void *args),
668 int flags,
669 void *args)
670 {
671 struct xfs_perag *pag;
672 int error = 0;
673 int last_error = 0;
674 xfs_agnumber_t ag;
675
676 ag = 0;
677 while ((pag = xfs_perag_get(mp, ag))) {
678 ag = pag->pag_agno + 1;
679 error = xfs_inode_ag_walk(mp, pag, execute, flags, args, -1);
680 xfs_perag_put(pag);
681 if (error) {
682 last_error = error;
683 if (error == -EFSCORRUPTED)
684 break;
685 }
686 }
687 return last_error;
688 }
689
690 int
691 xfs_inode_ag_iterator_tag(
692 struct xfs_mount *mp,
693 int (*execute)(struct xfs_inode *ip, int flags,
694 void *args),
695 int flags,
696 void *args,
697 int tag)
698 {
699 struct xfs_perag *pag;
700 int error = 0;
701 int last_error = 0;
702 xfs_agnumber_t ag;
703
704 ag = 0;
705 while ((pag = xfs_perag_get_tag(mp, ag, tag))) {
706 ag = pag->pag_agno + 1;
707 error = xfs_inode_ag_walk(mp, pag, execute, flags, args, tag);
708 xfs_perag_put(pag);
709 if (error) {
710 last_error = error;
711 if (error == -EFSCORRUPTED)
712 break;
713 }
714 }
715 return last_error;
716 }
717
718 /*
719 * Queue a new inode reclaim pass if there are reclaimable inodes and there
720 * isn't a reclaim pass already in progress. By default it runs every 5s based
721 * on the xfs periodic sync default of 30s. Perhaps this should have it's own
722 * tunable, but that can be done if this method proves to be ineffective or too
723 * aggressive.
724 */
725 static void
726 xfs_reclaim_work_queue(
727 struct xfs_mount *mp)
728 {
729
730 rcu_read_lock();
731 if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_RECLAIM_TAG)) {
732 queue_delayed_work(mp->m_reclaim_workqueue, &mp->m_reclaim_work,
733 msecs_to_jiffies(xfs_syncd_centisecs / 6 * 10));
734 }
735 rcu_read_unlock();
736 }
737
738 /*
739 * This is a fast pass over the inode cache to try to get reclaim moving on as
740 * many inodes as possible in a short period of time. It kicks itself every few
741 * seconds, as well as being kicked by the inode cache shrinker when memory
742 * goes low. It scans as quickly as possible avoiding locked inodes or those
743 * already being flushed, and once done schedules a future pass.
744 */
745 void
746 xfs_reclaim_worker(
747 struct work_struct *work)
748 {
749 struct xfs_mount *mp = container_of(to_delayed_work(work),
750 struct xfs_mount, m_reclaim_work);
751
752 xfs_reclaim_inodes(mp, SYNC_TRYLOCK);
753 xfs_reclaim_work_queue(mp);
754 }
755
756 static void
757 __xfs_inode_set_reclaim_tag(
758 struct xfs_perag *pag,
759 struct xfs_inode *ip)
760 {
761 radix_tree_tag_set(&pag->pag_ici_root,
762 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
763 XFS_ICI_RECLAIM_TAG);
764
765 if (!pag->pag_ici_reclaimable) {
766 /* propagate the reclaim tag up into the perag radix tree */
767 spin_lock(&ip->i_mount->m_perag_lock);
768 radix_tree_tag_set(&ip->i_mount->m_perag_tree,
769 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
770 XFS_ICI_RECLAIM_TAG);
771 spin_unlock(&ip->i_mount->m_perag_lock);
772
773 /* schedule periodic background inode reclaim */
774 xfs_reclaim_work_queue(ip->i_mount);
775
776 trace_xfs_perag_set_reclaim(ip->i_mount, pag->pag_agno,
777 -1, _RET_IP_);
778 }
779 pag->pag_ici_reclaimable++;
780 }
781
782 /*
783 * We set the inode flag atomically with the radix tree tag.
784 * Once we get tag lookups on the radix tree, this inode flag
785 * can go away.
786 */
787 void
788 xfs_inode_set_reclaim_tag(
789 xfs_inode_t *ip)
790 {
791 struct xfs_mount *mp = ip->i_mount;
792 struct xfs_perag *pag;
793
794 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
795 spin_lock(&pag->pag_ici_lock);
796 spin_lock(&ip->i_flags_lock);
797 __xfs_inode_set_reclaim_tag(pag, ip);
798 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
799 spin_unlock(&ip->i_flags_lock);
800 spin_unlock(&pag->pag_ici_lock);
801 xfs_perag_put(pag);
802 }
803
804 STATIC void
805 __xfs_inode_clear_reclaim(
806 xfs_perag_t *pag,
807 xfs_inode_t *ip)
808 {
809 pag->pag_ici_reclaimable--;
810 if (!pag->pag_ici_reclaimable) {
811 /* clear the reclaim tag from the perag radix tree */
812 spin_lock(&ip->i_mount->m_perag_lock);
813 radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
814 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
815 XFS_ICI_RECLAIM_TAG);
816 spin_unlock(&ip->i_mount->m_perag_lock);
817 trace_xfs_perag_clear_reclaim(ip->i_mount, pag->pag_agno,
818 -1, _RET_IP_);
819 }
820 }
821
822 STATIC void
823 __xfs_inode_clear_reclaim_tag(
824 xfs_mount_t *mp,
825 xfs_perag_t *pag,
826 xfs_inode_t *ip)
827 {
828 radix_tree_tag_clear(&pag->pag_ici_root,
829 XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
830 __xfs_inode_clear_reclaim(pag, ip);
831 }
832
833 /*
834 * Grab the inode for reclaim exclusively.
835 * Return 0 if we grabbed it, non-zero otherwise.
836 */
837 STATIC int
838 xfs_reclaim_inode_grab(
839 struct xfs_inode *ip,
840 int flags)
841 {
842 ASSERT(rcu_read_lock_held());
843
844 /* quick check for stale RCU freed inode */
845 if (!ip->i_ino)
846 return 1;
847
848 /*
849 * If we are asked for non-blocking operation, do unlocked checks to
850 * see if the inode already is being flushed or in reclaim to avoid
851 * lock traffic.
852 */
853 if ((flags & SYNC_TRYLOCK) &&
854 __xfs_iflags_test(ip, XFS_IFLOCK | XFS_IRECLAIM))
855 return 1;
856
857 /*
858 * The radix tree lock here protects a thread in xfs_iget from racing
859 * with us starting reclaim on the inode. Once we have the
860 * XFS_IRECLAIM flag set it will not touch us.
861 *
862 * Due to RCU lookup, we may find inodes that have been freed and only
863 * have XFS_IRECLAIM set. Indeed, we may see reallocated inodes that
864 * aren't candidates for reclaim at all, so we must check the
865 * XFS_IRECLAIMABLE is set first before proceeding to reclaim.
866 */
867 spin_lock(&ip->i_flags_lock);
868 if (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) ||
869 __xfs_iflags_test(ip, XFS_IRECLAIM)) {
870 /* not a reclaim candidate. */
871 spin_unlock(&ip->i_flags_lock);
872 return 1;
873 }
874 __xfs_iflags_set(ip, XFS_IRECLAIM);
875 spin_unlock(&ip->i_flags_lock);
876 return 0;
877 }
878
879 /*
880 * Inodes in different states need to be treated differently. The following
881 * table lists the inode states and the reclaim actions necessary:
882 *
883 * inode state iflush ret required action
884 * --------------- ---------- ---------------
885 * bad - reclaim
886 * shutdown EIO unpin and reclaim
887 * clean, unpinned 0 reclaim
888 * stale, unpinned 0 reclaim
889 * clean, pinned(*) 0 requeue
890 * stale, pinned EAGAIN requeue
891 * dirty, async - requeue
892 * dirty, sync 0 reclaim
893 *
894 * (*) dgc: I don't think the clean, pinned state is possible but it gets
895 * handled anyway given the order of checks implemented.
896 *
897 * Also, because we get the flush lock first, we know that any inode that has
898 * been flushed delwri has had the flush completed by the time we check that
899 * the inode is clean.
900 *
901 * Note that because the inode is flushed delayed write by AIL pushing, the
902 * flush lock may already be held here and waiting on it can result in very
903 * long latencies. Hence for sync reclaims, where we wait on the flush lock,
904 * the caller should push the AIL first before trying to reclaim inodes to
905 * minimise the amount of time spent waiting. For background relaim, we only
906 * bother to reclaim clean inodes anyway.
907 *
908 * Hence the order of actions after gaining the locks should be:
909 * bad => reclaim
910 * shutdown => unpin and reclaim
911 * pinned, async => requeue
912 * pinned, sync => unpin
913 * stale => reclaim
914 * clean => reclaim
915 * dirty, async => requeue
916 * dirty, sync => flush, wait and reclaim
917 */
918 STATIC int
919 xfs_reclaim_inode(
920 struct xfs_inode *ip,
921 struct xfs_perag *pag,
922 int sync_mode)
923 {
924 struct xfs_buf *bp = NULL;
925 int error;
926
927 restart:
928 error = 0;
929 xfs_ilock(ip, XFS_ILOCK_EXCL);
930 if (!xfs_iflock_nowait(ip)) {
931 if (!(sync_mode & SYNC_WAIT))
932 goto out;
933 xfs_iflock(ip);
934 }
935
936 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
937 xfs_iunpin_wait(ip);
938 xfs_iflush_abort(ip, false);
939 goto reclaim;
940 }
941 if (xfs_ipincount(ip)) {
942 if (!(sync_mode & SYNC_WAIT))
943 goto out_ifunlock;
944 xfs_iunpin_wait(ip);
945 }
946 if (xfs_iflags_test(ip, XFS_ISTALE))
947 goto reclaim;
948 if (xfs_inode_clean(ip))
949 goto reclaim;
950
951 /*
952 * Never flush out dirty data during non-blocking reclaim, as it would
953 * just contend with AIL pushing trying to do the same job.
954 */
955 if (!(sync_mode & SYNC_WAIT))
956 goto out_ifunlock;
957
958 /*
959 * Now we have an inode that needs flushing.
960 *
961 * Note that xfs_iflush will never block on the inode buffer lock, as
962 * xfs_ifree_cluster() can lock the inode buffer before it locks the
963 * ip->i_lock, and we are doing the exact opposite here. As a result,
964 * doing a blocking xfs_imap_to_bp() to get the cluster buffer would
965 * result in an ABBA deadlock with xfs_ifree_cluster().
966 *
967 * As xfs_ifree_cluser() must gather all inodes that are active in the
968 * cache to mark them stale, if we hit this case we don't actually want
969 * to do IO here - we want the inode marked stale so we can simply
970 * reclaim it. Hence if we get an EAGAIN error here, just unlock the
971 * inode, back off and try again. Hopefully the next pass through will
972 * see the stale flag set on the inode.
973 */
974 error = xfs_iflush(ip, &bp);
975 if (error == -EAGAIN) {
976 xfs_iunlock(ip, XFS_ILOCK_EXCL);
977 /* backoff longer than in xfs_ifree_cluster */
978 delay(2);
979 goto restart;
980 }
981
982 if (!error) {
983 error = xfs_bwrite(bp);
984 xfs_buf_relse(bp);
985 }
986
987 xfs_iflock(ip);
988 reclaim:
989 xfs_ifunlock(ip);
990 xfs_iunlock(ip, XFS_ILOCK_EXCL);
991
992 XFS_STATS_INC(ip->i_mount, xs_ig_reclaims);
993 /*
994 * Remove the inode from the per-AG radix tree.
995 *
996 * Because radix_tree_delete won't complain even if the item was never
997 * added to the tree assert that it's been there before to catch
998 * problems with the inode life time early on.
999 */
1000 spin_lock(&pag->pag_ici_lock);
1001 if (!radix_tree_delete(&pag->pag_ici_root,
1002 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino)))
1003 ASSERT(0);
1004 __xfs_inode_clear_reclaim(pag, ip);
1005 spin_unlock(&pag->pag_ici_lock);
1006
1007 /*
1008 * Here we do an (almost) spurious inode lock in order to coordinate
1009 * with inode cache radix tree lookups. This is because the lookup
1010 * can reference the inodes in the cache without taking references.
1011 *
1012 * We make that OK here by ensuring that we wait until the inode is
1013 * unlocked after the lookup before we go ahead and free it.
1014 */
1015 xfs_ilock(ip, XFS_ILOCK_EXCL);
1016 xfs_qm_dqdetach(ip);
1017 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1018
1019 xfs_inode_free(ip);
1020 return error;
1021
1022 out_ifunlock:
1023 xfs_ifunlock(ip);
1024 out:
1025 xfs_iflags_clear(ip, XFS_IRECLAIM);
1026 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1027 /*
1028 * We could return -EAGAIN here to make reclaim rescan the inode tree in
1029 * a short while. However, this just burns CPU time scanning the tree
1030 * waiting for IO to complete and the reclaim work never goes back to
1031 * the idle state. Instead, return 0 to let the next scheduled
1032 * background reclaim attempt to reclaim the inode again.
1033 */
1034 return 0;
1035 }
1036
1037 /*
1038 * Walk the AGs and reclaim the inodes in them. Even if the filesystem is
1039 * corrupted, we still want to try to reclaim all the inodes. If we don't,
1040 * then a shut down during filesystem unmount reclaim walk leak all the
1041 * unreclaimed inodes.
1042 */
1043 STATIC int
1044 xfs_reclaim_inodes_ag(
1045 struct xfs_mount *mp,
1046 int flags,
1047 int *nr_to_scan)
1048 {
1049 struct xfs_perag *pag;
1050 int error = 0;
1051 int last_error = 0;
1052 xfs_agnumber_t ag;
1053 int trylock = flags & SYNC_TRYLOCK;
1054 int skipped;
1055
1056 restart:
1057 ag = 0;
1058 skipped = 0;
1059 while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
1060 unsigned long first_index = 0;
1061 int done = 0;
1062 int nr_found = 0;
1063
1064 ag = pag->pag_agno + 1;
1065
1066 if (trylock) {
1067 if (!mutex_trylock(&pag->pag_ici_reclaim_lock)) {
1068 skipped++;
1069 xfs_perag_put(pag);
1070 continue;
1071 }
1072 first_index = pag->pag_ici_reclaim_cursor;
1073 } else
1074 mutex_lock(&pag->pag_ici_reclaim_lock);
1075
1076 do {
1077 struct xfs_inode *batch[XFS_LOOKUP_BATCH];
1078 int i;
1079
1080 rcu_read_lock();
1081 nr_found = radix_tree_gang_lookup_tag(
1082 &pag->pag_ici_root,
1083 (void **)batch, first_index,
1084 XFS_LOOKUP_BATCH,
1085 XFS_ICI_RECLAIM_TAG);
1086 if (!nr_found) {
1087 done = 1;
1088 rcu_read_unlock();
1089 break;
1090 }
1091
1092 /*
1093 * Grab the inodes before we drop the lock. if we found
1094 * nothing, nr == 0 and the loop will be skipped.
1095 */
1096 for (i = 0; i < nr_found; i++) {
1097 struct xfs_inode *ip = batch[i];
1098
1099 if (done || xfs_reclaim_inode_grab(ip, flags))
1100 batch[i] = NULL;
1101
1102 /*
1103 * Update the index for the next lookup. Catch
1104 * overflows into the next AG range which can
1105 * occur if we have inodes in the last block of
1106 * the AG and we are currently pointing to the
1107 * last inode.
1108 *
1109 * Because we may see inodes that are from the
1110 * wrong AG due to RCU freeing and
1111 * reallocation, only update the index if it
1112 * lies in this AG. It was a race that lead us
1113 * to see this inode, so another lookup from
1114 * the same index will not find it again.
1115 */
1116 if (XFS_INO_TO_AGNO(mp, ip->i_ino) !=
1117 pag->pag_agno)
1118 continue;
1119 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
1120 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
1121 done = 1;
1122 }
1123
1124 /* unlock now we've grabbed the inodes. */
1125 rcu_read_unlock();
1126
1127 for (i = 0; i < nr_found; i++) {
1128 if (!batch[i])
1129 continue;
1130 error = xfs_reclaim_inode(batch[i], pag, flags);
1131 if (error && last_error != -EFSCORRUPTED)
1132 last_error = error;
1133 }
1134
1135 *nr_to_scan -= XFS_LOOKUP_BATCH;
1136
1137 cond_resched();
1138
1139 } while (nr_found && !done && *nr_to_scan > 0);
1140
1141 if (trylock && !done)
1142 pag->pag_ici_reclaim_cursor = first_index;
1143 else
1144 pag->pag_ici_reclaim_cursor = 0;
1145 mutex_unlock(&pag->pag_ici_reclaim_lock);
1146 xfs_perag_put(pag);
1147 }
1148
1149 /*
1150 * if we skipped any AG, and we still have scan count remaining, do
1151 * another pass this time using blocking reclaim semantics (i.e
1152 * waiting on the reclaim locks and ignoring the reclaim cursors). This
1153 * ensure that when we get more reclaimers than AGs we block rather
1154 * than spin trying to execute reclaim.
1155 */
1156 if (skipped && (flags & SYNC_WAIT) && *nr_to_scan > 0) {
1157 trylock = 0;
1158 goto restart;
1159 }
1160 return last_error;
1161 }
1162
1163 int
1164 xfs_reclaim_inodes(
1165 xfs_mount_t *mp,
1166 int mode)
1167 {
1168 int nr_to_scan = INT_MAX;
1169
1170 return xfs_reclaim_inodes_ag(mp, mode, &nr_to_scan);
1171 }
1172
1173 /*
1174 * Scan a certain number of inodes for reclaim.
1175 *
1176 * When called we make sure that there is a background (fast) inode reclaim in
1177 * progress, while we will throttle the speed of reclaim via doing synchronous
1178 * reclaim of inodes. That means if we come across dirty inodes, we wait for
1179 * them to be cleaned, which we hope will not be very long due to the
1180 * background walker having already kicked the IO off on those dirty inodes.
1181 */
1182 long
1183 xfs_reclaim_inodes_nr(
1184 struct xfs_mount *mp,
1185 int nr_to_scan)
1186 {
1187 /* kick background reclaimer and push the AIL */
1188 xfs_reclaim_work_queue(mp);
1189 xfs_ail_push_all(mp->m_ail);
1190
1191 return xfs_reclaim_inodes_ag(mp, SYNC_TRYLOCK | SYNC_WAIT, &nr_to_scan);
1192 }
1193
1194 /*
1195 * Return the number of reclaimable inodes in the filesystem for
1196 * the shrinker to determine how much to reclaim.
1197 */
1198 int
1199 xfs_reclaim_inodes_count(
1200 struct xfs_mount *mp)
1201 {
1202 struct xfs_perag *pag;
1203 xfs_agnumber_t ag = 0;
1204 int reclaimable = 0;
1205
1206 while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
1207 ag = pag->pag_agno + 1;
1208 reclaimable += pag->pag_ici_reclaimable;
1209 xfs_perag_put(pag);
1210 }
1211 return reclaimable;
1212 }
1213
1214 STATIC int
1215 xfs_inode_match_id(
1216 struct xfs_inode *ip,
1217 struct xfs_eofblocks *eofb)
1218 {
1219 if ((eofb->eof_flags & XFS_EOF_FLAGS_UID) &&
1220 !uid_eq(VFS_I(ip)->i_uid, eofb->eof_uid))
1221 return 0;
1222
1223 if ((eofb->eof_flags & XFS_EOF_FLAGS_GID) &&
1224 !gid_eq(VFS_I(ip)->i_gid, eofb->eof_gid))
1225 return 0;
1226
1227 if ((eofb->eof_flags & XFS_EOF_FLAGS_PRID) &&
1228 xfs_get_projid(ip) != eofb->eof_prid)
1229 return 0;
1230
1231 return 1;
1232 }
1233
1234 /*
1235 * A union-based inode filtering algorithm. Process the inode if any of the
1236 * criteria match. This is for global/internal scans only.
1237 */
1238 STATIC int
1239 xfs_inode_match_id_union(
1240 struct xfs_inode *ip,
1241 struct xfs_eofblocks *eofb)
1242 {
1243 if ((eofb->eof_flags & XFS_EOF_FLAGS_UID) &&
1244 uid_eq(VFS_I(ip)->i_uid, eofb->eof_uid))
1245 return 1;
1246
1247 if ((eofb->eof_flags & XFS_EOF_FLAGS_GID) &&
1248 gid_eq(VFS_I(ip)->i_gid, eofb->eof_gid))
1249 return 1;
1250
1251 if ((eofb->eof_flags & XFS_EOF_FLAGS_PRID) &&
1252 xfs_get_projid(ip) == eofb->eof_prid)
1253 return 1;
1254
1255 return 0;
1256 }
1257
1258 STATIC int
1259 xfs_inode_free_eofblocks(
1260 struct xfs_inode *ip,
1261 int flags,
1262 void *args)
1263 {
1264 int ret;
1265 struct xfs_eofblocks *eofb = args;
1266 bool need_iolock = true;
1267 int match;
1268
1269 ASSERT(!eofb || (eofb && eofb->eof_scan_owner != 0));
1270
1271 if (!xfs_can_free_eofblocks(ip, false)) {
1272 /* inode could be preallocated or append-only */
1273 trace_xfs_inode_free_eofblocks_invalid(ip);
1274 xfs_inode_clear_eofblocks_tag(ip);
1275 return 0;
1276 }
1277
1278 /*
1279 * If the mapping is dirty the operation can block and wait for some
1280 * time. Unless we are waiting, skip it.
1281 */
1282 if (!(flags & SYNC_WAIT) &&
1283 mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_DIRTY))
1284 return 0;
1285
1286 if (eofb) {
1287 if (eofb->eof_flags & XFS_EOF_FLAGS_UNION)
1288 match = xfs_inode_match_id_union(ip, eofb);
1289 else
1290 match = xfs_inode_match_id(ip, eofb);
1291 if (!match)
1292 return 0;
1293
1294 /* skip the inode if the file size is too small */
1295 if (eofb->eof_flags & XFS_EOF_FLAGS_MINFILESIZE &&
1296 XFS_ISIZE(ip) < eofb->eof_min_file_size)
1297 return 0;
1298
1299 /*
1300 * A scan owner implies we already hold the iolock. Skip it in
1301 * xfs_free_eofblocks() to avoid deadlock. This also eliminates
1302 * the possibility of EAGAIN being returned.
1303 */
1304 if (eofb->eof_scan_owner == ip->i_ino)
1305 need_iolock = false;
1306 }
1307
1308 ret = xfs_free_eofblocks(ip->i_mount, ip, need_iolock);
1309
1310 /* don't revisit the inode if we're not waiting */
1311 if (ret == -EAGAIN && !(flags & SYNC_WAIT))
1312 ret = 0;
1313
1314 return ret;
1315 }
1316
1317 int
1318 xfs_icache_free_eofblocks(
1319 struct xfs_mount *mp,
1320 struct xfs_eofblocks *eofb)
1321 {
1322 int flags = SYNC_TRYLOCK;
1323
1324 if (eofb && (eofb->eof_flags & XFS_EOF_FLAGS_SYNC))
1325 flags = SYNC_WAIT;
1326
1327 return xfs_inode_ag_iterator_tag(mp, xfs_inode_free_eofblocks, flags,
1328 eofb, XFS_ICI_EOFBLOCKS_TAG);
1329 }
1330
1331 /*
1332 * Run eofblocks scans on the quotas applicable to the inode. For inodes with
1333 * multiple quotas, we don't know exactly which quota caused an allocation
1334 * failure. We make a best effort by including each quota under low free space
1335 * conditions (less than 1% free space) in the scan.
1336 */
1337 int
1338 xfs_inode_free_quota_eofblocks(
1339 struct xfs_inode *ip)
1340 {
1341 int scan = 0;
1342 struct xfs_eofblocks eofb = {0};
1343 struct xfs_dquot *dq;
1344
1345 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
1346
1347 /*
1348 * Set the scan owner to avoid a potential livelock. Otherwise, the scan
1349 * can repeatedly trylock on the inode we're currently processing. We
1350 * run a sync scan to increase effectiveness and use the union filter to
1351 * cover all applicable quotas in a single scan.
1352 */
1353 eofb.eof_scan_owner = ip->i_ino;
1354 eofb.eof_flags = XFS_EOF_FLAGS_UNION|XFS_EOF_FLAGS_SYNC;
1355
1356 if (XFS_IS_UQUOTA_ENFORCED(ip->i_mount)) {
1357 dq = xfs_inode_dquot(ip, XFS_DQ_USER);
1358 if (dq && xfs_dquot_lowsp(dq)) {
1359 eofb.eof_uid = VFS_I(ip)->i_uid;
1360 eofb.eof_flags |= XFS_EOF_FLAGS_UID;
1361 scan = 1;
1362 }
1363 }
1364
1365 if (XFS_IS_GQUOTA_ENFORCED(ip->i_mount)) {
1366 dq = xfs_inode_dquot(ip, XFS_DQ_GROUP);
1367 if (dq && xfs_dquot_lowsp(dq)) {
1368 eofb.eof_gid = VFS_I(ip)->i_gid;
1369 eofb.eof_flags |= XFS_EOF_FLAGS_GID;
1370 scan = 1;
1371 }
1372 }
1373
1374 if (scan)
1375 xfs_icache_free_eofblocks(ip->i_mount, &eofb);
1376
1377 return scan;
1378 }
1379
1380 void
1381 xfs_inode_set_eofblocks_tag(
1382 xfs_inode_t *ip)
1383 {
1384 struct xfs_mount *mp = ip->i_mount;
1385 struct xfs_perag *pag;
1386 int tagged;
1387
1388 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
1389 spin_lock(&pag->pag_ici_lock);
1390 trace_xfs_inode_set_eofblocks_tag(ip);
1391
1392 tagged = radix_tree_tagged(&pag->pag_ici_root,
1393 XFS_ICI_EOFBLOCKS_TAG);
1394 radix_tree_tag_set(&pag->pag_ici_root,
1395 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
1396 XFS_ICI_EOFBLOCKS_TAG);
1397 if (!tagged) {
1398 /* propagate the eofblocks tag up into the perag radix tree */
1399 spin_lock(&ip->i_mount->m_perag_lock);
1400 radix_tree_tag_set(&ip->i_mount->m_perag_tree,
1401 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
1402 XFS_ICI_EOFBLOCKS_TAG);
1403 spin_unlock(&ip->i_mount->m_perag_lock);
1404
1405 /* kick off background trimming */
1406 xfs_queue_eofblocks(ip->i_mount);
1407
1408 trace_xfs_perag_set_eofblocks(ip->i_mount, pag->pag_agno,
1409 -1, _RET_IP_);
1410 }
1411
1412 spin_unlock(&pag->pag_ici_lock);
1413 xfs_perag_put(pag);
1414 }
1415
1416 void
1417 xfs_inode_clear_eofblocks_tag(
1418 xfs_inode_t *ip)
1419 {
1420 struct xfs_mount *mp = ip->i_mount;
1421 struct xfs_perag *pag;
1422
1423 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
1424 spin_lock(&pag->pag_ici_lock);
1425 trace_xfs_inode_clear_eofblocks_tag(ip);
1426
1427 radix_tree_tag_clear(&pag->pag_ici_root,
1428 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
1429 XFS_ICI_EOFBLOCKS_TAG);
1430 if (!radix_tree_tagged(&pag->pag_ici_root, XFS_ICI_EOFBLOCKS_TAG)) {
1431 /* clear the eofblocks tag from the perag radix tree */
1432 spin_lock(&ip->i_mount->m_perag_lock);
1433 radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
1434 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
1435 XFS_ICI_EOFBLOCKS_TAG);
1436 spin_unlock(&ip->i_mount->m_perag_lock);
1437 trace_xfs_perag_clear_eofblocks(ip->i_mount, pag->pag_agno,
1438 -1, _RET_IP_);
1439 }
1440
1441 spin_unlock(&pag->pag_ici_lock);
1442 xfs_perag_put(pag);
1443 }
1444
This page took 0.061144 seconds and 5 git commands to generate.