xfs_sync.c 28 KB

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