xfs_trans_ail.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  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. * The cursor keeps track of where our current traversal is up to by tracking
  123. * the next item in the list for us. However, for this to be safe, removing an
  124. * object from the AIL needs to invalidate any cursor that points to it. hence
  125. * the traversal cursor needs to be linked to the struct xfs_ail so that
  126. * deletion can search all the active cursors for invalidation.
  127. */
  128. STATIC void
  129. xfs_trans_ail_cursor_init(
  130. struct xfs_ail *ailp,
  131. struct xfs_ail_cursor *cur)
  132. {
  133. cur->item = NULL;
  134. list_add_tail(&cur->list, &ailp->ail_cursors);
  135. }
  136. /*
  137. * Get the next item in the traversal and advance the cursor. If the cursor
  138. * was invalidated (indicated by a lip of 1), restart the traversal.
  139. */
  140. struct xfs_log_item *
  141. xfs_trans_ail_cursor_next(
  142. struct xfs_ail *ailp,
  143. struct xfs_ail_cursor *cur)
  144. {
  145. struct xfs_log_item *lip = cur->item;
  146. if ((uintptr_t)lip & 1)
  147. lip = xfs_ail_min(ailp);
  148. if (lip)
  149. cur->item = xfs_ail_next(ailp, lip);
  150. return lip;
  151. }
  152. /*
  153. * When the traversal is complete, we need to remove the cursor from the list
  154. * of traversing cursors.
  155. */
  156. void
  157. xfs_trans_ail_cursor_done(
  158. struct xfs_ail_cursor *cur)
  159. {
  160. cur->item = NULL;
  161. list_del_init(&cur->list);
  162. }
  163. /*
  164. * Invalidate any cursor that is pointing to this item. This is called when an
  165. * item is removed from the AIL. Any cursor pointing to this object is now
  166. * invalid and the traversal needs to be terminated so it doesn't reference a
  167. * freed object. We set the low bit of the cursor item pointer so we can
  168. * distinguish between an invalidation and the end of the list when getting the
  169. * next item from the cursor.
  170. */
  171. STATIC void
  172. xfs_trans_ail_cursor_clear(
  173. struct xfs_ail *ailp,
  174. struct xfs_log_item *lip)
  175. {
  176. struct xfs_ail_cursor *cur;
  177. list_for_each_entry(cur, &ailp->ail_cursors, list) {
  178. if (cur->item == lip)
  179. cur->item = (struct xfs_log_item *)
  180. ((uintptr_t)cur->item | 1);
  181. }
  182. }
  183. /*
  184. * Find the first item in the AIL with the given @lsn by searching in ascending
  185. * LSN order and initialise the cursor to point to the next item for a
  186. * ascending traversal. Pass a @lsn of zero to initialise the cursor to the
  187. * first item in the AIL. Returns NULL if the list is empty.
  188. */
  189. struct xfs_log_item *
  190. xfs_trans_ail_cursor_first(
  191. struct xfs_ail *ailp,
  192. struct xfs_ail_cursor *cur,
  193. xfs_lsn_t lsn)
  194. {
  195. struct xfs_log_item *lip;
  196. xfs_trans_ail_cursor_init(ailp, cur);
  197. if (lsn == 0) {
  198. lip = xfs_ail_min(ailp);
  199. goto out;
  200. }
  201. list_for_each_entry(lip, &ailp->ail_head, li_ail) {
  202. if (XFS_LSN_CMP(lip->li_lsn, lsn) >= 0)
  203. goto out;
  204. }
  205. return NULL;
  206. out:
  207. if (lip)
  208. cur->item = xfs_ail_next(ailp, lip);
  209. return lip;
  210. }
  211. static struct xfs_log_item *
  212. __xfs_trans_ail_cursor_last(
  213. struct xfs_ail *ailp,
  214. xfs_lsn_t lsn)
  215. {
  216. struct xfs_log_item *lip;
  217. list_for_each_entry_reverse(lip, &ailp->ail_head, li_ail) {
  218. if (XFS_LSN_CMP(lip->li_lsn, lsn) <= 0)
  219. return lip;
  220. }
  221. return NULL;
  222. }
  223. /*
  224. * Find the last item in the AIL with the given @lsn by searching in descending
  225. * LSN order and initialise the cursor to point to that item. If there is no
  226. * item with the value of @lsn, then it sets the cursor to the last item with an
  227. * LSN lower than @lsn. Returns NULL if the list is empty.
  228. */
  229. struct xfs_log_item *
  230. xfs_trans_ail_cursor_last(
  231. struct xfs_ail *ailp,
  232. struct xfs_ail_cursor *cur,
  233. xfs_lsn_t lsn)
  234. {
  235. xfs_trans_ail_cursor_init(ailp, cur);
  236. cur->item = __xfs_trans_ail_cursor_last(ailp, lsn);
  237. return cur->item;
  238. }
  239. /*
  240. * Splice the log item list into the AIL at the given LSN. We splice to the
  241. * tail of the given LSN to maintain insert order for push traversals. The
  242. * cursor is optional, allowing repeated updates to the same LSN to avoid
  243. * repeated traversals. This should not be called with an empty list.
  244. */
  245. static void
  246. xfs_ail_splice(
  247. struct xfs_ail *ailp,
  248. struct xfs_ail_cursor *cur,
  249. struct list_head *list,
  250. xfs_lsn_t lsn)
  251. {
  252. struct xfs_log_item *lip;
  253. ASSERT(!list_empty(list));
  254. /*
  255. * Use the cursor to determine the insertion point if one is
  256. * provided. If not, or if the one we got is not valid,
  257. * find the place in the AIL where the items belong.
  258. */
  259. lip = cur ? cur->item : NULL;
  260. if (!lip || (uintptr_t)lip & 1)
  261. lip = __xfs_trans_ail_cursor_last(ailp, lsn);
  262. /*
  263. * If a cursor is provided, we know we're processing the AIL
  264. * in lsn order, and future items to be spliced in will
  265. * follow the last one being inserted now. Update the
  266. * cursor to point to that last item, now while we have a
  267. * reliable pointer to it.
  268. */
  269. if (cur)
  270. cur->item = list_entry(list->prev, struct xfs_log_item, li_ail);
  271. /*
  272. * Finally perform the splice. Unless the AIL was empty,
  273. * lip points to the item in the AIL _after_ which the new
  274. * items should go. If lip is null the AIL was empty, so
  275. * the new items go at the head of the AIL.
  276. */
  277. if (lip)
  278. list_splice(list, &lip->li_ail);
  279. else
  280. list_splice(list, &ailp->ail_head);
  281. }
  282. /*
  283. * Delete the given item from the AIL. Return a pointer to the item.
  284. */
  285. static void
  286. xfs_ail_delete(
  287. struct xfs_ail *ailp,
  288. struct xfs_log_item *lip)
  289. {
  290. xfs_ail_check(ailp, lip);
  291. list_del(&lip->li_ail);
  292. xfs_trans_ail_cursor_clear(ailp, lip);
  293. }
  294. /*
  295. * Requeue a failed buffer for writeback.
  296. *
  297. * We clear the log item failed state here as well, but we have to be careful
  298. * about reference counts because the only active reference counts on the buffer
  299. * may be the failed log items. Hence if we clear the log item failed state
  300. * before queuing the buffer for IO we can release all active references to
  301. * the buffer and free it, leading to use after free problems in
  302. * xfs_buf_delwri_queue. It makes no difference to the buffer or log items which
  303. * order we process them in - the buffer is locked, and we own the buffer list
  304. * so nothing on them is going to change while we are performing this action.
  305. *
  306. * Hence we can safely queue the buffer for IO before we clear the failed log
  307. * item state, therefore always having an active reference to the buffer and
  308. * avoiding the transient zero-reference state that leads to use-after-free.
  309. */
  310. static inline int
  311. xfsaild_resubmit_item(
  312. struct xfs_log_item *lip,
  313. struct list_head *buffer_list)
  314. {
  315. struct xfs_buf *bp = lip->li_buf;
  316. if (!xfs_buf_trylock(bp))
  317. return XFS_ITEM_LOCKED;
  318. if (!xfs_buf_delwri_queue(bp, buffer_list)) {
  319. xfs_buf_unlock(bp);
  320. return XFS_ITEM_FLUSHING;
  321. }
  322. /* protected by ail_lock */
  323. list_for_each_entry(lip, &bp->b_li_list, li_bio_list) {
  324. if (bp->b_flags & (_XBF_INODES | _XBF_DQUOTS))
  325. clear_bit(XFS_LI_FAILED, &lip->li_flags);
  326. else
  327. xfs_clear_li_failed(lip);
  328. }
  329. xfs_buf_unlock(bp);
  330. return XFS_ITEM_SUCCESS;
  331. }
  332. static inline uint
  333. xfsaild_push_item(
  334. struct xfs_ail *ailp,
  335. struct xfs_log_item *lip)
  336. {
  337. /*
  338. * If log item pinning is enabled, skip the push and track the item as
  339. * pinned. This can help induce head-behind-tail conditions.
  340. */
  341. if (XFS_TEST_ERROR(false, ailp->ail_log->l_mp, XFS_ERRTAG_LOG_ITEM_PIN))
  342. return XFS_ITEM_PINNED;
  343. /*
  344. * Consider the item pinned if a push callback is not defined so the
  345. * caller will force the log. This should only happen for intent items
  346. * as they are unpinned once the associated done item is committed to
  347. * the on-disk log.
  348. */
  349. if (!lip->li_ops->iop_push)
  350. return XFS_ITEM_PINNED;
  351. if (test_bit(XFS_LI_FAILED, &lip->li_flags))
  352. return xfsaild_resubmit_item(lip, &ailp->ail_buf_list);
  353. return lip->li_ops->iop_push(lip, &ailp->ail_buf_list);
  354. }
  355. /*
  356. * Compute the LSN that we'd need to push the log tail towards in order to have
  357. * at least 25% of the log space free. If the log free space already meets this
  358. * threshold, this function returns the lowest LSN in the AIL to slowly keep
  359. * writeback ticking over and the tail of the log moving forward.
  360. */
  361. static xfs_lsn_t
  362. xfs_ail_calc_push_target(
  363. struct xfs_ail *ailp)
  364. {
  365. struct xlog *log = ailp->ail_log;
  366. struct xfs_log_item *lip;
  367. xfs_lsn_t target_lsn;
  368. xfs_lsn_t max_lsn;
  369. xfs_lsn_t min_lsn;
  370. int32_t free_bytes;
  371. uint32_t target_block;
  372. uint32_t target_cycle;
  373. lockdep_assert_held(&ailp->ail_lock);
  374. lip = xfs_ail_max(ailp);
  375. if (!lip)
  376. return NULLCOMMITLSN;
  377. max_lsn = lip->li_lsn;
  378. min_lsn = __xfs_ail_min_lsn(ailp);
  379. /*
  380. * If we are supposed to push all the items in the AIL, we want to push
  381. * to the current head. We then clear the push flag so that we don't
  382. * keep pushing newly queued items beyond where the push all command was
  383. * run. If the push waiter wants to empty the ail, it should queue
  384. * itself on the ail_empty wait queue.
  385. */
  386. if (test_and_clear_bit(XFS_AIL_OPSTATE_PUSH_ALL, &ailp->ail_opstate))
  387. return max_lsn;
  388. /* If someone wants the AIL empty, keep pushing everything we have. */
  389. if (waitqueue_active(&ailp->ail_empty))
  390. return max_lsn;
  391. /*
  392. * Background pushing - attempt to keep 25% of the log free and if we
  393. * have that much free retain the existing target.
  394. */
  395. free_bytes = log->l_logsize - xlog_lsn_sub(log, max_lsn, min_lsn);
  396. if (free_bytes >= log->l_logsize >> 2)
  397. return ailp->ail_target;
  398. target_cycle = CYCLE_LSN(min_lsn);
  399. target_block = BLOCK_LSN(min_lsn) + (log->l_logBBsize >> 2);
  400. if (target_block >= log->l_logBBsize) {
  401. target_block -= log->l_logBBsize;
  402. target_cycle += 1;
  403. }
  404. target_lsn = xlog_assign_lsn(target_cycle, target_block);
  405. /* Cap the target to the highest LSN known to be in the AIL. */
  406. if (XFS_LSN_CMP(target_lsn, max_lsn) > 0)
  407. return max_lsn;
  408. /* If the existing target is higher than the new target, keep it. */
  409. if (XFS_LSN_CMP(ailp->ail_target, target_lsn) >= 0)
  410. return ailp->ail_target;
  411. return target_lsn;
  412. }
  413. static long
  414. xfsaild_push(
  415. struct xfs_ail *ailp)
  416. {
  417. struct xfs_mount *mp = ailp->ail_log->l_mp;
  418. struct xfs_ail_cursor cur;
  419. struct xfs_log_item *lip;
  420. xfs_lsn_t lsn;
  421. long tout;
  422. int stuck = 0;
  423. int flushing = 0;
  424. int count = 0;
  425. /*
  426. * If we encountered pinned items or did not finish writing out all
  427. * buffers the last time we ran, force a background CIL push to get the
  428. * items unpinned in the near future. We do not wait on the CIL push as
  429. * that could stall us for seconds if there is enough background IO
  430. * load. Stalling for that long when the tail of the log is pinned and
  431. * needs flushing will hard stop the transaction subsystem when log
  432. * space runs out.
  433. */
  434. if (ailp->ail_log_flush && ailp->ail_last_pushed_lsn == 0 &&
  435. (!list_empty_careful(&ailp->ail_buf_list) ||
  436. xfs_ail_min_lsn(ailp))) {
  437. ailp->ail_log_flush = 0;
  438. XFS_STATS_INC(mp, xs_push_ail_flush);
  439. xlog_cil_flush(ailp->ail_log);
  440. }
  441. spin_lock(&ailp->ail_lock);
  442. WRITE_ONCE(ailp->ail_target, xfs_ail_calc_push_target(ailp));
  443. if (ailp->ail_target == NULLCOMMITLSN)
  444. goto out_done;
  445. /* we're done if the AIL is empty or our push has reached the end */
  446. lip = xfs_trans_ail_cursor_first(ailp, &cur, ailp->ail_last_pushed_lsn);
  447. if (!lip)
  448. goto out_done_cursor;
  449. XFS_STATS_INC(mp, xs_push_ail);
  450. ASSERT(ailp->ail_target != NULLCOMMITLSN);
  451. lsn = lip->li_lsn;
  452. while ((XFS_LSN_CMP(lip->li_lsn, ailp->ail_target) <= 0)) {
  453. int lock_result;
  454. if (test_bit(XFS_LI_FLUSHING, &lip->li_flags))
  455. goto next_item;
  456. /*
  457. * Note that iop_push may unlock and reacquire the AIL lock. We
  458. * rely on the AIL cursor implementation to be able to deal with
  459. * the dropped lock.
  460. */
  461. lock_result = xfsaild_push_item(ailp, lip);
  462. switch (lock_result) {
  463. case XFS_ITEM_SUCCESS:
  464. XFS_STATS_INC(mp, xs_push_ail_success);
  465. trace_xfs_ail_push(lip);
  466. ailp->ail_last_pushed_lsn = lsn;
  467. break;
  468. case XFS_ITEM_FLUSHING:
  469. /*
  470. * The item or its backing buffer is already being
  471. * flushed. The typical reason for that is that an
  472. * inode buffer is locked because we already pushed the
  473. * updates to it as part of inode clustering.
  474. *
  475. * We do not want to stop flushing just because lots
  476. * of items are already being flushed, but we need to
  477. * re-try the flushing relatively soon if most of the
  478. * AIL is being flushed.
  479. */
  480. XFS_STATS_INC(mp, xs_push_ail_flushing);
  481. trace_xfs_ail_flushing(lip);
  482. flushing++;
  483. ailp->ail_last_pushed_lsn = lsn;
  484. break;
  485. case XFS_ITEM_PINNED:
  486. XFS_STATS_INC(mp, xs_push_ail_pinned);
  487. trace_xfs_ail_pinned(lip);
  488. stuck++;
  489. ailp->ail_log_flush++;
  490. break;
  491. case XFS_ITEM_LOCKED:
  492. XFS_STATS_INC(mp, xs_push_ail_locked);
  493. trace_xfs_ail_locked(lip);
  494. stuck++;
  495. break;
  496. default:
  497. ASSERT(0);
  498. break;
  499. }
  500. count++;
  501. /*
  502. * Are there too many items we can't do anything with?
  503. *
  504. * If we are skipping too many items because we can't flush
  505. * them or they are already being flushed, we back off and
  506. * given them time to complete whatever operation is being
  507. * done. i.e. remove pressure from the AIL while we can't make
  508. * progress so traversals don't slow down further inserts and
  509. * removals to/from the AIL.
  510. *
  511. * The value of 100 is an arbitrary magic number based on
  512. * observation.
  513. */
  514. if (stuck > 100)
  515. break;
  516. next_item:
  517. lip = xfs_trans_ail_cursor_next(ailp, &cur);
  518. if (lip == NULL)
  519. break;
  520. if (lip->li_lsn != lsn && count > 1000)
  521. break;
  522. lsn = lip->li_lsn;
  523. }
  524. out_done_cursor:
  525. xfs_trans_ail_cursor_done(&cur);
  526. out_done:
  527. spin_unlock(&ailp->ail_lock);
  528. if (xfs_buf_delwri_submit_nowait(&ailp->ail_buf_list))
  529. ailp->ail_log_flush++;
  530. if (!count || XFS_LSN_CMP(lsn, ailp->ail_target) >= 0) {
  531. /*
  532. * We reached the target or the AIL is empty, so wait a bit
  533. * longer for I/O to complete and remove pushed items from the
  534. * AIL before we start the next scan from the start of the AIL.
  535. */
  536. tout = 50;
  537. ailp->ail_last_pushed_lsn = 0;
  538. } else if (((stuck + flushing) * 100) / count > 90) {
  539. /*
  540. * Either there is a lot of contention on the AIL or we are
  541. * stuck due to operations in progress. "Stuck" in this case
  542. * is defined as >90% of the items we tried to push were stuck.
  543. *
  544. * Backoff a bit more to allow some I/O to complete before
  545. * restarting from the start of the AIL. This prevents us from
  546. * spinning on the same items, and if they are pinned will all
  547. * the restart to issue a log force to unpin the stuck items.
  548. */
  549. tout = 20;
  550. ailp->ail_last_pushed_lsn = 0;
  551. } else {
  552. /*
  553. * Assume we have more work to do in a short while.
  554. */
  555. tout = 0;
  556. }
  557. return tout;
  558. }
  559. static int
  560. xfsaild(
  561. void *data)
  562. {
  563. struct xfs_ail *ailp = data;
  564. long tout = 0; /* milliseconds */
  565. unsigned int noreclaim_flag;
  566. noreclaim_flag = memalloc_noreclaim_save();
  567. set_freezable();
  568. while (1) {
  569. /*
  570. * Long waits of 50ms or more occur when we've run out of items
  571. * to push, so we only want uninterruptible state if we're
  572. * actually blocked on something.
  573. */
  574. if (tout && tout <= 20)
  575. set_current_state(TASK_KILLABLE|TASK_FREEZABLE);
  576. else
  577. set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE);
  578. /*
  579. * Check kthread_should_stop() after we set the task state to
  580. * guarantee that we either see the stop bit and exit or the
  581. * task state is reset to runnable such that it's not scheduled
  582. * out indefinitely and detects the stop bit at next iteration.
  583. * A memory barrier is included in above task state set to
  584. * serialize again kthread_stop().
  585. */
  586. if (kthread_should_stop()) {
  587. __set_current_state(TASK_RUNNING);
  588. /*
  589. * The caller forces out the AIL before stopping the
  590. * thread in the common case, which means the delwri
  591. * queue is drained. In the shutdown case, the queue may
  592. * still hold relogged buffers that haven't been
  593. * submitted because they were pinned since added to the
  594. * queue.
  595. *
  596. * Log I/O error processing stales the underlying buffer
  597. * and clears the delwri state, expecting the buf to be
  598. * removed on the next submission attempt. That won't
  599. * happen if we're shutting down, so this is the last
  600. * opportunity to release such buffers from the queue.
  601. */
  602. ASSERT(list_empty(&ailp->ail_buf_list) ||
  603. xlog_is_shutdown(ailp->ail_log));
  604. xfs_buf_delwri_cancel(&ailp->ail_buf_list);
  605. break;
  606. }
  607. /* Idle if the AIL is empty. */
  608. spin_lock(&ailp->ail_lock);
  609. if (!xfs_ail_min(ailp) && list_empty(&ailp->ail_buf_list)) {
  610. spin_unlock(&ailp->ail_lock);
  611. schedule();
  612. tout = 0;
  613. continue;
  614. }
  615. spin_unlock(&ailp->ail_lock);
  616. if (tout)
  617. schedule_timeout(msecs_to_jiffies(tout));
  618. __set_current_state(TASK_RUNNING);
  619. try_to_freeze();
  620. tout = xfsaild_push(ailp);
  621. }
  622. memalloc_noreclaim_restore(noreclaim_flag);
  623. return 0;
  624. }
  625. /*
  626. * Push out all items in the AIL immediately and wait until the AIL is empty.
  627. */
  628. void
  629. xfs_ail_push_all_sync(
  630. struct xfs_ail *ailp)
  631. {
  632. DEFINE_WAIT(wait);
  633. spin_lock(&ailp->ail_lock);
  634. while (xfs_ail_max(ailp) != NULL) {
  635. prepare_to_wait(&ailp->ail_empty, &wait, TASK_UNINTERRUPTIBLE);
  636. wake_up_process(ailp->ail_task);
  637. spin_unlock(&ailp->ail_lock);
  638. schedule();
  639. spin_lock(&ailp->ail_lock);
  640. }
  641. spin_unlock(&ailp->ail_lock);
  642. finish_wait(&ailp->ail_empty, &wait);
  643. }
  644. void
  645. __xfs_ail_assign_tail_lsn(
  646. struct xfs_ail *ailp)
  647. {
  648. struct xlog *log = ailp->ail_log;
  649. xfs_lsn_t tail_lsn;
  650. assert_spin_locked(&ailp->ail_lock);
  651. if (xlog_is_shutdown(log))
  652. return;
  653. tail_lsn = __xfs_ail_min_lsn(ailp);
  654. if (!tail_lsn)
  655. tail_lsn = ailp->ail_head_lsn;
  656. WRITE_ONCE(log->l_tail_space,
  657. xlog_lsn_sub(log, ailp->ail_head_lsn, tail_lsn));
  658. trace_xfs_log_assign_tail_lsn(log, tail_lsn);
  659. atomic64_set(&log->l_tail_lsn, tail_lsn);
  660. }
  661. /*
  662. * Callers should pass the original tail lsn so that we can detect if the tail
  663. * has moved as a result of the operation that was performed. If the caller
  664. * needs to force a tail space update, it should pass NULLCOMMITLSN to bypass
  665. * the "did the tail LSN change?" checks. If the caller wants to avoid a tail
  666. * update (e.g. it knows the tail did not change) it should pass an @old_lsn of
  667. * 0.
  668. */
  669. void
  670. xfs_ail_update_finish(
  671. struct xfs_ail *ailp,
  672. xfs_lsn_t old_lsn) __releases(ailp->ail_lock)
  673. {
  674. struct xlog *log = ailp->ail_log;
  675. /* If the tail lsn hasn't changed, don't do updates or wakeups. */
  676. if (!old_lsn || old_lsn == __xfs_ail_min_lsn(ailp)) {
  677. spin_unlock(&ailp->ail_lock);
  678. return;
  679. }
  680. __xfs_ail_assign_tail_lsn(ailp);
  681. if (list_empty(&ailp->ail_head))
  682. wake_up_all(&ailp->ail_empty);
  683. spin_unlock(&ailp->ail_lock);
  684. xfs_log_space_wake(log->l_mp);
  685. }
  686. /*
  687. * xfs_trans_ail_update - bulk AIL insertion operation.
  688. *
  689. * @xfs_trans_ail_update takes an array of log items that all need to be
  690. * positioned at the same LSN in the AIL. If an item is not in the AIL, it will
  691. * be added. Otherwise, it will be repositioned by removing it and re-adding
  692. * it to the AIL. If we move the first item in the AIL, update the log tail to
  693. * match the new minimum LSN in the AIL.
  694. *
  695. * This function takes the AIL lock once to execute the update operations on
  696. * all the items in the array, and as such should not be called with the AIL
  697. * lock held. As a result, once we have the AIL lock, we need to check each log
  698. * item LSN to confirm it needs to be moved forward in the AIL.
  699. *
  700. * To optimise the insert operation, we delete all the items from the AIL in
  701. * the first pass, moving them into a temporary list, then splice the temporary
  702. * list into the correct position in the AIL. This avoids needing to do an
  703. * insert operation on every item.
  704. *
  705. * This function must be called with the AIL lock held. The lock is dropped
  706. * before returning.
  707. */
  708. void
  709. xfs_trans_ail_update_bulk(
  710. struct xfs_ail *ailp,
  711. struct xfs_ail_cursor *cur,
  712. struct xfs_log_item **log_items,
  713. int nr_items,
  714. xfs_lsn_t lsn) __releases(ailp->ail_lock)
  715. {
  716. struct xfs_log_item *mlip;
  717. xfs_lsn_t tail_lsn = 0;
  718. int i;
  719. LIST_HEAD(tmp);
  720. ASSERT(nr_items > 0); /* Not required, but true. */
  721. mlip = xfs_ail_min(ailp);
  722. for (i = 0; i < nr_items; i++) {
  723. struct xfs_log_item *lip = log_items[i];
  724. if (test_and_set_bit(XFS_LI_IN_AIL, &lip->li_flags)) {
  725. /* check if we really need to move the item */
  726. if (XFS_LSN_CMP(lsn, lip->li_lsn) <= 0)
  727. continue;
  728. trace_xfs_ail_move(lip, lip->li_lsn, lsn);
  729. if (mlip == lip && !tail_lsn)
  730. tail_lsn = lip->li_lsn;
  731. xfs_ail_delete(ailp, lip);
  732. } else {
  733. trace_xfs_ail_insert(lip, 0, lsn);
  734. }
  735. lip->li_lsn = lsn;
  736. list_add_tail(&lip->li_ail, &tmp);
  737. }
  738. if (!list_empty(&tmp))
  739. xfs_ail_splice(ailp, cur, &tmp, lsn);
  740. /*
  741. * If this is the first insert, wake up the push daemon so it can
  742. * actively scan for items to push. We also need to do a log tail
  743. * LSN update to ensure that it is correctly tracked by the log, so
  744. * set the tail_lsn to NULLCOMMITLSN so that xfs_ail_update_finish()
  745. * will see that the tail lsn has changed and will update the tail
  746. * appropriately.
  747. */
  748. if (!mlip) {
  749. wake_up_process(ailp->ail_task);
  750. tail_lsn = NULLCOMMITLSN;
  751. }
  752. xfs_ail_update_finish(ailp, tail_lsn);
  753. }
  754. /* Insert a log item into the AIL. */
  755. void
  756. xfs_trans_ail_insert(
  757. struct xfs_ail *ailp,
  758. struct xfs_log_item *lip,
  759. xfs_lsn_t lsn)
  760. {
  761. spin_lock(&ailp->ail_lock);
  762. xfs_trans_ail_update_bulk(ailp, NULL, &lip, 1, lsn);
  763. }
  764. /*
  765. * Delete one log item from the AIL.
  766. *
  767. * If this item was at the tail of the AIL, return the LSN of the log item so
  768. * that we can use it to check if the LSN of the tail of the log has moved
  769. * when finishing up the AIL delete process in xfs_ail_update_finish().
  770. */
  771. xfs_lsn_t
  772. xfs_ail_delete_one(
  773. struct xfs_ail *ailp,
  774. struct xfs_log_item *lip)
  775. {
  776. struct xfs_log_item *mlip = xfs_ail_min(ailp);
  777. xfs_lsn_t lsn = lip->li_lsn;
  778. trace_xfs_ail_delete(lip, mlip->li_lsn, lip->li_lsn);
  779. xfs_ail_delete(ailp, lip);
  780. clear_bit(XFS_LI_IN_AIL, &lip->li_flags);
  781. lip->li_lsn = 0;
  782. if (mlip == lip)
  783. return lsn;
  784. return 0;
  785. }
  786. void
  787. xfs_trans_ail_delete(
  788. struct xfs_log_item *lip,
  789. int shutdown_type)
  790. {
  791. struct xfs_ail *ailp = lip->li_ailp;
  792. struct xlog *log = ailp->ail_log;
  793. xfs_lsn_t tail_lsn;
  794. spin_lock(&ailp->ail_lock);
  795. if (!test_bit(XFS_LI_IN_AIL, &lip->li_flags)) {
  796. spin_unlock(&ailp->ail_lock);
  797. if (shutdown_type && !xlog_is_shutdown(log)) {
  798. xfs_alert_tag(log->l_mp, XFS_PTAG_AILDELETE,
  799. "%s: attempting to delete a log item that is not in the AIL",
  800. __func__);
  801. xlog_force_shutdown(log, shutdown_type);
  802. }
  803. return;
  804. }
  805. /* xfs_ail_update_finish() drops the AIL lock */
  806. xfs_clear_li_failed(lip);
  807. tail_lsn = xfs_ail_delete_one(ailp, lip);
  808. xfs_ail_update_finish(ailp, tail_lsn);
  809. }
  810. int
  811. xfs_trans_ail_init(
  812. xfs_mount_t *mp)
  813. {
  814. struct xfs_ail *ailp;
  815. ailp = kzalloc(sizeof(struct xfs_ail),
  816. GFP_KERNEL | __GFP_RETRY_MAYFAIL);
  817. if (!ailp)
  818. return -ENOMEM;
  819. ailp->ail_log = mp->m_log;
  820. INIT_LIST_HEAD(&ailp->ail_head);
  821. INIT_LIST_HEAD(&ailp->ail_cursors);
  822. spin_lock_init(&ailp->ail_lock);
  823. INIT_LIST_HEAD(&ailp->ail_buf_list);
  824. init_waitqueue_head(&ailp->ail_empty);
  825. ailp->ail_task = kthread_run(xfsaild, ailp, "xfsaild/%s",
  826. mp->m_super->s_id);
  827. if (IS_ERR(ailp->ail_task))
  828. goto out_free_ailp;
  829. mp->m_ail = ailp;
  830. return 0;
  831. out_free_ailp:
  832. kfree(ailp);
  833. return -ENOMEM;
  834. }
  835. void
  836. xfs_trans_ail_destroy(
  837. xfs_mount_t *mp)
  838. {
  839. struct xfs_ail *ailp = mp->m_ail;
  840. kthread_stop(ailp->ail_task);
  841. kfree(ailp);
  842. }