xfs_sync.c 29 KB

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