xfs_trans_ail.c 14 KB

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