xfs_sync.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  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_mount.h"
  28. #include "xfs_bmap_btree.h"
  29. #include "xfs_inode.h"
  30. #include "xfs_dinode.h"
  31. #include "xfs_error.h"
  32. #include "xfs_filestream.h"
  33. #include "xfs_vnodeops.h"
  34. #include "xfs_inode_item.h"
  35. #include "xfs_quota.h"
  36. #include "xfs_trace.h"
  37. #include "xfs_fsops.h"
  38. #include <linux/kthread.h>
  39. #include <linux/freezer.h>
  40. STATIC xfs_inode_t *
  41. xfs_inode_ag_lookup(
  42. struct xfs_mount *mp,
  43. struct xfs_perag *pag,
  44. uint32_t *first_index,
  45. int tag)
  46. {
  47. int nr_found;
  48. struct xfs_inode *ip;
  49. /*
  50. * use a gang lookup to find the next inode in the tree
  51. * as the tree is sparse and a gang lookup walks to find
  52. * the number of objects requested.
  53. */
  54. if (tag == XFS_ICI_NO_TAG) {
  55. nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
  56. (void **)&ip, *first_index, 1);
  57. } else {
  58. nr_found = radix_tree_gang_lookup_tag(&pag->pag_ici_root,
  59. (void **)&ip, *first_index, 1, tag);
  60. }
  61. if (!nr_found)
  62. return NULL;
  63. /*
  64. * Update the index for the next lookup. Catch overflows
  65. * into the next AG range which can occur if we have inodes
  66. * in the last block of the AG and we are currently
  67. * pointing to the last inode.
  68. */
  69. *first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
  70. if (*first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
  71. return NULL;
  72. return ip;
  73. }
  74. STATIC int
  75. xfs_inode_ag_walk(
  76. struct xfs_mount *mp,
  77. struct xfs_perag *pag,
  78. int (*execute)(struct xfs_inode *ip,
  79. struct xfs_perag *pag, int flags),
  80. int flags,
  81. int tag,
  82. int exclusive,
  83. int *nr_to_scan)
  84. {
  85. uint32_t first_index;
  86. int last_error = 0;
  87. int skipped;
  88. restart:
  89. skipped = 0;
  90. first_index = 0;
  91. do {
  92. int error = 0;
  93. xfs_inode_t *ip;
  94. if (exclusive)
  95. write_lock(&pag->pag_ici_lock);
  96. else
  97. read_lock(&pag->pag_ici_lock);
  98. ip = xfs_inode_ag_lookup(mp, pag, &first_index, tag);
  99. if (!ip) {
  100. if (exclusive)
  101. write_unlock(&pag->pag_ici_lock);
  102. else
  103. read_unlock(&pag->pag_ici_lock);
  104. break;
  105. }
  106. /* execute releases pag->pag_ici_lock */
  107. error = execute(ip, pag, flags);
  108. if (error == EAGAIN) {
  109. skipped++;
  110. continue;
  111. }
  112. if (error)
  113. last_error = error;
  114. /* bail out if the filesystem is corrupted. */
  115. if (error == EFSCORRUPTED)
  116. break;
  117. } while ((*nr_to_scan)--);
  118. if (skipped) {
  119. delay(1);
  120. goto restart;
  121. }
  122. return last_error;
  123. }
  124. /*
  125. * Select the next per-ag structure to iterate during the walk. The reclaim
  126. * walk is optimised only to walk AGs with reclaimable inodes in them.
  127. */
  128. static struct xfs_perag *
  129. xfs_inode_ag_iter_next_pag(
  130. struct xfs_mount *mp,
  131. xfs_agnumber_t *first,
  132. int tag)
  133. {
  134. struct xfs_perag *pag = NULL;
  135. if (tag == XFS_ICI_RECLAIM_TAG) {
  136. int found;
  137. int ref;
  138. spin_lock(&mp->m_perag_lock);
  139. found = radix_tree_gang_lookup_tag(&mp->m_perag_tree,
  140. (void **)&pag, *first, 1, tag);
  141. if (found <= 0) {
  142. spin_unlock(&mp->m_perag_lock);
  143. return NULL;
  144. }
  145. *first = pag->pag_agno + 1;
  146. /* open coded pag reference increment */
  147. ref = atomic_inc_return(&pag->pag_ref);
  148. spin_unlock(&mp->m_perag_lock);
  149. trace_xfs_perag_get_reclaim(mp, pag->pag_agno, ref, _RET_IP_);
  150. } else {
  151. pag = xfs_perag_get(mp, *first);
  152. (*first)++;
  153. }
  154. return pag;
  155. }
  156. int
  157. xfs_inode_ag_iterator(
  158. struct xfs_mount *mp,
  159. int (*execute)(struct xfs_inode *ip,
  160. struct xfs_perag *pag, int flags),
  161. int flags,
  162. int tag,
  163. int exclusive,
  164. int *nr_to_scan)
  165. {
  166. struct xfs_perag *pag;
  167. int error = 0;
  168. int last_error = 0;
  169. xfs_agnumber_t ag;
  170. int nr;
  171. nr = nr_to_scan ? *nr_to_scan : INT_MAX;
  172. ag = 0;
  173. while ((pag = xfs_inode_ag_iter_next_pag(mp, &ag, tag))) {
  174. error = xfs_inode_ag_walk(mp, pag, execute, flags, tag,
  175. exclusive, &nr);
  176. xfs_perag_put(pag);
  177. if (error) {
  178. last_error = error;
  179. if (error == EFSCORRUPTED)
  180. break;
  181. }
  182. if (nr <= 0)
  183. break;
  184. }
  185. if (nr_to_scan)
  186. *nr_to_scan = nr;
  187. return XFS_ERROR(last_error);
  188. }
  189. /* must be called with pag_ici_lock held and releases it */
  190. int
  191. xfs_sync_inode_valid(
  192. struct xfs_inode *ip,
  193. struct xfs_perag *pag)
  194. {
  195. struct inode *inode = VFS_I(ip);
  196. int error = EFSCORRUPTED;
  197. /* nothing to sync during shutdown */
  198. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  199. goto out_unlock;
  200. /* avoid new or reclaimable inodes. Leave for reclaim code to flush */
  201. error = ENOENT;
  202. if (xfs_iflags_test(ip, XFS_INEW | XFS_IRECLAIMABLE | XFS_IRECLAIM))
  203. goto out_unlock;
  204. /* If we can't grab the inode, it must on it's way to reclaim. */
  205. if (!igrab(inode))
  206. goto out_unlock;
  207. if (is_bad_inode(inode)) {
  208. IRELE(ip);
  209. goto out_unlock;
  210. }
  211. /* inode is valid */
  212. error = 0;
  213. out_unlock:
  214. read_unlock(&pag->pag_ici_lock);
  215. return error;
  216. }
  217. STATIC int
  218. xfs_sync_inode_data(
  219. struct xfs_inode *ip,
  220. struct xfs_perag *pag,
  221. int flags)
  222. {
  223. struct inode *inode = VFS_I(ip);
  224. struct address_space *mapping = inode->i_mapping;
  225. int error = 0;
  226. error = xfs_sync_inode_valid(ip, pag);
  227. if (error)
  228. return error;
  229. if (!mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
  230. goto out_wait;
  231. if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED)) {
  232. if (flags & SYNC_TRYLOCK)
  233. goto out_wait;
  234. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  235. }
  236. error = xfs_flush_pages(ip, 0, -1, (flags & SYNC_WAIT) ?
  237. 0 : XBF_ASYNC, FI_NONE);
  238. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  239. out_wait:
  240. if (flags & SYNC_WAIT)
  241. xfs_ioend_wait(ip);
  242. IRELE(ip);
  243. return error;
  244. }
  245. STATIC int
  246. xfs_sync_inode_attr(
  247. struct xfs_inode *ip,
  248. struct xfs_perag *pag,
  249. int flags)
  250. {
  251. int error = 0;
  252. error = xfs_sync_inode_valid(ip, pag);
  253. if (error)
  254. return error;
  255. xfs_ilock(ip, XFS_ILOCK_SHARED);
  256. if (xfs_inode_clean(ip))
  257. goto out_unlock;
  258. if (!xfs_iflock_nowait(ip)) {
  259. if (!(flags & SYNC_WAIT))
  260. goto out_unlock;
  261. xfs_iflock(ip);
  262. }
  263. if (xfs_inode_clean(ip)) {
  264. xfs_ifunlock(ip);
  265. goto out_unlock;
  266. }
  267. error = xfs_iflush(ip, flags);
  268. out_unlock:
  269. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  270. IRELE(ip);
  271. return error;
  272. }
  273. /*
  274. * Write out pagecache data for the whole filesystem.
  275. */
  276. STATIC int
  277. xfs_sync_data(
  278. struct xfs_mount *mp,
  279. int flags)
  280. {
  281. int error;
  282. ASSERT((flags & ~(SYNC_TRYLOCK|SYNC_WAIT)) == 0);
  283. error = xfs_inode_ag_iterator(mp, xfs_sync_inode_data, flags,
  284. XFS_ICI_NO_TAG, 0, NULL);
  285. if (error)
  286. return XFS_ERROR(error);
  287. xfs_log_force(mp, (flags & SYNC_WAIT) ? XFS_LOG_SYNC : 0);
  288. return 0;
  289. }
  290. /*
  291. * Write out inode metadata (attributes) for the whole filesystem.
  292. */
  293. STATIC int
  294. xfs_sync_attr(
  295. struct xfs_mount *mp,
  296. int flags)
  297. {
  298. ASSERT((flags & ~SYNC_WAIT) == 0);
  299. return xfs_inode_ag_iterator(mp, xfs_sync_inode_attr, flags,
  300. XFS_ICI_NO_TAG, 0, NULL);
  301. }
  302. STATIC int
  303. xfs_sync_fsdata(
  304. struct xfs_mount *mp)
  305. {
  306. struct xfs_buf *bp;
  307. /*
  308. * If the buffer is pinned then push on the log so we won't get stuck
  309. * waiting in the write for someone, maybe ourselves, to flush the log.
  310. *
  311. * Even though we just pushed the log above, we did not have the
  312. * superblock buffer locked at that point so it can become pinned in
  313. * between there and here.
  314. */
  315. bp = xfs_getsb(mp, 0);
  316. if (XFS_BUF_ISPINNED(bp))
  317. xfs_log_force(mp, 0);
  318. return xfs_bwrite(mp, bp);
  319. }
  320. /*
  321. * When remounting a filesystem read-only or freezing the filesystem, we have
  322. * two phases to execute. This first phase is syncing the data before we
  323. * quiesce the filesystem, and the second is flushing all the inodes out after
  324. * we've waited for all the transactions created by the first phase to
  325. * complete. The second phase ensures that the inodes are written to their
  326. * location on disk rather than just existing in transactions in the log. This
  327. * means after a quiesce there is no log replay required to write the inodes to
  328. * disk (this is the main difference between a sync and a quiesce).
  329. */
  330. /*
  331. * First stage of freeze - no writers will make progress now we are here,
  332. * so we flush delwri and delalloc buffers here, then wait for all I/O to
  333. * complete. Data is frozen at that point. Metadata is not frozen,
  334. * transactions can still occur here so don't bother flushing the buftarg
  335. * because it'll just get dirty again.
  336. */
  337. int
  338. xfs_quiesce_data(
  339. struct xfs_mount *mp)
  340. {
  341. int error, error2 = 0;
  342. /* push non-blocking */
  343. xfs_sync_data(mp, 0);
  344. xfs_qm_sync(mp, SYNC_TRYLOCK);
  345. /* push and block till complete */
  346. xfs_sync_data(mp, SYNC_WAIT);
  347. xfs_qm_sync(mp, SYNC_WAIT);
  348. /* write superblock and hoover up shutdown errors */
  349. error = xfs_sync_fsdata(mp);
  350. /* make sure all delwri buffers are written out */
  351. xfs_flush_buftarg(mp->m_ddev_targp, 1);
  352. /* mark the log as covered if needed */
  353. if (xfs_log_need_covered(mp))
  354. error2 = xfs_fs_log_dummy(mp, SYNC_WAIT);
  355. /* flush data-only devices */
  356. if (mp->m_rtdev_targp)
  357. XFS_bflush(mp->m_rtdev_targp);
  358. return error ? error : error2;
  359. }
  360. STATIC void
  361. xfs_quiesce_fs(
  362. struct xfs_mount *mp)
  363. {
  364. int count = 0, pincount;
  365. xfs_reclaim_inodes(mp, 0);
  366. xfs_flush_buftarg(mp->m_ddev_targp, 0);
  367. /*
  368. * This loop must run at least twice. The first instance of the loop
  369. * will flush most meta data but that will generate more meta data
  370. * (typically directory updates). Which then must be flushed and
  371. * logged before we can write the unmount record. We also so sync
  372. * reclaim of inodes to catch any that the above delwri flush skipped.
  373. */
  374. do {
  375. xfs_reclaim_inodes(mp, SYNC_WAIT);
  376. xfs_sync_attr(mp, SYNC_WAIT);
  377. pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1);
  378. if (!pincount) {
  379. delay(50);
  380. count++;
  381. }
  382. } while (count < 2);
  383. }
  384. /*
  385. * Second stage of a quiesce. The data is already synced, now we have to take
  386. * care of the metadata. New transactions are already blocked, so we need to
  387. * wait for any remaining transactions to drain out before proceding.
  388. */
  389. void
  390. xfs_quiesce_attr(
  391. struct xfs_mount *mp)
  392. {
  393. int error = 0;
  394. /* wait for all modifications to complete */
  395. while (atomic_read(&mp->m_active_trans) > 0)
  396. delay(100);
  397. /* flush inodes and push all remaining buffers out to disk */
  398. xfs_quiesce_fs(mp);
  399. /*
  400. * Just warn here till VFS can correctly support
  401. * read-only remount without racing.
  402. */
  403. WARN_ON(atomic_read(&mp->m_active_trans) != 0);
  404. /* Push the superblock and write an unmount record */
  405. error = xfs_log_sbcount(mp, 1);
  406. if (error)
  407. xfs_fs_cmn_err(CE_WARN, mp,
  408. "xfs_attr_quiesce: failed to log sb changes. "
  409. "Frozen image may not be consistent.");
  410. xfs_log_unmount_write(mp);
  411. xfs_unmountfs_writesb(mp);
  412. }
  413. /*
  414. * Enqueue a work item to be picked up by the vfs xfssyncd thread.
  415. * Doing this has two advantages:
  416. * - It saves on stack space, which is tight in certain situations
  417. * - It can be used (with care) as a mechanism to avoid deadlocks.
  418. * Flushing while allocating in a full filesystem requires both.
  419. */
  420. STATIC void
  421. xfs_syncd_queue_work(
  422. struct xfs_mount *mp,
  423. void *data,
  424. void (*syncer)(struct xfs_mount *, void *),
  425. struct completion *completion)
  426. {
  427. struct xfs_sync_work *work;
  428. work = kmem_alloc(sizeof(struct xfs_sync_work), KM_SLEEP);
  429. INIT_LIST_HEAD(&work->w_list);
  430. work->w_syncer = syncer;
  431. work->w_data = data;
  432. work->w_mount = mp;
  433. work->w_completion = completion;
  434. spin_lock(&mp->m_sync_lock);
  435. list_add_tail(&work->w_list, &mp->m_sync_list);
  436. spin_unlock(&mp->m_sync_lock);
  437. wake_up_process(mp->m_sync_task);
  438. }
  439. /*
  440. * Flush delayed allocate data, attempting to free up reserved space
  441. * from existing allocations. At this point a new allocation attempt
  442. * has failed with ENOSPC and we are in the process of scratching our
  443. * heads, looking about for more room...
  444. */
  445. STATIC void
  446. xfs_flush_inodes_work(
  447. struct xfs_mount *mp,
  448. void *arg)
  449. {
  450. struct inode *inode = arg;
  451. xfs_sync_data(mp, SYNC_TRYLOCK);
  452. xfs_sync_data(mp, SYNC_TRYLOCK | SYNC_WAIT);
  453. iput(inode);
  454. }
  455. void
  456. xfs_flush_inodes(
  457. xfs_inode_t *ip)
  458. {
  459. struct inode *inode = VFS_I(ip);
  460. DECLARE_COMPLETION_ONSTACK(completion);
  461. igrab(inode);
  462. xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_inodes_work, &completion);
  463. wait_for_completion(&completion);
  464. xfs_log_force(ip->i_mount, XFS_LOG_SYNC);
  465. }
  466. /*
  467. * Every sync period we need to unpin all items, reclaim inodes and sync
  468. * disk quotas. We might need to cover the log to indicate that the
  469. * filesystem is idle and not frozen.
  470. */
  471. STATIC void
  472. xfs_sync_worker(
  473. struct xfs_mount *mp,
  474. void *unused)
  475. {
  476. int error;
  477. if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
  478. xfs_log_force(mp, 0);
  479. xfs_reclaim_inodes(mp, 0);
  480. /* dgc: errors ignored here */
  481. error = xfs_qm_sync(mp, SYNC_TRYLOCK);
  482. if (mp->m_super->s_frozen == SB_UNFROZEN &&
  483. xfs_log_need_covered(mp))
  484. error = xfs_fs_log_dummy(mp, 0);
  485. }
  486. mp->m_sync_seq++;
  487. wake_up(&mp->m_wait_single_sync_task);
  488. }
  489. STATIC int
  490. xfssyncd(
  491. void *arg)
  492. {
  493. struct xfs_mount *mp = arg;
  494. long timeleft;
  495. xfs_sync_work_t *work, *n;
  496. LIST_HEAD (tmp);
  497. set_freezable();
  498. timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10);
  499. for (;;) {
  500. if (list_empty(&mp->m_sync_list))
  501. timeleft = schedule_timeout_interruptible(timeleft);
  502. /* swsusp */
  503. try_to_freeze();
  504. if (kthread_should_stop() && list_empty(&mp->m_sync_list))
  505. break;
  506. spin_lock(&mp->m_sync_lock);
  507. /*
  508. * We can get woken by laptop mode, to do a sync -
  509. * that's the (only!) case where the list would be
  510. * empty with time remaining.
  511. */
  512. if (!timeleft || list_empty(&mp->m_sync_list)) {
  513. if (!timeleft)
  514. timeleft = xfs_syncd_centisecs *
  515. msecs_to_jiffies(10);
  516. INIT_LIST_HEAD(&mp->m_sync_work.w_list);
  517. list_add_tail(&mp->m_sync_work.w_list,
  518. &mp->m_sync_list);
  519. }
  520. list_splice_init(&mp->m_sync_list, &tmp);
  521. spin_unlock(&mp->m_sync_lock);
  522. list_for_each_entry_safe(work, n, &tmp, w_list) {
  523. (*work->w_syncer)(mp, work->w_data);
  524. list_del(&work->w_list);
  525. if (work == &mp->m_sync_work)
  526. continue;
  527. if (work->w_completion)
  528. complete(work->w_completion);
  529. kmem_free(work);
  530. }
  531. }
  532. return 0;
  533. }
  534. int
  535. xfs_syncd_init(
  536. struct xfs_mount *mp)
  537. {
  538. mp->m_sync_work.w_syncer = xfs_sync_worker;
  539. mp->m_sync_work.w_mount = mp;
  540. mp->m_sync_work.w_completion = NULL;
  541. mp->m_sync_task = kthread_run(xfssyncd, mp, "xfssyncd/%s", mp->m_fsname);
  542. if (IS_ERR(mp->m_sync_task))
  543. return -PTR_ERR(mp->m_sync_task);
  544. return 0;
  545. }
  546. void
  547. xfs_syncd_stop(
  548. struct xfs_mount *mp)
  549. {
  550. kthread_stop(mp->m_sync_task);
  551. }
  552. void
  553. __xfs_inode_set_reclaim_tag(
  554. struct xfs_perag *pag,
  555. struct xfs_inode *ip)
  556. {
  557. radix_tree_tag_set(&pag->pag_ici_root,
  558. XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
  559. XFS_ICI_RECLAIM_TAG);
  560. if (!pag->pag_ici_reclaimable) {
  561. /* propagate the reclaim tag up into the perag radix tree */
  562. spin_lock(&ip->i_mount->m_perag_lock);
  563. radix_tree_tag_set(&ip->i_mount->m_perag_tree,
  564. XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
  565. XFS_ICI_RECLAIM_TAG);
  566. spin_unlock(&ip->i_mount->m_perag_lock);
  567. trace_xfs_perag_set_reclaim(ip->i_mount, pag->pag_agno,
  568. -1, _RET_IP_);
  569. }
  570. pag->pag_ici_reclaimable++;
  571. }
  572. /*
  573. * We set the inode flag atomically with the radix tree tag.
  574. * Once we get tag lookups on the radix tree, this inode flag
  575. * can go away.
  576. */
  577. void
  578. xfs_inode_set_reclaim_tag(
  579. xfs_inode_t *ip)
  580. {
  581. struct xfs_mount *mp = ip->i_mount;
  582. struct xfs_perag *pag;
  583. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
  584. write_lock(&pag->pag_ici_lock);
  585. spin_lock(&ip->i_flags_lock);
  586. __xfs_inode_set_reclaim_tag(pag, ip);
  587. __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
  588. spin_unlock(&ip->i_flags_lock);
  589. write_unlock(&pag->pag_ici_lock);
  590. xfs_perag_put(pag);
  591. }
  592. void
  593. __xfs_inode_clear_reclaim_tag(
  594. xfs_mount_t *mp,
  595. xfs_perag_t *pag,
  596. xfs_inode_t *ip)
  597. {
  598. radix_tree_tag_clear(&pag->pag_ici_root,
  599. XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
  600. pag->pag_ici_reclaimable--;
  601. if (!pag->pag_ici_reclaimable) {
  602. /* clear the reclaim tag from the perag radix tree */
  603. spin_lock(&ip->i_mount->m_perag_lock);
  604. radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
  605. XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
  606. XFS_ICI_RECLAIM_TAG);
  607. spin_unlock(&ip->i_mount->m_perag_lock);
  608. trace_xfs_perag_clear_reclaim(ip->i_mount, pag->pag_agno,
  609. -1, _RET_IP_);
  610. }
  611. }
  612. /*
  613. * Inodes in different states need to be treated differently, and the return
  614. * value of xfs_iflush is not sufficient to get this right. The following table
  615. * lists the inode states and the reclaim actions necessary for non-blocking
  616. * reclaim:
  617. *
  618. *
  619. * inode state iflush ret required action
  620. * --------------- ---------- ---------------
  621. * bad - reclaim
  622. * shutdown EIO unpin and reclaim
  623. * clean, unpinned 0 reclaim
  624. * stale, unpinned 0 reclaim
  625. * clean, pinned(*) 0 requeue
  626. * stale, pinned EAGAIN requeue
  627. * dirty, delwri ok 0 requeue
  628. * dirty, delwri blocked EAGAIN requeue
  629. * dirty, sync flush 0 reclaim
  630. *
  631. * (*) dgc: I don't think the clean, pinned state is possible but it gets
  632. * handled anyway given the order of checks implemented.
  633. *
  634. * As can be seen from the table, the return value of xfs_iflush() is not
  635. * sufficient to correctly decide the reclaim action here. The checks in
  636. * xfs_iflush() might look like duplicates, but they are not.
  637. *
  638. * Also, because we get the flush lock first, we know that any inode that has
  639. * been flushed delwri has had the flush completed by the time we check that
  640. * the inode is clean. The clean inode check needs to be done before flushing
  641. * the inode delwri otherwise we would loop forever requeuing clean inodes as
  642. * we cannot tell apart a successful delwri flush and a clean inode from the
  643. * return value of xfs_iflush().
  644. *
  645. * Note that because the inode is flushed delayed write by background
  646. * writeback, the flush lock may already be held here and waiting on it can
  647. * result in very long latencies. Hence for sync reclaims, where we wait on the
  648. * flush lock, the caller should push out delayed write inodes first before
  649. * trying to reclaim them to minimise the amount of time spent waiting. For
  650. * background relaim, we just requeue the inode for the next pass.
  651. *
  652. * Hence the order of actions after gaining the locks should be:
  653. * bad => reclaim
  654. * shutdown => unpin and reclaim
  655. * pinned, delwri => requeue
  656. * pinned, sync => unpin
  657. * stale => reclaim
  658. * clean => reclaim
  659. * dirty, delwri => flush and requeue
  660. * dirty, sync => flush, wait and reclaim
  661. */
  662. STATIC int
  663. xfs_reclaim_inode(
  664. struct xfs_inode *ip,
  665. struct xfs_perag *pag,
  666. int sync_mode)
  667. {
  668. int error = 0;
  669. /*
  670. * The radix tree lock here protects a thread in xfs_iget from racing
  671. * with us starting reclaim on the inode. Once we have the
  672. * XFS_IRECLAIM flag set it will not touch us.
  673. */
  674. spin_lock(&ip->i_flags_lock);
  675. ASSERT_ALWAYS(__xfs_iflags_test(ip, XFS_IRECLAIMABLE));
  676. if (__xfs_iflags_test(ip, XFS_IRECLAIM)) {
  677. /* ignore as it is already under reclaim */
  678. spin_unlock(&ip->i_flags_lock);
  679. write_unlock(&pag->pag_ici_lock);
  680. return 0;
  681. }
  682. __xfs_iflags_set(ip, XFS_IRECLAIM);
  683. spin_unlock(&ip->i_flags_lock);
  684. write_unlock(&pag->pag_ici_lock);
  685. xfs_ilock(ip, XFS_ILOCK_EXCL);
  686. if (!xfs_iflock_nowait(ip)) {
  687. if (!(sync_mode & SYNC_WAIT))
  688. goto out;
  689. xfs_iflock(ip);
  690. }
  691. if (is_bad_inode(VFS_I(ip)))
  692. goto reclaim;
  693. if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  694. xfs_iunpin_wait(ip);
  695. goto reclaim;
  696. }
  697. if (xfs_ipincount(ip)) {
  698. if (!(sync_mode & SYNC_WAIT)) {
  699. xfs_ifunlock(ip);
  700. goto out;
  701. }
  702. xfs_iunpin_wait(ip);
  703. }
  704. if (xfs_iflags_test(ip, XFS_ISTALE))
  705. goto reclaim;
  706. if (xfs_inode_clean(ip))
  707. goto reclaim;
  708. /* Now we have an inode that needs flushing */
  709. error = xfs_iflush(ip, sync_mode);
  710. if (sync_mode & SYNC_WAIT) {
  711. xfs_iflock(ip);
  712. goto reclaim;
  713. }
  714. /*
  715. * When we have to flush an inode but don't have SYNC_WAIT set, we
  716. * flush the inode out using a delwri buffer and wait for the next
  717. * call into reclaim to find it in a clean state instead of waiting for
  718. * it now. We also don't return errors here - if the error is transient
  719. * then the next reclaim pass will flush the inode, and if the error
  720. * is permanent then the next sync reclaim will reclaim the inode and
  721. * pass on the error.
  722. */
  723. if (error && error != EAGAIN && !XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  724. xfs_fs_cmn_err(CE_WARN, ip->i_mount,
  725. "inode 0x%llx background reclaim flush failed with %d",
  726. (long long)ip->i_ino, error);
  727. }
  728. out:
  729. xfs_iflags_clear(ip, XFS_IRECLAIM);
  730. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  731. /*
  732. * We could return EAGAIN here to make reclaim rescan the inode tree in
  733. * a short while. However, this just burns CPU time scanning the tree
  734. * waiting for IO to complete and xfssyncd never goes back to the idle
  735. * state. Instead, return 0 to let the next scheduled background reclaim
  736. * attempt to reclaim the inode again.
  737. */
  738. return 0;
  739. reclaim:
  740. xfs_ifunlock(ip);
  741. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  742. XFS_STATS_INC(xs_ig_reclaims);
  743. /*
  744. * Remove the inode from the per-AG radix tree.
  745. *
  746. * Because radix_tree_delete won't complain even if the item was never
  747. * added to the tree assert that it's been there before to catch
  748. * problems with the inode life time early on.
  749. */
  750. write_lock(&pag->pag_ici_lock);
  751. if (!radix_tree_delete(&pag->pag_ici_root,
  752. XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino)))
  753. ASSERT(0);
  754. write_unlock(&pag->pag_ici_lock);
  755. /*
  756. * Here we do an (almost) spurious inode lock in order to coordinate
  757. * with inode cache radix tree lookups. This is because the lookup
  758. * can reference the inodes in the cache without taking references.
  759. *
  760. * We make that OK here by ensuring that we wait until the inode is
  761. * unlocked after the lookup before we go ahead and free it. We get
  762. * both the ilock and the iolock because the code may need to drop the
  763. * ilock one but will still hold the iolock.
  764. */
  765. xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  766. xfs_qm_dqdetach(ip);
  767. xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  768. xfs_inode_free(ip);
  769. return error;
  770. }
  771. int
  772. xfs_reclaim_inodes(
  773. xfs_mount_t *mp,
  774. int mode)
  775. {
  776. return xfs_inode_ag_iterator(mp, xfs_reclaim_inode, mode,
  777. XFS_ICI_RECLAIM_TAG, 1, NULL);
  778. }
  779. /*
  780. * Shrinker infrastructure.
  781. */
  782. static int
  783. xfs_reclaim_inode_shrink(
  784. struct shrinker *shrink,
  785. int nr_to_scan,
  786. gfp_t gfp_mask)
  787. {
  788. struct xfs_mount *mp;
  789. struct xfs_perag *pag;
  790. xfs_agnumber_t ag;
  791. int reclaimable;
  792. mp = container_of(shrink, struct xfs_mount, m_inode_shrink);
  793. if (nr_to_scan) {
  794. if (!(gfp_mask & __GFP_FS))
  795. return -1;
  796. xfs_inode_ag_iterator(mp, xfs_reclaim_inode, 0,
  797. XFS_ICI_RECLAIM_TAG, 1, &nr_to_scan);
  798. /* if we don't exhaust the scan, don't bother coming back */
  799. if (nr_to_scan > 0)
  800. return -1;
  801. }
  802. reclaimable = 0;
  803. ag = 0;
  804. while ((pag = xfs_inode_ag_iter_next_pag(mp, &ag,
  805. XFS_ICI_RECLAIM_TAG))) {
  806. reclaimable += pag->pag_ici_reclaimable;
  807. xfs_perag_put(pag);
  808. }
  809. return reclaimable;
  810. }
  811. void
  812. xfs_inode_shrinker_register(
  813. struct xfs_mount *mp)
  814. {
  815. mp->m_inode_shrink.shrink = xfs_reclaim_inode_shrink;
  816. mp->m_inode_shrink.seeks = DEFAULT_SEEKS;
  817. register_shrinker(&mp->m_inode_shrink);
  818. }
  819. void
  820. xfs_inode_shrinker_unregister(
  821. struct xfs_mount *mp)
  822. {
  823. unregister_shrinker(&mp->m_inode_shrink);
  824. }