xfs_trans_ail.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. /*
  2. * Copyright (c) 2000-2002,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_log.h"
  22. #include "xfs_inum.h"
  23. #include "xfs_trans.h"
  24. #include "xfs_sb.h"
  25. #include "xfs_ag.h"
  26. #include "xfs_dmapi.h"
  27. #include "xfs_mount.h"
  28. #include "xfs_trans_priv.h"
  29. #include "xfs_error.h"
  30. STATIC void xfs_ail_insert(xfs_ail_entry_t *, xfs_log_item_t *);
  31. STATIC xfs_log_item_t * xfs_ail_delete(xfs_ail_entry_t *, xfs_log_item_t *);
  32. STATIC xfs_log_item_t * xfs_ail_min(xfs_ail_entry_t *);
  33. STATIC xfs_log_item_t * xfs_ail_next(xfs_ail_entry_t *, xfs_log_item_t *);
  34. #ifdef DEBUG
  35. STATIC void xfs_ail_check(xfs_ail_entry_t *);
  36. #else
  37. #define xfs_ail_check(a)
  38. #endif /* DEBUG */
  39. /*
  40. * This is called by the log manager code to determine the LSN
  41. * of the tail of the log. This is exactly the LSN of the first
  42. * item in the AIL. If the AIL is empty, then this function
  43. * returns 0.
  44. *
  45. * We need the AIL lock in order to get a coherent read of the
  46. * lsn of the last item in the AIL.
  47. */
  48. xfs_lsn_t
  49. xfs_trans_tail_ail(
  50. xfs_mount_t *mp)
  51. {
  52. xfs_lsn_t lsn;
  53. xfs_log_item_t *lip;
  54. SPLDECL(s);
  55. AIL_LOCK(mp,s);
  56. lip = xfs_ail_min(&(mp->m_ail));
  57. if (lip == NULL) {
  58. lsn = (xfs_lsn_t)0;
  59. } else {
  60. lsn = lip->li_lsn;
  61. }
  62. AIL_UNLOCK(mp, s);
  63. return lsn;
  64. }
  65. /*
  66. * xfs_trans_push_ail
  67. *
  68. * This routine is called to move the tail of the AIL
  69. * forward. It does this by trying to flush items in the AIL
  70. * whose lsns are below the given threshold_lsn.
  71. *
  72. * The routine returns the lsn of the tail of the log.
  73. */
  74. xfs_lsn_t
  75. xfs_trans_push_ail(
  76. xfs_mount_t *mp,
  77. xfs_lsn_t threshold_lsn)
  78. {
  79. xfs_lsn_t lsn;
  80. xfs_log_item_t *lip;
  81. int gen;
  82. int restarts;
  83. int lock_result;
  84. int flush_log;
  85. SPLDECL(s);
  86. #define XFS_TRANS_PUSH_AIL_RESTARTS 1000
  87. AIL_LOCK(mp,s);
  88. lip = xfs_trans_first_ail(mp, &gen);
  89. if (lip == NULL || XFS_FORCED_SHUTDOWN(mp)) {
  90. /*
  91. * Just return if the AIL is empty.
  92. */
  93. AIL_UNLOCK(mp, s);
  94. return (xfs_lsn_t)0;
  95. }
  96. XFS_STATS_INC(xs_push_ail);
  97. /*
  98. * While the item we are looking at is below the given threshold
  99. * try to flush it out. Make sure to limit the number of times
  100. * we allow xfs_trans_next_ail() to restart scanning from the
  101. * beginning of the list. We'd like not to stop until we've at least
  102. * tried to push on everything in the AIL with an LSN less than
  103. * the given threshold. However, we may give up before that if
  104. * we realize that we've been holding the AIL_LOCK for 'too long',
  105. * blocking interrupts. Currently, too long is < 500us roughly.
  106. */
  107. flush_log = 0;
  108. restarts = 0;
  109. while (((restarts < XFS_TRANS_PUSH_AIL_RESTARTS) &&
  110. (XFS_LSN_CMP(lip->li_lsn, threshold_lsn) < 0))) {
  111. /*
  112. * If we can lock the item without sleeping, unlock
  113. * the AIL lock and flush the item. Then re-grab the
  114. * AIL lock so we can look for the next item on the
  115. * AIL. Since we unlock the AIL while we flush the
  116. * item, the next routine may start over again at the
  117. * the beginning of the list if anything has changed.
  118. * That is what the generation count is for.
  119. *
  120. * If we can't lock the item, either its holder will flush
  121. * it or it is already being flushed or it is being relogged.
  122. * In any of these case it is being taken care of and we
  123. * can just skip to the next item in the list.
  124. */
  125. lock_result = IOP_TRYLOCK(lip);
  126. switch (lock_result) {
  127. case XFS_ITEM_SUCCESS:
  128. AIL_UNLOCK(mp, s);
  129. XFS_STATS_INC(xs_push_ail_success);
  130. IOP_PUSH(lip);
  131. AIL_LOCK(mp,s);
  132. break;
  133. case XFS_ITEM_PUSHBUF:
  134. AIL_UNLOCK(mp, s);
  135. XFS_STATS_INC(xs_push_ail_pushbuf);
  136. #ifdef XFSRACEDEBUG
  137. delay_for_intr();
  138. delay(300);
  139. #endif
  140. ASSERT(lip->li_ops->iop_pushbuf);
  141. ASSERT(lip);
  142. IOP_PUSHBUF(lip);
  143. AIL_LOCK(mp,s);
  144. break;
  145. case XFS_ITEM_PINNED:
  146. XFS_STATS_INC(xs_push_ail_pinned);
  147. flush_log = 1;
  148. break;
  149. case XFS_ITEM_LOCKED:
  150. XFS_STATS_INC(xs_push_ail_locked);
  151. break;
  152. case XFS_ITEM_FLUSHING:
  153. XFS_STATS_INC(xs_push_ail_flushing);
  154. break;
  155. default:
  156. ASSERT(0);
  157. break;
  158. }
  159. lip = xfs_trans_next_ail(mp, lip, &gen, &restarts);
  160. if (lip == NULL) {
  161. break;
  162. }
  163. if (XFS_FORCED_SHUTDOWN(mp)) {
  164. /*
  165. * Just return if we shut down during the last try.
  166. */
  167. AIL_UNLOCK(mp, s);
  168. return (xfs_lsn_t)0;
  169. }
  170. }
  171. if (flush_log) {
  172. /*
  173. * If something we need to push out was pinned, then
  174. * push out the log so it will become unpinned and
  175. * move forward in the AIL.
  176. */
  177. AIL_UNLOCK(mp, s);
  178. XFS_STATS_INC(xs_push_ail_flush);
  179. xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
  180. AIL_LOCK(mp, s);
  181. }
  182. lip = xfs_ail_min(&(mp->m_ail));
  183. if (lip == NULL) {
  184. lsn = (xfs_lsn_t)0;
  185. } else {
  186. lsn = lip->li_lsn;
  187. }
  188. AIL_UNLOCK(mp, s);
  189. return lsn;
  190. } /* xfs_trans_push_ail */
  191. /*
  192. * This is to be called when an item is unlocked that may have
  193. * been in the AIL. It will wake up the first member of the AIL
  194. * wait list if this item's unlocking might allow it to progress.
  195. * If the item is in the AIL, then we need to get the AIL lock
  196. * while doing our checking so we don't race with someone going
  197. * to sleep waiting for this event in xfs_trans_push_ail().
  198. */
  199. void
  200. xfs_trans_unlocked_item(
  201. xfs_mount_t *mp,
  202. xfs_log_item_t *lip)
  203. {
  204. xfs_log_item_t *min_lip;
  205. /*
  206. * If we're forcibly shutting down, we may have
  207. * unlocked log items arbitrarily. The last thing
  208. * we want to do is to move the tail of the log
  209. * over some potentially valid data.
  210. */
  211. if (!(lip->li_flags & XFS_LI_IN_AIL) ||
  212. XFS_FORCED_SHUTDOWN(mp)) {
  213. return;
  214. }
  215. /*
  216. * This is the one case where we can call into xfs_ail_min()
  217. * without holding the AIL lock because we only care about the
  218. * case where we are at the tail of the AIL. If the object isn't
  219. * at the tail, it doesn't matter what result we get back. This
  220. * is slightly racy because since we were just unlocked, we could
  221. * go to sleep between the call to xfs_ail_min and the call to
  222. * xfs_log_move_tail, have someone else lock us, commit to us disk,
  223. * move us out of the tail of the AIL, and then we wake up. However,
  224. * the call to xfs_log_move_tail() doesn't do anything if there's
  225. * not enough free space to wake people up so we're safe calling it.
  226. */
  227. min_lip = xfs_ail_min(&mp->m_ail);
  228. if (min_lip == lip)
  229. xfs_log_move_tail(mp, 1);
  230. } /* xfs_trans_unlocked_item */
  231. /*
  232. * Update the position of the item in the AIL with the new
  233. * lsn. If it is not yet in the AIL, add it. Otherwise, move
  234. * it to its new position by removing it and re-adding it.
  235. *
  236. * Wakeup anyone with an lsn less than the item's lsn. If the item
  237. * we move in the AIL is the minimum one, update the tail lsn in the
  238. * log manager.
  239. *
  240. * Increment the AIL's generation count to indicate that the tree
  241. * has changed.
  242. *
  243. * This function must be called with the AIL lock held. The lock
  244. * is dropped before returning, so the caller must pass in the
  245. * cookie returned by AIL_LOCK.
  246. */
  247. void
  248. xfs_trans_update_ail(
  249. xfs_mount_t *mp,
  250. xfs_log_item_t *lip,
  251. xfs_lsn_t lsn,
  252. unsigned long s) __releases(mp->m_ail_lock)
  253. {
  254. xfs_ail_entry_t *ailp;
  255. xfs_log_item_t *dlip=NULL;
  256. xfs_log_item_t *mlip; /* ptr to minimum lip */
  257. ailp = &(mp->m_ail);
  258. mlip = xfs_ail_min(ailp);
  259. if (lip->li_flags & XFS_LI_IN_AIL) {
  260. dlip = xfs_ail_delete(ailp, lip);
  261. ASSERT(dlip == lip);
  262. } else {
  263. lip->li_flags |= XFS_LI_IN_AIL;
  264. }
  265. lip->li_lsn = lsn;
  266. xfs_ail_insert(ailp, lip);
  267. mp->m_ail_gen++;
  268. if (mlip == dlip) {
  269. mlip = xfs_ail_min(&(mp->m_ail));
  270. AIL_UNLOCK(mp, s);
  271. xfs_log_move_tail(mp, mlip->li_lsn);
  272. } else {
  273. AIL_UNLOCK(mp, s);
  274. }
  275. } /* xfs_trans_update_ail */
  276. /*
  277. * Delete the given item from the AIL. It must already be in
  278. * the AIL.
  279. *
  280. * Wakeup anyone with an lsn less than item's lsn. If the item
  281. * we delete in the AIL is the minimum one, update the tail lsn in the
  282. * log manager.
  283. *
  284. * Clear the IN_AIL flag from the item, reset its lsn to 0, and
  285. * bump the AIL's generation count to indicate that the tree
  286. * has changed.
  287. *
  288. * This function must be called with the AIL lock held. The lock
  289. * is dropped before returning, so the caller must pass in the
  290. * cookie returned by AIL_LOCK.
  291. */
  292. void
  293. xfs_trans_delete_ail(
  294. xfs_mount_t *mp,
  295. xfs_log_item_t *lip,
  296. unsigned long s) __releases(mp->m_ail_lock)
  297. {
  298. xfs_ail_entry_t *ailp;
  299. xfs_log_item_t *dlip;
  300. xfs_log_item_t *mlip;
  301. if (lip->li_flags & XFS_LI_IN_AIL) {
  302. ailp = &(mp->m_ail);
  303. mlip = xfs_ail_min(ailp);
  304. dlip = xfs_ail_delete(ailp, lip);
  305. ASSERT(dlip == lip);
  306. lip->li_flags &= ~XFS_LI_IN_AIL;
  307. lip->li_lsn = 0;
  308. mp->m_ail_gen++;
  309. if (mlip == dlip) {
  310. mlip = xfs_ail_min(&(mp->m_ail));
  311. AIL_UNLOCK(mp, s);
  312. xfs_log_move_tail(mp, (mlip ? mlip->li_lsn : 0));
  313. } else {
  314. AIL_UNLOCK(mp, s);
  315. }
  316. }
  317. else {
  318. /*
  319. * If the file system is not being shutdown, we are in
  320. * serious trouble if we get to this stage.
  321. */
  322. if (XFS_FORCED_SHUTDOWN(mp))
  323. AIL_UNLOCK(mp, s);
  324. else {
  325. xfs_cmn_err(XFS_PTAG_AILDELETE, CE_ALERT, mp,
  326. "%s: attempting to delete a log item that is not in the AIL",
  327. __FUNCTION__);
  328. AIL_UNLOCK(mp, s);
  329. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  330. }
  331. }
  332. }
  333. /*
  334. * Return the item in the AIL with the smallest lsn.
  335. * Return the current tree generation number for use
  336. * in calls to xfs_trans_next_ail().
  337. */
  338. xfs_log_item_t *
  339. xfs_trans_first_ail(
  340. xfs_mount_t *mp,
  341. int *gen)
  342. {
  343. xfs_log_item_t *lip;
  344. lip = xfs_ail_min(&(mp->m_ail));
  345. *gen = (int)mp->m_ail_gen;
  346. return (lip);
  347. }
  348. /*
  349. * If the generation count of the tree has not changed since the
  350. * caller last took something from the AIL, then return the elmt
  351. * in the tree which follows the one given. If the count has changed,
  352. * then return the minimum elmt of the AIL and bump the restarts counter
  353. * if one is given.
  354. */
  355. xfs_log_item_t *
  356. xfs_trans_next_ail(
  357. xfs_mount_t *mp,
  358. xfs_log_item_t *lip,
  359. int *gen,
  360. int *restarts)
  361. {
  362. xfs_log_item_t *nlip;
  363. ASSERT(mp && lip && gen);
  364. if (mp->m_ail_gen == *gen) {
  365. nlip = xfs_ail_next(&(mp->m_ail), lip);
  366. } else {
  367. nlip = xfs_ail_min(&(mp->m_ail));
  368. *gen = (int)mp->m_ail_gen;
  369. if (restarts != NULL) {
  370. XFS_STATS_INC(xs_push_ail_restarts);
  371. (*restarts)++;
  372. }
  373. }
  374. return (nlip);
  375. }
  376. /*
  377. * The active item list (AIL) is a doubly linked list of log
  378. * items sorted by ascending lsn. The base of the list is
  379. * a forw/back pointer pair embedded in the xfs mount structure.
  380. * The base is initialized with both pointers pointing to the
  381. * base. This case always needs to be distinguished, because
  382. * the base has no lsn to look at. We almost always insert
  383. * at the end of the list, so on inserts we search from the
  384. * end of the list to find where the new item belongs.
  385. */
  386. /*
  387. * Initialize the doubly linked list to point only to itself.
  388. */
  389. void
  390. xfs_trans_ail_init(
  391. xfs_mount_t *mp)
  392. {
  393. mp->m_ail.ail_forw = (xfs_log_item_t*)&(mp->m_ail);
  394. mp->m_ail.ail_back = (xfs_log_item_t*)&(mp->m_ail);
  395. }
  396. /*
  397. * Insert the given log item into the AIL.
  398. * We almost always insert at the end of the list, so on inserts
  399. * we search from the end of the list to find where the
  400. * new item belongs.
  401. */
  402. STATIC void
  403. xfs_ail_insert(
  404. xfs_ail_entry_t *base,
  405. xfs_log_item_t *lip)
  406. /* ARGSUSED */
  407. {
  408. xfs_log_item_t *next_lip;
  409. /*
  410. * If the list is empty, just insert the item.
  411. */
  412. if (base->ail_back == (xfs_log_item_t*)base) {
  413. base->ail_forw = lip;
  414. base->ail_back = lip;
  415. lip->li_ail.ail_forw = (xfs_log_item_t*)base;
  416. lip->li_ail.ail_back = (xfs_log_item_t*)base;
  417. return;
  418. }
  419. next_lip = base->ail_back;
  420. while ((next_lip != (xfs_log_item_t*)base) &&
  421. (XFS_LSN_CMP(next_lip->li_lsn, lip->li_lsn) > 0)) {
  422. next_lip = next_lip->li_ail.ail_back;
  423. }
  424. ASSERT((next_lip == (xfs_log_item_t*)base) ||
  425. (XFS_LSN_CMP(next_lip->li_lsn, lip->li_lsn) <= 0));
  426. lip->li_ail.ail_forw = next_lip->li_ail.ail_forw;
  427. lip->li_ail.ail_back = next_lip;
  428. next_lip->li_ail.ail_forw = lip;
  429. lip->li_ail.ail_forw->li_ail.ail_back = lip;
  430. xfs_ail_check(base);
  431. return;
  432. }
  433. /*
  434. * Delete the given item from the AIL. Return a pointer to the item.
  435. */
  436. /*ARGSUSED*/
  437. STATIC xfs_log_item_t *
  438. xfs_ail_delete(
  439. xfs_ail_entry_t *base,
  440. xfs_log_item_t *lip)
  441. /* ARGSUSED */
  442. {
  443. lip->li_ail.ail_forw->li_ail.ail_back = lip->li_ail.ail_back;
  444. lip->li_ail.ail_back->li_ail.ail_forw = lip->li_ail.ail_forw;
  445. lip->li_ail.ail_forw = NULL;
  446. lip->li_ail.ail_back = NULL;
  447. xfs_ail_check(base);
  448. return lip;
  449. }
  450. /*
  451. * Return a pointer to the first item in the AIL.
  452. * If the AIL is empty, then return NULL.
  453. */
  454. STATIC xfs_log_item_t *
  455. xfs_ail_min(
  456. xfs_ail_entry_t *base)
  457. /* ARGSUSED */
  458. {
  459. register xfs_log_item_t *forw = base->ail_forw;
  460. if (forw == (xfs_log_item_t*)base) {
  461. return NULL;
  462. }
  463. return forw;
  464. }
  465. /*
  466. * Return a pointer to the item which follows
  467. * the given item in the AIL. If the given item
  468. * is the last item in the list, then return NULL.
  469. */
  470. STATIC xfs_log_item_t *
  471. xfs_ail_next(
  472. xfs_ail_entry_t *base,
  473. xfs_log_item_t *lip)
  474. /* ARGSUSED */
  475. {
  476. if (lip->li_ail.ail_forw == (xfs_log_item_t*)base) {
  477. return NULL;
  478. }
  479. return lip->li_ail.ail_forw;
  480. }
  481. #ifdef DEBUG
  482. /*
  483. * Check that the list is sorted as it should be.
  484. */
  485. STATIC void
  486. xfs_ail_check(
  487. xfs_ail_entry_t *base)
  488. {
  489. xfs_log_item_t *lip;
  490. xfs_log_item_t *prev_lip;
  491. lip = base->ail_forw;
  492. if (lip == (xfs_log_item_t*)base) {
  493. /*
  494. * Make sure the pointers are correct when the list
  495. * is empty.
  496. */
  497. ASSERT(base->ail_back == (xfs_log_item_t*)base);
  498. return;
  499. }
  500. /*
  501. * Walk the list checking forward and backward pointers,
  502. * lsn ordering, and that every entry has the XFS_LI_IN_AIL
  503. * flag set.
  504. */
  505. prev_lip = (xfs_log_item_t*)base;
  506. while (lip != (xfs_log_item_t*)base) {
  507. if (prev_lip != (xfs_log_item_t*)base) {
  508. ASSERT(prev_lip->li_ail.ail_forw == lip);
  509. ASSERT(XFS_LSN_CMP(prev_lip->li_lsn, lip->li_lsn) <= 0);
  510. }
  511. ASSERT(lip->li_ail.ail_back == prev_lip);
  512. ASSERT((lip->li_flags & XFS_LI_IN_AIL) != 0);
  513. prev_lip = lip;
  514. lip = lip->li_ail.ail_forw;
  515. }
  516. ASSERT(lip == (xfs_log_item_t*)base);
  517. ASSERT(base->ail_back == prev_lip);
  518. }
  519. #endif /* DEBUG */