xfs_sync.c 18 KB

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