xref: /openbmc/linux/fs/xfs/xfs_trans_ail.c (revision 09a423a3)
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"
299e4c109aSChristoph Hellwig #include "xfs_trace.h"
301da177e4SLinus Torvalds #include "xfs_error.h"
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds #ifdef DEBUG
33cd4a3c50SDave Chinner /*
34cd4a3c50SDave Chinner  * Check that the list is sorted as it should be.
35cd4a3c50SDave Chinner  */
36cd4a3c50SDave Chinner STATIC void
37cd4a3c50SDave Chinner xfs_ail_check(
38cd4a3c50SDave Chinner 	struct xfs_ail	*ailp,
39cd4a3c50SDave Chinner 	xfs_log_item_t	*lip)
40cd4a3c50SDave Chinner {
41cd4a3c50SDave Chinner 	xfs_log_item_t	*prev_lip;
42cd4a3c50SDave Chinner 
43cd4a3c50SDave Chinner 	if (list_empty(&ailp->xa_ail))
44cd4a3c50SDave Chinner 		return;
45cd4a3c50SDave Chinner 
46cd4a3c50SDave Chinner 	/*
47cd4a3c50SDave Chinner 	 * Check the next and previous entries are valid.
48cd4a3c50SDave Chinner 	 */
49cd4a3c50SDave Chinner 	ASSERT((lip->li_flags & XFS_LI_IN_AIL) != 0);
50cd4a3c50SDave Chinner 	prev_lip = list_entry(lip->li_ail.prev, xfs_log_item_t, li_ail);
51cd4a3c50SDave Chinner 	if (&prev_lip->li_ail != &ailp->xa_ail)
52cd4a3c50SDave Chinner 		ASSERT(XFS_LSN_CMP(prev_lip->li_lsn, lip->li_lsn) <= 0);
53cd4a3c50SDave Chinner 
54cd4a3c50SDave Chinner 	prev_lip = list_entry(lip->li_ail.next, xfs_log_item_t, li_ail);
55cd4a3c50SDave Chinner 	if (&prev_lip->li_ail != &ailp->xa_ail)
56cd4a3c50SDave Chinner 		ASSERT(XFS_LSN_CMP(prev_lip->li_lsn, lip->li_lsn) >= 0);
57cd4a3c50SDave Chinner 
58cd4a3c50SDave Chinner 
59cd4a3c50SDave Chinner #ifdef XFS_TRANS_DEBUG
60cd4a3c50SDave Chinner 	/*
61cd4a3c50SDave Chinner 	 * Walk the list checking lsn ordering, and that every entry has the
62cd4a3c50SDave Chinner 	 * XFS_LI_IN_AIL flag set. This is really expensive, so only do it
63cd4a3c50SDave Chinner 	 * when specifically debugging the transaction subsystem.
64cd4a3c50SDave Chinner 	 */
65cd4a3c50SDave Chinner 	prev_lip = list_entry(&ailp->xa_ail, xfs_log_item_t, li_ail);
66cd4a3c50SDave Chinner 	list_for_each_entry(lip, &ailp->xa_ail, li_ail) {
67cd4a3c50SDave Chinner 		if (&prev_lip->li_ail != &ailp->xa_ail)
68cd4a3c50SDave Chinner 			ASSERT(XFS_LSN_CMP(prev_lip->li_lsn, lip->li_lsn) <= 0);
69cd4a3c50SDave Chinner 		ASSERT((lip->li_flags & XFS_LI_IN_AIL) != 0);
70cd4a3c50SDave Chinner 		prev_lip = lip;
71cd4a3c50SDave Chinner 	}
72cd4a3c50SDave Chinner #endif /* XFS_TRANS_DEBUG */
73cd4a3c50SDave Chinner }
74cd4a3c50SDave Chinner #else /* !DEBUG */
75de08dbc1SDavid Chinner #define	xfs_ail_check(a,l)
761da177e4SLinus Torvalds #endif /* DEBUG */
771da177e4SLinus Torvalds 
78cd4a3c50SDave Chinner /*
79cd4a3c50SDave Chinner  * Return a pointer to the first item in the AIL.  If the AIL is empty, then
80cd4a3c50SDave Chinner  * return NULL.
81cd4a3c50SDave Chinner  */
82cd4a3c50SDave Chinner static xfs_log_item_t *
83cd4a3c50SDave Chinner xfs_ail_min(
84cd4a3c50SDave Chinner 	struct xfs_ail  *ailp)
85cd4a3c50SDave Chinner {
86cd4a3c50SDave Chinner 	if (list_empty(&ailp->xa_ail))
87cd4a3c50SDave Chinner 		return NULL;
88cd4a3c50SDave Chinner 
89cd4a3c50SDave Chinner 	return list_first_entry(&ailp->xa_ail, xfs_log_item_t, li_ail);
90cd4a3c50SDave Chinner }
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds  /*
93fd074841SDave Chinner  * Return a pointer to the last item in the AIL.  If the AIL is empty, then
94fd074841SDave Chinner  * return NULL.
95fd074841SDave Chinner  */
96fd074841SDave Chinner static xfs_log_item_t *
97fd074841SDave Chinner xfs_ail_max(
98fd074841SDave Chinner 	struct xfs_ail  *ailp)
99fd074841SDave Chinner {
100fd074841SDave Chinner 	if (list_empty(&ailp->xa_ail))
101fd074841SDave Chinner 		return NULL;
102fd074841SDave Chinner 
103fd074841SDave Chinner 	return list_entry(ailp->xa_ail.prev, xfs_log_item_t, li_ail);
104fd074841SDave Chinner }
105fd074841SDave Chinner 
106fd074841SDave Chinner /*
107cd4a3c50SDave Chinner  * Return a pointer to the item which follows the given item in the AIL.  If
108cd4a3c50SDave Chinner  * the given item is the last item in the list, then return NULL.
109cd4a3c50SDave Chinner  */
110cd4a3c50SDave Chinner static xfs_log_item_t *
111cd4a3c50SDave Chinner xfs_ail_next(
112cd4a3c50SDave Chinner 	struct xfs_ail  *ailp,
113cd4a3c50SDave Chinner 	xfs_log_item_t  *lip)
114cd4a3c50SDave Chinner {
115cd4a3c50SDave Chinner 	if (lip->li_ail.next == &ailp->xa_ail)
116cd4a3c50SDave Chinner 		return NULL;
117cd4a3c50SDave Chinner 
118cd4a3c50SDave Chinner 	return list_first_entry(&lip->li_ail, xfs_log_item_t, li_ail);
119cd4a3c50SDave Chinner }
120cd4a3c50SDave Chinner 
121cd4a3c50SDave Chinner /*
122cd4a3c50SDave Chinner  * This is called by the log manager code to determine the LSN of the tail of
123cd4a3c50SDave Chinner  * the log.  This is exactly the LSN of the first item in the AIL.  If the AIL
124cd4a3c50SDave Chinner  * is empty, then this function returns 0.
1251da177e4SLinus Torvalds  *
126cd4a3c50SDave Chinner  * We need the AIL lock in order to get a coherent read of the lsn of the last
127cd4a3c50SDave Chinner  * item in the AIL.
1281da177e4SLinus Torvalds  */
1291da177e4SLinus Torvalds xfs_lsn_t
130fd074841SDave Chinner xfs_ail_min_lsn(
1315b00f14fSDavid Chinner 	struct xfs_ail	*ailp)
1321da177e4SLinus Torvalds {
133cd4a3c50SDave Chinner 	xfs_lsn_t	lsn = 0;
1341da177e4SLinus Torvalds 	xfs_log_item_t	*lip;
1351da177e4SLinus Torvalds 
136c7e8f268SDavid Chinner 	spin_lock(&ailp->xa_lock);
1375b00f14fSDavid Chinner 	lip = xfs_ail_min(ailp);
138cd4a3c50SDave Chinner 	if (lip)
1391da177e4SLinus Torvalds 		lsn = lip->li_lsn;
140c7e8f268SDavid Chinner 	spin_unlock(&ailp->xa_lock);
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds 	return lsn;
1431da177e4SLinus Torvalds }
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds /*
146fd074841SDave Chinner  * Return the maximum lsn held in the AIL, or zero if the AIL is empty.
147fd074841SDave Chinner  */
148fd074841SDave Chinner static xfs_lsn_t
149fd074841SDave Chinner xfs_ail_max_lsn(
150fd074841SDave Chinner 	struct xfs_ail  *ailp)
151fd074841SDave Chinner {
152fd074841SDave Chinner 	xfs_lsn_t       lsn = 0;
153fd074841SDave Chinner 	xfs_log_item_t  *lip;
154fd074841SDave Chinner 
155fd074841SDave Chinner 	spin_lock(&ailp->xa_lock);
156fd074841SDave Chinner 	lip = xfs_ail_max(ailp);
157fd074841SDave Chinner 	if (lip)
158fd074841SDave Chinner 		lsn = lip->li_lsn;
159fd074841SDave Chinner 	spin_unlock(&ailp->xa_lock);
160fd074841SDave Chinner 
161fd074841SDave Chinner 	return lsn;
162fd074841SDave Chinner }
163fd074841SDave Chinner 
164fd074841SDave Chinner /*
165af3e4022SDave Chinner  * The cursor keeps track of where our current traversal is up to by tracking
166af3e4022SDave Chinner  * the next item in the list for us. However, for this to be safe, removing an
167af3e4022SDave Chinner  * object from the AIL needs to invalidate any cursor that points to it. hence
168af3e4022SDave Chinner  * the traversal cursor needs to be linked to the struct xfs_ail so that
169af3e4022SDave Chinner  * deletion can search all the active cursors for invalidation.
17027d8d5feSDavid Chinner  */
1715b00f14fSDavid Chinner STATIC void
17227d8d5feSDavid Chinner xfs_trans_ail_cursor_init(
17327d8d5feSDavid Chinner 	struct xfs_ail		*ailp,
17427d8d5feSDavid Chinner 	struct xfs_ail_cursor	*cur)
17527d8d5feSDavid Chinner {
17627d8d5feSDavid Chinner 	cur->item = NULL;
177af3e4022SDave Chinner 	list_add_tail(&cur->list, &ailp->xa_cursors);
17827d8d5feSDavid Chinner }
17927d8d5feSDavid Chinner 
18027d8d5feSDavid Chinner /*
181af3e4022SDave Chinner  * Get the next item in the traversal and advance the cursor.  If the cursor
182af3e4022SDave Chinner  * was invalidated (indicated by a lip of 1), restart the traversal.
18327d8d5feSDavid Chinner  */
1845b00f14fSDavid Chinner struct xfs_log_item *
18527d8d5feSDavid Chinner xfs_trans_ail_cursor_next(
18627d8d5feSDavid Chinner 	struct xfs_ail		*ailp,
18727d8d5feSDavid Chinner 	struct xfs_ail_cursor	*cur)
18827d8d5feSDavid Chinner {
18927d8d5feSDavid Chinner 	struct xfs_log_item	*lip = cur->item;
19027d8d5feSDavid Chinner 
19127d8d5feSDavid Chinner 	if ((__psint_t)lip & 1)
19227d8d5feSDavid Chinner 		lip = xfs_ail_min(ailp);
19316b59029SDave Chinner 	if (lip)
19416b59029SDave Chinner 		cur->item = xfs_ail_next(ailp, lip);
19527d8d5feSDavid Chinner 	return lip;
19627d8d5feSDavid Chinner }
19727d8d5feSDavid Chinner 
19827d8d5feSDavid Chinner /*
199af3e4022SDave Chinner  * When the traversal is complete, we need to remove the cursor from the list
200af3e4022SDave Chinner  * of traversing cursors.
20127d8d5feSDavid Chinner  */
20227d8d5feSDavid Chinner void
20327d8d5feSDavid Chinner xfs_trans_ail_cursor_done(
20427d8d5feSDavid Chinner 	struct xfs_ail		*ailp,
205af3e4022SDave Chinner 	struct xfs_ail_cursor	*cur)
20627d8d5feSDavid Chinner {
207af3e4022SDave Chinner 	cur->item = NULL;
208af3e4022SDave Chinner 	list_del_init(&cur->list);
20927d8d5feSDavid Chinner }
21027d8d5feSDavid Chinner 
21127d8d5feSDavid Chinner /*
212af3e4022SDave Chinner  * Invalidate any cursor that is pointing to this item. This is called when an
213af3e4022SDave Chinner  * item is removed from the AIL. Any cursor pointing to this object is now
214af3e4022SDave Chinner  * invalid and the traversal needs to be terminated so it doesn't reference a
215af3e4022SDave Chinner  * freed object. We set the low bit of the cursor item pointer so we can
216af3e4022SDave Chinner  * distinguish between an invalidation and the end of the list when getting the
217af3e4022SDave Chinner  * next item from the cursor.
2185b00f14fSDavid Chinner  */
2195b00f14fSDavid Chinner STATIC void
2205b00f14fSDavid Chinner xfs_trans_ail_cursor_clear(
2215b00f14fSDavid Chinner 	struct xfs_ail		*ailp,
2225b00f14fSDavid Chinner 	struct xfs_log_item	*lip)
2235b00f14fSDavid Chinner {
2245b00f14fSDavid Chinner 	struct xfs_ail_cursor	*cur;
2255b00f14fSDavid Chinner 
226af3e4022SDave Chinner 	list_for_each_entry(cur, &ailp->xa_cursors, list) {
2275b00f14fSDavid Chinner 		if (cur->item == lip)
2285b00f14fSDavid Chinner 			cur->item = (struct xfs_log_item *)
2295b00f14fSDavid Chinner 					((__psint_t)cur->item | 1);
2305b00f14fSDavid Chinner 	}
2315b00f14fSDavid Chinner }
2325b00f14fSDavid Chinner 
2335b00f14fSDavid Chinner /*
23416b59029SDave Chinner  * Find the first item in the AIL with the given @lsn by searching in ascending
23516b59029SDave Chinner  * LSN order and initialise the cursor to point to the next item for a
23616b59029SDave Chinner  * ascending traversal.  Pass a @lsn of zero to initialise the cursor to the
23716b59029SDave Chinner  * first item in the AIL. Returns NULL if the list is empty.
238249a8c11SDavid Chinner  */
2395b00f14fSDavid Chinner xfs_log_item_t *
2405b00f14fSDavid Chinner xfs_trans_ail_cursor_first(
24127d8d5feSDavid Chinner 	struct xfs_ail		*ailp,
24227d8d5feSDavid Chinner 	struct xfs_ail_cursor	*cur,
243249a8c11SDavid Chinner 	xfs_lsn_t		lsn)
244249a8c11SDavid Chinner {
245249a8c11SDavid Chinner 	xfs_log_item_t		*lip;
246249a8c11SDavid Chinner 
2475b00f14fSDavid Chinner 	xfs_trans_ail_cursor_init(ailp, cur);
24816b59029SDave Chinner 
24916b59029SDave Chinner 	if (lsn == 0) {
25027d8d5feSDavid Chinner 		lip = xfs_ail_min(ailp);
2515b00f14fSDavid Chinner 		goto out;
25216b59029SDave Chinner 	}
253249a8c11SDavid Chinner 
25427d8d5feSDavid Chinner 	list_for_each_entry(lip, &ailp->xa_ail, li_ail) {
2555b00f14fSDavid Chinner 		if (XFS_LSN_CMP(lip->li_lsn, lsn) >= 0)
2567ee49acfSDavid Chinner 			goto out;
2575b00f14fSDavid Chinner 	}
25816b59029SDave Chinner 	return NULL;
25916b59029SDave Chinner 
2605b00f14fSDavid Chinner out:
26116b59029SDave Chinner 	if (lip)
26216b59029SDave Chinner 		cur->item = xfs_ail_next(ailp, lip);
263249a8c11SDavid Chinner 	return lip;
264249a8c11SDavid Chinner }
265535f6b37SJosef 'Jeff' Sipek 
2661d8c95a3SDave Chinner static struct xfs_log_item *
2671d8c95a3SDave Chinner __xfs_trans_ail_cursor_last(
2681d8c95a3SDave Chinner 	struct xfs_ail		*ailp,
2691d8c95a3SDave Chinner 	xfs_lsn_t		lsn)
2701d8c95a3SDave Chinner {
2711d8c95a3SDave Chinner 	xfs_log_item_t		*lip;
2721d8c95a3SDave Chinner 
2731d8c95a3SDave Chinner 	list_for_each_entry_reverse(lip, &ailp->xa_ail, li_ail) {
2741d8c95a3SDave Chinner 		if (XFS_LSN_CMP(lip->li_lsn, lsn) <= 0)
2751d8c95a3SDave Chinner 			return lip;
2761d8c95a3SDave Chinner 	}
2771d8c95a3SDave Chinner 	return NULL;
2781d8c95a3SDave Chinner }
2791d8c95a3SDave Chinner 
2801d8c95a3SDave Chinner /*
28116b59029SDave Chinner  * Find the last item in the AIL with the given @lsn by searching in descending
28216b59029SDave Chinner  * LSN order and initialise the cursor to point to that item.  If there is no
28316b59029SDave Chinner  * item with the value of @lsn, then it sets the cursor to the last item with an
28416b59029SDave Chinner  * LSN lower than @lsn.  Returns NULL if the list is empty.
2851d8c95a3SDave Chinner  */
2861d8c95a3SDave Chinner struct xfs_log_item *
2871d8c95a3SDave Chinner xfs_trans_ail_cursor_last(
2881d8c95a3SDave Chinner 	struct xfs_ail		*ailp,
2891d8c95a3SDave Chinner 	struct xfs_ail_cursor	*cur,
2901d8c95a3SDave Chinner 	xfs_lsn_t		lsn)
2911d8c95a3SDave Chinner {
2921d8c95a3SDave Chinner 	xfs_trans_ail_cursor_init(ailp, cur);
2931d8c95a3SDave Chinner 	cur->item = __xfs_trans_ail_cursor_last(ailp, lsn);
2941d8c95a3SDave Chinner 	return cur->item;
2951d8c95a3SDave Chinner }
2961d8c95a3SDave Chinner 
2971d8c95a3SDave Chinner /*
29816b59029SDave Chinner  * Splice the log item list into the AIL at the given LSN. We splice to the
2991d8c95a3SDave Chinner  * tail of the given LSN to maintain insert order for push traversals. The
3001d8c95a3SDave Chinner  * cursor is optional, allowing repeated updates to the same LSN to avoid
301e44f4112SAlex Elder  * repeated traversals.  This should not be called with an empty list.
302cd4a3c50SDave Chinner  */
303cd4a3c50SDave Chinner static void
304cd4a3c50SDave Chinner xfs_ail_splice(
305cd4a3c50SDave Chinner 	struct xfs_ail		*ailp,
3061d8c95a3SDave Chinner 	struct xfs_ail_cursor	*cur,
307cd4a3c50SDave Chinner 	struct list_head	*list,
308cd4a3c50SDave Chinner 	xfs_lsn_t		lsn)
309cd4a3c50SDave Chinner {
310e44f4112SAlex Elder 	struct xfs_log_item	*lip;
311e44f4112SAlex Elder 
312e44f4112SAlex Elder 	ASSERT(!list_empty(list));
313cd4a3c50SDave Chinner 
3141d8c95a3SDave Chinner 	/*
315e44f4112SAlex Elder 	 * Use the cursor to determine the insertion point if one is
316e44f4112SAlex Elder 	 * provided.  If not, or if the one we got is not valid,
317e44f4112SAlex Elder 	 * find the place in the AIL where the items belong.
3181d8c95a3SDave Chinner 	 */
319e44f4112SAlex Elder 	lip = cur ? cur->item : NULL;
320e44f4112SAlex Elder 	if (!lip || (__psint_t) lip & 1)
3211d8c95a3SDave Chinner 		lip = __xfs_trans_ail_cursor_last(ailp, lsn);
3221d8c95a3SDave Chinner 
323e44f4112SAlex Elder 	/*
324e44f4112SAlex Elder 	 * If a cursor is provided, we know we're processing the AIL
325e44f4112SAlex Elder 	 * in lsn order, and future items to be spliced in will
326e44f4112SAlex Elder 	 * follow the last one being inserted now.  Update the
327e44f4112SAlex Elder 	 * cursor to point to that last item, now while we have a
328e44f4112SAlex Elder 	 * reliable pointer to it.
329e44f4112SAlex Elder 	 */
3301d8c95a3SDave Chinner 	if (cur)
331e44f4112SAlex Elder 		cur->item = list_entry(list->prev, struct xfs_log_item, li_ail);
332cd4a3c50SDave Chinner 
3331d8c95a3SDave Chinner 	/*
334e44f4112SAlex Elder 	 * Finally perform the splice.  Unless the AIL was empty,
335e44f4112SAlex Elder 	 * lip points to the item in the AIL _after_ which the new
336e44f4112SAlex Elder 	 * items should go.  If lip is null the AIL was empty, so
337e44f4112SAlex Elder 	 * the new items go at the head of the AIL.
3381d8c95a3SDave Chinner 	 */
339e44f4112SAlex Elder 	if (lip)
3401d8c95a3SDave Chinner 		list_splice(list, &lip->li_ail);
341e44f4112SAlex Elder 	else
342e44f4112SAlex Elder 		list_splice(list, &ailp->xa_ail);
343cd4a3c50SDave Chinner }
344cd4a3c50SDave Chinner 
345cd4a3c50SDave Chinner /*
346cd4a3c50SDave Chinner  * Delete the given item from the AIL.  Return a pointer to the item.
347cd4a3c50SDave Chinner  */
348cd4a3c50SDave Chinner static void
349cd4a3c50SDave Chinner xfs_ail_delete(
350cd4a3c50SDave Chinner 	struct xfs_ail  *ailp,
351cd4a3c50SDave Chinner 	xfs_log_item_t  *lip)
352cd4a3c50SDave Chinner {
353cd4a3c50SDave Chinner 	xfs_ail_check(ailp, lip);
354cd4a3c50SDave Chinner 	list_del(&lip->li_ail);
355cd4a3c50SDave Chinner 	xfs_trans_ail_cursor_clear(ailp, lip);
356cd4a3c50SDave Chinner }
357cd4a3c50SDave Chinner 
3580030807cSChristoph Hellwig static long
3590030807cSChristoph Hellwig xfsaild_push(
3600030807cSChristoph Hellwig 	struct xfs_ail		*ailp)
361249a8c11SDavid Chinner {
36282fa9012SDavid Chinner 	xfs_mount_t		*mp = ailp->xa_mount;
363af3e4022SDave Chinner 	struct xfs_ail_cursor	cur;
3649e7004e7SDave Chinner 	xfs_log_item_t		*lip;
3659e7004e7SDave Chinner 	xfs_lsn_t		lsn;
366fe0da767SDave Chinner 	xfs_lsn_t		target;
3679e7004e7SDave Chinner 	long			tout = 10;
3689e7004e7SDave Chinner 	int			stuck = 0;
3699e7004e7SDave Chinner 	int			count = 0;
370d808f617SDave Chinner 	int			push_xfsbufd = 0;
3711da177e4SLinus Torvalds 
372670ce93fSDave Chinner 	/*
373670ce93fSDave Chinner 	 * If last time we ran we encountered pinned items, force the log first
374670ce93fSDave Chinner 	 * and wait for it before pushing again.
375670ce93fSDave Chinner 	 */
376c7e8f268SDavid Chinner 	spin_lock(&ailp->xa_lock);
377670ce93fSDave Chinner 	if (ailp->xa_last_pushed_lsn == 0 && ailp->xa_log_flush &&
378670ce93fSDave Chinner 	    !list_empty(&ailp->xa_ail)) {
379670ce93fSDave Chinner 		ailp->xa_log_flush = 0;
380670ce93fSDave Chinner 		spin_unlock(&ailp->xa_lock);
381670ce93fSDave Chinner 		XFS_STATS_INC(xs_push_ail_flush);
382670ce93fSDave Chinner 		xfs_log_force(mp, XFS_LOG_SYNC);
383670ce93fSDave Chinner 		spin_lock(&ailp->xa_lock);
384670ce93fSDave Chinner 	}
385670ce93fSDave Chinner 
386fe0da767SDave Chinner 	target = ailp->xa_target;
387af3e4022SDave Chinner 	lip = xfs_trans_ail_cursor_first(ailp, &cur, ailp->xa_last_pushed_lsn);
388249a8c11SDavid Chinner 	if (!lip || XFS_FORCED_SHUTDOWN(mp)) {
3891da177e4SLinus Torvalds 		/*
390249a8c11SDavid Chinner 		 * AIL is empty or our push has reached the end.
3911da177e4SLinus Torvalds 		 */
392af3e4022SDave Chinner 		xfs_trans_ail_cursor_done(ailp, &cur);
393c7e8f268SDavid Chinner 		spin_unlock(&ailp->xa_lock);
3949e7004e7SDave Chinner 		goto out_done;
3951da177e4SLinus Torvalds 	}
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 	XFS_STATS_INC(xs_push_ail);
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 	/*
4001da177e4SLinus Torvalds 	 * While the item we are looking at is below the given threshold
401249a8c11SDavid Chinner 	 * try to flush it out. We'd like not to stop until we've at least
4021da177e4SLinus Torvalds 	 * tried to push on everything in the AIL with an LSN less than
403249a8c11SDavid Chinner 	 * the given threshold.
4041da177e4SLinus Torvalds 	 *
405249a8c11SDavid Chinner 	 * However, we will stop after a certain number of pushes and wait
406249a8c11SDavid Chinner 	 * for a reduced timeout to fire before pushing further. This
407249a8c11SDavid Chinner 	 * prevents use from spinning when we can't do anything or there is
408249a8c11SDavid Chinner 	 * lots of contention on the AIL lists.
409249a8c11SDavid Chinner 	 */
410249a8c11SDavid Chinner 	lsn = lip->li_lsn;
41150e86686SDave Chinner 	while ((XFS_LSN_CMP(lip->li_lsn, target) <= 0)) {
412249a8c11SDavid Chinner 		int	lock_result;
413249a8c11SDavid Chinner 		/*
414249a8c11SDavid Chinner 		 * If we can lock the item without sleeping, unlock the AIL
415249a8c11SDavid Chinner 		 * lock and flush the item.  Then re-grab the AIL lock so we
416249a8c11SDavid Chinner 		 * can look for the next item on the AIL. List changes are
417249a8c11SDavid Chinner 		 * handled by the AIL lookup functions internally
418249a8c11SDavid Chinner 		 *
419249a8c11SDavid Chinner 		 * If we can't lock the item, either its holder will flush it
420249a8c11SDavid Chinner 		 * or it is already being flushed or it is being relogged.  In
421249a8c11SDavid Chinner 		 * any of these case it is being taken care of and we can just
422249a8c11SDavid Chinner 		 * skip to the next item in the list.
4231da177e4SLinus Torvalds 		 */
4241da177e4SLinus Torvalds 		lock_result = IOP_TRYLOCK(lip);
425c7e8f268SDavid Chinner 		spin_unlock(&ailp->xa_lock);
4261da177e4SLinus Torvalds 		switch (lock_result) {
4271da177e4SLinus Torvalds 		case XFS_ITEM_SUCCESS:
4281da177e4SLinus Torvalds 			XFS_STATS_INC(xs_push_ail_success);
4299e4c109aSChristoph Hellwig 			trace_xfs_ail_push(lip);
4309e4c109aSChristoph Hellwig 
4311da177e4SLinus Torvalds 			IOP_PUSH(lip);
4320bf6a5bdSDave Chinner 			ailp->xa_last_pushed_lsn = lsn;
4331da177e4SLinus Torvalds 			break;
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds 		case XFS_ITEM_PUSHBUF:
4361da177e4SLinus Torvalds 			XFS_STATS_INC(xs_push_ail_pushbuf);
4379e4c109aSChristoph Hellwig 			trace_xfs_ail_pushbuf(lip);
43817b38471SChristoph Hellwig 
43917b38471SChristoph Hellwig 			if (!IOP_PUSHBUF(lip)) {
4409e4c109aSChristoph Hellwig 				trace_xfs_ail_pushbuf_pinned(lip);
44117b38471SChristoph Hellwig 				stuck++;
4422900b339SAlex Elder 				ailp->xa_log_flush++;
44317b38471SChristoph Hellwig 			} else {
4440bf6a5bdSDave Chinner 				ailp->xa_last_pushed_lsn = lsn;
44517b38471SChristoph Hellwig 			}
446d808f617SDave Chinner 			push_xfsbufd = 1;
4471da177e4SLinus Torvalds 			break;
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 		case XFS_ITEM_PINNED:
4501da177e4SLinus Torvalds 			XFS_STATS_INC(xs_push_ail_pinned);
4519e4c109aSChristoph Hellwig 			trace_xfs_ail_pinned(lip);
4529e4c109aSChristoph Hellwig 
453249a8c11SDavid Chinner 			stuck++;
454670ce93fSDave Chinner 			ailp->xa_log_flush++;
4551da177e4SLinus Torvalds 			break;
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds 		case XFS_ITEM_LOCKED:
4581da177e4SLinus Torvalds 			XFS_STATS_INC(xs_push_ail_locked);
4599e4c109aSChristoph Hellwig 			trace_xfs_ail_locked(lip);
460249a8c11SDavid Chinner 			stuck++;
4611da177e4SLinus Torvalds 			break;
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds 		default:
4641da177e4SLinus Torvalds 			ASSERT(0);
4651da177e4SLinus Torvalds 			break;
4661da177e4SLinus Torvalds 		}
4671da177e4SLinus Torvalds 
468c7e8f268SDavid Chinner 		spin_lock(&ailp->xa_lock);
469249a8c11SDavid Chinner 		/* should we bother continuing? */
470249a8c11SDavid Chinner 		if (XFS_FORCED_SHUTDOWN(mp))
4711da177e4SLinus Torvalds 			break;
472249a8c11SDavid Chinner 		ASSERT(mp->m_log);
4731da177e4SLinus Torvalds 
474249a8c11SDavid Chinner 		count++;
475249a8c11SDavid Chinner 
476249a8c11SDavid Chinner 		/*
477249a8c11SDavid Chinner 		 * Are there too many items we can't do anything with?
478249a8c11SDavid Chinner 		 * If we we are skipping too many items because we can't flush
479249a8c11SDavid Chinner 		 * them or they are already being flushed, we back off and
480249a8c11SDavid Chinner 		 * given them time to complete whatever operation is being
481249a8c11SDavid Chinner 		 * done. i.e. remove pressure from the AIL while we can't make
482249a8c11SDavid Chinner 		 * progress so traversals don't slow down further inserts and
483249a8c11SDavid Chinner 		 * removals to/from the AIL.
484249a8c11SDavid Chinner 		 *
485249a8c11SDavid Chinner 		 * The value of 100 is an arbitrary magic number based on
486249a8c11SDavid Chinner 		 * observation.
487249a8c11SDavid Chinner 		 */
488249a8c11SDavid Chinner 		if (stuck > 100)
489249a8c11SDavid Chinner 			break;
490249a8c11SDavid Chinner 
491af3e4022SDave Chinner 		lip = xfs_trans_ail_cursor_next(ailp, &cur);
492249a8c11SDavid Chinner 		if (lip == NULL)
493249a8c11SDavid Chinner 			break;
494249a8c11SDavid Chinner 		lsn = lip->li_lsn;
4951da177e4SLinus Torvalds 	}
496af3e4022SDave Chinner 	xfs_trans_ail_cursor_done(ailp, &cur);
497c7e8f268SDavid Chinner 	spin_unlock(&ailp->xa_lock);
4981da177e4SLinus Torvalds 
499d808f617SDave Chinner 	if (push_xfsbufd) {
500d808f617SDave Chinner 		/* we've got delayed write buffers to flush */
501d808f617SDave Chinner 		wake_up_process(mp->m_ddev_targp->bt_task);
502d808f617SDave Chinner 	}
503d808f617SDave Chinner 
5040bf6a5bdSDave Chinner 	/* assume we have more work to do in a short while */
5059e7004e7SDave Chinner out_done:
50692d9cd10SDavid Chinner 	if (!count) {
50792d9cd10SDavid Chinner 		/* We're past our target or empty, so idle */
5080bf6a5bdSDave Chinner 		ailp->xa_last_pushed_lsn = 0;
509670ce93fSDave Chinner 		ailp->xa_log_flush = 0;
5100bf6a5bdSDave Chinner 
5110bf6a5bdSDave Chinner 		tout = 50;
51292d9cd10SDavid Chinner 	} else if (XFS_LSN_CMP(lsn, target) >= 0) {
513249a8c11SDavid Chinner 		/*
51492d9cd10SDavid Chinner 		 * We reached the target so wait a bit longer for I/O to
51592d9cd10SDavid Chinner 		 * complete and remove pushed items from the AIL before we
51692d9cd10SDavid Chinner 		 * start the next scan from the start of the AIL.
517249a8c11SDavid Chinner 		 */
518453eac8aSDave Chinner 		tout = 50;
5190bf6a5bdSDave Chinner 		ailp->xa_last_pushed_lsn = 0;
52027d8d5feSDavid Chinner 	} else if ((stuck * 100) / count > 90) {
521249a8c11SDavid Chinner 		/*
522249a8c11SDavid Chinner 		 * Either there is a lot of contention on the AIL or we
523249a8c11SDavid Chinner 		 * are stuck due to operations in progress. "Stuck" in this
524249a8c11SDavid Chinner 		 * case is defined as >90% of the items we tried to push
525249a8c11SDavid Chinner 		 * were stuck.
526249a8c11SDavid Chinner 		 *
527249a8c11SDavid Chinner 		 * Backoff a bit more to allow some I/O to complete before
528670ce93fSDave Chinner 		 * restarting from the start of the AIL. This prevents us
529670ce93fSDave Chinner 		 * from spinning on the same items, and if they are pinned will
530670ce93fSDave Chinner 		 * all the restart to issue a log force to unpin the stuck
531670ce93fSDave Chinner 		 * items.
532249a8c11SDavid Chinner 		 */
533453eac8aSDave Chinner 		tout = 20;
534670ce93fSDave Chinner 		ailp->xa_last_pushed_lsn = 0;
535453eac8aSDave Chinner 	}
5361da177e4SLinus Torvalds 
5370030807cSChristoph Hellwig 	return tout;
5380030807cSChristoph Hellwig }
5390030807cSChristoph Hellwig 
5400030807cSChristoph Hellwig static int
5410030807cSChristoph Hellwig xfsaild(
5420030807cSChristoph Hellwig 	void		*data)
5430030807cSChristoph Hellwig {
5440030807cSChristoph Hellwig 	struct xfs_ail	*ailp = data;
5450030807cSChristoph Hellwig 	long		tout = 0;	/* milliseconds */
5460030807cSChristoph Hellwig 
5470030807cSChristoph Hellwig 	while (!kthread_should_stop()) {
5480030807cSChristoph Hellwig 		if (tout && tout <= 20)
5490030807cSChristoph Hellwig 			__set_current_state(TASK_KILLABLE);
5500030807cSChristoph Hellwig 		else
5510030807cSChristoph Hellwig 			__set_current_state(TASK_INTERRUPTIBLE);
5520030807cSChristoph Hellwig 		schedule_timeout(tout ?
5530030807cSChristoph Hellwig 				 msecs_to_jiffies(tout) : MAX_SCHEDULE_TIMEOUT);
5540030807cSChristoph Hellwig 
5550030807cSChristoph Hellwig 		try_to_freeze();
5560030807cSChristoph Hellwig 
5570030807cSChristoph Hellwig 		tout = xfsaild_push(ailp);
5580030807cSChristoph Hellwig 	}
5590030807cSChristoph Hellwig 
5600030807cSChristoph Hellwig 	return 0;
5610bf6a5bdSDave Chinner }
5620bf6a5bdSDave Chinner 
5630bf6a5bdSDave Chinner /*
5640bf6a5bdSDave Chinner  * This routine is called to move the tail of the AIL forward.  It does this by
5650bf6a5bdSDave Chinner  * trying to flush items in the AIL whose lsns are below the given
5660bf6a5bdSDave Chinner  * threshold_lsn.
5670bf6a5bdSDave Chinner  *
5680bf6a5bdSDave Chinner  * The push is run asynchronously in a workqueue, which means the caller needs
5690bf6a5bdSDave Chinner  * to handle waiting on the async flush for space to become available.
5700bf6a5bdSDave Chinner  * We don't want to interrupt any push that is in progress, hence we only queue
5710bf6a5bdSDave Chinner  * work if we set the pushing bit approriately.
5720bf6a5bdSDave Chinner  *
5730bf6a5bdSDave Chinner  * We do this unlocked - we only need to know whether there is anything in the
5740bf6a5bdSDave Chinner  * AIL at the time we are called. We don't need to access the contents of
5750bf6a5bdSDave Chinner  * any of the objects, so the lock is not needed.
5760bf6a5bdSDave Chinner  */
5770bf6a5bdSDave Chinner void
578fd074841SDave Chinner xfs_ail_push(
5790bf6a5bdSDave Chinner 	struct xfs_ail	*ailp,
5800bf6a5bdSDave Chinner 	xfs_lsn_t	threshold_lsn)
5810bf6a5bdSDave Chinner {
5820bf6a5bdSDave Chinner 	xfs_log_item_t	*lip;
5830bf6a5bdSDave Chinner 
5840bf6a5bdSDave Chinner 	lip = xfs_ail_min(ailp);
5850bf6a5bdSDave Chinner 	if (!lip || XFS_FORCED_SHUTDOWN(ailp->xa_mount) ||
5860bf6a5bdSDave Chinner 	    XFS_LSN_CMP(threshold_lsn, ailp->xa_target) <= 0)
5870bf6a5bdSDave Chinner 		return;
5880bf6a5bdSDave Chinner 
5890bf6a5bdSDave Chinner 	/*
5900bf6a5bdSDave Chinner 	 * Ensure that the new target is noticed in push code before it clears
5910bf6a5bdSDave Chinner 	 * the XFS_AIL_PUSHING_BIT.
5920bf6a5bdSDave Chinner 	 */
5930bf6a5bdSDave Chinner 	smp_wmb();
594fe0da767SDave Chinner 	xfs_trans_ail_copy_lsn(ailp, &ailp->xa_target, &threshold_lsn);
5950030807cSChristoph Hellwig 	smp_wmb();
5960030807cSChristoph Hellwig 
5970030807cSChristoph Hellwig 	wake_up_process(ailp->xa_task);
5980bf6a5bdSDave Chinner }
5991da177e4SLinus Torvalds 
6001da177e4SLinus Torvalds /*
601fd074841SDave Chinner  * Push out all items in the AIL immediately
602fd074841SDave Chinner  */
603fd074841SDave Chinner void
604fd074841SDave Chinner xfs_ail_push_all(
605fd074841SDave Chinner 	struct xfs_ail  *ailp)
606fd074841SDave Chinner {
607fd074841SDave Chinner 	xfs_lsn_t       threshold_lsn = xfs_ail_max_lsn(ailp);
608fd074841SDave Chinner 
609fd074841SDave Chinner 	if (threshold_lsn)
610fd074841SDave Chinner 		xfs_ail_push(ailp, threshold_lsn);
611fd074841SDave Chinner }
612fd074841SDave Chinner 
613fd074841SDave Chinner /*
6141da177e4SLinus Torvalds  * This is to be called when an item is unlocked that may have
6151da177e4SLinus Torvalds  * been in the AIL.  It will wake up the first member of the AIL
6161da177e4SLinus Torvalds  * wait list if this item's unlocking might allow it to progress.
6171da177e4SLinus Torvalds  * If the item is in the AIL, then we need to get the AIL lock
6181da177e4SLinus Torvalds  * while doing our checking so we don't race with someone going
6191da177e4SLinus Torvalds  * to sleep waiting for this event in xfs_trans_push_ail().
6201da177e4SLinus Torvalds  */
6211da177e4SLinus Torvalds void
6221da177e4SLinus Torvalds xfs_trans_unlocked_item(
623783a2f65SDavid Chinner 	struct xfs_ail	*ailp,
6241da177e4SLinus Torvalds 	xfs_log_item_t	*lip)
6251da177e4SLinus Torvalds {
6261da177e4SLinus Torvalds 	xfs_log_item_t	*min_lip;
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds 	/*
6291da177e4SLinus Torvalds 	 * If we're forcibly shutting down, we may have
6301da177e4SLinus Torvalds 	 * unlocked log items arbitrarily. The last thing
6311da177e4SLinus Torvalds 	 * we want to do is to move the tail of the log
6321da177e4SLinus Torvalds 	 * over some potentially valid data.
6331da177e4SLinus Torvalds 	 */
6341da177e4SLinus Torvalds 	if (!(lip->li_flags & XFS_LI_IN_AIL) ||
635783a2f65SDavid Chinner 	    XFS_FORCED_SHUTDOWN(ailp->xa_mount)) {
6361da177e4SLinus Torvalds 		return;
6371da177e4SLinus Torvalds 	}
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds 	/*
6401da177e4SLinus Torvalds 	 * This is the one case where we can call into xfs_ail_min()
6411da177e4SLinus Torvalds 	 * without holding the AIL lock because we only care about the
6421da177e4SLinus Torvalds 	 * case where we are at the tail of the AIL.  If the object isn't
6431da177e4SLinus Torvalds 	 * at the tail, it doesn't matter what result we get back.  This
6441da177e4SLinus Torvalds 	 * is slightly racy because since we were just unlocked, we could
6451da177e4SLinus Torvalds 	 * go to sleep between the call to xfs_ail_min and the call to
64609a423a3SChristoph Hellwig 	 * xfs_log_space_wake, have someone else lock us, commit to us disk,
6471da177e4SLinus Torvalds 	 * move us out of the tail of the AIL, and then we wake up.  However,
64809a423a3SChristoph Hellwig 	 * the call to xfs_log_space_wake() doesn't do anything if there's
6491da177e4SLinus Torvalds 	 * not enough free space to wake people up so we're safe calling it.
6501da177e4SLinus Torvalds 	 */
651783a2f65SDavid Chinner 	min_lip = xfs_ail_min(ailp);
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds 	if (min_lip == lip)
65409a423a3SChristoph Hellwig 		xfs_log_space_wake(ailp->xa_mount, true);
6551da177e4SLinus Torvalds }	/* xfs_trans_unlocked_item */
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds /*
6580e57f6a3SDave Chinner  * xfs_trans_ail_update - bulk AIL insertion operation.
6590e57f6a3SDave Chinner  *
6600e57f6a3SDave Chinner  * @xfs_trans_ail_update takes an array of log items that all need to be
6610e57f6a3SDave Chinner  * positioned at the same LSN in the AIL. If an item is not in the AIL, it will
6620e57f6a3SDave Chinner  * be added.  Otherwise, it will be repositioned  by removing it and re-adding
6630e57f6a3SDave Chinner  * it to the AIL. If we move the first item in the AIL, update the log tail to
6640e57f6a3SDave Chinner  * match the new minimum LSN in the AIL.
6650e57f6a3SDave Chinner  *
6660e57f6a3SDave Chinner  * This function takes the AIL lock once to execute the update operations on
6670e57f6a3SDave Chinner  * all the items in the array, and as such should not be called with the AIL
6680e57f6a3SDave Chinner  * lock held. As a result, once we have the AIL lock, we need to check each log
6690e57f6a3SDave Chinner  * item LSN to confirm it needs to be moved forward in the AIL.
6700e57f6a3SDave Chinner  *
6710e57f6a3SDave Chinner  * To optimise the insert operation, we delete all the items from the AIL in
6720e57f6a3SDave Chinner  * the first pass, moving them into a temporary list, then splice the temporary
6730e57f6a3SDave Chinner  * list into the correct position in the AIL. This avoids needing to do an
6740e57f6a3SDave Chinner  * insert operation on every item.
6750e57f6a3SDave Chinner  *
6760e57f6a3SDave Chinner  * This function must be called with the AIL lock held.  The lock is dropped
6770e57f6a3SDave Chinner  * before returning.
6780e57f6a3SDave Chinner  */
6790e57f6a3SDave Chinner void
6800e57f6a3SDave Chinner xfs_trans_ail_update_bulk(
6810e57f6a3SDave Chinner 	struct xfs_ail		*ailp,
6821d8c95a3SDave Chinner 	struct xfs_ail_cursor	*cur,
6830e57f6a3SDave Chinner 	struct xfs_log_item	**log_items,
6840e57f6a3SDave Chinner 	int			nr_items,
6850e57f6a3SDave Chinner 	xfs_lsn_t		lsn) __releases(ailp->xa_lock)
6860e57f6a3SDave Chinner {
6870e57f6a3SDave Chinner 	xfs_log_item_t		*mlip;
6880e57f6a3SDave Chinner 	int			mlip_changed = 0;
6890e57f6a3SDave Chinner 	int			i;
6900e57f6a3SDave Chinner 	LIST_HEAD(tmp);
6910e57f6a3SDave Chinner 
692e44f4112SAlex Elder 	ASSERT(nr_items > 0);		/* Not required, but true. */
6930e57f6a3SDave Chinner 	mlip = xfs_ail_min(ailp);
6940e57f6a3SDave Chinner 
6950e57f6a3SDave Chinner 	for (i = 0; i < nr_items; i++) {
6960e57f6a3SDave Chinner 		struct xfs_log_item *lip = log_items[i];
6970e57f6a3SDave Chinner 		if (lip->li_flags & XFS_LI_IN_AIL) {
6980e57f6a3SDave Chinner 			/* check if we really need to move the item */
6990e57f6a3SDave Chinner 			if (XFS_LSN_CMP(lsn, lip->li_lsn) <= 0)
7000e57f6a3SDave Chinner 				continue;
7010e57f6a3SDave Chinner 
7020e57f6a3SDave Chinner 			xfs_ail_delete(ailp, lip);
7030e57f6a3SDave Chinner 			if (mlip == lip)
7040e57f6a3SDave Chinner 				mlip_changed = 1;
7050e57f6a3SDave Chinner 		} else {
7060e57f6a3SDave Chinner 			lip->li_flags |= XFS_LI_IN_AIL;
7070e57f6a3SDave Chinner 		}
7080e57f6a3SDave Chinner 		lip->li_lsn = lsn;
7090e57f6a3SDave Chinner 		list_add(&lip->li_ail, &tmp);
7100e57f6a3SDave Chinner 	}
7110e57f6a3SDave Chinner 
712e44f4112SAlex Elder 	if (!list_empty(&tmp))
7131d8c95a3SDave Chinner 		xfs_ail_splice(ailp, cur, &tmp, lsn);
7140e57f6a3SDave Chinner 	spin_unlock(&ailp->xa_lock);
71509a423a3SChristoph Hellwig 
71609a423a3SChristoph Hellwig 	if (mlip_changed && !XFS_FORCED_SHUTDOWN(ailp->xa_mount)) {
71709a423a3SChristoph Hellwig 		xlog_assign_tail_lsn(ailp->xa_mount);
71809a423a3SChristoph Hellwig 		xfs_log_space_wake(ailp->xa_mount, false);
7190e57f6a3SDave Chinner 	}
7200e57f6a3SDave Chinner }
7210e57f6a3SDave Chinner 
7220e57f6a3SDave Chinner /*
72330136832SDave Chinner  * xfs_trans_ail_delete_bulk - remove multiple log items from the AIL
72430136832SDave Chinner  *
72530136832SDave Chinner  * @xfs_trans_ail_delete_bulk takes an array of log items that all need to
72630136832SDave Chinner  * removed from the AIL. The caller is already holding the AIL lock, and done
72730136832SDave Chinner  * all the checks necessary to ensure the items passed in via @log_items are
72830136832SDave Chinner  * ready for deletion. This includes checking that the items are in the AIL.
72930136832SDave Chinner  *
73030136832SDave Chinner  * For each log item to be removed, unlink it  from the AIL, clear the IN_AIL
73130136832SDave Chinner  * flag from the item and reset the item's lsn to 0. If we remove the first
73230136832SDave Chinner  * item in the AIL, update the log tail to match the new minimum LSN in the
73330136832SDave Chinner  * AIL.
73430136832SDave Chinner  *
73530136832SDave Chinner  * This function will not drop the AIL lock until all items are removed from
73630136832SDave Chinner  * the AIL to minimise the amount of lock traffic on the AIL. This does not
73730136832SDave Chinner  * greatly increase the AIL hold time, but does significantly reduce the amount
73830136832SDave Chinner  * of traffic on the lock, especially during IO completion.
73930136832SDave Chinner  *
74030136832SDave Chinner  * This function must be called with the AIL lock held.  The lock is dropped
74130136832SDave Chinner  * before returning.
74230136832SDave Chinner  */
74330136832SDave Chinner void
74430136832SDave Chinner xfs_trans_ail_delete_bulk(
74530136832SDave Chinner 	struct xfs_ail		*ailp,
74630136832SDave Chinner 	struct xfs_log_item	**log_items,
74730136832SDave Chinner 	int			nr_items) __releases(ailp->xa_lock)
74830136832SDave Chinner {
74930136832SDave Chinner 	xfs_log_item_t		*mlip;
75030136832SDave Chinner 	int			mlip_changed = 0;
75130136832SDave Chinner 	int			i;
75230136832SDave Chinner 
75330136832SDave Chinner 	mlip = xfs_ail_min(ailp);
75430136832SDave Chinner 
75530136832SDave Chinner 	for (i = 0; i < nr_items; i++) {
75630136832SDave Chinner 		struct xfs_log_item *lip = log_items[i];
75730136832SDave Chinner 		if (!(lip->li_flags & XFS_LI_IN_AIL)) {
75830136832SDave Chinner 			struct xfs_mount	*mp = ailp->xa_mount;
75930136832SDave Chinner 
76030136832SDave Chinner 			spin_unlock(&ailp->xa_lock);
76130136832SDave Chinner 			if (!XFS_FORCED_SHUTDOWN(mp)) {
7626a19d939SDave Chinner 				xfs_alert_tag(mp, XFS_PTAG_AILDELETE,
76330136832SDave Chinner 		"%s: attempting to delete a log item that is not in the AIL",
76430136832SDave Chinner 						__func__);
76530136832SDave Chinner 				xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
76630136832SDave Chinner 			}
76730136832SDave Chinner 			return;
76830136832SDave Chinner 		}
76930136832SDave Chinner 
77030136832SDave Chinner 		xfs_ail_delete(ailp, lip);
77130136832SDave Chinner 		lip->li_flags &= ~XFS_LI_IN_AIL;
77230136832SDave Chinner 		lip->li_lsn = 0;
77330136832SDave Chinner 		if (mlip == lip)
77430136832SDave Chinner 			mlip_changed = 1;
77530136832SDave Chinner 	}
77630136832SDave Chinner 	spin_unlock(&ailp->xa_lock);
77709a423a3SChristoph Hellwig 
77809a423a3SChristoph Hellwig 	if (mlip_changed && !XFS_FORCED_SHUTDOWN(ailp->xa_mount)) {
77909a423a3SChristoph Hellwig 		xlog_assign_tail_lsn(ailp->xa_mount);
78009a423a3SChristoph Hellwig 		xfs_log_space_wake(ailp->xa_mount, false);
78130136832SDave Chinner 	}
78230136832SDave Chinner }
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds /*
7851da177e4SLinus Torvalds  * The active item list (AIL) is a doubly linked list of log
7861da177e4SLinus Torvalds  * items sorted by ascending lsn.  The base of the list is
7871da177e4SLinus Torvalds  * a forw/back pointer pair embedded in the xfs mount structure.
7881da177e4SLinus Torvalds  * The base is initialized with both pointers pointing to the
7891da177e4SLinus Torvalds  * base.  This case always needs to be distinguished, because
7901da177e4SLinus Torvalds  * the base has no lsn to look at.  We almost always insert
7911da177e4SLinus Torvalds  * at the end of the list, so on inserts we search from the
7921da177e4SLinus Torvalds  * end of the list to find where the new item belongs.
7931da177e4SLinus Torvalds  */
7941da177e4SLinus Torvalds 
7951da177e4SLinus Torvalds /*
7961da177e4SLinus Torvalds  * Initialize the doubly linked list to point only to itself.
7971da177e4SLinus Torvalds  */
798249a8c11SDavid Chinner int
7991da177e4SLinus Torvalds xfs_trans_ail_init(
8001da177e4SLinus Torvalds 	xfs_mount_t	*mp)
8011da177e4SLinus Torvalds {
80282fa9012SDavid Chinner 	struct xfs_ail	*ailp;
80382fa9012SDavid Chinner 
80482fa9012SDavid Chinner 	ailp = kmem_zalloc(sizeof(struct xfs_ail), KM_MAYFAIL);
80582fa9012SDavid Chinner 	if (!ailp)
80682fa9012SDavid Chinner 		return ENOMEM;
80782fa9012SDavid Chinner 
80882fa9012SDavid Chinner 	ailp->xa_mount = mp;
80982fa9012SDavid Chinner 	INIT_LIST_HEAD(&ailp->xa_ail);
810af3e4022SDave Chinner 	INIT_LIST_HEAD(&ailp->xa_cursors);
811c7e8f268SDavid Chinner 	spin_lock_init(&ailp->xa_lock);
8120030807cSChristoph Hellwig 
8130030807cSChristoph Hellwig 	ailp->xa_task = kthread_run(xfsaild, ailp, "xfsaild/%s",
8140030807cSChristoph Hellwig 			ailp->xa_mount->m_fsname);
8150030807cSChristoph Hellwig 	if (IS_ERR(ailp->xa_task))
8160030807cSChristoph Hellwig 		goto out_free_ailp;
8170030807cSChristoph Hellwig 
81827d8d5feSDavid Chinner 	mp->m_ail = ailp;
81927d8d5feSDavid Chinner 	return 0;
8200030807cSChristoph Hellwig 
8210030807cSChristoph Hellwig out_free_ailp:
8220030807cSChristoph Hellwig 	kmem_free(ailp);
8230030807cSChristoph Hellwig 	return ENOMEM;
824249a8c11SDavid Chinner }
825249a8c11SDavid Chinner 
826249a8c11SDavid Chinner void
827249a8c11SDavid Chinner xfs_trans_ail_destroy(
828249a8c11SDavid Chinner 	xfs_mount_t	*mp)
829249a8c11SDavid Chinner {
83082fa9012SDavid Chinner 	struct xfs_ail	*ailp = mp->m_ail;
83182fa9012SDavid Chinner 
8320030807cSChristoph Hellwig 	kthread_stop(ailp->xa_task);
83382fa9012SDavid Chinner 	kmem_free(ailp);
8341da177e4SLinus Torvalds }
835