xfs_sync.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  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. STATIC int
  192. xfs_sync_fsdata(
  193. struct xfs_mount *mp)
  194. {
  195. struct xfs_buf *bp;
  196. int error;
  197. /*
  198. * If the buffer is pinned then push on the log so we won't get stuck
  199. * waiting in the write for someone, maybe ourselves, to flush the log.
  200. *
  201. * Even though we just pushed the log above, we did not have the
  202. * superblock buffer locked at that point so it can become pinned in
  203. * between there and here.
  204. */
  205. bp = xfs_getsb(mp, 0);
  206. if (xfs_buf_ispinned(bp))
  207. xfs_log_force(mp, 0);
  208. error = xfs_bwrite(bp);
  209. xfs_buf_relse(bp);
  210. return error;
  211. }
  212. /*
  213. * When remounting a filesystem read-only or freezing the filesystem, we have
  214. * two phases to execute. This first phase is syncing the data before we
  215. * quiesce the filesystem, and the second is flushing all the inodes out after
  216. * we've waited for all the transactions created by the first phase to
  217. * complete. The second phase ensures that the inodes are written to their
  218. * location on disk rather than just existing in transactions in the log. This
  219. * means after a quiesce there is no log replay required to write the inodes to
  220. * disk (this is the main difference between a sync and a quiesce).
  221. */
  222. /*
  223. * First stage of freeze - no writers will make progress now we are here,
  224. * so we flush delwri and delalloc buffers here, then wait for all I/O to
  225. * complete. Data is frozen at that point. Metadata is not frozen,
  226. * transactions can still occur here so don't bother emptying the AIL
  227. * because it'll just get dirty again.
  228. */
  229. int
  230. xfs_quiesce_data(
  231. struct xfs_mount *mp)
  232. {
  233. int error, error2 = 0;
  234. /* force out the log */
  235. xfs_log_force(mp, XFS_LOG_SYNC);
  236. /* write superblock and hoover up shutdown errors */
  237. error = xfs_sync_fsdata(mp);
  238. /* mark the log as covered if needed */
  239. if (xfs_log_need_covered(mp))
  240. error2 = xfs_fs_log_dummy(mp);
  241. return error ? error : error2;
  242. }
  243. /*
  244. * Second stage of a quiesce. The data is already synced, now we have to take
  245. * care of the metadata. New transactions are already blocked, so we need to
  246. * wait for any remaining transactions to drain out before proceeding.
  247. *
  248. * Note: this stops background sync work - the callers must ensure it is started
  249. * again when appropriate.
  250. */
  251. void
  252. xfs_quiesce_attr(
  253. struct xfs_mount *mp)
  254. {
  255. int error = 0;
  256. /* wait for all modifications to complete */
  257. while (atomic_read(&mp->m_active_trans) > 0)
  258. delay(100);
  259. /* reclaim inodes to do any IO before the freeze completes */
  260. xfs_reclaim_inodes(mp, 0);
  261. xfs_reclaim_inodes(mp, SYNC_WAIT);
  262. /* flush all pending changes from the AIL */
  263. xfs_ail_push_all_sync(mp->m_ail);
  264. /* stop background log work */
  265. cancel_delayed_work_sync(&mp->m_log->l_work);
  266. /*
  267. * Just warn here till VFS can correctly support
  268. * read-only remount without racing.
  269. */
  270. WARN_ON(atomic_read(&mp->m_active_trans) != 0);
  271. /* Push the superblock and write an unmount record */
  272. error = xfs_log_sbcount(mp);
  273. if (error)
  274. xfs_warn(mp, "xfs_attr_quiesce: failed to log sb changes. "
  275. "Frozen image may not be consistent.");
  276. xfs_log_unmount_write(mp);
  277. /*
  278. * At this point we might have modified the superblock again and thus
  279. * added an item to the AIL, thus flush it again.
  280. */
  281. xfs_ail_push_all_sync(mp->m_ail);
  282. /*
  283. * The superblock buffer is uncached and xfsaild_push() will lock and
  284. * set the XBF_ASYNC flag on the buffer. We cannot do xfs_buf_iowait()
  285. * here but a lock on the superblock buffer will block until iodone()
  286. * has completed.
  287. */
  288. xfs_buf_lock(mp->m_sb_bp);
  289. xfs_buf_unlock(mp->m_sb_bp);
  290. }
  291. /*
  292. * Queue a new inode reclaim pass if there are reclaimable inodes and there
  293. * isn't a reclaim pass already in progress. By default it runs every 5s based
  294. * on the xfs periodic sync default of 30s. Perhaps this should have it's own
  295. * tunable, but that can be done if this method proves to be ineffective or too
  296. * aggressive.
  297. */
  298. static void
  299. xfs_reclaim_work_queue(
  300. struct xfs_mount *mp)
  301. {
  302. rcu_read_lock();
  303. if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_RECLAIM_TAG)) {
  304. queue_delayed_work(mp->m_reclaim_workqueue, &mp->m_reclaim_work,
  305. msecs_to_jiffies(xfs_syncd_centisecs / 6 * 10));
  306. }
  307. rcu_read_unlock();
  308. }
  309. /*
  310. * This is a fast pass over the inode cache to try to get reclaim moving on as
  311. * many inodes as possible in a short period of time. It kicks itself every few
  312. * seconds, as well as being kicked by the inode cache shrinker when memory
  313. * goes low. It scans as quickly as possible avoiding locked inodes or those
  314. * already being flushed, and once done schedules a future pass.
  315. */
  316. void
  317. xfs_reclaim_worker(
  318. struct work_struct *work)
  319. {
  320. struct xfs_mount *mp = container_of(to_delayed_work(work),
  321. struct xfs_mount, m_reclaim_work);
  322. xfs_reclaim_inodes(mp, SYNC_TRYLOCK);
  323. xfs_reclaim_work_queue(mp);
  324. }
  325. void
  326. __xfs_inode_set_reclaim_tag(
  327. struct xfs_perag *pag,
  328. struct xfs_inode *ip)
  329. {
  330. radix_tree_tag_set(&pag->pag_ici_root,
  331. XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
  332. XFS_ICI_RECLAIM_TAG);
  333. if (!pag->pag_ici_reclaimable) {
  334. /* propagate the reclaim tag up into the perag radix tree */
  335. spin_lock(&ip->i_mount->m_perag_lock);
  336. radix_tree_tag_set(&ip->i_mount->m_perag_tree,
  337. XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
  338. XFS_ICI_RECLAIM_TAG);
  339. spin_unlock(&ip->i_mount->m_perag_lock);
  340. /* schedule periodic background inode reclaim */
  341. xfs_reclaim_work_queue(ip->i_mount);
  342. trace_xfs_perag_set_reclaim(ip->i_mount, pag->pag_agno,
  343. -1, _RET_IP_);
  344. }
  345. pag->pag_ici_reclaimable++;
  346. }
  347. /*
  348. * We set the inode flag atomically with the radix tree tag.
  349. * Once we get tag lookups on the radix tree, this inode flag
  350. * can go away.
  351. */
  352. void
  353. xfs_inode_set_reclaim_tag(
  354. xfs_inode_t *ip)
  355. {
  356. struct xfs_mount *mp = ip->i_mount;
  357. struct xfs_perag *pag;
  358. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
  359. spin_lock(&pag->pag_ici_lock);
  360. spin_lock(&ip->i_flags_lock);
  361. __xfs_inode_set_reclaim_tag(pag, ip);
  362. __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
  363. spin_unlock(&ip->i_flags_lock);
  364. spin_unlock(&pag->pag_ici_lock);
  365. xfs_perag_put(pag);
  366. }
  367. STATIC void
  368. __xfs_inode_clear_reclaim(
  369. xfs_perag_t *pag,
  370. xfs_inode_t *ip)
  371. {
  372. pag->pag_ici_reclaimable--;
  373. if (!pag->pag_ici_reclaimable) {
  374. /* clear the reclaim tag from the perag radix tree */
  375. spin_lock(&ip->i_mount->m_perag_lock);
  376. radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
  377. XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
  378. XFS_ICI_RECLAIM_TAG);
  379. spin_unlock(&ip->i_mount->m_perag_lock);
  380. trace_xfs_perag_clear_reclaim(ip->i_mount, pag->pag_agno,
  381. -1, _RET_IP_);
  382. }
  383. }
  384. void
  385. __xfs_inode_clear_reclaim_tag(
  386. xfs_mount_t *mp,
  387. xfs_perag_t *pag,
  388. xfs_inode_t *ip)
  389. {
  390. radix_tree_tag_clear(&pag->pag_ici_root,
  391. XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
  392. __xfs_inode_clear_reclaim(pag, ip);
  393. }
  394. /*
  395. * Grab the inode for reclaim exclusively.
  396. * Return 0 if we grabbed it, non-zero otherwise.
  397. */
  398. STATIC int
  399. xfs_reclaim_inode_grab(
  400. struct xfs_inode *ip,
  401. int flags)
  402. {
  403. ASSERT(rcu_read_lock_held());
  404. /* quick check for stale RCU freed inode */
  405. if (!ip->i_ino)
  406. return 1;
  407. /*
  408. * If we are asked for non-blocking operation, do unlocked checks to
  409. * see if the inode already is being flushed or in reclaim to avoid
  410. * lock traffic.
  411. */
  412. if ((flags & SYNC_TRYLOCK) &&
  413. __xfs_iflags_test(ip, XFS_IFLOCK | XFS_IRECLAIM))
  414. return 1;
  415. /*
  416. * The radix tree lock here protects a thread in xfs_iget from racing
  417. * with us starting reclaim on the inode. Once we have the
  418. * XFS_IRECLAIM flag set it will not touch us.
  419. *
  420. * Due to RCU lookup, we may find inodes that have been freed and only
  421. * have XFS_IRECLAIM set. Indeed, we may see reallocated inodes that
  422. * aren't candidates for reclaim at all, so we must check the
  423. * XFS_IRECLAIMABLE is set first before proceeding to reclaim.
  424. */
  425. spin_lock(&ip->i_flags_lock);
  426. if (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) ||
  427. __xfs_iflags_test(ip, XFS_IRECLAIM)) {
  428. /* not a reclaim candidate. */
  429. spin_unlock(&ip->i_flags_lock);
  430. return 1;
  431. }
  432. __xfs_iflags_set(ip, XFS_IRECLAIM);
  433. spin_unlock(&ip->i_flags_lock);
  434. return 0;
  435. }
  436. /*
  437. * Inodes in different states need to be treated differently. The following
  438. * table lists the inode states and the reclaim actions necessary:
  439. *
  440. * inode state iflush ret required action
  441. * --------------- ---------- ---------------
  442. * bad - reclaim
  443. * shutdown EIO unpin and reclaim
  444. * clean, unpinned 0 reclaim
  445. * stale, unpinned 0 reclaim
  446. * clean, pinned(*) 0 requeue
  447. * stale, pinned EAGAIN requeue
  448. * dirty, async - requeue
  449. * dirty, sync 0 reclaim
  450. *
  451. * (*) dgc: I don't think the clean, pinned state is possible but it gets
  452. * handled anyway given the order of checks implemented.
  453. *
  454. * Also, because we get the flush lock first, we know that any inode that has
  455. * been flushed delwri has had the flush completed by the time we check that
  456. * the inode is clean.
  457. *
  458. * Note that because the inode is flushed delayed write by AIL pushing, the
  459. * flush lock may already be held here and waiting on it can result in very
  460. * long latencies. Hence for sync reclaims, where we wait on the flush lock,
  461. * the caller should push the AIL first before trying to reclaim inodes to
  462. * minimise the amount of time spent waiting. For background relaim, we only
  463. * bother to reclaim clean inodes anyway.
  464. *
  465. * Hence the order of actions after gaining the locks should be:
  466. * bad => reclaim
  467. * shutdown => unpin and reclaim
  468. * pinned, async => requeue
  469. * pinned, sync => unpin
  470. * stale => reclaim
  471. * clean => reclaim
  472. * dirty, async => requeue
  473. * dirty, sync => flush, wait and reclaim
  474. */
  475. STATIC int
  476. xfs_reclaim_inode(
  477. struct xfs_inode *ip,
  478. struct xfs_perag *pag,
  479. int sync_mode)
  480. {
  481. struct xfs_buf *bp = NULL;
  482. int error;
  483. restart:
  484. error = 0;
  485. xfs_ilock(ip, XFS_ILOCK_EXCL);
  486. if (!xfs_iflock_nowait(ip)) {
  487. if (!(sync_mode & SYNC_WAIT))
  488. goto out;
  489. xfs_iflock(ip);
  490. }
  491. if (is_bad_inode(VFS_I(ip)))
  492. goto reclaim;
  493. if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  494. xfs_iunpin_wait(ip);
  495. xfs_iflush_abort(ip, false);
  496. goto reclaim;
  497. }
  498. if (xfs_ipincount(ip)) {
  499. if (!(sync_mode & SYNC_WAIT))
  500. goto out_ifunlock;
  501. xfs_iunpin_wait(ip);
  502. }
  503. if (xfs_iflags_test(ip, XFS_ISTALE))
  504. goto reclaim;
  505. if (xfs_inode_clean(ip))
  506. goto reclaim;
  507. /*
  508. * Never flush out dirty data during non-blocking reclaim, as it would
  509. * just contend with AIL pushing trying to do the same job.
  510. */
  511. if (!(sync_mode & SYNC_WAIT))
  512. goto out_ifunlock;
  513. /*
  514. * Now we have an inode that needs flushing.
  515. *
  516. * Note that xfs_iflush will never block on the inode buffer lock, as
  517. * xfs_ifree_cluster() can lock the inode buffer before it locks the
  518. * ip->i_lock, and we are doing the exact opposite here. As a result,
  519. * doing a blocking xfs_imap_to_bp() to get the cluster buffer would
  520. * result in an ABBA deadlock with xfs_ifree_cluster().
  521. *
  522. * As xfs_ifree_cluser() must gather all inodes that are active in the
  523. * cache to mark them stale, if we hit this case we don't actually want
  524. * to do IO here - we want the inode marked stale so we can simply
  525. * reclaim it. Hence if we get an EAGAIN error here, just unlock the
  526. * inode, back off and try again. Hopefully the next pass through will
  527. * see the stale flag set on the inode.
  528. */
  529. error = xfs_iflush(ip, &bp);
  530. if (error == EAGAIN) {
  531. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  532. /* backoff longer than in xfs_ifree_cluster */
  533. delay(2);
  534. goto restart;
  535. }
  536. if (!error) {
  537. error = xfs_bwrite(bp);
  538. xfs_buf_relse(bp);
  539. }
  540. xfs_iflock(ip);
  541. reclaim:
  542. xfs_ifunlock(ip);
  543. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  544. XFS_STATS_INC(xs_ig_reclaims);
  545. /*
  546. * Remove the inode from the per-AG radix tree.
  547. *
  548. * Because radix_tree_delete won't complain even if the item was never
  549. * added to the tree assert that it's been there before to catch
  550. * problems with the inode life time early on.
  551. */
  552. spin_lock(&pag->pag_ici_lock);
  553. if (!radix_tree_delete(&pag->pag_ici_root,
  554. XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino)))
  555. ASSERT(0);
  556. __xfs_inode_clear_reclaim(pag, ip);
  557. spin_unlock(&pag->pag_ici_lock);
  558. /*
  559. * Here we do an (almost) spurious inode lock in order to coordinate
  560. * with inode cache radix tree lookups. This is because the lookup
  561. * can reference the inodes in the cache without taking references.
  562. *
  563. * We make that OK here by ensuring that we wait until the inode is
  564. * unlocked after the lookup before we go ahead and free it.
  565. */
  566. xfs_ilock(ip, XFS_ILOCK_EXCL);
  567. xfs_qm_dqdetach(ip);
  568. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  569. xfs_inode_free(ip);
  570. return error;
  571. out_ifunlock:
  572. xfs_ifunlock(ip);
  573. out:
  574. xfs_iflags_clear(ip, XFS_IRECLAIM);
  575. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  576. /*
  577. * We could return EAGAIN here to make reclaim rescan the inode tree in
  578. * a short while. However, this just burns CPU time scanning the tree
  579. * waiting for IO to complete and the reclaim work never goes back to
  580. * the idle state. Instead, return 0 to let the next scheduled
  581. * background reclaim attempt to reclaim the inode again.
  582. */
  583. return 0;
  584. }
  585. /*
  586. * Walk the AGs and reclaim the inodes in them. Even if the filesystem is
  587. * corrupted, we still want to try to reclaim all the inodes. If we don't,
  588. * then a shut down during filesystem unmount reclaim walk leak all the
  589. * unreclaimed inodes.
  590. */
  591. int
  592. xfs_reclaim_inodes_ag(
  593. struct xfs_mount *mp,
  594. int flags,
  595. int *nr_to_scan)
  596. {
  597. struct xfs_perag *pag;
  598. int error = 0;
  599. int last_error = 0;
  600. xfs_agnumber_t ag;
  601. int trylock = flags & SYNC_TRYLOCK;
  602. int skipped;
  603. restart:
  604. ag = 0;
  605. skipped = 0;
  606. while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
  607. unsigned long first_index = 0;
  608. int done = 0;
  609. int nr_found = 0;
  610. ag = pag->pag_agno + 1;
  611. if (trylock) {
  612. if (!mutex_trylock(&pag->pag_ici_reclaim_lock)) {
  613. skipped++;
  614. xfs_perag_put(pag);
  615. continue;
  616. }
  617. first_index = pag->pag_ici_reclaim_cursor;
  618. } else
  619. mutex_lock(&pag->pag_ici_reclaim_lock);
  620. do {
  621. struct xfs_inode *batch[XFS_LOOKUP_BATCH];
  622. int i;
  623. rcu_read_lock();
  624. nr_found = radix_tree_gang_lookup_tag(
  625. &pag->pag_ici_root,
  626. (void **)batch, first_index,
  627. XFS_LOOKUP_BATCH,
  628. XFS_ICI_RECLAIM_TAG);
  629. if (!nr_found) {
  630. done = 1;
  631. rcu_read_unlock();
  632. break;
  633. }
  634. /*
  635. * Grab the inodes before we drop the lock. if we found
  636. * nothing, nr == 0 and the loop will be skipped.
  637. */
  638. for (i = 0; i < nr_found; i++) {
  639. struct xfs_inode *ip = batch[i];
  640. if (done || xfs_reclaim_inode_grab(ip, flags))
  641. batch[i] = NULL;
  642. /*
  643. * Update the index for the next lookup. Catch
  644. * overflows into the next AG range which can
  645. * occur if we have inodes in the last block of
  646. * the AG and we are currently pointing to the
  647. * last inode.
  648. *
  649. * Because we may see inodes that are from the
  650. * wrong AG due to RCU freeing and
  651. * reallocation, only update the index if it
  652. * lies in this AG. It was a race that lead us
  653. * to see this inode, so another lookup from
  654. * the same index will not find it again.
  655. */
  656. if (XFS_INO_TO_AGNO(mp, ip->i_ino) !=
  657. pag->pag_agno)
  658. continue;
  659. first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
  660. if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
  661. done = 1;
  662. }
  663. /* unlock now we've grabbed the inodes. */
  664. rcu_read_unlock();
  665. for (i = 0; i < nr_found; i++) {
  666. if (!batch[i])
  667. continue;
  668. error = xfs_reclaim_inode(batch[i], pag, flags);
  669. if (error && last_error != EFSCORRUPTED)
  670. last_error = error;
  671. }
  672. *nr_to_scan -= XFS_LOOKUP_BATCH;
  673. cond_resched();
  674. } while (nr_found && !done && *nr_to_scan > 0);
  675. if (trylock && !done)
  676. pag->pag_ici_reclaim_cursor = first_index;
  677. else
  678. pag->pag_ici_reclaim_cursor = 0;
  679. mutex_unlock(&pag->pag_ici_reclaim_lock);
  680. xfs_perag_put(pag);
  681. }
  682. /*
  683. * if we skipped any AG, and we still have scan count remaining, do
  684. * another pass this time using blocking reclaim semantics (i.e
  685. * waiting on the reclaim locks and ignoring the reclaim cursors). This
  686. * ensure that when we get more reclaimers than AGs we block rather
  687. * than spin trying to execute reclaim.
  688. */
  689. if (skipped && (flags & SYNC_WAIT) && *nr_to_scan > 0) {
  690. trylock = 0;
  691. goto restart;
  692. }
  693. return XFS_ERROR(last_error);
  694. }
  695. int
  696. xfs_reclaim_inodes(
  697. xfs_mount_t *mp,
  698. int mode)
  699. {
  700. int nr_to_scan = INT_MAX;
  701. return xfs_reclaim_inodes_ag(mp, mode, &nr_to_scan);
  702. }
  703. /*
  704. * Scan a certain number of inodes for reclaim.
  705. *
  706. * When called we make sure that there is a background (fast) inode reclaim in
  707. * progress, while we will throttle the speed of reclaim via doing synchronous
  708. * reclaim of inodes. That means if we come across dirty inodes, we wait for
  709. * them to be cleaned, which we hope will not be very long due to the
  710. * background walker having already kicked the IO off on those dirty inodes.
  711. */
  712. void
  713. xfs_reclaim_inodes_nr(
  714. struct xfs_mount *mp,
  715. int nr_to_scan)
  716. {
  717. /* kick background reclaimer and push the AIL */
  718. xfs_reclaim_work_queue(mp);
  719. xfs_ail_push_all(mp->m_ail);
  720. xfs_reclaim_inodes_ag(mp, SYNC_TRYLOCK | SYNC_WAIT, &nr_to_scan);
  721. }
  722. /*
  723. * Return the number of reclaimable inodes in the filesystem for
  724. * the shrinker to determine how much to reclaim.
  725. */
  726. int
  727. xfs_reclaim_inodes_count(
  728. struct xfs_mount *mp)
  729. {
  730. struct xfs_perag *pag;
  731. xfs_agnumber_t ag = 0;
  732. int reclaimable = 0;
  733. while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
  734. ag = pag->pag_agno + 1;
  735. reclaimable += pag->pag_ici_reclaimable;
  736. xfs_perag_put(pag);
  737. }
  738. return reclaimable;
  739. }