xref: /openbmc/linux/fs/xfs/xfs_trans_ail.c (revision 27d8d5fe)
11da177e4SLinus Torvalds /*
27b718769SNathan Scott  * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
37b718769SNathan Scott  * All Rights Reserved.
41da177e4SLinus Torvalds  *
57b718769SNathan Scott  * This program is free software; you can redistribute it and/or
67b718769SNathan Scott  * modify it under the terms of the GNU General Public License as
71da177e4SLinus Torvalds  * published by the Free Software Foundation.
81da177e4SLinus Torvalds  *
97b718769SNathan Scott  * This program is distributed in the hope that it would be useful,
107b718769SNathan Scott  * but WITHOUT ANY WARRANTY; without even the implied warranty of
117b718769SNathan Scott  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
127b718769SNathan Scott  * GNU General Public License for more details.
131da177e4SLinus Torvalds  *
147b718769SNathan Scott  * You should have received a copy of the GNU General Public License
157b718769SNathan Scott  * along with this program; if not, write the Free Software Foundation,
167b718769SNathan Scott  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
171da177e4SLinus Torvalds  */
181da177e4SLinus Torvalds #include "xfs.h"
19a844f451SNathan Scott #include "xfs_fs.h"
201da177e4SLinus Torvalds #include "xfs_types.h"
211da177e4SLinus Torvalds #include "xfs_log.h"
22a844f451SNathan Scott #include "xfs_inum.h"
231da177e4SLinus Torvalds #include "xfs_trans.h"
241da177e4SLinus Torvalds #include "xfs_sb.h"
25da353b0dSDavid Chinner #include "xfs_ag.h"
261da177e4SLinus Torvalds #include "xfs_dmapi.h"
271da177e4SLinus Torvalds #include "xfs_mount.h"
281da177e4SLinus Torvalds #include "xfs_trans_priv.h"
291da177e4SLinus Torvalds #include "xfs_error.h"
301da177e4SLinus Torvalds 
3182fa9012SDavid Chinner STATIC void xfs_ail_insert(struct xfs_ail *, xfs_log_item_t *);
3282fa9012SDavid Chinner STATIC xfs_log_item_t * xfs_ail_delete(struct xfs_ail *, xfs_log_item_t *);
3382fa9012SDavid Chinner STATIC xfs_log_item_t * xfs_ail_min(struct xfs_ail *);
3482fa9012SDavid Chinner STATIC xfs_log_item_t * xfs_ail_next(struct xfs_ail *, xfs_log_item_t *);
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds #ifdef DEBUG
3782fa9012SDavid Chinner STATIC void xfs_ail_check(struct xfs_ail *, xfs_log_item_t *);
381da177e4SLinus Torvalds #else
39de08dbc1SDavid Chinner #define	xfs_ail_check(a,l)
401da177e4SLinus Torvalds #endif /* DEBUG */
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds /*
441da177e4SLinus Torvalds  * This is called by the log manager code to determine the LSN
451da177e4SLinus Torvalds  * of the tail of the log.  This is exactly the LSN of the first
461da177e4SLinus Torvalds  * item in the AIL.  If the AIL is empty, then this function
471da177e4SLinus Torvalds  * returns 0.
481da177e4SLinus Torvalds  *
491da177e4SLinus Torvalds  * We need the AIL lock in order to get a coherent read of the
501da177e4SLinus Torvalds  * lsn of the last item in the AIL.
511da177e4SLinus Torvalds  */
521da177e4SLinus Torvalds xfs_lsn_t
531da177e4SLinus Torvalds xfs_trans_tail_ail(
541da177e4SLinus Torvalds 	xfs_mount_t	*mp)
551da177e4SLinus Torvalds {
561da177e4SLinus Torvalds 	xfs_lsn_t	lsn;
571da177e4SLinus Torvalds 	xfs_log_item_t	*lip;
581da177e4SLinus Torvalds 
59287f3dadSDonald Douwsma 	spin_lock(&mp->m_ail_lock);
6082fa9012SDavid Chinner 	lip = xfs_ail_min(mp->m_ail);
611da177e4SLinus Torvalds 	if (lip == NULL) {
621da177e4SLinus Torvalds 		lsn = (xfs_lsn_t)0;
631da177e4SLinus Torvalds 	} else {
641da177e4SLinus Torvalds 		lsn = lip->li_lsn;
651da177e4SLinus Torvalds 	}
66287f3dadSDonald Douwsma 	spin_unlock(&mp->m_ail_lock);
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds 	return lsn;
691da177e4SLinus Torvalds }
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds /*
721da177e4SLinus Torvalds  * xfs_trans_push_ail
731da177e4SLinus Torvalds  *
74249a8c11SDavid Chinner  * This routine is called to move the tail of the AIL forward.  It does this by
75249a8c11SDavid Chinner  * trying to flush items in the AIL whose lsns are below the given
76249a8c11SDavid Chinner  * threshold_lsn.
771da177e4SLinus Torvalds  *
78249a8c11SDavid Chinner  * the push is run asynchronously in a separate thread, so we return the tail
79249a8c11SDavid Chinner  * of the log right now instead of the tail after the push. This means we will
80249a8c11SDavid Chinner  * either continue right away, or we will sleep waiting on the async thread to
81249a8c11SDavid Chinner  * do it's work.
82249a8c11SDavid Chinner  *
83249a8c11SDavid Chinner  * We do this unlocked - we only need to know whether there is anything in the
84249a8c11SDavid Chinner  * AIL at the time we are called. We don't need to access the contents of
85249a8c11SDavid Chinner  * any of the objects, so the lock is not needed.
861da177e4SLinus Torvalds  */
87249a8c11SDavid Chinner void
881da177e4SLinus Torvalds xfs_trans_push_ail(
891da177e4SLinus Torvalds 	xfs_mount_t		*mp,
901da177e4SLinus Torvalds 	xfs_lsn_t		threshold_lsn)
911da177e4SLinus Torvalds {
92249a8c11SDavid Chinner 	xfs_log_item_t		*lip;
93249a8c11SDavid Chinner 
9482fa9012SDavid Chinner 	lip = xfs_ail_min(mp->m_ail);
95249a8c11SDavid Chinner 	if (lip && !XFS_FORCED_SHUTDOWN(mp)) {
9682fa9012SDavid Chinner 		if (XFS_LSN_CMP(threshold_lsn, mp->m_ail->xa_target) > 0)
9782fa9012SDavid Chinner 			xfsaild_wakeup(mp->m_ail, threshold_lsn);
98249a8c11SDavid Chinner 	}
99249a8c11SDavid Chinner }
100249a8c11SDavid Chinner 
101249a8c11SDavid Chinner /*
10227d8d5feSDavid Chinner  * AIL traversal cursor initialisation.
10327d8d5feSDavid Chinner  *
10427d8d5feSDavid Chinner  * The cursor keeps track of where our current traversal is up
10527d8d5feSDavid Chinner  * to by tracking the next ƣtem in the list for us. However, for
10627d8d5feSDavid Chinner  * this to be safe, removing an object from the AIL needs to invalidate
10727d8d5feSDavid Chinner  * any cursor that points to it. hence the traversal cursor needs to
10827d8d5feSDavid Chinner  * be linked to the struct xfs_ail so that deletion can search all the
10927d8d5feSDavid Chinner  * active cursors for invalidation.
11027d8d5feSDavid Chinner  *
11127d8d5feSDavid Chinner  * We don't link the push cursor because it is embedded in the struct
11227d8d5feSDavid Chinner  * xfs_ail and hence easily findable.
11327d8d5feSDavid Chinner  */
11427d8d5feSDavid Chinner void
11527d8d5feSDavid Chinner xfs_trans_ail_cursor_init(
11627d8d5feSDavid Chinner 	struct xfs_ail		*ailp,
11727d8d5feSDavid Chinner 	struct xfs_ail_cursor	*cur)
11827d8d5feSDavid Chinner {
11927d8d5feSDavid Chinner 	cur->item = NULL;
12027d8d5feSDavid Chinner 	if (cur == &ailp->xa_cursors)
12127d8d5feSDavid Chinner 		return;
12227d8d5feSDavid Chinner 
12327d8d5feSDavid Chinner 	cur->next = ailp->xa_cursors.next;
12427d8d5feSDavid Chinner 	ailp->xa_cursors.next = cur;
12527d8d5feSDavid Chinner }
12627d8d5feSDavid Chinner 
12727d8d5feSDavid Chinner /*
12827d8d5feSDavid Chinner  * Set the cursor to the next item, because when we look
12927d8d5feSDavid Chinner  * up the cursor the current item may have been freed.
13027d8d5feSDavid Chinner  */
13127d8d5feSDavid Chinner STATIC void
13227d8d5feSDavid Chinner xfs_trans_ail_cursor_set(
13327d8d5feSDavid Chinner 	struct xfs_ail		*ailp,
13427d8d5feSDavid Chinner 	struct xfs_ail_cursor	*cur,
13527d8d5feSDavid Chinner 	struct xfs_log_item	*lip)
13627d8d5feSDavid Chinner {
13727d8d5feSDavid Chinner 	if (lip)
13827d8d5feSDavid Chinner 		cur->item = xfs_ail_next(ailp, lip);
13927d8d5feSDavid Chinner }
14027d8d5feSDavid Chinner 
14127d8d5feSDavid Chinner /*
14227d8d5feSDavid Chinner  * Get the next item in the traversal and advance the cursor.
14327d8d5feSDavid Chinner  * If the cursor was invalidated (inidicated by a lip of 1),
14427d8d5feSDavid Chinner  * restart the traversal.
14527d8d5feSDavid Chinner  */
14627d8d5feSDavid Chinner STATIC struct xfs_log_item *
14727d8d5feSDavid Chinner xfs_trans_ail_cursor_next(
14827d8d5feSDavid Chinner 	struct xfs_ail		*ailp,
14927d8d5feSDavid Chinner 	struct xfs_ail_cursor	*cur)
15027d8d5feSDavid Chinner {
15127d8d5feSDavid Chinner 	struct xfs_log_item	*lip = cur->item;
15227d8d5feSDavid Chinner 
15327d8d5feSDavid Chinner 	if ((__psint_t)lip & 1)
15427d8d5feSDavid Chinner 		lip = xfs_ail_min(ailp);
15527d8d5feSDavid Chinner 	xfs_trans_ail_cursor_set(ailp, cur, lip);
15627d8d5feSDavid Chinner 	return lip;
15727d8d5feSDavid Chinner }
15827d8d5feSDavid Chinner 
15927d8d5feSDavid Chinner /*
16027d8d5feSDavid Chinner  * Invalidate any cursor that is pointing to this item. This is
16127d8d5feSDavid Chinner  * called when an item is removed from the AIL. Any cursor pointing
16227d8d5feSDavid Chinner  * to this object is now invalid and the traversal needs to be
16327d8d5feSDavid Chinner  * terminated so it doesn't reference a freed object. We set the
16427d8d5feSDavid Chinner  * cursor item to a value of 1 so we can distinguish between an
16527d8d5feSDavid Chinner  * invalidation and the end of the list when getting the next item
16627d8d5feSDavid Chinner  * from the cursor.
16727d8d5feSDavid Chinner  */
16827d8d5feSDavid Chinner STATIC void
16927d8d5feSDavid Chinner xfs_trans_ail_cursor_clear(
17027d8d5feSDavid Chinner 	struct xfs_ail		*ailp,
17127d8d5feSDavid Chinner 	struct xfs_log_item	*lip)
17227d8d5feSDavid Chinner {
17327d8d5feSDavid Chinner 	struct xfs_ail_cursor	*cur;
17427d8d5feSDavid Chinner 
17527d8d5feSDavid Chinner 	/* need to search all cursors */
17627d8d5feSDavid Chinner 	for (cur = &ailp->xa_cursors; cur; cur = cur->next) {
17727d8d5feSDavid Chinner 		if (cur->item == lip)
17827d8d5feSDavid Chinner 			cur->item = (struct xfs_log_item *)
17927d8d5feSDavid Chinner 					((__psint_t)cur->item | 1);
18027d8d5feSDavid Chinner 	}
18127d8d5feSDavid Chinner }
18227d8d5feSDavid Chinner 
18327d8d5feSDavid Chinner /*
18427d8d5feSDavid Chinner  * Now that the traversal is complete, we need to remove the cursor
18527d8d5feSDavid Chinner  * from the list of traversing cursors. Avoid removing the embedded
18627d8d5feSDavid Chinner  * push cursor, but use the fact it is alway present to make the
18727d8d5feSDavid Chinner  * list deletion simple.
18827d8d5feSDavid Chinner  */
18927d8d5feSDavid Chinner void
19027d8d5feSDavid Chinner xfs_trans_ail_cursor_done(
19127d8d5feSDavid Chinner 	struct xfs_ail		*ailp,
19227d8d5feSDavid Chinner 	struct xfs_ail_cursor	*done)
19327d8d5feSDavid Chinner {
19427d8d5feSDavid Chinner 	struct xfs_ail_cursor	*prev = NULL;
19527d8d5feSDavid Chinner 	struct xfs_ail_cursor	*cur;
19627d8d5feSDavid Chinner 
19727d8d5feSDavid Chinner 	done->item = NULL;
19827d8d5feSDavid Chinner 	if (done == &ailp->xa_cursors)
19927d8d5feSDavid Chinner 		return;
20027d8d5feSDavid Chinner 	prev = &ailp->xa_cursors;
20127d8d5feSDavid Chinner 	for (cur = prev->next; cur; prev = cur, cur = prev->next) {
20227d8d5feSDavid Chinner 		if (cur == done) {
20327d8d5feSDavid Chinner 			prev->next = cur->next;
20427d8d5feSDavid Chinner 			break;
20527d8d5feSDavid Chinner 		}
20627d8d5feSDavid Chinner 	}
20727d8d5feSDavid Chinner 	ASSERT(cur);
20827d8d5feSDavid Chinner }
20927d8d5feSDavid Chinner 
21027d8d5feSDavid Chinner /*
211249a8c11SDavid Chinner  * Return the item in the AIL with the current lsn.
212249a8c11SDavid Chinner  * Return the current tree generation number for use
213249a8c11SDavid Chinner  * in calls to xfs_trans_next_ail().
214249a8c11SDavid Chinner  */
215249a8c11SDavid Chinner STATIC xfs_log_item_t *
216249a8c11SDavid Chinner xfs_trans_first_push_ail(
21727d8d5feSDavid Chinner 	struct xfs_ail		*ailp,
21827d8d5feSDavid Chinner 	struct xfs_ail_cursor	*cur,
219249a8c11SDavid Chinner 	xfs_lsn_t		lsn)
220249a8c11SDavid Chinner {
221249a8c11SDavid Chinner 	xfs_log_item_t		*lip;
222249a8c11SDavid Chinner 
22327d8d5feSDavid Chinner 	lip = xfs_ail_min(ailp);
22427d8d5feSDavid Chinner 	xfs_trans_ail_cursor_set(ailp, cur, lip);
225249a8c11SDavid Chinner 	if (lsn == 0)
226249a8c11SDavid Chinner 		return lip;
227249a8c11SDavid Chinner 
22827d8d5feSDavid Chinner 	list_for_each_entry(lip, &ailp->xa_ail, li_ail) {
22927d8d5feSDavid Chinner 		if (XFS_LSN_CMP(lip->li_lsn, lsn) >= 0) {
23027d8d5feSDavid Chinner 			xfs_trans_ail_cursor_set(ailp, cur, lip);
231249a8c11SDavid Chinner 			return lip;
232249a8c11SDavid Chinner 		}
23327d8d5feSDavid Chinner 	}
234249a8c11SDavid Chinner 
235535f6b37SJosef 'Jeff' Sipek 	return NULL;
236535f6b37SJosef 'Jeff' Sipek }
237535f6b37SJosef 'Jeff' Sipek 
238249a8c11SDavid Chinner /*
239249a8c11SDavid Chinner  * Function that does the work of pushing on the AIL
240249a8c11SDavid Chinner  */
241249a8c11SDavid Chinner long
242249a8c11SDavid Chinner xfsaild_push(
24382fa9012SDavid Chinner 	struct xfs_ail	*ailp,
244249a8c11SDavid Chinner 	xfs_lsn_t	*last_lsn)
245249a8c11SDavid Chinner {
246249a8c11SDavid Chinner 	long		tout = 1000; /* milliseconds */
247249a8c11SDavid Chinner 	xfs_lsn_t	last_pushed_lsn = *last_lsn;
24882fa9012SDavid Chinner 	xfs_lsn_t	target =  ailp->xa_target;
2491da177e4SLinus Torvalds 	xfs_lsn_t	lsn;
2501da177e4SLinus Torvalds 	xfs_log_item_t	*lip;
251249a8c11SDavid Chinner 	int		flush_log, count, stuck;
25282fa9012SDavid Chinner 	xfs_mount_t	*mp = ailp->xa_mount;
25327d8d5feSDavid Chinner 	struct xfs_ail_cursor	*cur = &ailp->xa_cursors;
2541da177e4SLinus Torvalds 
255287f3dadSDonald Douwsma 	spin_lock(&mp->m_ail_lock);
25627d8d5feSDavid Chinner 	xfs_trans_ail_cursor_init(ailp, cur);
25727d8d5feSDavid Chinner 	lip = xfs_trans_first_push_ail(ailp, cur, *last_lsn);
258249a8c11SDavid Chinner 	if (!lip || XFS_FORCED_SHUTDOWN(mp)) {
2591da177e4SLinus Torvalds 		/*
260249a8c11SDavid Chinner 		 * AIL is empty or our push has reached the end.
2611da177e4SLinus Torvalds 		 */
26227d8d5feSDavid Chinner 		xfs_trans_ail_cursor_done(ailp, cur);
263287f3dadSDonald Douwsma 		spin_unlock(&mp->m_ail_lock);
264249a8c11SDavid Chinner 		last_pushed_lsn = 0;
26527d8d5feSDavid Chinner 		return tout;
2661da177e4SLinus Torvalds 	}
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 	XFS_STATS_INC(xs_push_ail);
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 	/*
2711da177e4SLinus Torvalds 	 * While the item we are looking at is below the given threshold
272249a8c11SDavid Chinner 	 * try to flush it out. We'd like not to stop until we've at least
2731da177e4SLinus Torvalds 	 * tried to push on everything in the AIL with an LSN less than
274249a8c11SDavid Chinner 	 * the given threshold.
2751da177e4SLinus Torvalds 	 *
276249a8c11SDavid Chinner 	 * However, we will stop after a certain number of pushes and wait
277249a8c11SDavid Chinner 	 * for a reduced timeout to fire before pushing further. This
278249a8c11SDavid Chinner 	 * prevents use from spinning when we can't do anything or there is
279249a8c11SDavid Chinner 	 * lots of contention on the AIL lists.
280249a8c11SDavid Chinner 	 */
281249a8c11SDavid Chinner 	tout = 10;
282249a8c11SDavid Chinner 	lsn = lip->li_lsn;
28327d8d5feSDavid Chinner 	flush_log = stuck = count = 0;
284249a8c11SDavid Chinner 	while ((XFS_LSN_CMP(lip->li_lsn, target) < 0)) {
285249a8c11SDavid Chinner 		int	lock_result;
286249a8c11SDavid Chinner 		/*
287249a8c11SDavid Chinner 		 * If we can lock the item without sleeping, unlock the AIL
288249a8c11SDavid Chinner 		 * lock and flush the item.  Then re-grab the AIL lock so we
289249a8c11SDavid Chinner 		 * can look for the next item on the AIL. List changes are
290249a8c11SDavid Chinner 		 * handled by the AIL lookup functions internally
291249a8c11SDavid Chinner 		 *
292249a8c11SDavid Chinner 		 * If we can't lock the item, either its holder will flush it
293249a8c11SDavid Chinner 		 * or it is already being flushed or it is being relogged.  In
294249a8c11SDavid Chinner 		 * any of these case it is being taken care of and we can just
295249a8c11SDavid Chinner 		 * skip to the next item in the list.
2961da177e4SLinus Torvalds 		 */
2971da177e4SLinus Torvalds 		lock_result = IOP_TRYLOCK(lip);
298249a8c11SDavid Chinner 		spin_unlock(&mp->m_ail_lock);
2991da177e4SLinus Torvalds 		switch (lock_result) {
3001da177e4SLinus Torvalds 		case XFS_ITEM_SUCCESS:
3011da177e4SLinus Torvalds 			XFS_STATS_INC(xs_push_ail_success);
3021da177e4SLinus Torvalds 			IOP_PUSH(lip);
303249a8c11SDavid Chinner 			last_pushed_lsn = lsn;
3041da177e4SLinus Torvalds 			break;
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 		case XFS_ITEM_PUSHBUF:
3071da177e4SLinus Torvalds 			XFS_STATS_INC(xs_push_ail_pushbuf);
3081da177e4SLinus Torvalds 			IOP_PUSHBUF(lip);
309249a8c11SDavid Chinner 			last_pushed_lsn = lsn;
3101da177e4SLinus Torvalds 			break;
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 		case XFS_ITEM_PINNED:
3131da177e4SLinus Torvalds 			XFS_STATS_INC(xs_push_ail_pinned);
314249a8c11SDavid Chinner 			stuck++;
3151da177e4SLinus Torvalds 			flush_log = 1;
3161da177e4SLinus Torvalds 			break;
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds 		case XFS_ITEM_LOCKED:
3191da177e4SLinus Torvalds 			XFS_STATS_INC(xs_push_ail_locked);
320249a8c11SDavid Chinner 			last_pushed_lsn = lsn;
321249a8c11SDavid Chinner 			stuck++;
3221da177e4SLinus Torvalds 			break;
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds 		case XFS_ITEM_FLUSHING:
3251da177e4SLinus Torvalds 			XFS_STATS_INC(xs_push_ail_flushing);
326249a8c11SDavid Chinner 			last_pushed_lsn = lsn;
327249a8c11SDavid Chinner 			stuck++;
3281da177e4SLinus Torvalds 			break;
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 		default:
3311da177e4SLinus Torvalds 			ASSERT(0);
3321da177e4SLinus Torvalds 			break;
3331da177e4SLinus Torvalds 		}
3341da177e4SLinus Torvalds 
335249a8c11SDavid Chinner 		spin_lock(&mp->m_ail_lock);
336249a8c11SDavid Chinner 		/* should we bother continuing? */
337249a8c11SDavid Chinner 		if (XFS_FORCED_SHUTDOWN(mp))
3381da177e4SLinus Torvalds 			break;
339249a8c11SDavid Chinner 		ASSERT(mp->m_log);
3401da177e4SLinus Torvalds 
341249a8c11SDavid Chinner 		count++;
342249a8c11SDavid Chinner 
343249a8c11SDavid Chinner 		/*
344249a8c11SDavid Chinner 		 * Are there too many items we can't do anything with?
345249a8c11SDavid Chinner 		 * If we we are skipping too many items because we can't flush
346249a8c11SDavid Chinner 		 * them or they are already being flushed, we back off and
347249a8c11SDavid Chinner 		 * given them time to complete whatever operation is being
348249a8c11SDavid Chinner 		 * done. i.e. remove pressure from the AIL while we can't make
349249a8c11SDavid Chinner 		 * progress so traversals don't slow down further inserts and
350249a8c11SDavid Chinner 		 * removals to/from the AIL.
351249a8c11SDavid Chinner 		 *
352249a8c11SDavid Chinner 		 * The value of 100 is an arbitrary magic number based on
353249a8c11SDavid Chinner 		 * observation.
354249a8c11SDavid Chinner 		 */
355249a8c11SDavid Chinner 		if (stuck > 100)
356249a8c11SDavid Chinner 			break;
357249a8c11SDavid Chinner 
35827d8d5feSDavid Chinner 		lip = xfs_trans_ail_cursor_next(ailp, cur);
359249a8c11SDavid Chinner 		if (lip == NULL)
360249a8c11SDavid Chinner 			break;
361249a8c11SDavid Chinner 		lsn = lip->li_lsn;
3621da177e4SLinus Torvalds 	}
36327d8d5feSDavid Chinner 	xfs_trans_ail_cursor_done(ailp, cur);
364249a8c11SDavid Chinner 	spin_unlock(&mp->m_ail_lock);
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds 	if (flush_log) {
3671da177e4SLinus Torvalds 		/*
3681da177e4SLinus Torvalds 		 * If something we need to push out was pinned, then
3691da177e4SLinus Torvalds 		 * push out the log so it will become unpinned and
3701da177e4SLinus Torvalds 		 * move forward in the AIL.
3711da177e4SLinus Torvalds 		 */
3721da177e4SLinus Torvalds 		XFS_STATS_INC(xs_push_ail_flush);
3731da177e4SLinus Torvalds 		xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
3741da177e4SLinus Torvalds 	}
3751da177e4SLinus Torvalds 
37692d9cd10SDavid Chinner 	if (!count) {
37792d9cd10SDavid Chinner 		/* We're past our target or empty, so idle */
37892d9cd10SDavid Chinner 		tout = 1000;
37992d9cd10SDavid Chinner 	} else if (XFS_LSN_CMP(lsn, target) >= 0) {
380249a8c11SDavid Chinner 		/*
38192d9cd10SDavid Chinner 		 * We reached the target so wait a bit longer for I/O to
38292d9cd10SDavid Chinner 		 * complete and remove pushed items from the AIL before we
38392d9cd10SDavid Chinner 		 * start the next scan from the start of the AIL.
384249a8c11SDavid Chinner 		 */
385249a8c11SDavid Chinner 		tout += 20;
386249a8c11SDavid Chinner 		last_pushed_lsn = 0;
38727d8d5feSDavid Chinner 	} else if ((stuck * 100) / count > 90) {
388249a8c11SDavid Chinner 		/*
389249a8c11SDavid Chinner 		 * Either there is a lot of contention on the AIL or we
390249a8c11SDavid Chinner 		 * are stuck due to operations in progress. "Stuck" in this
391249a8c11SDavid Chinner 		 * case is defined as >90% of the items we tried to push
392249a8c11SDavid Chinner 		 * were stuck.
393249a8c11SDavid Chinner 		 *
394249a8c11SDavid Chinner 		 * Backoff a bit more to allow some I/O to complete before
395249a8c11SDavid Chinner 		 * continuing from where we were.
396249a8c11SDavid Chinner 		 */
397249a8c11SDavid Chinner 		tout += 10;
3981da177e4SLinus Torvalds 	}
399249a8c11SDavid Chinner 	*last_lsn = last_pushed_lsn;
400249a8c11SDavid Chinner 	return tout;
401249a8c11SDavid Chinner }	/* xfsaild_push */
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds /*
4051da177e4SLinus Torvalds  * This is to be called when an item is unlocked that may have
4061da177e4SLinus Torvalds  * been in the AIL.  It will wake up the first member of the AIL
4071da177e4SLinus Torvalds  * wait list if this item's unlocking might allow it to progress.
4081da177e4SLinus Torvalds  * If the item is in the AIL, then we need to get the AIL lock
4091da177e4SLinus Torvalds  * while doing our checking so we don't race with someone going
4101da177e4SLinus Torvalds  * to sleep waiting for this event in xfs_trans_push_ail().
4111da177e4SLinus Torvalds  */
4121da177e4SLinus Torvalds void
4131da177e4SLinus Torvalds xfs_trans_unlocked_item(
4141da177e4SLinus Torvalds 	xfs_mount_t	*mp,
4151da177e4SLinus Torvalds 	xfs_log_item_t	*lip)
4161da177e4SLinus Torvalds {
4171da177e4SLinus Torvalds 	xfs_log_item_t	*min_lip;
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 	/*
4201da177e4SLinus Torvalds 	 * If we're forcibly shutting down, we may have
4211da177e4SLinus Torvalds 	 * unlocked log items arbitrarily. The last thing
4221da177e4SLinus Torvalds 	 * we want to do is to move the tail of the log
4231da177e4SLinus Torvalds 	 * over some potentially valid data.
4241da177e4SLinus Torvalds 	 */
4251da177e4SLinus Torvalds 	if (!(lip->li_flags & XFS_LI_IN_AIL) ||
4261da177e4SLinus Torvalds 	    XFS_FORCED_SHUTDOWN(mp)) {
4271da177e4SLinus Torvalds 		return;
4281da177e4SLinus Torvalds 	}
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 	/*
4311da177e4SLinus Torvalds 	 * This is the one case where we can call into xfs_ail_min()
4321da177e4SLinus Torvalds 	 * without holding the AIL lock because we only care about the
4331da177e4SLinus Torvalds 	 * case where we are at the tail of the AIL.  If the object isn't
4341da177e4SLinus Torvalds 	 * at the tail, it doesn't matter what result we get back.  This
4351da177e4SLinus Torvalds 	 * is slightly racy because since we were just unlocked, we could
4361da177e4SLinus Torvalds 	 * go to sleep between the call to xfs_ail_min and the call to
4371da177e4SLinus Torvalds 	 * xfs_log_move_tail, have someone else lock us, commit to us disk,
4381da177e4SLinus Torvalds 	 * move us out of the tail of the AIL, and then we wake up.  However,
4391da177e4SLinus Torvalds 	 * the call to xfs_log_move_tail() doesn't do anything if there's
4401da177e4SLinus Torvalds 	 * not enough free space to wake people up so we're safe calling it.
4411da177e4SLinus Torvalds 	 */
44282fa9012SDavid Chinner 	min_lip = xfs_ail_min(mp->m_ail);
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds 	if (min_lip == lip)
4451da177e4SLinus Torvalds 		xfs_log_move_tail(mp, 1);
4461da177e4SLinus Torvalds }	/* xfs_trans_unlocked_item */
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds /*
4501da177e4SLinus Torvalds  * Update the position of the item in the AIL with the new
4511da177e4SLinus Torvalds  * lsn.  If it is not yet in the AIL, add it.  Otherwise, move
4521da177e4SLinus Torvalds  * it to its new position by removing it and re-adding it.
4531da177e4SLinus Torvalds  *
4541da177e4SLinus Torvalds  * Wakeup anyone with an lsn less than the item's lsn.  If the item
4551da177e4SLinus Torvalds  * we move in the AIL is the minimum one, update the tail lsn in the
4561da177e4SLinus Torvalds  * log manager.
4571da177e4SLinus Torvalds  *
4581da177e4SLinus Torvalds  * This function must be called with the AIL lock held.  The lock
459287f3dadSDonald Douwsma  * is dropped before returning.
4601da177e4SLinus Torvalds  */
4611da177e4SLinus Torvalds void
4621da177e4SLinus Torvalds xfs_trans_update_ail(
4631da177e4SLinus Torvalds 	xfs_mount_t	*mp,
4641da177e4SLinus Torvalds 	xfs_log_item_t	*lip,
465287f3dadSDonald Douwsma 	xfs_lsn_t	lsn) __releases(mp->m_ail_lock)
4661da177e4SLinus Torvalds {
4671da177e4SLinus Torvalds 	xfs_log_item_t		*dlip=NULL;
4681da177e4SLinus Torvalds 	xfs_log_item_t		*mlip;	/* ptr to minimum lip */
4691da177e4SLinus Torvalds 
47082fa9012SDavid Chinner 	mlip = xfs_ail_min(mp->m_ail);
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds 	if (lip->li_flags & XFS_LI_IN_AIL) {
47382fa9012SDavid Chinner 		dlip = xfs_ail_delete(mp->m_ail, lip);
4741da177e4SLinus Torvalds 		ASSERT(dlip == lip);
47527d8d5feSDavid Chinner 		xfs_trans_ail_cursor_clear(mp->m_ail, dlip);
4761da177e4SLinus Torvalds 	} else {
4771da177e4SLinus Torvalds 		lip->li_flags |= XFS_LI_IN_AIL;
4781da177e4SLinus Torvalds 	}
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds 	lip->li_lsn = lsn;
48182fa9012SDavid Chinner 	xfs_ail_insert(mp->m_ail, lip);
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds 	if (mlip == dlip) {
48482fa9012SDavid Chinner 		mlip = xfs_ail_min(mp->m_ail);
485287f3dadSDonald Douwsma 		spin_unlock(&mp->m_ail_lock);
4861da177e4SLinus Torvalds 		xfs_log_move_tail(mp, mlip->li_lsn);
4871da177e4SLinus Torvalds 	} else {
488287f3dadSDonald Douwsma 		spin_unlock(&mp->m_ail_lock);
4891da177e4SLinus Torvalds 	}
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds }	/* xfs_trans_update_ail */
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds /*
4951da177e4SLinus Torvalds  * Delete the given item from the AIL.  It must already be in
4961da177e4SLinus Torvalds  * the AIL.
4971da177e4SLinus Torvalds  *
4981da177e4SLinus Torvalds  * Wakeup anyone with an lsn less than item's lsn.    If the item
4991da177e4SLinus Torvalds  * we delete in the AIL is the minimum one, update the tail lsn in the
5001da177e4SLinus Torvalds  * log manager.
5011da177e4SLinus Torvalds  *
5021da177e4SLinus Torvalds  * Clear the IN_AIL flag from the item, reset its lsn to 0, and
5031da177e4SLinus Torvalds  * bump the AIL's generation count to indicate that the tree
5041da177e4SLinus Torvalds  * has changed.
5051da177e4SLinus Torvalds  *
5061da177e4SLinus Torvalds  * This function must be called with the AIL lock held.  The lock
507287f3dadSDonald Douwsma  * is dropped before returning.
5081da177e4SLinus Torvalds  */
5091da177e4SLinus Torvalds void
5101da177e4SLinus Torvalds xfs_trans_delete_ail(
5111da177e4SLinus Torvalds 	xfs_mount_t	*mp,
512287f3dadSDonald Douwsma 	xfs_log_item_t	*lip) __releases(mp->m_ail_lock)
5131da177e4SLinus Torvalds {
5141da177e4SLinus Torvalds 	xfs_log_item_t		*dlip;
5151da177e4SLinus Torvalds 	xfs_log_item_t		*mlip;
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds 	if (lip->li_flags & XFS_LI_IN_AIL) {
51882fa9012SDavid Chinner 		mlip = xfs_ail_min(mp->m_ail);
51982fa9012SDavid Chinner 		dlip = xfs_ail_delete(mp->m_ail, lip);
5201da177e4SLinus Torvalds 		ASSERT(dlip == lip);
52127d8d5feSDavid Chinner 		xfs_trans_ail_cursor_clear(mp->m_ail, dlip);
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds 
5241da177e4SLinus Torvalds 		lip->li_flags &= ~XFS_LI_IN_AIL;
5251da177e4SLinus Torvalds 		lip->li_lsn = 0;
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds 		if (mlip == dlip) {
52882fa9012SDavid Chinner 			mlip = xfs_ail_min(mp->m_ail);
529287f3dadSDonald Douwsma 			spin_unlock(&mp->m_ail_lock);
5301da177e4SLinus Torvalds 			xfs_log_move_tail(mp, (mlip ? mlip->li_lsn : 0));
5311da177e4SLinus Torvalds 		} else {
532287f3dadSDonald Douwsma 			spin_unlock(&mp->m_ail_lock);
5331da177e4SLinus Torvalds 		}
5341da177e4SLinus Torvalds 	}
5351da177e4SLinus Torvalds 	else {
5361da177e4SLinus Torvalds 		/*
5371da177e4SLinus Torvalds 		 * If the file system is not being shutdown, we are in
5381da177e4SLinus Torvalds 		 * serious trouble if we get to this stage.
5391da177e4SLinus Torvalds 		 */
5401da177e4SLinus Torvalds 		if (XFS_FORCED_SHUTDOWN(mp))
541287f3dadSDonald Douwsma 			spin_unlock(&mp->m_ail_lock);
5421da177e4SLinus Torvalds 		else {
5431da177e4SLinus Torvalds 			xfs_cmn_err(XFS_PTAG_AILDELETE, CE_ALERT, mp,
5447d04a335SNathan Scott 		"%s: attempting to delete a log item that is not in the AIL",
54534a622b2SHarvey Harrison 					__func__);
546287f3dadSDonald Douwsma 			spin_unlock(&mp->m_ail_lock);
5477d04a335SNathan Scott 			xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
5481da177e4SLinus Torvalds 		}
5491da177e4SLinus Torvalds 	}
5501da177e4SLinus Torvalds }
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds 
5541da177e4SLinus Torvalds /*
5551da177e4SLinus Torvalds  * Return the item in the AIL with the smallest lsn.
5561da177e4SLinus Torvalds  * Return the current tree generation number for use
5571da177e4SLinus Torvalds  * in calls to xfs_trans_next_ail().
5581da177e4SLinus Torvalds  */
5591da177e4SLinus Torvalds xfs_log_item_t *
5601da177e4SLinus Torvalds xfs_trans_first_ail(
56127d8d5feSDavid Chinner 	struct xfs_mount	*mp,
56227d8d5feSDavid Chinner 	struct xfs_ail_cursor	*cur)
5631da177e4SLinus Torvalds {
5641da177e4SLinus Torvalds 	xfs_log_item_t		*lip;
56527d8d5feSDavid Chinner 	struct xfs_ail		*ailp = mp->m_ail;
5661da177e4SLinus Torvalds 
56727d8d5feSDavid Chinner 	lip = xfs_ail_min(ailp);
56827d8d5feSDavid Chinner 	xfs_trans_ail_cursor_set(ailp, cur, lip);
5691da177e4SLinus Torvalds 
570249a8c11SDavid Chinner 	return lip;
5711da177e4SLinus Torvalds }
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds /*
57427d8d5feSDavid Chinner  * Grab the next item in the AIL from the cursor passed in.
5751da177e4SLinus Torvalds  */
5761da177e4SLinus Torvalds xfs_log_item_t *
5771da177e4SLinus Torvalds xfs_trans_next_ail(
57827d8d5feSDavid Chinner 	struct xfs_mount	*mp,
57927d8d5feSDavid Chinner 	struct xfs_ail_cursor	*cur)
5801da177e4SLinus Torvalds {
58127d8d5feSDavid Chinner 	struct xfs_ail		*ailp = mp->m_ail;
5821da177e4SLinus Torvalds 
58327d8d5feSDavid Chinner 	return xfs_trans_ail_cursor_next(ailp, cur);
5841da177e4SLinus Torvalds }
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds /*
5881da177e4SLinus Torvalds  * The active item list (AIL) is a doubly linked list of log
5891da177e4SLinus Torvalds  * items sorted by ascending lsn.  The base of the list is
5901da177e4SLinus Torvalds  * a forw/back pointer pair embedded in the xfs mount structure.
5911da177e4SLinus Torvalds  * The base is initialized with both pointers pointing to the
5921da177e4SLinus Torvalds  * base.  This case always needs to be distinguished, because
5931da177e4SLinus Torvalds  * the base has no lsn to look at.  We almost always insert
5941da177e4SLinus Torvalds  * at the end of the list, so on inserts we search from the
5951da177e4SLinus Torvalds  * end of the list to find where the new item belongs.
5961da177e4SLinus Torvalds  */
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds /*
5991da177e4SLinus Torvalds  * Initialize the doubly linked list to point only to itself.
6001da177e4SLinus Torvalds  */
601249a8c11SDavid Chinner int
6021da177e4SLinus Torvalds xfs_trans_ail_init(
6031da177e4SLinus Torvalds 	xfs_mount_t	*mp)
6041da177e4SLinus Torvalds {
60582fa9012SDavid Chinner 	struct xfs_ail	*ailp;
60627d8d5feSDavid Chinner 	int		error;
60782fa9012SDavid Chinner 
60882fa9012SDavid Chinner 	ailp = kmem_zalloc(sizeof(struct xfs_ail), KM_MAYFAIL);
60982fa9012SDavid Chinner 	if (!ailp)
61082fa9012SDavid Chinner 		return ENOMEM;
61182fa9012SDavid Chinner 
61282fa9012SDavid Chinner 	ailp->xa_mount = mp;
61382fa9012SDavid Chinner 	INIT_LIST_HEAD(&ailp->xa_ail);
61427d8d5feSDavid Chinner 	error = xfsaild_start(ailp);
61527d8d5feSDavid Chinner 	if (error)
61627d8d5feSDavid Chinner 		goto out_free_ailp;
61727d8d5feSDavid Chinner 	mp->m_ail = ailp;
61827d8d5feSDavid Chinner 	return 0;
61927d8d5feSDavid Chinner 
62027d8d5feSDavid Chinner out_free_ailp:
62127d8d5feSDavid Chinner 	kmem_free(ailp);
62227d8d5feSDavid Chinner 	return error;
623249a8c11SDavid Chinner }
624249a8c11SDavid Chinner 
625249a8c11SDavid Chinner void
626249a8c11SDavid Chinner xfs_trans_ail_destroy(
627249a8c11SDavid Chinner 	xfs_mount_t	*mp)
628249a8c11SDavid Chinner {
62982fa9012SDavid Chinner 	struct xfs_ail	*ailp = mp->m_ail;
63082fa9012SDavid Chinner 
63182fa9012SDavid Chinner 	xfsaild_stop(ailp);
63282fa9012SDavid Chinner 	kmem_free(ailp);
6331da177e4SLinus Torvalds }
6341da177e4SLinus Torvalds 
6351da177e4SLinus Torvalds /*
6361da177e4SLinus Torvalds  * Insert the given log item into the AIL.
6371da177e4SLinus Torvalds  * We almost always insert at the end of the list, so on inserts
6381da177e4SLinus Torvalds  * we search from the end of the list to find where the
6391da177e4SLinus Torvalds  * new item belongs.
6401da177e4SLinus Torvalds  */
6411da177e4SLinus Torvalds STATIC void
6421da177e4SLinus Torvalds xfs_ail_insert(
64382fa9012SDavid Chinner 	struct xfs_ail	*ailp,
6441da177e4SLinus Torvalds 	xfs_log_item_t	*lip)
6451da177e4SLinus Torvalds /* ARGSUSED */
6461da177e4SLinus Torvalds {
6471da177e4SLinus Torvalds 	xfs_log_item_t	*next_lip;
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 	/*
6501da177e4SLinus Torvalds 	 * If the list is empty, just insert the item.
6511da177e4SLinus Torvalds 	 */
652535f6b37SJosef 'Jeff' Sipek 	if (list_empty(&ailp->xa_ail)) {
653535f6b37SJosef 'Jeff' Sipek 		list_add(&lip->li_ail, &ailp->xa_ail);
6541da177e4SLinus Torvalds 		return;
6551da177e4SLinus Torvalds 	}
6561da177e4SLinus Torvalds 
657535f6b37SJosef 'Jeff' Sipek 	list_for_each_entry_reverse(next_lip, &ailp->xa_ail, li_ail) {
658535f6b37SJosef 'Jeff' Sipek 		if (XFS_LSN_CMP(next_lip->li_lsn, lip->li_lsn) <= 0)
659535f6b37SJosef 'Jeff' Sipek 			break;
6601da177e4SLinus Torvalds 	}
6611da177e4SLinus Torvalds 
662535f6b37SJosef 'Jeff' Sipek 	ASSERT((&next_lip->li_ail == &ailp->xa_ail) ||
663535f6b37SJosef 'Jeff' Sipek 	       (XFS_LSN_CMP(next_lip->li_lsn, lip->li_lsn) <= 0));
664535f6b37SJosef 'Jeff' Sipek 
665535f6b37SJosef 'Jeff' Sipek 	list_add(&lip->li_ail, &next_lip->li_ail);
666535f6b37SJosef 'Jeff' Sipek 
667535f6b37SJosef 'Jeff' Sipek 	xfs_ail_check(ailp, lip);
6681da177e4SLinus Torvalds 	return;
6691da177e4SLinus Torvalds }
6701da177e4SLinus Torvalds 
6711da177e4SLinus Torvalds /*
6721da177e4SLinus Torvalds  * Delete the given item from the AIL.  Return a pointer to the item.
6731da177e4SLinus Torvalds  */
6741da177e4SLinus Torvalds /*ARGSUSED*/
6751da177e4SLinus Torvalds STATIC xfs_log_item_t *
6761da177e4SLinus Torvalds xfs_ail_delete(
67782fa9012SDavid Chinner 	struct xfs_ail	*ailp,
6781da177e4SLinus Torvalds 	xfs_log_item_t	*lip)
6791da177e4SLinus Torvalds /* ARGSUSED */
6801da177e4SLinus Torvalds {
681535f6b37SJosef 'Jeff' Sipek 	xfs_ail_check(ailp, lip);
682535f6b37SJosef 'Jeff' Sipek 
683535f6b37SJosef 'Jeff' Sipek 	list_del(&lip->li_ail);
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds 	return lip;
6861da177e4SLinus Torvalds }
6871da177e4SLinus Torvalds 
6881da177e4SLinus Torvalds /*
6891da177e4SLinus Torvalds  * Return a pointer to the first item in the AIL.
6901da177e4SLinus Torvalds  * If the AIL is empty, then return NULL.
6911da177e4SLinus Torvalds  */
6921da177e4SLinus Torvalds STATIC xfs_log_item_t *
6931da177e4SLinus Torvalds xfs_ail_min(
69482fa9012SDavid Chinner 	struct xfs_ail	*ailp)
6951da177e4SLinus Torvalds /* ARGSUSED */
6961da177e4SLinus Torvalds {
697535f6b37SJosef 'Jeff' Sipek 	if (list_empty(&ailp->xa_ail))
6981da177e4SLinus Torvalds 		return NULL;
699535f6b37SJosef 'Jeff' Sipek 
700535f6b37SJosef 'Jeff' Sipek 	return list_first_entry(&ailp->xa_ail, xfs_log_item_t, li_ail);
7011da177e4SLinus Torvalds }
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds /*
7041da177e4SLinus Torvalds  * Return a pointer to the item which follows
7051da177e4SLinus Torvalds  * the given item in the AIL.  If the given item
7061da177e4SLinus Torvalds  * is the last item in the list, then return NULL.
7071da177e4SLinus Torvalds  */
7081da177e4SLinus Torvalds STATIC xfs_log_item_t *
7091da177e4SLinus Torvalds xfs_ail_next(
71082fa9012SDavid Chinner 	struct xfs_ail	*ailp,
7111da177e4SLinus Torvalds 	xfs_log_item_t	*lip)
7121da177e4SLinus Torvalds /* ARGSUSED */
7131da177e4SLinus Torvalds {
714535f6b37SJosef 'Jeff' Sipek 	if (lip->li_ail.next == &ailp->xa_ail)
7151da177e4SLinus Torvalds 		return NULL;
7161da177e4SLinus Torvalds 
717535f6b37SJosef 'Jeff' Sipek 	return list_first_entry(&lip->li_ail, xfs_log_item_t, li_ail);
7181da177e4SLinus Torvalds }
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds #ifdef DEBUG
7211da177e4SLinus Torvalds /*
7221da177e4SLinus Torvalds  * Check that the list is sorted as it should be.
7231da177e4SLinus Torvalds  */
7241da177e4SLinus Torvalds STATIC void
7251da177e4SLinus Torvalds xfs_ail_check(
72682fa9012SDavid Chinner 	struct xfs_ail	*ailp,
727de08dbc1SDavid Chinner 	xfs_log_item_t	*lip)
7281da177e4SLinus Torvalds {
7291da177e4SLinus Torvalds 	xfs_log_item_t	*prev_lip;
7301da177e4SLinus Torvalds 
731535f6b37SJosef 'Jeff' Sipek 	if (list_empty(&ailp->xa_ail))
7321da177e4SLinus Torvalds 		return;
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds 	/*
735de08dbc1SDavid Chinner 	 * Check the next and previous entries are valid.
736de08dbc1SDavid Chinner 	 */
737de08dbc1SDavid Chinner 	ASSERT((lip->li_flags & XFS_LI_IN_AIL) != 0);
738535f6b37SJosef 'Jeff' Sipek 	prev_lip = list_entry(lip->li_ail.prev, xfs_log_item_t, li_ail);
739535f6b37SJosef 'Jeff' Sipek 	if (&prev_lip->li_ail != &ailp->xa_ail)
740de08dbc1SDavid Chinner 		ASSERT(XFS_LSN_CMP(prev_lip->li_lsn, lip->li_lsn) <= 0);
741535f6b37SJosef 'Jeff' Sipek 
742535f6b37SJosef 'Jeff' Sipek 	prev_lip = list_entry(lip->li_ail.next, xfs_log_item_t, li_ail);
743535f6b37SJosef 'Jeff' Sipek 	if (&prev_lip->li_ail != &ailp->xa_ail)
744de08dbc1SDavid Chinner 		ASSERT(XFS_LSN_CMP(prev_lip->li_lsn, lip->li_lsn) >= 0);
745de08dbc1SDavid Chinner 
746de08dbc1SDavid Chinner 
747de08dbc1SDavid Chinner #ifdef XFS_TRANS_DEBUG
748de08dbc1SDavid Chinner 	/*
749535f6b37SJosef 'Jeff' Sipek 	 * Walk the list checking lsn ordering, and that every entry has the
750535f6b37SJosef 'Jeff' Sipek 	 * XFS_LI_IN_AIL flag set. This is really expensive, so only do it
751535f6b37SJosef 'Jeff' Sipek 	 * when specifically debugging the transaction subsystem.
7521da177e4SLinus Torvalds 	 */
753535f6b37SJosef 'Jeff' Sipek 	prev_lip = list_entry(&ailp->xa_ail, xfs_log_item_t, li_ail);
754535f6b37SJosef 'Jeff' Sipek 	list_for_each_entry(lip, &ailp->xa_ail, li_ail) {
755535f6b37SJosef 'Jeff' Sipek 		if (&prev_lip->li_ail != &ailp->xa_ail)
7561da177e4SLinus Torvalds 			ASSERT(XFS_LSN_CMP(prev_lip->li_lsn, lip->li_lsn) <= 0);
7571da177e4SLinus Torvalds 		ASSERT((lip->li_flags & XFS_LI_IN_AIL) != 0);
7581da177e4SLinus Torvalds 		prev_lip = lip;
7591da177e4SLinus Torvalds 	}
760de08dbc1SDavid Chinner #endif /* XFS_TRANS_DEBUG */
7611da177e4SLinus Torvalds }
7621da177e4SLinus Torvalds #endif /* DEBUG */
763