xfs_sync.c 19 KB

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