xfs_sync.c 22 KB

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