xref: /openbmc/linux/fs/xfs/xfs_trans_ail.c (revision f5d39b02)
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"
200020a190SDave Chinner #include "xfs_log_priv.h"
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds #ifdef DEBUG
23cd4a3c50SDave Chinner /*
24cd4a3c50SDave Chinner  * Check that the list is sorted as it should be.
25d686d12dSDave Chinner  *
26d686d12dSDave Chinner  * Called with the ail lock held, but we don't want to assert fail with it
27d686d12dSDave Chinner  * held otherwise we'll lock everything up and won't be able to debug the
28d686d12dSDave Chinner  * cause. Hence we sample and check the state under the AIL lock and return if
29d686d12dSDave Chinner  * everything is fine, otherwise we drop the lock and run the ASSERT checks.
30d686d12dSDave Chinner  * Asserts may not be fatal, so pick the lock back up and continue onwards.
31cd4a3c50SDave Chinner  */
32cd4a3c50SDave Chinner STATIC void
33cd4a3c50SDave Chinner xfs_ail_check(
34cd4a3c50SDave Chinner 	struct xfs_ail		*ailp,
35d686d12dSDave Chinner 	struct xfs_log_item	*lip)
36daebba1bSJules Irenge 	__must_hold(&ailp->ail_lock)
37cd4a3c50SDave Chinner {
38d686d12dSDave Chinner 	struct xfs_log_item	*prev_lip;
39d686d12dSDave Chinner 	struct xfs_log_item	*next_lip;
40d686d12dSDave Chinner 	xfs_lsn_t		prev_lsn = NULLCOMMITLSN;
41d686d12dSDave Chinner 	xfs_lsn_t		next_lsn = NULLCOMMITLSN;
42d686d12dSDave Chinner 	xfs_lsn_t		lsn;
43d686d12dSDave Chinner 	bool			in_ail;
44d686d12dSDave Chinner 
45cd4a3c50SDave Chinner 
4657e80956SMatthew Wilcox 	if (list_empty(&ailp->ail_head))
47cd4a3c50SDave Chinner 		return;
48cd4a3c50SDave Chinner 
49cd4a3c50SDave Chinner 	/*
50d686d12dSDave Chinner 	 * Sample then check the next and previous entries are valid.
51cd4a3c50SDave Chinner 	 */
52d686d12dSDave Chinner 	in_ail = test_bit(XFS_LI_IN_AIL, &lip->li_flags);
53d686d12dSDave Chinner 	prev_lip = list_entry(lip->li_ail.prev, struct xfs_log_item, li_ail);
5457e80956SMatthew Wilcox 	if (&prev_lip->li_ail != &ailp->ail_head)
55d686d12dSDave Chinner 		prev_lsn = prev_lip->li_lsn;
56d686d12dSDave Chinner 	next_lip = list_entry(lip->li_ail.next, struct xfs_log_item, li_ail);
57d686d12dSDave Chinner 	if (&next_lip->li_ail != &ailp->ail_head)
58d686d12dSDave Chinner 		next_lsn = next_lip->li_lsn;
59d686d12dSDave Chinner 	lsn = lip->li_lsn;
60cd4a3c50SDave Chinner 
61d686d12dSDave Chinner 	if (in_ail &&
62d686d12dSDave Chinner 	    (prev_lsn == NULLCOMMITLSN || XFS_LSN_CMP(prev_lsn, lsn) <= 0) &&
63d686d12dSDave Chinner 	    (next_lsn == NULLCOMMITLSN || XFS_LSN_CMP(next_lsn, lsn) >= 0))
64d686d12dSDave Chinner 		return;
65cd4a3c50SDave Chinner 
66d686d12dSDave Chinner 	spin_unlock(&ailp->ail_lock);
67d686d12dSDave Chinner 	ASSERT(in_ail);
68d686d12dSDave Chinner 	ASSERT(prev_lsn == NULLCOMMITLSN || XFS_LSN_CMP(prev_lsn, lsn) <= 0);
69d686d12dSDave Chinner 	ASSERT(next_lsn == NULLCOMMITLSN || XFS_LSN_CMP(next_lsn, lsn) >= 0);
70d686d12dSDave Chinner 	spin_lock(&ailp->ail_lock);
71cd4a3c50SDave Chinner }
72cd4a3c50SDave Chinner #else /* !DEBUG */
73de08dbc1SDavid Chinner #define	xfs_ail_check(a,l)
741da177e4SLinus Torvalds #endif /* DEBUG */
751da177e4SLinus Torvalds 
76cd4a3c50SDave Chinner /*
77fd074841SDave Chinner  * Return a pointer to the last item in the AIL.  If the AIL is empty, then
78fd074841SDave Chinner  * return NULL.
79fd074841SDave Chinner  */
80efe2330fSChristoph Hellwig static struct xfs_log_item *
81fd074841SDave Chinner xfs_ail_max(
82fd074841SDave Chinner 	struct xfs_ail  *ailp)
83fd074841SDave Chinner {
8457e80956SMatthew Wilcox 	if (list_empty(&ailp->ail_head))
85fd074841SDave Chinner 		return NULL;
86fd074841SDave Chinner 
87efe2330fSChristoph Hellwig 	return list_entry(ailp->ail_head.prev, struct xfs_log_item, li_ail);
88fd074841SDave Chinner }
89fd074841SDave Chinner 
90fd074841SDave Chinner /*
91cd4a3c50SDave Chinner  * Return a pointer to the item which follows the given item in the AIL.  If
92cd4a3c50SDave Chinner  * the given item is the last item in the list, then return NULL.
93cd4a3c50SDave Chinner  */
94efe2330fSChristoph Hellwig static struct xfs_log_item *
95cd4a3c50SDave Chinner xfs_ail_next(
96cd4a3c50SDave Chinner 	struct xfs_ail		*ailp,
97efe2330fSChristoph Hellwig 	struct xfs_log_item	*lip)
98cd4a3c50SDave Chinner {
9957e80956SMatthew Wilcox 	if (lip->li_ail.next == &ailp->ail_head)
100cd4a3c50SDave Chinner 		return NULL;
101cd4a3c50SDave Chinner 
102efe2330fSChristoph Hellwig 	return list_first_entry(&lip->li_ail, struct xfs_log_item, li_ail);
103cd4a3c50SDave Chinner }
104cd4a3c50SDave Chinner 
105cd4a3c50SDave Chinner /*
106cd4a3c50SDave Chinner  * This is called by the log manager code to determine the LSN of the tail of
107cd4a3c50SDave Chinner  * the log.  This is exactly the LSN of the first item in the AIL.  If the AIL
108cd4a3c50SDave Chinner  * is empty, then this function returns 0.
1091da177e4SLinus Torvalds  *
110cd4a3c50SDave Chinner  * We need the AIL lock in order to get a coherent read of the lsn of the last
111cd4a3c50SDave Chinner  * item in the AIL.
1121da177e4SLinus Torvalds  */
1138eb807bdSDave Chinner static xfs_lsn_t
1148eb807bdSDave Chinner __xfs_ail_min_lsn(
1158eb807bdSDave Chinner 	struct xfs_ail		*ailp)
1168eb807bdSDave Chinner {
1178eb807bdSDave Chinner 	struct xfs_log_item	*lip = xfs_ail_min(ailp);
1188eb807bdSDave Chinner 
1198eb807bdSDave Chinner 	if (lip)
1208eb807bdSDave Chinner 		return lip->li_lsn;
1218eb807bdSDave Chinner 	return 0;
1228eb807bdSDave Chinner }
1238eb807bdSDave Chinner 
1241da177e4SLinus Torvalds xfs_lsn_t
125fd074841SDave Chinner xfs_ail_min_lsn(
1265b00f14fSDavid Chinner 	struct xfs_ail		*ailp)
1271da177e4SLinus Torvalds {
1288eb807bdSDave Chinner 	xfs_lsn_t		lsn;
1291da177e4SLinus Torvalds 
13057e80956SMatthew Wilcox 	spin_lock(&ailp->ail_lock);
1318eb807bdSDave Chinner 	lsn = __xfs_ail_min_lsn(ailp);
13257e80956SMatthew Wilcox 	spin_unlock(&ailp->ail_lock);
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds 	return lsn;
1351da177e4SLinus Torvalds }
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds /*
138fd074841SDave Chinner  * Return the maximum lsn held in the AIL, or zero if the AIL is empty.
139fd074841SDave Chinner  */
140fd074841SDave Chinner static xfs_lsn_t
141fd074841SDave Chinner xfs_ail_max_lsn(
142fd074841SDave Chinner 	struct xfs_ail		*ailp)
143fd074841SDave Chinner {
144fd074841SDave Chinner 	xfs_lsn_t       	lsn = 0;
145efe2330fSChristoph Hellwig 	struct xfs_log_item	*lip;
146fd074841SDave Chinner 
14757e80956SMatthew Wilcox 	spin_lock(&ailp->ail_lock);
148fd074841SDave Chinner 	lip = xfs_ail_max(ailp);
149fd074841SDave Chinner 	if (lip)
150fd074841SDave Chinner 		lsn = lip->li_lsn;
15157e80956SMatthew Wilcox 	spin_unlock(&ailp->ail_lock);
152fd074841SDave Chinner 
153fd074841SDave Chinner 	return lsn;
154fd074841SDave Chinner }
155fd074841SDave Chinner 
156fd074841SDave Chinner /*
157af3e4022SDave Chinner  * The cursor keeps track of where our current traversal is up to by tracking
158af3e4022SDave Chinner  * the next item in the list for us. However, for this to be safe, removing an
159af3e4022SDave Chinner  * object from the AIL needs to invalidate any cursor that points to it. hence
160af3e4022SDave Chinner  * the traversal cursor needs to be linked to the struct xfs_ail so that
161af3e4022SDave Chinner  * deletion can search all the active cursors for invalidation.
16227d8d5feSDavid Chinner  */
1635b00f14fSDavid Chinner STATIC void
16427d8d5feSDavid Chinner xfs_trans_ail_cursor_init(
16527d8d5feSDavid Chinner 	struct xfs_ail		*ailp,
16627d8d5feSDavid Chinner 	struct xfs_ail_cursor	*cur)
16727d8d5feSDavid Chinner {
16827d8d5feSDavid Chinner 	cur->item = NULL;
16957e80956SMatthew Wilcox 	list_add_tail(&cur->list, &ailp->ail_cursors);
17027d8d5feSDavid Chinner }
17127d8d5feSDavid Chinner 
17227d8d5feSDavid Chinner /*
173af3e4022SDave Chinner  * Get the next item in the traversal and advance the cursor.  If the cursor
174af3e4022SDave Chinner  * was invalidated (indicated by a lip of 1), restart the traversal.
17527d8d5feSDavid Chinner  */
1765b00f14fSDavid Chinner struct xfs_log_item *
17727d8d5feSDavid Chinner xfs_trans_ail_cursor_next(
17827d8d5feSDavid Chinner 	struct xfs_ail		*ailp,
17927d8d5feSDavid Chinner 	struct xfs_ail_cursor	*cur)
18027d8d5feSDavid Chinner {
18127d8d5feSDavid Chinner 	struct xfs_log_item	*lip = cur->item;
18227d8d5feSDavid Chinner 
183db9d67d6SChristoph Hellwig 	if ((uintptr_t)lip & 1)
18427d8d5feSDavid Chinner 		lip = xfs_ail_min(ailp);
18516b59029SDave Chinner 	if (lip)
18616b59029SDave Chinner 		cur->item = xfs_ail_next(ailp, lip);
18727d8d5feSDavid Chinner 	return lip;
18827d8d5feSDavid Chinner }
18927d8d5feSDavid Chinner 
19027d8d5feSDavid Chinner /*
191af3e4022SDave Chinner  * When the traversal is complete, we need to remove the cursor from the list
192af3e4022SDave Chinner  * of traversing cursors.
19327d8d5feSDavid Chinner  */
19427d8d5feSDavid Chinner void
19527d8d5feSDavid Chinner xfs_trans_ail_cursor_done(
196af3e4022SDave Chinner 	struct xfs_ail_cursor	*cur)
19727d8d5feSDavid Chinner {
198af3e4022SDave Chinner 	cur->item = NULL;
199af3e4022SDave Chinner 	list_del_init(&cur->list);
20027d8d5feSDavid Chinner }
20127d8d5feSDavid Chinner 
20227d8d5feSDavid Chinner /*
203af3e4022SDave Chinner  * Invalidate any cursor that is pointing to this item. This is called when an
204af3e4022SDave Chinner  * item is removed from the AIL. Any cursor pointing to this object is now
205af3e4022SDave Chinner  * invalid and the traversal needs to be terminated so it doesn't reference a
206af3e4022SDave Chinner  * freed object. We set the low bit of the cursor item pointer so we can
207af3e4022SDave Chinner  * distinguish between an invalidation and the end of the list when getting the
208af3e4022SDave Chinner  * next item from the cursor.
2095b00f14fSDavid Chinner  */
2105b00f14fSDavid Chinner STATIC void
2115b00f14fSDavid Chinner xfs_trans_ail_cursor_clear(
2125b00f14fSDavid Chinner 	struct xfs_ail		*ailp,
2135b00f14fSDavid Chinner 	struct xfs_log_item	*lip)
2145b00f14fSDavid Chinner {
2155b00f14fSDavid Chinner 	struct xfs_ail_cursor	*cur;
2165b00f14fSDavid Chinner 
21757e80956SMatthew Wilcox 	list_for_each_entry(cur, &ailp->ail_cursors, list) {
2185b00f14fSDavid Chinner 		if (cur->item == lip)
2195b00f14fSDavid Chinner 			cur->item = (struct xfs_log_item *)
220db9d67d6SChristoph Hellwig 					((uintptr_t)cur->item | 1);
2215b00f14fSDavid Chinner 	}
2225b00f14fSDavid Chinner }
2235b00f14fSDavid Chinner 
2245b00f14fSDavid Chinner /*
22516b59029SDave Chinner  * Find the first item in the AIL with the given @lsn by searching in ascending
22616b59029SDave Chinner  * LSN order and initialise the cursor to point to the next item for a
22716b59029SDave Chinner  * ascending traversal.  Pass a @lsn of zero to initialise the cursor to the
22816b59029SDave Chinner  * first item in the AIL. Returns NULL if the list is empty.
229249a8c11SDavid Chinner  */
230efe2330fSChristoph Hellwig struct xfs_log_item *
2315b00f14fSDavid Chinner xfs_trans_ail_cursor_first(
23227d8d5feSDavid Chinner 	struct xfs_ail		*ailp,
23327d8d5feSDavid Chinner 	struct xfs_ail_cursor	*cur,
234249a8c11SDavid Chinner 	xfs_lsn_t		lsn)
235249a8c11SDavid Chinner {
236efe2330fSChristoph Hellwig 	struct xfs_log_item	*lip;
237249a8c11SDavid Chinner 
2385b00f14fSDavid Chinner 	xfs_trans_ail_cursor_init(ailp, cur);
23916b59029SDave Chinner 
24016b59029SDave Chinner 	if (lsn == 0) {
24127d8d5feSDavid Chinner 		lip = xfs_ail_min(ailp);
2425b00f14fSDavid Chinner 		goto out;
24316b59029SDave Chinner 	}
244249a8c11SDavid Chinner 
24557e80956SMatthew Wilcox 	list_for_each_entry(lip, &ailp->ail_head, li_ail) {
2465b00f14fSDavid Chinner 		if (XFS_LSN_CMP(lip->li_lsn, lsn) >= 0)
2477ee49acfSDavid Chinner 			goto out;
2485b00f14fSDavid Chinner 	}
24916b59029SDave Chinner 	return NULL;
25016b59029SDave Chinner 
2515b00f14fSDavid Chinner out:
25216b59029SDave Chinner 	if (lip)
25316b59029SDave Chinner 		cur->item = xfs_ail_next(ailp, lip);
254249a8c11SDavid Chinner 	return lip;
255249a8c11SDavid Chinner }
256535f6b37SJosef 'Jeff' Sipek 
2571d8c95a3SDave Chinner static struct xfs_log_item *
2581d8c95a3SDave Chinner __xfs_trans_ail_cursor_last(
2591d8c95a3SDave Chinner 	struct xfs_ail		*ailp,
2601d8c95a3SDave Chinner 	xfs_lsn_t		lsn)
2611d8c95a3SDave Chinner {
262efe2330fSChristoph Hellwig 	struct xfs_log_item	*lip;
2631d8c95a3SDave Chinner 
26457e80956SMatthew Wilcox 	list_for_each_entry_reverse(lip, &ailp->ail_head, li_ail) {
2651d8c95a3SDave Chinner 		if (XFS_LSN_CMP(lip->li_lsn, lsn) <= 0)
2661d8c95a3SDave Chinner 			return lip;
2671d8c95a3SDave Chinner 	}
2681d8c95a3SDave Chinner 	return NULL;
2691d8c95a3SDave Chinner }
2701d8c95a3SDave Chinner 
2711d8c95a3SDave Chinner /*
27216b59029SDave Chinner  * Find the last item in the AIL with the given @lsn by searching in descending
27316b59029SDave Chinner  * LSN order and initialise the cursor to point to that item.  If there is no
27416b59029SDave Chinner  * item with the value of @lsn, then it sets the cursor to the last item with an
27516b59029SDave Chinner  * LSN lower than @lsn.  Returns NULL if the list is empty.
2761d8c95a3SDave Chinner  */
2771d8c95a3SDave Chinner struct xfs_log_item *
2781d8c95a3SDave Chinner xfs_trans_ail_cursor_last(
2791d8c95a3SDave Chinner 	struct xfs_ail		*ailp,
2801d8c95a3SDave Chinner 	struct xfs_ail_cursor	*cur,
2811d8c95a3SDave Chinner 	xfs_lsn_t		lsn)
2821d8c95a3SDave Chinner {
2831d8c95a3SDave Chinner 	xfs_trans_ail_cursor_init(ailp, cur);
2841d8c95a3SDave Chinner 	cur->item = __xfs_trans_ail_cursor_last(ailp, lsn);
2851d8c95a3SDave Chinner 	return cur->item;
2861d8c95a3SDave Chinner }
2871d8c95a3SDave Chinner 
2881d8c95a3SDave Chinner /*
28916b59029SDave Chinner  * Splice the log item list into the AIL at the given LSN. We splice to the
2901d8c95a3SDave Chinner  * tail of the given LSN to maintain insert order for push traversals. The
2911d8c95a3SDave Chinner  * cursor is optional, allowing repeated updates to the same LSN to avoid
292e44f4112SAlex Elder  * repeated traversals.  This should not be called with an empty list.
293cd4a3c50SDave Chinner  */
294cd4a3c50SDave Chinner static void
295cd4a3c50SDave Chinner xfs_ail_splice(
296cd4a3c50SDave Chinner 	struct xfs_ail		*ailp,
2971d8c95a3SDave Chinner 	struct xfs_ail_cursor	*cur,
298cd4a3c50SDave Chinner 	struct list_head	*list,
299cd4a3c50SDave Chinner 	xfs_lsn_t		lsn)
300cd4a3c50SDave Chinner {
301e44f4112SAlex Elder 	struct xfs_log_item	*lip;
302e44f4112SAlex Elder 
303e44f4112SAlex Elder 	ASSERT(!list_empty(list));
304cd4a3c50SDave Chinner 
3051d8c95a3SDave Chinner 	/*
306e44f4112SAlex Elder 	 * Use the cursor to determine the insertion point if one is
307e44f4112SAlex Elder 	 * provided.  If not, or if the one we got is not valid,
308e44f4112SAlex Elder 	 * find the place in the AIL where the items belong.
3091d8c95a3SDave Chinner 	 */
310e44f4112SAlex Elder 	lip = cur ? cur->item : NULL;
311db9d67d6SChristoph Hellwig 	if (!lip || (uintptr_t)lip & 1)
3121d8c95a3SDave Chinner 		lip = __xfs_trans_ail_cursor_last(ailp, lsn);
3131d8c95a3SDave Chinner 
314e44f4112SAlex Elder 	/*
315e44f4112SAlex Elder 	 * If a cursor is provided, we know we're processing the AIL
316e44f4112SAlex Elder 	 * in lsn order, and future items to be spliced in will
317e44f4112SAlex Elder 	 * follow the last one being inserted now.  Update the
318e44f4112SAlex Elder 	 * cursor to point to that last item, now while we have a
319e44f4112SAlex Elder 	 * reliable pointer to it.
320e44f4112SAlex Elder 	 */
3211d8c95a3SDave Chinner 	if (cur)
322e44f4112SAlex Elder 		cur->item = list_entry(list->prev, struct xfs_log_item, li_ail);
323cd4a3c50SDave Chinner 
3241d8c95a3SDave Chinner 	/*
325e44f4112SAlex Elder 	 * Finally perform the splice.  Unless the AIL was empty,
326e44f4112SAlex Elder 	 * lip points to the item in the AIL _after_ which the new
327e44f4112SAlex Elder 	 * items should go.  If lip is null the AIL was empty, so
328e44f4112SAlex Elder 	 * the new items go at the head of the AIL.
3291d8c95a3SDave Chinner 	 */
330e44f4112SAlex Elder 	if (lip)
3311d8c95a3SDave Chinner 		list_splice(list, &lip->li_ail);
332e44f4112SAlex Elder 	else
33357e80956SMatthew Wilcox 		list_splice(list, &ailp->ail_head);
334cd4a3c50SDave Chinner }
335cd4a3c50SDave Chinner 
336cd4a3c50SDave Chinner /*
337cd4a3c50SDave Chinner  * Delete the given item from the AIL.  Return a pointer to the item.
338cd4a3c50SDave Chinner  */
339cd4a3c50SDave Chinner static void
340cd4a3c50SDave Chinner xfs_ail_delete(
341cd4a3c50SDave Chinner 	struct xfs_ail		*ailp,
342efe2330fSChristoph Hellwig 	struct xfs_log_item	*lip)
343cd4a3c50SDave Chinner {
344cd4a3c50SDave Chinner 	xfs_ail_check(ailp, lip);
345cd4a3c50SDave Chinner 	list_del(&lip->li_ail);
346cd4a3c50SDave Chinner 	xfs_trans_ail_cursor_clear(ailp, lip);
347cd4a3c50SDave Chinner }
348cd4a3c50SDave Chinner 
349cb6ad099SBrian Foster /*
350cb6ad099SBrian Foster  * Requeue a failed buffer for writeback.
351cb6ad099SBrian Foster  *
352cb6ad099SBrian Foster  * We clear the log item failed state here as well, but we have to be careful
353cb6ad099SBrian Foster  * about reference counts because the only active reference counts on the buffer
354cb6ad099SBrian Foster  * may be the failed log items. Hence if we clear the log item failed state
355cb6ad099SBrian Foster  * before queuing the buffer for IO we can release all active references to
356cb6ad099SBrian Foster  * the buffer and free it, leading to use after free problems in
357cb6ad099SBrian Foster  * xfs_buf_delwri_queue. It makes no difference to the buffer or log items which
358cb6ad099SBrian Foster  * order we process them in - the buffer is locked, and we own the buffer list
359cb6ad099SBrian Foster  * so nothing on them is going to change while we are performing this action.
360cb6ad099SBrian Foster  *
361cb6ad099SBrian Foster  * Hence we can safely queue the buffer for IO before we clear the failed log
362cb6ad099SBrian Foster  * item state, therefore  always having an active reference to the buffer and
363cb6ad099SBrian Foster  * avoiding the transient zero-reference state that leads to use-after-free.
364cb6ad099SBrian Foster  */
365cb6ad099SBrian Foster static inline int
366cb6ad099SBrian Foster xfsaild_resubmit_item(
367cb6ad099SBrian Foster 	struct xfs_log_item	*lip,
368cb6ad099SBrian Foster 	struct list_head	*buffer_list)
369cb6ad099SBrian Foster {
370cb6ad099SBrian Foster 	struct xfs_buf		*bp = lip->li_buf;
371cb6ad099SBrian Foster 
372cb6ad099SBrian Foster 	if (!xfs_buf_trylock(bp))
373cb6ad099SBrian Foster 		return XFS_ITEM_LOCKED;
374cb6ad099SBrian Foster 
375cb6ad099SBrian Foster 	if (!xfs_buf_delwri_queue(bp, buffer_list)) {
376cb6ad099SBrian Foster 		xfs_buf_unlock(bp);
377cb6ad099SBrian Foster 		return XFS_ITEM_FLUSHING;
378cb6ad099SBrian Foster 	}
379cb6ad099SBrian Foster 
380cb6ad099SBrian Foster 	/* protected by ail_lock */
381298f7becSDave Chinner 	list_for_each_entry(lip, &bp->b_li_list, li_bio_list) {
382298f7becSDave Chinner 		if (bp->b_flags & _XBF_INODES)
383298f7becSDave Chinner 			clear_bit(XFS_LI_FAILED, &lip->li_flags);
384298f7becSDave Chinner 		else
385cb6ad099SBrian Foster 			xfs_clear_li_failed(lip);
386298f7becSDave Chinner 	}
387cb6ad099SBrian Foster 
388cb6ad099SBrian Foster 	xfs_buf_unlock(bp);
389cb6ad099SBrian Foster 	return XFS_ITEM_SUCCESS;
390cb6ad099SBrian Foster }
391cb6ad099SBrian Foster 
3927f4d01f3SBrian Foster static inline uint
3937f4d01f3SBrian Foster xfsaild_push_item(
3947f4d01f3SBrian Foster 	struct xfs_ail		*ailp,
3957f4d01f3SBrian Foster 	struct xfs_log_item	*lip)
3967f4d01f3SBrian Foster {
3977f4d01f3SBrian Foster 	/*
3987f4d01f3SBrian Foster 	 * If log item pinning is enabled, skip the push and track the item as
3997f4d01f3SBrian Foster 	 * pinned. This can help induce head-behind-tail conditions.
4007f4d01f3SBrian Foster 	 */
4018eda8721SDave Chinner 	if (XFS_TEST_ERROR(false, ailp->ail_log->l_mp, XFS_ERRTAG_LOG_ITEM_PIN))
4027f4d01f3SBrian Foster 		return XFS_ITEM_PINNED;
4037f4d01f3SBrian Foster 
404e8b78db7SChristoph Hellwig 	/*
405e8b78db7SChristoph Hellwig 	 * Consider the item pinned if a push callback is not defined so the
406e8b78db7SChristoph Hellwig 	 * caller will force the log. This should only happen for intent items
407e8b78db7SChristoph Hellwig 	 * as they are unpinned once the associated done item is committed to
408e8b78db7SChristoph Hellwig 	 * the on-disk log.
409e8b78db7SChristoph Hellwig 	 */
410e8b78db7SChristoph Hellwig 	if (!lip->li_ops->iop_push)
411e8b78db7SChristoph Hellwig 		return XFS_ITEM_PINNED;
412cb6ad099SBrian Foster 	if (test_bit(XFS_LI_FAILED, &lip->li_flags))
413cb6ad099SBrian Foster 		return xfsaild_resubmit_item(lip, &ailp->ail_buf_list);
41457e80956SMatthew Wilcox 	return lip->li_ops->iop_push(lip, &ailp->ail_buf_list);
4157f4d01f3SBrian Foster }
4167f4d01f3SBrian Foster 
4170030807cSChristoph Hellwig static long
4180030807cSChristoph Hellwig xfsaild_push(
4190030807cSChristoph Hellwig 	struct xfs_ail		*ailp)
420249a8c11SDavid Chinner {
4218eda8721SDave Chinner 	struct xfs_mount	*mp = ailp->ail_log->l_mp;
422af3e4022SDave Chinner 	struct xfs_ail_cursor	cur;
423efe2330fSChristoph Hellwig 	struct xfs_log_item	*lip;
4249e7004e7SDave Chinner 	xfs_lsn_t		lsn;
425fe0da767SDave Chinner 	xfs_lsn_t		target;
42643ff2122SChristoph Hellwig 	long			tout;
4279e7004e7SDave Chinner 	int			stuck = 0;
42843ff2122SChristoph Hellwig 	int			flushing = 0;
4299e7004e7SDave Chinner 	int			count = 0;
4301da177e4SLinus Torvalds 
431670ce93fSDave Chinner 	/*
43243ff2122SChristoph Hellwig 	 * If we encountered pinned items or did not finish writing out all
4330020a190SDave Chinner 	 * buffers the last time we ran, force a background CIL push to get the
4340020a190SDave Chinner 	 * items unpinned in the near future. We do not wait on the CIL push as
4350020a190SDave Chinner 	 * that could stall us for seconds if there is enough background IO
4360020a190SDave Chinner 	 * load. Stalling for that long when the tail of the log is pinned and
4370020a190SDave Chinner 	 * needs flushing will hard stop the transaction subsystem when log
4380020a190SDave Chinner 	 * space runs out.
439670ce93fSDave Chinner 	 */
44057e80956SMatthew Wilcox 	if (ailp->ail_log_flush && ailp->ail_last_pushed_lsn == 0 &&
44157e80956SMatthew Wilcox 	    (!list_empty_careful(&ailp->ail_buf_list) ||
44243ff2122SChristoph Hellwig 	     xfs_ail_min_lsn(ailp))) {
44357e80956SMatthew Wilcox 		ailp->ail_log_flush = 0;
44443ff2122SChristoph Hellwig 
445ff6d6af2SBill O'Donnell 		XFS_STATS_INC(mp, xs_push_ail_flush);
4468eda8721SDave Chinner 		xlog_cil_flush(ailp->ail_log);
447670ce93fSDave Chinner 	}
448670ce93fSDave Chinner 
44957e80956SMatthew Wilcox 	spin_lock(&ailp->ail_lock);
4508375f922SBrian Foster 
451941fbdfdSDave Chinner 	/*
452941fbdfdSDave Chinner 	 * If we have a sync push waiter, we always have to push till the AIL is
453941fbdfdSDave Chinner 	 * empty. Update the target to point to the end of the AIL so that
454941fbdfdSDave Chinner 	 * capture updates that occur after the sync push waiter has gone to
455941fbdfdSDave Chinner 	 * sleep.
456941fbdfdSDave Chinner 	 */
457941fbdfdSDave Chinner 	if (waitqueue_active(&ailp->ail_empty)) {
458941fbdfdSDave Chinner 		lip = xfs_ail_max(ailp);
459941fbdfdSDave Chinner 		if (lip)
460941fbdfdSDave Chinner 			target = lip->li_lsn;
461941fbdfdSDave Chinner 	} else {
46257e80956SMatthew Wilcox 		/* barrier matches the ail_target update in xfs_ail_push() */
4638375f922SBrian Foster 		smp_rmb();
46457e80956SMatthew Wilcox 		target = ailp->ail_target;
46557e80956SMatthew Wilcox 		ailp->ail_target_prev = target;
466941fbdfdSDave Chinner 	}
4678375f922SBrian Foster 
468f376b45eSBrian Foster 	/* we're done if the AIL is empty or our push has reached the end */
46957e80956SMatthew Wilcox 	lip = xfs_trans_ail_cursor_first(ailp, &cur, ailp->ail_last_pushed_lsn);
470f376b45eSBrian Foster 	if (!lip)
4719e7004e7SDave Chinner 		goto out_done;
4721da177e4SLinus Torvalds 
473ff6d6af2SBill O'Donnell 	XFS_STATS_INC(mp, xs_push_ail);
4741da177e4SLinus Torvalds 
475249a8c11SDavid Chinner 	lsn = lip->li_lsn;
47650e86686SDave Chinner 	while ((XFS_LSN_CMP(lip->li_lsn, target) <= 0)) {
477249a8c11SDavid Chinner 		int	lock_result;
47843ff2122SChristoph Hellwig 
479249a8c11SDavid Chinner 		/*
480904c17e6SDave Chinner 		 * Note that iop_push may unlock and reacquire the AIL lock.  We
48143ff2122SChristoph Hellwig 		 * rely on the AIL cursor implementation to be able to deal with
48243ff2122SChristoph Hellwig 		 * the dropped lock.
4831da177e4SLinus Torvalds 		 */
4847f4d01f3SBrian Foster 		lock_result = xfsaild_push_item(ailp, lip);
4851da177e4SLinus Torvalds 		switch (lock_result) {
4861da177e4SLinus Torvalds 		case XFS_ITEM_SUCCESS:
487ff6d6af2SBill O'Donnell 			XFS_STATS_INC(mp, xs_push_ail_success);
4889e4c109aSChristoph Hellwig 			trace_xfs_ail_push(lip);
4899e4c109aSChristoph Hellwig 
49057e80956SMatthew Wilcox 			ailp->ail_last_pushed_lsn = lsn;
4911da177e4SLinus Torvalds 			break;
4921da177e4SLinus Torvalds 
49343ff2122SChristoph Hellwig 		case XFS_ITEM_FLUSHING:
49443ff2122SChristoph Hellwig 			/*
495cf085a1bSJoe Perches 			 * The item or its backing buffer is already being
49643ff2122SChristoph Hellwig 			 * flushed.  The typical reason for that is that an
49743ff2122SChristoph Hellwig 			 * inode buffer is locked because we already pushed the
49843ff2122SChristoph Hellwig 			 * updates to it as part of inode clustering.
49943ff2122SChristoph Hellwig 			 *
500b63da6c8SRandy Dunlap 			 * We do not want to stop flushing just because lots
501cf085a1bSJoe Perches 			 * of items are already being flushed, but we need to
50243ff2122SChristoph Hellwig 			 * re-try the flushing relatively soon if most of the
503cf085a1bSJoe Perches 			 * AIL is being flushed.
50443ff2122SChristoph Hellwig 			 */
505ff6d6af2SBill O'Donnell 			XFS_STATS_INC(mp, xs_push_ail_flushing);
50643ff2122SChristoph Hellwig 			trace_xfs_ail_flushing(lip);
50717b38471SChristoph Hellwig 
50843ff2122SChristoph Hellwig 			flushing++;
50957e80956SMatthew Wilcox 			ailp->ail_last_pushed_lsn = lsn;
5101da177e4SLinus Torvalds 			break;
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds 		case XFS_ITEM_PINNED:
513ff6d6af2SBill O'Donnell 			XFS_STATS_INC(mp, xs_push_ail_pinned);
5149e4c109aSChristoph Hellwig 			trace_xfs_ail_pinned(lip);
5159e4c109aSChristoph Hellwig 
516249a8c11SDavid Chinner 			stuck++;
51757e80956SMatthew Wilcox 			ailp->ail_log_flush++;
5181da177e4SLinus Torvalds 			break;
5191da177e4SLinus Torvalds 		case XFS_ITEM_LOCKED:
520ff6d6af2SBill O'Donnell 			XFS_STATS_INC(mp, xs_push_ail_locked);
5219e4c109aSChristoph Hellwig 			trace_xfs_ail_locked(lip);
52243ff2122SChristoph Hellwig 
523249a8c11SDavid Chinner 			stuck++;
5241da177e4SLinus Torvalds 			break;
5251da177e4SLinus Torvalds 		default:
5261da177e4SLinus Torvalds 			ASSERT(0);
5271da177e4SLinus Torvalds 			break;
5281da177e4SLinus Torvalds 		}
5291da177e4SLinus Torvalds 
530249a8c11SDavid Chinner 		count++;
531249a8c11SDavid Chinner 
532249a8c11SDavid Chinner 		/*
533249a8c11SDavid Chinner 		 * Are there too many items we can't do anything with?
53443ff2122SChristoph Hellwig 		 *
535b63da6c8SRandy Dunlap 		 * If we are skipping too many items because we can't flush
536249a8c11SDavid Chinner 		 * them or they are already being flushed, we back off and
537249a8c11SDavid Chinner 		 * given them time to complete whatever operation is being
538249a8c11SDavid Chinner 		 * done. i.e. remove pressure from the AIL while we can't make
539249a8c11SDavid Chinner 		 * progress so traversals don't slow down further inserts and
540249a8c11SDavid Chinner 		 * removals to/from the AIL.
541249a8c11SDavid Chinner 		 *
542249a8c11SDavid Chinner 		 * The value of 100 is an arbitrary magic number based on
543249a8c11SDavid Chinner 		 * observation.
544249a8c11SDavid Chinner 		 */
545249a8c11SDavid Chinner 		if (stuck > 100)
546249a8c11SDavid Chinner 			break;
547249a8c11SDavid Chinner 
548af3e4022SDave Chinner 		lip = xfs_trans_ail_cursor_next(ailp, &cur);
549249a8c11SDavid Chinner 		if (lip == NULL)
550249a8c11SDavid Chinner 			break;
551249a8c11SDavid Chinner 		lsn = lip->li_lsn;
5521da177e4SLinus Torvalds 	}
553f376b45eSBrian Foster 
554f376b45eSBrian Foster out_done:
555e4a1e29cSEric Sandeen 	xfs_trans_ail_cursor_done(&cur);
55657e80956SMatthew Wilcox 	spin_unlock(&ailp->ail_lock);
5571da177e4SLinus Torvalds 
55857e80956SMatthew Wilcox 	if (xfs_buf_delwri_submit_nowait(&ailp->ail_buf_list))
55957e80956SMatthew Wilcox 		ailp->ail_log_flush++;
560d808f617SDave Chinner 
56143ff2122SChristoph Hellwig 	if (!count || XFS_LSN_CMP(lsn, target) >= 0) {
562249a8c11SDavid Chinner 		/*
56343ff2122SChristoph Hellwig 		 * We reached the target or the AIL is empty, so wait a bit
56443ff2122SChristoph Hellwig 		 * longer for I/O to complete and remove pushed items from the
56543ff2122SChristoph Hellwig 		 * AIL before we start the next scan from the start of the AIL.
566249a8c11SDavid Chinner 		 */
567453eac8aSDave Chinner 		tout = 50;
56857e80956SMatthew Wilcox 		ailp->ail_last_pushed_lsn = 0;
56943ff2122SChristoph Hellwig 	} else if (((stuck + flushing) * 100) / count > 90) {
570249a8c11SDavid Chinner 		/*
57143ff2122SChristoph Hellwig 		 * Either there is a lot of contention on the AIL or we are
57243ff2122SChristoph Hellwig 		 * stuck due to operations in progress. "Stuck" in this case
57343ff2122SChristoph Hellwig 		 * is defined as >90% of the items we tried to push were stuck.
574249a8c11SDavid Chinner 		 *
575249a8c11SDavid Chinner 		 * Backoff a bit more to allow some I/O to complete before
57643ff2122SChristoph Hellwig 		 * restarting from the start of the AIL. This prevents us from
57743ff2122SChristoph Hellwig 		 * spinning on the same items, and if they are pinned will all
57843ff2122SChristoph Hellwig 		 * the restart to issue a log force to unpin the stuck items.
579249a8c11SDavid Chinner 		 */
580453eac8aSDave Chinner 		tout = 20;
58157e80956SMatthew Wilcox 		ailp->ail_last_pushed_lsn = 0;
58243ff2122SChristoph Hellwig 	} else {
58343ff2122SChristoph Hellwig 		/*
58443ff2122SChristoph Hellwig 		 * Assume we have more work to do in a short while.
58543ff2122SChristoph Hellwig 		 */
58643ff2122SChristoph Hellwig 		tout = 10;
587453eac8aSDave Chinner 	}
5881da177e4SLinus Torvalds 
5890030807cSChristoph Hellwig 	return tout;
5900030807cSChristoph Hellwig }
5910030807cSChristoph Hellwig 
5920030807cSChristoph Hellwig static int
5930030807cSChristoph Hellwig xfsaild(
5940030807cSChristoph Hellwig 	void		*data)
5950030807cSChristoph Hellwig {
5960030807cSChristoph Hellwig 	struct xfs_ail	*ailp = data;
5970030807cSChristoph Hellwig 	long		tout = 0;	/* milliseconds */
59810a98cb1SEric Biggers 	unsigned int	noreclaim_flag;
5990030807cSChristoph Hellwig 
60010a98cb1SEric Biggers 	noreclaim_flag = memalloc_noreclaim_save();
60118f1df4eSMichal Hocko 	set_freezable();
60243ff2122SChristoph Hellwig 
6030bd89676SHou Tao 	while (1) {
6040030807cSChristoph Hellwig 		if (tout && tout <= 20)
605*f5d39b02SPeter Zijlstra 			set_current_state(TASK_KILLABLE|TASK_FREEZABLE);
6060030807cSChristoph Hellwig 		else
607*f5d39b02SPeter Zijlstra 			set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE);
6080bd89676SHou Tao 
6090bd89676SHou Tao 		/*
610efc3289cSBrian Foster 		 * Check kthread_should_stop() after we set the task state to
611efc3289cSBrian Foster 		 * guarantee that we either see the stop bit and exit or the
612efc3289cSBrian Foster 		 * task state is reset to runnable such that it's not scheduled
613efc3289cSBrian Foster 		 * out indefinitely and detects the stop bit at next iteration.
6140bd89676SHou Tao 		 * A memory barrier is included in above task state set to
6150bd89676SHou Tao 		 * serialize again kthread_stop().
6160bd89676SHou Tao 		 */
6170bd89676SHou Tao 		if (kthread_should_stop()) {
6180bd89676SHou Tao 			__set_current_state(TASK_RUNNING);
619efc3289cSBrian Foster 
620efc3289cSBrian Foster 			/*
621efc3289cSBrian Foster 			 * The caller forces out the AIL before stopping the
622efc3289cSBrian Foster 			 * thread in the common case, which means the delwri
623efc3289cSBrian Foster 			 * queue is drained. In the shutdown case, the queue may
624efc3289cSBrian Foster 			 * still hold relogged buffers that haven't been
625efc3289cSBrian Foster 			 * submitted because they were pinned since added to the
626efc3289cSBrian Foster 			 * queue.
627efc3289cSBrian Foster 			 *
628efc3289cSBrian Foster 			 * Log I/O error processing stales the underlying buffer
629efc3289cSBrian Foster 			 * and clears the delwri state, expecting the buf to be
630efc3289cSBrian Foster 			 * removed on the next submission attempt. That won't
631efc3289cSBrian Foster 			 * happen if we're shutting down, so this is the last
632efc3289cSBrian Foster 			 * opportunity to release such buffers from the queue.
633efc3289cSBrian Foster 			 */
634efc3289cSBrian Foster 			ASSERT(list_empty(&ailp->ail_buf_list) ||
6358eda8721SDave Chinner 			       xlog_is_shutdown(ailp->ail_log));
636efc3289cSBrian Foster 			xfs_buf_delwri_cancel(&ailp->ail_buf_list);
6370bd89676SHou Tao 			break;
6380bd89676SHou Tao 		}
6398375f922SBrian Foster 
64057e80956SMatthew Wilcox 		spin_lock(&ailp->ail_lock);
6418375f922SBrian Foster 
6428375f922SBrian Foster 		/*
6438375f922SBrian Foster 		 * Idle if the AIL is empty and we are not racing with a target
6448375f922SBrian Foster 		 * update. We check the AIL after we set the task to a sleep
64557e80956SMatthew Wilcox 		 * state to guarantee that we either catch an ail_target update
6468375f922SBrian Foster 		 * or that a wake_up resets the state to TASK_RUNNING.
6478375f922SBrian Foster 		 * Otherwise, we run the risk of sleeping indefinitely.
6488375f922SBrian Foster 		 *
64957e80956SMatthew Wilcox 		 * The barrier matches the ail_target update in xfs_ail_push().
6508375f922SBrian Foster 		 */
6518375f922SBrian Foster 		smp_rmb();
6528375f922SBrian Foster 		if (!xfs_ail_min(ailp) &&
653f376b45eSBrian Foster 		    ailp->ail_target == ailp->ail_target_prev &&
654f376b45eSBrian Foster 		    list_empty(&ailp->ail_buf_list)) {
65557e80956SMatthew Wilcox 			spin_unlock(&ailp->ail_lock);
656*f5d39b02SPeter Zijlstra 			schedule();
6578375f922SBrian Foster 			tout = 0;
6588375f922SBrian Foster 			continue;
6598375f922SBrian Foster 		}
66057e80956SMatthew Wilcox 		spin_unlock(&ailp->ail_lock);
6618375f922SBrian Foster 
6628375f922SBrian Foster 		if (tout)
663*f5d39b02SPeter Zijlstra 			schedule_timeout(msecs_to_jiffies(tout));
6648375f922SBrian Foster 
6658375f922SBrian Foster 		__set_current_state(TASK_RUNNING);
6660030807cSChristoph Hellwig 
6670030807cSChristoph Hellwig 		try_to_freeze();
6680030807cSChristoph Hellwig 
6690030807cSChristoph Hellwig 		tout = xfsaild_push(ailp);
6700030807cSChristoph Hellwig 	}
6710030807cSChristoph Hellwig 
67210a98cb1SEric Biggers 	memalloc_noreclaim_restore(noreclaim_flag);
6730030807cSChristoph Hellwig 	return 0;
6740bf6a5bdSDave Chinner }
6750bf6a5bdSDave Chinner 
6760bf6a5bdSDave Chinner /*
6770bf6a5bdSDave Chinner  * This routine is called to move the tail of the AIL forward.  It does this by
6780bf6a5bdSDave Chinner  * trying to flush items in the AIL whose lsns are below the given
6790bf6a5bdSDave Chinner  * threshold_lsn.
6800bf6a5bdSDave Chinner  *
6810bf6a5bdSDave Chinner  * The push is run asynchronously in a workqueue, which means the caller needs
6820bf6a5bdSDave Chinner  * to handle waiting on the async flush for space to become available.
6830bf6a5bdSDave Chinner  * We don't want to interrupt any push that is in progress, hence we only queue
684cf085a1bSJoe Perches  * work if we set the pushing bit appropriately.
6850bf6a5bdSDave Chinner  *
6860bf6a5bdSDave Chinner  * We do this unlocked - we only need to know whether there is anything in the
6870bf6a5bdSDave Chinner  * AIL at the time we are called. We don't need to access the contents of
6880bf6a5bdSDave Chinner  * any of the objects, so the lock is not needed.
6890bf6a5bdSDave Chinner  */
6900bf6a5bdSDave Chinner void
691fd074841SDave Chinner xfs_ail_push(
6920bf6a5bdSDave Chinner 	struct xfs_ail		*ailp,
6930bf6a5bdSDave Chinner 	xfs_lsn_t		threshold_lsn)
6940bf6a5bdSDave Chinner {
695efe2330fSChristoph Hellwig 	struct xfs_log_item	*lip;
6960bf6a5bdSDave Chinner 
6970bf6a5bdSDave Chinner 	lip = xfs_ail_min(ailp);
6988eda8721SDave Chinner 	if (!lip || xlog_is_shutdown(ailp->ail_log) ||
69957e80956SMatthew Wilcox 	    XFS_LSN_CMP(threshold_lsn, ailp->ail_target) <= 0)
7000bf6a5bdSDave Chinner 		return;
7010bf6a5bdSDave Chinner 
7020bf6a5bdSDave Chinner 	/*
7030bf6a5bdSDave Chinner 	 * Ensure that the new target is noticed in push code before it clears
7040bf6a5bdSDave Chinner 	 * the XFS_AIL_PUSHING_BIT.
7050bf6a5bdSDave Chinner 	 */
7060bf6a5bdSDave Chinner 	smp_wmb();
70757e80956SMatthew Wilcox 	xfs_trans_ail_copy_lsn(ailp, &ailp->ail_target, &threshold_lsn);
7080030807cSChristoph Hellwig 	smp_wmb();
7090030807cSChristoph Hellwig 
71057e80956SMatthew Wilcox 	wake_up_process(ailp->ail_task);
7110bf6a5bdSDave Chinner }
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds /*
714fd074841SDave Chinner  * Push out all items in the AIL immediately
715fd074841SDave Chinner  */
716fd074841SDave Chinner void
717fd074841SDave Chinner xfs_ail_push_all(
718fd074841SDave Chinner 	struct xfs_ail  *ailp)
719fd074841SDave Chinner {
720fd074841SDave Chinner 	xfs_lsn_t       threshold_lsn = xfs_ail_max_lsn(ailp);
721fd074841SDave Chinner 
722fd074841SDave Chinner 	if (threshold_lsn)
723fd074841SDave Chinner 		xfs_ail_push(ailp, threshold_lsn);
724fd074841SDave Chinner }
725fd074841SDave Chinner 
726fd074841SDave Chinner /*
727211e4d43SChristoph Hellwig  * Push out all items in the AIL immediately and wait until the AIL is empty.
728211e4d43SChristoph Hellwig  */
729211e4d43SChristoph Hellwig void
730211e4d43SChristoph Hellwig xfs_ail_push_all_sync(
731211e4d43SChristoph Hellwig 	struct xfs_ail  *ailp)
732211e4d43SChristoph Hellwig {
733211e4d43SChristoph Hellwig 	struct xfs_log_item	*lip;
734211e4d43SChristoph Hellwig 	DEFINE_WAIT(wait);
735211e4d43SChristoph Hellwig 
73657e80956SMatthew Wilcox 	spin_lock(&ailp->ail_lock);
737211e4d43SChristoph Hellwig 	while ((lip = xfs_ail_max(ailp)) != NULL) {
73857e80956SMatthew Wilcox 		prepare_to_wait(&ailp->ail_empty, &wait, TASK_UNINTERRUPTIBLE);
73957e80956SMatthew Wilcox 		wake_up_process(ailp->ail_task);
74057e80956SMatthew Wilcox 		spin_unlock(&ailp->ail_lock);
741211e4d43SChristoph Hellwig 		schedule();
74257e80956SMatthew Wilcox 		spin_lock(&ailp->ail_lock);
743211e4d43SChristoph Hellwig 	}
74457e80956SMatthew Wilcox 	spin_unlock(&ailp->ail_lock);
745211e4d43SChristoph Hellwig 
74657e80956SMatthew Wilcox 	finish_wait(&ailp->ail_empty, &wait);
747211e4d43SChristoph Hellwig }
748211e4d43SChristoph Hellwig 
7494165994aSDave Chinner void
7504165994aSDave Chinner xfs_ail_update_finish(
7514165994aSDave Chinner 	struct xfs_ail		*ailp,
7528eb807bdSDave Chinner 	xfs_lsn_t		old_lsn) __releases(ailp->ail_lock)
7534165994aSDave Chinner {
7548eda8721SDave Chinner 	struct xlog		*log = ailp->ail_log;
7554165994aSDave Chinner 
7568eb807bdSDave Chinner 	/* if the tail lsn hasn't changed, don't do updates or wakeups. */
7578eb807bdSDave Chinner 	if (!old_lsn || old_lsn == __xfs_ail_min_lsn(ailp)) {
7584165994aSDave Chinner 		spin_unlock(&ailp->ail_lock);
7594165994aSDave Chinner 		return;
7604165994aSDave Chinner 	}
7614165994aSDave Chinner 
7628eda8721SDave Chinner 	if (!xlog_is_shutdown(log))
7638eda8721SDave Chinner 		xlog_assign_tail_lsn_locked(log->l_mp);
7644165994aSDave Chinner 
7654165994aSDave Chinner 	if (list_empty(&ailp->ail_head))
7664165994aSDave Chinner 		wake_up_all(&ailp->ail_empty);
7674165994aSDave Chinner 	spin_unlock(&ailp->ail_lock);
7688eda8721SDave Chinner 	xfs_log_space_wake(log->l_mp);
7694165994aSDave Chinner }
7704165994aSDave Chinner 
771211e4d43SChristoph Hellwig /*
7720e57f6a3SDave Chinner  * xfs_trans_ail_update - bulk AIL insertion operation.
7730e57f6a3SDave Chinner  *
7740e57f6a3SDave Chinner  * @xfs_trans_ail_update takes an array of log items that all need to be
7750e57f6a3SDave Chinner  * positioned at the same LSN in the AIL. If an item is not in the AIL, it will
7760e57f6a3SDave Chinner  * be added.  Otherwise, it will be repositioned  by removing it and re-adding
7770e57f6a3SDave Chinner  * it to the AIL. If we move the first item in the AIL, update the log tail to
7780e57f6a3SDave Chinner  * match the new minimum LSN in the AIL.
7790e57f6a3SDave Chinner  *
7800e57f6a3SDave Chinner  * This function takes the AIL lock once to execute the update operations on
7810e57f6a3SDave Chinner  * all the items in the array, and as such should not be called with the AIL
7820e57f6a3SDave Chinner  * lock held. As a result, once we have the AIL lock, we need to check each log
7830e57f6a3SDave Chinner  * item LSN to confirm it needs to be moved forward in the AIL.
7840e57f6a3SDave Chinner  *
7850e57f6a3SDave Chinner  * To optimise the insert operation, we delete all the items from the AIL in
7860e57f6a3SDave Chinner  * the first pass, moving them into a temporary list, then splice the temporary
7870e57f6a3SDave Chinner  * list into the correct position in the AIL. This avoids needing to do an
7880e57f6a3SDave Chinner  * insert operation on every item.
7890e57f6a3SDave Chinner  *
7900e57f6a3SDave Chinner  * This function must be called with the AIL lock held.  The lock is dropped
7910e57f6a3SDave Chinner  * before returning.
7920e57f6a3SDave Chinner  */
7930e57f6a3SDave Chinner void
7940e57f6a3SDave Chinner xfs_trans_ail_update_bulk(
7950e57f6a3SDave Chinner 	struct xfs_ail		*ailp,
7961d8c95a3SDave Chinner 	struct xfs_ail_cursor	*cur,
7970e57f6a3SDave Chinner 	struct xfs_log_item	**log_items,
7980e57f6a3SDave Chinner 	int			nr_items,
79957e80956SMatthew Wilcox 	xfs_lsn_t		lsn) __releases(ailp->ail_lock)
8000e57f6a3SDave Chinner {
801efe2330fSChristoph Hellwig 	struct xfs_log_item	*mlip;
8028eb807bdSDave Chinner 	xfs_lsn_t		tail_lsn = 0;
8030e57f6a3SDave Chinner 	int			i;
8040e57f6a3SDave Chinner 	LIST_HEAD(tmp);
8050e57f6a3SDave Chinner 
806e44f4112SAlex Elder 	ASSERT(nr_items > 0);		/* Not required, but true. */
8070e57f6a3SDave Chinner 	mlip = xfs_ail_min(ailp);
8080e57f6a3SDave Chinner 
8090e57f6a3SDave Chinner 	for (i = 0; i < nr_items; i++) {
8100e57f6a3SDave Chinner 		struct xfs_log_item *lip = log_items[i];
81122525c17SDave Chinner 		if (test_and_set_bit(XFS_LI_IN_AIL, &lip->li_flags)) {
8120e57f6a3SDave Chinner 			/* check if we really need to move the item */
8130e57f6a3SDave Chinner 			if (XFS_LSN_CMP(lsn, lip->li_lsn) <= 0)
8140e57f6a3SDave Chinner 				continue;
8150e57f6a3SDave Chinner 
816750b9c90SDave Chinner 			trace_xfs_ail_move(lip, lip->li_lsn, lsn);
8178eb807bdSDave Chinner 			if (mlip == lip && !tail_lsn)
8188eb807bdSDave Chinner 				tail_lsn = lip->li_lsn;
8198eb807bdSDave Chinner 
8200e57f6a3SDave Chinner 			xfs_ail_delete(ailp, lip);
8210e57f6a3SDave Chinner 		} else {
822750b9c90SDave Chinner 			trace_xfs_ail_insert(lip, 0, lsn);
8230e57f6a3SDave Chinner 		}
8240e57f6a3SDave Chinner 		lip->li_lsn = lsn;
8250e57f6a3SDave Chinner 		list_add(&lip->li_ail, &tmp);
8260e57f6a3SDave Chinner 	}
8270e57f6a3SDave Chinner 
828e44f4112SAlex Elder 	if (!list_empty(&tmp))
8291d8c95a3SDave Chinner 		xfs_ail_splice(ailp, cur, &tmp, lsn);
8301c304625SChristoph Hellwig 
8318eb807bdSDave Chinner 	xfs_ail_update_finish(ailp, tail_lsn);
8320e57f6a3SDave Chinner }
8330e57f6a3SDave Chinner 
83486a37174SDarrick J. Wong /* Insert a log item into the AIL. */
83586a37174SDarrick J. Wong void
83686a37174SDarrick J. Wong xfs_trans_ail_insert(
83786a37174SDarrick J. Wong 	struct xfs_ail		*ailp,
83886a37174SDarrick J. Wong 	struct xfs_log_item	*lip,
83986a37174SDarrick J. Wong 	xfs_lsn_t		lsn)
84086a37174SDarrick J. Wong {
84186a37174SDarrick J. Wong 	spin_lock(&ailp->ail_lock);
84286a37174SDarrick J. Wong 	xfs_trans_ail_update_bulk(ailp, NULL, &lip, 1, lsn);
84386a37174SDarrick J. Wong }
84486a37174SDarrick J. Wong 
8458eb807bdSDave Chinner /*
8468eb807bdSDave Chinner  * Delete one log item from the AIL.
8478eb807bdSDave Chinner  *
8488eb807bdSDave Chinner  * If this item was at the tail of the AIL, return the LSN of the log item so
8498eb807bdSDave Chinner  * that we can use it to check if the LSN of the tail of the log has moved
8508eb807bdSDave Chinner  * when finishing up the AIL delete process in xfs_ail_update_finish().
8518eb807bdSDave Chinner  */
8528eb807bdSDave Chinner xfs_lsn_t
85327af1bbfSChristoph Hellwig xfs_ail_delete_one(
85427af1bbfSChristoph Hellwig 	struct xfs_ail		*ailp,
85527af1bbfSChristoph Hellwig 	struct xfs_log_item	*lip)
85627af1bbfSChristoph Hellwig {
85727af1bbfSChristoph Hellwig 	struct xfs_log_item	*mlip = xfs_ail_min(ailp);
8588eb807bdSDave Chinner 	xfs_lsn_t		lsn = lip->li_lsn;
85927af1bbfSChristoph Hellwig 
86027af1bbfSChristoph Hellwig 	trace_xfs_ail_delete(lip, mlip->li_lsn, lip->li_lsn);
86127af1bbfSChristoph Hellwig 	xfs_ail_delete(ailp, lip);
86222525c17SDave Chinner 	clear_bit(XFS_LI_IN_AIL, &lip->li_flags);
86327af1bbfSChristoph Hellwig 	lip->li_lsn = 0;
86427af1bbfSChristoph Hellwig 
8658eb807bdSDave Chinner 	if (mlip == lip)
8668eb807bdSDave Chinner 		return lsn;
8678eb807bdSDave Chinner 	return 0;
86827af1bbfSChristoph Hellwig }
86927af1bbfSChristoph Hellwig 
87030136832SDave Chinner void
87127af1bbfSChristoph Hellwig xfs_trans_ail_delete(
87227af1bbfSChristoph Hellwig 	struct xfs_log_item	*lip,
8734165994aSDave Chinner 	int			shutdown_type)
87430136832SDave Chinner {
875849274c1SBrian Foster 	struct xfs_ail		*ailp = lip->li_ailp;
876b5f17becSDave Chinner 	struct xlog		*log = ailp->ail_log;
8778eb807bdSDave Chinner 	xfs_lsn_t		tail_lsn;
87830136832SDave Chinner 
879849274c1SBrian Foster 	spin_lock(&ailp->ail_lock);
88022525c17SDave Chinner 	if (!test_bit(XFS_LI_IN_AIL, &lip->li_flags)) {
88157e80956SMatthew Wilcox 		spin_unlock(&ailp->ail_lock);
882b5f17becSDave Chinner 		if (shutdown_type && !xlog_is_shutdown(log)) {
883b5f17becSDave Chinner 			xfs_alert_tag(log->l_mp, XFS_PTAG_AILDELETE,
88430136832SDave Chinner 	"%s: attempting to delete a log item that is not in the AIL",
88530136832SDave Chinner 					__func__);
886b5f17becSDave Chinner 			xlog_force_shutdown(log, shutdown_type);
88730136832SDave Chinner 		}
88830136832SDave Chinner 		return;
88930136832SDave Chinner 	}
89030136832SDave Chinner 
8912b3cf093SBrian Foster 	/* xfs_ail_update_finish() drops the AIL lock */
892e98084b8SDave Chinner 	xfs_clear_li_failed(lip);
8938eb807bdSDave Chinner 	tail_lsn = xfs_ail_delete_one(ailp, lip);
8948eb807bdSDave Chinner 	xfs_ail_update_finish(ailp, tail_lsn);
89530136832SDave Chinner }
8961da177e4SLinus Torvalds 
897249a8c11SDavid Chinner int
8981da177e4SLinus Torvalds xfs_trans_ail_init(
8991da177e4SLinus Torvalds 	xfs_mount_t	*mp)
9001da177e4SLinus Torvalds {
90182fa9012SDavid Chinner 	struct xfs_ail	*ailp;
90282fa9012SDavid Chinner 
90382fa9012SDavid Chinner 	ailp = kmem_zalloc(sizeof(struct xfs_ail), KM_MAYFAIL);
90482fa9012SDavid Chinner 	if (!ailp)
9052451337dSDave Chinner 		return -ENOMEM;
90682fa9012SDavid Chinner 
9078eda8721SDave Chinner 	ailp->ail_log = mp->m_log;
90857e80956SMatthew Wilcox 	INIT_LIST_HEAD(&ailp->ail_head);
90957e80956SMatthew Wilcox 	INIT_LIST_HEAD(&ailp->ail_cursors);
91057e80956SMatthew Wilcox 	spin_lock_init(&ailp->ail_lock);
91157e80956SMatthew Wilcox 	INIT_LIST_HEAD(&ailp->ail_buf_list);
91257e80956SMatthew Wilcox 	init_waitqueue_head(&ailp->ail_empty);
9130030807cSChristoph Hellwig 
91457e80956SMatthew Wilcox 	ailp->ail_task = kthread_run(xfsaild, ailp, "xfsaild/%s",
9158eda8721SDave Chinner 				mp->m_super->s_id);
91657e80956SMatthew Wilcox 	if (IS_ERR(ailp->ail_task))
9170030807cSChristoph Hellwig 		goto out_free_ailp;
9180030807cSChristoph Hellwig 
91927d8d5feSDavid Chinner 	mp->m_ail = ailp;
92027d8d5feSDavid Chinner 	return 0;
9210030807cSChristoph Hellwig 
9220030807cSChristoph Hellwig out_free_ailp:
9230030807cSChristoph Hellwig 	kmem_free(ailp);
9242451337dSDave Chinner 	return -ENOMEM;
925249a8c11SDavid Chinner }
926249a8c11SDavid Chinner 
927249a8c11SDavid Chinner void
928249a8c11SDavid Chinner xfs_trans_ail_destroy(
929249a8c11SDavid Chinner 	xfs_mount_t	*mp)
930249a8c11SDavid Chinner {
93182fa9012SDavid Chinner 	struct xfs_ail	*ailp = mp->m_ail;
93282fa9012SDavid Chinner 
93357e80956SMatthew Wilcox 	kthread_stop(ailp->ail_task);
93482fa9012SDavid Chinner 	kmem_free(ailp);
9351da177e4SLinus Torvalds }
936