xfs_sync.c 27 KB

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