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