xfs_sync.c 18 KB

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