xfs_sync.c 27 KB

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