xfs_sync.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  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_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_mount.h"
  28. #include "xfs_bmap_btree.h"
  29. #include "xfs_inode.h"
  30. #include "xfs_dinode.h"
  31. #include "xfs_error.h"
  32. #include "xfs_filestream.h"
  33. #include "xfs_vnodeops.h"
  34. #include "xfs_inode_item.h"
  35. #include "xfs_quota.h"
  36. #include "xfs_trace.h"
  37. #include "xfs_fsops.h"
  38. #include <linux/kthread.h>
  39. #include <linux/freezer.h>
  40. STATIC int
  41. xfs_inode_ag_walk_grab(
  42. struct xfs_inode *ip)
  43. {
  44. struct inode *inode = VFS_I(ip);
  45. /* nothing to sync during shutdown */
  46. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  47. return EFSCORRUPTED;
  48. /* avoid new or reclaimable inodes. Leave for reclaim code to flush */
  49. if (xfs_iflags_test(ip, XFS_INEW | XFS_IRECLAIMABLE | XFS_IRECLAIM))
  50. return ENOENT;
  51. /* If we can't grab the inode, it must on it's way to reclaim. */
  52. if (!igrab(inode))
  53. return ENOENT;
  54. if (is_bad_inode(inode)) {
  55. IRELE(ip);
  56. return ENOENT;
  57. }
  58. /* inode is valid */
  59. return 0;
  60. }
  61. STATIC int
  62. xfs_inode_ag_walk(
  63. struct xfs_mount *mp,
  64. struct xfs_perag *pag,
  65. int (*execute)(struct xfs_inode *ip,
  66. struct xfs_perag *pag, int flags),
  67. int flags)
  68. {
  69. uint32_t first_index;
  70. int last_error = 0;
  71. int skipped;
  72. int done;
  73. restart:
  74. done = 0;
  75. skipped = 0;
  76. first_index = 0;
  77. do {
  78. int error = 0;
  79. int nr_found;
  80. xfs_inode_t *ip;
  81. read_lock(&pag->pag_ici_lock);
  82. nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
  83. (void **)&ip, first_index, 1);
  84. if (!nr_found) {
  85. read_unlock(&pag->pag_ici_lock);
  86. break;
  87. }
  88. /*
  89. * Update the index for the next lookup. Catch overflows
  90. * into the next AG range which can occur if we have inodes
  91. * in the last block of the AG and we are currently
  92. * pointing to the last inode.
  93. */
  94. first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
  95. if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
  96. done = 1;
  97. if (xfs_inode_ag_walk_grab(ip)) {
  98. read_unlock(&pag->pag_ici_lock);
  99. continue;
  100. }
  101. read_unlock(&pag->pag_ici_lock);
  102. error = execute(ip, pag, flags);
  103. IRELE(ip);
  104. if (error == EAGAIN) {
  105. skipped++;
  106. continue;
  107. }
  108. if (error)
  109. last_error = error;
  110. /* bail out if the filesystem is corrupted. */
  111. if (error == EFSCORRUPTED)
  112. break;
  113. } while (!done);
  114. if (skipped) {
  115. delay(1);
  116. goto restart;
  117. }
  118. return last_error;
  119. }
  120. int
  121. xfs_inode_ag_iterator(
  122. struct xfs_mount *mp,
  123. int (*execute)(struct xfs_inode *ip,
  124. struct xfs_perag *pag, int flags),
  125. int flags)
  126. {
  127. struct xfs_perag *pag;
  128. int error = 0;
  129. int last_error = 0;
  130. xfs_agnumber_t ag;
  131. ag = 0;
  132. while ((pag = xfs_perag_get(mp, ag))) {
  133. ag = pag->pag_agno + 1;
  134. error = xfs_inode_ag_walk(mp, pag, execute, flags);
  135. xfs_perag_put(pag);
  136. if (error) {
  137. last_error = error;
  138. if (error == EFSCORRUPTED)
  139. break;
  140. }
  141. }
  142. return XFS_ERROR(last_error);
  143. }
  144. STATIC int
  145. xfs_sync_inode_data(
  146. struct xfs_inode *ip,
  147. struct xfs_perag *pag,
  148. int flags)
  149. {
  150. struct inode *inode = VFS_I(ip);
  151. struct address_space *mapping = inode->i_mapping;
  152. int error = 0;
  153. if (!mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
  154. goto out_wait;
  155. if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED)) {
  156. if (flags & SYNC_TRYLOCK)
  157. goto out_wait;
  158. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  159. }
  160. error = xfs_flush_pages(ip, 0, -1, (flags & SYNC_WAIT) ?
  161. 0 : XBF_ASYNC, FI_NONE);
  162. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  163. out_wait:
  164. if (flags & SYNC_WAIT)
  165. xfs_ioend_wait(ip);
  166. return error;
  167. }
  168. STATIC int
  169. xfs_sync_inode_attr(
  170. struct xfs_inode *ip,
  171. struct xfs_perag *pag,
  172. int flags)
  173. {
  174. int error = 0;
  175. xfs_ilock(ip, XFS_ILOCK_SHARED);
  176. if (xfs_inode_clean(ip))
  177. goto out_unlock;
  178. if (!xfs_iflock_nowait(ip)) {
  179. if (!(flags & SYNC_WAIT))
  180. goto out_unlock;
  181. xfs_iflock(ip);
  182. }
  183. if (xfs_inode_clean(ip)) {
  184. xfs_ifunlock(ip);
  185. goto out_unlock;
  186. }
  187. error = xfs_iflush(ip, flags);
  188. out_unlock:
  189. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  190. return error;
  191. }
  192. /*
  193. * Write out pagecache data for the whole filesystem.
  194. */
  195. STATIC int
  196. xfs_sync_data(
  197. struct xfs_mount *mp,
  198. int flags)
  199. {
  200. int error;
  201. ASSERT((flags & ~(SYNC_TRYLOCK|SYNC_WAIT)) == 0);
  202. error = xfs_inode_ag_iterator(mp, xfs_sync_inode_data, flags);
  203. if (error)
  204. return XFS_ERROR(error);
  205. xfs_log_force(mp, (flags & SYNC_WAIT) ? XFS_LOG_SYNC : 0);
  206. return 0;
  207. }
  208. /*
  209. * Write out inode metadata (attributes) for the whole filesystem.
  210. */
  211. STATIC int
  212. xfs_sync_attr(
  213. struct xfs_mount *mp,
  214. int flags)
  215. {
  216. ASSERT((flags & ~SYNC_WAIT) == 0);
  217. return xfs_inode_ag_iterator(mp, xfs_sync_inode_attr, flags);
  218. }
  219. STATIC int
  220. xfs_sync_fsdata(
  221. struct xfs_mount *mp)
  222. {
  223. struct xfs_buf *bp;
  224. /*
  225. * If the buffer is pinned then push on the log so we won't get stuck
  226. * waiting in the write for someone, maybe ourselves, to flush the log.
  227. *
  228. * Even though we just pushed the log above, we did not have the
  229. * superblock buffer locked at that point so it can become pinned in
  230. * between there and here.
  231. */
  232. bp = xfs_getsb(mp, 0);
  233. if (XFS_BUF_ISPINNED(bp))
  234. xfs_log_force(mp, 0);
  235. return xfs_bwrite(mp, bp);
  236. }
  237. /*
  238. * When remounting a filesystem read-only or freezing the filesystem, we have
  239. * two phases to execute. This first phase is syncing the data before we
  240. * quiesce the filesystem, and the second is flushing all the inodes out after
  241. * we've waited for all the transactions created by the first phase to
  242. * complete. The second phase ensures that the inodes are written to their
  243. * location on disk rather than just existing in transactions in the log. This
  244. * means after a quiesce there is no log replay required to write the inodes to
  245. * disk (this is the main difference between a sync and a quiesce).
  246. */
  247. /*
  248. * First stage of freeze - no writers will make progress now we are here,
  249. * so we flush delwri and delalloc buffers here, then wait for all I/O to
  250. * complete. Data is frozen at that point. Metadata is not frozen,
  251. * transactions can still occur here so don't bother flushing the buftarg
  252. * because it'll just get dirty again.
  253. */
  254. int
  255. xfs_quiesce_data(
  256. struct xfs_mount *mp)
  257. {
  258. int error, error2 = 0;
  259. /* push non-blocking */
  260. xfs_sync_data(mp, 0);
  261. xfs_qm_sync(mp, SYNC_TRYLOCK);
  262. /* push and block till complete */
  263. xfs_sync_data(mp, SYNC_WAIT);
  264. xfs_qm_sync(mp, SYNC_WAIT);
  265. /* write superblock and hoover up shutdown errors */
  266. error = xfs_sync_fsdata(mp);
  267. /* make sure all delwri buffers are written out */
  268. xfs_flush_buftarg(mp->m_ddev_targp, 1);
  269. /* mark the log as covered if needed */
  270. if (xfs_log_need_covered(mp))
  271. error2 = xfs_fs_log_dummy(mp, SYNC_WAIT);
  272. /* flush data-only devices */
  273. if (mp->m_rtdev_targp)
  274. XFS_bflush(mp->m_rtdev_targp);
  275. return error ? error : error2;
  276. }
  277. STATIC void
  278. xfs_quiesce_fs(
  279. struct xfs_mount *mp)
  280. {
  281. int count = 0, pincount;
  282. xfs_reclaim_inodes(mp, 0);
  283. xfs_flush_buftarg(mp->m_ddev_targp, 0);
  284. /*
  285. * This loop must run at least twice. The first instance of the loop
  286. * will flush most meta data but that will generate more meta data
  287. * (typically directory updates). Which then must be flushed and
  288. * logged before we can write the unmount record. We also so sync
  289. * reclaim of inodes to catch any that the above delwri flush skipped.
  290. */
  291. do {
  292. xfs_reclaim_inodes(mp, SYNC_WAIT);
  293. xfs_sync_attr(mp, SYNC_WAIT);
  294. pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1);
  295. if (!pincount) {
  296. delay(50);
  297. count++;
  298. }
  299. } while (count < 2);
  300. }
  301. /*
  302. * Second stage of a quiesce. The data is already synced, now we have to take
  303. * care of the metadata. New transactions are already blocked, so we need to
  304. * wait for any remaining transactions to drain out before proceding.
  305. */
  306. void
  307. xfs_quiesce_attr(
  308. struct xfs_mount *mp)
  309. {
  310. int error = 0;
  311. /* wait for all modifications to complete */
  312. while (atomic_read(&mp->m_active_trans) > 0)
  313. delay(100);
  314. /* flush inodes and push all remaining buffers out to disk */
  315. xfs_quiesce_fs(mp);
  316. /*
  317. * Just warn here till VFS can correctly support
  318. * read-only remount without racing.
  319. */
  320. WARN_ON(atomic_read(&mp->m_active_trans) != 0);
  321. /* Push the superblock and write an unmount record */
  322. error = xfs_log_sbcount(mp, 1);
  323. if (error)
  324. xfs_fs_cmn_err(CE_WARN, mp,
  325. "xfs_attr_quiesce: failed to log sb changes. "
  326. "Frozen image may not be consistent.");
  327. xfs_log_unmount_write(mp);
  328. xfs_unmountfs_writesb(mp);
  329. }
  330. /*
  331. * Enqueue a work item to be picked up by the vfs xfssyncd thread.
  332. * Doing this has two advantages:
  333. * - It saves on stack space, which is tight in certain situations
  334. * - It can be used (with care) as a mechanism to avoid deadlocks.
  335. * Flushing while allocating in a full filesystem requires both.
  336. */
  337. STATIC void
  338. xfs_syncd_queue_work(
  339. struct xfs_mount *mp,
  340. void *data,
  341. void (*syncer)(struct xfs_mount *, void *),
  342. struct completion *completion)
  343. {
  344. struct xfs_sync_work *work;
  345. work = kmem_alloc(sizeof(struct xfs_sync_work), KM_SLEEP);
  346. INIT_LIST_HEAD(&work->w_list);
  347. work->w_syncer = syncer;
  348. work->w_data = data;
  349. work->w_mount = mp;
  350. work->w_completion = completion;
  351. spin_lock(&mp->m_sync_lock);
  352. list_add_tail(&work->w_list, &mp->m_sync_list);
  353. spin_unlock(&mp->m_sync_lock);
  354. wake_up_process(mp->m_sync_task);
  355. }
  356. /*
  357. * Flush delayed allocate data, attempting to free up reserved space
  358. * from existing allocations. At this point a new allocation attempt
  359. * has failed with ENOSPC and we are in the process of scratching our
  360. * heads, looking about for more room...
  361. */
  362. STATIC void
  363. xfs_flush_inodes_work(
  364. struct xfs_mount *mp,
  365. void *arg)
  366. {
  367. struct inode *inode = arg;
  368. xfs_sync_data(mp, SYNC_TRYLOCK);
  369. xfs_sync_data(mp, SYNC_TRYLOCK | SYNC_WAIT);
  370. iput(inode);
  371. }
  372. void
  373. xfs_flush_inodes(
  374. xfs_inode_t *ip)
  375. {
  376. struct inode *inode = VFS_I(ip);
  377. DECLARE_COMPLETION_ONSTACK(completion);
  378. igrab(inode);
  379. xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_inodes_work, &completion);
  380. wait_for_completion(&completion);
  381. xfs_log_force(ip->i_mount, XFS_LOG_SYNC);
  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 xfs_mount *mp,
  391. void *unused)
  392. {
  393. int error;
  394. if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
  395. xfs_log_force(mp, 0);
  396. xfs_reclaim_inodes(mp, 0);
  397. /* dgc: errors ignored here */
  398. error = xfs_qm_sync(mp, SYNC_TRYLOCK);
  399. if (mp->m_super->s_frozen == SB_UNFROZEN &&
  400. xfs_log_need_covered(mp))
  401. error = xfs_fs_log_dummy(mp, 0);
  402. }
  403. mp->m_sync_seq++;
  404. wake_up(&mp->m_wait_single_sync_task);
  405. }
  406. STATIC int
  407. xfssyncd(
  408. void *arg)
  409. {
  410. struct xfs_mount *mp = arg;
  411. long timeleft;
  412. xfs_sync_work_t *work, *n;
  413. LIST_HEAD (tmp);
  414. set_freezable();
  415. timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10);
  416. for (;;) {
  417. if (list_empty(&mp->m_sync_list))
  418. timeleft = schedule_timeout_interruptible(timeleft);
  419. /* swsusp */
  420. try_to_freeze();
  421. if (kthread_should_stop() && list_empty(&mp->m_sync_list))
  422. break;
  423. spin_lock(&mp->m_sync_lock);
  424. /*
  425. * We can get woken by laptop mode, to do a sync -
  426. * that's the (only!) case where the list would be
  427. * empty with time remaining.
  428. */
  429. if (!timeleft || list_empty(&mp->m_sync_list)) {
  430. if (!timeleft)
  431. timeleft = xfs_syncd_centisecs *
  432. msecs_to_jiffies(10);
  433. INIT_LIST_HEAD(&mp->m_sync_work.w_list);
  434. list_add_tail(&mp->m_sync_work.w_list,
  435. &mp->m_sync_list);
  436. }
  437. list_splice_init(&mp->m_sync_list, &tmp);
  438. spin_unlock(&mp->m_sync_lock);
  439. list_for_each_entry_safe(work, n, &tmp, w_list) {
  440. (*work->w_syncer)(mp, work->w_data);
  441. list_del(&work->w_list);
  442. if (work == &mp->m_sync_work)
  443. continue;
  444. if (work->w_completion)
  445. complete(work->w_completion);
  446. kmem_free(work);
  447. }
  448. }
  449. return 0;
  450. }
  451. int
  452. xfs_syncd_init(
  453. struct xfs_mount *mp)
  454. {
  455. mp->m_sync_work.w_syncer = xfs_sync_worker;
  456. mp->m_sync_work.w_mount = mp;
  457. mp->m_sync_work.w_completion = NULL;
  458. mp->m_sync_task = kthread_run(xfssyncd, mp, "xfssyncd/%s", mp->m_fsname);
  459. if (IS_ERR(mp->m_sync_task))
  460. return -PTR_ERR(mp->m_sync_task);
  461. return 0;
  462. }
  463. void
  464. xfs_syncd_stop(
  465. struct xfs_mount *mp)
  466. {
  467. kthread_stop(mp->m_sync_task);
  468. }
  469. void
  470. __xfs_inode_set_reclaim_tag(
  471. struct xfs_perag *pag,
  472. struct xfs_inode *ip)
  473. {
  474. radix_tree_tag_set(&pag->pag_ici_root,
  475. XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
  476. XFS_ICI_RECLAIM_TAG);
  477. if (!pag->pag_ici_reclaimable) {
  478. /* propagate the reclaim tag up into the perag radix tree */
  479. spin_lock(&ip->i_mount->m_perag_lock);
  480. radix_tree_tag_set(&ip->i_mount->m_perag_tree,
  481. XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
  482. XFS_ICI_RECLAIM_TAG);
  483. spin_unlock(&ip->i_mount->m_perag_lock);
  484. trace_xfs_perag_set_reclaim(ip->i_mount, pag->pag_agno,
  485. -1, _RET_IP_);
  486. }
  487. pag->pag_ici_reclaimable++;
  488. }
  489. /*
  490. * We set the inode flag atomically with the radix tree tag.
  491. * Once we get tag lookups on the radix tree, this inode flag
  492. * can go away.
  493. */
  494. void
  495. xfs_inode_set_reclaim_tag(
  496. xfs_inode_t *ip)
  497. {
  498. struct xfs_mount *mp = ip->i_mount;
  499. struct xfs_perag *pag;
  500. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
  501. write_lock(&pag->pag_ici_lock);
  502. spin_lock(&ip->i_flags_lock);
  503. __xfs_inode_set_reclaim_tag(pag, ip);
  504. __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
  505. spin_unlock(&ip->i_flags_lock);
  506. write_unlock(&pag->pag_ici_lock);
  507. xfs_perag_put(pag);
  508. }
  509. STATIC void
  510. __xfs_inode_clear_reclaim(
  511. xfs_perag_t *pag,
  512. xfs_inode_t *ip)
  513. {
  514. pag->pag_ici_reclaimable--;
  515. if (!pag->pag_ici_reclaimable) {
  516. /* clear the reclaim tag from the perag radix tree */
  517. spin_lock(&ip->i_mount->m_perag_lock);
  518. radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
  519. XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
  520. XFS_ICI_RECLAIM_TAG);
  521. spin_unlock(&ip->i_mount->m_perag_lock);
  522. trace_xfs_perag_clear_reclaim(ip->i_mount, pag->pag_agno,
  523. -1, _RET_IP_);
  524. }
  525. }
  526. void
  527. __xfs_inode_clear_reclaim_tag(
  528. xfs_mount_t *mp,
  529. xfs_perag_t *pag,
  530. xfs_inode_t *ip)
  531. {
  532. radix_tree_tag_clear(&pag->pag_ici_root,
  533. XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
  534. __xfs_inode_clear_reclaim(pag, ip);
  535. }
  536. /*
  537. * Inodes in different states need to be treated differently, and the return
  538. * value of xfs_iflush is not sufficient to get this right. The following table
  539. * lists the inode states and the reclaim actions necessary for non-blocking
  540. * reclaim:
  541. *
  542. *
  543. * inode state iflush ret required action
  544. * --------------- ---------- ---------------
  545. * bad - reclaim
  546. * shutdown EIO unpin and reclaim
  547. * clean, unpinned 0 reclaim
  548. * stale, unpinned 0 reclaim
  549. * clean, pinned(*) 0 requeue
  550. * stale, pinned EAGAIN requeue
  551. * dirty, delwri ok 0 requeue
  552. * dirty, delwri blocked EAGAIN requeue
  553. * dirty, sync flush 0 reclaim
  554. *
  555. * (*) dgc: I don't think the clean, pinned state is possible but it gets
  556. * handled anyway given the order of checks implemented.
  557. *
  558. * As can be seen from the table, the return value of xfs_iflush() is not
  559. * sufficient to correctly decide the reclaim action here. The checks in
  560. * xfs_iflush() might look like duplicates, but they are not.
  561. *
  562. * Also, because we get the flush lock first, we know that any inode that has
  563. * been flushed delwri has had the flush completed by the time we check that
  564. * the inode is clean. The clean inode check needs to be done before flushing
  565. * the inode delwri otherwise we would loop forever requeuing clean inodes as
  566. * we cannot tell apart a successful delwri flush and a clean inode from the
  567. * return value of xfs_iflush().
  568. *
  569. * Note that because the inode is flushed delayed write by background
  570. * writeback, the flush lock may already be held here and waiting on it can
  571. * result in very long latencies. Hence for sync reclaims, where we wait on the
  572. * flush lock, the caller should push out delayed write inodes first before
  573. * trying to reclaim them to minimise the amount of time spent waiting. For
  574. * background relaim, we just requeue the inode for the next pass.
  575. *
  576. * Hence the order of actions after gaining the locks should be:
  577. * bad => reclaim
  578. * shutdown => unpin and reclaim
  579. * pinned, delwri => requeue
  580. * pinned, sync => unpin
  581. * stale => reclaim
  582. * clean => reclaim
  583. * dirty, delwri => flush and requeue
  584. * dirty, sync => flush, wait and reclaim
  585. */
  586. STATIC int
  587. xfs_reclaim_inode(
  588. struct xfs_inode *ip,
  589. struct xfs_perag *pag,
  590. int sync_mode)
  591. {
  592. int error = 0;
  593. /*
  594. * The radix tree lock here protects a thread in xfs_iget from racing
  595. * with us starting reclaim on the inode. Once we have the
  596. * XFS_IRECLAIM flag set it will not touch us.
  597. */
  598. spin_lock(&ip->i_flags_lock);
  599. ASSERT_ALWAYS(__xfs_iflags_test(ip, XFS_IRECLAIMABLE));
  600. if (__xfs_iflags_test(ip, XFS_IRECLAIM)) {
  601. /* ignore as it is already under reclaim */
  602. spin_unlock(&ip->i_flags_lock);
  603. write_unlock(&pag->pag_ici_lock);
  604. return 0;
  605. }
  606. __xfs_iflags_set(ip, XFS_IRECLAIM);
  607. spin_unlock(&ip->i_flags_lock);
  608. write_unlock(&pag->pag_ici_lock);
  609. xfs_ilock(ip, XFS_ILOCK_EXCL);
  610. if (!xfs_iflock_nowait(ip)) {
  611. if (!(sync_mode & SYNC_WAIT))
  612. goto out;
  613. xfs_iflock(ip);
  614. }
  615. if (is_bad_inode(VFS_I(ip)))
  616. goto reclaim;
  617. if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  618. xfs_iunpin_wait(ip);
  619. goto reclaim;
  620. }
  621. if (xfs_ipincount(ip)) {
  622. if (!(sync_mode & SYNC_WAIT)) {
  623. xfs_ifunlock(ip);
  624. goto out;
  625. }
  626. xfs_iunpin_wait(ip);
  627. }
  628. if (xfs_iflags_test(ip, XFS_ISTALE))
  629. goto reclaim;
  630. if (xfs_inode_clean(ip))
  631. goto reclaim;
  632. /* Now we have an inode that needs flushing */
  633. error = xfs_iflush(ip, sync_mode);
  634. if (sync_mode & SYNC_WAIT) {
  635. xfs_iflock(ip);
  636. goto reclaim;
  637. }
  638. /*
  639. * When we have to flush an inode but don't have SYNC_WAIT set, we
  640. * flush the inode out using a delwri buffer and wait for the next
  641. * call into reclaim to find it in a clean state instead of waiting for
  642. * it now. We also don't return errors here - if the error is transient
  643. * then the next reclaim pass will flush the inode, and if the error
  644. * is permanent then the next sync reclaim will reclaim the inode and
  645. * pass on the error.
  646. */
  647. if (error && error != EAGAIN && !XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  648. xfs_fs_cmn_err(CE_WARN, ip->i_mount,
  649. "inode 0x%llx background reclaim flush failed with %d",
  650. (long long)ip->i_ino, error);
  651. }
  652. out:
  653. xfs_iflags_clear(ip, XFS_IRECLAIM);
  654. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  655. /*
  656. * We could return EAGAIN here to make reclaim rescan the inode tree in
  657. * a short while. However, this just burns CPU time scanning the tree
  658. * waiting for IO to complete and xfssyncd never goes back to the idle
  659. * state. Instead, return 0 to let the next scheduled background reclaim
  660. * attempt to reclaim the inode again.
  661. */
  662. return 0;
  663. reclaim:
  664. xfs_ifunlock(ip);
  665. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  666. XFS_STATS_INC(xs_ig_reclaims);
  667. /*
  668. * Remove the inode from the per-AG radix tree.
  669. *
  670. * Because radix_tree_delete won't complain even if the item was never
  671. * added to the tree assert that it's been there before to catch
  672. * problems with the inode life time early on.
  673. */
  674. write_lock(&pag->pag_ici_lock);
  675. if (!radix_tree_delete(&pag->pag_ici_root,
  676. XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino)))
  677. ASSERT(0);
  678. __xfs_inode_clear_reclaim(pag, ip);
  679. write_unlock(&pag->pag_ici_lock);
  680. /*
  681. * Here we do an (almost) spurious inode lock in order to coordinate
  682. * with inode cache radix tree lookups. This is because the lookup
  683. * can reference the inodes in the cache without taking references.
  684. *
  685. * We make that OK here by ensuring that we wait until the inode is
  686. * unlocked after the lookup before we go ahead and free it. We get
  687. * both the ilock and the iolock because the code may need to drop the
  688. * ilock one but will still hold the iolock.
  689. */
  690. xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  691. xfs_qm_dqdetach(ip);
  692. xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  693. xfs_inode_free(ip);
  694. return error;
  695. }
  696. /*
  697. * Walk the AGs and reclaim the inodes in them. Even if the filesystem is
  698. * corrupted, we still want to try to reclaim all the inodes. If we don't,
  699. * then a shut down during filesystem unmount reclaim walk leak all the
  700. * unreclaimed inodes.
  701. */
  702. int
  703. xfs_reclaim_inodes_ag(
  704. struct xfs_mount *mp,
  705. int flags,
  706. int *nr_to_scan)
  707. {
  708. struct xfs_perag *pag;
  709. int error = 0;
  710. int last_error = 0;
  711. xfs_agnumber_t ag;
  712. ag = 0;
  713. while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
  714. unsigned long first_index = 0;
  715. int done = 0;
  716. ag = pag->pag_agno + 1;
  717. do {
  718. struct xfs_inode *ip;
  719. int nr_found;
  720. write_lock(&pag->pag_ici_lock);
  721. nr_found = radix_tree_gang_lookup_tag(&pag->pag_ici_root,
  722. (void **)&ip, first_index, 1,
  723. XFS_ICI_RECLAIM_TAG);
  724. if (!nr_found) {
  725. write_unlock(&pag->pag_ici_lock);
  726. break;
  727. }
  728. /*
  729. * Update the index for the next lookup. Catch overflows
  730. * into the next AG range which can occur if we have inodes
  731. * in the last block of the AG and we are currently
  732. * pointing to the last inode.
  733. */
  734. first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
  735. if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
  736. done = 1;
  737. error = xfs_reclaim_inode(ip, pag, flags);
  738. if (error && last_error != EFSCORRUPTED)
  739. last_error = error;
  740. } while (!done && (*nr_to_scan)--);
  741. xfs_perag_put(pag);
  742. }
  743. return XFS_ERROR(last_error);
  744. }
  745. int
  746. xfs_reclaim_inodes(
  747. xfs_mount_t *mp,
  748. int mode)
  749. {
  750. int nr_to_scan = INT_MAX;
  751. return xfs_reclaim_inodes_ag(mp, mode, &nr_to_scan);
  752. }
  753. /*
  754. * Shrinker infrastructure.
  755. */
  756. static int
  757. xfs_reclaim_inode_shrink(
  758. struct shrinker *shrink,
  759. int nr_to_scan,
  760. gfp_t gfp_mask)
  761. {
  762. struct xfs_mount *mp;
  763. struct xfs_perag *pag;
  764. xfs_agnumber_t ag;
  765. int reclaimable;
  766. mp = container_of(shrink, struct xfs_mount, m_inode_shrink);
  767. if (nr_to_scan) {
  768. if (!(gfp_mask & __GFP_FS))
  769. return -1;
  770. xfs_reclaim_inodes_ag(mp, 0, &nr_to_scan);
  771. /* terminate if we don't exhaust the scan */
  772. if (nr_to_scan > 0)
  773. return -1;
  774. }
  775. reclaimable = 0;
  776. ag = 0;
  777. while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
  778. ag = pag->pag_agno + 1;
  779. reclaimable += pag->pag_ici_reclaimable;
  780. xfs_perag_put(pag);
  781. }
  782. return reclaimable;
  783. }
  784. void
  785. xfs_inode_shrinker_register(
  786. struct xfs_mount *mp)
  787. {
  788. mp->m_inode_shrink.shrink = xfs_reclaim_inode_shrink;
  789. mp->m_inode_shrink.seeks = DEFAULT_SEEKS;
  790. register_shrinker(&mp->m_inode_shrink);
  791. }
  792. void
  793. xfs_inode_shrinker_unregister(
  794. struct xfs_mount *mp)
  795. {
  796. unregister_shrinker(&mp->m_inode_shrink);
  797. }