xfs_sync.c 30 KB

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