2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
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.
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.
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
20 #include "xfs_types.h"
23 #include "xfs_trans.h"
26 #include "xfs_dmapi.h"
27 #include "xfs_mount.h"
28 #include "xfs_trans_priv.h"
29 #include "xfs_error.h"
31 STATIC
void xfs_ail_insert(xfs_ail_entry_t
*, xfs_log_item_t
*);
32 STATIC xfs_log_item_t
* xfs_ail_delete(xfs_ail_entry_t
*, xfs_log_item_t
*);
33 STATIC xfs_log_item_t
* xfs_ail_min(xfs_ail_entry_t
*);
34 STATIC xfs_log_item_t
* xfs_ail_next(xfs_ail_entry_t
*, xfs_log_item_t
*);
37 STATIC
void xfs_ail_check(xfs_ail_entry_t
*);
39 #define xfs_ail_check(a)
44 * This is called by the log manager code to determine the LSN
45 * of the tail of the log. This is exactly the LSN of the first
46 * item in the AIL. If the AIL is empty, then this function
49 * We need the AIL lock in order to get a coherent read of the
50 * lsn of the last item in the AIL.
59 spin_lock(&mp
->m_ail_lock
);
60 lip
= xfs_ail_min(&(mp
->m_ail
));
66 spin_unlock(&mp
->m_ail_lock
);
74 * This routine is called to move the tail of the AIL
75 * forward. It does this by trying to flush items in the AIL
76 * whose lsns are below the given threshold_lsn.
78 * The routine returns the lsn of the tail of the log.
83 xfs_lsn_t threshold_lsn
)
92 #define XFS_TRANS_PUSH_AIL_RESTARTS 1000
94 spin_lock(&mp
->m_ail_lock
);
95 lip
= xfs_trans_first_ail(mp
, &gen
);
96 if (lip
== NULL
|| XFS_FORCED_SHUTDOWN(mp
)) {
98 * Just return if the AIL is empty.
100 spin_unlock(&mp
->m_ail_lock
);
104 XFS_STATS_INC(xs_push_ail
);
107 * While the item we are looking at is below the given threshold
108 * try to flush it out. Make sure to limit the number of times
109 * we allow xfs_trans_next_ail() to restart scanning from the
110 * beginning of the list. We'd like not to stop until we've at least
111 * tried to push on everything in the AIL with an LSN less than
112 * the given threshold. However, we may give up before that if
113 * we realize that we've been holding the AIL lock for 'too long',
114 * blocking interrupts. Currently, too long is < 500us roughly.
118 while (((restarts
< XFS_TRANS_PUSH_AIL_RESTARTS
) &&
119 (XFS_LSN_CMP(lip
->li_lsn
, threshold_lsn
) < 0))) {
121 * If we can lock the item without sleeping, unlock
122 * the AIL lock and flush the item. Then re-grab the
123 * AIL lock so we can look for the next item on the
124 * AIL. Since we unlock the AIL while we flush the
125 * item, the next routine may start over again at the
126 * the beginning of the list if anything has changed.
127 * That is what the generation count is for.
129 * If we can't lock the item, either its holder will flush
130 * it or it is already being flushed or it is being relogged.
131 * In any of these case it is being taken care of and we
132 * can just skip to the next item in the list.
134 lock_result
= IOP_TRYLOCK(lip
);
135 switch (lock_result
) {
136 case XFS_ITEM_SUCCESS
:
137 spin_unlock(&mp
->m_ail_lock
);
138 XFS_STATS_INC(xs_push_ail_success
);
140 spin_lock(&mp
->m_ail_lock
);
143 case XFS_ITEM_PUSHBUF
:
144 spin_unlock(&mp
->m_ail_lock
);
145 XFS_STATS_INC(xs_push_ail_pushbuf
);
150 ASSERT(lip
->li_ops
->iop_pushbuf
);
153 spin_lock(&mp
->m_ail_lock
);
156 case XFS_ITEM_PINNED
:
157 XFS_STATS_INC(xs_push_ail_pinned
);
161 case XFS_ITEM_LOCKED
:
162 XFS_STATS_INC(xs_push_ail_locked
);
165 case XFS_ITEM_FLUSHING
:
166 XFS_STATS_INC(xs_push_ail_flushing
);
174 lip
= xfs_trans_next_ail(mp
, lip
, &gen
, &restarts
);
178 if (XFS_FORCED_SHUTDOWN(mp
)) {
180 * Just return if we shut down during the last try.
182 spin_unlock(&mp
->m_ail_lock
);
190 * If something we need to push out was pinned, then
191 * push out the log so it will become unpinned and
192 * move forward in the AIL.
194 spin_unlock(&mp
->m_ail_lock
);
195 XFS_STATS_INC(xs_push_ail_flush
);
196 xfs_log_force(mp
, (xfs_lsn_t
)0, XFS_LOG_FORCE
);
197 spin_lock(&mp
->m_ail_lock
);
200 lip
= xfs_ail_min(&(mp
->m_ail
));
207 spin_unlock(&mp
->m_ail_lock
);
209 } /* xfs_trans_push_ail */
213 * This is to be called when an item is unlocked that may have
214 * been in the AIL. It will wake up the first member of the AIL
215 * wait list if this item's unlocking might allow it to progress.
216 * If the item is in the AIL, then we need to get the AIL lock
217 * while doing our checking so we don't race with someone going
218 * to sleep waiting for this event in xfs_trans_push_ail().
221 xfs_trans_unlocked_item(
225 xfs_log_item_t
*min_lip
;
228 * If we're forcibly shutting down, we may have
229 * unlocked log items arbitrarily. The last thing
230 * we want to do is to move the tail of the log
231 * over some potentially valid data.
233 if (!(lip
->li_flags
& XFS_LI_IN_AIL
) ||
234 XFS_FORCED_SHUTDOWN(mp
)) {
239 * This is the one case where we can call into xfs_ail_min()
240 * without holding the AIL lock because we only care about the
241 * case where we are at the tail of the AIL. If the object isn't
242 * at the tail, it doesn't matter what result we get back. This
243 * is slightly racy because since we were just unlocked, we could
244 * go to sleep between the call to xfs_ail_min and the call to
245 * xfs_log_move_tail, have someone else lock us, commit to us disk,
246 * move us out of the tail of the AIL, and then we wake up. However,
247 * the call to xfs_log_move_tail() doesn't do anything if there's
248 * not enough free space to wake people up so we're safe calling it.
250 min_lip
= xfs_ail_min(&mp
->m_ail
);
253 xfs_log_move_tail(mp
, 1);
254 } /* xfs_trans_unlocked_item */
258 * Update the position of the item in the AIL with the new
259 * lsn. If it is not yet in the AIL, add it. Otherwise, move
260 * it to its new position by removing it and re-adding it.
262 * Wakeup anyone with an lsn less than the item's lsn. If the item
263 * we move in the AIL is the minimum one, update the tail lsn in the
266 * Increment the AIL's generation count to indicate that the tree
269 * This function must be called with the AIL lock held. The lock
270 * is dropped before returning.
273 xfs_trans_update_ail(
276 xfs_lsn_t lsn
) __releases(mp
->m_ail_lock
)
278 xfs_ail_entry_t
*ailp
;
279 xfs_log_item_t
*dlip
=NULL
;
280 xfs_log_item_t
*mlip
; /* ptr to minimum lip */
283 mlip
= xfs_ail_min(ailp
);
285 if (lip
->li_flags
& XFS_LI_IN_AIL
) {
286 dlip
= xfs_ail_delete(ailp
, lip
);
289 lip
->li_flags
|= XFS_LI_IN_AIL
;
294 xfs_ail_insert(ailp
, lip
);
298 mlip
= xfs_ail_min(&(mp
->m_ail
));
299 spin_unlock(&mp
->m_ail_lock
);
300 xfs_log_move_tail(mp
, mlip
->li_lsn
);
302 spin_unlock(&mp
->m_ail_lock
);
306 } /* xfs_trans_update_ail */
309 * Delete the given item from the AIL. It must already be in
312 * Wakeup anyone with an lsn less than item's lsn. If the item
313 * we delete in the AIL is the minimum one, update the tail lsn in the
316 * Clear the IN_AIL flag from the item, reset its lsn to 0, and
317 * bump the AIL's generation count to indicate that the tree
320 * This function must be called with the AIL lock held. The lock
321 * is dropped before returning.
324 xfs_trans_delete_ail(
326 xfs_log_item_t
*lip
) __releases(mp
->m_ail_lock
)
328 xfs_ail_entry_t
*ailp
;
329 xfs_log_item_t
*dlip
;
330 xfs_log_item_t
*mlip
;
332 if (lip
->li_flags
& XFS_LI_IN_AIL
) {
334 mlip
= xfs_ail_min(ailp
);
335 dlip
= xfs_ail_delete(ailp
, lip
);
339 lip
->li_flags
&= ~XFS_LI_IN_AIL
;
344 mlip
= xfs_ail_min(&(mp
->m_ail
));
345 spin_unlock(&mp
->m_ail_lock
);
346 xfs_log_move_tail(mp
, (mlip
? mlip
->li_lsn
: 0));
348 spin_unlock(&mp
->m_ail_lock
);
353 * If the file system is not being shutdown, we are in
354 * serious trouble if we get to this stage.
356 if (XFS_FORCED_SHUTDOWN(mp
))
357 spin_unlock(&mp
->m_ail_lock
);
359 xfs_cmn_err(XFS_PTAG_AILDELETE
, CE_ALERT
, mp
,
360 "%s: attempting to delete a log item that is not in the AIL",
362 spin_unlock(&mp
->m_ail_lock
);
363 xfs_force_shutdown(mp
, SHUTDOWN_CORRUPT_INCORE
);
371 * Return the item in the AIL with the smallest lsn.
372 * Return the current tree generation number for use
373 * in calls to xfs_trans_next_ail().
382 lip
= xfs_ail_min(&(mp
->m_ail
));
383 *gen
= (int)mp
->m_ail_gen
;
389 * If the generation count of the tree has not changed since the
390 * caller last took something from the AIL, then return the elmt
391 * in the tree which follows the one given. If the count has changed,
392 * then return the minimum elmt of the AIL and bump the restarts counter
402 xfs_log_item_t
*nlip
;
404 ASSERT(mp
&& lip
&& gen
);
405 if (mp
->m_ail_gen
== *gen
) {
406 nlip
= xfs_ail_next(&(mp
->m_ail
), lip
);
408 nlip
= xfs_ail_min(&(mp
->m_ail
));
409 *gen
= (int)mp
->m_ail_gen
;
410 if (restarts
!= NULL
) {
411 XFS_STATS_INC(xs_push_ail_restarts
);
421 * The active item list (AIL) is a doubly linked list of log
422 * items sorted by ascending lsn. The base of the list is
423 * a forw/back pointer pair embedded in the xfs mount structure.
424 * The base is initialized with both pointers pointing to the
425 * base. This case always needs to be distinguished, because
426 * the base has no lsn to look at. We almost always insert
427 * at the end of the list, so on inserts we search from the
428 * end of the list to find where the new item belongs.
432 * Initialize the doubly linked list to point only to itself.
438 mp
->m_ail
.ail_forw
= (xfs_log_item_t
*)&(mp
->m_ail
);
439 mp
->m_ail
.ail_back
= (xfs_log_item_t
*)&(mp
->m_ail
);
443 * Insert the given log item into the AIL.
444 * We almost always insert at the end of the list, so on inserts
445 * we search from the end of the list to find where the
450 xfs_ail_entry_t
*base
,
454 xfs_log_item_t
*next_lip
;
457 * If the list is empty, just insert the item.
459 if (base
->ail_back
== (xfs_log_item_t
*)base
) {
460 base
->ail_forw
= lip
;
461 base
->ail_back
= lip
;
462 lip
->li_ail
.ail_forw
= (xfs_log_item_t
*)base
;
463 lip
->li_ail
.ail_back
= (xfs_log_item_t
*)base
;
467 next_lip
= base
->ail_back
;
468 while ((next_lip
!= (xfs_log_item_t
*)base
) &&
469 (XFS_LSN_CMP(next_lip
->li_lsn
, lip
->li_lsn
) > 0)) {
470 next_lip
= next_lip
->li_ail
.ail_back
;
472 ASSERT((next_lip
== (xfs_log_item_t
*)base
) ||
473 (XFS_LSN_CMP(next_lip
->li_lsn
, lip
->li_lsn
) <= 0));
474 lip
->li_ail
.ail_forw
= next_lip
->li_ail
.ail_forw
;
475 lip
->li_ail
.ail_back
= next_lip
;
476 next_lip
->li_ail
.ail_forw
= lip
;
477 lip
->li_ail
.ail_forw
->li_ail
.ail_back
= lip
;
484 * Delete the given item from the AIL. Return a pointer to the item.
487 STATIC xfs_log_item_t
*
489 xfs_ail_entry_t
*base
,
493 lip
->li_ail
.ail_forw
->li_ail
.ail_back
= lip
->li_ail
.ail_back
;
494 lip
->li_ail
.ail_back
->li_ail
.ail_forw
= lip
->li_ail
.ail_forw
;
495 lip
->li_ail
.ail_forw
= NULL
;
496 lip
->li_ail
.ail_back
= NULL
;
503 * Return a pointer to the first item in the AIL.
504 * If the AIL is empty, then return NULL.
506 STATIC xfs_log_item_t
*
508 xfs_ail_entry_t
*base
)
511 register xfs_log_item_t
*forw
= base
->ail_forw
;
512 if (forw
== (xfs_log_item_t
*)base
) {
519 * Return a pointer to the item which follows
520 * the given item in the AIL. If the given item
521 * is the last item in the list, then return NULL.
523 STATIC xfs_log_item_t
*
525 xfs_ail_entry_t
*base
,
529 if (lip
->li_ail
.ail_forw
== (xfs_log_item_t
*)base
) {
532 return lip
->li_ail
.ail_forw
;
538 * Check that the list is sorted as it should be.
542 xfs_ail_entry_t
*base
)
545 xfs_log_item_t
*prev_lip
;
547 lip
= base
->ail_forw
;
548 if (lip
== (xfs_log_item_t
*)base
) {
550 * Make sure the pointers are correct when the list
553 ASSERT(base
->ail_back
== (xfs_log_item_t
*)base
);
558 * Walk the list checking forward and backward pointers,
559 * lsn ordering, and that every entry has the XFS_LI_IN_AIL
562 prev_lip
= (xfs_log_item_t
*)base
;
563 while (lip
!= (xfs_log_item_t
*)base
) {
564 if (prev_lip
!= (xfs_log_item_t
*)base
) {
565 ASSERT(prev_lip
->li_ail
.ail_forw
== lip
);
566 ASSERT(XFS_LSN_CMP(prev_lip
->li_lsn
, lip
->li_lsn
) <= 0);
568 ASSERT(lip
->li_ail
.ail_back
== prev_lip
);
569 ASSERT((lip
->li_flags
& XFS_LI_IN_AIL
) != 0);
571 lip
= lip
->li_ail
.ail_forw
;
573 ASSERT(lip
== (xfs_log_item_t
*)base
);
574 ASSERT(base
->ail_back
== prev_lip
);