xfs_sync.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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. error = xfs_qm_sync(mp, SYNC_TRYLOCK);
  402. }
  403. /* queue us up again */
  404. xfs_syncd_queue_sync(mp);
  405. }
  406. /*
  407. * Queue a new inode reclaim pass if there are reclaimable inodes and there
  408. * isn't a reclaim pass already in progress. By default it runs every 5s based
  409. * on the xfs syncd work default of 30s. Perhaps this should have it's own
  410. * tunable, but that can be done if this method proves to be ineffective or too
  411. * aggressive.
  412. */
  413. static void
  414. xfs_syncd_queue_reclaim(
  415. struct xfs_mount *mp)
  416. {
  417. /*
  418. * We can have inodes enter reclaim after we've shut down the syncd
  419. * workqueue during unmount, so don't allow reclaim work to be queued
  420. * during unmount.
  421. */
  422. if (!(mp->m_super->s_flags & MS_ACTIVE))
  423. return;
  424. rcu_read_lock();
  425. if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_RECLAIM_TAG)) {
  426. queue_delayed_work(xfs_syncd_wq, &mp->m_reclaim_work,
  427. msecs_to_jiffies(xfs_syncd_centisecs / 6 * 10));
  428. }
  429. rcu_read_unlock();
  430. }
  431. /*
  432. * This is a fast pass over the inode cache to try to get reclaim moving on as
  433. * many inodes as possible in a short period of time. It kicks itself every few
  434. * seconds, as well as being kicked by the inode cache shrinker when memory
  435. * goes low. It scans as quickly as possible avoiding locked inodes or those
  436. * already being flushed, and once done schedules a future pass.
  437. */
  438. STATIC void
  439. xfs_reclaim_worker(
  440. struct work_struct *work)
  441. {
  442. struct xfs_mount *mp = container_of(to_delayed_work(work),
  443. struct xfs_mount, m_reclaim_work);
  444. xfs_reclaim_inodes(mp, SYNC_TRYLOCK);
  445. xfs_syncd_queue_reclaim(mp);
  446. }
  447. /*
  448. * Flush delayed allocate data, attempting to free up reserved space
  449. * from existing allocations. At this point a new allocation attempt
  450. * has failed with ENOSPC and we are in the process of scratching our
  451. * heads, looking about for more room.
  452. *
  453. * Queue a new data flush if there isn't one already in progress and
  454. * wait for completion of the flush. This means that we only ever have one
  455. * inode flush in progress no matter how many ENOSPC events are occurring and
  456. * so will prevent the system from bogging down due to every concurrent
  457. * ENOSPC event scanning all the active inodes in the system for writeback.
  458. */
  459. void
  460. xfs_flush_inodes(
  461. struct xfs_inode *ip)
  462. {
  463. struct xfs_mount *mp = ip->i_mount;
  464. queue_work(xfs_syncd_wq, &mp->m_flush_work);
  465. flush_work_sync(&mp->m_flush_work);
  466. }
  467. STATIC void
  468. xfs_flush_worker(
  469. struct work_struct *work)
  470. {
  471. struct xfs_mount *mp = container_of(work,
  472. struct xfs_mount, m_flush_work);
  473. xfs_sync_data(mp, SYNC_TRYLOCK);
  474. xfs_sync_data(mp, SYNC_TRYLOCK | SYNC_WAIT);
  475. }
  476. int
  477. xfs_syncd_init(
  478. struct xfs_mount *mp)
  479. {
  480. INIT_WORK(&mp->m_flush_work, xfs_flush_worker);
  481. INIT_DELAYED_WORK(&mp->m_sync_work, xfs_sync_worker);
  482. INIT_DELAYED_WORK(&mp->m_reclaim_work, xfs_reclaim_worker);
  483. xfs_syncd_queue_sync(mp);
  484. xfs_syncd_queue_reclaim(mp);
  485. return 0;
  486. }
  487. void
  488. xfs_syncd_stop(
  489. struct xfs_mount *mp)
  490. {
  491. cancel_delayed_work_sync(&mp->m_sync_work);
  492. cancel_delayed_work_sync(&mp->m_reclaim_work);
  493. cancel_work_sync(&mp->m_flush_work);
  494. }
  495. void
  496. __xfs_inode_set_reclaim_tag(
  497. struct xfs_perag *pag,
  498. struct xfs_inode *ip)
  499. {
  500. radix_tree_tag_set(&pag->pag_ici_root,
  501. XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
  502. XFS_ICI_RECLAIM_TAG);
  503. if (!pag->pag_ici_reclaimable) {
  504. /* propagate the reclaim tag up into the perag radix tree */
  505. spin_lock(&ip->i_mount->m_perag_lock);
  506. radix_tree_tag_set(&ip->i_mount->m_perag_tree,
  507. XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
  508. XFS_ICI_RECLAIM_TAG);
  509. spin_unlock(&ip->i_mount->m_perag_lock);
  510. /* schedule periodic background inode reclaim */
  511. xfs_syncd_queue_reclaim(ip->i_mount);
  512. trace_xfs_perag_set_reclaim(ip->i_mount, pag->pag_agno,
  513. -1, _RET_IP_);
  514. }
  515. pag->pag_ici_reclaimable++;
  516. }
  517. /*
  518. * We set the inode flag atomically with the radix tree tag.
  519. * Once we get tag lookups on the radix tree, this inode flag
  520. * can go away.
  521. */
  522. void
  523. xfs_inode_set_reclaim_tag(
  524. xfs_inode_t *ip)
  525. {
  526. struct xfs_mount *mp = ip->i_mount;
  527. struct xfs_perag *pag;
  528. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
  529. spin_lock(&pag->pag_ici_lock);
  530. spin_lock(&ip->i_flags_lock);
  531. __xfs_inode_set_reclaim_tag(pag, ip);
  532. __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
  533. spin_unlock(&ip->i_flags_lock);
  534. spin_unlock(&pag->pag_ici_lock);
  535. xfs_perag_put(pag);
  536. }
  537. STATIC void
  538. __xfs_inode_clear_reclaim(
  539. xfs_perag_t *pag,
  540. xfs_inode_t *ip)
  541. {
  542. pag->pag_ici_reclaimable--;
  543. if (!pag->pag_ici_reclaimable) {
  544. /* clear the reclaim tag from the perag radix tree */
  545. spin_lock(&ip->i_mount->m_perag_lock);
  546. radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
  547. XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
  548. XFS_ICI_RECLAIM_TAG);
  549. spin_unlock(&ip->i_mount->m_perag_lock);
  550. trace_xfs_perag_clear_reclaim(ip->i_mount, pag->pag_agno,
  551. -1, _RET_IP_);
  552. }
  553. }
  554. void
  555. __xfs_inode_clear_reclaim_tag(
  556. xfs_mount_t *mp,
  557. xfs_perag_t *pag,
  558. xfs_inode_t *ip)
  559. {
  560. radix_tree_tag_clear(&pag->pag_ici_root,
  561. XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
  562. __xfs_inode_clear_reclaim(pag, ip);
  563. }
  564. /*
  565. * Grab the inode for reclaim exclusively.
  566. * Return 0 if we grabbed it, non-zero otherwise.
  567. */
  568. STATIC int
  569. xfs_reclaim_inode_grab(
  570. struct xfs_inode *ip,
  571. int flags)
  572. {
  573. ASSERT(rcu_read_lock_held());
  574. /* quick check for stale RCU freed inode */
  575. if (!ip->i_ino)
  576. return 1;
  577. /*
  578. * do some unlocked checks first to avoid unnecessary lock traffic.
  579. * The first is a flush lock check, the second is a already in reclaim
  580. * check. Only do these checks if we are not going to block on locks.
  581. */
  582. if ((flags & SYNC_TRYLOCK) &&
  583. (!ip->i_flush.done || __xfs_iflags_test(ip, XFS_IRECLAIM))) {
  584. return 1;
  585. }
  586. /*
  587. * The radix tree lock here protects a thread in xfs_iget from racing
  588. * with us starting reclaim on the inode. Once we have the
  589. * XFS_IRECLAIM flag set it will not touch us.
  590. *
  591. * Due to RCU lookup, we may find inodes that have been freed and only
  592. * have XFS_IRECLAIM set. Indeed, we may see reallocated inodes that
  593. * aren't candidates for reclaim at all, so we must check the
  594. * XFS_IRECLAIMABLE is set first before proceeding to reclaim.
  595. */
  596. spin_lock(&ip->i_flags_lock);
  597. if (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) ||
  598. __xfs_iflags_test(ip, XFS_IRECLAIM)) {
  599. /* not a reclaim candidate. */
  600. spin_unlock(&ip->i_flags_lock);
  601. return 1;
  602. }
  603. __xfs_iflags_set(ip, XFS_IRECLAIM);
  604. spin_unlock(&ip->i_flags_lock);
  605. return 0;
  606. }
  607. /*
  608. * Inodes in different states need to be treated differently, and the return
  609. * value of xfs_iflush is not sufficient to get this right. The following table
  610. * lists the inode states and the reclaim actions necessary for non-blocking
  611. * reclaim:
  612. *
  613. *
  614. * inode state iflush ret required action
  615. * --------------- ---------- ---------------
  616. * bad - reclaim
  617. * shutdown EIO unpin and reclaim
  618. * clean, unpinned 0 reclaim
  619. * stale, unpinned 0 reclaim
  620. * clean, pinned(*) 0 requeue
  621. * stale, pinned EAGAIN requeue
  622. * dirty, delwri ok 0 requeue
  623. * dirty, delwri blocked EAGAIN requeue
  624. * dirty, sync flush 0 reclaim
  625. *
  626. * (*) dgc: I don't think the clean, pinned state is possible but it gets
  627. * handled anyway given the order of checks implemented.
  628. *
  629. * As can be seen from the table, the return value of xfs_iflush() is not
  630. * sufficient to correctly decide the reclaim action here. The checks in
  631. * xfs_iflush() might look like duplicates, but they are not.
  632. *
  633. * Also, because we get the flush lock first, we know that any inode that has
  634. * been flushed delwri has had the flush completed by the time we check that
  635. * the inode is clean. The clean inode check needs to be done before flushing
  636. * the inode delwri otherwise we would loop forever requeuing clean inodes as
  637. * we cannot tell apart a successful delwri flush and a clean inode from the
  638. * return value of xfs_iflush().
  639. *
  640. * Note that because the inode is flushed delayed write by background
  641. * writeback, the flush lock may already be held here and waiting on it can
  642. * result in very long latencies. Hence for sync reclaims, where we wait on the
  643. * flush lock, the caller should push out delayed write inodes first before
  644. * trying to reclaim them to minimise the amount of time spent waiting. For
  645. * background relaim, we just requeue the inode for the next pass.
  646. *
  647. * Hence the order of actions after gaining the locks should be:
  648. * bad => reclaim
  649. * shutdown => unpin and reclaim
  650. * pinned, delwri => requeue
  651. * pinned, sync => unpin
  652. * stale => reclaim
  653. * clean => reclaim
  654. * dirty, delwri => flush and requeue
  655. * dirty, sync => flush, wait and reclaim
  656. */
  657. STATIC int
  658. xfs_reclaim_inode(
  659. struct xfs_inode *ip,
  660. struct xfs_perag *pag,
  661. int sync_mode)
  662. {
  663. int error;
  664. restart:
  665. error = 0;
  666. xfs_ilock(ip, XFS_ILOCK_EXCL);
  667. if (!xfs_iflock_nowait(ip)) {
  668. if (!(sync_mode & SYNC_WAIT))
  669. goto out;
  670. xfs_iflock(ip);
  671. }
  672. if (is_bad_inode(VFS_I(ip)))
  673. goto reclaim;
  674. if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  675. xfs_iunpin_wait(ip);
  676. goto reclaim;
  677. }
  678. if (xfs_ipincount(ip)) {
  679. if (!(sync_mode & SYNC_WAIT)) {
  680. xfs_ifunlock(ip);
  681. goto out;
  682. }
  683. xfs_iunpin_wait(ip);
  684. }
  685. if (xfs_iflags_test(ip, XFS_ISTALE))
  686. goto reclaim;
  687. if (xfs_inode_clean(ip))
  688. goto reclaim;
  689. /*
  690. * Now we have an inode that needs flushing.
  691. *
  692. * We do a nonblocking flush here even if we are doing a SYNC_WAIT
  693. * reclaim as we can deadlock with inode cluster removal.
  694. * xfs_ifree_cluster() can lock the inode buffer before it locks the
  695. * ip->i_lock, and we are doing the exact opposite here. As a result,
  696. * doing a blocking xfs_itobp() to get the cluster buffer will result
  697. * in an ABBA deadlock with xfs_ifree_cluster().
  698. *
  699. * As xfs_ifree_cluser() must gather all inodes that are active in the
  700. * cache to mark them stale, if we hit this case we don't actually want
  701. * to do IO here - we want the inode marked stale so we can simply
  702. * reclaim it. Hence if we get an EAGAIN error on a SYNC_WAIT flush,
  703. * just unlock the inode, back off and try again. Hopefully the next
  704. * pass through will see the stale flag set on the inode.
  705. */
  706. error = xfs_iflush(ip, SYNC_TRYLOCK | sync_mode);
  707. if (sync_mode & SYNC_WAIT) {
  708. if (error == EAGAIN) {
  709. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  710. /* backoff longer than in xfs_ifree_cluster */
  711. delay(2);
  712. goto restart;
  713. }
  714. xfs_iflock(ip);
  715. goto reclaim;
  716. }
  717. /*
  718. * When we have to flush an inode but don't have SYNC_WAIT set, we
  719. * flush the inode out using a delwri buffer and wait for the next
  720. * call into reclaim to find it in a clean state instead of waiting for
  721. * it now. We also don't return errors here - if the error is transient
  722. * then the next reclaim pass will flush the inode, and if the error
  723. * is permanent then the next sync reclaim will reclaim the inode and
  724. * pass on the error.
  725. */
  726. if (error && error != EAGAIN && !XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  727. xfs_warn(ip->i_mount,
  728. "inode 0x%llx background reclaim flush failed with %d",
  729. (long long)ip->i_ino, error);
  730. }
  731. out:
  732. xfs_iflags_clear(ip, XFS_IRECLAIM);
  733. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  734. /*
  735. * We could return EAGAIN here to make reclaim rescan the inode tree in
  736. * a short while. However, this just burns CPU time scanning the tree
  737. * waiting for IO to complete and xfssyncd never goes back to the idle
  738. * state. Instead, return 0 to let the next scheduled background reclaim
  739. * attempt to reclaim the inode again.
  740. */
  741. return 0;
  742. reclaim:
  743. xfs_ifunlock(ip);
  744. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  745. XFS_STATS_INC(xs_ig_reclaims);
  746. /*
  747. * Remove the inode from the per-AG radix tree.
  748. *
  749. * Because radix_tree_delete won't complain even if the item was never
  750. * added to the tree assert that it's been there before to catch
  751. * problems with the inode life time early on.
  752. */
  753. spin_lock(&pag->pag_ici_lock);
  754. if (!radix_tree_delete(&pag->pag_ici_root,
  755. XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino)))
  756. ASSERT(0);
  757. __xfs_inode_clear_reclaim(pag, ip);
  758. spin_unlock(&pag->pag_ici_lock);
  759. /*
  760. * Here we do an (almost) spurious inode lock in order to coordinate
  761. * with inode cache radix tree lookups. This is because the lookup
  762. * can reference the inodes in the cache without taking references.
  763. *
  764. * We make that OK here by ensuring that we wait until the inode is
  765. * unlocked after the lookup before we go ahead and free it. We get
  766. * both the ilock and the iolock because the code may need to drop the
  767. * ilock one but will still hold the iolock.
  768. */
  769. xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  770. xfs_qm_dqdetach(ip);
  771. xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  772. xfs_inode_free(ip);
  773. return error;
  774. }
  775. /*
  776. * Walk the AGs and reclaim the inodes in them. Even if the filesystem is
  777. * corrupted, we still want to try to reclaim all the inodes. If we don't,
  778. * then a shut down during filesystem unmount reclaim walk leak all the
  779. * unreclaimed inodes.
  780. */
  781. int
  782. xfs_reclaim_inodes_ag(
  783. struct xfs_mount *mp,
  784. int flags,
  785. int *nr_to_scan)
  786. {
  787. struct xfs_perag *pag;
  788. int error = 0;
  789. int last_error = 0;
  790. xfs_agnumber_t ag;
  791. int trylock = flags & SYNC_TRYLOCK;
  792. int skipped;
  793. restart:
  794. ag = 0;
  795. skipped = 0;
  796. while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
  797. unsigned long first_index = 0;
  798. int done = 0;
  799. int nr_found = 0;
  800. ag = pag->pag_agno + 1;
  801. if (trylock) {
  802. if (!mutex_trylock(&pag->pag_ici_reclaim_lock)) {
  803. skipped++;
  804. xfs_perag_put(pag);
  805. continue;
  806. }
  807. first_index = pag->pag_ici_reclaim_cursor;
  808. } else
  809. mutex_lock(&pag->pag_ici_reclaim_lock);
  810. do {
  811. struct xfs_inode *batch[XFS_LOOKUP_BATCH];
  812. int i;
  813. rcu_read_lock();
  814. nr_found = radix_tree_gang_lookup_tag(
  815. &pag->pag_ici_root,
  816. (void **)batch, first_index,
  817. XFS_LOOKUP_BATCH,
  818. XFS_ICI_RECLAIM_TAG);
  819. if (!nr_found) {
  820. rcu_read_unlock();
  821. break;
  822. }
  823. /*
  824. * Grab the inodes before we drop the lock. if we found
  825. * nothing, nr == 0 and the loop will be skipped.
  826. */
  827. for (i = 0; i < nr_found; i++) {
  828. struct xfs_inode *ip = batch[i];
  829. if (done || xfs_reclaim_inode_grab(ip, flags))
  830. batch[i] = NULL;
  831. /*
  832. * Update the index for the next lookup. Catch
  833. * overflows into the next AG range which can
  834. * occur if we have inodes in the last block of
  835. * the AG and we are currently pointing to the
  836. * last inode.
  837. *
  838. * Because we may see inodes that are from the
  839. * wrong AG due to RCU freeing and
  840. * reallocation, only update the index if it
  841. * lies in this AG. It was a race that lead us
  842. * to see this inode, so another lookup from
  843. * the same index will not find it again.
  844. */
  845. if (XFS_INO_TO_AGNO(mp, ip->i_ino) !=
  846. pag->pag_agno)
  847. continue;
  848. first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
  849. if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
  850. done = 1;
  851. }
  852. /* unlock now we've grabbed the inodes. */
  853. rcu_read_unlock();
  854. for (i = 0; i < nr_found; i++) {
  855. if (!batch[i])
  856. continue;
  857. error = xfs_reclaim_inode(batch[i], pag, flags);
  858. if (error && last_error != EFSCORRUPTED)
  859. last_error = error;
  860. }
  861. *nr_to_scan -= XFS_LOOKUP_BATCH;
  862. } while (nr_found && !done && *nr_to_scan > 0);
  863. if (trylock && !done)
  864. pag->pag_ici_reclaim_cursor = first_index;
  865. else
  866. pag->pag_ici_reclaim_cursor = 0;
  867. mutex_unlock(&pag->pag_ici_reclaim_lock);
  868. xfs_perag_put(pag);
  869. }
  870. /*
  871. * if we skipped any AG, and we still have scan count remaining, do
  872. * another pass this time using blocking reclaim semantics (i.e
  873. * waiting on the reclaim locks and ignoring the reclaim cursors). This
  874. * ensure that when we get more reclaimers than AGs we block rather
  875. * than spin trying to execute reclaim.
  876. */
  877. if (trylock && skipped && *nr_to_scan > 0) {
  878. trylock = 0;
  879. goto restart;
  880. }
  881. return XFS_ERROR(last_error);
  882. }
  883. int
  884. xfs_reclaim_inodes(
  885. xfs_mount_t *mp,
  886. int mode)
  887. {
  888. int nr_to_scan = INT_MAX;
  889. return xfs_reclaim_inodes_ag(mp, mode, &nr_to_scan);
  890. }
  891. /*
  892. * Inode cache shrinker.
  893. *
  894. * When called we make sure that there is a background (fast) inode reclaim in
  895. * progress, while we will throttle the speed of reclaim via doiing synchronous
  896. * reclaim of inodes. That means if we come across dirty inodes, we wait for
  897. * them to be cleaned, which we hope will not be very long due to the
  898. * background walker having already kicked the IO off on those dirty inodes.
  899. */
  900. static int
  901. xfs_reclaim_inode_shrink(
  902. struct shrinker *shrink,
  903. int nr_to_scan,
  904. gfp_t gfp_mask)
  905. {
  906. struct xfs_mount *mp;
  907. struct xfs_perag *pag;
  908. xfs_agnumber_t ag;
  909. int reclaimable;
  910. mp = container_of(shrink, struct xfs_mount, m_inode_shrink);
  911. if (nr_to_scan) {
  912. /* kick background reclaimer */
  913. xfs_syncd_queue_reclaim(mp);
  914. if (!(gfp_mask & __GFP_FS))
  915. return -1;
  916. xfs_reclaim_inodes_ag(mp, SYNC_TRYLOCK | SYNC_WAIT,
  917. &nr_to_scan);
  918. /* terminate if we don't exhaust the scan */
  919. if (nr_to_scan > 0)
  920. return -1;
  921. }
  922. reclaimable = 0;
  923. ag = 0;
  924. while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
  925. ag = pag->pag_agno + 1;
  926. reclaimable += pag->pag_ici_reclaimable;
  927. xfs_perag_put(pag);
  928. }
  929. return reclaimable;
  930. }
  931. void
  932. xfs_inode_shrinker_register(
  933. struct xfs_mount *mp)
  934. {
  935. mp->m_inode_shrink.shrink = xfs_reclaim_inode_shrink;
  936. mp->m_inode_shrink.seeks = DEFAULT_SEEKS;
  937. register_shrinker(&mp->m_inode_shrink);
  938. }
  939. void
  940. xfs_inode_shrinker_unregister(
  941. struct xfs_mount *mp)
  942. {
  943. unregister_shrinker(&mp->m_inode_shrink);
  944. }