xref: /openbmc/linux/fs/xfs/xfs_trans_ail.c (revision 10a98cb1)
10b61f8a4SDave Chinner // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
37b718769SNathan Scott  * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
4c7e8f268SDavid Chinner  * Copyright (c) 2008 Dave Chinner
57b718769SNathan Scott  * All Rights Reserved.
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds #include "xfs.h"
8a844f451SNathan Scott #include "xfs_fs.h"
95467b34bSDarrick J. Wong #include "xfs_shared.h"
104fb6e8adSChristoph Hellwig #include "xfs_format.h"
11239880efSDave Chinner #include "xfs_log_format.h"
12239880efSDave Chinner #include "xfs_trans_resv.h"
131da177e4SLinus Torvalds #include "xfs_mount.h"
14239880efSDave Chinner #include "xfs_trans.h"
151da177e4SLinus Torvalds #include "xfs_trans_priv.h"
169e4c109aSChristoph Hellwig #include "xfs_trace.h"
17e9e899a2SDarrick J. Wong #include "xfs_errortag.h"
181da177e4SLinus Torvalds #include "xfs_error.h"
19239880efSDave Chinner #include "xfs_log.h"
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds #ifdef DEBUG
22cd4a3c50SDave Chinner /*
23cd4a3c50SDave Chinner  * Check that the list is sorted as it should be.
24d686d12dSDave Chinner  *
25d686d12dSDave Chinner  * Called with the ail lock held, but we don't want to assert fail with it
26d686d12dSDave Chinner  * held otherwise we'll lock everything up and won't be able to debug the
27d686d12dSDave Chinner  * cause. Hence we sample and check the state under the AIL lock and return if
28d686d12dSDave Chinner  * everything is fine, otherwise we drop the lock and run the ASSERT checks.
29d686d12dSDave Chinner  * Asserts may not be fatal, so pick the lock back up and continue onwards.
30cd4a3c50SDave Chinner  */
31cd4a3c50SDave Chinner STATIC void
32cd4a3c50SDave Chinner xfs_ail_check(
33cd4a3c50SDave Chinner 	struct xfs_ail		*ailp,
34d686d12dSDave Chinner 	struct xfs_log_item	*lip)
35daebba1bSJules Irenge 	__must_hold(&ailp->ail_lock)
36cd4a3c50SDave Chinner {
37d686d12dSDave Chinner 	struct xfs_log_item	*prev_lip;
38d686d12dSDave Chinner 	struct xfs_log_item	*next_lip;
39d686d12dSDave Chinner 	xfs_lsn_t		prev_lsn = NULLCOMMITLSN;
40d686d12dSDave Chinner 	xfs_lsn_t		next_lsn = NULLCOMMITLSN;
41d686d12dSDave Chinner 	xfs_lsn_t		lsn;
42d686d12dSDave Chinner 	bool			in_ail;
43d686d12dSDave Chinner 
44cd4a3c50SDave Chinner 
4557e80956SMatthew Wilcox 	if (list_empty(&ailp->ail_head))
46cd4a3c50SDave Chinner 		return;
47cd4a3c50SDave Chinner 
48cd4a3c50SDave Chinner 	/*
49d686d12dSDave Chinner 	 * Sample then check the next and previous entries are valid.
50cd4a3c50SDave Chinner 	 */
51d686d12dSDave Chinner 	in_ail = test_bit(XFS_LI_IN_AIL, &lip->li_flags);
52d686d12dSDave Chinner 	prev_lip = list_entry(lip->li_ail.prev, struct xfs_log_item, li_ail);
5357e80956SMatthew Wilcox 	if (&prev_lip->li_ail != &ailp->ail_head)
54d686d12dSDave Chinner 		prev_lsn = prev_lip->li_lsn;
55d686d12dSDave Chinner 	next_lip = list_entry(lip->li_ail.next, struct xfs_log_item, li_ail);
56d686d12dSDave Chinner 	if (&next_lip->li_ail != &ailp->ail_head)
57d686d12dSDave Chinner 		next_lsn = next_lip->li_lsn;
58d686d12dSDave Chinner 	lsn = lip->li_lsn;
59cd4a3c50SDave Chinner 
60d686d12dSDave Chinner 	if (in_ail &&
61d686d12dSDave Chinner 	    (prev_lsn == NULLCOMMITLSN || XFS_LSN_CMP(prev_lsn, lsn) <= 0) &&
62d686d12dSDave Chinner 	    (next_lsn == NULLCOMMITLSN || XFS_LSN_CMP(next_lsn, lsn) >= 0))
63d686d12dSDave Chinner 		return;
64cd4a3c50SDave Chinner 
65d686d12dSDave Chinner 	spin_unlock(&ailp->ail_lock);
66d686d12dSDave Chinner 	ASSERT(in_ail);
67d686d12dSDave Chinner 	ASSERT(prev_lsn == NULLCOMMITLSN || XFS_LSN_CMP(prev_lsn, lsn) <= 0);
68d686d12dSDave Chinner 	ASSERT(next_lsn == NULLCOMMITLSN || XFS_LSN_CMP(next_lsn, lsn) >= 0);
69d686d12dSDave Chinner 	spin_lock(&ailp->ail_lock);
70cd4a3c50SDave Chinner }
71cd4a3c50SDave Chinner #else /* !DEBUG */
72de08dbc1SDavid Chinner #define	xfs_ail_check(a,l)
731da177e4SLinus Torvalds #endif /* DEBUG */
741da177e4SLinus Torvalds 
75cd4a3c50SDave Chinner /*
76fd074841SDave Chinner  * Return a pointer to the last item in the AIL.  If the AIL is empty, then
77fd074841SDave Chinner  * return NULL.
78fd074841SDave Chinner  */
79efe2330fSChristoph Hellwig static struct xfs_log_item *
80fd074841SDave Chinner xfs_ail_max(
81fd074841SDave Chinner 	struct xfs_ail  *ailp)
82fd074841SDave Chinner {
8357e80956SMatthew Wilcox 	if (list_empty(&ailp->ail_head))
84fd074841SDave Chinner 		return NULL;
85fd074841SDave Chinner 
86efe2330fSChristoph Hellwig 	return list_entry(ailp->ail_head.prev, struct xfs_log_item, li_ail);
87fd074841SDave Chinner }
88fd074841SDave Chinner 
89fd074841SDave Chinner /*
90cd4a3c50SDave Chinner  * Return a pointer to the item which follows the given item in the AIL.  If
91cd4a3c50SDave Chinner  * the given item is the last item in the list, then return NULL.
92cd4a3c50SDave Chinner  */
93efe2330fSChristoph Hellwig static struct xfs_log_item *
94cd4a3c50SDave Chinner xfs_ail_next(
95cd4a3c50SDave Chinner 	struct xfs_ail		*ailp,
96efe2330fSChristoph Hellwig 	struct xfs_log_item	*lip)
97cd4a3c50SDave Chinner {
9857e80956SMatthew Wilcox 	if (lip->li_ail.next == &ailp->ail_head)
99cd4a3c50SDave Chinner 		return NULL;
100cd4a3c50SDave Chinner 
101efe2330fSChristoph Hellwig 	return list_first_entry(&lip->li_ail, struct xfs_log_item, li_ail);
102cd4a3c50SDave Chinner }
103cd4a3c50SDave Chinner 
104cd4a3c50SDave Chinner /*
105cd4a3c50SDave Chinner  * This is called by the log manager code to determine the LSN of the tail of
106cd4a3c50SDave Chinner  * the log.  This is exactly the LSN of the first item in the AIL.  If the AIL
107cd4a3c50SDave Chinner  * is empty, then this function returns 0.
1081da177e4SLinus Torvalds  *
109cd4a3c50SDave Chinner  * We need the AIL lock in order to get a coherent read of the lsn of the last
110cd4a3c50SDave Chinner  * item in the AIL.
1111da177e4SLinus Torvalds  */
1121da177e4SLinus Torvalds xfs_lsn_t
113fd074841SDave Chinner xfs_ail_min_lsn(
1145b00f14fSDavid Chinner 	struct xfs_ail		*ailp)
1151da177e4SLinus Torvalds {
116cd4a3c50SDave Chinner 	xfs_lsn_t		lsn = 0;
117efe2330fSChristoph Hellwig 	struct xfs_log_item	*lip;
1181da177e4SLinus Torvalds 
11957e80956SMatthew Wilcox 	spin_lock(&ailp->ail_lock);
1205b00f14fSDavid Chinner 	lip = xfs_ail_min(ailp);
121cd4a3c50SDave Chinner 	if (lip)
1221da177e4SLinus Torvalds 		lsn = lip->li_lsn;
12357e80956SMatthew Wilcox 	spin_unlock(&ailp->ail_lock);
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds 	return lsn;
1261da177e4SLinus Torvalds }
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds /*
129fd074841SDave Chinner  * Return the maximum lsn held in the AIL, or zero if the AIL is empty.
130fd074841SDave Chinner  */
131fd074841SDave Chinner static xfs_lsn_t
132fd074841SDave Chinner xfs_ail_max_lsn(
133fd074841SDave Chinner 	struct xfs_ail		*ailp)
134fd074841SDave Chinner {
135fd074841SDave Chinner 	xfs_lsn_t       	lsn = 0;
136efe2330fSChristoph Hellwig 	struct xfs_log_item	*lip;
137fd074841SDave Chinner 
13857e80956SMatthew Wilcox 	spin_lock(&ailp->ail_lock);
139fd074841SDave Chinner 	lip = xfs_ail_max(ailp);
140fd074841SDave Chinner 	if (lip)
141fd074841SDave Chinner 		lsn = lip->li_lsn;
14257e80956SMatthew Wilcox 	spin_unlock(&ailp->ail_lock);
143fd074841SDave Chinner 
144fd074841SDave Chinner 	return lsn;
145fd074841SDave Chinner }
146fd074841SDave Chinner 
147fd074841SDave Chinner /*
148af3e4022SDave Chinner  * The cursor keeps track of where our current traversal is up to by tracking
149af3e4022SDave Chinner  * the next item in the list for us. However, for this to be safe, removing an
150af3e4022SDave Chinner  * object from the AIL needs to invalidate any cursor that points to it. hence
151af3e4022SDave Chinner  * the traversal cursor needs to be linked to the struct xfs_ail so that
152af3e4022SDave Chinner  * deletion can search all the active cursors for invalidation.
15327d8d5feSDavid Chinner  */
1545b00f14fSDavid Chinner STATIC void
15527d8d5feSDavid Chinner xfs_trans_ail_cursor_init(
15627d8d5feSDavid Chinner 	struct xfs_ail		*ailp,
15727d8d5feSDavid Chinner 	struct xfs_ail_cursor	*cur)
15827d8d5feSDavid Chinner {
15927d8d5feSDavid Chinner 	cur->item = NULL;
16057e80956SMatthew Wilcox 	list_add_tail(&cur->list, &ailp->ail_cursors);
16127d8d5feSDavid Chinner }
16227d8d5feSDavid Chinner 
16327d8d5feSDavid Chinner /*
164af3e4022SDave Chinner  * Get the next item in the traversal and advance the cursor.  If the cursor
165af3e4022SDave Chinner  * was invalidated (indicated by a lip of 1), restart the traversal.
16627d8d5feSDavid Chinner  */
1675b00f14fSDavid Chinner struct xfs_log_item *
16827d8d5feSDavid Chinner xfs_trans_ail_cursor_next(
16927d8d5feSDavid Chinner 	struct xfs_ail		*ailp,
17027d8d5feSDavid Chinner 	struct xfs_ail_cursor	*cur)
17127d8d5feSDavid Chinner {
17227d8d5feSDavid Chinner 	struct xfs_log_item	*lip = cur->item;
17327d8d5feSDavid Chinner 
174db9d67d6SChristoph Hellwig 	if ((uintptr_t)lip & 1)
17527d8d5feSDavid Chinner 		lip = xfs_ail_min(ailp);
17616b59029SDave Chinner 	if (lip)
17716b59029SDave Chinner 		cur->item = xfs_ail_next(ailp, lip);
17827d8d5feSDavid Chinner 	return lip;
17927d8d5feSDavid Chinner }
18027d8d5feSDavid Chinner 
18127d8d5feSDavid Chinner /*
182af3e4022SDave Chinner  * When the traversal is complete, we need to remove the cursor from the list
183af3e4022SDave Chinner  * of traversing cursors.
18427d8d5feSDavid Chinner  */
18527d8d5feSDavid Chinner void
18627d8d5feSDavid Chinner xfs_trans_ail_cursor_done(
187af3e4022SDave Chinner 	struct xfs_ail_cursor	*cur)
18827d8d5feSDavid Chinner {
189af3e4022SDave Chinner 	cur->item = NULL;
190af3e4022SDave Chinner 	list_del_init(&cur->list);
19127d8d5feSDavid Chinner }
19227d8d5feSDavid Chinner 
19327d8d5feSDavid Chinner /*
194af3e4022SDave Chinner  * Invalidate any cursor that is pointing to this item. This is called when an
195af3e4022SDave Chinner  * item is removed from the AIL. Any cursor pointing to this object is now
196af3e4022SDave Chinner  * invalid and the traversal needs to be terminated so it doesn't reference a
197af3e4022SDave Chinner  * freed object. We set the low bit of the cursor item pointer so we can
198af3e4022SDave Chinner  * distinguish between an invalidation and the end of the list when getting the
199af3e4022SDave Chinner  * next item from the cursor.
2005b00f14fSDavid Chinner  */
2015b00f14fSDavid Chinner STATIC void
2025b00f14fSDavid Chinner xfs_trans_ail_cursor_clear(
2035b00f14fSDavid Chinner 	struct xfs_ail		*ailp,
2045b00f14fSDavid Chinner 	struct xfs_log_item	*lip)
2055b00f14fSDavid Chinner {
2065b00f14fSDavid Chinner 	struct xfs_ail_cursor	*cur;
2075b00f14fSDavid Chinner 
20857e80956SMatthew Wilcox 	list_for_each_entry(cur, &ailp->ail_cursors, list) {
2095b00f14fSDavid Chinner 		if (cur->item == lip)
2105b00f14fSDavid Chinner 			cur->item = (struct xfs_log_item *)
211db9d67d6SChristoph Hellwig 					((uintptr_t)cur->item | 1);
2125b00f14fSDavid Chinner 	}
2135b00f14fSDavid Chinner }
2145b00f14fSDavid Chinner 
2155b00f14fSDavid Chinner /*
21616b59029SDave Chinner  * Find the first item in the AIL with the given @lsn by searching in ascending
21716b59029SDave Chinner  * LSN order and initialise the cursor to point to the next item for a
21816b59029SDave Chinner  * ascending traversal.  Pass a @lsn of zero to initialise the cursor to the
21916b59029SDave Chinner  * first item in the AIL. Returns NULL if the list is empty.
220249a8c11SDavid Chinner  */
221efe2330fSChristoph Hellwig struct xfs_log_item *
2225b00f14fSDavid Chinner xfs_trans_ail_cursor_first(
22327d8d5feSDavid Chinner 	struct xfs_ail		*ailp,
22427d8d5feSDavid Chinner 	struct xfs_ail_cursor	*cur,
225249a8c11SDavid Chinner 	xfs_lsn_t		lsn)
226249a8c11SDavid Chinner {
227efe2330fSChristoph Hellwig 	struct xfs_log_item	*lip;
228249a8c11SDavid Chinner 
2295b00f14fSDavid Chinner 	xfs_trans_ail_cursor_init(ailp, cur);
23016b59029SDave Chinner 
23116b59029SDave Chinner 	if (lsn == 0) {
23227d8d5feSDavid Chinner 		lip = xfs_ail_min(ailp);
2335b00f14fSDavid Chinner 		goto out;
23416b59029SDave Chinner 	}
235249a8c11SDavid Chinner 
23657e80956SMatthew Wilcox 	list_for_each_entry(lip, &ailp->ail_head, li_ail) {
2375b00f14fSDavid Chinner 		if (XFS_LSN_CMP(lip->li_lsn, lsn) >= 0)
2387ee49acfSDavid Chinner 			goto out;
2395b00f14fSDavid Chinner 	}
24016b59029SDave Chinner 	return NULL;
24116b59029SDave Chinner 
2425b00f14fSDavid Chinner out:
24316b59029SDave Chinner 	if (lip)
24416b59029SDave Chinner 		cur->item = xfs_ail_next(ailp, lip);
245249a8c11SDavid Chinner 	return lip;
246249a8c11SDavid Chinner }
247535f6b37SJosef 'Jeff' Sipek 
2481d8c95a3SDave Chinner static struct xfs_log_item *
2491d8c95a3SDave Chinner __xfs_trans_ail_cursor_last(
2501d8c95a3SDave Chinner 	struct xfs_ail		*ailp,
2511d8c95a3SDave Chinner 	xfs_lsn_t		lsn)
2521d8c95a3SDave Chinner {
253efe2330fSChristoph Hellwig 	struct xfs_log_item	*lip;
2541d8c95a3SDave Chinner 
25557e80956SMatthew Wilcox 	list_for_each_entry_reverse(lip, &ailp->ail_head, li_ail) {
2561d8c95a3SDave Chinner 		if (XFS_LSN_CMP(lip->li_lsn, lsn) <= 0)
2571d8c95a3SDave Chinner 			return lip;
2581d8c95a3SDave Chinner 	}
2591d8c95a3SDave Chinner 	return NULL;
2601d8c95a3SDave Chinner }
2611d8c95a3SDave Chinner 
2621d8c95a3SDave Chinner /*
26316b59029SDave Chinner  * Find the last item in the AIL with the given @lsn by searching in descending
26416b59029SDave Chinner  * LSN order and initialise the cursor to point to that item.  If there is no
26516b59029SDave Chinner  * item with the value of @lsn, then it sets the cursor to the last item with an
26616b59029SDave Chinner  * LSN lower than @lsn.  Returns NULL if the list is empty.
2671d8c95a3SDave Chinner  */
2681d8c95a3SDave Chinner struct xfs_log_item *
2691d8c95a3SDave Chinner xfs_trans_ail_cursor_last(
2701d8c95a3SDave Chinner 	struct xfs_ail		*ailp,
2711d8c95a3SDave Chinner 	struct xfs_ail_cursor	*cur,
2721d8c95a3SDave Chinner 	xfs_lsn_t		lsn)
2731d8c95a3SDave Chinner {
2741d8c95a3SDave Chinner 	xfs_trans_ail_cursor_init(ailp, cur);
2751d8c95a3SDave Chinner 	cur->item = __xfs_trans_ail_cursor_last(ailp, lsn);
2761d8c95a3SDave Chinner 	return cur->item;
2771d8c95a3SDave Chinner }
2781d8c95a3SDave Chinner 
2791d8c95a3SDave Chinner /*
28016b59029SDave Chinner  * Splice the log item list into the AIL at the given LSN. We splice to the
2811d8c95a3SDave Chinner  * tail of the given LSN to maintain insert order for push traversals. The
2821d8c95a3SDave Chinner  * cursor is optional, allowing repeated updates to the same LSN to avoid
283e44f4112SAlex Elder  * repeated traversals.  This should not be called with an empty list.
284cd4a3c50SDave Chinner  */
285cd4a3c50SDave Chinner static void
286cd4a3c50SDave Chinner xfs_ail_splice(
287cd4a3c50SDave Chinner 	struct xfs_ail		*ailp,
2881d8c95a3SDave Chinner 	struct xfs_ail_cursor	*cur,
289cd4a3c50SDave Chinner 	struct list_head	*list,
290cd4a3c50SDave Chinner 	xfs_lsn_t		lsn)
291cd4a3c50SDave Chinner {
292e44f4112SAlex Elder 	struct xfs_log_item	*lip;
293e44f4112SAlex Elder 
294e44f4112SAlex Elder 	ASSERT(!list_empty(list));
295cd4a3c50SDave Chinner 
2961d8c95a3SDave Chinner 	/*
297e44f4112SAlex Elder 	 * Use the cursor to determine the insertion point if one is
298e44f4112SAlex Elder 	 * provided.  If not, or if the one we got is not valid,
299e44f4112SAlex Elder 	 * find the place in the AIL where the items belong.
3001d8c95a3SDave Chinner 	 */
301e44f4112SAlex Elder 	lip = cur ? cur->item : NULL;
302db9d67d6SChristoph Hellwig 	if (!lip || (uintptr_t)lip & 1)
3031d8c95a3SDave Chinner 		lip = __xfs_trans_ail_cursor_last(ailp, lsn);
3041d8c95a3SDave Chinner 
305e44f4112SAlex Elder 	/*
306e44f4112SAlex Elder 	 * If a cursor is provided, we know we're processing the AIL
307e44f4112SAlex Elder 	 * in lsn order, and future items to be spliced in will
308e44f4112SAlex Elder 	 * follow the last one being inserted now.  Update the
309e44f4112SAlex Elder 	 * cursor to point to that last item, now while we have a
310e44f4112SAlex Elder 	 * reliable pointer to it.
311e44f4112SAlex Elder 	 */
3121d8c95a3SDave Chinner 	if (cur)
313e44f4112SAlex Elder 		cur->item = list_entry(list->prev, struct xfs_log_item, li_ail);
314cd4a3c50SDave Chinner 
3151d8c95a3SDave Chinner 	/*
316e44f4112SAlex Elder 	 * Finally perform the splice.  Unless the AIL was empty,
317e44f4112SAlex Elder 	 * lip points to the item in the AIL _after_ which the new
318e44f4112SAlex Elder 	 * items should go.  If lip is null the AIL was empty, so
319e44f4112SAlex Elder 	 * the new items go at the head of the AIL.
3201d8c95a3SDave Chinner 	 */
321e44f4112SAlex Elder 	if (lip)
3221d8c95a3SDave Chinner 		list_splice(list, &lip->li_ail);
323e44f4112SAlex Elder 	else
32457e80956SMatthew Wilcox 		list_splice(list, &ailp->ail_head);
325cd4a3c50SDave Chinner }
326cd4a3c50SDave Chinner 
327cd4a3c50SDave Chinner /*
328cd4a3c50SDave Chinner  * Delete the given item from the AIL.  Return a pointer to the item.
329cd4a3c50SDave Chinner  */
330cd4a3c50SDave Chinner static void
331cd4a3c50SDave Chinner xfs_ail_delete(
332cd4a3c50SDave Chinner 	struct xfs_ail		*ailp,
333efe2330fSChristoph Hellwig 	struct xfs_log_item	*lip)
334cd4a3c50SDave Chinner {
335cd4a3c50SDave Chinner 	xfs_ail_check(ailp, lip);
336cd4a3c50SDave Chinner 	list_del(&lip->li_ail);
337cd4a3c50SDave Chinner 	xfs_trans_ail_cursor_clear(ailp, lip);
338cd4a3c50SDave Chinner }
339cd4a3c50SDave Chinner 
3407f4d01f3SBrian Foster static inline uint
3417f4d01f3SBrian Foster xfsaild_push_item(
3427f4d01f3SBrian Foster 	struct xfs_ail		*ailp,
3437f4d01f3SBrian Foster 	struct xfs_log_item	*lip)
3447f4d01f3SBrian Foster {
3457f4d01f3SBrian Foster 	/*
3467f4d01f3SBrian Foster 	 * If log item pinning is enabled, skip the push and track the item as
3477f4d01f3SBrian Foster 	 * pinned. This can help induce head-behind-tail conditions.
3487f4d01f3SBrian Foster 	 */
34957e80956SMatthew Wilcox 	if (XFS_TEST_ERROR(false, ailp->ail_mount, XFS_ERRTAG_LOG_ITEM_PIN))
3507f4d01f3SBrian Foster 		return XFS_ITEM_PINNED;
3517f4d01f3SBrian Foster 
352e8b78db7SChristoph Hellwig 	/*
353e8b78db7SChristoph Hellwig 	 * Consider the item pinned if a push callback is not defined so the
354e8b78db7SChristoph Hellwig 	 * caller will force the log. This should only happen for intent items
355e8b78db7SChristoph Hellwig 	 * as they are unpinned once the associated done item is committed to
356e8b78db7SChristoph Hellwig 	 * the on-disk log.
357e8b78db7SChristoph Hellwig 	 */
358e8b78db7SChristoph Hellwig 	if (!lip->li_ops->iop_push)
359e8b78db7SChristoph Hellwig 		return XFS_ITEM_PINNED;
36057e80956SMatthew Wilcox 	return lip->li_ops->iop_push(lip, &ailp->ail_buf_list);
3617f4d01f3SBrian Foster }
3627f4d01f3SBrian Foster 
3630030807cSChristoph Hellwig static long
3640030807cSChristoph Hellwig xfsaild_push(
3650030807cSChristoph Hellwig 	struct xfs_ail		*ailp)
366249a8c11SDavid Chinner {
36757e80956SMatthew Wilcox 	xfs_mount_t		*mp = ailp->ail_mount;
368af3e4022SDave Chinner 	struct xfs_ail_cursor	cur;
369efe2330fSChristoph Hellwig 	struct xfs_log_item	*lip;
3709e7004e7SDave Chinner 	xfs_lsn_t		lsn;
371fe0da767SDave Chinner 	xfs_lsn_t		target;
37243ff2122SChristoph Hellwig 	long			tout;
3739e7004e7SDave Chinner 	int			stuck = 0;
37443ff2122SChristoph Hellwig 	int			flushing = 0;
3759e7004e7SDave Chinner 	int			count = 0;
3761da177e4SLinus Torvalds 
377670ce93fSDave Chinner 	/*
37843ff2122SChristoph Hellwig 	 * If we encountered pinned items or did not finish writing out all
37943ff2122SChristoph Hellwig 	 * buffers the last time we ran, force the log first and wait for it
38043ff2122SChristoph Hellwig 	 * before pushing again.
381670ce93fSDave Chinner 	 */
38257e80956SMatthew Wilcox 	if (ailp->ail_log_flush && ailp->ail_last_pushed_lsn == 0 &&
38357e80956SMatthew Wilcox 	    (!list_empty_careful(&ailp->ail_buf_list) ||
38443ff2122SChristoph Hellwig 	     xfs_ail_min_lsn(ailp))) {
38557e80956SMatthew Wilcox 		ailp->ail_log_flush = 0;
38643ff2122SChristoph Hellwig 
387ff6d6af2SBill O'Donnell 		XFS_STATS_INC(mp, xs_push_ail_flush);
388670ce93fSDave Chinner 		xfs_log_force(mp, XFS_LOG_SYNC);
389670ce93fSDave Chinner 	}
390670ce93fSDave Chinner 
39157e80956SMatthew Wilcox 	spin_lock(&ailp->ail_lock);
3928375f922SBrian Foster 
39357e80956SMatthew Wilcox 	/* barrier matches the ail_target update in xfs_ail_push() */
3948375f922SBrian Foster 	smp_rmb();
39557e80956SMatthew Wilcox 	target = ailp->ail_target;
39657e80956SMatthew Wilcox 	ailp->ail_target_prev = target;
3978375f922SBrian Foster 
39857e80956SMatthew Wilcox 	lip = xfs_trans_ail_cursor_first(ailp, &cur, ailp->ail_last_pushed_lsn);
399211e4d43SChristoph Hellwig 	if (!lip) {
4001da177e4SLinus Torvalds 		/*
40143ff2122SChristoph Hellwig 		 * If the AIL is empty or our push has reached the end we are
40243ff2122SChristoph Hellwig 		 * done now.
4031da177e4SLinus Torvalds 		 */
404e4a1e29cSEric Sandeen 		xfs_trans_ail_cursor_done(&cur);
40557e80956SMatthew Wilcox 		spin_unlock(&ailp->ail_lock);
4069e7004e7SDave Chinner 		goto out_done;
4071da177e4SLinus Torvalds 	}
4081da177e4SLinus Torvalds 
409ff6d6af2SBill O'Donnell 	XFS_STATS_INC(mp, xs_push_ail);
4101da177e4SLinus Torvalds 
411249a8c11SDavid Chinner 	lsn = lip->li_lsn;
41250e86686SDave Chinner 	while ((XFS_LSN_CMP(lip->li_lsn, target) <= 0)) {
413249a8c11SDavid Chinner 		int	lock_result;
41443ff2122SChristoph Hellwig 
415249a8c11SDavid Chinner 		/*
416904c17e6SDave Chinner 		 * Note that iop_push may unlock and reacquire the AIL lock.  We
41743ff2122SChristoph Hellwig 		 * rely on the AIL cursor implementation to be able to deal with
41843ff2122SChristoph Hellwig 		 * the dropped lock.
4191da177e4SLinus Torvalds 		 */
4207f4d01f3SBrian Foster 		lock_result = xfsaild_push_item(ailp, lip);
4211da177e4SLinus Torvalds 		switch (lock_result) {
4221da177e4SLinus Torvalds 		case XFS_ITEM_SUCCESS:
423ff6d6af2SBill O'Donnell 			XFS_STATS_INC(mp, xs_push_ail_success);
4249e4c109aSChristoph Hellwig 			trace_xfs_ail_push(lip);
4259e4c109aSChristoph Hellwig 
42657e80956SMatthew Wilcox 			ailp->ail_last_pushed_lsn = lsn;
4271da177e4SLinus Torvalds 			break;
4281da177e4SLinus Torvalds 
42943ff2122SChristoph Hellwig 		case XFS_ITEM_FLUSHING:
43043ff2122SChristoph Hellwig 			/*
431cf085a1bSJoe Perches 			 * The item or its backing buffer is already being
43243ff2122SChristoph Hellwig 			 * flushed.  The typical reason for that is that an
43343ff2122SChristoph Hellwig 			 * inode buffer is locked because we already pushed the
43443ff2122SChristoph Hellwig 			 * updates to it as part of inode clustering.
43543ff2122SChristoph Hellwig 			 *
43643ff2122SChristoph Hellwig 			 * We do not want to to stop flushing just because lots
437cf085a1bSJoe Perches 			 * of items are already being flushed, but we need to
43843ff2122SChristoph Hellwig 			 * re-try the flushing relatively soon if most of the
439cf085a1bSJoe Perches 			 * AIL is being flushed.
44043ff2122SChristoph Hellwig 			 */
441ff6d6af2SBill O'Donnell 			XFS_STATS_INC(mp, xs_push_ail_flushing);
44243ff2122SChristoph Hellwig 			trace_xfs_ail_flushing(lip);
44317b38471SChristoph Hellwig 
44443ff2122SChristoph Hellwig 			flushing++;
44557e80956SMatthew Wilcox 			ailp->ail_last_pushed_lsn = lsn;
4461da177e4SLinus Torvalds 			break;
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds 		case XFS_ITEM_PINNED:
449ff6d6af2SBill O'Donnell 			XFS_STATS_INC(mp, xs_push_ail_pinned);
4509e4c109aSChristoph Hellwig 			trace_xfs_ail_pinned(lip);
4519e4c109aSChristoph Hellwig 
452249a8c11SDavid Chinner 			stuck++;
45357e80956SMatthew Wilcox 			ailp->ail_log_flush++;
4541da177e4SLinus Torvalds 			break;
4551da177e4SLinus Torvalds 		case XFS_ITEM_LOCKED:
456ff6d6af2SBill O'Donnell 			XFS_STATS_INC(mp, xs_push_ail_locked);
4579e4c109aSChristoph Hellwig 			trace_xfs_ail_locked(lip);
45843ff2122SChristoph Hellwig 
459249a8c11SDavid Chinner 			stuck++;
4601da177e4SLinus Torvalds 			break;
4611da177e4SLinus Torvalds 		default:
4621da177e4SLinus Torvalds 			ASSERT(0);
4631da177e4SLinus Torvalds 			break;
4641da177e4SLinus Torvalds 		}
4651da177e4SLinus Torvalds 
466249a8c11SDavid Chinner 		count++;
467249a8c11SDavid Chinner 
468249a8c11SDavid Chinner 		/*
469249a8c11SDavid Chinner 		 * Are there too many items we can't do anything with?
47043ff2122SChristoph Hellwig 		 *
471249a8c11SDavid Chinner 		 * If we we are skipping too many items because we can't flush
472249a8c11SDavid Chinner 		 * them or they are already being flushed, we back off and
473249a8c11SDavid Chinner 		 * given them time to complete whatever operation is being
474249a8c11SDavid Chinner 		 * done. i.e. remove pressure from the AIL while we can't make
475249a8c11SDavid Chinner 		 * progress so traversals don't slow down further inserts and
476249a8c11SDavid Chinner 		 * removals to/from the AIL.
477249a8c11SDavid Chinner 		 *
478249a8c11SDavid Chinner 		 * The value of 100 is an arbitrary magic number based on
479249a8c11SDavid Chinner 		 * observation.
480249a8c11SDavid Chinner 		 */
481249a8c11SDavid Chinner 		if (stuck > 100)
482249a8c11SDavid Chinner 			break;
483249a8c11SDavid Chinner 
484af3e4022SDave Chinner 		lip = xfs_trans_ail_cursor_next(ailp, &cur);
485249a8c11SDavid Chinner 		if (lip == NULL)
486249a8c11SDavid Chinner 			break;
487249a8c11SDavid Chinner 		lsn = lip->li_lsn;
4881da177e4SLinus Torvalds 	}
489e4a1e29cSEric Sandeen 	xfs_trans_ail_cursor_done(&cur);
49057e80956SMatthew Wilcox 	spin_unlock(&ailp->ail_lock);
4911da177e4SLinus Torvalds 
49257e80956SMatthew Wilcox 	if (xfs_buf_delwri_submit_nowait(&ailp->ail_buf_list))
49357e80956SMatthew Wilcox 		ailp->ail_log_flush++;
494d808f617SDave Chinner 
49543ff2122SChristoph Hellwig 	if (!count || XFS_LSN_CMP(lsn, target) >= 0) {
4969e7004e7SDave Chinner out_done:
497249a8c11SDavid Chinner 		/*
49843ff2122SChristoph Hellwig 		 * We reached the target or the AIL is empty, so wait a bit
49943ff2122SChristoph Hellwig 		 * longer for I/O to complete and remove pushed items from the
50043ff2122SChristoph Hellwig 		 * AIL before we start the next scan from the start of the AIL.
501249a8c11SDavid Chinner 		 */
502453eac8aSDave Chinner 		tout = 50;
50357e80956SMatthew Wilcox 		ailp->ail_last_pushed_lsn = 0;
50443ff2122SChristoph Hellwig 	} else if (((stuck + flushing) * 100) / count > 90) {
505249a8c11SDavid Chinner 		/*
50643ff2122SChristoph Hellwig 		 * Either there is a lot of contention on the AIL or we are
50743ff2122SChristoph Hellwig 		 * stuck due to operations in progress. "Stuck" in this case
50843ff2122SChristoph Hellwig 		 * is defined as >90% of the items we tried to push were stuck.
509249a8c11SDavid Chinner 		 *
510249a8c11SDavid Chinner 		 * Backoff a bit more to allow some I/O to complete before
51143ff2122SChristoph Hellwig 		 * restarting from the start of the AIL. This prevents us from
51243ff2122SChristoph Hellwig 		 * spinning on the same items, and if they are pinned will all
51343ff2122SChristoph Hellwig 		 * the restart to issue a log force to unpin the stuck items.
514249a8c11SDavid Chinner 		 */
515453eac8aSDave Chinner 		tout = 20;
51657e80956SMatthew Wilcox 		ailp->ail_last_pushed_lsn = 0;
51743ff2122SChristoph Hellwig 	} else {
51843ff2122SChristoph Hellwig 		/*
51943ff2122SChristoph Hellwig 		 * Assume we have more work to do in a short while.
52043ff2122SChristoph Hellwig 		 */
52143ff2122SChristoph Hellwig 		tout = 10;
522453eac8aSDave Chinner 	}
5231da177e4SLinus Torvalds 
5240030807cSChristoph Hellwig 	return tout;
5250030807cSChristoph Hellwig }
5260030807cSChristoph Hellwig 
5270030807cSChristoph Hellwig static int
5280030807cSChristoph Hellwig xfsaild(
5290030807cSChristoph Hellwig 	void		*data)
5300030807cSChristoph Hellwig {
5310030807cSChristoph Hellwig 	struct xfs_ail	*ailp = data;
5320030807cSChristoph Hellwig 	long		tout = 0;	/* milliseconds */
53310a98cb1SEric Biggers 	unsigned int	noreclaim_flag;
5340030807cSChristoph Hellwig 
53510a98cb1SEric Biggers 	noreclaim_flag = memalloc_noreclaim_save();
53618f1df4eSMichal Hocko 	set_freezable();
53743ff2122SChristoph Hellwig 
5380bd89676SHou Tao 	while (1) {
5390030807cSChristoph Hellwig 		if (tout && tout <= 20)
5400bd89676SHou Tao 			set_current_state(TASK_KILLABLE);
5410030807cSChristoph Hellwig 		else
5420bd89676SHou Tao 			set_current_state(TASK_INTERRUPTIBLE);
5430bd89676SHou Tao 
5440bd89676SHou Tao 		/*
545efc3289cSBrian Foster 		 * Check kthread_should_stop() after we set the task state to
546efc3289cSBrian Foster 		 * guarantee that we either see the stop bit and exit or the
547efc3289cSBrian Foster 		 * task state is reset to runnable such that it's not scheduled
548efc3289cSBrian Foster 		 * out indefinitely and detects the stop bit at next iteration.
5490bd89676SHou Tao 		 * A memory barrier is included in above task state set to
5500bd89676SHou Tao 		 * serialize again kthread_stop().
5510bd89676SHou Tao 		 */
5520bd89676SHou Tao 		if (kthread_should_stop()) {
5530bd89676SHou Tao 			__set_current_state(TASK_RUNNING);
554efc3289cSBrian Foster 
555efc3289cSBrian Foster 			/*
556efc3289cSBrian Foster 			 * The caller forces out the AIL before stopping the
557efc3289cSBrian Foster 			 * thread in the common case, which means the delwri
558efc3289cSBrian Foster 			 * queue is drained. In the shutdown case, the queue may
559efc3289cSBrian Foster 			 * still hold relogged buffers that haven't been
560efc3289cSBrian Foster 			 * submitted because they were pinned since added to the
561efc3289cSBrian Foster 			 * queue.
562efc3289cSBrian Foster 			 *
563efc3289cSBrian Foster 			 * Log I/O error processing stales the underlying buffer
564efc3289cSBrian Foster 			 * and clears the delwri state, expecting the buf to be
565efc3289cSBrian Foster 			 * removed on the next submission attempt. That won't
566efc3289cSBrian Foster 			 * happen if we're shutting down, so this is the last
567efc3289cSBrian Foster 			 * opportunity to release such buffers from the queue.
568efc3289cSBrian Foster 			 */
569efc3289cSBrian Foster 			ASSERT(list_empty(&ailp->ail_buf_list) ||
570efc3289cSBrian Foster 			       XFS_FORCED_SHUTDOWN(ailp->ail_mount));
571efc3289cSBrian Foster 			xfs_buf_delwri_cancel(&ailp->ail_buf_list);
5720bd89676SHou Tao 			break;
5730bd89676SHou Tao 		}
5748375f922SBrian Foster 
57557e80956SMatthew Wilcox 		spin_lock(&ailp->ail_lock);
5768375f922SBrian Foster 
5778375f922SBrian Foster 		/*
5788375f922SBrian Foster 		 * Idle if the AIL is empty and we are not racing with a target
5798375f922SBrian Foster 		 * update. We check the AIL after we set the task to a sleep
58057e80956SMatthew Wilcox 		 * state to guarantee that we either catch an ail_target update
5818375f922SBrian Foster 		 * or that a wake_up resets the state to TASK_RUNNING.
5828375f922SBrian Foster 		 * Otherwise, we run the risk of sleeping indefinitely.
5838375f922SBrian Foster 		 *
58457e80956SMatthew Wilcox 		 * The barrier matches the ail_target update in xfs_ail_push().
5858375f922SBrian Foster 		 */
5868375f922SBrian Foster 		smp_rmb();
5878375f922SBrian Foster 		if (!xfs_ail_min(ailp) &&
58857e80956SMatthew Wilcox 		    ailp->ail_target == ailp->ail_target_prev) {
58957e80956SMatthew Wilcox 			spin_unlock(&ailp->ail_lock);
59018f1df4eSMichal Hocko 			freezable_schedule();
5918375f922SBrian Foster 			tout = 0;
5928375f922SBrian Foster 			continue;
5938375f922SBrian Foster 		}
59457e80956SMatthew Wilcox 		spin_unlock(&ailp->ail_lock);
5958375f922SBrian Foster 
5968375f922SBrian Foster 		if (tout)
59718f1df4eSMichal Hocko 			freezable_schedule_timeout(msecs_to_jiffies(tout));
5988375f922SBrian Foster 
5998375f922SBrian Foster 		__set_current_state(TASK_RUNNING);
6000030807cSChristoph Hellwig 
6010030807cSChristoph Hellwig 		try_to_freeze();
6020030807cSChristoph Hellwig 
6030030807cSChristoph Hellwig 		tout = xfsaild_push(ailp);
6040030807cSChristoph Hellwig 	}
6050030807cSChristoph Hellwig 
60610a98cb1SEric Biggers 	memalloc_noreclaim_restore(noreclaim_flag);
6070030807cSChristoph Hellwig 	return 0;
6080bf6a5bdSDave Chinner }
6090bf6a5bdSDave Chinner 
6100bf6a5bdSDave Chinner /*
6110bf6a5bdSDave Chinner  * This routine is called to move the tail of the AIL forward.  It does this by
6120bf6a5bdSDave Chinner  * trying to flush items in the AIL whose lsns are below the given
6130bf6a5bdSDave Chinner  * threshold_lsn.
6140bf6a5bdSDave Chinner  *
6150bf6a5bdSDave Chinner  * The push is run asynchronously in a workqueue, which means the caller needs
6160bf6a5bdSDave Chinner  * to handle waiting on the async flush for space to become available.
6170bf6a5bdSDave Chinner  * We don't want to interrupt any push that is in progress, hence we only queue
618cf085a1bSJoe Perches  * work if we set the pushing bit appropriately.
6190bf6a5bdSDave Chinner  *
6200bf6a5bdSDave Chinner  * We do this unlocked - we only need to know whether there is anything in the
6210bf6a5bdSDave Chinner  * AIL at the time we are called. We don't need to access the contents of
6220bf6a5bdSDave Chinner  * any of the objects, so the lock is not needed.
6230bf6a5bdSDave Chinner  */
6240bf6a5bdSDave Chinner void
625fd074841SDave Chinner xfs_ail_push(
6260bf6a5bdSDave Chinner 	struct xfs_ail		*ailp,
6270bf6a5bdSDave Chinner 	xfs_lsn_t		threshold_lsn)
6280bf6a5bdSDave Chinner {
629efe2330fSChristoph Hellwig 	struct xfs_log_item	*lip;
6300bf6a5bdSDave Chinner 
6310bf6a5bdSDave Chinner 	lip = xfs_ail_min(ailp);
63257e80956SMatthew Wilcox 	if (!lip || XFS_FORCED_SHUTDOWN(ailp->ail_mount) ||
63357e80956SMatthew Wilcox 	    XFS_LSN_CMP(threshold_lsn, ailp->ail_target) <= 0)
6340bf6a5bdSDave Chinner 		return;
6350bf6a5bdSDave Chinner 
6360bf6a5bdSDave Chinner 	/*
6370bf6a5bdSDave Chinner 	 * Ensure that the new target is noticed in push code before it clears
6380bf6a5bdSDave Chinner 	 * the XFS_AIL_PUSHING_BIT.
6390bf6a5bdSDave Chinner 	 */
6400bf6a5bdSDave Chinner 	smp_wmb();
64157e80956SMatthew Wilcox 	xfs_trans_ail_copy_lsn(ailp, &ailp->ail_target, &threshold_lsn);
6420030807cSChristoph Hellwig 	smp_wmb();
6430030807cSChristoph Hellwig 
64457e80956SMatthew Wilcox 	wake_up_process(ailp->ail_task);
6450bf6a5bdSDave Chinner }
6461da177e4SLinus Torvalds 
6471da177e4SLinus Torvalds /*
648fd074841SDave Chinner  * Push out all items in the AIL immediately
649fd074841SDave Chinner  */
650fd074841SDave Chinner void
651fd074841SDave Chinner xfs_ail_push_all(
652fd074841SDave Chinner 	struct xfs_ail  *ailp)
653fd074841SDave Chinner {
654fd074841SDave Chinner 	xfs_lsn_t       threshold_lsn = xfs_ail_max_lsn(ailp);
655fd074841SDave Chinner 
656fd074841SDave Chinner 	if (threshold_lsn)
657fd074841SDave Chinner 		xfs_ail_push(ailp, threshold_lsn);
658fd074841SDave Chinner }
659fd074841SDave Chinner 
660fd074841SDave Chinner /*
661211e4d43SChristoph Hellwig  * Push out all items in the AIL immediately and wait until the AIL is empty.
662211e4d43SChristoph Hellwig  */
663211e4d43SChristoph Hellwig void
664211e4d43SChristoph Hellwig xfs_ail_push_all_sync(
665211e4d43SChristoph Hellwig 	struct xfs_ail  *ailp)
666211e4d43SChristoph Hellwig {
667211e4d43SChristoph Hellwig 	struct xfs_log_item	*lip;
668211e4d43SChristoph Hellwig 	DEFINE_WAIT(wait);
669211e4d43SChristoph Hellwig 
67057e80956SMatthew Wilcox 	spin_lock(&ailp->ail_lock);
671211e4d43SChristoph Hellwig 	while ((lip = xfs_ail_max(ailp)) != NULL) {
67257e80956SMatthew Wilcox 		prepare_to_wait(&ailp->ail_empty, &wait, TASK_UNINTERRUPTIBLE);
67357e80956SMatthew Wilcox 		ailp->ail_target = lip->li_lsn;
67457e80956SMatthew Wilcox 		wake_up_process(ailp->ail_task);
67557e80956SMatthew Wilcox 		spin_unlock(&ailp->ail_lock);
676211e4d43SChristoph Hellwig 		schedule();
67757e80956SMatthew Wilcox 		spin_lock(&ailp->ail_lock);
678211e4d43SChristoph Hellwig 	}
67957e80956SMatthew Wilcox 	spin_unlock(&ailp->ail_lock);
680211e4d43SChristoph Hellwig 
68157e80956SMatthew Wilcox 	finish_wait(&ailp->ail_empty, &wait);
682211e4d43SChristoph Hellwig }
683211e4d43SChristoph Hellwig 
684211e4d43SChristoph Hellwig /*
6850e57f6a3SDave Chinner  * xfs_trans_ail_update - bulk AIL insertion operation.
6860e57f6a3SDave Chinner  *
6870e57f6a3SDave Chinner  * @xfs_trans_ail_update takes an array of log items that all need to be
6880e57f6a3SDave Chinner  * positioned at the same LSN in the AIL. If an item is not in the AIL, it will
6890e57f6a3SDave Chinner  * be added.  Otherwise, it will be repositioned  by removing it and re-adding
6900e57f6a3SDave Chinner  * it to the AIL. If we move the first item in the AIL, update the log tail to
6910e57f6a3SDave Chinner  * match the new minimum LSN in the AIL.
6920e57f6a3SDave Chinner  *
6930e57f6a3SDave Chinner  * This function takes the AIL lock once to execute the update operations on
6940e57f6a3SDave Chinner  * all the items in the array, and as such should not be called with the AIL
6950e57f6a3SDave Chinner  * lock held. As a result, once we have the AIL lock, we need to check each log
6960e57f6a3SDave Chinner  * item LSN to confirm it needs to be moved forward in the AIL.
6970e57f6a3SDave Chinner  *
6980e57f6a3SDave Chinner  * To optimise the insert operation, we delete all the items from the AIL in
6990e57f6a3SDave Chinner  * the first pass, moving them into a temporary list, then splice the temporary
7000e57f6a3SDave Chinner  * list into the correct position in the AIL. This avoids needing to do an
7010e57f6a3SDave Chinner  * insert operation on every item.
7020e57f6a3SDave Chinner  *
7030e57f6a3SDave Chinner  * This function must be called with the AIL lock held.  The lock is dropped
7040e57f6a3SDave Chinner  * before returning.
7050e57f6a3SDave Chinner  */
7060e57f6a3SDave Chinner void
7070e57f6a3SDave Chinner xfs_trans_ail_update_bulk(
7080e57f6a3SDave Chinner 	struct xfs_ail		*ailp,
7091d8c95a3SDave Chinner 	struct xfs_ail_cursor	*cur,
7100e57f6a3SDave Chinner 	struct xfs_log_item	**log_items,
7110e57f6a3SDave Chinner 	int			nr_items,
71257e80956SMatthew Wilcox 	xfs_lsn_t		lsn) __releases(ailp->ail_lock)
7130e57f6a3SDave Chinner {
714efe2330fSChristoph Hellwig 	struct xfs_log_item	*mlip;
7150e57f6a3SDave Chinner 	int			mlip_changed = 0;
7160e57f6a3SDave Chinner 	int			i;
7170e57f6a3SDave Chinner 	LIST_HEAD(tmp);
7180e57f6a3SDave Chinner 
719e44f4112SAlex Elder 	ASSERT(nr_items > 0);		/* Not required, but true. */
7200e57f6a3SDave Chinner 	mlip = xfs_ail_min(ailp);
7210e57f6a3SDave Chinner 
7220e57f6a3SDave Chinner 	for (i = 0; i < nr_items; i++) {
7230e57f6a3SDave Chinner 		struct xfs_log_item *lip = log_items[i];
72422525c17SDave Chinner 		if (test_and_set_bit(XFS_LI_IN_AIL, &lip->li_flags)) {
7250e57f6a3SDave Chinner 			/* check if we really need to move the item */
7260e57f6a3SDave Chinner 			if (XFS_LSN_CMP(lsn, lip->li_lsn) <= 0)
7270e57f6a3SDave Chinner 				continue;
7280e57f6a3SDave Chinner 
729750b9c90SDave Chinner 			trace_xfs_ail_move(lip, lip->li_lsn, lsn);
7300e57f6a3SDave Chinner 			xfs_ail_delete(ailp, lip);
7310e57f6a3SDave Chinner 			if (mlip == lip)
7320e57f6a3SDave Chinner 				mlip_changed = 1;
7330e57f6a3SDave Chinner 		} else {
734750b9c90SDave Chinner 			trace_xfs_ail_insert(lip, 0, lsn);
7350e57f6a3SDave Chinner 		}
7360e57f6a3SDave Chinner 		lip->li_lsn = lsn;
7370e57f6a3SDave Chinner 		list_add(&lip->li_ail, &tmp);
7380e57f6a3SDave Chinner 	}
7390e57f6a3SDave Chinner 
740e44f4112SAlex Elder 	if (!list_empty(&tmp))
7411d8c95a3SDave Chinner 		xfs_ail_splice(ailp, cur, &tmp, lsn);
7421c304625SChristoph Hellwig 
7431c304625SChristoph Hellwig 	if (mlip_changed) {
74457e80956SMatthew Wilcox 		if (!XFS_FORCED_SHUTDOWN(ailp->ail_mount))
74557e80956SMatthew Wilcox 			xlog_assign_tail_lsn_locked(ailp->ail_mount);
74657e80956SMatthew Wilcox 		spin_unlock(&ailp->ail_lock);
74709a423a3SChristoph Hellwig 
74857e80956SMatthew Wilcox 		xfs_log_space_wake(ailp->ail_mount);
7491c304625SChristoph Hellwig 	} else {
75057e80956SMatthew Wilcox 		spin_unlock(&ailp->ail_lock);
7510e57f6a3SDave Chinner 	}
7520e57f6a3SDave Chinner }
7530e57f6a3SDave Chinner 
75427af1bbfSChristoph Hellwig bool
75527af1bbfSChristoph Hellwig xfs_ail_delete_one(
75627af1bbfSChristoph Hellwig 	struct xfs_ail		*ailp,
75727af1bbfSChristoph Hellwig 	struct xfs_log_item	*lip)
75827af1bbfSChristoph Hellwig {
75927af1bbfSChristoph Hellwig 	struct xfs_log_item	*mlip = xfs_ail_min(ailp);
76027af1bbfSChristoph Hellwig 
76127af1bbfSChristoph Hellwig 	trace_xfs_ail_delete(lip, mlip->li_lsn, lip->li_lsn);
76227af1bbfSChristoph Hellwig 	xfs_ail_delete(ailp, lip);
763d3a304b6SCarlos Maiolino 	xfs_clear_li_failed(lip);
76422525c17SDave Chinner 	clear_bit(XFS_LI_IN_AIL, &lip->li_flags);
76527af1bbfSChristoph Hellwig 	lip->li_lsn = 0;
76627af1bbfSChristoph Hellwig 
76727af1bbfSChristoph Hellwig 	return mlip == lip;
76827af1bbfSChristoph Hellwig }
76927af1bbfSChristoph Hellwig 
77027af1bbfSChristoph Hellwig /**
77127af1bbfSChristoph Hellwig  * Remove a log items from the AIL
77230136832SDave Chinner  *
77330136832SDave Chinner  * @xfs_trans_ail_delete_bulk takes an array of log items that all need to
77430136832SDave Chinner  * removed from the AIL. The caller is already holding the AIL lock, and done
77530136832SDave Chinner  * all the checks necessary to ensure the items passed in via @log_items are
77630136832SDave Chinner  * ready for deletion. This includes checking that the items are in the AIL.
77730136832SDave Chinner  *
77830136832SDave Chinner  * For each log item to be removed, unlink it  from the AIL, clear the IN_AIL
77930136832SDave Chinner  * flag from the item and reset the item's lsn to 0. If we remove the first
78030136832SDave Chinner  * item in the AIL, update the log tail to match the new minimum LSN in the
78130136832SDave Chinner  * AIL.
78230136832SDave Chinner  *
78330136832SDave Chinner  * This function will not drop the AIL lock until all items are removed from
78430136832SDave Chinner  * the AIL to minimise the amount of lock traffic on the AIL. This does not
78530136832SDave Chinner  * greatly increase the AIL hold time, but does significantly reduce the amount
78630136832SDave Chinner  * of traffic on the lock, especially during IO completion.
78730136832SDave Chinner  *
78830136832SDave Chinner  * This function must be called with the AIL lock held.  The lock is dropped
78930136832SDave Chinner  * before returning.
79030136832SDave Chinner  */
79130136832SDave Chinner void
79227af1bbfSChristoph Hellwig xfs_trans_ail_delete(
79330136832SDave Chinner 	struct xfs_ail		*ailp,
79427af1bbfSChristoph Hellwig 	struct xfs_log_item	*lip,
79557e80956SMatthew Wilcox 	int			shutdown_type) __releases(ailp->ail_lock)
79630136832SDave Chinner {
79757e80956SMatthew Wilcox 	struct xfs_mount	*mp = ailp->ail_mount;
79827af1bbfSChristoph Hellwig 	bool			mlip_changed;
79930136832SDave Chinner 
80022525c17SDave Chinner 	if (!test_bit(XFS_LI_IN_AIL, &lip->li_flags)) {
80157e80956SMatthew Wilcox 		spin_unlock(&ailp->ail_lock);
80230136832SDave Chinner 		if (!XFS_FORCED_SHUTDOWN(mp)) {
8036a19d939SDave Chinner 			xfs_alert_tag(mp, XFS_PTAG_AILDELETE,
80430136832SDave Chinner 	"%s: attempting to delete a log item that is not in the AIL",
80530136832SDave Chinner 					__func__);
80604913fddSDave Chinner 			xfs_force_shutdown(mp, shutdown_type);
80730136832SDave Chinner 		}
80830136832SDave Chinner 		return;
80930136832SDave Chinner 	}
81030136832SDave Chinner 
81127af1bbfSChristoph Hellwig 	mlip_changed = xfs_ail_delete_one(ailp, lip);
8121c304625SChristoph Hellwig 	if (mlip_changed) {
81327af1bbfSChristoph Hellwig 		if (!XFS_FORCED_SHUTDOWN(mp))
81427af1bbfSChristoph Hellwig 			xlog_assign_tail_lsn_locked(mp);
81557e80956SMatthew Wilcox 		if (list_empty(&ailp->ail_head))
81657e80956SMatthew Wilcox 			wake_up_all(&ailp->ail_empty);
81730136832SDave Chinner 	}
81827af1bbfSChristoph Hellwig 
81957e80956SMatthew Wilcox 	spin_unlock(&ailp->ail_lock);
82027af1bbfSChristoph Hellwig 	if (mlip_changed)
82157e80956SMatthew Wilcox 		xfs_log_space_wake(ailp->ail_mount);
82230136832SDave Chinner }
8231da177e4SLinus Torvalds 
824249a8c11SDavid Chinner int
8251da177e4SLinus Torvalds xfs_trans_ail_init(
8261da177e4SLinus Torvalds 	xfs_mount_t	*mp)
8271da177e4SLinus Torvalds {
82882fa9012SDavid Chinner 	struct xfs_ail	*ailp;
82982fa9012SDavid Chinner 
83082fa9012SDavid Chinner 	ailp = kmem_zalloc(sizeof(struct xfs_ail), KM_MAYFAIL);
83182fa9012SDavid Chinner 	if (!ailp)
8322451337dSDave Chinner 		return -ENOMEM;
83382fa9012SDavid Chinner 
83457e80956SMatthew Wilcox 	ailp->ail_mount = mp;
83557e80956SMatthew Wilcox 	INIT_LIST_HEAD(&ailp->ail_head);
83657e80956SMatthew Wilcox 	INIT_LIST_HEAD(&ailp->ail_cursors);
83757e80956SMatthew Wilcox 	spin_lock_init(&ailp->ail_lock);
83857e80956SMatthew Wilcox 	INIT_LIST_HEAD(&ailp->ail_buf_list);
83957e80956SMatthew Wilcox 	init_waitqueue_head(&ailp->ail_empty);
8400030807cSChristoph Hellwig 
84157e80956SMatthew Wilcox 	ailp->ail_task = kthread_run(xfsaild, ailp, "xfsaild/%s",
842e1d3d218SIan Kent 			ailp->ail_mount->m_super->s_id);
84357e80956SMatthew Wilcox 	if (IS_ERR(ailp->ail_task))
8440030807cSChristoph Hellwig 		goto out_free_ailp;
8450030807cSChristoph Hellwig 
84627d8d5feSDavid Chinner 	mp->m_ail = ailp;
84727d8d5feSDavid Chinner 	return 0;
8480030807cSChristoph Hellwig 
8490030807cSChristoph Hellwig out_free_ailp:
8500030807cSChristoph Hellwig 	kmem_free(ailp);
8512451337dSDave Chinner 	return -ENOMEM;
852249a8c11SDavid Chinner }
853249a8c11SDavid Chinner 
854249a8c11SDavid Chinner void
855249a8c11SDavid Chinner xfs_trans_ail_destroy(
856249a8c11SDavid Chinner 	xfs_mount_t	*mp)
857249a8c11SDavid Chinner {
85882fa9012SDavid Chinner 	struct xfs_ail	*ailp = mp->m_ail;
85982fa9012SDavid Chinner 
86057e80956SMatthew Wilcox 	kthread_stop(ailp->ail_task);
86182fa9012SDavid Chinner 	kmem_free(ailp);
8621da177e4SLinus Torvalds }
863