xfs_sync.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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, SYNC_WAIT);
  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. xfs_log_force(mp, 0);
  441. xfs_reclaim_inodes(mp, 0);
  442. /* dgc: errors ignored here */
  443. error = xfs_qm_sync(mp, SYNC_TRYLOCK);
  444. if (mp->m_super->s_frozen == SB_UNFROZEN &&
  445. xfs_log_need_covered(mp))
  446. error = xfs_fs_log_dummy(mp, 0);
  447. }
  448. mp->m_sync_seq++;
  449. wake_up(&mp->m_wait_single_sync_task);
  450. }
  451. STATIC int
  452. xfssyncd(
  453. void *arg)
  454. {
  455. struct xfs_mount *mp = arg;
  456. long timeleft;
  457. xfs_sync_work_t *work, *n;
  458. LIST_HEAD (tmp);
  459. set_freezable();
  460. timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10);
  461. for (;;) {
  462. if (list_empty(&mp->m_sync_list))
  463. timeleft = schedule_timeout_interruptible(timeleft);
  464. /* swsusp */
  465. try_to_freeze();
  466. if (kthread_should_stop() && list_empty(&mp->m_sync_list))
  467. break;
  468. spin_lock(&mp->m_sync_lock);
  469. /*
  470. * We can get woken by laptop mode, to do a sync -
  471. * that's the (only!) case where the list would be
  472. * empty with time remaining.
  473. */
  474. if (!timeleft || list_empty(&mp->m_sync_list)) {
  475. if (!timeleft)
  476. timeleft = xfs_syncd_centisecs *
  477. msecs_to_jiffies(10);
  478. INIT_LIST_HEAD(&mp->m_sync_work.w_list);
  479. list_add_tail(&mp->m_sync_work.w_list,
  480. &mp->m_sync_list);
  481. }
  482. list_splice_init(&mp->m_sync_list, &tmp);
  483. spin_unlock(&mp->m_sync_lock);
  484. list_for_each_entry_safe(work, n, &tmp, w_list) {
  485. (*work->w_syncer)(mp, work->w_data);
  486. list_del(&work->w_list);
  487. if (work == &mp->m_sync_work)
  488. continue;
  489. if (work->w_completion)
  490. complete(work->w_completion);
  491. kmem_free(work);
  492. }
  493. }
  494. return 0;
  495. }
  496. int
  497. xfs_syncd_init(
  498. struct xfs_mount *mp)
  499. {
  500. mp->m_sync_work.w_syncer = xfs_sync_worker;
  501. mp->m_sync_work.w_mount = mp;
  502. mp->m_sync_work.w_completion = NULL;
  503. mp->m_sync_task = kthread_run(xfssyncd, mp, "xfssyncd/%s", mp->m_fsname);
  504. if (IS_ERR(mp->m_sync_task))
  505. return -PTR_ERR(mp->m_sync_task);
  506. return 0;
  507. }
  508. void
  509. xfs_syncd_stop(
  510. struct xfs_mount *mp)
  511. {
  512. kthread_stop(mp->m_sync_task);
  513. }
  514. void
  515. __xfs_inode_set_reclaim_tag(
  516. struct xfs_perag *pag,
  517. struct xfs_inode *ip)
  518. {
  519. radix_tree_tag_set(&pag->pag_ici_root,
  520. XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
  521. XFS_ICI_RECLAIM_TAG);
  522. if (!pag->pag_ici_reclaimable) {
  523. /* propagate the reclaim tag up into the perag radix tree */
  524. spin_lock(&ip->i_mount->m_perag_lock);
  525. radix_tree_tag_set(&ip->i_mount->m_perag_tree,
  526. XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
  527. XFS_ICI_RECLAIM_TAG);
  528. spin_unlock(&ip->i_mount->m_perag_lock);
  529. trace_xfs_perag_set_reclaim(ip->i_mount, pag->pag_agno,
  530. -1, _RET_IP_);
  531. }
  532. pag->pag_ici_reclaimable++;
  533. }
  534. /*
  535. * We set the inode flag atomically with the radix tree tag.
  536. * Once we get tag lookups on the radix tree, this inode flag
  537. * can go away.
  538. */
  539. void
  540. xfs_inode_set_reclaim_tag(
  541. xfs_inode_t *ip)
  542. {
  543. struct xfs_mount *mp = ip->i_mount;
  544. struct xfs_perag *pag;
  545. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
  546. write_lock(&pag->pag_ici_lock);
  547. spin_lock(&ip->i_flags_lock);
  548. __xfs_inode_set_reclaim_tag(pag, ip);
  549. __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
  550. spin_unlock(&ip->i_flags_lock);
  551. write_unlock(&pag->pag_ici_lock);
  552. xfs_perag_put(pag);
  553. }
  554. STATIC void
  555. __xfs_inode_clear_reclaim(
  556. xfs_perag_t *pag,
  557. xfs_inode_t *ip)
  558. {
  559. pag->pag_ici_reclaimable--;
  560. if (!pag->pag_ici_reclaimable) {
  561. /* clear the reclaim tag from the perag radix tree */
  562. spin_lock(&ip->i_mount->m_perag_lock);
  563. radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
  564. XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
  565. XFS_ICI_RECLAIM_TAG);
  566. spin_unlock(&ip->i_mount->m_perag_lock);
  567. trace_xfs_perag_clear_reclaim(ip->i_mount, pag->pag_agno,
  568. -1, _RET_IP_);
  569. }
  570. }
  571. void
  572. __xfs_inode_clear_reclaim_tag(
  573. xfs_mount_t *mp,
  574. xfs_perag_t *pag,
  575. xfs_inode_t *ip)
  576. {
  577. radix_tree_tag_clear(&pag->pag_ici_root,
  578. XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
  579. __xfs_inode_clear_reclaim(pag, ip);
  580. }
  581. /*
  582. * Grab the inode for reclaim exclusively.
  583. * Return 0 if we grabbed it, non-zero otherwise.
  584. */
  585. STATIC int
  586. xfs_reclaim_inode_grab(
  587. struct xfs_inode *ip,
  588. int flags)
  589. {
  590. ASSERT(rcu_read_lock_held());
  591. /* quick check for stale RCU freed inode */
  592. if (!ip->i_ino)
  593. return 1;
  594. /*
  595. * do some unlocked checks first to avoid unnecessary lock traffic.
  596. * The first is a flush lock check, the second is a already in reclaim
  597. * check. Only do these checks if we are not going to block on locks.
  598. */
  599. if ((flags & SYNC_TRYLOCK) &&
  600. (!ip->i_flush.done || __xfs_iflags_test(ip, XFS_IRECLAIM))) {
  601. return 1;
  602. }
  603. /*
  604. * The radix tree lock here protects a thread in xfs_iget from racing
  605. * with us starting reclaim on the inode. Once we have the
  606. * XFS_IRECLAIM flag set it will not touch us.
  607. *
  608. * Due to RCU lookup, we may find inodes that have been freed and only
  609. * have XFS_IRECLAIM set. Indeed, we may see reallocated inodes that
  610. * aren't candidates for reclaim at all, so we must check the
  611. * XFS_IRECLAIMABLE is set first before proceeding to reclaim.
  612. */
  613. spin_lock(&ip->i_flags_lock);
  614. if (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) ||
  615. __xfs_iflags_test(ip, XFS_IRECLAIM)) {
  616. /* not a reclaim candidate. */
  617. spin_unlock(&ip->i_flags_lock);
  618. return 1;
  619. }
  620. __xfs_iflags_set(ip, XFS_IRECLAIM);
  621. spin_unlock(&ip->i_flags_lock);
  622. return 0;
  623. }
  624. /*
  625. * Inodes in different states need to be treated differently, and the return
  626. * value of xfs_iflush is not sufficient to get this right. The following table
  627. * lists the inode states and the reclaim actions necessary for non-blocking
  628. * reclaim:
  629. *
  630. *
  631. * inode state iflush ret required action
  632. * --------------- ---------- ---------------
  633. * bad - reclaim
  634. * shutdown EIO unpin and reclaim
  635. * clean, unpinned 0 reclaim
  636. * stale, unpinned 0 reclaim
  637. * clean, pinned(*) 0 requeue
  638. * stale, pinned EAGAIN requeue
  639. * dirty, delwri ok 0 requeue
  640. * dirty, delwri blocked EAGAIN requeue
  641. * dirty, sync flush 0 reclaim
  642. *
  643. * (*) dgc: I don't think the clean, pinned state is possible but it gets
  644. * handled anyway given the order of checks implemented.
  645. *
  646. * As can be seen from the table, the return value of xfs_iflush() is not
  647. * sufficient to correctly decide the reclaim action here. The checks in
  648. * xfs_iflush() might look like duplicates, but they are not.
  649. *
  650. * Also, because we get the flush lock first, we know that any inode that has
  651. * been flushed delwri has had the flush completed by the time we check that
  652. * the inode is clean. The clean inode check needs to be done before flushing
  653. * the inode delwri otherwise we would loop forever requeuing clean inodes as
  654. * we cannot tell apart a successful delwri flush and a clean inode from the
  655. * return value of xfs_iflush().
  656. *
  657. * Note that because the inode is flushed delayed write by background
  658. * writeback, the flush lock may already be held here and waiting on it can
  659. * result in very long latencies. Hence for sync reclaims, where we wait on the
  660. * flush lock, the caller should push out delayed write inodes first before
  661. * trying to reclaim them to minimise the amount of time spent waiting. For
  662. * background relaim, we just requeue the inode for the next pass.
  663. *
  664. * Hence the order of actions after gaining the locks should be:
  665. * bad => reclaim
  666. * shutdown => unpin and reclaim
  667. * pinned, delwri => requeue
  668. * pinned, sync => unpin
  669. * stale => reclaim
  670. * clean => reclaim
  671. * dirty, delwri => flush and requeue
  672. * dirty, sync => flush, wait and reclaim
  673. */
  674. STATIC int
  675. xfs_reclaim_inode(
  676. struct xfs_inode *ip,
  677. struct xfs_perag *pag,
  678. int sync_mode)
  679. {
  680. int error = 0;
  681. xfs_ilock(ip, XFS_ILOCK_EXCL);
  682. if (!xfs_iflock_nowait(ip)) {
  683. if (!(sync_mode & SYNC_WAIT))
  684. goto out;
  685. xfs_iflock(ip);
  686. }
  687. if (is_bad_inode(VFS_I(ip)))
  688. goto reclaim;
  689. if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  690. xfs_iunpin_wait(ip);
  691. goto reclaim;
  692. }
  693. if (xfs_ipincount(ip)) {
  694. if (!(sync_mode & SYNC_WAIT)) {
  695. xfs_ifunlock(ip);
  696. goto out;
  697. }
  698. xfs_iunpin_wait(ip);
  699. }
  700. if (xfs_iflags_test(ip, XFS_ISTALE))
  701. goto reclaim;
  702. if (xfs_inode_clean(ip))
  703. goto reclaim;
  704. /* Now we have an inode that needs flushing */
  705. error = xfs_iflush(ip, sync_mode);
  706. if (sync_mode & SYNC_WAIT) {
  707. xfs_iflock(ip);
  708. goto reclaim;
  709. }
  710. /*
  711. * When we have to flush an inode but don't have SYNC_WAIT set, we
  712. * flush the inode out using a delwri buffer and wait for the next
  713. * call into reclaim to find it in a clean state instead of waiting for
  714. * it now. We also don't return errors here - if the error is transient
  715. * then the next reclaim pass will flush the inode, and if the error
  716. * is permanent then the next sync reclaim will reclaim the inode and
  717. * pass on the error.
  718. */
  719. if (error && error != EAGAIN && !XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  720. xfs_fs_cmn_err(CE_WARN, ip->i_mount,
  721. "inode 0x%llx background reclaim flush failed with %d",
  722. (long long)ip->i_ino, error);
  723. }
  724. out:
  725. xfs_iflags_clear(ip, XFS_IRECLAIM);
  726. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  727. /*
  728. * We could return EAGAIN here to make reclaim rescan the inode tree in
  729. * a short while. However, this just burns CPU time scanning the tree
  730. * waiting for IO to complete and xfssyncd never goes back to the idle
  731. * state. Instead, return 0 to let the next scheduled background reclaim
  732. * attempt to reclaim the inode again.
  733. */
  734. return 0;
  735. reclaim:
  736. xfs_ifunlock(ip);
  737. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  738. XFS_STATS_INC(xs_ig_reclaims);
  739. /*
  740. * Remove the inode from the per-AG radix tree.
  741. *
  742. * Because radix_tree_delete won't complain even if the item was never
  743. * added to the tree assert that it's been there before to catch
  744. * problems with the inode life time early on.
  745. */
  746. write_lock(&pag->pag_ici_lock);
  747. if (!radix_tree_delete(&pag->pag_ici_root,
  748. XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino)))
  749. ASSERT(0);
  750. __xfs_inode_clear_reclaim(pag, ip);
  751. write_unlock(&pag->pag_ici_lock);
  752. /*
  753. * Here we do an (almost) spurious inode lock in order to coordinate
  754. * with inode cache radix tree lookups. This is because the lookup
  755. * can reference the inodes in the cache without taking references.
  756. *
  757. * We make that OK here by ensuring that we wait until the inode is
  758. * unlocked after the lookup before we go ahead and free it. We get
  759. * both the ilock and the iolock because the code may need to drop the
  760. * ilock one but will still hold the iolock.
  761. */
  762. xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  763. xfs_qm_dqdetach(ip);
  764. xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  765. xfs_inode_free(ip);
  766. return error;
  767. }
  768. /*
  769. * Walk the AGs and reclaim the inodes in them. Even if the filesystem is
  770. * corrupted, we still want to try to reclaim all the inodes. If we don't,
  771. * then a shut down during filesystem unmount reclaim walk leak all the
  772. * unreclaimed inodes.
  773. */
  774. int
  775. xfs_reclaim_inodes_ag(
  776. struct xfs_mount *mp,
  777. int flags,
  778. int *nr_to_scan)
  779. {
  780. struct xfs_perag *pag;
  781. int error = 0;
  782. int last_error = 0;
  783. xfs_agnumber_t ag;
  784. int trylock = flags & SYNC_TRYLOCK;
  785. int skipped;
  786. restart:
  787. ag = 0;
  788. skipped = 0;
  789. while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
  790. unsigned long first_index = 0;
  791. int done = 0;
  792. int nr_found = 0;
  793. ag = pag->pag_agno + 1;
  794. if (trylock) {
  795. if (!mutex_trylock(&pag->pag_ici_reclaim_lock)) {
  796. skipped++;
  797. xfs_perag_put(pag);
  798. continue;
  799. }
  800. first_index = pag->pag_ici_reclaim_cursor;
  801. } else
  802. mutex_lock(&pag->pag_ici_reclaim_lock);
  803. do {
  804. struct xfs_inode *batch[XFS_LOOKUP_BATCH];
  805. int i;
  806. rcu_read_lock();
  807. nr_found = radix_tree_gang_lookup_tag(
  808. &pag->pag_ici_root,
  809. (void **)batch, first_index,
  810. XFS_LOOKUP_BATCH,
  811. XFS_ICI_RECLAIM_TAG);
  812. if (!nr_found) {
  813. rcu_read_unlock();
  814. break;
  815. }
  816. /*
  817. * Grab the inodes before we drop the lock. if we found
  818. * nothing, nr == 0 and the loop will be skipped.
  819. */
  820. for (i = 0; i < nr_found; i++) {
  821. struct xfs_inode *ip = batch[i];
  822. if (done || xfs_reclaim_inode_grab(ip, flags))
  823. batch[i] = NULL;
  824. /*
  825. * Update the index for the next lookup. Catch
  826. * overflows into the next AG range which can
  827. * occur if we have inodes in the last block of
  828. * the AG and we are currently pointing to the
  829. * last inode.
  830. *
  831. * Because we may see inodes that are from the
  832. * wrong AG due to RCU freeing and
  833. * reallocation, only update the index if it
  834. * lies in this AG. It was a race that lead us
  835. * to see this inode, so another lookup from
  836. * the same index will not find it again.
  837. */
  838. if (XFS_INO_TO_AGNO(mp, ip->i_ino) !=
  839. pag->pag_agno)
  840. continue;
  841. first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
  842. if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
  843. done = 1;
  844. }
  845. /* unlock now we've grabbed the inodes. */
  846. rcu_read_unlock();
  847. for (i = 0; i < nr_found; i++) {
  848. if (!batch[i])
  849. continue;
  850. error = xfs_reclaim_inode(batch[i], pag, flags);
  851. if (error && last_error != EFSCORRUPTED)
  852. last_error = error;
  853. }
  854. *nr_to_scan -= XFS_LOOKUP_BATCH;
  855. } while (nr_found && !done && *nr_to_scan > 0);
  856. if (trylock && !done)
  857. pag->pag_ici_reclaim_cursor = first_index;
  858. else
  859. pag->pag_ici_reclaim_cursor = 0;
  860. mutex_unlock(&pag->pag_ici_reclaim_lock);
  861. xfs_perag_put(pag);
  862. }
  863. /*
  864. * if we skipped any AG, and we still have scan count remaining, do
  865. * another pass this time using blocking reclaim semantics (i.e
  866. * waiting on the reclaim locks and ignoring the reclaim cursors). This
  867. * ensure that when we get more reclaimers than AGs we block rather
  868. * than spin trying to execute reclaim.
  869. */
  870. if (trylock && skipped && *nr_to_scan > 0) {
  871. trylock = 0;
  872. goto restart;
  873. }
  874. return XFS_ERROR(last_error);
  875. }
  876. int
  877. xfs_reclaim_inodes(
  878. xfs_mount_t *mp,
  879. int mode)
  880. {
  881. int nr_to_scan = INT_MAX;
  882. return xfs_reclaim_inodes_ag(mp, mode, &nr_to_scan);
  883. }
  884. /*
  885. * Shrinker infrastructure.
  886. */
  887. static int
  888. xfs_reclaim_inode_shrink(
  889. struct shrinker *shrink,
  890. int nr_to_scan,
  891. gfp_t gfp_mask)
  892. {
  893. struct xfs_mount *mp;
  894. struct xfs_perag *pag;
  895. xfs_agnumber_t ag;
  896. int reclaimable;
  897. mp = container_of(shrink, struct xfs_mount, m_inode_shrink);
  898. if (nr_to_scan) {
  899. if (!(gfp_mask & __GFP_FS))
  900. return -1;
  901. xfs_reclaim_inodes_ag(mp, SYNC_TRYLOCK, &nr_to_scan);
  902. /* terminate if we don't exhaust the scan */
  903. if (nr_to_scan > 0)
  904. return -1;
  905. }
  906. reclaimable = 0;
  907. ag = 0;
  908. while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
  909. ag = pag->pag_agno + 1;
  910. reclaimable += pag->pag_ici_reclaimable;
  911. xfs_perag_put(pag);
  912. }
  913. return reclaimable;
  914. }
  915. void
  916. xfs_inode_shrinker_register(
  917. struct xfs_mount *mp)
  918. {
  919. mp->m_inode_shrink.shrink = xfs_reclaim_inode_shrink;
  920. mp->m_inode_shrink.seeks = DEFAULT_SEEKS;
  921. register_shrinker(&mp->m_inode_shrink);
  922. }
  923. void
  924. xfs_inode_shrinker_unregister(
  925. struct xfs_mount *mp)
  926. {
  927. unregister_shrinker(&mp->m_inode_shrink);
  928. }