xfs_sync.c 18 KB

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