xfs_sync.c 26 KB

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