xfs_sync.c 18 KB

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