xfs_sync.c 28 KB

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