xfs_sync.c 23 KB

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