xref: /openbmc/linux/fs/xfs/xfs_trans_ail.c (revision e9e899a2)
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"
214fb6e8adSChristoph Hellwig #include "xfs_format.h"
22239880efSDave Chinner #include "xfs_log_format.h"
23239880efSDave Chinner #include "xfs_trans_resv.h"
241da177e4SLinus Torvalds #include "xfs_mount.h"
25239880efSDave Chinner #include "xfs_trans.h"
261da177e4SLinus Torvalds #include "xfs_trans_priv.h"
279e4c109aSChristoph Hellwig #include "xfs_trace.h"
28e9e899a2SDarrick J. Wong #include "xfs_errortag.h"
291da177e4SLinus Torvalds #include "xfs_error.h"
30239880efSDave Chinner #include "xfs_log.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 }
60cd4a3c50SDave Chinner #else /* !DEBUG */
61de08dbc1SDavid Chinner #define	xfs_ail_check(a,l)
621da177e4SLinus Torvalds #endif /* DEBUG */
631da177e4SLinus Torvalds 
64cd4a3c50SDave Chinner /*
65fd074841SDave Chinner  * Return a pointer to the last item in the AIL.  If the AIL is empty, then
66fd074841SDave Chinner  * return NULL.
67fd074841SDave Chinner  */
68fd074841SDave Chinner static xfs_log_item_t *
69fd074841SDave Chinner xfs_ail_max(
70fd074841SDave Chinner 	struct xfs_ail  *ailp)
71fd074841SDave Chinner {
72fd074841SDave Chinner 	if (list_empty(&ailp->xa_ail))
73fd074841SDave Chinner 		return NULL;
74fd074841SDave Chinner 
75fd074841SDave Chinner 	return list_entry(ailp->xa_ail.prev, xfs_log_item_t, li_ail);
76fd074841SDave Chinner }
77fd074841SDave Chinner 
78fd074841SDave Chinner /*
79cd4a3c50SDave Chinner  * Return a pointer to the item which follows the given item in the AIL.  If
80cd4a3c50SDave Chinner  * the given item is the last item in the list, then return NULL.
81cd4a3c50SDave Chinner  */
82cd4a3c50SDave Chinner static xfs_log_item_t *
83cd4a3c50SDave Chinner xfs_ail_next(
84cd4a3c50SDave Chinner 	struct xfs_ail  *ailp,
85cd4a3c50SDave Chinner 	xfs_log_item_t  *lip)
86cd4a3c50SDave Chinner {
87cd4a3c50SDave Chinner 	if (lip->li_ail.next == &ailp->xa_ail)
88cd4a3c50SDave Chinner 		return NULL;
89cd4a3c50SDave Chinner 
90cd4a3c50SDave Chinner 	return list_first_entry(&lip->li_ail, xfs_log_item_t, li_ail);
91cd4a3c50SDave Chinner }
92cd4a3c50SDave Chinner 
93cd4a3c50SDave Chinner /*
94cd4a3c50SDave Chinner  * This is called by the log manager code to determine the LSN of the tail of
95cd4a3c50SDave Chinner  * the log.  This is exactly the LSN of the first item in the AIL.  If the AIL
96cd4a3c50SDave Chinner  * is empty, then this function returns 0.
971da177e4SLinus Torvalds  *
98cd4a3c50SDave Chinner  * We need the AIL lock in order to get a coherent read of the lsn of the last
99cd4a3c50SDave Chinner  * item in the AIL.
1001da177e4SLinus Torvalds  */
1011da177e4SLinus Torvalds xfs_lsn_t
102fd074841SDave Chinner xfs_ail_min_lsn(
1035b00f14fSDavid Chinner 	struct xfs_ail	*ailp)
1041da177e4SLinus Torvalds {
105cd4a3c50SDave Chinner 	xfs_lsn_t	lsn = 0;
1061da177e4SLinus Torvalds 	xfs_log_item_t	*lip;
1071da177e4SLinus Torvalds 
108c7e8f268SDavid Chinner 	spin_lock(&ailp->xa_lock);
1095b00f14fSDavid Chinner 	lip = xfs_ail_min(ailp);
110cd4a3c50SDave Chinner 	if (lip)
1111da177e4SLinus Torvalds 		lsn = lip->li_lsn;
112c7e8f268SDavid Chinner 	spin_unlock(&ailp->xa_lock);
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds 	return lsn;
1151da177e4SLinus Torvalds }
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds /*
118fd074841SDave Chinner  * Return the maximum lsn held in the AIL, or zero if the AIL is empty.
119fd074841SDave Chinner  */
120fd074841SDave Chinner static xfs_lsn_t
121fd074841SDave Chinner xfs_ail_max_lsn(
122fd074841SDave Chinner 	struct xfs_ail  *ailp)
123fd074841SDave Chinner {
124fd074841SDave Chinner 	xfs_lsn_t       lsn = 0;
125fd074841SDave Chinner 	xfs_log_item_t  *lip;
126fd074841SDave Chinner 
127fd074841SDave Chinner 	spin_lock(&ailp->xa_lock);
128fd074841SDave Chinner 	lip = xfs_ail_max(ailp);
129fd074841SDave Chinner 	if (lip)
130fd074841SDave Chinner 		lsn = lip->li_lsn;
131fd074841SDave Chinner 	spin_unlock(&ailp->xa_lock);
132fd074841SDave Chinner 
133fd074841SDave Chinner 	return lsn;
134fd074841SDave Chinner }
135fd074841SDave Chinner 
136fd074841SDave Chinner /*
137af3e4022SDave Chinner  * The cursor keeps track of where our current traversal is up to by tracking
138af3e4022SDave Chinner  * the next item in the list for us. However, for this to be safe, removing an
139af3e4022SDave Chinner  * object from the AIL needs to invalidate any cursor that points to it. hence
140af3e4022SDave Chinner  * the traversal cursor needs to be linked to the struct xfs_ail so that
141af3e4022SDave Chinner  * deletion can search all the active cursors for invalidation.
14227d8d5feSDavid Chinner  */
1435b00f14fSDavid Chinner STATIC void
14427d8d5feSDavid Chinner xfs_trans_ail_cursor_init(
14527d8d5feSDavid Chinner 	struct xfs_ail		*ailp,
14627d8d5feSDavid Chinner 	struct xfs_ail_cursor	*cur)
14727d8d5feSDavid Chinner {
14827d8d5feSDavid Chinner 	cur->item = NULL;
149af3e4022SDave Chinner 	list_add_tail(&cur->list, &ailp->xa_cursors);
15027d8d5feSDavid Chinner }
15127d8d5feSDavid Chinner 
15227d8d5feSDavid Chinner /*
153af3e4022SDave Chinner  * Get the next item in the traversal and advance the cursor.  If the cursor
154af3e4022SDave Chinner  * was invalidated (indicated by a lip of 1), restart the traversal.
15527d8d5feSDavid Chinner  */
1565b00f14fSDavid Chinner struct xfs_log_item *
15727d8d5feSDavid Chinner xfs_trans_ail_cursor_next(
15827d8d5feSDavid Chinner 	struct xfs_ail		*ailp,
15927d8d5feSDavid Chinner 	struct xfs_ail_cursor	*cur)
16027d8d5feSDavid Chinner {
16127d8d5feSDavid Chinner 	struct xfs_log_item	*lip = cur->item;
16227d8d5feSDavid Chinner 
163db9d67d6SChristoph Hellwig 	if ((uintptr_t)lip & 1)
16427d8d5feSDavid Chinner 		lip = xfs_ail_min(ailp);
16516b59029SDave Chinner 	if (lip)
16616b59029SDave Chinner 		cur->item = xfs_ail_next(ailp, lip);
16727d8d5feSDavid Chinner 	return lip;
16827d8d5feSDavid Chinner }
16927d8d5feSDavid Chinner 
17027d8d5feSDavid Chinner /*
171af3e4022SDave Chinner  * When the traversal is complete, we need to remove the cursor from the list
172af3e4022SDave Chinner  * of traversing cursors.
17327d8d5feSDavid Chinner  */
17427d8d5feSDavid Chinner void
17527d8d5feSDavid Chinner xfs_trans_ail_cursor_done(
176af3e4022SDave Chinner 	struct xfs_ail_cursor	*cur)
17727d8d5feSDavid Chinner {
178af3e4022SDave Chinner 	cur->item = NULL;
179af3e4022SDave Chinner 	list_del_init(&cur->list);
18027d8d5feSDavid Chinner }
18127d8d5feSDavid Chinner 
18227d8d5feSDavid Chinner /*
183af3e4022SDave Chinner  * Invalidate any cursor that is pointing to this item. This is called when an
184af3e4022SDave Chinner  * item is removed from the AIL. Any cursor pointing to this object is now
185af3e4022SDave Chinner  * invalid and the traversal needs to be terminated so it doesn't reference a
186af3e4022SDave Chinner  * freed object. We set the low bit of the cursor item pointer so we can
187af3e4022SDave Chinner  * distinguish between an invalidation and the end of the list when getting the
188af3e4022SDave Chinner  * next item from the cursor.
1895b00f14fSDavid Chinner  */
1905b00f14fSDavid Chinner STATIC void
1915b00f14fSDavid Chinner xfs_trans_ail_cursor_clear(
1925b00f14fSDavid Chinner 	struct xfs_ail		*ailp,
1935b00f14fSDavid Chinner 	struct xfs_log_item	*lip)
1945b00f14fSDavid Chinner {
1955b00f14fSDavid Chinner 	struct xfs_ail_cursor	*cur;
1965b00f14fSDavid Chinner 
197af3e4022SDave Chinner 	list_for_each_entry(cur, &ailp->xa_cursors, list) {
1985b00f14fSDavid Chinner 		if (cur->item == lip)
1995b00f14fSDavid Chinner 			cur->item = (struct xfs_log_item *)
200db9d67d6SChristoph Hellwig 					((uintptr_t)cur->item | 1);
2015b00f14fSDavid Chinner 	}
2025b00f14fSDavid Chinner }
2035b00f14fSDavid Chinner 
2045b00f14fSDavid Chinner /*
20516b59029SDave Chinner  * Find the first item in the AIL with the given @lsn by searching in ascending
20616b59029SDave Chinner  * LSN order and initialise the cursor to point to the next item for a
20716b59029SDave Chinner  * ascending traversal.  Pass a @lsn of zero to initialise the cursor to the
20816b59029SDave Chinner  * first item in the AIL. Returns NULL if the list is empty.
209249a8c11SDavid Chinner  */
2105b00f14fSDavid Chinner xfs_log_item_t *
2115b00f14fSDavid Chinner xfs_trans_ail_cursor_first(
21227d8d5feSDavid Chinner 	struct xfs_ail		*ailp,
21327d8d5feSDavid Chinner 	struct xfs_ail_cursor	*cur,
214249a8c11SDavid Chinner 	xfs_lsn_t		lsn)
215249a8c11SDavid Chinner {
216249a8c11SDavid Chinner 	xfs_log_item_t		*lip;
217249a8c11SDavid Chinner 
2185b00f14fSDavid Chinner 	xfs_trans_ail_cursor_init(ailp, cur);
21916b59029SDave Chinner 
22016b59029SDave Chinner 	if (lsn == 0) {
22127d8d5feSDavid Chinner 		lip = xfs_ail_min(ailp);
2225b00f14fSDavid Chinner 		goto out;
22316b59029SDave Chinner 	}
224249a8c11SDavid Chinner 
22527d8d5feSDavid Chinner 	list_for_each_entry(lip, &ailp->xa_ail, li_ail) {
2265b00f14fSDavid Chinner 		if (XFS_LSN_CMP(lip->li_lsn, lsn) >= 0)
2277ee49acfSDavid Chinner 			goto out;
2285b00f14fSDavid Chinner 	}
22916b59029SDave Chinner 	return NULL;
23016b59029SDave Chinner 
2315b00f14fSDavid Chinner out:
23216b59029SDave Chinner 	if (lip)
23316b59029SDave Chinner 		cur->item = xfs_ail_next(ailp, lip);
234249a8c11SDavid Chinner 	return lip;
235249a8c11SDavid Chinner }
236535f6b37SJosef 'Jeff' Sipek 
2371d8c95a3SDave Chinner static struct xfs_log_item *
2381d8c95a3SDave Chinner __xfs_trans_ail_cursor_last(
2391d8c95a3SDave Chinner 	struct xfs_ail		*ailp,
2401d8c95a3SDave Chinner 	xfs_lsn_t		lsn)
2411d8c95a3SDave Chinner {
2421d8c95a3SDave Chinner 	xfs_log_item_t		*lip;
2431d8c95a3SDave Chinner 
2441d8c95a3SDave Chinner 	list_for_each_entry_reverse(lip, &ailp->xa_ail, li_ail) {
2451d8c95a3SDave Chinner 		if (XFS_LSN_CMP(lip->li_lsn, lsn) <= 0)
2461d8c95a3SDave Chinner 			return lip;
2471d8c95a3SDave Chinner 	}
2481d8c95a3SDave Chinner 	return NULL;
2491d8c95a3SDave Chinner }
2501d8c95a3SDave Chinner 
2511d8c95a3SDave Chinner /*
25216b59029SDave Chinner  * Find the last item in the AIL with the given @lsn by searching in descending
25316b59029SDave Chinner  * LSN order and initialise the cursor to point to that item.  If there is no
25416b59029SDave Chinner  * item with the value of @lsn, then it sets the cursor to the last item with an
25516b59029SDave Chinner  * LSN lower than @lsn.  Returns NULL if the list is empty.
2561d8c95a3SDave Chinner  */
2571d8c95a3SDave Chinner struct xfs_log_item *
2581d8c95a3SDave Chinner xfs_trans_ail_cursor_last(
2591d8c95a3SDave Chinner 	struct xfs_ail		*ailp,
2601d8c95a3SDave Chinner 	struct xfs_ail_cursor	*cur,
2611d8c95a3SDave Chinner 	xfs_lsn_t		lsn)
2621d8c95a3SDave Chinner {
2631d8c95a3SDave Chinner 	xfs_trans_ail_cursor_init(ailp, cur);
2641d8c95a3SDave Chinner 	cur->item = __xfs_trans_ail_cursor_last(ailp, lsn);
2651d8c95a3SDave Chinner 	return cur->item;
2661d8c95a3SDave Chinner }
2671d8c95a3SDave Chinner 
2681d8c95a3SDave Chinner /*
26916b59029SDave Chinner  * Splice the log item list into the AIL at the given LSN. We splice to the
2701d8c95a3SDave Chinner  * tail of the given LSN to maintain insert order for push traversals. The
2711d8c95a3SDave Chinner  * cursor is optional, allowing repeated updates to the same LSN to avoid
272e44f4112SAlex Elder  * repeated traversals.  This should not be called with an empty list.
273cd4a3c50SDave Chinner  */
274cd4a3c50SDave Chinner static void
275cd4a3c50SDave Chinner xfs_ail_splice(
276cd4a3c50SDave Chinner 	struct xfs_ail		*ailp,
2771d8c95a3SDave Chinner 	struct xfs_ail_cursor	*cur,
278cd4a3c50SDave Chinner 	struct list_head	*list,
279cd4a3c50SDave Chinner 	xfs_lsn_t		lsn)
280cd4a3c50SDave Chinner {
281e44f4112SAlex Elder 	struct xfs_log_item	*lip;
282e44f4112SAlex Elder 
283e44f4112SAlex Elder 	ASSERT(!list_empty(list));
284cd4a3c50SDave Chinner 
2851d8c95a3SDave Chinner 	/*
286e44f4112SAlex Elder 	 * Use the cursor to determine the insertion point if one is
287e44f4112SAlex Elder 	 * provided.  If not, or if the one we got is not valid,
288e44f4112SAlex Elder 	 * find the place in the AIL where the items belong.
2891d8c95a3SDave Chinner 	 */
290e44f4112SAlex Elder 	lip = cur ? cur->item : NULL;
291db9d67d6SChristoph Hellwig 	if (!lip || (uintptr_t)lip & 1)
2921d8c95a3SDave Chinner 		lip = __xfs_trans_ail_cursor_last(ailp, lsn);
2931d8c95a3SDave Chinner 
294e44f4112SAlex Elder 	/*
295e44f4112SAlex Elder 	 * If a cursor is provided, we know we're processing the AIL
296e44f4112SAlex Elder 	 * in lsn order, and future items to be spliced in will
297e44f4112SAlex Elder 	 * follow the last one being inserted now.  Update the
298e44f4112SAlex Elder 	 * cursor to point to that last item, now while we have a
299e44f4112SAlex Elder 	 * reliable pointer to it.
300e44f4112SAlex Elder 	 */
3011d8c95a3SDave Chinner 	if (cur)
302e44f4112SAlex Elder 		cur->item = list_entry(list->prev, struct xfs_log_item, li_ail);
303cd4a3c50SDave Chinner 
3041d8c95a3SDave Chinner 	/*
305e44f4112SAlex Elder 	 * Finally perform the splice.  Unless the AIL was empty,
306e44f4112SAlex Elder 	 * lip points to the item in the AIL _after_ which the new
307e44f4112SAlex Elder 	 * items should go.  If lip is null the AIL was empty, so
308e44f4112SAlex Elder 	 * the new items go at the head of the AIL.
3091d8c95a3SDave Chinner 	 */
310e44f4112SAlex Elder 	if (lip)
3111d8c95a3SDave Chinner 		list_splice(list, &lip->li_ail);
312e44f4112SAlex Elder 	else
313e44f4112SAlex Elder 		list_splice(list, &ailp->xa_ail);
314cd4a3c50SDave Chinner }
315cd4a3c50SDave Chinner 
316cd4a3c50SDave Chinner /*
317cd4a3c50SDave Chinner  * Delete the given item from the AIL.  Return a pointer to the item.
318cd4a3c50SDave Chinner  */
319cd4a3c50SDave Chinner static void
320cd4a3c50SDave Chinner xfs_ail_delete(
321cd4a3c50SDave Chinner 	struct xfs_ail  *ailp,
322cd4a3c50SDave Chinner 	xfs_log_item_t  *lip)
323cd4a3c50SDave Chinner {
324cd4a3c50SDave Chinner 	xfs_ail_check(ailp, lip);
325cd4a3c50SDave Chinner 	list_del(&lip->li_ail);
326cd4a3c50SDave Chinner 	xfs_trans_ail_cursor_clear(ailp, lip);
327cd4a3c50SDave Chinner }
328cd4a3c50SDave Chinner 
3297f4d01f3SBrian Foster static inline uint
3307f4d01f3SBrian Foster xfsaild_push_item(
3317f4d01f3SBrian Foster 	struct xfs_ail		*ailp,
3327f4d01f3SBrian Foster 	struct xfs_log_item	*lip)
3337f4d01f3SBrian Foster {
3347f4d01f3SBrian Foster 	/*
3357f4d01f3SBrian Foster 	 * If log item pinning is enabled, skip the push and track the item as
3367f4d01f3SBrian Foster 	 * pinned. This can help induce head-behind-tail conditions.
3377f4d01f3SBrian Foster 	 */
3387f4d01f3SBrian Foster 	if (XFS_TEST_ERROR(false, ailp->xa_mount, XFS_ERRTAG_LOG_ITEM_PIN))
3397f4d01f3SBrian Foster 		return XFS_ITEM_PINNED;
3407f4d01f3SBrian Foster 
3417f4d01f3SBrian Foster 	return lip->li_ops->iop_push(lip, &ailp->xa_buf_list);
3427f4d01f3SBrian Foster }
3437f4d01f3SBrian Foster 
3440030807cSChristoph Hellwig static long
3450030807cSChristoph Hellwig xfsaild_push(
3460030807cSChristoph Hellwig 	struct xfs_ail		*ailp)
347249a8c11SDavid Chinner {
34882fa9012SDavid Chinner 	xfs_mount_t		*mp = ailp->xa_mount;
349af3e4022SDave Chinner 	struct xfs_ail_cursor	cur;
3509e7004e7SDave Chinner 	xfs_log_item_t		*lip;
3519e7004e7SDave Chinner 	xfs_lsn_t		lsn;
352fe0da767SDave Chinner 	xfs_lsn_t		target;
35343ff2122SChristoph Hellwig 	long			tout;
3549e7004e7SDave Chinner 	int			stuck = 0;
35543ff2122SChristoph Hellwig 	int			flushing = 0;
3569e7004e7SDave Chinner 	int			count = 0;
3571da177e4SLinus Torvalds 
358670ce93fSDave Chinner 	/*
35943ff2122SChristoph Hellwig 	 * If we encountered pinned items or did not finish writing out all
36043ff2122SChristoph Hellwig 	 * buffers the last time we ran, force the log first and wait for it
36143ff2122SChristoph Hellwig 	 * before pushing again.
362670ce93fSDave Chinner 	 */
36343ff2122SChristoph Hellwig 	if (ailp->xa_log_flush && ailp->xa_last_pushed_lsn == 0 &&
36443ff2122SChristoph Hellwig 	    (!list_empty_careful(&ailp->xa_buf_list) ||
36543ff2122SChristoph Hellwig 	     xfs_ail_min_lsn(ailp))) {
366670ce93fSDave Chinner 		ailp->xa_log_flush = 0;
36743ff2122SChristoph Hellwig 
368ff6d6af2SBill O'Donnell 		XFS_STATS_INC(mp, xs_push_ail_flush);
369670ce93fSDave Chinner 		xfs_log_force(mp, XFS_LOG_SYNC);
370670ce93fSDave Chinner 	}
371670ce93fSDave Chinner 
37243ff2122SChristoph Hellwig 	spin_lock(&ailp->xa_lock);
3738375f922SBrian Foster 
3748375f922SBrian Foster 	/* barrier matches the xa_target update in xfs_ail_push() */
3758375f922SBrian Foster 	smp_rmb();
3768375f922SBrian Foster 	target = ailp->xa_target;
3778375f922SBrian Foster 	ailp->xa_target_prev = target;
3788375f922SBrian Foster 
379af3e4022SDave Chinner 	lip = xfs_trans_ail_cursor_first(ailp, &cur, ailp->xa_last_pushed_lsn);
380211e4d43SChristoph Hellwig 	if (!lip) {
3811da177e4SLinus Torvalds 		/*
38243ff2122SChristoph Hellwig 		 * If the AIL is empty or our push has reached the end we are
38343ff2122SChristoph Hellwig 		 * done now.
3841da177e4SLinus Torvalds 		 */
385e4a1e29cSEric Sandeen 		xfs_trans_ail_cursor_done(&cur);
386c7e8f268SDavid Chinner 		spin_unlock(&ailp->xa_lock);
3879e7004e7SDave Chinner 		goto out_done;
3881da177e4SLinus Torvalds 	}
3891da177e4SLinus Torvalds 
390ff6d6af2SBill O'Donnell 	XFS_STATS_INC(mp, xs_push_ail);
3911da177e4SLinus Torvalds 
392249a8c11SDavid Chinner 	lsn = lip->li_lsn;
39350e86686SDave Chinner 	while ((XFS_LSN_CMP(lip->li_lsn, target) <= 0)) {
394249a8c11SDavid Chinner 		int	lock_result;
39543ff2122SChristoph Hellwig 
396249a8c11SDavid Chinner 		/*
397904c17e6SDave Chinner 		 * Note that iop_push may unlock and reacquire the AIL lock.  We
39843ff2122SChristoph Hellwig 		 * rely on the AIL cursor implementation to be able to deal with
39943ff2122SChristoph Hellwig 		 * the dropped lock.
4001da177e4SLinus Torvalds 		 */
4017f4d01f3SBrian Foster 		lock_result = xfsaild_push_item(ailp, lip);
4021da177e4SLinus Torvalds 		switch (lock_result) {
4031da177e4SLinus Torvalds 		case XFS_ITEM_SUCCESS:
404ff6d6af2SBill O'Donnell 			XFS_STATS_INC(mp, xs_push_ail_success);
4059e4c109aSChristoph Hellwig 			trace_xfs_ail_push(lip);
4069e4c109aSChristoph Hellwig 
4070bf6a5bdSDave Chinner 			ailp->xa_last_pushed_lsn = lsn;
4081da177e4SLinus Torvalds 			break;
4091da177e4SLinus Torvalds 
41043ff2122SChristoph Hellwig 		case XFS_ITEM_FLUSHING:
41143ff2122SChristoph Hellwig 			/*
41243ff2122SChristoph Hellwig 			 * The item or its backing buffer is already beeing
41343ff2122SChristoph Hellwig 			 * flushed.  The typical reason for that is that an
41443ff2122SChristoph Hellwig 			 * inode buffer is locked because we already pushed the
41543ff2122SChristoph Hellwig 			 * updates to it as part of inode clustering.
41643ff2122SChristoph Hellwig 			 *
41743ff2122SChristoph Hellwig 			 * We do not want to to stop flushing just because lots
41843ff2122SChristoph Hellwig 			 * of items are already beeing flushed, but we need to
41943ff2122SChristoph Hellwig 			 * re-try the flushing relatively soon if most of the
42043ff2122SChristoph Hellwig 			 * AIL is beeing flushed.
42143ff2122SChristoph Hellwig 			 */
422ff6d6af2SBill O'Donnell 			XFS_STATS_INC(mp, xs_push_ail_flushing);
42343ff2122SChristoph Hellwig 			trace_xfs_ail_flushing(lip);
42417b38471SChristoph Hellwig 
42543ff2122SChristoph Hellwig 			flushing++;
4260bf6a5bdSDave Chinner 			ailp->xa_last_pushed_lsn = lsn;
4271da177e4SLinus Torvalds 			break;
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds 		case XFS_ITEM_PINNED:
430ff6d6af2SBill O'Donnell 			XFS_STATS_INC(mp, xs_push_ail_pinned);
4319e4c109aSChristoph Hellwig 			trace_xfs_ail_pinned(lip);
4329e4c109aSChristoph Hellwig 
433249a8c11SDavid Chinner 			stuck++;
434670ce93fSDave Chinner 			ailp->xa_log_flush++;
4351da177e4SLinus Torvalds 			break;
4361da177e4SLinus Torvalds 		case XFS_ITEM_LOCKED:
437ff6d6af2SBill O'Donnell 			XFS_STATS_INC(mp, xs_push_ail_locked);
4389e4c109aSChristoph Hellwig 			trace_xfs_ail_locked(lip);
43943ff2122SChristoph Hellwig 
440249a8c11SDavid Chinner 			stuck++;
4411da177e4SLinus Torvalds 			break;
4421da177e4SLinus Torvalds 		default:
4431da177e4SLinus Torvalds 			ASSERT(0);
4441da177e4SLinus Torvalds 			break;
4451da177e4SLinus Torvalds 		}
4461da177e4SLinus Torvalds 
447249a8c11SDavid Chinner 		count++;
448249a8c11SDavid Chinner 
449249a8c11SDavid Chinner 		/*
450249a8c11SDavid Chinner 		 * Are there too many items we can't do anything with?
45143ff2122SChristoph Hellwig 		 *
452249a8c11SDavid Chinner 		 * If we we are skipping too many items because we can't flush
453249a8c11SDavid Chinner 		 * them or they are already being flushed, we back off and
454249a8c11SDavid Chinner 		 * given them time to complete whatever operation is being
455249a8c11SDavid Chinner 		 * done. i.e. remove pressure from the AIL while we can't make
456249a8c11SDavid Chinner 		 * progress so traversals don't slow down further inserts and
457249a8c11SDavid Chinner 		 * removals to/from the AIL.
458249a8c11SDavid Chinner 		 *
459249a8c11SDavid Chinner 		 * The value of 100 is an arbitrary magic number based on
460249a8c11SDavid Chinner 		 * observation.
461249a8c11SDavid Chinner 		 */
462249a8c11SDavid Chinner 		if (stuck > 100)
463249a8c11SDavid Chinner 			break;
464249a8c11SDavid Chinner 
465af3e4022SDave Chinner 		lip = xfs_trans_ail_cursor_next(ailp, &cur);
466249a8c11SDavid Chinner 		if (lip == NULL)
467249a8c11SDavid Chinner 			break;
468249a8c11SDavid Chinner 		lsn = lip->li_lsn;
4691da177e4SLinus Torvalds 	}
470e4a1e29cSEric Sandeen 	xfs_trans_ail_cursor_done(&cur);
471c7e8f268SDavid Chinner 	spin_unlock(&ailp->xa_lock);
4721da177e4SLinus Torvalds 
47343ff2122SChristoph Hellwig 	if (xfs_buf_delwri_submit_nowait(&ailp->xa_buf_list))
47443ff2122SChristoph Hellwig 		ailp->xa_log_flush++;
475d808f617SDave Chinner 
47643ff2122SChristoph Hellwig 	if (!count || XFS_LSN_CMP(lsn, target) >= 0) {
4779e7004e7SDave Chinner out_done:
478249a8c11SDavid Chinner 		/*
47943ff2122SChristoph Hellwig 		 * We reached the target or the AIL is empty, so wait a bit
48043ff2122SChristoph Hellwig 		 * longer for I/O to complete and remove pushed items from the
48143ff2122SChristoph Hellwig 		 * AIL before we start the next scan from the start of the AIL.
482249a8c11SDavid Chinner 		 */
483453eac8aSDave Chinner 		tout = 50;
4840bf6a5bdSDave Chinner 		ailp->xa_last_pushed_lsn = 0;
48543ff2122SChristoph Hellwig 	} else if (((stuck + flushing) * 100) / count > 90) {
486249a8c11SDavid Chinner 		/*
48743ff2122SChristoph Hellwig 		 * Either there is a lot of contention on the AIL or we are
48843ff2122SChristoph Hellwig 		 * stuck due to operations in progress. "Stuck" in this case
48943ff2122SChristoph Hellwig 		 * is defined as >90% of the items we tried to push were stuck.
490249a8c11SDavid Chinner 		 *
491249a8c11SDavid Chinner 		 * Backoff a bit more to allow some I/O to complete before
49243ff2122SChristoph Hellwig 		 * restarting from the start of the AIL. This prevents us from
49343ff2122SChristoph Hellwig 		 * spinning on the same items, and if they are pinned will all
49443ff2122SChristoph Hellwig 		 * the restart to issue a log force to unpin the stuck items.
495249a8c11SDavid Chinner 		 */
496453eac8aSDave Chinner 		tout = 20;
497670ce93fSDave Chinner 		ailp->xa_last_pushed_lsn = 0;
49843ff2122SChristoph Hellwig 	} else {
49943ff2122SChristoph Hellwig 		/*
50043ff2122SChristoph Hellwig 		 * Assume we have more work to do in a short while.
50143ff2122SChristoph Hellwig 		 */
50243ff2122SChristoph Hellwig 		tout = 10;
503453eac8aSDave Chinner 	}
5041da177e4SLinus Torvalds 
5050030807cSChristoph Hellwig 	return tout;
5060030807cSChristoph Hellwig }
5070030807cSChristoph Hellwig 
5080030807cSChristoph Hellwig static int
5090030807cSChristoph Hellwig xfsaild(
5100030807cSChristoph Hellwig 	void		*data)
5110030807cSChristoph Hellwig {
5120030807cSChristoph Hellwig 	struct xfs_ail	*ailp = data;
5130030807cSChristoph Hellwig 	long		tout = 0;	/* milliseconds */
5140030807cSChristoph Hellwig 
51543ff2122SChristoph Hellwig 	current->flags |= PF_MEMALLOC;
51618f1df4eSMichal Hocko 	set_freezable();
51743ff2122SChristoph Hellwig 
5180bd89676SHou Tao 	while (1) {
5190030807cSChristoph Hellwig 		if (tout && tout <= 20)
5200bd89676SHou Tao 			set_current_state(TASK_KILLABLE);
5210030807cSChristoph Hellwig 		else
5220bd89676SHou Tao 			set_current_state(TASK_INTERRUPTIBLE);
5230bd89676SHou Tao 
5240bd89676SHou Tao 		/*
5250bd89676SHou Tao 		 * Check kthread_should_stop() after we set the task state
5260bd89676SHou Tao 		 * to guarantee that we either see the stop bit and exit or
5270bd89676SHou Tao 		 * the task state is reset to runnable such that it's not
5280bd89676SHou Tao 		 * scheduled out indefinitely and detects the stop bit at
5290bd89676SHou Tao 		 * next iteration.
5300bd89676SHou Tao 		 *
5310bd89676SHou Tao 		 * A memory barrier is included in above task state set to
5320bd89676SHou Tao 		 * serialize again kthread_stop().
5330bd89676SHou Tao 		 */
5340bd89676SHou Tao 		if (kthread_should_stop()) {
5350bd89676SHou Tao 			__set_current_state(TASK_RUNNING);
5360bd89676SHou Tao 			break;
5370bd89676SHou Tao 		}
5388375f922SBrian Foster 
5398375f922SBrian Foster 		spin_lock(&ailp->xa_lock);
5408375f922SBrian Foster 
5418375f922SBrian Foster 		/*
5428375f922SBrian Foster 		 * Idle if the AIL is empty and we are not racing with a target
5438375f922SBrian Foster 		 * update. We check the AIL after we set the task to a sleep
5448375f922SBrian Foster 		 * state to guarantee that we either catch an xa_target update
5458375f922SBrian Foster 		 * or that a wake_up resets the state to TASK_RUNNING.
5468375f922SBrian Foster 		 * Otherwise, we run the risk of sleeping indefinitely.
5478375f922SBrian Foster 		 *
5488375f922SBrian Foster 		 * The barrier matches the xa_target update in xfs_ail_push().
5498375f922SBrian Foster 		 */
5508375f922SBrian Foster 		smp_rmb();
5518375f922SBrian Foster 		if (!xfs_ail_min(ailp) &&
5528375f922SBrian Foster 		    ailp->xa_target == ailp->xa_target_prev) {
5538375f922SBrian Foster 			spin_unlock(&ailp->xa_lock);
55418f1df4eSMichal Hocko 			freezable_schedule();
5558375f922SBrian Foster 			tout = 0;
5568375f922SBrian Foster 			continue;
5578375f922SBrian Foster 		}
5588375f922SBrian Foster 		spin_unlock(&ailp->xa_lock);
5598375f922SBrian Foster 
5608375f922SBrian Foster 		if (tout)
56118f1df4eSMichal Hocko 			freezable_schedule_timeout(msecs_to_jiffies(tout));
5628375f922SBrian Foster 
5638375f922SBrian Foster 		__set_current_state(TASK_RUNNING);
5640030807cSChristoph Hellwig 
5650030807cSChristoph Hellwig 		try_to_freeze();
5660030807cSChristoph Hellwig 
5670030807cSChristoph Hellwig 		tout = xfsaild_push(ailp);
5680030807cSChristoph Hellwig 	}
5690030807cSChristoph Hellwig 
5700030807cSChristoph Hellwig 	return 0;
5710bf6a5bdSDave Chinner }
5720bf6a5bdSDave Chinner 
5730bf6a5bdSDave Chinner /*
5740bf6a5bdSDave Chinner  * This routine is called to move the tail of the AIL forward.  It does this by
5750bf6a5bdSDave Chinner  * trying to flush items in the AIL whose lsns are below the given
5760bf6a5bdSDave Chinner  * threshold_lsn.
5770bf6a5bdSDave Chinner  *
5780bf6a5bdSDave Chinner  * The push is run asynchronously in a workqueue, which means the caller needs
5790bf6a5bdSDave Chinner  * to handle waiting on the async flush for space to become available.
5800bf6a5bdSDave Chinner  * We don't want to interrupt any push that is in progress, hence we only queue
5810bf6a5bdSDave Chinner  * work if we set the pushing bit approriately.
5820bf6a5bdSDave Chinner  *
5830bf6a5bdSDave Chinner  * We do this unlocked - we only need to know whether there is anything in the
5840bf6a5bdSDave Chinner  * AIL at the time we are called. We don't need to access the contents of
5850bf6a5bdSDave Chinner  * any of the objects, so the lock is not needed.
5860bf6a5bdSDave Chinner  */
5870bf6a5bdSDave Chinner void
588fd074841SDave Chinner xfs_ail_push(
5890bf6a5bdSDave Chinner 	struct xfs_ail	*ailp,
5900bf6a5bdSDave Chinner 	xfs_lsn_t	threshold_lsn)
5910bf6a5bdSDave Chinner {
5920bf6a5bdSDave Chinner 	xfs_log_item_t	*lip;
5930bf6a5bdSDave Chinner 
5940bf6a5bdSDave Chinner 	lip = xfs_ail_min(ailp);
5950bf6a5bdSDave Chinner 	if (!lip || XFS_FORCED_SHUTDOWN(ailp->xa_mount) ||
5960bf6a5bdSDave Chinner 	    XFS_LSN_CMP(threshold_lsn, ailp->xa_target) <= 0)
5970bf6a5bdSDave Chinner 		return;
5980bf6a5bdSDave Chinner 
5990bf6a5bdSDave Chinner 	/*
6000bf6a5bdSDave Chinner 	 * Ensure that the new target is noticed in push code before it clears
6010bf6a5bdSDave Chinner 	 * the XFS_AIL_PUSHING_BIT.
6020bf6a5bdSDave Chinner 	 */
6030bf6a5bdSDave Chinner 	smp_wmb();
604fe0da767SDave Chinner 	xfs_trans_ail_copy_lsn(ailp, &ailp->xa_target, &threshold_lsn);
6050030807cSChristoph Hellwig 	smp_wmb();
6060030807cSChristoph Hellwig 
6070030807cSChristoph Hellwig 	wake_up_process(ailp->xa_task);
6080bf6a5bdSDave Chinner }
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds /*
611fd074841SDave Chinner  * Push out all items in the AIL immediately
612fd074841SDave Chinner  */
613fd074841SDave Chinner void
614fd074841SDave Chinner xfs_ail_push_all(
615fd074841SDave Chinner 	struct xfs_ail  *ailp)
616fd074841SDave Chinner {
617fd074841SDave Chinner 	xfs_lsn_t       threshold_lsn = xfs_ail_max_lsn(ailp);
618fd074841SDave Chinner 
619fd074841SDave Chinner 	if (threshold_lsn)
620fd074841SDave Chinner 		xfs_ail_push(ailp, threshold_lsn);
621fd074841SDave Chinner }
622fd074841SDave Chinner 
623fd074841SDave Chinner /*
624211e4d43SChristoph Hellwig  * Push out all items in the AIL immediately and wait until the AIL is empty.
625211e4d43SChristoph Hellwig  */
626211e4d43SChristoph Hellwig void
627211e4d43SChristoph Hellwig xfs_ail_push_all_sync(
628211e4d43SChristoph Hellwig 	struct xfs_ail  *ailp)
629211e4d43SChristoph Hellwig {
630211e4d43SChristoph Hellwig 	struct xfs_log_item	*lip;
631211e4d43SChristoph Hellwig 	DEFINE_WAIT(wait);
632211e4d43SChristoph Hellwig 
633211e4d43SChristoph Hellwig 	spin_lock(&ailp->xa_lock);
634211e4d43SChristoph Hellwig 	while ((lip = xfs_ail_max(ailp)) != NULL) {
635211e4d43SChristoph Hellwig 		prepare_to_wait(&ailp->xa_empty, &wait, TASK_UNINTERRUPTIBLE);
636211e4d43SChristoph Hellwig 		ailp->xa_target = lip->li_lsn;
637211e4d43SChristoph Hellwig 		wake_up_process(ailp->xa_task);
638211e4d43SChristoph Hellwig 		spin_unlock(&ailp->xa_lock);
639211e4d43SChristoph Hellwig 		schedule();
640211e4d43SChristoph Hellwig 		spin_lock(&ailp->xa_lock);
641211e4d43SChristoph Hellwig 	}
642211e4d43SChristoph Hellwig 	spin_unlock(&ailp->xa_lock);
643211e4d43SChristoph Hellwig 
644211e4d43SChristoph Hellwig 	finish_wait(&ailp->xa_empty, &wait);
645211e4d43SChristoph Hellwig }
646211e4d43SChristoph Hellwig 
647211e4d43SChristoph Hellwig /*
6480e57f6a3SDave Chinner  * xfs_trans_ail_update - bulk AIL insertion operation.
6490e57f6a3SDave Chinner  *
6500e57f6a3SDave Chinner  * @xfs_trans_ail_update takes an array of log items that all need to be
6510e57f6a3SDave Chinner  * positioned at the same LSN in the AIL. If an item is not in the AIL, it will
6520e57f6a3SDave Chinner  * be added.  Otherwise, it will be repositioned  by removing it and re-adding
6530e57f6a3SDave Chinner  * it to the AIL. If we move the first item in the AIL, update the log tail to
6540e57f6a3SDave Chinner  * match the new minimum LSN in the AIL.
6550e57f6a3SDave Chinner  *
6560e57f6a3SDave Chinner  * This function takes the AIL lock once to execute the update operations on
6570e57f6a3SDave Chinner  * all the items in the array, and as such should not be called with the AIL
6580e57f6a3SDave Chinner  * lock held. As a result, once we have the AIL lock, we need to check each log
6590e57f6a3SDave Chinner  * item LSN to confirm it needs to be moved forward in the AIL.
6600e57f6a3SDave Chinner  *
6610e57f6a3SDave Chinner  * To optimise the insert operation, we delete all the items from the AIL in
6620e57f6a3SDave Chinner  * the first pass, moving them into a temporary list, then splice the temporary
6630e57f6a3SDave Chinner  * list into the correct position in the AIL. This avoids needing to do an
6640e57f6a3SDave Chinner  * insert operation on every item.
6650e57f6a3SDave Chinner  *
6660e57f6a3SDave Chinner  * This function must be called with the AIL lock held.  The lock is dropped
6670e57f6a3SDave Chinner  * before returning.
6680e57f6a3SDave Chinner  */
6690e57f6a3SDave Chinner void
6700e57f6a3SDave Chinner xfs_trans_ail_update_bulk(
6710e57f6a3SDave Chinner 	struct xfs_ail		*ailp,
6721d8c95a3SDave Chinner 	struct xfs_ail_cursor	*cur,
6730e57f6a3SDave Chinner 	struct xfs_log_item	**log_items,
6740e57f6a3SDave Chinner 	int			nr_items,
6750e57f6a3SDave Chinner 	xfs_lsn_t		lsn) __releases(ailp->xa_lock)
6760e57f6a3SDave Chinner {
6770e57f6a3SDave Chinner 	xfs_log_item_t		*mlip;
6780e57f6a3SDave Chinner 	int			mlip_changed = 0;
6790e57f6a3SDave Chinner 	int			i;
6800e57f6a3SDave Chinner 	LIST_HEAD(tmp);
6810e57f6a3SDave Chinner 
682e44f4112SAlex Elder 	ASSERT(nr_items > 0);		/* Not required, but true. */
6830e57f6a3SDave Chinner 	mlip = xfs_ail_min(ailp);
6840e57f6a3SDave Chinner 
6850e57f6a3SDave Chinner 	for (i = 0; i < nr_items; i++) {
6860e57f6a3SDave Chinner 		struct xfs_log_item *lip = log_items[i];
6870e57f6a3SDave Chinner 		if (lip->li_flags & XFS_LI_IN_AIL) {
6880e57f6a3SDave Chinner 			/* check if we really need to move the item */
6890e57f6a3SDave Chinner 			if (XFS_LSN_CMP(lsn, lip->li_lsn) <= 0)
6900e57f6a3SDave Chinner 				continue;
6910e57f6a3SDave Chinner 
692750b9c90SDave Chinner 			trace_xfs_ail_move(lip, lip->li_lsn, lsn);
6930e57f6a3SDave Chinner 			xfs_ail_delete(ailp, lip);
6940e57f6a3SDave Chinner 			if (mlip == lip)
6950e57f6a3SDave Chinner 				mlip_changed = 1;
6960e57f6a3SDave Chinner 		} else {
6970e57f6a3SDave Chinner 			lip->li_flags |= XFS_LI_IN_AIL;
698750b9c90SDave Chinner 			trace_xfs_ail_insert(lip, 0, lsn);
6990e57f6a3SDave Chinner 		}
7000e57f6a3SDave Chinner 		lip->li_lsn = lsn;
7010e57f6a3SDave Chinner 		list_add(&lip->li_ail, &tmp);
7020e57f6a3SDave Chinner 	}
7030e57f6a3SDave Chinner 
704e44f4112SAlex Elder 	if (!list_empty(&tmp))
7051d8c95a3SDave Chinner 		xfs_ail_splice(ailp, cur, &tmp, lsn);
7061c304625SChristoph Hellwig 
7071c304625SChristoph Hellwig 	if (mlip_changed) {
7081c304625SChristoph Hellwig 		if (!XFS_FORCED_SHUTDOWN(ailp->xa_mount))
7091c304625SChristoph Hellwig 			xlog_assign_tail_lsn_locked(ailp->xa_mount);
7100e57f6a3SDave Chinner 		spin_unlock(&ailp->xa_lock);
71109a423a3SChristoph Hellwig 
712cfb7cdcaSChristoph Hellwig 		xfs_log_space_wake(ailp->xa_mount);
7131c304625SChristoph Hellwig 	} else {
7141c304625SChristoph Hellwig 		spin_unlock(&ailp->xa_lock);
7150e57f6a3SDave Chinner 	}
7160e57f6a3SDave Chinner }
7170e57f6a3SDave Chinner 
71827af1bbfSChristoph Hellwig bool
71927af1bbfSChristoph Hellwig xfs_ail_delete_one(
72027af1bbfSChristoph Hellwig 	struct xfs_ail		*ailp,
72127af1bbfSChristoph Hellwig 	struct xfs_log_item	*lip)
72227af1bbfSChristoph Hellwig {
72327af1bbfSChristoph Hellwig 	struct xfs_log_item	*mlip = xfs_ail_min(ailp);
72427af1bbfSChristoph Hellwig 
72527af1bbfSChristoph Hellwig 	trace_xfs_ail_delete(lip, mlip->li_lsn, lip->li_lsn);
72627af1bbfSChristoph Hellwig 	xfs_ail_delete(ailp, lip);
727d3a304b6SCarlos Maiolino 	xfs_clear_li_failed(lip);
72827af1bbfSChristoph Hellwig 	lip->li_flags &= ~XFS_LI_IN_AIL;
72927af1bbfSChristoph Hellwig 	lip->li_lsn = 0;
73027af1bbfSChristoph Hellwig 
73127af1bbfSChristoph Hellwig 	return mlip == lip;
73227af1bbfSChristoph Hellwig }
73327af1bbfSChristoph Hellwig 
73427af1bbfSChristoph Hellwig /**
73527af1bbfSChristoph Hellwig  * Remove a log items from the AIL
73630136832SDave Chinner  *
73730136832SDave Chinner  * @xfs_trans_ail_delete_bulk takes an array of log items that all need to
73830136832SDave Chinner  * removed from the AIL. The caller is already holding the AIL lock, and done
73930136832SDave Chinner  * all the checks necessary to ensure the items passed in via @log_items are
74030136832SDave Chinner  * ready for deletion. This includes checking that the items are in the AIL.
74130136832SDave Chinner  *
74230136832SDave Chinner  * For each log item to be removed, unlink it  from the AIL, clear the IN_AIL
74330136832SDave Chinner  * flag from the item and reset the item's lsn to 0. If we remove the first
74430136832SDave Chinner  * item in the AIL, update the log tail to match the new minimum LSN in the
74530136832SDave Chinner  * AIL.
74630136832SDave Chinner  *
74730136832SDave Chinner  * This function will not drop the AIL lock until all items are removed from
74830136832SDave Chinner  * the AIL to minimise the amount of lock traffic on the AIL. This does not
74930136832SDave Chinner  * greatly increase the AIL hold time, but does significantly reduce the amount
75030136832SDave Chinner  * of traffic on the lock, especially during IO completion.
75130136832SDave Chinner  *
75230136832SDave Chinner  * This function must be called with the AIL lock held.  The lock is dropped
75330136832SDave Chinner  * before returning.
75430136832SDave Chinner  */
75530136832SDave Chinner void
75627af1bbfSChristoph Hellwig xfs_trans_ail_delete(
75730136832SDave Chinner 	struct xfs_ail		*ailp,
75827af1bbfSChristoph Hellwig 	struct xfs_log_item	*lip,
75904913fddSDave Chinner 	int			shutdown_type) __releases(ailp->xa_lock)
76030136832SDave Chinner {
76130136832SDave Chinner 	struct xfs_mount	*mp = ailp->xa_mount;
76227af1bbfSChristoph Hellwig 	bool			mlip_changed;
76330136832SDave Chinner 
76427af1bbfSChristoph Hellwig 	if (!(lip->li_flags & XFS_LI_IN_AIL)) {
76530136832SDave Chinner 		spin_unlock(&ailp->xa_lock);
76630136832SDave Chinner 		if (!XFS_FORCED_SHUTDOWN(mp)) {
7676a19d939SDave Chinner 			xfs_alert_tag(mp, XFS_PTAG_AILDELETE,
76830136832SDave Chinner 	"%s: attempting to delete a log item that is not in the AIL",
76930136832SDave Chinner 					__func__);
77004913fddSDave Chinner 			xfs_force_shutdown(mp, shutdown_type);
77130136832SDave Chinner 		}
77230136832SDave Chinner 		return;
77330136832SDave Chinner 	}
77430136832SDave Chinner 
77527af1bbfSChristoph Hellwig 	mlip_changed = xfs_ail_delete_one(ailp, lip);
7761c304625SChristoph Hellwig 	if (mlip_changed) {
77727af1bbfSChristoph Hellwig 		if (!XFS_FORCED_SHUTDOWN(mp))
77827af1bbfSChristoph Hellwig 			xlog_assign_tail_lsn_locked(mp);
779211e4d43SChristoph Hellwig 		if (list_empty(&ailp->xa_ail))
780211e4d43SChristoph Hellwig 			wake_up_all(&ailp->xa_empty);
78130136832SDave Chinner 	}
78227af1bbfSChristoph Hellwig 
78327af1bbfSChristoph Hellwig 	spin_unlock(&ailp->xa_lock);
78427af1bbfSChristoph Hellwig 	if (mlip_changed)
78527af1bbfSChristoph Hellwig 		xfs_log_space_wake(ailp->xa_mount);
78630136832SDave Chinner }
7871da177e4SLinus Torvalds 
788249a8c11SDavid Chinner int
7891da177e4SLinus Torvalds xfs_trans_ail_init(
7901da177e4SLinus Torvalds 	xfs_mount_t	*mp)
7911da177e4SLinus Torvalds {
79282fa9012SDavid Chinner 	struct xfs_ail	*ailp;
79382fa9012SDavid Chinner 
79482fa9012SDavid Chinner 	ailp = kmem_zalloc(sizeof(struct xfs_ail), KM_MAYFAIL);
79582fa9012SDavid Chinner 	if (!ailp)
7962451337dSDave Chinner 		return -ENOMEM;
79782fa9012SDavid Chinner 
79882fa9012SDavid Chinner 	ailp->xa_mount = mp;
79982fa9012SDavid Chinner 	INIT_LIST_HEAD(&ailp->xa_ail);
800af3e4022SDave Chinner 	INIT_LIST_HEAD(&ailp->xa_cursors);
801c7e8f268SDavid Chinner 	spin_lock_init(&ailp->xa_lock);
80243ff2122SChristoph Hellwig 	INIT_LIST_HEAD(&ailp->xa_buf_list);
803211e4d43SChristoph Hellwig 	init_waitqueue_head(&ailp->xa_empty);
8040030807cSChristoph Hellwig 
8050030807cSChristoph Hellwig 	ailp->xa_task = kthread_run(xfsaild, ailp, "xfsaild/%s",
8060030807cSChristoph Hellwig 			ailp->xa_mount->m_fsname);
8070030807cSChristoph Hellwig 	if (IS_ERR(ailp->xa_task))
8080030807cSChristoph Hellwig 		goto out_free_ailp;
8090030807cSChristoph Hellwig 
81027d8d5feSDavid Chinner 	mp->m_ail = ailp;
81127d8d5feSDavid Chinner 	return 0;
8120030807cSChristoph Hellwig 
8130030807cSChristoph Hellwig out_free_ailp:
8140030807cSChristoph Hellwig 	kmem_free(ailp);
8152451337dSDave Chinner 	return -ENOMEM;
816249a8c11SDavid Chinner }
817249a8c11SDavid Chinner 
818249a8c11SDavid Chinner void
819249a8c11SDavid Chinner xfs_trans_ail_destroy(
820249a8c11SDavid Chinner 	xfs_mount_t	*mp)
821249a8c11SDavid Chinner {
82282fa9012SDavid Chinner 	struct xfs_ail	*ailp = mp->m_ail;
82382fa9012SDavid Chinner 
8240030807cSChristoph Hellwig 	kthread_stop(ailp->xa_task);
82582fa9012SDavid Chinner 	kmem_free(ailp);
8261da177e4SLinus Torvalds }
827