xfs_sync.c 30 KB

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