xfs_sync.c 23 KB

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