xfs_sync.c 19 KB

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