xfs_sync.c 26 KB

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