xfs_sync.c 18 KB

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