xfs_sync.c 19 KB

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