xfs_sync.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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. struct completion *completion)
  360. {
  361. struct xfs_sync_work *work;
  362. work = kmem_alloc(sizeof(struct xfs_sync_work), KM_SLEEP);
  363. INIT_LIST_HEAD(&work->w_list);
  364. work->w_syncer = syncer;
  365. work->w_data = data;
  366. work->w_mount = mp;
  367. work->w_completion = completion;
  368. spin_lock(&mp->m_sync_lock);
  369. list_add_tail(&work->w_list, &mp->m_sync_list);
  370. spin_unlock(&mp->m_sync_lock);
  371. wake_up_process(mp->m_sync_task);
  372. }
  373. /*
  374. * Flush delayed allocate data, attempting to free up reserved space
  375. * from existing allocations. At this point a new allocation attempt
  376. * has failed with ENOSPC and we are in the process of scratching our
  377. * heads, looking about for more room...
  378. */
  379. STATIC void
  380. xfs_flush_inodes_work(
  381. struct xfs_mount *mp,
  382. void *arg)
  383. {
  384. struct inode *inode = arg;
  385. xfs_sync_inodes(mp, SYNC_DELWRI | SYNC_TRYLOCK);
  386. xfs_sync_inodes(mp, SYNC_DELWRI | SYNC_TRYLOCK | SYNC_IOWAIT);
  387. iput(inode);
  388. }
  389. void
  390. xfs_flush_inodes(
  391. xfs_inode_t *ip)
  392. {
  393. struct inode *inode = VFS_I(ip);
  394. DECLARE_COMPLETION_ONSTACK(completion);
  395. igrab(inode);
  396. xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_inodes_work, &completion);
  397. wait_for_completion(&completion);
  398. xfs_log_force(ip->i_mount, (xfs_lsn_t)0, XFS_LOG_FORCE|XFS_LOG_SYNC);
  399. }
  400. /*
  401. * Every sync period we need to unpin all items, reclaim inodes, sync
  402. * quota and write out the superblock. We might need to cover the log
  403. * to indicate it is idle.
  404. */
  405. STATIC void
  406. xfs_sync_worker(
  407. struct xfs_mount *mp,
  408. void *unused)
  409. {
  410. int error;
  411. if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
  412. xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
  413. xfs_reclaim_inodes(mp, 0, XFS_IFLUSH_DELWRI_ELSE_ASYNC);
  414. /* dgc: errors ignored here */
  415. error = XFS_QM_DQSYNC(mp, SYNC_BDFLUSH);
  416. error = xfs_sync_fsdata(mp, SYNC_BDFLUSH);
  417. if (xfs_log_need_covered(mp))
  418. error = xfs_commit_dummy_trans(mp, XFS_LOG_FORCE);
  419. }
  420. mp->m_sync_seq++;
  421. wake_up(&mp->m_wait_single_sync_task);
  422. }
  423. STATIC int
  424. xfssyncd(
  425. void *arg)
  426. {
  427. struct xfs_mount *mp = arg;
  428. long timeleft;
  429. xfs_sync_work_t *work, *n;
  430. LIST_HEAD (tmp);
  431. set_freezable();
  432. timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10);
  433. for (;;) {
  434. timeleft = schedule_timeout_interruptible(timeleft);
  435. /* swsusp */
  436. try_to_freeze();
  437. if (kthread_should_stop() && list_empty(&mp->m_sync_list))
  438. break;
  439. spin_lock(&mp->m_sync_lock);
  440. /*
  441. * We can get woken by laptop mode, to do a sync -
  442. * that's the (only!) case where the list would be
  443. * empty with time remaining.
  444. */
  445. if (!timeleft || list_empty(&mp->m_sync_list)) {
  446. if (!timeleft)
  447. timeleft = xfs_syncd_centisecs *
  448. msecs_to_jiffies(10);
  449. INIT_LIST_HEAD(&mp->m_sync_work.w_list);
  450. list_add_tail(&mp->m_sync_work.w_list,
  451. &mp->m_sync_list);
  452. }
  453. list_for_each_entry_safe(work, n, &mp->m_sync_list, w_list)
  454. list_move(&work->w_list, &tmp);
  455. spin_unlock(&mp->m_sync_lock);
  456. list_for_each_entry_safe(work, n, &tmp, w_list) {
  457. (*work->w_syncer)(mp, work->w_data);
  458. list_del(&work->w_list);
  459. if (work == &mp->m_sync_work)
  460. continue;
  461. if (work->w_completion)
  462. complete(work->w_completion);
  463. kmem_free(work);
  464. }
  465. }
  466. return 0;
  467. }
  468. int
  469. xfs_syncd_init(
  470. struct xfs_mount *mp)
  471. {
  472. mp->m_sync_work.w_syncer = xfs_sync_worker;
  473. mp->m_sync_work.w_mount = mp;
  474. mp->m_sync_work.w_completion = NULL;
  475. mp->m_sync_task = kthread_run(xfssyncd, mp, "xfssyncd");
  476. if (IS_ERR(mp->m_sync_task))
  477. return -PTR_ERR(mp->m_sync_task);
  478. return 0;
  479. }
  480. void
  481. xfs_syncd_stop(
  482. struct xfs_mount *mp)
  483. {
  484. kthread_stop(mp->m_sync_task);
  485. }
  486. int
  487. xfs_reclaim_inode(
  488. xfs_inode_t *ip,
  489. int locked,
  490. int sync_mode)
  491. {
  492. xfs_perag_t *pag = xfs_get_perag(ip->i_mount, ip->i_ino);
  493. /* The hash lock here protects a thread in xfs_iget_core from
  494. * racing with us on linking the inode back with a vnode.
  495. * Once we have the XFS_IRECLAIM flag set it will not touch
  496. * us.
  497. */
  498. write_lock(&pag->pag_ici_lock);
  499. spin_lock(&ip->i_flags_lock);
  500. if (__xfs_iflags_test(ip, XFS_IRECLAIM) ||
  501. !__xfs_iflags_test(ip, XFS_IRECLAIMABLE)) {
  502. spin_unlock(&ip->i_flags_lock);
  503. write_unlock(&pag->pag_ici_lock);
  504. if (locked) {
  505. xfs_ifunlock(ip);
  506. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  507. }
  508. return 1;
  509. }
  510. __xfs_iflags_set(ip, XFS_IRECLAIM);
  511. spin_unlock(&ip->i_flags_lock);
  512. write_unlock(&pag->pag_ici_lock);
  513. xfs_put_perag(ip->i_mount, pag);
  514. /*
  515. * If the inode is still dirty, then flush it out. If the inode
  516. * is not in the AIL, then it will be OK to flush it delwri as
  517. * long as xfs_iflush() does not keep any references to the inode.
  518. * We leave that decision up to xfs_iflush() since it has the
  519. * knowledge of whether it's OK to simply do a delwri flush of
  520. * the inode or whether we need to wait until the inode is
  521. * pulled from the AIL.
  522. * We get the flush lock regardless, though, just to make sure
  523. * we don't free it while it is being flushed.
  524. */
  525. if (!locked) {
  526. xfs_ilock(ip, XFS_ILOCK_EXCL);
  527. xfs_iflock(ip);
  528. }
  529. /*
  530. * In the case of a forced shutdown we rely on xfs_iflush() to
  531. * wait for the inode to be unpinned before returning an error.
  532. */
  533. if (!is_bad_inode(VFS_I(ip)) && xfs_iflush(ip, sync_mode) == 0) {
  534. /* synchronize with xfs_iflush_done */
  535. xfs_iflock(ip);
  536. xfs_ifunlock(ip);
  537. }
  538. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  539. xfs_ireclaim(ip);
  540. return 0;
  541. }
  542. /*
  543. * We set the inode flag atomically with the radix tree tag.
  544. * Once we get tag lookups on the radix tree, this inode flag
  545. * can go away.
  546. */
  547. void
  548. xfs_inode_set_reclaim_tag(
  549. xfs_inode_t *ip)
  550. {
  551. xfs_mount_t *mp = ip->i_mount;
  552. xfs_perag_t *pag = xfs_get_perag(mp, ip->i_ino);
  553. read_lock(&pag->pag_ici_lock);
  554. spin_lock(&ip->i_flags_lock);
  555. radix_tree_tag_set(&pag->pag_ici_root,
  556. XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
  557. __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
  558. spin_unlock(&ip->i_flags_lock);
  559. read_unlock(&pag->pag_ici_lock);
  560. xfs_put_perag(mp, pag);
  561. }
  562. void
  563. __xfs_inode_clear_reclaim_tag(
  564. xfs_mount_t *mp,
  565. xfs_perag_t *pag,
  566. xfs_inode_t *ip)
  567. {
  568. radix_tree_tag_clear(&pag->pag_ici_root,
  569. XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
  570. }
  571. void
  572. xfs_inode_clear_reclaim_tag(
  573. xfs_inode_t *ip)
  574. {
  575. xfs_mount_t *mp = ip->i_mount;
  576. xfs_perag_t *pag = xfs_get_perag(mp, ip->i_ino);
  577. read_lock(&pag->pag_ici_lock);
  578. spin_lock(&ip->i_flags_lock);
  579. __xfs_inode_clear_reclaim_tag(mp, pag, ip);
  580. spin_unlock(&ip->i_flags_lock);
  581. read_unlock(&pag->pag_ici_lock);
  582. xfs_put_perag(mp, pag);
  583. }
  584. STATIC void
  585. xfs_reclaim_inodes_ag(
  586. xfs_mount_t *mp,
  587. int ag,
  588. int noblock,
  589. int mode)
  590. {
  591. xfs_inode_t *ip = NULL;
  592. xfs_perag_t *pag = &mp->m_perag[ag];
  593. int nr_found;
  594. uint32_t first_index;
  595. int skipped;
  596. restart:
  597. first_index = 0;
  598. skipped = 0;
  599. do {
  600. /*
  601. * use a gang lookup to find the next inode in the tree
  602. * as the tree is sparse and a gang lookup walks to find
  603. * the number of objects requested.
  604. */
  605. read_lock(&pag->pag_ici_lock);
  606. nr_found = radix_tree_gang_lookup_tag(&pag->pag_ici_root,
  607. (void**)&ip, first_index, 1,
  608. XFS_ICI_RECLAIM_TAG);
  609. if (!nr_found) {
  610. read_unlock(&pag->pag_ici_lock);
  611. break;
  612. }
  613. /*
  614. * Update the index for the next lookup. Catch overflows
  615. * into the next AG range which can occur if we have inodes
  616. * in the last block of the AG and we are currently
  617. * pointing to the last inode.
  618. */
  619. first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
  620. if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino)) {
  621. read_unlock(&pag->pag_ici_lock);
  622. break;
  623. }
  624. /* ignore if already under reclaim */
  625. if (xfs_iflags_test(ip, XFS_IRECLAIM)) {
  626. read_unlock(&pag->pag_ici_lock);
  627. continue;
  628. }
  629. if (noblock) {
  630. if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
  631. read_unlock(&pag->pag_ici_lock);
  632. continue;
  633. }
  634. if (xfs_ipincount(ip) ||
  635. !xfs_iflock_nowait(ip)) {
  636. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  637. read_unlock(&pag->pag_ici_lock);
  638. continue;
  639. }
  640. }
  641. read_unlock(&pag->pag_ici_lock);
  642. /*
  643. * hmmm - this is an inode already in reclaim. Do
  644. * we even bother catching it here?
  645. */
  646. if (xfs_reclaim_inode(ip, noblock, mode))
  647. skipped++;
  648. } while (nr_found);
  649. if (skipped) {
  650. delay(1);
  651. goto restart;
  652. }
  653. return;
  654. }
  655. int
  656. xfs_reclaim_inodes(
  657. xfs_mount_t *mp,
  658. int noblock,
  659. int mode)
  660. {
  661. int i;
  662. for (i = 0; i < mp->m_sb.sb_agcount; i++) {
  663. if (!mp->m_perag[i].pag_ici_init)
  664. continue;
  665. xfs_reclaim_inodes_ag(mp, i, noblock, mode);
  666. }
  667. return 0;
  668. }