xfs_sync.c 25 KB

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