xref: /openbmc/linux/fs/xfs/xfs_trans_ail.c (revision 535f6b37)
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 
31535f6b37SJosef 'Jeff' Sipek STATIC void xfs_ail_insert(xfs_ail_t *, xfs_log_item_t *);
32535f6b37SJosef 'Jeff' Sipek STATIC xfs_log_item_t * xfs_ail_delete(xfs_ail_t *, xfs_log_item_t *);
33535f6b37SJosef 'Jeff' Sipek STATIC xfs_log_item_t * xfs_ail_min(xfs_ail_t *);
34535f6b37SJosef 'Jeff' Sipek STATIC xfs_log_item_t * xfs_ail_next(xfs_ail_t *, xfs_log_item_t *);
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds #ifdef DEBUG
37535f6b37SJosef 'Jeff' Sipek STATIC void xfs_ail_check(xfs_ail_t *, 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);
60535f6b37SJosef 'Jeff' Sipek 	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 
94535f6b37SJosef 'Jeff' Sipek 	lip = xfs_ail_min(&mp->m_ail);
95249a8c11SDavid Chinner 	if (lip && !XFS_FORCED_SHUTDOWN(mp)) {
96249a8c11SDavid Chinner 		if (XFS_LSN_CMP(threshold_lsn, mp->m_ail.xa_target) > 0)
97249a8c11SDavid Chinner 			xfsaild_wakeup(mp, threshold_lsn);
98249a8c11SDavid Chinner 	}
99249a8c11SDavid Chinner }
100249a8c11SDavid Chinner 
101249a8c11SDavid Chinner /*
102249a8c11SDavid Chinner  * Return the item in the AIL with the current lsn.
103249a8c11SDavid Chinner  * Return the current tree generation number for use
104249a8c11SDavid Chinner  * in calls to xfs_trans_next_ail().
105249a8c11SDavid Chinner  */
106249a8c11SDavid Chinner STATIC xfs_log_item_t *
107249a8c11SDavid Chinner xfs_trans_first_push_ail(
108249a8c11SDavid Chinner 	xfs_mount_t	*mp,
109249a8c11SDavid Chinner 	int		*gen,
110249a8c11SDavid Chinner 	xfs_lsn_t	lsn)
111249a8c11SDavid Chinner {
112249a8c11SDavid Chinner 	xfs_log_item_t	*lip;
113249a8c11SDavid Chinner 
114535f6b37SJosef 'Jeff' Sipek 	lip = xfs_ail_min(&mp->m_ail);
115249a8c11SDavid Chinner 	*gen = (int)mp->m_ail.xa_gen;
116249a8c11SDavid Chinner 	if (lsn == 0)
117249a8c11SDavid Chinner 		return lip;
118249a8c11SDavid Chinner 
119535f6b37SJosef 'Jeff' Sipek 	list_for_each_entry(lip, &mp->m_ail.xa_ail, li_ail) {
120535f6b37SJosef 'Jeff' Sipek 		if (XFS_LSN_CMP(lip->li_lsn, lsn) >= 0)
121249a8c11SDavid Chinner 			return lip;
122249a8c11SDavid Chinner 	}
123249a8c11SDavid Chinner 
124535f6b37SJosef 'Jeff' Sipek 	return NULL;
125535f6b37SJosef 'Jeff' Sipek }
126535f6b37SJosef 'Jeff' Sipek 
127249a8c11SDavid Chinner /*
128249a8c11SDavid Chinner  * Function that does the work of pushing on the AIL
129249a8c11SDavid Chinner  */
130249a8c11SDavid Chinner long
131249a8c11SDavid Chinner xfsaild_push(
132249a8c11SDavid Chinner 	xfs_mount_t	*mp,
133249a8c11SDavid Chinner 	xfs_lsn_t	*last_lsn)
134249a8c11SDavid Chinner {
135249a8c11SDavid Chinner 	long		tout = 1000; /* milliseconds */
136249a8c11SDavid Chinner 	xfs_lsn_t	last_pushed_lsn = *last_lsn;
137249a8c11SDavid Chinner 	xfs_lsn_t	target =  mp->m_ail.xa_target;
1381da177e4SLinus Torvalds 	xfs_lsn_t	lsn;
1391da177e4SLinus Torvalds 	xfs_log_item_t	*lip;
1401da177e4SLinus Torvalds 	int		gen;
1411da177e4SLinus Torvalds 	int		restarts;
142249a8c11SDavid Chinner 	int		flush_log, count, stuck;
1431da177e4SLinus Torvalds 
144249a8c11SDavid Chinner #define	XFS_TRANS_PUSH_AIL_RESTARTS	10
1451da177e4SLinus Torvalds 
146287f3dadSDonald Douwsma 	spin_lock(&mp->m_ail_lock);
147249a8c11SDavid Chinner 	lip = xfs_trans_first_push_ail(mp, &gen, *last_lsn);
148249a8c11SDavid Chinner 	if (!lip || XFS_FORCED_SHUTDOWN(mp)) {
1491da177e4SLinus Torvalds 		/*
150249a8c11SDavid Chinner 		 * AIL is empty or our push has reached the end.
1511da177e4SLinus Torvalds 		 */
152287f3dadSDonald Douwsma 		spin_unlock(&mp->m_ail_lock);
153249a8c11SDavid Chinner 		last_pushed_lsn = 0;
154249a8c11SDavid Chinner 		goto out;
1551da177e4SLinus Torvalds 	}
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds 	XFS_STATS_INC(xs_push_ail);
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 	/*
1601da177e4SLinus Torvalds 	 * While the item we are looking at is below the given threshold
161249a8c11SDavid Chinner 	 * try to flush it out. We'd like not to stop until we've at least
1621da177e4SLinus Torvalds 	 * tried to push on everything in the AIL with an LSN less than
163249a8c11SDavid Chinner 	 * the given threshold.
1641da177e4SLinus Torvalds 	 *
165249a8c11SDavid Chinner 	 * However, we will stop after a certain number of pushes and wait
166249a8c11SDavid Chinner 	 * for a reduced timeout to fire before pushing further. This
167249a8c11SDavid Chinner 	 * prevents use from spinning when we can't do anything or there is
168249a8c11SDavid Chinner 	 * lots of contention on the AIL lists.
169249a8c11SDavid Chinner 	 */
170249a8c11SDavid Chinner 	tout = 10;
171249a8c11SDavid Chinner 	lsn = lip->li_lsn;
172249a8c11SDavid Chinner 	flush_log = stuck = count = restarts = 0;
173249a8c11SDavid Chinner 	while ((XFS_LSN_CMP(lip->li_lsn, target) < 0)) {
174249a8c11SDavid Chinner 		int	lock_result;
175249a8c11SDavid Chinner 		/*
176249a8c11SDavid Chinner 		 * If we can lock the item without sleeping, unlock the AIL
177249a8c11SDavid Chinner 		 * lock and flush the item.  Then re-grab the AIL lock so we
178249a8c11SDavid Chinner 		 * can look for the next item on the AIL. List changes are
179249a8c11SDavid Chinner 		 * handled by the AIL lookup functions internally
180249a8c11SDavid Chinner 		 *
181249a8c11SDavid Chinner 		 * If we can't lock the item, either its holder will flush it
182249a8c11SDavid Chinner 		 * or it is already being flushed or it is being relogged.  In
183249a8c11SDavid Chinner 		 * any of these case it is being taken care of and we can just
184249a8c11SDavid Chinner 		 * skip to the next item in the list.
1851da177e4SLinus Torvalds 		 */
1861da177e4SLinus Torvalds 		lock_result = IOP_TRYLOCK(lip);
187249a8c11SDavid Chinner 		spin_unlock(&mp->m_ail_lock);
1881da177e4SLinus Torvalds 		switch (lock_result) {
1891da177e4SLinus Torvalds 		case XFS_ITEM_SUCCESS:
1901da177e4SLinus Torvalds 			XFS_STATS_INC(xs_push_ail_success);
1911da177e4SLinus Torvalds 			IOP_PUSH(lip);
192249a8c11SDavid Chinner 			last_pushed_lsn = lsn;
1931da177e4SLinus Torvalds 			break;
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 		case XFS_ITEM_PUSHBUF:
1961da177e4SLinus Torvalds 			XFS_STATS_INC(xs_push_ail_pushbuf);
1971da177e4SLinus Torvalds 			IOP_PUSHBUF(lip);
198249a8c11SDavid Chinner 			last_pushed_lsn = lsn;
1991da177e4SLinus Torvalds 			break;
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 		case XFS_ITEM_PINNED:
2021da177e4SLinus Torvalds 			XFS_STATS_INC(xs_push_ail_pinned);
203249a8c11SDavid Chinner 			stuck++;
2041da177e4SLinus Torvalds 			flush_log = 1;
2051da177e4SLinus Torvalds 			break;
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds 		case XFS_ITEM_LOCKED:
2081da177e4SLinus Torvalds 			XFS_STATS_INC(xs_push_ail_locked);
209249a8c11SDavid Chinner 			last_pushed_lsn = lsn;
210249a8c11SDavid Chinner 			stuck++;
2111da177e4SLinus Torvalds 			break;
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds 		case XFS_ITEM_FLUSHING:
2141da177e4SLinus Torvalds 			XFS_STATS_INC(xs_push_ail_flushing);
215249a8c11SDavid Chinner 			last_pushed_lsn = lsn;
216249a8c11SDavid Chinner 			stuck++;
2171da177e4SLinus Torvalds 			break;
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 		default:
2201da177e4SLinus Torvalds 			ASSERT(0);
2211da177e4SLinus Torvalds 			break;
2221da177e4SLinus Torvalds 		}
2231da177e4SLinus Torvalds 
224249a8c11SDavid Chinner 		spin_lock(&mp->m_ail_lock);
225249a8c11SDavid Chinner 		/* should we bother continuing? */
226249a8c11SDavid Chinner 		if (XFS_FORCED_SHUTDOWN(mp))
2271da177e4SLinus Torvalds 			break;
228249a8c11SDavid Chinner 		ASSERT(mp->m_log);
2291da177e4SLinus Torvalds 
230249a8c11SDavid Chinner 		count++;
231249a8c11SDavid Chinner 
232249a8c11SDavid Chinner 		/*
233249a8c11SDavid Chinner 		 * Are there too many items we can't do anything with?
234249a8c11SDavid Chinner 		 * If we we are skipping too many items because we can't flush
235249a8c11SDavid Chinner 		 * them or they are already being flushed, we back off and
236249a8c11SDavid Chinner 		 * given them time to complete whatever operation is being
237249a8c11SDavid Chinner 		 * done. i.e. remove pressure from the AIL while we can't make
238249a8c11SDavid Chinner 		 * progress so traversals don't slow down further inserts and
239249a8c11SDavid Chinner 		 * removals to/from the AIL.
240249a8c11SDavid Chinner 		 *
241249a8c11SDavid Chinner 		 * The value of 100 is an arbitrary magic number based on
242249a8c11SDavid Chinner 		 * observation.
243249a8c11SDavid Chinner 		 */
244249a8c11SDavid Chinner 		if (stuck > 100)
245249a8c11SDavid Chinner 			break;
246249a8c11SDavid Chinner 
247249a8c11SDavid Chinner 		lip = xfs_trans_next_ail(mp, lip, &gen, &restarts);
248249a8c11SDavid Chinner 		if (lip == NULL)
249249a8c11SDavid Chinner 			break;
250249a8c11SDavid Chinner 		if (restarts > XFS_TRANS_PUSH_AIL_RESTARTS)
251249a8c11SDavid Chinner 			break;
252249a8c11SDavid Chinner 		lsn = lip->li_lsn;
2531da177e4SLinus Torvalds 	}
254249a8c11SDavid Chinner 	spin_unlock(&mp->m_ail_lock);
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 	if (flush_log) {
2571da177e4SLinus Torvalds 		/*
2581da177e4SLinus Torvalds 		 * If something we need to push out was pinned, then
2591da177e4SLinus Torvalds 		 * push out the log so it will become unpinned and
2601da177e4SLinus Torvalds 		 * move forward in the AIL.
2611da177e4SLinus Torvalds 		 */
2621da177e4SLinus Torvalds 		XFS_STATS_INC(xs_push_ail_flush);
2631da177e4SLinus Torvalds 		xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
2641da177e4SLinus Torvalds 	}
2651da177e4SLinus Torvalds 
26692d9cd10SDavid Chinner 	if (!count) {
26792d9cd10SDavid Chinner 		/* We're past our target or empty, so idle */
26892d9cd10SDavid Chinner 		tout = 1000;
26992d9cd10SDavid Chinner 	} else if (XFS_LSN_CMP(lsn, target) >= 0) {
270249a8c11SDavid Chinner 		/*
27192d9cd10SDavid Chinner 		 * We reached the target so wait a bit longer for I/O to
27292d9cd10SDavid Chinner 		 * complete and remove pushed items from the AIL before we
27392d9cd10SDavid Chinner 		 * start the next scan from the start of the AIL.
274249a8c11SDavid Chinner 		 */
275249a8c11SDavid Chinner 		tout += 20;
276249a8c11SDavid Chinner 		last_pushed_lsn = 0;
277249a8c11SDavid Chinner 	} else if ((restarts > XFS_TRANS_PUSH_AIL_RESTARTS) ||
27892d9cd10SDavid Chinner 		   ((stuck * 100) / count > 90)) {
279249a8c11SDavid Chinner 		/*
280249a8c11SDavid Chinner 		 * Either there is a lot of contention on the AIL or we
281249a8c11SDavid Chinner 		 * are stuck due to operations in progress. "Stuck" in this
282249a8c11SDavid Chinner 		 * case is defined as >90% of the items we tried to push
283249a8c11SDavid Chinner 		 * were stuck.
284249a8c11SDavid Chinner 		 *
285249a8c11SDavid Chinner 		 * Backoff a bit more to allow some I/O to complete before
286249a8c11SDavid Chinner 		 * continuing from where we were.
287249a8c11SDavid Chinner 		 */
288249a8c11SDavid Chinner 		tout += 10;
2891da177e4SLinus Torvalds 	}
290249a8c11SDavid Chinner out:
291249a8c11SDavid Chinner 	*last_lsn = last_pushed_lsn;
292249a8c11SDavid Chinner 	return tout;
293249a8c11SDavid Chinner }	/* xfsaild_push */
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds /*
2971da177e4SLinus Torvalds  * This is to be called when an item is unlocked that may have
2981da177e4SLinus Torvalds  * been in the AIL.  It will wake up the first member of the AIL
2991da177e4SLinus Torvalds  * wait list if this item's unlocking might allow it to progress.
3001da177e4SLinus Torvalds  * If the item is in the AIL, then we need to get the AIL lock
3011da177e4SLinus Torvalds  * while doing our checking so we don't race with someone going
3021da177e4SLinus Torvalds  * to sleep waiting for this event in xfs_trans_push_ail().
3031da177e4SLinus Torvalds  */
3041da177e4SLinus Torvalds void
3051da177e4SLinus Torvalds xfs_trans_unlocked_item(
3061da177e4SLinus Torvalds 	xfs_mount_t	*mp,
3071da177e4SLinus Torvalds 	xfs_log_item_t	*lip)
3081da177e4SLinus Torvalds {
3091da177e4SLinus Torvalds 	xfs_log_item_t	*min_lip;
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds 	/*
3121da177e4SLinus Torvalds 	 * If we're forcibly shutting down, we may have
3131da177e4SLinus Torvalds 	 * unlocked log items arbitrarily. The last thing
3141da177e4SLinus Torvalds 	 * we want to do is to move the tail of the log
3151da177e4SLinus Torvalds 	 * over some potentially valid data.
3161da177e4SLinus Torvalds 	 */
3171da177e4SLinus Torvalds 	if (!(lip->li_flags & XFS_LI_IN_AIL) ||
3181da177e4SLinus Torvalds 	    XFS_FORCED_SHUTDOWN(mp)) {
3191da177e4SLinus Torvalds 		return;
3201da177e4SLinus Torvalds 	}
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds 	/*
3231da177e4SLinus Torvalds 	 * This is the one case where we can call into xfs_ail_min()
3241da177e4SLinus Torvalds 	 * without holding the AIL lock because we only care about the
3251da177e4SLinus Torvalds 	 * case where we are at the tail of the AIL.  If the object isn't
3261da177e4SLinus Torvalds 	 * at the tail, it doesn't matter what result we get back.  This
3271da177e4SLinus Torvalds 	 * is slightly racy because since we were just unlocked, we could
3281da177e4SLinus Torvalds 	 * go to sleep between the call to xfs_ail_min and the call to
3291da177e4SLinus Torvalds 	 * xfs_log_move_tail, have someone else lock us, commit to us disk,
3301da177e4SLinus Torvalds 	 * move us out of the tail of the AIL, and then we wake up.  However,
3311da177e4SLinus Torvalds 	 * the call to xfs_log_move_tail() doesn't do anything if there's
3321da177e4SLinus Torvalds 	 * not enough free space to wake people up so we're safe calling it.
3331da177e4SLinus Torvalds 	 */
334535f6b37SJosef 'Jeff' Sipek 	min_lip = xfs_ail_min(&mp->m_ail);
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	if (min_lip == lip)
3371da177e4SLinus Torvalds 		xfs_log_move_tail(mp, 1);
3381da177e4SLinus Torvalds }	/* xfs_trans_unlocked_item */
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds /*
3421da177e4SLinus Torvalds  * Update the position of the item in the AIL with the new
3431da177e4SLinus Torvalds  * lsn.  If it is not yet in the AIL, add it.  Otherwise, move
3441da177e4SLinus Torvalds  * it to its new position by removing it and re-adding it.
3451da177e4SLinus Torvalds  *
3461da177e4SLinus Torvalds  * Wakeup anyone with an lsn less than the item's lsn.  If the item
3471da177e4SLinus Torvalds  * we move in the AIL is the minimum one, update the tail lsn in the
3481da177e4SLinus Torvalds  * log manager.
3491da177e4SLinus Torvalds  *
3501da177e4SLinus Torvalds  * Increment the AIL's generation count to indicate that the tree
3511da177e4SLinus Torvalds  * has changed.
3521da177e4SLinus Torvalds  *
3531da177e4SLinus Torvalds  * This function must be called with the AIL lock held.  The lock
354287f3dadSDonald Douwsma  * is dropped before returning.
3551da177e4SLinus Torvalds  */
3561da177e4SLinus Torvalds void
3571da177e4SLinus Torvalds xfs_trans_update_ail(
3581da177e4SLinus Torvalds 	xfs_mount_t	*mp,
3591da177e4SLinus Torvalds 	xfs_log_item_t	*lip,
360287f3dadSDonald Douwsma 	xfs_lsn_t	lsn) __releases(mp->m_ail_lock)
3611da177e4SLinus Torvalds {
3621da177e4SLinus Torvalds 	xfs_log_item_t		*dlip=NULL;
3631da177e4SLinus Torvalds 	xfs_log_item_t		*mlip;	/* ptr to minimum lip */
3641da177e4SLinus Torvalds 
365535f6b37SJosef 'Jeff' Sipek 	mlip = xfs_ail_min(&mp->m_ail);
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 	if (lip->li_flags & XFS_LI_IN_AIL) {
368535f6b37SJosef 'Jeff' Sipek 		dlip = xfs_ail_delete(&mp->m_ail, lip);
3691da177e4SLinus Torvalds 		ASSERT(dlip == lip);
3701da177e4SLinus Torvalds 	} else {
3711da177e4SLinus Torvalds 		lip->li_flags |= XFS_LI_IN_AIL;
3721da177e4SLinus Torvalds 	}
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 	lip->li_lsn = lsn;
3751da177e4SLinus Torvalds 
376535f6b37SJosef 'Jeff' Sipek 	xfs_ail_insert(&mp->m_ail, lip);
377249a8c11SDavid Chinner 	mp->m_ail.xa_gen++;
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 	if (mlip == dlip) {
380535f6b37SJosef 'Jeff' Sipek 		mlip = xfs_ail_min(&mp->m_ail);
381287f3dadSDonald Douwsma 		spin_unlock(&mp->m_ail_lock);
3821da177e4SLinus Torvalds 		xfs_log_move_tail(mp, mlip->li_lsn);
3831da177e4SLinus Torvalds 	} else {
384287f3dadSDonald Douwsma 		spin_unlock(&mp->m_ail_lock);
3851da177e4SLinus Torvalds 	}
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds }	/* xfs_trans_update_ail */
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds /*
3911da177e4SLinus Torvalds  * Delete the given item from the AIL.  It must already be in
3921da177e4SLinus Torvalds  * the AIL.
3931da177e4SLinus Torvalds  *
3941da177e4SLinus Torvalds  * Wakeup anyone with an lsn less than item's lsn.    If the item
3951da177e4SLinus Torvalds  * we delete in the AIL is the minimum one, update the tail lsn in the
3961da177e4SLinus Torvalds  * log manager.
3971da177e4SLinus Torvalds  *
3981da177e4SLinus Torvalds  * Clear the IN_AIL flag from the item, reset its lsn to 0, and
3991da177e4SLinus Torvalds  * bump the AIL's generation count to indicate that the tree
4001da177e4SLinus Torvalds  * has changed.
4011da177e4SLinus Torvalds  *
4021da177e4SLinus Torvalds  * This function must be called with the AIL lock held.  The lock
403287f3dadSDonald Douwsma  * is dropped before returning.
4041da177e4SLinus Torvalds  */
4051da177e4SLinus Torvalds void
4061da177e4SLinus Torvalds xfs_trans_delete_ail(
4071da177e4SLinus Torvalds 	xfs_mount_t	*mp,
408287f3dadSDonald Douwsma 	xfs_log_item_t	*lip) __releases(mp->m_ail_lock)
4091da177e4SLinus Torvalds {
4101da177e4SLinus Torvalds 	xfs_log_item_t		*dlip;
4111da177e4SLinus Torvalds 	xfs_log_item_t		*mlip;
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 	if (lip->li_flags & XFS_LI_IN_AIL) {
414535f6b37SJosef 'Jeff' Sipek 		mlip = xfs_ail_min(&mp->m_ail);
415535f6b37SJosef 'Jeff' Sipek 		dlip = xfs_ail_delete(&mp->m_ail, lip);
4161da177e4SLinus Torvalds 		ASSERT(dlip == lip);
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 		lip->li_flags &= ~XFS_LI_IN_AIL;
4201da177e4SLinus Torvalds 		lip->li_lsn = 0;
421249a8c11SDavid Chinner 		mp->m_ail.xa_gen++;
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds 		if (mlip == dlip) {
424535f6b37SJosef 'Jeff' Sipek 			mlip = xfs_ail_min(&mp->m_ail);
425287f3dadSDonald Douwsma 			spin_unlock(&mp->m_ail_lock);
4261da177e4SLinus Torvalds 			xfs_log_move_tail(mp, (mlip ? mlip->li_lsn : 0));
4271da177e4SLinus Torvalds 		} else {
428287f3dadSDonald Douwsma 			spin_unlock(&mp->m_ail_lock);
4291da177e4SLinus Torvalds 		}
4301da177e4SLinus Torvalds 	}
4311da177e4SLinus Torvalds 	else {
4321da177e4SLinus Torvalds 		/*
4331da177e4SLinus Torvalds 		 * If the file system is not being shutdown, we are in
4341da177e4SLinus Torvalds 		 * serious trouble if we get to this stage.
4351da177e4SLinus Torvalds 		 */
4361da177e4SLinus Torvalds 		if (XFS_FORCED_SHUTDOWN(mp))
437287f3dadSDonald Douwsma 			spin_unlock(&mp->m_ail_lock);
4381da177e4SLinus Torvalds 		else {
4391da177e4SLinus Torvalds 			xfs_cmn_err(XFS_PTAG_AILDELETE, CE_ALERT, mp,
4407d04a335SNathan Scott 		"%s: attempting to delete a log item that is not in the AIL",
4417d04a335SNathan Scott 					__FUNCTION__);
442287f3dadSDonald Douwsma 			spin_unlock(&mp->m_ail_lock);
4437d04a335SNathan Scott 			xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
4441da177e4SLinus Torvalds 		}
4451da177e4SLinus Torvalds 	}
4461da177e4SLinus Torvalds }
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds /*
4511da177e4SLinus Torvalds  * Return the item in the AIL with the smallest lsn.
4521da177e4SLinus Torvalds  * Return the current tree generation number for use
4531da177e4SLinus Torvalds  * in calls to xfs_trans_next_ail().
4541da177e4SLinus Torvalds  */
4551da177e4SLinus Torvalds xfs_log_item_t *
4561da177e4SLinus Torvalds xfs_trans_first_ail(
4571da177e4SLinus Torvalds 	xfs_mount_t	*mp,
4581da177e4SLinus Torvalds 	int		*gen)
4591da177e4SLinus Torvalds {
4601da177e4SLinus Torvalds 	xfs_log_item_t	*lip;
4611da177e4SLinus Torvalds 
462535f6b37SJosef 'Jeff' Sipek 	lip = xfs_ail_min(&mp->m_ail);
463249a8c11SDavid Chinner 	*gen = (int)mp->m_ail.xa_gen;
4641da177e4SLinus Torvalds 
465249a8c11SDavid Chinner 	return lip;
4661da177e4SLinus Torvalds }
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds /*
4691da177e4SLinus Torvalds  * If the generation count of the tree has not changed since the
4701da177e4SLinus Torvalds  * caller last took something from the AIL, then return the elmt
4711da177e4SLinus Torvalds  * in the tree which follows the one given.  If the count has changed,
4721da177e4SLinus Torvalds  * then return the minimum elmt of the AIL and bump the restarts counter
4731da177e4SLinus Torvalds  * if one is given.
4741da177e4SLinus Torvalds  */
4751da177e4SLinus Torvalds xfs_log_item_t *
4761da177e4SLinus Torvalds xfs_trans_next_ail(
4771da177e4SLinus Torvalds 	xfs_mount_t	*mp,
4781da177e4SLinus Torvalds 	xfs_log_item_t	*lip,
4791da177e4SLinus Torvalds 	int		*gen,
4801da177e4SLinus Torvalds 	int		*restarts)
4811da177e4SLinus Torvalds {
4821da177e4SLinus Torvalds 	xfs_log_item_t	*nlip;
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds 	ASSERT(mp && lip && gen);
485249a8c11SDavid Chinner 	if (mp->m_ail.xa_gen == *gen) {
486535f6b37SJosef 'Jeff' Sipek 		nlip = xfs_ail_next(&mp->m_ail, lip);
4871da177e4SLinus Torvalds 	} else {
488535f6b37SJosef 'Jeff' Sipek 		nlip = xfs_ail_min(&mp->m_ail);
489249a8c11SDavid Chinner 		*gen = (int)mp->m_ail.xa_gen;
4901da177e4SLinus Torvalds 		if (restarts != NULL) {
4911da177e4SLinus Torvalds 			XFS_STATS_INC(xs_push_ail_restarts);
4921da177e4SLinus Torvalds 			(*restarts)++;
4931da177e4SLinus Torvalds 		}
4941da177e4SLinus Torvalds 	}
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds 	return (nlip);
4971da177e4SLinus Torvalds }
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds /*
5011da177e4SLinus Torvalds  * The active item list (AIL) is a doubly linked list of log
5021da177e4SLinus Torvalds  * items sorted by ascending lsn.  The base of the list is
5031da177e4SLinus Torvalds  * a forw/back pointer pair embedded in the xfs mount structure.
5041da177e4SLinus Torvalds  * The base is initialized with both pointers pointing to the
5051da177e4SLinus Torvalds  * base.  This case always needs to be distinguished, because
5061da177e4SLinus Torvalds  * the base has no lsn to look at.  We almost always insert
5071da177e4SLinus Torvalds  * at the end of the list, so on inserts we search from the
5081da177e4SLinus Torvalds  * end of the list to find where the new item belongs.
5091da177e4SLinus Torvalds  */
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds /*
5121da177e4SLinus Torvalds  * Initialize the doubly linked list to point only to itself.
5131da177e4SLinus Torvalds  */
514249a8c11SDavid Chinner int
5151da177e4SLinus Torvalds xfs_trans_ail_init(
5161da177e4SLinus Torvalds 	xfs_mount_t	*mp)
5171da177e4SLinus Torvalds {
518535f6b37SJosef 'Jeff' Sipek 	INIT_LIST_HEAD(&mp->m_ail.xa_ail);
519249a8c11SDavid Chinner 	return xfsaild_start(mp);
520249a8c11SDavid Chinner }
521249a8c11SDavid Chinner 
522249a8c11SDavid Chinner void
523249a8c11SDavid Chinner xfs_trans_ail_destroy(
524249a8c11SDavid Chinner 	xfs_mount_t	*mp)
525249a8c11SDavid Chinner {
526249a8c11SDavid Chinner 	xfsaild_stop(mp);
5271da177e4SLinus Torvalds }
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds /*
5301da177e4SLinus Torvalds  * Insert the given log item into the AIL.
5311da177e4SLinus Torvalds  * We almost always insert at the end of the list, so on inserts
5321da177e4SLinus Torvalds  * we search from the end of the list to find where the
5331da177e4SLinus Torvalds  * new item belongs.
5341da177e4SLinus Torvalds  */
5351da177e4SLinus Torvalds STATIC void
5361da177e4SLinus Torvalds xfs_ail_insert(
537535f6b37SJosef 'Jeff' Sipek 	xfs_ail_t	*ailp,
5381da177e4SLinus Torvalds 	xfs_log_item_t	*lip)
5391da177e4SLinus Torvalds /* ARGSUSED */
5401da177e4SLinus Torvalds {
5411da177e4SLinus Torvalds 	xfs_log_item_t	*next_lip;
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds 	/*
5441da177e4SLinus Torvalds 	 * If the list is empty, just insert the item.
5451da177e4SLinus Torvalds 	 */
546535f6b37SJosef 'Jeff' Sipek 	if (list_empty(&ailp->xa_ail)) {
547535f6b37SJosef 'Jeff' Sipek 		list_add(&lip->li_ail, &ailp->xa_ail);
5481da177e4SLinus Torvalds 		return;
5491da177e4SLinus Torvalds 	}
5501da177e4SLinus Torvalds 
551535f6b37SJosef 'Jeff' Sipek 	list_for_each_entry_reverse(next_lip, &ailp->xa_ail, li_ail) {
552535f6b37SJosef 'Jeff' Sipek 		if (XFS_LSN_CMP(next_lip->li_lsn, lip->li_lsn) <= 0)
553535f6b37SJosef 'Jeff' Sipek 			break;
5541da177e4SLinus Torvalds 	}
5551da177e4SLinus Torvalds 
556535f6b37SJosef 'Jeff' Sipek 	ASSERT((&next_lip->li_ail == &ailp->xa_ail) ||
557535f6b37SJosef 'Jeff' Sipek 	       (XFS_LSN_CMP(next_lip->li_lsn, lip->li_lsn) <= 0));
558535f6b37SJosef 'Jeff' Sipek 
559535f6b37SJosef 'Jeff' Sipek 	list_add(&lip->li_ail, &next_lip->li_ail);
560535f6b37SJosef 'Jeff' Sipek 
561535f6b37SJosef 'Jeff' Sipek 	xfs_ail_check(ailp, lip);
5621da177e4SLinus Torvalds 	return;
5631da177e4SLinus Torvalds }
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds /*
5661da177e4SLinus Torvalds  * Delete the given item from the AIL.  Return a pointer to the item.
5671da177e4SLinus Torvalds  */
5681da177e4SLinus Torvalds /*ARGSUSED*/
5691da177e4SLinus Torvalds STATIC xfs_log_item_t *
5701da177e4SLinus Torvalds xfs_ail_delete(
571535f6b37SJosef 'Jeff' Sipek 	xfs_ail_t	*ailp,
5721da177e4SLinus Torvalds 	xfs_log_item_t	*lip)
5731da177e4SLinus Torvalds /* ARGSUSED */
5741da177e4SLinus Torvalds {
575535f6b37SJosef 'Jeff' Sipek 	xfs_ail_check(ailp, lip);
576535f6b37SJosef 'Jeff' Sipek 
577535f6b37SJosef 'Jeff' Sipek 	list_del(&lip->li_ail);
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds 	return lip;
5801da177e4SLinus Torvalds }
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds /*
5831da177e4SLinus Torvalds  * Return a pointer to the first item in the AIL.
5841da177e4SLinus Torvalds  * If the AIL is empty, then return NULL.
5851da177e4SLinus Torvalds  */
5861da177e4SLinus Torvalds STATIC xfs_log_item_t *
5871da177e4SLinus Torvalds xfs_ail_min(
588535f6b37SJosef 'Jeff' Sipek 	xfs_ail_t	*ailp)
5891da177e4SLinus Torvalds /* ARGSUSED */
5901da177e4SLinus Torvalds {
591535f6b37SJosef 'Jeff' Sipek 	if (list_empty(&ailp->xa_ail))
5921da177e4SLinus Torvalds 		return NULL;
593535f6b37SJosef 'Jeff' Sipek 
594535f6b37SJosef 'Jeff' Sipek 	return list_first_entry(&ailp->xa_ail, xfs_log_item_t, li_ail);
5951da177e4SLinus Torvalds }
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds /*
5981da177e4SLinus Torvalds  * Return a pointer to the item which follows
5991da177e4SLinus Torvalds  * the given item in the AIL.  If the given item
6001da177e4SLinus Torvalds  * is the last item in the list, then return NULL.
6011da177e4SLinus Torvalds  */
6021da177e4SLinus Torvalds STATIC xfs_log_item_t *
6031da177e4SLinus Torvalds xfs_ail_next(
604535f6b37SJosef 'Jeff' Sipek 	xfs_ail_t	*ailp,
6051da177e4SLinus Torvalds 	xfs_log_item_t	*lip)
6061da177e4SLinus Torvalds /* ARGSUSED */
6071da177e4SLinus Torvalds {
608535f6b37SJosef 'Jeff' Sipek 	if (lip->li_ail.next == &ailp->xa_ail)
6091da177e4SLinus Torvalds 		return NULL;
6101da177e4SLinus Torvalds 
611535f6b37SJosef 'Jeff' Sipek 	return list_first_entry(&lip->li_ail, xfs_log_item_t, li_ail);
6121da177e4SLinus Torvalds }
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds #ifdef DEBUG
6151da177e4SLinus Torvalds /*
6161da177e4SLinus Torvalds  * Check that the list is sorted as it should be.
6171da177e4SLinus Torvalds  */
6181da177e4SLinus Torvalds STATIC void
6191da177e4SLinus Torvalds xfs_ail_check(
620535f6b37SJosef 'Jeff' Sipek 	xfs_ail_t 	*ailp,
621de08dbc1SDavid Chinner 	xfs_log_item_t	*lip)
6221da177e4SLinus Torvalds {
6231da177e4SLinus Torvalds 	xfs_log_item_t	*prev_lip;
6241da177e4SLinus Torvalds 
625535f6b37SJosef 'Jeff' Sipek 	if (list_empty(&ailp->xa_ail))
6261da177e4SLinus Torvalds 		return;
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds 	/*
629de08dbc1SDavid Chinner 	 * Check the next and previous entries are valid.
630de08dbc1SDavid Chinner 	 */
631de08dbc1SDavid Chinner 	ASSERT((lip->li_flags & XFS_LI_IN_AIL) != 0);
632535f6b37SJosef 'Jeff' Sipek 	prev_lip = list_entry(lip->li_ail.prev, xfs_log_item_t, li_ail);
633535f6b37SJosef 'Jeff' Sipek 	if (&prev_lip->li_ail != &ailp->xa_ail)
634de08dbc1SDavid Chinner 		ASSERT(XFS_LSN_CMP(prev_lip->li_lsn, lip->li_lsn) <= 0);
635535f6b37SJosef 'Jeff' Sipek 
636535f6b37SJosef 'Jeff' Sipek 	prev_lip = list_entry(lip->li_ail.next, xfs_log_item_t, li_ail);
637535f6b37SJosef 'Jeff' Sipek 	if (&prev_lip->li_ail != &ailp->xa_ail)
638de08dbc1SDavid Chinner 		ASSERT(XFS_LSN_CMP(prev_lip->li_lsn, lip->li_lsn) >= 0);
639de08dbc1SDavid Chinner 
640de08dbc1SDavid Chinner 
641de08dbc1SDavid Chinner #ifdef XFS_TRANS_DEBUG
642de08dbc1SDavid Chinner 	/*
643535f6b37SJosef 'Jeff' Sipek 	 * Walk the list checking lsn ordering, and that every entry has the
644535f6b37SJosef 'Jeff' Sipek 	 * XFS_LI_IN_AIL flag set. This is really expensive, so only do it
645535f6b37SJosef 'Jeff' Sipek 	 * when specifically debugging the transaction subsystem.
6461da177e4SLinus Torvalds 	 */
647535f6b37SJosef 'Jeff' Sipek 	prev_lip = list_entry(&ailp->xa_ail, xfs_log_item_t, li_ail);
648535f6b37SJosef 'Jeff' Sipek 	list_for_each_entry(lip, &ailp->xa_ail, li_ail) {
649535f6b37SJosef 'Jeff' Sipek 		if (&prev_lip->li_ail != &ailp->xa_ail)
6501da177e4SLinus Torvalds 			ASSERT(XFS_LSN_CMP(prev_lip->li_lsn, lip->li_lsn) <= 0);
6511da177e4SLinus Torvalds 		ASSERT((lip->li_flags & XFS_LI_IN_AIL) != 0);
6521da177e4SLinus Torvalds 		prev_lip = lip;
6531da177e4SLinus Torvalds 	}
654de08dbc1SDavid Chinner #endif /* XFS_TRANS_DEBUG */
6551da177e4SLinus Torvalds }
6561da177e4SLinus Torvalds #endif /* DEBUG */
657