xfs_trans_ail.c 15 KB

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