xfs_sync.c 26 KB

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