xfs_sync.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  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. STATIC 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. STATIC 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);
  321. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  322. error = xfs_trans_commit(tp, 0);
  323. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  324. /* the log force ensures this transaction is pushed to disk */
  325. xfs_log_force(mp, (flags & SYNC_WAIT) ? XFS_LOG_SYNC : 0);
  326. return error;
  327. }
  328. STATIC int
  329. xfs_sync_fsdata(
  330. struct xfs_mount *mp)
  331. {
  332. struct xfs_buf *bp;
  333. /*
  334. * If the buffer is pinned then push on the log so we won't get stuck
  335. * waiting in the write for someone, maybe ourselves, to flush the log.
  336. *
  337. * Even though we just pushed the log above, we did not have the
  338. * superblock buffer locked at that point so it can become pinned in
  339. * between there and here.
  340. */
  341. bp = xfs_getsb(mp, 0);
  342. if (XFS_BUF_ISPINNED(bp))
  343. xfs_log_force(mp, 0);
  344. return xfs_bwrite(mp, bp);
  345. }
  346. /*
  347. * When remounting a filesystem read-only or freezing the filesystem, we have
  348. * two phases to execute. This first phase is syncing the data before we
  349. * quiesce the filesystem, and the second is flushing all the inodes out after
  350. * we've waited for all the transactions created by the first phase to
  351. * complete. The second phase ensures that the inodes are written to their
  352. * location on disk rather than just existing in transactions in the log. This
  353. * means after a quiesce there is no log replay required to write the inodes to
  354. * disk (this is the main difference between a sync and a quiesce).
  355. */
  356. /*
  357. * First stage of freeze - no writers will make progress now we are here,
  358. * so we flush delwri and delalloc buffers here, then wait for all I/O to
  359. * complete. Data is frozen at that point. Metadata is not frozen,
  360. * transactions can still occur here so don't bother flushing the buftarg
  361. * because it'll just get dirty again.
  362. */
  363. int
  364. xfs_quiesce_data(
  365. struct xfs_mount *mp)
  366. {
  367. int error, error2 = 0;
  368. /* push non-blocking */
  369. xfs_sync_data(mp, 0);
  370. xfs_qm_sync(mp, SYNC_TRYLOCK);
  371. /* push and block till complete */
  372. xfs_sync_data(mp, SYNC_WAIT);
  373. xfs_qm_sync(mp, SYNC_WAIT);
  374. /* write superblock and hoover up shutdown errors */
  375. error = xfs_sync_fsdata(mp);
  376. /* make sure all delwri buffers are written out */
  377. xfs_flush_buftarg(mp->m_ddev_targp, 1);
  378. /* mark the log as covered if needed */
  379. if (xfs_log_need_covered(mp))
  380. error2 = xfs_commit_dummy_trans(mp, SYNC_WAIT);
  381. /* flush data-only devices */
  382. if (mp->m_rtdev_targp)
  383. XFS_bflush(mp->m_rtdev_targp);
  384. return error ? error : error2;
  385. }
  386. STATIC void
  387. xfs_quiesce_fs(
  388. struct xfs_mount *mp)
  389. {
  390. int count = 0, pincount;
  391. xfs_reclaim_inodes(mp, 0);
  392. xfs_flush_buftarg(mp->m_ddev_targp, 0);
  393. /*
  394. * This loop must run at least twice. The first instance of the loop
  395. * will flush most meta data but that will generate more meta data
  396. * (typically directory updates). Which then must be flushed and
  397. * logged before we can write the unmount record. We also so sync
  398. * reclaim of inodes to catch any that the above delwri flush skipped.
  399. */
  400. do {
  401. xfs_reclaim_inodes(mp, SYNC_WAIT);
  402. xfs_sync_attr(mp, SYNC_WAIT);
  403. pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1);
  404. if (!pincount) {
  405. delay(50);
  406. count++;
  407. }
  408. } while (count < 2);
  409. }
  410. /*
  411. * Second stage of a quiesce. The data is already synced, now we have to take
  412. * care of the metadata. New transactions are already blocked, so we need to
  413. * wait for any remaining transactions to drain out before proceding.
  414. */
  415. void
  416. xfs_quiesce_attr(
  417. struct xfs_mount *mp)
  418. {
  419. int error = 0;
  420. /* wait for all modifications to complete */
  421. while (atomic_read(&mp->m_active_trans) > 0)
  422. delay(100);
  423. /* flush inodes and push all remaining buffers out to disk */
  424. xfs_quiesce_fs(mp);
  425. /*
  426. * Just warn here till VFS can correctly support
  427. * read-only remount without racing.
  428. */
  429. WARN_ON(atomic_read(&mp->m_active_trans) != 0);
  430. /* Push the superblock and write an unmount record */
  431. error = xfs_log_sbcount(mp, 1);
  432. if (error)
  433. xfs_fs_cmn_err(CE_WARN, mp,
  434. "xfs_attr_quiesce: failed to log sb changes. "
  435. "Frozen image may not be consistent.");
  436. xfs_log_unmount_write(mp);
  437. xfs_unmountfs_writesb(mp);
  438. }
  439. /*
  440. * Enqueue a work item to be picked up by the vfs xfssyncd thread.
  441. * Doing this has two advantages:
  442. * - It saves on stack space, which is tight in certain situations
  443. * - It can be used (with care) as a mechanism to avoid deadlocks.
  444. * Flushing while allocating in a full filesystem requires both.
  445. */
  446. STATIC void
  447. xfs_syncd_queue_work(
  448. struct xfs_mount *mp,
  449. void *data,
  450. void (*syncer)(struct xfs_mount *, void *),
  451. struct completion *completion)
  452. {
  453. struct xfs_sync_work *work;
  454. work = kmem_alloc(sizeof(struct xfs_sync_work), KM_SLEEP);
  455. INIT_LIST_HEAD(&work->w_list);
  456. work->w_syncer = syncer;
  457. work->w_data = data;
  458. work->w_mount = mp;
  459. work->w_completion = completion;
  460. spin_lock(&mp->m_sync_lock);
  461. list_add_tail(&work->w_list, &mp->m_sync_list);
  462. spin_unlock(&mp->m_sync_lock);
  463. wake_up_process(mp->m_sync_task);
  464. }
  465. /*
  466. * Flush delayed allocate data, attempting to free up reserved space
  467. * from existing allocations. At this point a new allocation attempt
  468. * has failed with ENOSPC and we are in the process of scratching our
  469. * heads, looking about for more room...
  470. */
  471. STATIC void
  472. xfs_flush_inodes_work(
  473. struct xfs_mount *mp,
  474. void *arg)
  475. {
  476. struct inode *inode = arg;
  477. xfs_sync_data(mp, SYNC_TRYLOCK);
  478. xfs_sync_data(mp, SYNC_TRYLOCK | SYNC_WAIT);
  479. iput(inode);
  480. }
  481. void
  482. xfs_flush_inodes(
  483. xfs_inode_t *ip)
  484. {
  485. struct inode *inode = VFS_I(ip);
  486. DECLARE_COMPLETION_ONSTACK(completion);
  487. igrab(inode);
  488. xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_inodes_work, &completion);
  489. wait_for_completion(&completion);
  490. xfs_log_force(ip->i_mount, XFS_LOG_SYNC);
  491. }
  492. /*
  493. * Every sync period we need to unpin all items, reclaim inodes and sync
  494. * disk quotas. We might need to cover the log to indicate that the
  495. * filesystem is idle.
  496. */
  497. STATIC void
  498. xfs_sync_worker(
  499. struct xfs_mount *mp,
  500. void *unused)
  501. {
  502. int error;
  503. if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
  504. xfs_log_force(mp, 0);
  505. xfs_reclaim_inodes(mp, 0);
  506. /* dgc: errors ignored here */
  507. error = xfs_qm_sync(mp, SYNC_TRYLOCK);
  508. if (xfs_log_need_covered(mp))
  509. error = xfs_commit_dummy_trans(mp, 0);
  510. }
  511. mp->m_sync_seq++;
  512. wake_up(&mp->m_wait_single_sync_task);
  513. }
  514. STATIC int
  515. xfssyncd(
  516. void *arg)
  517. {
  518. struct xfs_mount *mp = arg;
  519. long timeleft;
  520. xfs_sync_work_t *work, *n;
  521. LIST_HEAD (tmp);
  522. set_freezable();
  523. timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10);
  524. for (;;) {
  525. if (list_empty(&mp->m_sync_list))
  526. timeleft = schedule_timeout_interruptible(timeleft);
  527. /* swsusp */
  528. try_to_freeze();
  529. if (kthread_should_stop() && list_empty(&mp->m_sync_list))
  530. break;
  531. spin_lock(&mp->m_sync_lock);
  532. /*
  533. * We can get woken by laptop mode, to do a sync -
  534. * that's the (only!) case where the list would be
  535. * empty with time remaining.
  536. */
  537. if (!timeleft || list_empty(&mp->m_sync_list)) {
  538. if (!timeleft)
  539. timeleft = xfs_syncd_centisecs *
  540. msecs_to_jiffies(10);
  541. INIT_LIST_HEAD(&mp->m_sync_work.w_list);
  542. list_add_tail(&mp->m_sync_work.w_list,
  543. &mp->m_sync_list);
  544. }
  545. list_splice_init(&mp->m_sync_list, &tmp);
  546. spin_unlock(&mp->m_sync_lock);
  547. list_for_each_entry_safe(work, n, &tmp, w_list) {
  548. (*work->w_syncer)(mp, work->w_data);
  549. list_del(&work->w_list);
  550. if (work == &mp->m_sync_work)
  551. continue;
  552. if (work->w_completion)
  553. complete(work->w_completion);
  554. kmem_free(work);
  555. }
  556. }
  557. return 0;
  558. }
  559. int
  560. xfs_syncd_init(
  561. struct xfs_mount *mp)
  562. {
  563. mp->m_sync_work.w_syncer = xfs_sync_worker;
  564. mp->m_sync_work.w_mount = mp;
  565. mp->m_sync_work.w_completion = NULL;
  566. mp->m_sync_task = kthread_run(xfssyncd, mp, "xfssyncd/%s", mp->m_fsname);
  567. if (IS_ERR(mp->m_sync_task))
  568. return -PTR_ERR(mp->m_sync_task);
  569. return 0;
  570. }
  571. void
  572. xfs_syncd_stop(
  573. struct xfs_mount *mp)
  574. {
  575. kthread_stop(mp->m_sync_task);
  576. }
  577. void
  578. __xfs_inode_set_reclaim_tag(
  579. struct xfs_perag *pag,
  580. struct xfs_inode *ip)
  581. {
  582. radix_tree_tag_set(&pag->pag_ici_root,
  583. XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
  584. XFS_ICI_RECLAIM_TAG);
  585. if (!pag->pag_ici_reclaimable) {
  586. /* propagate the reclaim tag up into the perag radix tree */
  587. spin_lock(&ip->i_mount->m_perag_lock);
  588. radix_tree_tag_set(&ip->i_mount->m_perag_tree,
  589. XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
  590. XFS_ICI_RECLAIM_TAG);
  591. spin_unlock(&ip->i_mount->m_perag_lock);
  592. trace_xfs_perag_set_reclaim(ip->i_mount, pag->pag_agno,
  593. -1, _RET_IP_);
  594. }
  595. pag->pag_ici_reclaimable++;
  596. }
  597. /*
  598. * We set the inode flag atomically with the radix tree tag.
  599. * Once we get tag lookups on the radix tree, this inode flag
  600. * can go away.
  601. */
  602. void
  603. xfs_inode_set_reclaim_tag(
  604. xfs_inode_t *ip)
  605. {
  606. struct xfs_mount *mp = ip->i_mount;
  607. struct xfs_perag *pag;
  608. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
  609. write_lock(&pag->pag_ici_lock);
  610. spin_lock(&ip->i_flags_lock);
  611. __xfs_inode_set_reclaim_tag(pag, ip);
  612. __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
  613. spin_unlock(&ip->i_flags_lock);
  614. write_unlock(&pag->pag_ici_lock);
  615. xfs_perag_put(pag);
  616. }
  617. void
  618. __xfs_inode_clear_reclaim_tag(
  619. xfs_mount_t *mp,
  620. xfs_perag_t *pag,
  621. xfs_inode_t *ip)
  622. {
  623. radix_tree_tag_clear(&pag->pag_ici_root,
  624. XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
  625. pag->pag_ici_reclaimable--;
  626. if (!pag->pag_ici_reclaimable) {
  627. /* clear the reclaim tag from the perag radix tree */
  628. spin_lock(&ip->i_mount->m_perag_lock);
  629. radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
  630. XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
  631. XFS_ICI_RECLAIM_TAG);
  632. spin_unlock(&ip->i_mount->m_perag_lock);
  633. trace_xfs_perag_clear_reclaim(ip->i_mount, pag->pag_agno,
  634. -1, _RET_IP_);
  635. }
  636. }
  637. /*
  638. * Inodes in different states need to be treated differently, and the return
  639. * value of xfs_iflush is not sufficient to get this right. The following table
  640. * lists the inode states and the reclaim actions necessary for non-blocking
  641. * reclaim:
  642. *
  643. *
  644. * inode state iflush ret required action
  645. * --------------- ---------- ---------------
  646. * bad - reclaim
  647. * shutdown EIO unpin and reclaim
  648. * clean, unpinned 0 reclaim
  649. * stale, unpinned 0 reclaim
  650. * clean, pinned(*) 0 requeue
  651. * stale, pinned EAGAIN requeue
  652. * dirty, delwri ok 0 requeue
  653. * dirty, delwri blocked EAGAIN requeue
  654. * dirty, sync flush 0 reclaim
  655. *
  656. * (*) dgc: I don't think the clean, pinned state is possible but it gets
  657. * handled anyway given the order of checks implemented.
  658. *
  659. * As can be seen from the table, the return value of xfs_iflush() is not
  660. * sufficient to correctly decide the reclaim action here. The checks in
  661. * xfs_iflush() might look like duplicates, but they are not.
  662. *
  663. * Also, because we get the flush lock first, we know that any inode that has
  664. * been flushed delwri has had the flush completed by the time we check that
  665. * the inode is clean. The clean inode check needs to be done before flushing
  666. * the inode delwri otherwise we would loop forever requeuing clean inodes as
  667. * we cannot tell apart a successful delwri flush and a clean inode from the
  668. * return value of xfs_iflush().
  669. *
  670. * Note that because the inode is flushed delayed write by background
  671. * writeback, the flush lock may already be held here and waiting on it can
  672. * result in very long latencies. Hence for sync reclaims, where we wait on the
  673. * flush lock, the caller should push out delayed write inodes first before
  674. * trying to reclaim them to minimise the amount of time spent waiting. For
  675. * background relaim, we just requeue the inode for the next pass.
  676. *
  677. * Hence the order of actions after gaining the locks should be:
  678. * bad => reclaim
  679. * shutdown => unpin and reclaim
  680. * pinned, delwri => requeue
  681. * pinned, sync => unpin
  682. * stale => reclaim
  683. * clean => reclaim
  684. * dirty, delwri => flush and requeue
  685. * dirty, sync => flush, wait and reclaim
  686. */
  687. STATIC int
  688. xfs_reclaim_inode(
  689. struct xfs_inode *ip,
  690. struct xfs_perag *pag,
  691. int sync_mode)
  692. {
  693. int error = 0;
  694. /*
  695. * The radix tree lock here protects a thread in xfs_iget from racing
  696. * with us starting reclaim on the inode. Once we have the
  697. * XFS_IRECLAIM flag set it will not touch us.
  698. */
  699. spin_lock(&ip->i_flags_lock);
  700. ASSERT_ALWAYS(__xfs_iflags_test(ip, XFS_IRECLAIMABLE));
  701. if (__xfs_iflags_test(ip, XFS_IRECLAIM)) {
  702. /* ignore as it is already under reclaim */
  703. spin_unlock(&ip->i_flags_lock);
  704. write_unlock(&pag->pag_ici_lock);
  705. return 0;
  706. }
  707. __xfs_iflags_set(ip, XFS_IRECLAIM);
  708. spin_unlock(&ip->i_flags_lock);
  709. write_unlock(&pag->pag_ici_lock);
  710. xfs_ilock(ip, XFS_ILOCK_EXCL);
  711. if (!xfs_iflock_nowait(ip)) {
  712. if (!(sync_mode & SYNC_WAIT))
  713. goto out;
  714. xfs_iflock(ip);
  715. }
  716. if (is_bad_inode(VFS_I(ip)))
  717. goto reclaim;
  718. if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  719. xfs_iunpin_wait(ip);
  720. goto reclaim;
  721. }
  722. if (xfs_ipincount(ip)) {
  723. if (!(sync_mode & SYNC_WAIT)) {
  724. xfs_ifunlock(ip);
  725. goto out;
  726. }
  727. xfs_iunpin_wait(ip);
  728. }
  729. if (xfs_iflags_test(ip, XFS_ISTALE))
  730. goto reclaim;
  731. if (xfs_inode_clean(ip))
  732. goto reclaim;
  733. /* Now we have an inode that needs flushing */
  734. error = xfs_iflush(ip, sync_mode);
  735. if (sync_mode & SYNC_WAIT) {
  736. xfs_iflock(ip);
  737. goto reclaim;
  738. }
  739. /*
  740. * When we have to flush an inode but don't have SYNC_WAIT set, we
  741. * flush the inode out using a delwri buffer and wait for the next
  742. * call into reclaim to find it in a clean state instead of waiting for
  743. * it now. We also don't return errors here - if the error is transient
  744. * then the next reclaim pass will flush the inode, and if the error
  745. * is permanent then the next sync reclaim will reclaim the inode and
  746. * pass on the error.
  747. */
  748. if (error && error != EAGAIN && !XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  749. xfs_fs_cmn_err(CE_WARN, ip->i_mount,
  750. "inode 0x%llx background reclaim flush failed with %d",
  751. (long long)ip->i_ino, error);
  752. }
  753. out:
  754. xfs_iflags_clear(ip, XFS_IRECLAIM);
  755. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  756. /*
  757. * We could return EAGAIN here to make reclaim rescan the inode tree in
  758. * a short while. However, this just burns CPU time scanning the tree
  759. * waiting for IO to complete and xfssyncd never goes back to the idle
  760. * state. Instead, return 0 to let the next scheduled background reclaim
  761. * attempt to reclaim the inode again.
  762. */
  763. return 0;
  764. reclaim:
  765. xfs_ifunlock(ip);
  766. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  767. XFS_STATS_INC(xs_ig_reclaims);
  768. /*
  769. * Remove the inode from the per-AG radix tree.
  770. *
  771. * Because radix_tree_delete won't complain even if the item was never
  772. * added to the tree assert that it's been there before to catch
  773. * problems with the inode life time early on.
  774. */
  775. write_lock(&pag->pag_ici_lock);
  776. if (!radix_tree_delete(&pag->pag_ici_root,
  777. XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino)))
  778. ASSERT(0);
  779. write_unlock(&pag->pag_ici_lock);
  780. /*
  781. * Here we do an (almost) spurious inode lock in order to coordinate
  782. * with inode cache radix tree lookups. This is because the lookup
  783. * can reference the inodes in the cache without taking references.
  784. *
  785. * We make that OK here by ensuring that we wait until the inode is
  786. * unlocked after the lookup before we go ahead and free it. We get
  787. * both the ilock and the iolock because the code may need to drop the
  788. * ilock one but will still hold the iolock.
  789. */
  790. xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  791. xfs_qm_dqdetach(ip);
  792. xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  793. xfs_inode_free(ip);
  794. return error;
  795. }
  796. int
  797. xfs_reclaim_inodes(
  798. xfs_mount_t *mp,
  799. int mode)
  800. {
  801. return xfs_inode_ag_iterator(mp, xfs_reclaim_inode, mode,
  802. XFS_ICI_RECLAIM_TAG, 1, NULL);
  803. }
  804. /*
  805. * Shrinker infrastructure.
  806. */
  807. static int
  808. xfs_reclaim_inode_shrink(
  809. struct shrinker *shrink,
  810. int nr_to_scan,
  811. gfp_t gfp_mask)
  812. {
  813. struct xfs_mount *mp;
  814. struct xfs_perag *pag;
  815. xfs_agnumber_t ag;
  816. int reclaimable;
  817. mp = container_of(shrink, struct xfs_mount, m_inode_shrink);
  818. if (nr_to_scan) {
  819. if (!(gfp_mask & __GFP_FS))
  820. return -1;
  821. xfs_inode_ag_iterator(mp, xfs_reclaim_inode, 0,
  822. XFS_ICI_RECLAIM_TAG, 1, &nr_to_scan);
  823. /* if we don't exhaust the scan, don't bother coming back */
  824. if (nr_to_scan > 0)
  825. return -1;
  826. }
  827. reclaimable = 0;
  828. ag = 0;
  829. while ((pag = xfs_inode_ag_iter_next_pag(mp, &ag,
  830. XFS_ICI_RECLAIM_TAG))) {
  831. reclaimable += pag->pag_ici_reclaimable;
  832. xfs_perag_put(pag);
  833. }
  834. return reclaimable;
  835. }
  836. void
  837. xfs_inode_shrinker_register(
  838. struct xfs_mount *mp)
  839. {
  840. mp->m_inode_shrink.shrink = xfs_reclaim_inode_shrink;
  841. mp->m_inode_shrink.seeks = DEFAULT_SEEKS;
  842. register_shrinker(&mp->m_inode_shrink);
  843. }
  844. void
  845. xfs_inode_shrinker_unregister(
  846. struct xfs_mount *mp)
  847. {
  848. unregister_shrinker(&mp->m_inode_shrink);
  849. }