xfs_sync.c 29 KB

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