xfs_sync.c 21 KB

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