xfs_sync.c 29 KB

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