xfs_sync.c 23 KB

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