xfs_trans_ail.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
  4. * Copyright (c) 2008 Dave Chinner
  5. * All Rights Reserved.
  6. */
  7. #include "xfs.h"
  8. #include "xfs_fs.h"
  9. #include "xfs_shared.h"
  10. #include "xfs_format.h"
  11. #include "xfs_log_format.h"
  12. #include "xfs_trans_resv.h"
  13. #include "xfs_mount.h"
  14. #include "xfs_trans.h"
  15. #include "xfs_trans_priv.h"
  16. #include "xfs_trace.h"
  17. #include "xfs_errortag.h"
  18. #include "xfs_error.h"
  19. #include "xfs_log.h"
  20. #include "xfs_log_priv.h"
  21. #ifdef DEBUG
  22. /*
  23. * Check that the list is sorted as it should be.
  24. *
  25. * Called with the ail lock held, but we don't want to assert fail with it
  26. * held otherwise we'll lock everything up and won't be able to debug the
  27. * cause. Hence we sample and check the state under the AIL lock and return if
  28. * everything is fine, otherwise we drop the lock and run the ASSERT checks.
  29. * Asserts may not be fatal, so pick the lock back up and continue onwards.
  30. */
  31. STATIC void
  32. xfs_ail_check(
  33. struct xfs_ail *ailp,
  34. struct xfs_log_item *lip)
  35. __must_hold(&ailp->ail_lock)
  36. {
  37. struct xfs_log_item *prev_lip;
  38. struct xfs_log_item *next_lip;
  39. xfs_lsn_t prev_lsn = NULLCOMMITLSN;
  40. xfs_lsn_t next_lsn = NULLCOMMITLSN;
  41. xfs_lsn_t lsn;
  42. bool in_ail;
  43. if (list_empty(&ailp->ail_head))
  44. return;
  45. /*
  46. * Sample then check the next and previous entries are valid.
  47. */
  48. in_ail = test_bit(XFS_LI_IN_AIL, &lip->li_flags);
  49. prev_lip = list_entry(lip->li_ail.prev, struct xfs_log_item, li_ail);
  50. if (&prev_lip->li_ail != &ailp->ail_head)
  51. prev_lsn = prev_lip->li_lsn;
  52. next_lip = list_entry(lip->li_ail.next, struct xfs_log_item, li_ail);
  53. if (&next_lip->li_ail != &ailp->ail_head)
  54. next_lsn = next_lip->li_lsn;
  55. lsn = lip->li_lsn;
  56. if (in_ail &&
  57. (prev_lsn == NULLCOMMITLSN || XFS_LSN_CMP(prev_lsn, lsn) <= 0) &&
  58. (next_lsn == NULLCOMMITLSN || XFS_LSN_CMP(next_lsn, lsn) >= 0))
  59. return;
  60. spin_unlock(&ailp->ail_lock);
  61. ASSERT(in_ail);
  62. ASSERT(prev_lsn == NULLCOMMITLSN || XFS_LSN_CMP(prev_lsn, lsn) <= 0);
  63. ASSERT(next_lsn == NULLCOMMITLSN || XFS_LSN_CMP(next_lsn, lsn) >= 0);
  64. spin_lock(&ailp->ail_lock);
  65. }
  66. #else /* !DEBUG */
  67. #define xfs_ail_check(a,l)
  68. #endif /* DEBUG */
  69. /*
  70. * Return a pointer to the last item in the AIL. If the AIL is empty, then
  71. * return NULL.
  72. */
  73. static struct xfs_log_item *
  74. xfs_ail_max(
  75. struct xfs_ail *ailp)
  76. {
  77. if (list_empty(&ailp->ail_head))
  78. return NULL;
  79. return list_entry(ailp->ail_head.prev, struct xfs_log_item, li_ail);
  80. }
  81. /*
  82. * Return a pointer to the item which follows the given item in the AIL. If
  83. * the given item is the last item in the list, then return NULL.
  84. */
  85. static struct xfs_log_item *
  86. xfs_ail_next(
  87. struct xfs_ail *ailp,
  88. struct xfs_log_item *lip)
  89. {
  90. if (lip->li_ail.next == &ailp->ail_head)
  91. return NULL;
  92. return list_first_entry(&lip->li_ail, struct xfs_log_item, li_ail);
  93. }
  94. /*
  95. * This is called by the log manager code to determine the LSN of the tail of
  96. * the log. This is exactly the LSN of the first item in the AIL. If the AIL
  97. * is empty, then this function returns 0.
  98. *
  99. * We need the AIL lock in order to get a coherent read of the lsn of the last
  100. * item in the AIL.
  101. */
  102. static xfs_lsn_t
  103. __xfs_ail_min_lsn(
  104. struct xfs_ail *ailp)
  105. {
  106. struct xfs_log_item *lip = xfs_ail_min(ailp);
  107. if (lip)
  108. return lip->li_lsn;
  109. return 0;
  110. }
  111. xfs_lsn_t
  112. xfs_ail_min_lsn(
  113. struct xfs_ail *ailp)
  114. {
  115. xfs_lsn_t lsn;
  116. spin_lock(&ailp->ail_lock);
  117. lsn = __xfs_ail_min_lsn(ailp);
  118. spin_unlock(&ailp->ail_lock);
  119. return lsn;
  120. }
  121. /*
  122. * Return the maximum lsn held in the AIL, or zero if the AIL is empty.
  123. */
  124. static xfs_lsn_t
  125. xfs_ail_max_lsn(
  126. struct xfs_ail *ailp)
  127. {
  128. xfs_lsn_t lsn = 0;
  129. struct xfs_log_item *lip;
  130. spin_lock(&ailp->ail_lock);
  131. lip = xfs_ail_max(ailp);
  132. if (lip)
  133. lsn = lip->li_lsn;
  134. spin_unlock(&ailp->ail_lock);
  135. return lsn;
  136. }
  137. /*
  138. * The cursor keeps track of where our current traversal is up to by tracking
  139. * the next item in the list for us. However, for this to be safe, removing an
  140. * object from the AIL needs to invalidate any cursor that points to it. hence
  141. * the traversal cursor needs to be linked to the struct xfs_ail so that
  142. * deletion can search all the active cursors for invalidation.
  143. */
  144. STATIC void
  145. xfs_trans_ail_cursor_init(
  146. struct xfs_ail *ailp,
  147. struct xfs_ail_cursor *cur)
  148. {
  149. cur->item = NULL;
  150. list_add_tail(&cur->list, &ailp->ail_cursors);
  151. }
  152. /*
  153. * Get the next item in the traversal and advance the cursor. If the cursor
  154. * was invalidated (indicated by a lip of 1), restart the traversal.
  155. */
  156. struct xfs_log_item *
  157. xfs_trans_ail_cursor_next(
  158. struct xfs_ail *ailp,
  159. struct xfs_ail_cursor *cur)
  160. {
  161. struct xfs_log_item *lip = cur->item;
  162. if ((uintptr_t)lip & 1)
  163. lip = xfs_ail_min(ailp);
  164. if (lip)
  165. cur->item = xfs_ail_next(ailp, lip);
  166. return lip;
  167. }
  168. /*
  169. * When the traversal is complete, we need to remove the cursor from the list
  170. * of traversing cursors.
  171. */
  172. void
  173. xfs_trans_ail_cursor_done(
  174. struct xfs_ail_cursor *cur)
  175. {
  176. cur->item = NULL;
  177. list_del_init(&cur->list);
  178. }
  179. /*
  180. * Invalidate any cursor that is pointing to this item. This is called when an
  181. * item is removed from the AIL. Any cursor pointing to this object is now
  182. * invalid and the traversal needs to be terminated so it doesn't reference a
  183. * freed object. We set the low bit of the cursor item pointer so we can
  184. * distinguish between an invalidation and the end of the list when getting the
  185. * next item from the cursor.
  186. */
  187. STATIC void
  188. xfs_trans_ail_cursor_clear(
  189. struct xfs_ail *ailp,
  190. struct xfs_log_item *lip)
  191. {
  192. struct xfs_ail_cursor *cur;
  193. list_for_each_entry(cur, &ailp->ail_cursors, list) {
  194. if (cur->item == lip)
  195. cur->item = (struct xfs_log_item *)
  196. ((uintptr_t)cur->item | 1);
  197. }
  198. }
  199. /*
  200. * Find the first item in the AIL with the given @lsn by searching in ascending
  201. * LSN order and initialise the cursor to point to the next item for a
  202. * ascending traversal. Pass a @lsn of zero to initialise the cursor to the
  203. * first item in the AIL. Returns NULL if the list is empty.
  204. */
  205. struct xfs_log_item *
  206. xfs_trans_ail_cursor_first(
  207. struct xfs_ail *ailp,
  208. struct xfs_ail_cursor *cur,
  209. xfs_lsn_t lsn)
  210. {
  211. struct xfs_log_item *lip;
  212. xfs_trans_ail_cursor_init(ailp, cur);
  213. if (lsn == 0) {
  214. lip = xfs_ail_min(ailp);
  215. goto out;
  216. }
  217. list_for_each_entry(lip, &ailp->ail_head, li_ail) {
  218. if (XFS_LSN_CMP(lip->li_lsn, lsn) >= 0)
  219. goto out;
  220. }
  221. return NULL;
  222. out:
  223. if (lip)
  224. cur->item = xfs_ail_next(ailp, lip);
  225. return lip;
  226. }
  227. static struct xfs_log_item *
  228. __xfs_trans_ail_cursor_last(
  229. struct xfs_ail *ailp,
  230. xfs_lsn_t lsn)
  231. {
  232. struct xfs_log_item *lip;
  233. list_for_each_entry_reverse(lip, &ailp->ail_head, li_ail) {
  234. if (XFS_LSN_CMP(lip->li_lsn, lsn) <= 0)
  235. return lip;
  236. }
  237. return NULL;
  238. }
  239. /*
  240. * Find the last item in the AIL with the given @lsn by searching in descending
  241. * LSN order and initialise the cursor to point to that item. If there is no
  242. * item with the value of @lsn, then it sets the cursor to the last item with an
  243. * LSN lower than @lsn. Returns NULL if the list is empty.
  244. */
  245. struct xfs_log_item *
  246. xfs_trans_ail_cursor_last(
  247. struct xfs_ail *ailp,
  248. struct xfs_ail_cursor *cur,
  249. xfs_lsn_t lsn)
  250. {
  251. xfs_trans_ail_cursor_init(ailp, cur);
  252. cur->item = __xfs_trans_ail_cursor_last(ailp, lsn);
  253. return cur->item;
  254. }
  255. /*
  256. * Splice the log item list into the AIL at the given LSN. We splice to the
  257. * tail of the given LSN to maintain insert order for push traversals. The
  258. * cursor is optional, allowing repeated updates to the same LSN to avoid
  259. * repeated traversals. This should not be called with an empty list.
  260. */
  261. static void
  262. xfs_ail_splice(
  263. struct xfs_ail *ailp,
  264. struct xfs_ail_cursor *cur,
  265. struct list_head *list,
  266. xfs_lsn_t lsn)
  267. {
  268. struct xfs_log_item *lip;
  269. ASSERT(!list_empty(list));
  270. /*
  271. * Use the cursor to determine the insertion point if one is
  272. * provided. If not, or if the one we got is not valid,
  273. * find the place in the AIL where the items belong.
  274. */
  275. lip = cur ? cur->item : NULL;
  276. if (!lip || (uintptr_t)lip & 1)
  277. lip = __xfs_trans_ail_cursor_last(ailp, lsn);
  278. /*
  279. * If a cursor is provided, we know we're processing the AIL
  280. * in lsn order, and future items to be spliced in will
  281. * follow the last one being inserted now. Update the
  282. * cursor to point to that last item, now while we have a
  283. * reliable pointer to it.
  284. */
  285. if (cur)
  286. cur->item = list_entry(list->prev, struct xfs_log_item, li_ail);
  287. /*
  288. * Finally perform the splice. Unless the AIL was empty,
  289. * lip points to the item in the AIL _after_ which the new
  290. * items should go. If lip is null the AIL was empty, so
  291. * the new items go at the head of the AIL.
  292. */
  293. if (lip)
  294. list_splice(list, &lip->li_ail);
  295. else
  296. list_splice(list, &ailp->ail_head);
  297. }
  298. /*
  299. * Delete the given item from the AIL. Return a pointer to the item.
  300. */
  301. static void
  302. xfs_ail_delete(
  303. struct xfs_ail *ailp,
  304. struct xfs_log_item *lip)
  305. {
  306. xfs_ail_check(ailp, lip);
  307. list_del(&lip->li_ail);
  308. xfs_trans_ail_cursor_clear(ailp, lip);
  309. }
  310. /*
  311. * Requeue a failed buffer for writeback.
  312. *
  313. * We clear the log item failed state here as well, but we have to be careful
  314. * about reference counts because the only active reference counts on the buffer
  315. * may be the failed log items. Hence if we clear the log item failed state
  316. * before queuing the buffer for IO we can release all active references to
  317. * the buffer and free it, leading to use after free problems in
  318. * xfs_buf_delwri_queue. It makes no difference to the buffer or log items which
  319. * order we process them in - the buffer is locked, and we own the buffer list
  320. * so nothing on them is going to change while we are performing this action.
  321. *
  322. * Hence we can safely queue the buffer for IO before we clear the failed log
  323. * item state, therefore always having an active reference to the buffer and
  324. * avoiding the transient zero-reference state that leads to use-after-free.
  325. */
  326. static inline int
  327. xfsaild_resubmit_item(
  328. struct xfs_log_item *lip,
  329. struct list_head *buffer_list)
  330. {
  331. struct xfs_buf *bp = lip->li_buf;
  332. if (!xfs_buf_trylock(bp))
  333. return XFS_ITEM_LOCKED;
  334. if (!xfs_buf_delwri_queue(bp, buffer_list)) {
  335. xfs_buf_unlock(bp);
  336. return XFS_ITEM_FLUSHING;
  337. }
  338. /* protected by ail_lock */
  339. list_for_each_entry(lip, &bp->b_li_list, li_bio_list) {
  340. if (bp->b_flags & _XBF_INODES)
  341. clear_bit(XFS_LI_FAILED, &lip->li_flags);
  342. else
  343. xfs_clear_li_failed(lip);
  344. }
  345. xfs_buf_unlock(bp);
  346. return XFS_ITEM_SUCCESS;
  347. }
  348. static inline uint
  349. xfsaild_push_item(
  350. struct xfs_ail *ailp,
  351. struct xfs_log_item *lip)
  352. {
  353. /*
  354. * If log item pinning is enabled, skip the push and track the item as
  355. * pinned. This can help induce head-behind-tail conditions.
  356. */
  357. if (XFS_TEST_ERROR(false, ailp->ail_log->l_mp, XFS_ERRTAG_LOG_ITEM_PIN))
  358. return XFS_ITEM_PINNED;
  359. /*
  360. * Consider the item pinned if a push callback is not defined so the
  361. * caller will force the log. This should only happen for intent items
  362. * as they are unpinned once the associated done item is committed to
  363. * the on-disk log.
  364. */
  365. if (!lip->li_ops->iop_push)
  366. return XFS_ITEM_PINNED;
  367. if (test_bit(XFS_LI_FAILED, &lip->li_flags))
  368. return xfsaild_resubmit_item(lip, &ailp->ail_buf_list);
  369. return lip->li_ops->iop_push(lip, &ailp->ail_buf_list);
  370. }
  371. static long
  372. xfsaild_push(
  373. struct xfs_ail *ailp)
  374. {
  375. struct xfs_mount *mp = ailp->ail_log->l_mp;
  376. struct xfs_ail_cursor cur;
  377. struct xfs_log_item *lip;
  378. xfs_lsn_t lsn;
  379. xfs_lsn_t target;
  380. long tout;
  381. int stuck = 0;
  382. int flushing = 0;
  383. int count = 0;
  384. /*
  385. * If we encountered pinned items or did not finish writing out all
  386. * buffers the last time we ran, force a background CIL push to get the
  387. * items unpinned in the near future. We do not wait on the CIL push as
  388. * that could stall us for seconds if there is enough background IO
  389. * load. Stalling for that long when the tail of the log is pinned and
  390. * needs flushing will hard stop the transaction subsystem when log
  391. * space runs out.
  392. */
  393. if (ailp->ail_log_flush && ailp->ail_last_pushed_lsn == 0 &&
  394. (!list_empty_careful(&ailp->ail_buf_list) ||
  395. xfs_ail_min_lsn(ailp))) {
  396. ailp->ail_log_flush = 0;
  397. XFS_STATS_INC(mp, xs_push_ail_flush);
  398. xlog_cil_flush(ailp->ail_log);
  399. }
  400. spin_lock(&ailp->ail_lock);
  401. /*
  402. * If we have a sync push waiter, we always have to push till the AIL is
  403. * empty. Update the target to point to the end of the AIL so that
  404. * capture updates that occur after the sync push waiter has gone to
  405. * sleep.
  406. */
  407. if (waitqueue_active(&ailp->ail_empty)) {
  408. lip = xfs_ail_max(ailp);
  409. if (lip)
  410. target = lip->li_lsn;
  411. } else {
  412. /* barrier matches the ail_target update in xfs_ail_push() */
  413. smp_rmb();
  414. target = ailp->ail_target;
  415. ailp->ail_target_prev = target;
  416. }
  417. /* we're done if the AIL is empty or our push has reached the end */
  418. lip = xfs_trans_ail_cursor_first(ailp, &cur, ailp->ail_last_pushed_lsn);
  419. if (!lip)
  420. goto out_done;
  421. XFS_STATS_INC(mp, xs_push_ail);
  422. lsn = lip->li_lsn;
  423. while ((XFS_LSN_CMP(lip->li_lsn, target) <= 0)) {
  424. int lock_result;
  425. /*
  426. * Note that iop_push may unlock and reacquire the AIL lock. We
  427. * rely on the AIL cursor implementation to be able to deal with
  428. * the dropped lock.
  429. */
  430. lock_result = xfsaild_push_item(ailp, lip);
  431. switch (lock_result) {
  432. case XFS_ITEM_SUCCESS:
  433. XFS_STATS_INC(mp, xs_push_ail_success);
  434. trace_xfs_ail_push(lip);
  435. ailp->ail_last_pushed_lsn = lsn;
  436. break;
  437. case XFS_ITEM_FLUSHING:
  438. /*
  439. * The item or its backing buffer is already being
  440. * flushed. The typical reason for that is that an
  441. * inode buffer is locked because we already pushed the
  442. * updates to it as part of inode clustering.
  443. *
  444. * We do not want to stop flushing just because lots
  445. * of items are already being flushed, but we need to
  446. * re-try the flushing relatively soon if most of the
  447. * AIL is being flushed.
  448. */
  449. XFS_STATS_INC(mp, xs_push_ail_flushing);
  450. trace_xfs_ail_flushing(lip);
  451. flushing++;
  452. ailp->ail_last_pushed_lsn = lsn;
  453. break;
  454. case XFS_ITEM_PINNED:
  455. XFS_STATS_INC(mp, xs_push_ail_pinned);
  456. trace_xfs_ail_pinned(lip);
  457. stuck++;
  458. ailp->ail_log_flush++;
  459. break;
  460. case XFS_ITEM_LOCKED:
  461. XFS_STATS_INC(mp, xs_push_ail_locked);
  462. trace_xfs_ail_locked(lip);
  463. stuck++;
  464. break;
  465. default:
  466. ASSERT(0);
  467. break;
  468. }
  469. count++;
  470. /*
  471. * Are there too many items we can't do anything with?
  472. *
  473. * If we are skipping too many items because we can't flush
  474. * them or they are already being flushed, we back off and
  475. * given them time to complete whatever operation is being
  476. * done. i.e. remove pressure from the AIL while we can't make
  477. * progress so traversals don't slow down further inserts and
  478. * removals to/from the AIL.
  479. *
  480. * The value of 100 is an arbitrary magic number based on
  481. * observation.
  482. */
  483. if (stuck > 100)
  484. break;
  485. lip = xfs_trans_ail_cursor_next(ailp, &cur);
  486. if (lip == NULL)
  487. break;
  488. lsn = lip->li_lsn;
  489. }
  490. out_done:
  491. xfs_trans_ail_cursor_done(&cur);
  492. spin_unlock(&ailp->ail_lock);
  493. if (xfs_buf_delwri_submit_nowait(&ailp->ail_buf_list))
  494. ailp->ail_log_flush++;
  495. if (!count || XFS_LSN_CMP(lsn, target) >= 0) {
  496. /*
  497. * We reached the target or the AIL is empty, so wait a bit
  498. * longer for I/O to complete and remove pushed items from the
  499. * AIL before we start the next scan from the start of the AIL.
  500. */
  501. tout = 50;
  502. ailp->ail_last_pushed_lsn = 0;
  503. } else if (((stuck + flushing) * 100) / count > 90) {
  504. /*
  505. * Either there is a lot of contention on the AIL or we are
  506. * stuck due to operations in progress. "Stuck" in this case
  507. * is defined as >90% of the items we tried to push were stuck.
  508. *
  509. * Backoff a bit more to allow some I/O to complete before
  510. * restarting from the start of the AIL. This prevents us from
  511. * spinning on the same items, and if they are pinned will all
  512. * the restart to issue a log force to unpin the stuck items.
  513. */
  514. tout = 20;
  515. ailp->ail_last_pushed_lsn = 0;
  516. } else {
  517. /*
  518. * Assume we have more work to do in a short while.
  519. */
  520. tout = 10;
  521. }
  522. return tout;
  523. }
  524. static int
  525. xfsaild(
  526. void *data)
  527. {
  528. struct xfs_ail *ailp = data;
  529. long tout = 0; /* milliseconds */
  530. unsigned int noreclaim_flag;
  531. noreclaim_flag = memalloc_noreclaim_save();
  532. set_freezable();
  533. while (1) {
  534. if (tout && tout <= 20)
  535. set_current_state(TASK_KILLABLE|TASK_FREEZABLE);
  536. else
  537. set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE);
  538. /*
  539. * Check kthread_should_stop() after we set the task state to
  540. * guarantee that we either see the stop bit and exit or the
  541. * task state is reset to runnable such that it's not scheduled
  542. * out indefinitely and detects the stop bit at next iteration.
  543. * A memory barrier is included in above task state set to
  544. * serialize again kthread_stop().
  545. */
  546. if (kthread_should_stop()) {
  547. __set_current_state(TASK_RUNNING);
  548. /*
  549. * The caller forces out the AIL before stopping the
  550. * thread in the common case, which means the delwri
  551. * queue is drained. In the shutdown case, the queue may
  552. * still hold relogged buffers that haven't been
  553. * submitted because they were pinned since added to the
  554. * queue.
  555. *
  556. * Log I/O error processing stales the underlying buffer
  557. * and clears the delwri state, expecting the buf to be
  558. * removed on the next submission attempt. That won't
  559. * happen if we're shutting down, so this is the last
  560. * opportunity to release such buffers from the queue.
  561. */
  562. ASSERT(list_empty(&ailp->ail_buf_list) ||
  563. xlog_is_shutdown(ailp->ail_log));
  564. xfs_buf_delwri_cancel(&ailp->ail_buf_list);
  565. break;
  566. }
  567. spin_lock(&ailp->ail_lock);
  568. /*
  569. * Idle if the AIL is empty and we are not racing with a target
  570. * update. We check the AIL after we set the task to a sleep
  571. * state to guarantee that we either catch an ail_target update
  572. * or that a wake_up resets the state to TASK_RUNNING.
  573. * Otherwise, we run the risk of sleeping indefinitely.
  574. *
  575. * The barrier matches the ail_target update in xfs_ail_push().
  576. */
  577. smp_rmb();
  578. if (!xfs_ail_min(ailp) &&
  579. ailp->ail_target == ailp->ail_target_prev &&
  580. list_empty(&ailp->ail_buf_list)) {
  581. spin_unlock(&ailp->ail_lock);
  582. schedule();
  583. tout = 0;
  584. continue;
  585. }
  586. spin_unlock(&ailp->ail_lock);
  587. if (tout)
  588. schedule_timeout(msecs_to_jiffies(tout));
  589. __set_current_state(TASK_RUNNING);
  590. try_to_freeze();
  591. tout = xfsaild_push(ailp);
  592. }
  593. memalloc_noreclaim_restore(noreclaim_flag);
  594. return 0;
  595. }
  596. /*
  597. * This routine is called to move the tail of the AIL forward. It does this by
  598. * trying to flush items in the AIL whose lsns are below the given
  599. * threshold_lsn.
  600. *
  601. * The push is run asynchronously in a workqueue, which means the caller needs
  602. * to handle waiting on the async flush for space to become available.
  603. * We don't want to interrupt any push that is in progress, hence we only queue
  604. * work if we set the pushing bit appropriately.
  605. *
  606. * We do this unlocked - we only need to know whether there is anything in the
  607. * AIL at the time we are called. We don't need to access the contents of
  608. * any of the objects, so the lock is not needed.
  609. */
  610. void
  611. xfs_ail_push(
  612. struct xfs_ail *ailp,
  613. xfs_lsn_t threshold_lsn)
  614. {
  615. struct xfs_log_item *lip;
  616. lip = xfs_ail_min(ailp);
  617. if (!lip || xlog_is_shutdown(ailp->ail_log) ||
  618. XFS_LSN_CMP(threshold_lsn, ailp->ail_target) <= 0)
  619. return;
  620. /*
  621. * Ensure that the new target is noticed in push code before it clears
  622. * the XFS_AIL_PUSHING_BIT.
  623. */
  624. smp_wmb();
  625. xfs_trans_ail_copy_lsn(ailp, &ailp->ail_target, &threshold_lsn);
  626. smp_wmb();
  627. wake_up_process(ailp->ail_task);
  628. }
  629. /*
  630. * Push out all items in the AIL immediately
  631. */
  632. void
  633. xfs_ail_push_all(
  634. struct xfs_ail *ailp)
  635. {
  636. xfs_lsn_t threshold_lsn = xfs_ail_max_lsn(ailp);
  637. if (threshold_lsn)
  638. xfs_ail_push(ailp, threshold_lsn);
  639. }
  640. /*
  641. * Push out all items in the AIL immediately and wait until the AIL is empty.
  642. */
  643. void
  644. xfs_ail_push_all_sync(
  645. struct xfs_ail *ailp)
  646. {
  647. DEFINE_WAIT(wait);
  648. spin_lock(&ailp->ail_lock);
  649. while (xfs_ail_max(ailp) != NULL) {
  650. prepare_to_wait(&ailp->ail_empty, &wait, TASK_UNINTERRUPTIBLE);
  651. wake_up_process(ailp->ail_task);
  652. spin_unlock(&ailp->ail_lock);
  653. schedule();
  654. spin_lock(&ailp->ail_lock);
  655. }
  656. spin_unlock(&ailp->ail_lock);
  657. finish_wait(&ailp->ail_empty, &wait);
  658. }
  659. void
  660. xfs_ail_update_finish(
  661. struct xfs_ail *ailp,
  662. xfs_lsn_t old_lsn) __releases(ailp->ail_lock)
  663. {
  664. struct xlog *log = ailp->ail_log;
  665. /* if the tail lsn hasn't changed, don't do updates or wakeups. */
  666. if (!old_lsn || old_lsn == __xfs_ail_min_lsn(ailp)) {
  667. spin_unlock(&ailp->ail_lock);
  668. return;
  669. }
  670. if (!xlog_is_shutdown(log))
  671. xlog_assign_tail_lsn_locked(log->l_mp);
  672. if (list_empty(&ailp->ail_head))
  673. wake_up_all(&ailp->ail_empty);
  674. spin_unlock(&ailp->ail_lock);
  675. xfs_log_space_wake(log->l_mp);
  676. }
  677. /*
  678. * xfs_trans_ail_update - bulk AIL insertion operation.
  679. *
  680. * @xfs_trans_ail_update takes an array of log items that all need to be
  681. * positioned at the same LSN in the AIL. If an item is not in the AIL, it will
  682. * be added. Otherwise, it will be repositioned by removing it and re-adding
  683. * it to the AIL. If we move the first item in the AIL, update the log tail to
  684. * match the new minimum LSN in the AIL.
  685. *
  686. * This function takes the AIL lock once to execute the update operations on
  687. * all the items in the array, and as such should not be called with the AIL
  688. * lock held. As a result, once we have the AIL lock, we need to check each log
  689. * item LSN to confirm it needs to be moved forward in the AIL.
  690. *
  691. * To optimise the insert operation, we delete all the items from the AIL in
  692. * the first pass, moving them into a temporary list, then splice the temporary
  693. * list into the correct position in the AIL. This avoids needing to do an
  694. * insert operation on every item.
  695. *
  696. * This function must be called with the AIL lock held. The lock is dropped
  697. * before returning.
  698. */
  699. void
  700. xfs_trans_ail_update_bulk(
  701. struct xfs_ail *ailp,
  702. struct xfs_ail_cursor *cur,
  703. struct xfs_log_item **log_items,
  704. int nr_items,
  705. xfs_lsn_t lsn) __releases(ailp->ail_lock)
  706. {
  707. struct xfs_log_item *mlip;
  708. xfs_lsn_t tail_lsn = 0;
  709. int i;
  710. LIST_HEAD(tmp);
  711. ASSERT(nr_items > 0); /* Not required, but true. */
  712. mlip = xfs_ail_min(ailp);
  713. for (i = 0; i < nr_items; i++) {
  714. struct xfs_log_item *lip = log_items[i];
  715. if (test_and_set_bit(XFS_LI_IN_AIL, &lip->li_flags)) {
  716. /* check if we really need to move the item */
  717. if (XFS_LSN_CMP(lsn, lip->li_lsn) <= 0)
  718. continue;
  719. trace_xfs_ail_move(lip, lip->li_lsn, lsn);
  720. if (mlip == lip && !tail_lsn)
  721. tail_lsn = lip->li_lsn;
  722. xfs_ail_delete(ailp, lip);
  723. } else {
  724. trace_xfs_ail_insert(lip, 0, lsn);
  725. }
  726. lip->li_lsn = lsn;
  727. list_add(&lip->li_ail, &tmp);
  728. }
  729. if (!list_empty(&tmp))
  730. xfs_ail_splice(ailp, cur, &tmp, lsn);
  731. xfs_ail_update_finish(ailp, tail_lsn);
  732. }
  733. /* Insert a log item into the AIL. */
  734. void
  735. xfs_trans_ail_insert(
  736. struct xfs_ail *ailp,
  737. struct xfs_log_item *lip,
  738. xfs_lsn_t lsn)
  739. {
  740. spin_lock(&ailp->ail_lock);
  741. xfs_trans_ail_update_bulk(ailp, NULL, &lip, 1, lsn);
  742. }
  743. /*
  744. * Delete one log item from the AIL.
  745. *
  746. * If this item was at the tail of the AIL, return the LSN of the log item so
  747. * that we can use it to check if the LSN of the tail of the log has moved
  748. * when finishing up the AIL delete process in xfs_ail_update_finish().
  749. */
  750. xfs_lsn_t
  751. xfs_ail_delete_one(
  752. struct xfs_ail *ailp,
  753. struct xfs_log_item *lip)
  754. {
  755. struct xfs_log_item *mlip = xfs_ail_min(ailp);
  756. xfs_lsn_t lsn = lip->li_lsn;
  757. trace_xfs_ail_delete(lip, mlip->li_lsn, lip->li_lsn);
  758. xfs_ail_delete(ailp, lip);
  759. clear_bit(XFS_LI_IN_AIL, &lip->li_flags);
  760. lip->li_lsn = 0;
  761. if (mlip == lip)
  762. return lsn;
  763. return 0;
  764. }
  765. void
  766. xfs_trans_ail_delete(
  767. struct xfs_log_item *lip,
  768. int shutdown_type)
  769. {
  770. struct xfs_ail *ailp = lip->li_ailp;
  771. struct xlog *log = ailp->ail_log;
  772. xfs_lsn_t tail_lsn;
  773. spin_lock(&ailp->ail_lock);
  774. if (!test_bit(XFS_LI_IN_AIL, &lip->li_flags)) {
  775. spin_unlock(&ailp->ail_lock);
  776. if (shutdown_type && !xlog_is_shutdown(log)) {
  777. xfs_alert_tag(log->l_mp, XFS_PTAG_AILDELETE,
  778. "%s: attempting to delete a log item that is not in the AIL",
  779. __func__);
  780. xlog_force_shutdown(log, shutdown_type);
  781. }
  782. return;
  783. }
  784. /* xfs_ail_update_finish() drops the AIL lock */
  785. xfs_clear_li_failed(lip);
  786. tail_lsn = xfs_ail_delete_one(ailp, lip);
  787. xfs_ail_update_finish(ailp, tail_lsn);
  788. }
  789. int
  790. xfs_trans_ail_init(
  791. xfs_mount_t *mp)
  792. {
  793. struct xfs_ail *ailp;
  794. ailp = kmem_zalloc(sizeof(struct xfs_ail), KM_MAYFAIL);
  795. if (!ailp)
  796. return -ENOMEM;
  797. ailp->ail_log = mp->m_log;
  798. INIT_LIST_HEAD(&ailp->ail_head);
  799. INIT_LIST_HEAD(&ailp->ail_cursors);
  800. spin_lock_init(&ailp->ail_lock);
  801. INIT_LIST_HEAD(&ailp->ail_buf_list);
  802. init_waitqueue_head(&ailp->ail_empty);
  803. ailp->ail_task = kthread_run(xfsaild, ailp, "xfsaild/%s",
  804. mp->m_super->s_id);
  805. if (IS_ERR(ailp->ail_task))
  806. goto out_free_ailp;
  807. mp->m_ail = ailp;
  808. return 0;
  809. out_free_ailp:
  810. kmem_free(ailp);
  811. return -ENOMEM;
  812. }
  813. void
  814. xfs_trans_ail_destroy(
  815. xfs_mount_t *mp)
  816. {
  817. struct xfs_ail *ailp = mp->m_ail;
  818. kthread_stop(ailp->ail_task);
  819. kmem_free(ailp);
  820. }