xfs_sync.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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 <linux/kthread.h>
  48. #include <linux/freezer.h>
  49. STATIC xfs_inode_t *
  50. xfs_inode_ag_lookup(
  51. struct xfs_mount *mp,
  52. struct xfs_perag *pag,
  53. uint32_t *first_index,
  54. int tag)
  55. {
  56. int nr_found;
  57. struct xfs_inode *ip;
  58. /*
  59. * use a gang lookup to find the next inode in the tree
  60. * as the tree is sparse and a gang lookup walks to find
  61. * the number of objects requested.
  62. */
  63. read_lock(&pag->pag_ici_lock);
  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. goto unlock;
  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. goto unlock;
  82. return ip;
  83. unlock:
  84. read_unlock(&pag->pag_ici_lock);
  85. return NULL;
  86. }
  87. STATIC int
  88. xfs_inode_ag_walk(
  89. struct xfs_mount *mp,
  90. xfs_agnumber_t ag,
  91. int (*execute)(struct xfs_inode *ip,
  92. struct xfs_perag *pag, int flags),
  93. int flags,
  94. int tag)
  95. {
  96. struct xfs_perag *pag = &mp->m_perag[ag];
  97. uint32_t first_index;
  98. int last_error = 0;
  99. int skipped;
  100. restart:
  101. skipped = 0;
  102. first_index = 0;
  103. do {
  104. int error = 0;
  105. xfs_inode_t *ip;
  106. ip = xfs_inode_ag_lookup(mp, pag, &first_index, tag);
  107. if (!ip)
  108. break;
  109. error = execute(ip, pag, flags);
  110. if (error == EAGAIN) {
  111. skipped++;
  112. continue;
  113. }
  114. if (error)
  115. last_error = error;
  116. /*
  117. * bail out if the filesystem is corrupted.
  118. */
  119. if (error == EFSCORRUPTED)
  120. break;
  121. } while (1);
  122. if (skipped) {
  123. delay(1);
  124. goto restart;
  125. }
  126. xfs_put_perag(mp, pag);
  127. return last_error;
  128. }
  129. int
  130. xfs_inode_ag_iterator(
  131. struct xfs_mount *mp,
  132. int (*execute)(struct xfs_inode *ip,
  133. struct xfs_perag *pag, int flags),
  134. int flags,
  135. int tag)
  136. {
  137. int error = 0;
  138. int last_error = 0;
  139. xfs_agnumber_t ag;
  140. for (ag = 0; ag < mp->m_sb.sb_agcount; ag++) {
  141. if (!mp->m_perag[ag].pag_ici_init)
  142. continue;
  143. error = xfs_inode_ag_walk(mp, ag, execute, flags, tag);
  144. if (error) {
  145. last_error = error;
  146. if (error == EFSCORRUPTED)
  147. break;
  148. }
  149. }
  150. return XFS_ERROR(last_error);
  151. }
  152. /* must be called with pag_ici_lock held and releases it */
  153. int
  154. xfs_sync_inode_valid(
  155. struct xfs_inode *ip,
  156. struct xfs_perag *pag)
  157. {
  158. struct inode *inode = VFS_I(ip);
  159. /* nothing to sync during shutdown */
  160. if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  161. read_unlock(&pag->pag_ici_lock);
  162. return EFSCORRUPTED;
  163. }
  164. /*
  165. * If we can't get a reference on the inode, it must be in reclaim.
  166. * Leave it for the reclaim code to flush. Also avoid inodes that
  167. * haven't been fully initialised.
  168. */
  169. if (!igrab(inode)) {
  170. read_unlock(&pag->pag_ici_lock);
  171. return ENOENT;
  172. }
  173. read_unlock(&pag->pag_ici_lock);
  174. if (is_bad_inode(inode) || xfs_iflags_test(ip, XFS_INEW)) {
  175. IRELE(ip);
  176. return ENOENT;
  177. }
  178. return 0;
  179. }
  180. STATIC int
  181. xfs_sync_inode_data(
  182. struct xfs_inode *ip,
  183. struct xfs_perag *pag,
  184. int flags)
  185. {
  186. struct inode *inode = VFS_I(ip);
  187. struct address_space *mapping = inode->i_mapping;
  188. int error = 0;
  189. error = xfs_sync_inode_valid(ip, pag);
  190. if (error)
  191. return error;
  192. if (!mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
  193. goto out_wait;
  194. if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED)) {
  195. if (flags & SYNC_TRYLOCK)
  196. goto out_wait;
  197. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  198. }
  199. error = xfs_flush_pages(ip, 0, -1, (flags & SYNC_WAIT) ?
  200. 0 : XFS_B_ASYNC, FI_NONE);
  201. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  202. out_wait:
  203. if (flags & SYNC_WAIT)
  204. xfs_ioend_wait(ip);
  205. IRELE(ip);
  206. return error;
  207. }
  208. STATIC int
  209. xfs_sync_inode_attr(
  210. struct xfs_inode *ip,
  211. struct xfs_perag *pag,
  212. int flags)
  213. {
  214. int error = 0;
  215. error = xfs_sync_inode_valid(ip, pag);
  216. if (error)
  217. return error;
  218. xfs_ilock(ip, XFS_ILOCK_SHARED);
  219. if (xfs_inode_clean(ip))
  220. goto out_unlock;
  221. if (!xfs_iflock_nowait(ip)) {
  222. if (!(flags & SYNC_WAIT))
  223. goto out_unlock;
  224. xfs_iflock(ip);
  225. }
  226. if (xfs_inode_clean(ip)) {
  227. xfs_ifunlock(ip);
  228. goto out_unlock;
  229. }
  230. error = xfs_iflush(ip, (flags & SYNC_WAIT) ?
  231. XFS_IFLUSH_SYNC : XFS_IFLUSH_DELWRI);
  232. out_unlock:
  233. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  234. IRELE(ip);
  235. return error;
  236. }
  237. /*
  238. * Write out pagecache data for the whole filesystem.
  239. */
  240. int
  241. xfs_sync_data(
  242. struct xfs_mount *mp,
  243. int flags)
  244. {
  245. int error;
  246. ASSERT((flags & ~(SYNC_TRYLOCK|SYNC_WAIT)) == 0);
  247. error = xfs_inode_ag_iterator(mp, xfs_sync_inode_data, flags,
  248. XFS_ICI_NO_TAG);
  249. if (error)
  250. return XFS_ERROR(error);
  251. xfs_log_force(mp, 0,
  252. (flags & SYNC_WAIT) ?
  253. XFS_LOG_FORCE | XFS_LOG_SYNC :
  254. XFS_LOG_FORCE);
  255. return 0;
  256. }
  257. /*
  258. * Write out inode metadata (attributes) for the whole filesystem.
  259. */
  260. int
  261. xfs_sync_attr(
  262. struct xfs_mount *mp,
  263. int flags)
  264. {
  265. ASSERT((flags & ~SYNC_WAIT) == 0);
  266. return xfs_inode_ag_iterator(mp, xfs_sync_inode_attr, flags,
  267. XFS_ICI_NO_TAG);
  268. }
  269. STATIC int
  270. xfs_commit_dummy_trans(
  271. struct xfs_mount *mp,
  272. uint log_flags)
  273. {
  274. struct xfs_inode *ip = mp->m_rootip;
  275. struct xfs_trans *tp;
  276. int error;
  277. /*
  278. * Put a dummy transaction in the log to tell recovery
  279. * that all others are OK.
  280. */
  281. tp = xfs_trans_alloc(mp, XFS_TRANS_DUMMY1);
  282. error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
  283. if (error) {
  284. xfs_trans_cancel(tp, 0);
  285. return error;
  286. }
  287. xfs_ilock(ip, XFS_ILOCK_EXCL);
  288. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  289. xfs_trans_ihold(tp, ip);
  290. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  291. /* XXX(hch): ignoring the error here.. */
  292. error = xfs_trans_commit(tp, 0);
  293. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  294. xfs_log_force(mp, 0, log_flags);
  295. return 0;
  296. }
  297. int
  298. xfs_sync_fsdata(
  299. struct xfs_mount *mp,
  300. int flags)
  301. {
  302. struct xfs_buf *bp;
  303. struct xfs_buf_log_item *bip;
  304. int error = 0;
  305. /*
  306. * If this is xfssyncd() then only sync the superblock if we can
  307. * lock it without sleeping and it is not pinned.
  308. */
  309. if (flags & SYNC_TRYLOCK) {
  310. ASSERT(!(flags & SYNC_WAIT));
  311. bp = xfs_getsb(mp, XFS_BUF_TRYLOCK);
  312. if (!bp)
  313. goto out;
  314. bip = XFS_BUF_FSPRIVATE(bp, struct xfs_buf_log_item *);
  315. if (!bip || !xfs_buf_item_dirty(bip) || XFS_BUF_ISPINNED(bp))
  316. goto out_brelse;
  317. } else {
  318. bp = xfs_getsb(mp, 0);
  319. /*
  320. * If the buffer is pinned then push on the log so we won't
  321. * get stuck waiting in the write for someone, maybe
  322. * ourselves, to flush the log.
  323. *
  324. * Even though we just pushed the log above, we did not have
  325. * the superblock buffer locked at that point so it can
  326. * become pinned in between there and here.
  327. */
  328. if (XFS_BUF_ISPINNED(bp))
  329. xfs_log_force(mp, 0, XFS_LOG_FORCE);
  330. }
  331. if (flags & SYNC_WAIT)
  332. XFS_BUF_UNASYNC(bp);
  333. else
  334. XFS_BUF_ASYNC(bp);
  335. return xfs_bwrite(mp, bp);
  336. out_brelse:
  337. xfs_buf_relse(bp);
  338. out:
  339. return error;
  340. }
  341. /*
  342. * When remounting a filesystem read-only or freezing the filesystem, we have
  343. * two phases to execute. This first phase is syncing the data before we
  344. * quiesce the filesystem, and the second is flushing all the inodes out after
  345. * we've waited for all the transactions created by the first phase to
  346. * complete. The second phase ensures that the inodes are written to their
  347. * location on disk rather than just existing in transactions in the log. This
  348. * means after a quiesce there is no log replay required to write the inodes to
  349. * disk (this is the main difference between a sync and a quiesce).
  350. */
  351. /*
  352. * First stage of freeze - no writers will make progress now we are here,
  353. * so we flush delwri and delalloc buffers here, then wait for all I/O to
  354. * complete. Data is frozen at that point. Metadata is not frozen,
  355. * transactions can still occur here so don't bother flushing the buftarg
  356. * because it'll just get dirty again.
  357. */
  358. int
  359. xfs_quiesce_data(
  360. struct xfs_mount *mp)
  361. {
  362. int error;
  363. /* push non-blocking */
  364. xfs_sync_data(mp, 0);
  365. xfs_qm_sync(mp, SYNC_TRYLOCK);
  366. xfs_filestream_flush(mp);
  367. /* push and block */
  368. xfs_sync_data(mp, SYNC_WAIT);
  369. xfs_qm_sync(mp, SYNC_WAIT);
  370. /* write superblock and hoover up shutdown errors */
  371. error = xfs_sync_fsdata(mp, 0);
  372. /* flush data-only devices */
  373. if (mp->m_rtdev_targp)
  374. XFS_bflush(mp->m_rtdev_targp);
  375. return error;
  376. }
  377. STATIC void
  378. xfs_quiesce_fs(
  379. struct xfs_mount *mp)
  380. {
  381. int count = 0, pincount;
  382. xfs_flush_buftarg(mp->m_ddev_targp, 0);
  383. xfs_reclaim_inodes(mp, XFS_IFLUSH_DELWRI_ELSE_ASYNC);
  384. /*
  385. * This loop must run at least twice. The first instance of the loop
  386. * will flush most meta data but that will generate more meta data
  387. * (typically directory updates). Which then must be flushed and
  388. * logged before we can write the unmount record.
  389. */
  390. do {
  391. xfs_sync_attr(mp, SYNC_WAIT);
  392. pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1);
  393. if (!pincount) {
  394. delay(50);
  395. count++;
  396. }
  397. } while (count < 2);
  398. }
  399. /*
  400. * Second stage of a quiesce. The data is already synced, now we have to take
  401. * care of the metadata. New transactions are already blocked, so we need to
  402. * wait for any remaining transactions to drain out before proceding.
  403. */
  404. void
  405. xfs_quiesce_attr(
  406. struct xfs_mount *mp)
  407. {
  408. int error = 0;
  409. /* wait for all modifications to complete */
  410. while (atomic_read(&mp->m_active_trans) > 0)
  411. delay(100);
  412. /* flush inodes and push all remaining buffers out to disk */
  413. xfs_quiesce_fs(mp);
  414. /*
  415. * Just warn here till VFS can correctly support
  416. * read-only remount without racing.
  417. */
  418. WARN_ON(atomic_read(&mp->m_active_trans) != 0);
  419. /* Push the superblock and write an unmount record */
  420. error = xfs_log_sbcount(mp, 1);
  421. if (error)
  422. xfs_fs_cmn_err(CE_WARN, mp,
  423. "xfs_attr_quiesce: failed to log sb changes. "
  424. "Frozen image may not be consistent.");
  425. xfs_log_unmount_write(mp);
  426. xfs_unmountfs_writesb(mp);
  427. }
  428. /*
  429. * Enqueue a work item to be picked up by the vfs xfssyncd thread.
  430. * Doing this has two advantages:
  431. * - It saves on stack space, which is tight in certain situations
  432. * - It can be used (with care) as a mechanism to avoid deadlocks.
  433. * Flushing while allocating in a full filesystem requires both.
  434. */
  435. STATIC void
  436. xfs_syncd_queue_work(
  437. struct xfs_mount *mp,
  438. void *data,
  439. void (*syncer)(struct xfs_mount *, void *),
  440. struct completion *completion)
  441. {
  442. struct xfs_sync_work *work;
  443. work = kmem_alloc(sizeof(struct xfs_sync_work), KM_SLEEP);
  444. INIT_LIST_HEAD(&work->w_list);
  445. work->w_syncer = syncer;
  446. work->w_data = data;
  447. work->w_mount = mp;
  448. work->w_completion = completion;
  449. spin_lock(&mp->m_sync_lock);
  450. list_add_tail(&work->w_list, &mp->m_sync_list);
  451. spin_unlock(&mp->m_sync_lock);
  452. wake_up_process(mp->m_sync_task);
  453. }
  454. /*
  455. * Flush delayed allocate data, attempting to free up reserved space
  456. * from existing allocations. At this point a new allocation attempt
  457. * has failed with ENOSPC and we are in the process of scratching our
  458. * heads, looking about for more room...
  459. */
  460. STATIC void
  461. xfs_flush_inodes_work(
  462. struct xfs_mount *mp,
  463. void *arg)
  464. {
  465. struct inode *inode = arg;
  466. xfs_sync_data(mp, SYNC_TRYLOCK);
  467. xfs_sync_data(mp, SYNC_TRYLOCK | SYNC_WAIT);
  468. iput(inode);
  469. }
  470. void
  471. xfs_flush_inodes(
  472. xfs_inode_t *ip)
  473. {
  474. struct inode *inode = VFS_I(ip);
  475. DECLARE_COMPLETION_ONSTACK(completion);
  476. igrab(inode);
  477. xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_inodes_work, &completion);
  478. wait_for_completion(&completion);
  479. xfs_log_force(ip->i_mount, (xfs_lsn_t)0, XFS_LOG_FORCE|XFS_LOG_SYNC);
  480. }
  481. /*
  482. * Every sync period we need to unpin all items, reclaim inodes, sync
  483. * quota and write out the superblock. We might need to cover the log
  484. * to indicate it is idle.
  485. */
  486. STATIC void
  487. xfs_sync_worker(
  488. struct xfs_mount *mp,
  489. void *unused)
  490. {
  491. int error;
  492. if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
  493. xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
  494. xfs_reclaim_inodes(mp, XFS_IFLUSH_DELWRI_ELSE_ASYNC);
  495. /* dgc: errors ignored here */
  496. error = xfs_qm_sync(mp, SYNC_TRYLOCK);
  497. error = xfs_sync_fsdata(mp, SYNC_TRYLOCK);
  498. if (xfs_log_need_covered(mp))
  499. error = xfs_commit_dummy_trans(mp, XFS_LOG_FORCE);
  500. }
  501. mp->m_sync_seq++;
  502. wake_up(&mp->m_wait_single_sync_task);
  503. }
  504. STATIC int
  505. xfssyncd(
  506. void *arg)
  507. {
  508. struct xfs_mount *mp = arg;
  509. long timeleft;
  510. xfs_sync_work_t *work, *n;
  511. LIST_HEAD (tmp);
  512. set_freezable();
  513. timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10);
  514. for (;;) {
  515. timeleft = schedule_timeout_interruptible(timeleft);
  516. /* swsusp */
  517. try_to_freeze();
  518. if (kthread_should_stop() && list_empty(&mp->m_sync_list))
  519. break;
  520. spin_lock(&mp->m_sync_lock);
  521. /*
  522. * We can get woken by laptop mode, to do a sync -
  523. * that's the (only!) case where the list would be
  524. * empty with time remaining.
  525. */
  526. if (!timeleft || list_empty(&mp->m_sync_list)) {
  527. if (!timeleft)
  528. timeleft = xfs_syncd_centisecs *
  529. msecs_to_jiffies(10);
  530. INIT_LIST_HEAD(&mp->m_sync_work.w_list);
  531. list_add_tail(&mp->m_sync_work.w_list,
  532. &mp->m_sync_list);
  533. }
  534. list_for_each_entry_safe(work, n, &mp->m_sync_list, w_list)
  535. list_move(&work->w_list, &tmp);
  536. spin_unlock(&mp->m_sync_lock);
  537. list_for_each_entry_safe(work, n, &tmp, w_list) {
  538. (*work->w_syncer)(mp, work->w_data);
  539. list_del(&work->w_list);
  540. if (work == &mp->m_sync_work)
  541. continue;
  542. if (work->w_completion)
  543. complete(work->w_completion);
  544. kmem_free(work);
  545. }
  546. }
  547. return 0;
  548. }
  549. int
  550. xfs_syncd_init(
  551. struct xfs_mount *mp)
  552. {
  553. mp->m_sync_work.w_syncer = xfs_sync_worker;
  554. mp->m_sync_work.w_mount = mp;
  555. mp->m_sync_work.w_completion = NULL;
  556. mp->m_sync_task = kthread_run(xfssyncd, mp, "xfssyncd");
  557. if (IS_ERR(mp->m_sync_task))
  558. return -PTR_ERR(mp->m_sync_task);
  559. return 0;
  560. }
  561. void
  562. xfs_syncd_stop(
  563. struct xfs_mount *mp)
  564. {
  565. kthread_stop(mp->m_sync_task);
  566. }
  567. int
  568. xfs_reclaim_inode(
  569. xfs_inode_t *ip,
  570. int locked,
  571. int sync_mode)
  572. {
  573. xfs_perag_t *pag = xfs_get_perag(ip->i_mount, ip->i_ino);
  574. /* The hash lock here protects a thread in xfs_iget_core from
  575. * racing with us on linking the inode back with a vnode.
  576. * Once we have the XFS_IRECLAIM flag set it will not touch
  577. * us.
  578. */
  579. write_lock(&pag->pag_ici_lock);
  580. spin_lock(&ip->i_flags_lock);
  581. if (__xfs_iflags_test(ip, XFS_IRECLAIM) ||
  582. !__xfs_iflags_test(ip, XFS_IRECLAIMABLE)) {
  583. spin_unlock(&ip->i_flags_lock);
  584. write_unlock(&pag->pag_ici_lock);
  585. if (locked) {
  586. xfs_ifunlock(ip);
  587. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  588. }
  589. return -EAGAIN;
  590. }
  591. __xfs_iflags_set(ip, XFS_IRECLAIM);
  592. spin_unlock(&ip->i_flags_lock);
  593. write_unlock(&pag->pag_ici_lock);
  594. xfs_put_perag(ip->i_mount, pag);
  595. /*
  596. * If the inode is still dirty, then flush it out. If the inode
  597. * is not in the AIL, then it will be OK to flush it delwri as
  598. * long as xfs_iflush() does not keep any references to the inode.
  599. * We leave that decision up to xfs_iflush() since it has the
  600. * knowledge of whether it's OK to simply do a delwri flush of
  601. * the inode or whether we need to wait until the inode is
  602. * pulled from the AIL.
  603. * We get the flush lock regardless, though, just to make sure
  604. * we don't free it while it is being flushed.
  605. */
  606. if (!locked) {
  607. xfs_ilock(ip, XFS_ILOCK_EXCL);
  608. xfs_iflock(ip);
  609. }
  610. /*
  611. * In the case of a forced shutdown we rely on xfs_iflush() to
  612. * wait for the inode to be unpinned before returning an error.
  613. */
  614. if (!is_bad_inode(VFS_I(ip)) && xfs_iflush(ip, sync_mode) == 0) {
  615. /* synchronize with xfs_iflush_done */
  616. xfs_iflock(ip);
  617. xfs_ifunlock(ip);
  618. }
  619. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  620. xfs_ireclaim(ip);
  621. return 0;
  622. }
  623. /*
  624. * We set the inode flag atomically with the radix tree tag.
  625. * Once we get tag lookups on the radix tree, this inode flag
  626. * can go away.
  627. */
  628. void
  629. xfs_inode_set_reclaim_tag(
  630. xfs_inode_t *ip)
  631. {
  632. xfs_mount_t *mp = ip->i_mount;
  633. xfs_perag_t *pag = xfs_get_perag(mp, ip->i_ino);
  634. read_lock(&pag->pag_ici_lock);
  635. spin_lock(&ip->i_flags_lock);
  636. radix_tree_tag_set(&pag->pag_ici_root,
  637. XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
  638. __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
  639. spin_unlock(&ip->i_flags_lock);
  640. read_unlock(&pag->pag_ici_lock);
  641. xfs_put_perag(mp, pag);
  642. }
  643. void
  644. __xfs_inode_clear_reclaim_tag(
  645. xfs_mount_t *mp,
  646. xfs_perag_t *pag,
  647. xfs_inode_t *ip)
  648. {
  649. radix_tree_tag_clear(&pag->pag_ici_root,
  650. XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
  651. }
  652. void
  653. xfs_inode_clear_reclaim_tag(
  654. xfs_inode_t *ip)
  655. {
  656. xfs_mount_t *mp = ip->i_mount;
  657. xfs_perag_t *pag = xfs_get_perag(mp, ip->i_ino);
  658. read_lock(&pag->pag_ici_lock);
  659. spin_lock(&ip->i_flags_lock);
  660. __xfs_inode_clear_reclaim_tag(mp, pag, ip);
  661. spin_unlock(&ip->i_flags_lock);
  662. read_unlock(&pag->pag_ici_lock);
  663. xfs_put_perag(mp, pag);
  664. }
  665. STATIC int
  666. xfs_reclaim_inode_now(
  667. struct xfs_inode *ip,
  668. struct xfs_perag *pag,
  669. int flags)
  670. {
  671. /* ignore if already under reclaim */
  672. if (xfs_iflags_test(ip, XFS_IRECLAIM)) {
  673. read_unlock(&pag->pag_ici_lock);
  674. return 0;
  675. }
  676. read_unlock(&pag->pag_ici_lock);
  677. return xfs_reclaim_inode(ip, 0, flags);
  678. }
  679. int
  680. xfs_reclaim_inodes(
  681. xfs_mount_t *mp,
  682. int mode)
  683. {
  684. return xfs_inode_ag_iterator(mp, xfs_reclaim_inode_now, mode,
  685. XFS_ICI_RECLAIM_TAG);
  686. }