xfs_icache.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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_types.h"
  21. #include "xfs_log.h"
  22. #include "xfs_log_priv.h"
  23. #include "xfs_inum.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_trans_priv.h"
  26. #include "xfs_sb.h"
  27. #include "xfs_ag.h"
  28. #include "xfs_mount.h"
  29. #include "xfs_bmap_btree.h"
  30. #include "xfs_inode.h"
  31. #include "xfs_dinode.h"
  32. #include "xfs_error.h"
  33. #include "xfs_filestream.h"
  34. #include "xfs_vnodeops.h"
  35. #include "xfs_inode_item.h"
  36. #include "xfs_quota.h"
  37. #include "xfs_trace.h"
  38. #include "xfs_fsops.h"
  39. #include "xfs_icache.h"
  40. #include <linux/kthread.h>
  41. #include <linux/freezer.h>
  42. /*
  43. * The inode lookup is done in batches to keep the amount of lock traffic and
  44. * radix tree lookups to a minimum. The batch size is a trade off between
  45. * lookup reduction and stack usage. This is in the reclaim path, so we can't
  46. * be too greedy.
  47. */
  48. #define XFS_LOOKUP_BATCH 32
  49. STATIC int
  50. xfs_inode_ag_walk_grab(
  51. struct xfs_inode *ip)
  52. {
  53. struct inode *inode = VFS_I(ip);
  54. ASSERT(rcu_read_lock_held());
  55. /*
  56. * check for stale RCU freed inode
  57. *
  58. * If the inode has been reallocated, it doesn't matter if it's not in
  59. * the AG we are walking - we are walking for writeback, so if it
  60. * passes all the "valid inode" checks and is dirty, then we'll write
  61. * it back anyway. If it has been reallocated and still being
  62. * initialised, the XFS_INEW check below will catch it.
  63. */
  64. spin_lock(&ip->i_flags_lock);
  65. if (!ip->i_ino)
  66. goto out_unlock_noent;
  67. /* avoid new or reclaimable inodes. Leave for reclaim code to flush */
  68. if (__xfs_iflags_test(ip, XFS_INEW | XFS_IRECLAIMABLE | XFS_IRECLAIM))
  69. goto out_unlock_noent;
  70. spin_unlock(&ip->i_flags_lock);
  71. /* nothing to sync during shutdown */
  72. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  73. return EFSCORRUPTED;
  74. /* If we can't grab the inode, it must on it's way to reclaim. */
  75. if (!igrab(inode))
  76. return ENOENT;
  77. if (is_bad_inode(inode)) {
  78. IRELE(ip);
  79. return ENOENT;
  80. }
  81. /* inode is valid */
  82. return 0;
  83. out_unlock_noent:
  84. spin_unlock(&ip->i_flags_lock);
  85. return ENOENT;
  86. }
  87. STATIC int
  88. xfs_inode_ag_walk(
  89. struct xfs_mount *mp,
  90. struct xfs_perag *pag,
  91. int (*execute)(struct xfs_inode *ip,
  92. struct xfs_perag *pag, int flags),
  93. int flags)
  94. {
  95. uint32_t first_index;
  96. int last_error = 0;
  97. int skipped;
  98. int done;
  99. int nr_found;
  100. restart:
  101. done = 0;
  102. skipped = 0;
  103. first_index = 0;
  104. nr_found = 0;
  105. do {
  106. struct xfs_inode *batch[XFS_LOOKUP_BATCH];
  107. int error = 0;
  108. int i;
  109. rcu_read_lock();
  110. nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
  111. (void **)batch, first_index,
  112. XFS_LOOKUP_BATCH);
  113. if (!nr_found) {
  114. rcu_read_unlock();
  115. break;
  116. }
  117. /*
  118. * Grab the inodes before we drop the lock. if we found
  119. * nothing, nr == 0 and the loop will be skipped.
  120. */
  121. for (i = 0; i < nr_found; i++) {
  122. struct xfs_inode *ip = batch[i];
  123. if (done || xfs_inode_ag_walk_grab(ip))
  124. batch[i] = NULL;
  125. /*
  126. * Update the index for the next lookup. Catch
  127. * overflows into the next AG range which can occur if
  128. * we have inodes in the last block of the AG and we
  129. * are currently pointing to the last inode.
  130. *
  131. * Because we may see inodes that are from the wrong AG
  132. * due to RCU freeing and reallocation, only update the
  133. * index if it lies in this AG. It was a race that lead
  134. * us to see this inode, so another lookup from the
  135. * same index will not find it again.
  136. */
  137. if (XFS_INO_TO_AGNO(mp, ip->i_ino) != pag->pag_agno)
  138. continue;
  139. first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
  140. if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
  141. done = 1;
  142. }
  143. /* unlock now we've grabbed the inodes. */
  144. rcu_read_unlock();
  145. for (i = 0; i < nr_found; i++) {
  146. if (!batch[i])
  147. continue;
  148. error = execute(batch[i], pag, flags);
  149. IRELE(batch[i]);
  150. if (error == EAGAIN) {
  151. skipped++;
  152. continue;
  153. }
  154. if (error && last_error != EFSCORRUPTED)
  155. last_error = error;
  156. }
  157. /* bail out if the filesystem is corrupted. */
  158. if (error == EFSCORRUPTED)
  159. break;
  160. cond_resched();
  161. } while (nr_found && !done);
  162. if (skipped) {
  163. delay(1);
  164. goto restart;
  165. }
  166. return last_error;
  167. }
  168. int
  169. xfs_inode_ag_iterator(
  170. struct xfs_mount *mp,
  171. int (*execute)(struct xfs_inode *ip,
  172. struct xfs_perag *pag, int flags),
  173. int flags)
  174. {
  175. struct xfs_perag *pag;
  176. int error = 0;
  177. int last_error = 0;
  178. xfs_agnumber_t ag;
  179. ag = 0;
  180. while ((pag = xfs_perag_get(mp, ag))) {
  181. ag = pag->pag_agno + 1;
  182. error = xfs_inode_ag_walk(mp, pag, execute, flags);
  183. xfs_perag_put(pag);
  184. if (error) {
  185. last_error = error;
  186. if (error == EFSCORRUPTED)
  187. break;
  188. }
  189. }
  190. return XFS_ERROR(last_error);
  191. }
  192. /*
  193. * Queue a new inode reclaim pass if there are reclaimable inodes and there
  194. * isn't a reclaim pass already in progress. By default it runs every 5s based
  195. * on the xfs periodic sync default of 30s. Perhaps this should have it's own
  196. * tunable, but that can be done if this method proves to be ineffective or too
  197. * aggressive.
  198. */
  199. static void
  200. xfs_reclaim_work_queue(
  201. struct xfs_mount *mp)
  202. {
  203. rcu_read_lock();
  204. if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_RECLAIM_TAG)) {
  205. queue_delayed_work(mp->m_reclaim_workqueue, &mp->m_reclaim_work,
  206. msecs_to_jiffies(xfs_syncd_centisecs / 6 * 10));
  207. }
  208. rcu_read_unlock();
  209. }
  210. /*
  211. * This is a fast pass over the inode cache to try to get reclaim moving on as
  212. * many inodes as possible in a short period of time. It kicks itself every few
  213. * seconds, as well as being kicked by the inode cache shrinker when memory
  214. * goes low. It scans as quickly as possible avoiding locked inodes or those
  215. * already being flushed, and once done schedules a future pass.
  216. */
  217. void
  218. xfs_reclaim_worker(
  219. struct work_struct *work)
  220. {
  221. struct xfs_mount *mp = container_of(to_delayed_work(work),
  222. struct xfs_mount, m_reclaim_work);
  223. xfs_reclaim_inodes(mp, SYNC_TRYLOCK);
  224. xfs_reclaim_work_queue(mp);
  225. }
  226. void
  227. __xfs_inode_set_reclaim_tag(
  228. struct xfs_perag *pag,
  229. struct xfs_inode *ip)
  230. {
  231. radix_tree_tag_set(&pag->pag_ici_root,
  232. XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
  233. XFS_ICI_RECLAIM_TAG);
  234. if (!pag->pag_ici_reclaimable) {
  235. /* propagate the reclaim tag up into the perag radix tree */
  236. spin_lock(&ip->i_mount->m_perag_lock);
  237. radix_tree_tag_set(&ip->i_mount->m_perag_tree,
  238. XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
  239. XFS_ICI_RECLAIM_TAG);
  240. spin_unlock(&ip->i_mount->m_perag_lock);
  241. /* schedule periodic background inode reclaim */
  242. xfs_reclaim_work_queue(ip->i_mount);
  243. trace_xfs_perag_set_reclaim(ip->i_mount, pag->pag_agno,
  244. -1, _RET_IP_);
  245. }
  246. pag->pag_ici_reclaimable++;
  247. }
  248. /*
  249. * We set the inode flag atomically with the radix tree tag.
  250. * Once we get tag lookups on the radix tree, this inode flag
  251. * can go away.
  252. */
  253. void
  254. xfs_inode_set_reclaim_tag(
  255. xfs_inode_t *ip)
  256. {
  257. struct xfs_mount *mp = ip->i_mount;
  258. struct xfs_perag *pag;
  259. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
  260. spin_lock(&pag->pag_ici_lock);
  261. spin_lock(&ip->i_flags_lock);
  262. __xfs_inode_set_reclaim_tag(pag, ip);
  263. __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
  264. spin_unlock(&ip->i_flags_lock);
  265. spin_unlock(&pag->pag_ici_lock);
  266. xfs_perag_put(pag);
  267. }
  268. STATIC void
  269. __xfs_inode_clear_reclaim(
  270. xfs_perag_t *pag,
  271. xfs_inode_t *ip)
  272. {
  273. pag->pag_ici_reclaimable--;
  274. if (!pag->pag_ici_reclaimable) {
  275. /* clear the reclaim tag from the perag radix tree */
  276. spin_lock(&ip->i_mount->m_perag_lock);
  277. radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
  278. XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
  279. XFS_ICI_RECLAIM_TAG);
  280. spin_unlock(&ip->i_mount->m_perag_lock);
  281. trace_xfs_perag_clear_reclaim(ip->i_mount, pag->pag_agno,
  282. -1, _RET_IP_);
  283. }
  284. }
  285. void
  286. __xfs_inode_clear_reclaim_tag(
  287. xfs_mount_t *mp,
  288. xfs_perag_t *pag,
  289. xfs_inode_t *ip)
  290. {
  291. radix_tree_tag_clear(&pag->pag_ici_root,
  292. XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
  293. __xfs_inode_clear_reclaim(pag, ip);
  294. }
  295. /*
  296. * Grab the inode for reclaim exclusively.
  297. * Return 0 if we grabbed it, non-zero otherwise.
  298. */
  299. STATIC int
  300. xfs_reclaim_inode_grab(
  301. struct xfs_inode *ip,
  302. int flags)
  303. {
  304. ASSERT(rcu_read_lock_held());
  305. /* quick check for stale RCU freed inode */
  306. if (!ip->i_ino)
  307. return 1;
  308. /*
  309. * If we are asked for non-blocking operation, do unlocked checks to
  310. * see if the inode already is being flushed or in reclaim to avoid
  311. * lock traffic.
  312. */
  313. if ((flags & SYNC_TRYLOCK) &&
  314. __xfs_iflags_test(ip, XFS_IFLOCK | XFS_IRECLAIM))
  315. return 1;
  316. /*
  317. * The radix tree lock here protects a thread in xfs_iget from racing
  318. * with us starting reclaim on the inode. Once we have the
  319. * XFS_IRECLAIM flag set it will not touch us.
  320. *
  321. * Due to RCU lookup, we may find inodes that have been freed and only
  322. * have XFS_IRECLAIM set. Indeed, we may see reallocated inodes that
  323. * aren't candidates for reclaim at all, so we must check the
  324. * XFS_IRECLAIMABLE is set first before proceeding to reclaim.
  325. */
  326. spin_lock(&ip->i_flags_lock);
  327. if (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) ||
  328. __xfs_iflags_test(ip, XFS_IRECLAIM)) {
  329. /* not a reclaim candidate. */
  330. spin_unlock(&ip->i_flags_lock);
  331. return 1;
  332. }
  333. __xfs_iflags_set(ip, XFS_IRECLAIM);
  334. spin_unlock(&ip->i_flags_lock);
  335. return 0;
  336. }
  337. /*
  338. * Inodes in different states need to be treated differently. The following
  339. * table lists the inode states and the reclaim actions necessary:
  340. *
  341. * inode state iflush ret required action
  342. * --------------- ---------- ---------------
  343. * bad - reclaim
  344. * shutdown EIO unpin and reclaim
  345. * clean, unpinned 0 reclaim
  346. * stale, unpinned 0 reclaim
  347. * clean, pinned(*) 0 requeue
  348. * stale, pinned EAGAIN requeue
  349. * dirty, async - requeue
  350. * dirty, sync 0 reclaim
  351. *
  352. * (*) dgc: I don't think the clean, pinned state is possible but it gets
  353. * handled anyway given the order of checks implemented.
  354. *
  355. * Also, because we get the flush lock first, we know that any inode that has
  356. * been flushed delwri has had the flush completed by the time we check that
  357. * the inode is clean.
  358. *
  359. * Note that because the inode is flushed delayed write by AIL pushing, the
  360. * flush lock may already be held here and waiting on it can result in very
  361. * long latencies. Hence for sync reclaims, where we wait on the flush lock,
  362. * the caller should push the AIL first before trying to reclaim inodes to
  363. * minimise the amount of time spent waiting. For background relaim, we only
  364. * bother to reclaim clean inodes anyway.
  365. *
  366. * Hence the order of actions after gaining the locks should be:
  367. * bad => reclaim
  368. * shutdown => unpin and reclaim
  369. * pinned, async => requeue
  370. * pinned, sync => unpin
  371. * stale => reclaim
  372. * clean => reclaim
  373. * dirty, async => requeue
  374. * dirty, sync => flush, wait and reclaim
  375. */
  376. STATIC int
  377. xfs_reclaim_inode(
  378. struct xfs_inode *ip,
  379. struct xfs_perag *pag,
  380. int sync_mode)
  381. {
  382. struct xfs_buf *bp = NULL;
  383. int error;
  384. restart:
  385. error = 0;
  386. xfs_ilock(ip, XFS_ILOCK_EXCL);
  387. if (!xfs_iflock_nowait(ip)) {
  388. if (!(sync_mode & SYNC_WAIT))
  389. goto out;
  390. xfs_iflock(ip);
  391. }
  392. if (is_bad_inode(VFS_I(ip)))
  393. goto reclaim;
  394. if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  395. xfs_iunpin_wait(ip);
  396. xfs_iflush_abort(ip, false);
  397. goto reclaim;
  398. }
  399. if (xfs_ipincount(ip)) {
  400. if (!(sync_mode & SYNC_WAIT))
  401. goto out_ifunlock;
  402. xfs_iunpin_wait(ip);
  403. }
  404. if (xfs_iflags_test(ip, XFS_ISTALE))
  405. goto reclaim;
  406. if (xfs_inode_clean(ip))
  407. goto reclaim;
  408. /*
  409. * Never flush out dirty data during non-blocking reclaim, as it would
  410. * just contend with AIL pushing trying to do the same job.
  411. */
  412. if (!(sync_mode & SYNC_WAIT))
  413. goto out_ifunlock;
  414. /*
  415. * Now we have an inode that needs flushing.
  416. *
  417. * Note that xfs_iflush will never block on the inode buffer lock, as
  418. * xfs_ifree_cluster() can lock the inode buffer before it locks the
  419. * ip->i_lock, and we are doing the exact opposite here. As a result,
  420. * doing a blocking xfs_imap_to_bp() to get the cluster buffer would
  421. * result in an ABBA deadlock with xfs_ifree_cluster().
  422. *
  423. * As xfs_ifree_cluser() must gather all inodes that are active in the
  424. * cache to mark them stale, if we hit this case we don't actually want
  425. * to do IO here - we want the inode marked stale so we can simply
  426. * reclaim it. Hence if we get an EAGAIN error here, just unlock the
  427. * inode, back off and try again. Hopefully the next pass through will
  428. * see the stale flag set on the inode.
  429. */
  430. error = xfs_iflush(ip, &bp);
  431. if (error == EAGAIN) {
  432. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  433. /* backoff longer than in xfs_ifree_cluster */
  434. delay(2);
  435. goto restart;
  436. }
  437. if (!error) {
  438. error = xfs_bwrite(bp);
  439. xfs_buf_relse(bp);
  440. }
  441. xfs_iflock(ip);
  442. reclaim:
  443. xfs_ifunlock(ip);
  444. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  445. XFS_STATS_INC(xs_ig_reclaims);
  446. /*
  447. * Remove the inode from the per-AG radix tree.
  448. *
  449. * Because radix_tree_delete won't complain even if the item was never
  450. * added to the tree assert that it's been there before to catch
  451. * problems with the inode life time early on.
  452. */
  453. spin_lock(&pag->pag_ici_lock);
  454. if (!radix_tree_delete(&pag->pag_ici_root,
  455. XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino)))
  456. ASSERT(0);
  457. __xfs_inode_clear_reclaim(pag, ip);
  458. spin_unlock(&pag->pag_ici_lock);
  459. /*
  460. * Here we do an (almost) spurious inode lock in order to coordinate
  461. * with inode cache radix tree lookups. This is because the lookup
  462. * can reference the inodes in the cache without taking references.
  463. *
  464. * We make that OK here by ensuring that we wait until the inode is
  465. * unlocked after the lookup before we go ahead and free it.
  466. */
  467. xfs_ilock(ip, XFS_ILOCK_EXCL);
  468. xfs_qm_dqdetach(ip);
  469. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  470. xfs_inode_free(ip);
  471. return error;
  472. out_ifunlock:
  473. xfs_ifunlock(ip);
  474. out:
  475. xfs_iflags_clear(ip, XFS_IRECLAIM);
  476. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  477. /*
  478. * We could return EAGAIN here to make reclaim rescan the inode tree in
  479. * a short while. However, this just burns CPU time scanning the tree
  480. * waiting for IO to complete and the reclaim work never goes back to
  481. * the idle state. Instead, return 0 to let the next scheduled
  482. * background reclaim attempt to reclaim the inode again.
  483. */
  484. return 0;
  485. }
  486. /*
  487. * Walk the AGs and reclaim the inodes in them. Even if the filesystem is
  488. * corrupted, we still want to try to reclaim all the inodes. If we don't,
  489. * then a shut down during filesystem unmount reclaim walk leak all the
  490. * unreclaimed inodes.
  491. */
  492. int
  493. xfs_reclaim_inodes_ag(
  494. struct xfs_mount *mp,
  495. int flags,
  496. int *nr_to_scan)
  497. {
  498. struct xfs_perag *pag;
  499. int error = 0;
  500. int last_error = 0;
  501. xfs_agnumber_t ag;
  502. int trylock = flags & SYNC_TRYLOCK;
  503. int skipped;
  504. restart:
  505. ag = 0;
  506. skipped = 0;
  507. while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
  508. unsigned long first_index = 0;
  509. int done = 0;
  510. int nr_found = 0;
  511. ag = pag->pag_agno + 1;
  512. if (trylock) {
  513. if (!mutex_trylock(&pag->pag_ici_reclaim_lock)) {
  514. skipped++;
  515. xfs_perag_put(pag);
  516. continue;
  517. }
  518. first_index = pag->pag_ici_reclaim_cursor;
  519. } else
  520. mutex_lock(&pag->pag_ici_reclaim_lock);
  521. do {
  522. struct xfs_inode *batch[XFS_LOOKUP_BATCH];
  523. int i;
  524. rcu_read_lock();
  525. nr_found = radix_tree_gang_lookup_tag(
  526. &pag->pag_ici_root,
  527. (void **)batch, first_index,
  528. XFS_LOOKUP_BATCH,
  529. XFS_ICI_RECLAIM_TAG);
  530. if (!nr_found) {
  531. done = 1;
  532. rcu_read_unlock();
  533. break;
  534. }
  535. /*
  536. * Grab the inodes before we drop the lock. if we found
  537. * nothing, nr == 0 and the loop will be skipped.
  538. */
  539. for (i = 0; i < nr_found; i++) {
  540. struct xfs_inode *ip = batch[i];
  541. if (done || xfs_reclaim_inode_grab(ip, flags))
  542. batch[i] = NULL;
  543. /*
  544. * Update the index for the next lookup. Catch
  545. * overflows into the next AG range which can
  546. * occur if we have inodes in the last block of
  547. * the AG and we are currently pointing to the
  548. * last inode.
  549. *
  550. * Because we may see inodes that are from the
  551. * wrong AG due to RCU freeing and
  552. * reallocation, only update the index if it
  553. * lies in this AG. It was a race that lead us
  554. * to see this inode, so another lookup from
  555. * the same index will not find it again.
  556. */
  557. if (XFS_INO_TO_AGNO(mp, ip->i_ino) !=
  558. pag->pag_agno)
  559. continue;
  560. first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
  561. if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
  562. done = 1;
  563. }
  564. /* unlock now we've grabbed the inodes. */
  565. rcu_read_unlock();
  566. for (i = 0; i < nr_found; i++) {
  567. if (!batch[i])
  568. continue;
  569. error = xfs_reclaim_inode(batch[i], pag, flags);
  570. if (error && last_error != EFSCORRUPTED)
  571. last_error = error;
  572. }
  573. *nr_to_scan -= XFS_LOOKUP_BATCH;
  574. cond_resched();
  575. } while (nr_found && !done && *nr_to_scan > 0);
  576. if (trylock && !done)
  577. pag->pag_ici_reclaim_cursor = first_index;
  578. else
  579. pag->pag_ici_reclaim_cursor = 0;
  580. mutex_unlock(&pag->pag_ici_reclaim_lock);
  581. xfs_perag_put(pag);
  582. }
  583. /*
  584. * if we skipped any AG, and we still have scan count remaining, do
  585. * another pass this time using blocking reclaim semantics (i.e
  586. * waiting on the reclaim locks and ignoring the reclaim cursors). This
  587. * ensure that when we get more reclaimers than AGs we block rather
  588. * than spin trying to execute reclaim.
  589. */
  590. if (skipped && (flags & SYNC_WAIT) && *nr_to_scan > 0) {
  591. trylock = 0;
  592. goto restart;
  593. }
  594. return XFS_ERROR(last_error);
  595. }
  596. int
  597. xfs_reclaim_inodes(
  598. xfs_mount_t *mp,
  599. int mode)
  600. {
  601. int nr_to_scan = INT_MAX;
  602. return xfs_reclaim_inodes_ag(mp, mode, &nr_to_scan);
  603. }
  604. /*
  605. * Scan a certain number of inodes for reclaim.
  606. *
  607. * When called we make sure that there is a background (fast) inode reclaim in
  608. * progress, while we will throttle the speed of reclaim via doing synchronous
  609. * reclaim of inodes. That means if we come across dirty inodes, we wait for
  610. * them to be cleaned, which we hope will not be very long due to the
  611. * background walker having already kicked the IO off on those dirty inodes.
  612. */
  613. void
  614. xfs_reclaim_inodes_nr(
  615. struct xfs_mount *mp,
  616. int nr_to_scan)
  617. {
  618. /* kick background reclaimer and push the AIL */
  619. xfs_reclaim_work_queue(mp);
  620. xfs_ail_push_all(mp->m_ail);
  621. xfs_reclaim_inodes_ag(mp, SYNC_TRYLOCK | SYNC_WAIT, &nr_to_scan);
  622. }
  623. /*
  624. * Return the number of reclaimable inodes in the filesystem for
  625. * the shrinker to determine how much to reclaim.
  626. */
  627. int
  628. xfs_reclaim_inodes_count(
  629. struct xfs_mount *mp)
  630. {
  631. struct xfs_perag *pag;
  632. xfs_agnumber_t ag = 0;
  633. int reclaimable = 0;
  634. while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
  635. ag = pag->pag_agno + 1;
  636. reclaimable += pag->pag_ici_reclaimable;
  637. xfs_perag_put(pag);
  638. }
  639. return reclaimable;
  640. }