xref: /openbmc/linux/fs/xfs/xfs_trans_ail.c (revision 0e57f6a3)
11da177e4SLinus Torvalds /*
27b718769SNathan Scott  * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3c7e8f268SDavid Chinner  * Copyright (c) 2008 Dave Chinner
47b718769SNathan Scott  * All Rights Reserved.
51da177e4SLinus Torvalds  *
67b718769SNathan Scott  * This program is free software; you can redistribute it and/or
77b718769SNathan Scott  * modify it under the terms of the GNU General Public License as
81da177e4SLinus Torvalds  * published by the Free Software Foundation.
91da177e4SLinus Torvalds  *
107b718769SNathan Scott  * This program is distributed in the hope that it would be useful,
117b718769SNathan Scott  * but WITHOUT ANY WARRANTY; without even the implied warranty of
127b718769SNathan Scott  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
137b718769SNathan Scott  * GNU General Public License for more details.
141da177e4SLinus Torvalds  *
157b718769SNathan Scott  * You should have received a copy of the GNU General Public License
167b718769SNathan Scott  * along with this program; if not, write the Free Software Foundation,
177b718769SNathan Scott  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
181da177e4SLinus Torvalds  */
191da177e4SLinus Torvalds #include "xfs.h"
20a844f451SNathan Scott #include "xfs_fs.h"
211da177e4SLinus Torvalds #include "xfs_types.h"
221da177e4SLinus Torvalds #include "xfs_log.h"
23a844f451SNathan Scott #include "xfs_inum.h"
241da177e4SLinus Torvalds #include "xfs_trans.h"
251da177e4SLinus Torvalds #include "xfs_sb.h"
26da353b0dSDavid Chinner #include "xfs_ag.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 *);
320e57f6a3SDave Chinner STATIC void xfs_ail_splice(struct xfs_ail *, struct list_head *, xfs_lsn_t);
33eb3efa12SDave Chinner STATIC void xfs_ail_delete(struct xfs_ail *, xfs_log_item_t *);
3482fa9012SDavid Chinner STATIC xfs_log_item_t * xfs_ail_min(struct xfs_ail *);
3582fa9012SDavid Chinner STATIC xfs_log_item_t * xfs_ail_next(struct xfs_ail *, xfs_log_item_t *);
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds #ifdef DEBUG
3882fa9012SDavid Chinner STATIC void xfs_ail_check(struct xfs_ail *, xfs_log_item_t *);
391da177e4SLinus Torvalds #else
40de08dbc1SDavid Chinner #define	xfs_ail_check(a,l)
411da177e4SLinus Torvalds #endif /* DEBUG */
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds /*
451da177e4SLinus Torvalds  * This is called by the log manager code to determine the LSN
461da177e4SLinus Torvalds  * of the tail of the log.  This is exactly the LSN of the first
471da177e4SLinus Torvalds  * item in the AIL.  If the AIL is empty, then this function
481da177e4SLinus Torvalds  * returns 0.
491da177e4SLinus Torvalds  *
501da177e4SLinus Torvalds  * We need the AIL lock in order to get a coherent read of the
511da177e4SLinus Torvalds  * lsn of the last item in the AIL.
521da177e4SLinus Torvalds  */
531da177e4SLinus Torvalds xfs_lsn_t
545b00f14fSDavid Chinner xfs_trans_ail_tail(
555b00f14fSDavid Chinner 	struct xfs_ail	*ailp)
561da177e4SLinus Torvalds {
571da177e4SLinus Torvalds 	xfs_lsn_t	lsn;
581da177e4SLinus Torvalds 	xfs_log_item_t	*lip;
591da177e4SLinus Torvalds 
60c7e8f268SDavid Chinner 	spin_lock(&ailp->xa_lock);
615b00f14fSDavid Chinner 	lip = xfs_ail_min(ailp);
621da177e4SLinus Torvalds 	if (lip == NULL) {
631da177e4SLinus Torvalds 		lsn = (xfs_lsn_t)0;
641da177e4SLinus Torvalds 	} else {
651da177e4SLinus Torvalds 		lsn = lip->li_lsn;
661da177e4SLinus Torvalds 	}
67c7e8f268SDavid Chinner 	spin_unlock(&ailp->xa_lock);
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds 	return lsn;
701da177e4SLinus Torvalds }
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds /*
731da177e4SLinus Torvalds  * xfs_trans_push_ail
741da177e4SLinus Torvalds  *
75249a8c11SDavid Chinner  * This routine is called to move the tail of the AIL forward.  It does this by
76249a8c11SDavid Chinner  * trying to flush items in the AIL whose lsns are below the given
77249a8c11SDavid Chinner  * threshold_lsn.
781da177e4SLinus Torvalds  *
79249a8c11SDavid Chinner  * the push is run asynchronously in a separate thread, so we return the tail
80249a8c11SDavid Chinner  * of the log right now instead of the tail after the push. This means we will
81249a8c11SDavid Chinner  * either continue right away, or we will sleep waiting on the async thread to
829da096fdSMalcolm Parsons  * do its work.
83249a8c11SDavid Chinner  *
84249a8c11SDavid Chinner  * We do this unlocked - we only need to know whether there is anything in the
85249a8c11SDavid Chinner  * AIL at the time we are called. We don't need to access the contents of
86249a8c11SDavid Chinner  * any of the objects, so the lock is not needed.
871da177e4SLinus Torvalds  */
88249a8c11SDavid Chinner void
89783a2f65SDavid Chinner xfs_trans_ail_push(
90783a2f65SDavid Chinner 	struct xfs_ail	*ailp,
911da177e4SLinus Torvalds 	xfs_lsn_t	threshold_lsn)
921da177e4SLinus Torvalds {
93249a8c11SDavid Chinner 	xfs_log_item_t	*lip;
94249a8c11SDavid Chinner 
95783a2f65SDavid Chinner 	lip = xfs_ail_min(ailp);
96783a2f65SDavid Chinner 	if (lip && !XFS_FORCED_SHUTDOWN(ailp->xa_mount)) {
97783a2f65SDavid Chinner 		if (XFS_LSN_CMP(threshold_lsn, ailp->xa_target) > 0)
98783a2f65SDavid Chinner 			xfsaild_wakeup(ailp, threshold_lsn);
99249a8c11SDavid Chinner 	}
100249a8c11SDavid Chinner }
101249a8c11SDavid Chinner 
102249a8c11SDavid Chinner /*
10327d8d5feSDavid Chinner  * AIL traversal cursor initialisation.
10427d8d5feSDavid Chinner  *
10527d8d5feSDavid Chinner  * The cursor keeps track of where our current traversal is up
10627d8d5feSDavid Chinner  * to by tracking the next ƣtem in the list for us. However, for
10727d8d5feSDavid Chinner  * this to be safe, removing an object from the AIL needs to invalidate
10827d8d5feSDavid Chinner  * any cursor that points to it. hence the traversal cursor needs to
10927d8d5feSDavid Chinner  * be linked to the struct xfs_ail so that deletion can search all the
11027d8d5feSDavid Chinner  * active cursors for invalidation.
11127d8d5feSDavid Chinner  *
11227d8d5feSDavid Chinner  * We don't link the push cursor because it is embedded in the struct
11327d8d5feSDavid Chinner  * xfs_ail and hence easily findable.
11427d8d5feSDavid Chinner  */
1155b00f14fSDavid Chinner STATIC void
11627d8d5feSDavid Chinner xfs_trans_ail_cursor_init(
11727d8d5feSDavid Chinner 	struct xfs_ail		*ailp,
11827d8d5feSDavid Chinner 	struct xfs_ail_cursor	*cur)
11927d8d5feSDavid Chinner {
12027d8d5feSDavid Chinner 	cur->item = NULL;
12127d8d5feSDavid Chinner 	if (cur == &ailp->xa_cursors)
12227d8d5feSDavid Chinner 		return;
12327d8d5feSDavid Chinner 
12427d8d5feSDavid Chinner 	cur->next = ailp->xa_cursors.next;
12527d8d5feSDavid Chinner 	ailp->xa_cursors.next = cur;
12627d8d5feSDavid Chinner }
12727d8d5feSDavid Chinner 
12827d8d5feSDavid Chinner /*
12927d8d5feSDavid Chinner  * Set the cursor to the next item, because when we look
13027d8d5feSDavid Chinner  * up the cursor the current item may have been freed.
13127d8d5feSDavid Chinner  */
13227d8d5feSDavid Chinner STATIC void
13327d8d5feSDavid Chinner xfs_trans_ail_cursor_set(
13427d8d5feSDavid Chinner 	struct xfs_ail		*ailp,
13527d8d5feSDavid Chinner 	struct xfs_ail_cursor	*cur,
13627d8d5feSDavid Chinner 	struct xfs_log_item	*lip)
13727d8d5feSDavid Chinner {
13827d8d5feSDavid Chinner 	if (lip)
13927d8d5feSDavid Chinner 		cur->item = xfs_ail_next(ailp, lip);
14027d8d5feSDavid Chinner }
14127d8d5feSDavid Chinner 
14227d8d5feSDavid Chinner /*
14327d8d5feSDavid Chinner  * Get the next item in the traversal and advance the cursor.
14427d8d5feSDavid Chinner  * If the cursor was invalidated (inidicated by a lip of 1),
14527d8d5feSDavid Chinner  * restart the traversal.
14627d8d5feSDavid Chinner  */
1475b00f14fSDavid Chinner struct xfs_log_item *
14827d8d5feSDavid Chinner xfs_trans_ail_cursor_next(
14927d8d5feSDavid Chinner 	struct xfs_ail		*ailp,
15027d8d5feSDavid Chinner 	struct xfs_ail_cursor	*cur)
15127d8d5feSDavid Chinner {
15227d8d5feSDavid Chinner 	struct xfs_log_item	*lip = cur->item;
15327d8d5feSDavid Chinner 
15427d8d5feSDavid Chinner 	if ((__psint_t)lip & 1)
15527d8d5feSDavid Chinner 		lip = xfs_ail_min(ailp);
15627d8d5feSDavid Chinner 	xfs_trans_ail_cursor_set(ailp, cur, lip);
15727d8d5feSDavid Chinner 	return lip;
15827d8d5feSDavid Chinner }
15927d8d5feSDavid Chinner 
16027d8d5feSDavid Chinner /*
16127d8d5feSDavid Chinner  * Now that the traversal is complete, we need to remove the cursor
16227d8d5feSDavid Chinner  * from the list of traversing cursors. Avoid removing the embedded
1639da096fdSMalcolm Parsons  * push cursor, but use the fact it is always present to make the
16427d8d5feSDavid Chinner  * list deletion simple.
16527d8d5feSDavid Chinner  */
16627d8d5feSDavid Chinner void
16727d8d5feSDavid Chinner xfs_trans_ail_cursor_done(
16827d8d5feSDavid Chinner 	struct xfs_ail		*ailp,
16927d8d5feSDavid Chinner 	struct xfs_ail_cursor	*done)
17027d8d5feSDavid Chinner {
17127d8d5feSDavid Chinner 	struct xfs_ail_cursor	*prev = NULL;
17227d8d5feSDavid Chinner 	struct xfs_ail_cursor	*cur;
17327d8d5feSDavid Chinner 
17427d8d5feSDavid Chinner 	done->item = NULL;
17527d8d5feSDavid Chinner 	if (done == &ailp->xa_cursors)
17627d8d5feSDavid Chinner 		return;
17727d8d5feSDavid Chinner 	prev = &ailp->xa_cursors;
17827d8d5feSDavid Chinner 	for (cur = prev->next; cur; prev = cur, cur = prev->next) {
17927d8d5feSDavid Chinner 		if (cur == done) {
18027d8d5feSDavid Chinner 			prev->next = cur->next;
18127d8d5feSDavid Chinner 			break;
18227d8d5feSDavid Chinner 		}
18327d8d5feSDavid Chinner 	}
18427d8d5feSDavid Chinner 	ASSERT(cur);
18527d8d5feSDavid Chinner }
18627d8d5feSDavid Chinner 
18727d8d5feSDavid Chinner /*
1885b00f14fSDavid Chinner  * Invalidate any cursor that is pointing to this item. This is
1895b00f14fSDavid Chinner  * called when an item is removed from the AIL. Any cursor pointing
1905b00f14fSDavid Chinner  * to this object is now invalid and the traversal needs to be
1915b00f14fSDavid Chinner  * terminated so it doesn't reference a freed object. We set the
1925b00f14fSDavid Chinner  * cursor item to a value of 1 so we can distinguish between an
1935b00f14fSDavid Chinner  * invalidation and the end of the list when getting the next item
1945b00f14fSDavid Chinner  * from the cursor.
1955b00f14fSDavid Chinner  */
1965b00f14fSDavid Chinner STATIC void
1975b00f14fSDavid Chinner xfs_trans_ail_cursor_clear(
1985b00f14fSDavid Chinner 	struct xfs_ail		*ailp,
1995b00f14fSDavid Chinner 	struct xfs_log_item	*lip)
2005b00f14fSDavid Chinner {
2015b00f14fSDavid Chinner 	struct xfs_ail_cursor	*cur;
2025b00f14fSDavid Chinner 
2035b00f14fSDavid Chinner 	/* need to search all cursors */
2045b00f14fSDavid Chinner 	for (cur = &ailp->xa_cursors; cur; cur = cur->next) {
2055b00f14fSDavid Chinner 		if (cur->item == lip)
2065b00f14fSDavid Chinner 			cur->item = (struct xfs_log_item *)
2075b00f14fSDavid Chinner 					((__psint_t)cur->item | 1);
2085b00f14fSDavid Chinner 	}
2095b00f14fSDavid Chinner }
2105b00f14fSDavid Chinner 
2115b00f14fSDavid Chinner /*
212249a8c11SDavid Chinner  * Return the item in the AIL with the current lsn.
213249a8c11SDavid Chinner  * Return the current tree generation number for use
214249a8c11SDavid Chinner  * in calls to xfs_trans_next_ail().
215249a8c11SDavid Chinner  */
2165b00f14fSDavid Chinner xfs_log_item_t *
2175b00f14fSDavid Chinner xfs_trans_ail_cursor_first(
21827d8d5feSDavid Chinner 	struct xfs_ail		*ailp,
21927d8d5feSDavid Chinner 	struct xfs_ail_cursor	*cur,
220249a8c11SDavid Chinner 	xfs_lsn_t		lsn)
221249a8c11SDavid Chinner {
222249a8c11SDavid Chinner 	xfs_log_item_t		*lip;
223249a8c11SDavid Chinner 
2245b00f14fSDavid Chinner 	xfs_trans_ail_cursor_init(ailp, cur);
22527d8d5feSDavid Chinner 	lip = xfs_ail_min(ailp);
226249a8c11SDavid Chinner 	if (lsn == 0)
2275b00f14fSDavid Chinner 		goto out;
228249a8c11SDavid Chinner 
22927d8d5feSDavid Chinner 	list_for_each_entry(lip, &ailp->xa_ail, li_ail) {
2305b00f14fSDavid Chinner 		if (XFS_LSN_CMP(lip->li_lsn, lsn) >= 0)
2317ee49acfSDavid Chinner 			goto out;
2325b00f14fSDavid Chinner 	}
2335b00f14fSDavid Chinner 	lip = NULL;
2345b00f14fSDavid Chinner out:
23527d8d5feSDavid Chinner 	xfs_trans_ail_cursor_set(ailp, cur, lip);
236249a8c11SDavid Chinner 	return lip;
237249a8c11SDavid Chinner }
238535f6b37SJosef 'Jeff' Sipek 
239249a8c11SDavid Chinner /*
240453eac8aSDave Chinner  * xfsaild_push does the work of pushing on the AIL.  Returning a timeout of
241453eac8aSDave Chinner  * zero indicates that the caller should sleep until woken.
242249a8c11SDavid Chinner  */
243249a8c11SDavid Chinner long
244249a8c11SDavid Chinner xfsaild_push(
24582fa9012SDavid Chinner 	struct xfs_ail	*ailp,
246249a8c11SDavid Chinner 	xfs_lsn_t	*last_lsn)
247249a8c11SDavid Chinner {
248453eac8aSDave Chinner 	long		tout = 0;
249249a8c11SDavid Chinner 	xfs_lsn_t	last_pushed_lsn = *last_lsn;
25082fa9012SDavid Chinner 	xfs_lsn_t	target =  ailp->xa_target;
2511da177e4SLinus Torvalds 	xfs_lsn_t	lsn;
2521da177e4SLinus Torvalds 	xfs_log_item_t	*lip;
253249a8c11SDavid Chinner 	int		flush_log, count, stuck;
25482fa9012SDavid Chinner 	xfs_mount_t	*mp = ailp->xa_mount;
25527d8d5feSDavid Chinner 	struct xfs_ail_cursor	*cur = &ailp->xa_cursors;
256d808f617SDave Chinner 	int		push_xfsbufd = 0;
2571da177e4SLinus Torvalds 
258c7e8f268SDavid Chinner 	spin_lock(&ailp->xa_lock);
25927d8d5feSDavid Chinner 	xfs_trans_ail_cursor_init(ailp, cur);
2605b00f14fSDavid Chinner 	lip = xfs_trans_ail_cursor_first(ailp, cur, *last_lsn);
261249a8c11SDavid Chinner 	if (!lip || XFS_FORCED_SHUTDOWN(mp)) {
2621da177e4SLinus Torvalds 		/*
263249a8c11SDavid Chinner 		 * AIL is empty or our push has reached the end.
2641da177e4SLinus Torvalds 		 */
26527d8d5feSDavid Chinner 		xfs_trans_ail_cursor_done(ailp, cur);
266c7e8f268SDavid Chinner 		spin_unlock(&ailp->xa_lock);
267453eac8aSDave Chinner 		*last_lsn = 0;
26827d8d5feSDavid Chinner 		return tout;
2691da177e4SLinus Torvalds 	}
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds 	XFS_STATS_INC(xs_push_ail);
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	/*
2741da177e4SLinus Torvalds 	 * While the item we are looking at is below the given threshold
275249a8c11SDavid Chinner 	 * try to flush it out. We'd like not to stop until we've at least
2761da177e4SLinus Torvalds 	 * tried to push on everything in the AIL with an LSN less than
277249a8c11SDavid Chinner 	 * the given threshold.
2781da177e4SLinus Torvalds 	 *
279249a8c11SDavid Chinner 	 * However, we will stop after a certain number of pushes and wait
280249a8c11SDavid Chinner 	 * for a reduced timeout to fire before pushing further. This
281249a8c11SDavid Chinner 	 * prevents use from spinning when we can't do anything or there is
282249a8c11SDavid Chinner 	 * lots of contention on the AIL lists.
283249a8c11SDavid Chinner 	 */
284249a8c11SDavid Chinner 	lsn = lip->li_lsn;
28527d8d5feSDavid Chinner 	flush_log = stuck = count = 0;
286249a8c11SDavid Chinner 	while ((XFS_LSN_CMP(lip->li_lsn, target) < 0)) {
287249a8c11SDavid Chinner 		int	lock_result;
288249a8c11SDavid Chinner 		/*
289249a8c11SDavid Chinner 		 * If we can lock the item without sleeping, unlock the AIL
290249a8c11SDavid Chinner 		 * lock and flush the item.  Then re-grab the AIL lock so we
291249a8c11SDavid Chinner 		 * can look for the next item on the AIL. List changes are
292249a8c11SDavid Chinner 		 * handled by the AIL lookup functions internally
293249a8c11SDavid Chinner 		 *
294249a8c11SDavid Chinner 		 * If we can't lock the item, either its holder will flush it
295249a8c11SDavid Chinner 		 * or it is already being flushed or it is being relogged.  In
296249a8c11SDavid Chinner 		 * any of these case it is being taken care of and we can just
297249a8c11SDavid Chinner 		 * skip to the next item in the list.
2981da177e4SLinus Torvalds 		 */
2991da177e4SLinus Torvalds 		lock_result = IOP_TRYLOCK(lip);
300c7e8f268SDavid Chinner 		spin_unlock(&ailp->xa_lock);
3011da177e4SLinus Torvalds 		switch (lock_result) {
3021da177e4SLinus Torvalds 		case XFS_ITEM_SUCCESS:
3031da177e4SLinus Torvalds 			XFS_STATS_INC(xs_push_ail_success);
3041da177e4SLinus Torvalds 			IOP_PUSH(lip);
305249a8c11SDavid Chinner 			last_pushed_lsn = lsn;
3061da177e4SLinus Torvalds 			break;
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 		case XFS_ITEM_PUSHBUF:
3091da177e4SLinus Torvalds 			XFS_STATS_INC(xs_push_ail_pushbuf);
3101da177e4SLinus Torvalds 			IOP_PUSHBUF(lip);
311249a8c11SDavid Chinner 			last_pushed_lsn = lsn;
312d808f617SDave Chinner 			push_xfsbufd = 1;
3131da177e4SLinus Torvalds 			break;
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds 		case XFS_ITEM_PINNED:
3161da177e4SLinus Torvalds 			XFS_STATS_INC(xs_push_ail_pinned);
317249a8c11SDavid Chinner 			stuck++;
3181da177e4SLinus Torvalds 			flush_log = 1;
3191da177e4SLinus Torvalds 			break;
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 		case XFS_ITEM_LOCKED:
3221da177e4SLinus Torvalds 			XFS_STATS_INC(xs_push_ail_locked);
323249a8c11SDavid Chinner 			last_pushed_lsn = lsn;
324249a8c11SDavid Chinner 			stuck++;
3251da177e4SLinus Torvalds 			break;
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds 		default:
3281da177e4SLinus Torvalds 			ASSERT(0);
3291da177e4SLinus Torvalds 			break;
3301da177e4SLinus Torvalds 		}
3311da177e4SLinus Torvalds 
332c7e8f268SDavid Chinner 		spin_lock(&ailp->xa_lock);
333249a8c11SDavid Chinner 		/* should we bother continuing? */
334249a8c11SDavid Chinner 		if (XFS_FORCED_SHUTDOWN(mp))
3351da177e4SLinus Torvalds 			break;
336249a8c11SDavid Chinner 		ASSERT(mp->m_log);
3371da177e4SLinus Torvalds 
338249a8c11SDavid Chinner 		count++;
339249a8c11SDavid Chinner 
340249a8c11SDavid Chinner 		/*
341249a8c11SDavid Chinner 		 * Are there too many items we can't do anything with?
342249a8c11SDavid Chinner 		 * If we we are skipping too many items because we can't flush
343249a8c11SDavid Chinner 		 * them or they are already being flushed, we back off and
344249a8c11SDavid Chinner 		 * given them time to complete whatever operation is being
345249a8c11SDavid Chinner 		 * done. i.e. remove pressure from the AIL while we can't make
346249a8c11SDavid Chinner 		 * progress so traversals don't slow down further inserts and
347249a8c11SDavid Chinner 		 * removals to/from the AIL.
348249a8c11SDavid Chinner 		 *
349249a8c11SDavid Chinner 		 * The value of 100 is an arbitrary magic number based on
350249a8c11SDavid Chinner 		 * observation.
351249a8c11SDavid Chinner 		 */
352249a8c11SDavid Chinner 		if (stuck > 100)
353249a8c11SDavid Chinner 			break;
354249a8c11SDavid Chinner 
35527d8d5feSDavid Chinner 		lip = xfs_trans_ail_cursor_next(ailp, cur);
356249a8c11SDavid Chinner 		if (lip == NULL)
357249a8c11SDavid Chinner 			break;
358249a8c11SDavid Chinner 		lsn = lip->li_lsn;
3591da177e4SLinus Torvalds 	}
36027d8d5feSDavid Chinner 	xfs_trans_ail_cursor_done(ailp, cur);
361c7e8f268SDavid Chinner 	spin_unlock(&ailp->xa_lock);
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 	if (flush_log) {
3641da177e4SLinus Torvalds 		/*
3651da177e4SLinus Torvalds 		 * If something we need to push out was pinned, then
3661da177e4SLinus Torvalds 		 * push out the log so it will become unpinned and
3671da177e4SLinus Torvalds 		 * move forward in the AIL.
3681da177e4SLinus Torvalds 		 */
3691da177e4SLinus Torvalds 		XFS_STATS_INC(xs_push_ail_flush);
370a14a348bSChristoph Hellwig 		xfs_log_force(mp, 0);
3711da177e4SLinus Torvalds 	}
3721da177e4SLinus Torvalds 
373d808f617SDave Chinner 	if (push_xfsbufd) {
374d808f617SDave Chinner 		/* we've got delayed write buffers to flush */
375d808f617SDave Chinner 		wake_up_process(mp->m_ddev_targp->bt_task);
376d808f617SDave Chinner 	}
377d808f617SDave Chinner 
37892d9cd10SDavid Chinner 	if (!count) {
37992d9cd10SDavid Chinner 		/* We're past our target or empty, so idle */
380453eac8aSDave Chinner 		last_pushed_lsn = 0;
38192d9cd10SDavid Chinner 	} else if (XFS_LSN_CMP(lsn, target) >= 0) {
382249a8c11SDavid Chinner 		/*
38392d9cd10SDavid Chinner 		 * We reached the target so wait a bit longer for I/O to
38492d9cd10SDavid Chinner 		 * complete and remove pushed items from the AIL before we
38592d9cd10SDavid Chinner 		 * start the next scan from the start of the AIL.
386249a8c11SDavid Chinner 		 */
387453eac8aSDave Chinner 		tout = 50;
388249a8c11SDavid Chinner 		last_pushed_lsn = 0;
38927d8d5feSDavid Chinner 	} else if ((stuck * 100) / count > 90) {
390249a8c11SDavid Chinner 		/*
391249a8c11SDavid Chinner 		 * Either there is a lot of contention on the AIL or we
392249a8c11SDavid Chinner 		 * are stuck due to operations in progress. "Stuck" in this
393249a8c11SDavid Chinner 		 * case is defined as >90% of the items we tried to push
394249a8c11SDavid Chinner 		 * were stuck.
395249a8c11SDavid Chinner 		 *
396249a8c11SDavid Chinner 		 * Backoff a bit more to allow some I/O to complete before
397249a8c11SDavid Chinner 		 * continuing from where we were.
398249a8c11SDavid Chinner 		 */
399453eac8aSDave Chinner 		tout = 20;
400453eac8aSDave Chinner 	} else {
401453eac8aSDave Chinner 		/* more to do, but wait a short while before continuing */
402453eac8aSDave Chinner 		tout = 10;
4031da177e4SLinus Torvalds 	}
404249a8c11SDavid Chinner 	*last_lsn = last_pushed_lsn;
405249a8c11SDavid Chinner 	return tout;
406453eac8aSDave Chinner }
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds /*
4101da177e4SLinus Torvalds  * This is to be called when an item is unlocked that may have
4111da177e4SLinus Torvalds  * been in the AIL.  It will wake up the first member of the AIL
4121da177e4SLinus Torvalds  * wait list if this item's unlocking might allow it to progress.
4131da177e4SLinus Torvalds  * If the item is in the AIL, then we need to get the AIL lock
4141da177e4SLinus Torvalds  * while doing our checking so we don't race with someone going
4151da177e4SLinus Torvalds  * to sleep waiting for this event in xfs_trans_push_ail().
4161da177e4SLinus Torvalds  */
4171da177e4SLinus Torvalds void
4181da177e4SLinus Torvalds xfs_trans_unlocked_item(
419783a2f65SDavid Chinner 	struct xfs_ail	*ailp,
4201da177e4SLinus Torvalds 	xfs_log_item_t	*lip)
4211da177e4SLinus Torvalds {
4221da177e4SLinus Torvalds 	xfs_log_item_t	*min_lip;
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds 	/*
4251da177e4SLinus Torvalds 	 * If we're forcibly shutting down, we may have
4261da177e4SLinus Torvalds 	 * unlocked log items arbitrarily. The last thing
4271da177e4SLinus Torvalds 	 * we want to do is to move the tail of the log
4281da177e4SLinus Torvalds 	 * over some potentially valid data.
4291da177e4SLinus Torvalds 	 */
4301da177e4SLinus Torvalds 	if (!(lip->li_flags & XFS_LI_IN_AIL) ||
431783a2f65SDavid Chinner 	    XFS_FORCED_SHUTDOWN(ailp->xa_mount)) {
4321da177e4SLinus Torvalds 		return;
4331da177e4SLinus Torvalds 	}
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds 	/*
4361da177e4SLinus Torvalds 	 * This is the one case where we can call into xfs_ail_min()
4371da177e4SLinus Torvalds 	 * without holding the AIL lock because we only care about the
4381da177e4SLinus Torvalds 	 * case where we are at the tail of the AIL.  If the object isn't
4391da177e4SLinus Torvalds 	 * at the tail, it doesn't matter what result we get back.  This
4401da177e4SLinus Torvalds 	 * is slightly racy because since we were just unlocked, we could
4411da177e4SLinus Torvalds 	 * go to sleep between the call to xfs_ail_min and the call to
4421da177e4SLinus Torvalds 	 * xfs_log_move_tail, have someone else lock us, commit to us disk,
4431da177e4SLinus Torvalds 	 * move us out of the tail of the AIL, and then we wake up.  However,
4441da177e4SLinus Torvalds 	 * the call to xfs_log_move_tail() doesn't do anything if there's
4451da177e4SLinus Torvalds 	 * not enough free space to wake people up so we're safe calling it.
4461da177e4SLinus Torvalds 	 */
447783a2f65SDavid Chinner 	min_lip = xfs_ail_min(ailp);
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 	if (min_lip == lip)
450783a2f65SDavid Chinner 		xfs_log_move_tail(ailp->xa_mount, 1);
4511da177e4SLinus Torvalds }	/* xfs_trans_unlocked_item */
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds /*
4551da177e4SLinus Torvalds  * Update the position of the item in the AIL with the new
4561da177e4SLinus Torvalds  * lsn.  If it is not yet in the AIL, add it.  Otherwise, move
4571da177e4SLinus Torvalds  * it to its new position by removing it and re-adding it.
4581da177e4SLinus Torvalds  *
4591da177e4SLinus Torvalds  * Wakeup anyone with an lsn less than the item's lsn.  If the item
4601da177e4SLinus Torvalds  * we move in the AIL is the minimum one, update the tail lsn in the
4611da177e4SLinus Torvalds  * log manager.
4621da177e4SLinus Torvalds  *
4631da177e4SLinus Torvalds  * This function must be called with the AIL lock held.  The lock
464287f3dadSDonald Douwsma  * is dropped before returning.
4651da177e4SLinus Torvalds  */
4661da177e4SLinus Torvalds void
467783a2f65SDavid Chinner xfs_trans_ail_update(
468783a2f65SDavid Chinner 	struct xfs_ail	*ailp,
4691da177e4SLinus Torvalds 	xfs_log_item_t	*lip,
470c7e8f268SDavid Chinner 	xfs_lsn_t	lsn) __releases(ailp->xa_lock)
4711da177e4SLinus Torvalds {
4721da177e4SLinus Torvalds 	xfs_log_item_t		*mlip;	/* ptr to minimum lip */
4736c06f072SNathaniel W. Turner 	xfs_lsn_t		tail_lsn;
4741da177e4SLinus Torvalds 
475c7e8f268SDavid Chinner 	mlip = xfs_ail_min(ailp);
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds 	if (lip->li_flags & XFS_LI_IN_AIL) {
478eb3efa12SDave Chinner 		xfs_ail_delete(ailp, lip);
4791da177e4SLinus Torvalds 	} else {
4801da177e4SLinus Torvalds 		lip->li_flags |= XFS_LI_IN_AIL;
4811da177e4SLinus Torvalds 	}
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds 	lip->li_lsn = lsn;
484c7e8f268SDavid Chinner 	xfs_ail_insert(ailp, lip);
4851da177e4SLinus Torvalds 
486eb3efa12SDave Chinner 	if (mlip == lip) {
487c7e8f268SDavid Chinner 		mlip = xfs_ail_min(ailp);
4886c06f072SNathaniel W. Turner 		/*
4896c06f072SNathaniel W. Turner 		 * It is not safe to access mlip after the AIL lock is
4906c06f072SNathaniel W. Turner 		 * dropped, so we must get a copy of li_lsn before we do
4916c06f072SNathaniel W. Turner 		 * so.  This is especially important on 32-bit platforms
4926c06f072SNathaniel W. Turner 		 * where accessing and updating 64-bit values like li_lsn
4936c06f072SNathaniel W. Turner 		 * is not atomic.
4946c06f072SNathaniel W. Turner 		 */
4956c06f072SNathaniel W. Turner 		tail_lsn = mlip->li_lsn;
496c7e8f268SDavid Chinner 		spin_unlock(&ailp->xa_lock);
4976c06f072SNathaniel W. Turner 		xfs_log_move_tail(ailp->xa_mount, tail_lsn);
4981da177e4SLinus Torvalds 	} else {
499c7e8f268SDavid Chinner 		spin_unlock(&ailp->xa_lock);
5001da177e4SLinus Torvalds 	}
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds }	/* xfs_trans_update_ail */
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds /*
5060e57f6a3SDave Chinner  * xfs_trans_ail_update - bulk AIL insertion operation.
5070e57f6a3SDave Chinner  *
5080e57f6a3SDave Chinner  * @xfs_trans_ail_update takes an array of log items that all need to be
5090e57f6a3SDave Chinner  * positioned at the same LSN in the AIL. If an item is not in the AIL, it will
5100e57f6a3SDave Chinner  * be added.  Otherwise, it will be repositioned  by removing it and re-adding
5110e57f6a3SDave Chinner  * it to the AIL. If we move the first item in the AIL, update the log tail to
5120e57f6a3SDave Chinner  * match the new minimum LSN in the AIL.
5130e57f6a3SDave Chinner  *
5140e57f6a3SDave Chinner  * This function takes the AIL lock once to execute the update operations on
5150e57f6a3SDave Chinner  * all the items in the array, and as such should not be called with the AIL
5160e57f6a3SDave Chinner  * lock held. As a result, once we have the AIL lock, we need to check each log
5170e57f6a3SDave Chinner  * item LSN to confirm it needs to be moved forward in the AIL.
5180e57f6a3SDave Chinner  *
5190e57f6a3SDave Chinner  * To optimise the insert operation, we delete all the items from the AIL in
5200e57f6a3SDave Chinner  * the first pass, moving them into a temporary list, then splice the temporary
5210e57f6a3SDave Chinner  * list into the correct position in the AIL. This avoids needing to do an
5220e57f6a3SDave Chinner  * insert operation on every item.
5230e57f6a3SDave Chinner  *
5240e57f6a3SDave Chinner  * This function must be called with the AIL lock held.  The lock is dropped
5250e57f6a3SDave Chinner  * before returning.
5260e57f6a3SDave Chinner  */
5270e57f6a3SDave Chinner void
5280e57f6a3SDave Chinner xfs_trans_ail_update_bulk(
5290e57f6a3SDave Chinner 	struct xfs_ail		*ailp,
5300e57f6a3SDave Chinner 	struct xfs_log_item	**log_items,
5310e57f6a3SDave Chinner 	int			nr_items,
5320e57f6a3SDave Chinner 	xfs_lsn_t		lsn) __releases(ailp->xa_lock)
5330e57f6a3SDave Chinner {
5340e57f6a3SDave Chinner 	xfs_log_item_t		*mlip;
5350e57f6a3SDave Chinner 	xfs_lsn_t		tail_lsn;
5360e57f6a3SDave Chinner 	int			mlip_changed = 0;
5370e57f6a3SDave Chinner 	int			i;
5380e57f6a3SDave Chinner 	LIST_HEAD(tmp);
5390e57f6a3SDave Chinner 
5400e57f6a3SDave Chinner 	mlip = xfs_ail_min(ailp);
5410e57f6a3SDave Chinner 
5420e57f6a3SDave Chinner 	for (i = 0; i < nr_items; i++) {
5430e57f6a3SDave Chinner 		struct xfs_log_item *lip = log_items[i];
5440e57f6a3SDave Chinner 		if (lip->li_flags & XFS_LI_IN_AIL) {
5450e57f6a3SDave Chinner 			/* check if we really need to move the item */
5460e57f6a3SDave Chinner 			if (XFS_LSN_CMP(lsn, lip->li_lsn) <= 0)
5470e57f6a3SDave Chinner 				continue;
5480e57f6a3SDave Chinner 
5490e57f6a3SDave Chinner 			xfs_ail_delete(ailp, lip);
5500e57f6a3SDave Chinner 			if (mlip == lip)
5510e57f6a3SDave Chinner 				mlip_changed = 1;
5520e57f6a3SDave Chinner 		} else {
5530e57f6a3SDave Chinner 			lip->li_flags |= XFS_LI_IN_AIL;
5540e57f6a3SDave Chinner 		}
5550e57f6a3SDave Chinner 		lip->li_lsn = lsn;
5560e57f6a3SDave Chinner 		list_add(&lip->li_ail, &tmp);
5570e57f6a3SDave Chinner 	}
5580e57f6a3SDave Chinner 
5590e57f6a3SDave Chinner 	xfs_ail_splice(ailp, &tmp, lsn);
5600e57f6a3SDave Chinner 
5610e57f6a3SDave Chinner 	if (!mlip_changed) {
5620e57f6a3SDave Chinner 		spin_unlock(&ailp->xa_lock);
5630e57f6a3SDave Chinner 		return;
5640e57f6a3SDave Chinner 	}
5650e57f6a3SDave Chinner 
5660e57f6a3SDave Chinner 	/*
5670e57f6a3SDave Chinner 	 * It is not safe to access mlip after the AIL lock is dropped, so we
5680e57f6a3SDave Chinner 	 * must get a copy of li_lsn before we do so.  This is especially
5690e57f6a3SDave Chinner 	 * important on 32-bit platforms where accessing and updating 64-bit
5700e57f6a3SDave Chinner 	 * values like li_lsn is not atomic.
5710e57f6a3SDave Chinner 	 */
5720e57f6a3SDave Chinner 	mlip = xfs_ail_min(ailp);
5730e57f6a3SDave Chinner 	tail_lsn = mlip->li_lsn;
5740e57f6a3SDave Chinner 	spin_unlock(&ailp->xa_lock);
5750e57f6a3SDave Chinner 	xfs_log_move_tail(ailp->xa_mount, tail_lsn);
5760e57f6a3SDave Chinner }
5770e57f6a3SDave Chinner 
5780e57f6a3SDave Chinner /*
5791da177e4SLinus Torvalds  * Delete the given item from the AIL.  It must already be in
5801da177e4SLinus Torvalds  * the AIL.
5811da177e4SLinus Torvalds  *
5821da177e4SLinus Torvalds  * Wakeup anyone with an lsn less than item's lsn.    If the item
5831da177e4SLinus Torvalds  * we delete in the AIL is the minimum one, update the tail lsn in the
5841da177e4SLinus Torvalds  * log manager.
5851da177e4SLinus Torvalds  *
5861da177e4SLinus Torvalds  * Clear the IN_AIL flag from the item, reset its lsn to 0, and
5871da177e4SLinus Torvalds  * bump the AIL's generation count to indicate that the tree
5881da177e4SLinus Torvalds  * has changed.
5891da177e4SLinus Torvalds  *
5901da177e4SLinus Torvalds  * This function must be called with the AIL lock held.  The lock
591287f3dadSDonald Douwsma  * is dropped before returning.
5921da177e4SLinus Torvalds  */
5931da177e4SLinus Torvalds void
594783a2f65SDavid Chinner xfs_trans_ail_delete(
595783a2f65SDavid Chinner 	struct xfs_ail	*ailp,
596c7e8f268SDavid Chinner 	xfs_log_item_t	*lip) __releases(ailp->xa_lock)
5971da177e4SLinus Torvalds {
5981da177e4SLinus Torvalds 	xfs_log_item_t		*mlip;
5996c06f072SNathaniel W. Turner 	xfs_lsn_t		tail_lsn;
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds 	if (lip->li_flags & XFS_LI_IN_AIL) {
602c7e8f268SDavid Chinner 		mlip = xfs_ail_min(ailp);
603eb3efa12SDave Chinner 		xfs_ail_delete(ailp, lip);
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds 
6061da177e4SLinus Torvalds 		lip->li_flags &= ~XFS_LI_IN_AIL;
6071da177e4SLinus Torvalds 		lip->li_lsn = 0;
6081da177e4SLinus Torvalds 
609eb3efa12SDave Chinner 		if (mlip == lip) {
610c7e8f268SDavid Chinner 			mlip = xfs_ail_min(ailp);
6116c06f072SNathaniel W. Turner 			/*
6126c06f072SNathaniel W. Turner 			 * It is not safe to access mlip after the AIL lock
6136c06f072SNathaniel W. Turner 			 * is dropped, so we must get a copy of li_lsn
6146c06f072SNathaniel W. Turner 			 * before we do so.  This is especially important
6156c06f072SNathaniel W. Turner 			 * on 32-bit platforms where accessing and updating
6166c06f072SNathaniel W. Turner 			 * 64-bit values like li_lsn is not atomic.
6176c06f072SNathaniel W. Turner 			 */
6186c06f072SNathaniel W. Turner 			tail_lsn = mlip ? mlip->li_lsn : 0;
619c7e8f268SDavid Chinner 			spin_unlock(&ailp->xa_lock);
6206c06f072SNathaniel W. Turner 			xfs_log_move_tail(ailp->xa_mount, tail_lsn);
6211da177e4SLinus Torvalds 		} else {
622c7e8f268SDavid Chinner 			spin_unlock(&ailp->xa_lock);
6231da177e4SLinus Torvalds 		}
6241da177e4SLinus Torvalds 	}
6251da177e4SLinus Torvalds 	else {
6261da177e4SLinus Torvalds 		/*
6271da177e4SLinus Torvalds 		 * If the file system is not being shutdown, we are in
6281da177e4SLinus Torvalds 		 * serious trouble if we get to this stage.
6291da177e4SLinus Torvalds 		 */
630783a2f65SDavid Chinner 		struct xfs_mount	*mp = ailp->xa_mount;
631783a2f65SDavid Chinner 
632c7e8f268SDavid Chinner 		spin_unlock(&ailp->xa_lock);
633c7e8f268SDavid Chinner 		if (!XFS_FORCED_SHUTDOWN(mp)) {
6341da177e4SLinus Torvalds 			xfs_cmn_err(XFS_PTAG_AILDELETE, CE_ALERT, mp,
6357d04a335SNathan Scott 		"%s: attempting to delete a log item that is not in the AIL",
63634a622b2SHarvey Harrison 					__func__);
6377d04a335SNathan Scott 			xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
6381da177e4SLinus Torvalds 		}
6391da177e4SLinus Torvalds 	}
6401da177e4SLinus Torvalds }
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds 
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds /*
6451da177e4SLinus Torvalds  * The active item list (AIL) is a doubly linked list of log
6461da177e4SLinus Torvalds  * items sorted by ascending lsn.  The base of the list is
6471da177e4SLinus Torvalds  * a forw/back pointer pair embedded in the xfs mount structure.
6481da177e4SLinus Torvalds  * The base is initialized with both pointers pointing to the
6491da177e4SLinus Torvalds  * base.  This case always needs to be distinguished, because
6501da177e4SLinus Torvalds  * the base has no lsn to look at.  We almost always insert
6511da177e4SLinus Torvalds  * at the end of the list, so on inserts we search from the
6521da177e4SLinus Torvalds  * end of the list to find where the new item belongs.
6531da177e4SLinus Torvalds  */
6541da177e4SLinus Torvalds 
6551da177e4SLinus Torvalds /*
6561da177e4SLinus Torvalds  * Initialize the doubly linked list to point only to itself.
6571da177e4SLinus Torvalds  */
658249a8c11SDavid Chinner int
6591da177e4SLinus Torvalds xfs_trans_ail_init(
6601da177e4SLinus Torvalds 	xfs_mount_t	*mp)
6611da177e4SLinus Torvalds {
66282fa9012SDavid Chinner 	struct xfs_ail	*ailp;
66327d8d5feSDavid Chinner 	int		error;
66482fa9012SDavid Chinner 
66582fa9012SDavid Chinner 	ailp = kmem_zalloc(sizeof(struct xfs_ail), KM_MAYFAIL);
66682fa9012SDavid Chinner 	if (!ailp)
66782fa9012SDavid Chinner 		return ENOMEM;
66882fa9012SDavid Chinner 
66982fa9012SDavid Chinner 	ailp->xa_mount = mp;
67082fa9012SDavid Chinner 	INIT_LIST_HEAD(&ailp->xa_ail);
671c7e8f268SDavid Chinner 	spin_lock_init(&ailp->xa_lock);
67227d8d5feSDavid Chinner 	error = xfsaild_start(ailp);
67327d8d5feSDavid Chinner 	if (error)
67427d8d5feSDavid Chinner 		goto out_free_ailp;
67527d8d5feSDavid Chinner 	mp->m_ail = ailp;
67627d8d5feSDavid Chinner 	return 0;
67727d8d5feSDavid Chinner 
67827d8d5feSDavid Chinner out_free_ailp:
67927d8d5feSDavid Chinner 	kmem_free(ailp);
68027d8d5feSDavid Chinner 	return error;
681249a8c11SDavid Chinner }
682249a8c11SDavid Chinner 
683249a8c11SDavid Chinner void
684249a8c11SDavid Chinner xfs_trans_ail_destroy(
685249a8c11SDavid Chinner 	xfs_mount_t	*mp)
686249a8c11SDavid Chinner {
68782fa9012SDavid Chinner 	struct xfs_ail	*ailp = mp->m_ail;
68882fa9012SDavid Chinner 
68982fa9012SDavid Chinner 	xfsaild_stop(ailp);
69082fa9012SDavid Chinner 	kmem_free(ailp);
6911da177e4SLinus Torvalds }
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds /*
6941da177e4SLinus Torvalds  * Insert the given log item into the AIL.
6951da177e4SLinus Torvalds  * We almost always insert at the end of the list, so on inserts
6961da177e4SLinus Torvalds  * we search from the end of the list to find where the
6971da177e4SLinus Torvalds  * new item belongs.
6981da177e4SLinus Torvalds  */
6991da177e4SLinus Torvalds STATIC void
7001da177e4SLinus Torvalds xfs_ail_insert(
70182fa9012SDavid Chinner 	struct xfs_ail	*ailp,
7021da177e4SLinus Torvalds 	xfs_log_item_t	*lip)
7031da177e4SLinus Torvalds {
7041da177e4SLinus Torvalds 	xfs_log_item_t	*next_lip;
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds 	/*
7071da177e4SLinus Torvalds 	 * If the list is empty, just insert the item.
7081da177e4SLinus Torvalds 	 */
709535f6b37SJosef 'Jeff' Sipek 	if (list_empty(&ailp->xa_ail)) {
710535f6b37SJosef 'Jeff' Sipek 		list_add(&lip->li_ail, &ailp->xa_ail);
7111da177e4SLinus Torvalds 		return;
7121da177e4SLinus Torvalds 	}
7131da177e4SLinus Torvalds 
714535f6b37SJosef 'Jeff' Sipek 	list_for_each_entry_reverse(next_lip, &ailp->xa_ail, li_ail) {
715535f6b37SJosef 'Jeff' Sipek 		if (XFS_LSN_CMP(next_lip->li_lsn, lip->li_lsn) <= 0)
716535f6b37SJosef 'Jeff' Sipek 			break;
7171da177e4SLinus Torvalds 	}
7181da177e4SLinus Torvalds 
7190e57f6a3SDave Chinner 	ASSERT(&next_lip->li_ail == &ailp->xa_ail ||
7200e57f6a3SDave Chinner 	       XFS_LSN_CMP(next_lip->li_lsn, lip->li_lsn) <= 0);
721535f6b37SJosef 'Jeff' Sipek 
722535f6b37SJosef 'Jeff' Sipek 	list_add(&lip->li_ail, &next_lip->li_ail);
723535f6b37SJosef 'Jeff' Sipek 
724535f6b37SJosef 'Jeff' Sipek 	xfs_ail_check(ailp, lip);
7251da177e4SLinus Torvalds 	return;
7261da177e4SLinus Torvalds }
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds /*
7290e57f6a3SDave Chinner  * splice the log item list into the AIL at the given LSN.
7300e57f6a3SDave Chinner  */
7310e57f6a3SDave Chinner STATIC void
7320e57f6a3SDave Chinner xfs_ail_splice(
7330e57f6a3SDave Chinner 	struct xfs_ail	*ailp,
7340e57f6a3SDave Chinner 	struct list_head *list,
7350e57f6a3SDave Chinner 	xfs_lsn_t	lsn)
7360e57f6a3SDave Chinner {
7370e57f6a3SDave Chinner 	xfs_log_item_t	*next_lip;
7380e57f6a3SDave Chinner 
7390e57f6a3SDave Chinner 	/*
7400e57f6a3SDave Chinner 	 * If the list is empty, just insert the item.
7410e57f6a3SDave Chinner 	 */
7420e57f6a3SDave Chinner 	if (list_empty(&ailp->xa_ail)) {
7430e57f6a3SDave Chinner 		list_splice(list, &ailp->xa_ail);
7440e57f6a3SDave Chinner 		return;
7450e57f6a3SDave Chinner 	}
7460e57f6a3SDave Chinner 
7470e57f6a3SDave Chinner 	list_for_each_entry_reverse(next_lip, &ailp->xa_ail, li_ail) {
7480e57f6a3SDave Chinner 		if (XFS_LSN_CMP(next_lip->li_lsn, lsn) <= 0)
7490e57f6a3SDave Chinner 			break;
7500e57f6a3SDave Chinner 	}
7510e57f6a3SDave Chinner 
7520e57f6a3SDave Chinner 	ASSERT((&next_lip->li_ail == &ailp->xa_ail) ||
7530e57f6a3SDave Chinner 	       (XFS_LSN_CMP(next_lip->li_lsn, lsn) <= 0));
7540e57f6a3SDave Chinner 
7550e57f6a3SDave Chinner 	list_splice_init(list, &next_lip->li_ail);
7560e57f6a3SDave Chinner 	return;
7570e57f6a3SDave Chinner }
7580e57f6a3SDave Chinner 
7590e57f6a3SDave Chinner /*
7601da177e4SLinus Torvalds  * Delete the given item from the AIL.  Return a pointer to the item.
7611da177e4SLinus Torvalds  */
762eb3efa12SDave Chinner STATIC void
7631da177e4SLinus Torvalds xfs_ail_delete(
76482fa9012SDavid Chinner 	struct xfs_ail	*ailp,
7651da177e4SLinus Torvalds 	xfs_log_item_t	*lip)
7661da177e4SLinus Torvalds {
767535f6b37SJosef 'Jeff' Sipek 	xfs_ail_check(ailp, lip);
768535f6b37SJosef 'Jeff' Sipek 	list_del(&lip->li_ail);
769eb3efa12SDave Chinner 	xfs_trans_ail_cursor_clear(ailp, lip);
7701da177e4SLinus Torvalds }
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds /*
7731da177e4SLinus Torvalds  * Return a pointer to the first item in the AIL.
7741da177e4SLinus Torvalds  * If the AIL is empty, then return NULL.
7751da177e4SLinus Torvalds  */
7761da177e4SLinus Torvalds STATIC xfs_log_item_t *
7771da177e4SLinus Torvalds xfs_ail_min(
77882fa9012SDavid Chinner 	struct xfs_ail	*ailp)
7791da177e4SLinus Torvalds {
780535f6b37SJosef 'Jeff' Sipek 	if (list_empty(&ailp->xa_ail))
7811da177e4SLinus Torvalds 		return NULL;
782535f6b37SJosef 'Jeff' Sipek 
783535f6b37SJosef 'Jeff' Sipek 	return list_first_entry(&ailp->xa_ail, xfs_log_item_t, li_ail);
7841da177e4SLinus Torvalds }
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds /*
7871da177e4SLinus Torvalds  * Return a pointer to the item which follows
7881da177e4SLinus Torvalds  * the given item in the AIL.  If the given item
7891da177e4SLinus Torvalds  * is the last item in the list, then return NULL.
7901da177e4SLinus Torvalds  */
7911da177e4SLinus Torvalds STATIC xfs_log_item_t *
7921da177e4SLinus Torvalds xfs_ail_next(
79382fa9012SDavid Chinner 	struct xfs_ail	*ailp,
7941da177e4SLinus Torvalds 	xfs_log_item_t	*lip)
7951da177e4SLinus Torvalds {
796535f6b37SJosef 'Jeff' Sipek 	if (lip->li_ail.next == &ailp->xa_ail)
7971da177e4SLinus Torvalds 		return NULL;
7981da177e4SLinus Torvalds 
799535f6b37SJosef 'Jeff' Sipek 	return list_first_entry(&lip->li_ail, xfs_log_item_t, li_ail);
8001da177e4SLinus Torvalds }
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds #ifdef DEBUG
8031da177e4SLinus Torvalds /*
8041da177e4SLinus Torvalds  * Check that the list is sorted as it should be.
8051da177e4SLinus Torvalds  */
8061da177e4SLinus Torvalds STATIC void
8071da177e4SLinus Torvalds xfs_ail_check(
80882fa9012SDavid Chinner 	struct xfs_ail	*ailp,
809de08dbc1SDavid Chinner 	xfs_log_item_t	*lip)
8101da177e4SLinus Torvalds {
8111da177e4SLinus Torvalds 	xfs_log_item_t	*prev_lip;
8121da177e4SLinus Torvalds 
813535f6b37SJosef 'Jeff' Sipek 	if (list_empty(&ailp->xa_ail))
8141da177e4SLinus Torvalds 		return;
8151da177e4SLinus Torvalds 
8161da177e4SLinus Torvalds 	/*
817de08dbc1SDavid Chinner 	 * Check the next and previous entries are valid.
818de08dbc1SDavid Chinner 	 */
819de08dbc1SDavid Chinner 	ASSERT((lip->li_flags & XFS_LI_IN_AIL) != 0);
820535f6b37SJosef 'Jeff' Sipek 	prev_lip = list_entry(lip->li_ail.prev, xfs_log_item_t, li_ail);
821535f6b37SJosef 'Jeff' Sipek 	if (&prev_lip->li_ail != &ailp->xa_ail)
822de08dbc1SDavid Chinner 		ASSERT(XFS_LSN_CMP(prev_lip->li_lsn, lip->li_lsn) <= 0);
823535f6b37SJosef 'Jeff' Sipek 
824535f6b37SJosef 'Jeff' Sipek 	prev_lip = list_entry(lip->li_ail.next, xfs_log_item_t, li_ail);
825535f6b37SJosef 'Jeff' Sipek 	if (&prev_lip->li_ail != &ailp->xa_ail)
826de08dbc1SDavid Chinner 		ASSERT(XFS_LSN_CMP(prev_lip->li_lsn, lip->li_lsn) >= 0);
827de08dbc1SDavid Chinner 
828de08dbc1SDavid Chinner 
829de08dbc1SDavid Chinner #ifdef XFS_TRANS_DEBUG
830de08dbc1SDavid Chinner 	/*
831535f6b37SJosef 'Jeff' Sipek 	 * Walk the list checking lsn ordering, and that every entry has the
832535f6b37SJosef 'Jeff' Sipek 	 * XFS_LI_IN_AIL flag set. This is really expensive, so only do it
833535f6b37SJosef 'Jeff' Sipek 	 * when specifically debugging the transaction subsystem.
8341da177e4SLinus Torvalds 	 */
835535f6b37SJosef 'Jeff' Sipek 	prev_lip = list_entry(&ailp->xa_ail, xfs_log_item_t, li_ail);
836535f6b37SJosef 'Jeff' Sipek 	list_for_each_entry(lip, &ailp->xa_ail, li_ail) {
837535f6b37SJosef 'Jeff' Sipek 		if (&prev_lip->li_ail != &ailp->xa_ail)
8381da177e4SLinus Torvalds 			ASSERT(XFS_LSN_CMP(prev_lip->li_lsn, lip->li_lsn) <= 0);
8391da177e4SLinus Torvalds 		ASSERT((lip->li_flags & XFS_LI_IN_AIL) != 0);
8401da177e4SLinus Torvalds 		prev_lip = lip;
8411da177e4SLinus Torvalds 	}
842de08dbc1SDavid Chinner #endif /* XFS_TRANS_DEBUG */
8431da177e4SLinus Torvalds }
8441da177e4SLinus Torvalds #endif /* DEBUG */
845