xfs_sync.c 18 KB

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