xref: /openbmc/linux/fs/xfs/xfs_trans_ail.c (revision d686d12d)
11da177e4SLinus Torvalds /*
27b718769SNathan Scott  * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3c7e8f268SDavid Chinner  * Copyright (c) 2008 Dave Chinner
47b718769SNathan Scott  * All Rights Reserved.
51da177e4SLinus Torvalds  *
67b718769SNathan Scott  * This program is free software; you can redistribute it and/or
77b718769SNathan Scott  * modify it under the terms of the GNU General Public License as
81da177e4SLinus Torvalds  * published by the Free Software Foundation.
91da177e4SLinus Torvalds  *
107b718769SNathan Scott  * This program is distributed in the hope that it would be useful,
117b718769SNathan Scott  * but WITHOUT ANY WARRANTY; without even the implied warranty of
127b718769SNathan Scott  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
137b718769SNathan Scott  * GNU General Public License for more details.
141da177e4SLinus Torvalds  *
157b718769SNathan Scott  * You should have received a copy of the GNU General Public License
167b718769SNathan Scott  * along with this program; if not, write the Free Software Foundation,
177b718769SNathan Scott  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
181da177e4SLinus Torvalds  */
191da177e4SLinus Torvalds #include "xfs.h"
20a844f451SNathan Scott #include "xfs_fs.h"
214fb6e8adSChristoph Hellwig #include "xfs_format.h"
22239880efSDave Chinner #include "xfs_log_format.h"
23239880efSDave Chinner #include "xfs_trans_resv.h"
241da177e4SLinus Torvalds #include "xfs_mount.h"
25239880efSDave Chinner #include "xfs_trans.h"
261da177e4SLinus Torvalds #include "xfs_trans_priv.h"
279e4c109aSChristoph Hellwig #include "xfs_trace.h"
28e9e899a2SDarrick J. Wong #include "xfs_errortag.h"
291da177e4SLinus Torvalds #include "xfs_error.h"
30239880efSDave Chinner #include "xfs_log.h"
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds #ifdef DEBUG
33cd4a3c50SDave Chinner /*
34cd4a3c50SDave Chinner  * Check that the list is sorted as it should be.
35d686d12dSDave Chinner  *
36d686d12dSDave Chinner  * Called with the ail lock held, but we don't want to assert fail with it
37d686d12dSDave Chinner  * held otherwise we'll lock everything up and won't be able to debug the
38d686d12dSDave Chinner  * cause. Hence we sample and check the state under the AIL lock and return if
39d686d12dSDave Chinner  * everything is fine, otherwise we drop the lock and run the ASSERT checks.
40d686d12dSDave Chinner  * Asserts may not be fatal, so pick the lock back up and continue onwards.
41cd4a3c50SDave Chinner  */
42cd4a3c50SDave Chinner STATIC void
43cd4a3c50SDave Chinner xfs_ail_check(
44cd4a3c50SDave Chinner 	struct xfs_ail		*ailp,
45d686d12dSDave Chinner 	struct xfs_log_item	*lip)
46cd4a3c50SDave Chinner {
47d686d12dSDave Chinner 	struct xfs_log_item	*prev_lip;
48d686d12dSDave Chinner 	struct xfs_log_item	*next_lip;
49d686d12dSDave Chinner 	xfs_lsn_t		prev_lsn = NULLCOMMITLSN;
50d686d12dSDave Chinner 	xfs_lsn_t		next_lsn = NULLCOMMITLSN;
51d686d12dSDave Chinner 	xfs_lsn_t		lsn;
52d686d12dSDave Chinner 	bool			in_ail;
53d686d12dSDave Chinner 
54cd4a3c50SDave Chinner 
5557e80956SMatthew Wilcox 	if (list_empty(&ailp->ail_head))
56cd4a3c50SDave Chinner 		return;
57cd4a3c50SDave Chinner 
58cd4a3c50SDave Chinner 	/*
59d686d12dSDave Chinner 	 * Sample then check the next and previous entries are valid.
60cd4a3c50SDave Chinner 	 */
61d686d12dSDave Chinner 	in_ail = test_bit(XFS_LI_IN_AIL, &lip->li_flags);
62d686d12dSDave Chinner 	prev_lip = list_entry(lip->li_ail.prev, struct xfs_log_item, li_ail);
6357e80956SMatthew Wilcox 	if (&prev_lip->li_ail != &ailp->ail_head)
64d686d12dSDave Chinner 		prev_lsn = prev_lip->li_lsn;
65d686d12dSDave Chinner 	next_lip = list_entry(lip->li_ail.next, struct xfs_log_item, li_ail);
66d686d12dSDave Chinner 	if (&next_lip->li_ail != &ailp->ail_head)
67d686d12dSDave Chinner 		next_lsn = next_lip->li_lsn;
68d686d12dSDave Chinner 	lsn = lip->li_lsn;
69cd4a3c50SDave Chinner 
70d686d12dSDave Chinner 	if (in_ail &&
71d686d12dSDave Chinner 	    (prev_lsn == NULLCOMMITLSN || XFS_LSN_CMP(prev_lsn, lsn) <= 0) &&
72d686d12dSDave Chinner 	    (next_lsn == NULLCOMMITLSN || XFS_LSN_CMP(next_lsn, lsn) >= 0))
73d686d12dSDave Chinner 		return;
74cd4a3c50SDave Chinner 
75d686d12dSDave Chinner 	spin_unlock(&ailp->ail_lock);
76d686d12dSDave Chinner 	ASSERT(in_ail);
77d686d12dSDave Chinner 	ASSERT(prev_lsn == NULLCOMMITLSN || XFS_LSN_CMP(prev_lsn, lsn) <= 0);
78d686d12dSDave Chinner 	ASSERT(next_lsn == NULLCOMMITLSN || XFS_LSN_CMP(next_lsn, lsn) >= 0);
79d686d12dSDave Chinner 	spin_lock(&ailp->ail_lock);
80cd4a3c50SDave Chinner }
81cd4a3c50SDave Chinner #else /* !DEBUG */
82de08dbc1SDavid Chinner #define	xfs_ail_check(a,l)
831da177e4SLinus Torvalds #endif /* DEBUG */
841da177e4SLinus Torvalds 
85cd4a3c50SDave Chinner /*
86fd074841SDave Chinner  * Return a pointer to the last item in the AIL.  If the AIL is empty, then
87fd074841SDave Chinner  * return NULL.
88fd074841SDave Chinner  */
89fd074841SDave Chinner static xfs_log_item_t *
90fd074841SDave Chinner xfs_ail_max(
91fd074841SDave Chinner 	struct xfs_ail  *ailp)
92fd074841SDave Chinner {
9357e80956SMatthew Wilcox 	if (list_empty(&ailp->ail_head))
94fd074841SDave Chinner 		return NULL;
95fd074841SDave Chinner 
9657e80956SMatthew Wilcox 	return list_entry(ailp->ail_head.prev, xfs_log_item_t, li_ail);
97fd074841SDave Chinner }
98fd074841SDave Chinner 
99fd074841SDave Chinner /*
100cd4a3c50SDave Chinner  * Return a pointer to the item which follows the given item in the AIL.  If
101cd4a3c50SDave Chinner  * the given item is the last item in the list, then return NULL.
102cd4a3c50SDave Chinner  */
103cd4a3c50SDave Chinner static xfs_log_item_t *
104cd4a3c50SDave Chinner xfs_ail_next(
105cd4a3c50SDave Chinner 	struct xfs_ail  *ailp,
106cd4a3c50SDave Chinner 	xfs_log_item_t  *lip)
107cd4a3c50SDave Chinner {
10857e80956SMatthew Wilcox 	if (lip->li_ail.next == &ailp->ail_head)
109cd4a3c50SDave Chinner 		return NULL;
110cd4a3c50SDave Chinner 
111cd4a3c50SDave Chinner 	return list_first_entry(&lip->li_ail, xfs_log_item_t, li_ail);
112cd4a3c50SDave Chinner }
113cd4a3c50SDave Chinner 
114cd4a3c50SDave Chinner /*
115cd4a3c50SDave Chinner  * This is called by the log manager code to determine the LSN of the tail of
116cd4a3c50SDave Chinner  * the log.  This is exactly the LSN of the first item in the AIL.  If the AIL
117cd4a3c50SDave Chinner  * is empty, then this function returns 0.
1181da177e4SLinus Torvalds  *
119cd4a3c50SDave Chinner  * We need the AIL lock in order to get a coherent read of the lsn of the last
120cd4a3c50SDave Chinner  * item in the AIL.
1211da177e4SLinus Torvalds  */
1221da177e4SLinus Torvalds xfs_lsn_t
123fd074841SDave Chinner xfs_ail_min_lsn(
1245b00f14fSDavid Chinner 	struct xfs_ail	*ailp)
1251da177e4SLinus Torvalds {
126cd4a3c50SDave Chinner 	xfs_lsn_t	lsn = 0;
1271da177e4SLinus Torvalds 	xfs_log_item_t	*lip;
1281da177e4SLinus Torvalds 
12957e80956SMatthew Wilcox 	spin_lock(&ailp->ail_lock);
1305b00f14fSDavid Chinner 	lip = xfs_ail_min(ailp);
131cd4a3c50SDave Chinner 	if (lip)
1321da177e4SLinus Torvalds 		lsn = lip->li_lsn;
13357e80956SMatthew Wilcox 	spin_unlock(&ailp->ail_lock);
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds 	return lsn;
1361da177e4SLinus Torvalds }
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds /*
139fd074841SDave Chinner  * Return the maximum lsn held in the AIL, or zero if the AIL is empty.
140fd074841SDave Chinner  */
141fd074841SDave Chinner static xfs_lsn_t
142fd074841SDave Chinner xfs_ail_max_lsn(
143fd074841SDave Chinner 	struct xfs_ail  *ailp)
144fd074841SDave Chinner {
145fd074841SDave Chinner 	xfs_lsn_t       lsn = 0;
146fd074841SDave Chinner 	xfs_log_item_t  *lip;
147fd074841SDave Chinner 
14857e80956SMatthew Wilcox 	spin_lock(&ailp->ail_lock);
149fd074841SDave Chinner 	lip = xfs_ail_max(ailp);
150fd074841SDave Chinner 	if (lip)
151fd074841SDave Chinner 		lsn = lip->li_lsn;
15257e80956SMatthew Wilcox 	spin_unlock(&ailp->ail_lock);
153fd074841SDave Chinner 
154fd074841SDave Chinner 	return lsn;
155fd074841SDave Chinner }
156fd074841SDave Chinner 
157fd074841SDave Chinner /*
158af3e4022SDave Chinner  * The cursor keeps track of where our current traversal is up to by tracking
159af3e4022SDave Chinner  * the next item in the list for us. However, for this to be safe, removing an
160af3e4022SDave Chinner  * object from the AIL needs to invalidate any cursor that points to it. hence
161af3e4022SDave Chinner  * the traversal cursor needs to be linked to the struct xfs_ail so that
162af3e4022SDave Chinner  * deletion can search all the active cursors for invalidation.
16327d8d5feSDavid Chinner  */
1645b00f14fSDavid Chinner STATIC void
16527d8d5feSDavid Chinner xfs_trans_ail_cursor_init(
16627d8d5feSDavid Chinner 	struct xfs_ail		*ailp,
16727d8d5feSDavid Chinner 	struct xfs_ail_cursor	*cur)
16827d8d5feSDavid Chinner {
16927d8d5feSDavid Chinner 	cur->item = NULL;
17057e80956SMatthew Wilcox 	list_add_tail(&cur->list, &ailp->ail_cursors);
17127d8d5feSDavid Chinner }
17227d8d5feSDavid Chinner 
17327d8d5feSDavid Chinner /*
174af3e4022SDave Chinner  * Get the next item in the traversal and advance the cursor.  If the cursor
175af3e4022SDave Chinner  * was invalidated (indicated by a lip of 1), restart the traversal.
17627d8d5feSDavid Chinner  */
1775b00f14fSDavid Chinner struct xfs_log_item *
17827d8d5feSDavid Chinner xfs_trans_ail_cursor_next(
17927d8d5feSDavid Chinner 	struct xfs_ail		*ailp,
18027d8d5feSDavid Chinner 	struct xfs_ail_cursor	*cur)
18127d8d5feSDavid Chinner {
18227d8d5feSDavid Chinner 	struct xfs_log_item	*lip = cur->item;
18327d8d5feSDavid Chinner 
184db9d67d6SChristoph Hellwig 	if ((uintptr_t)lip & 1)
18527d8d5feSDavid Chinner 		lip = xfs_ail_min(ailp);
18616b59029SDave Chinner 	if (lip)
18716b59029SDave Chinner 		cur->item = xfs_ail_next(ailp, lip);
18827d8d5feSDavid Chinner 	return lip;
18927d8d5feSDavid Chinner }
19027d8d5feSDavid Chinner 
19127d8d5feSDavid Chinner /*
192af3e4022SDave Chinner  * When the traversal is complete, we need to remove the cursor from the list
193af3e4022SDave Chinner  * of traversing cursors.
19427d8d5feSDavid Chinner  */
19527d8d5feSDavid Chinner void
19627d8d5feSDavid Chinner xfs_trans_ail_cursor_done(
197af3e4022SDave Chinner 	struct xfs_ail_cursor	*cur)
19827d8d5feSDavid Chinner {
199af3e4022SDave Chinner 	cur->item = NULL;
200af3e4022SDave Chinner 	list_del_init(&cur->list);
20127d8d5feSDavid Chinner }
20227d8d5feSDavid Chinner 
20327d8d5feSDavid Chinner /*
204af3e4022SDave Chinner  * Invalidate any cursor that is pointing to this item. This is called when an
205af3e4022SDave Chinner  * item is removed from the AIL. Any cursor pointing to this object is now
206af3e4022SDave Chinner  * invalid and the traversal needs to be terminated so it doesn't reference a
207af3e4022SDave Chinner  * freed object. We set the low bit of the cursor item pointer so we can
208af3e4022SDave Chinner  * distinguish between an invalidation and the end of the list when getting the
209af3e4022SDave Chinner  * next item from the cursor.
2105b00f14fSDavid Chinner  */
2115b00f14fSDavid Chinner STATIC void
2125b00f14fSDavid Chinner xfs_trans_ail_cursor_clear(
2135b00f14fSDavid Chinner 	struct xfs_ail		*ailp,
2145b00f14fSDavid Chinner 	struct xfs_log_item	*lip)
2155b00f14fSDavid Chinner {
2165b00f14fSDavid Chinner 	struct xfs_ail_cursor	*cur;
2175b00f14fSDavid Chinner 
21857e80956SMatthew Wilcox 	list_for_each_entry(cur, &ailp->ail_cursors, list) {
2195b00f14fSDavid Chinner 		if (cur->item == lip)
2205b00f14fSDavid Chinner 			cur->item = (struct xfs_log_item *)
221db9d67d6SChristoph Hellwig 					((uintptr_t)cur->item | 1);
2225b00f14fSDavid Chinner 	}
2235b00f14fSDavid Chinner }
2245b00f14fSDavid Chinner 
2255b00f14fSDavid Chinner /*
22616b59029SDave Chinner  * Find the first item in the AIL with the given @lsn by searching in ascending
22716b59029SDave Chinner  * LSN order and initialise the cursor to point to the next item for a
22816b59029SDave Chinner  * ascending traversal.  Pass a @lsn of zero to initialise the cursor to the
22916b59029SDave Chinner  * first item in the AIL. Returns NULL if the list is empty.
230249a8c11SDavid Chinner  */
2315b00f14fSDavid Chinner xfs_log_item_t *
2325b00f14fSDavid Chinner xfs_trans_ail_cursor_first(
23327d8d5feSDavid Chinner 	struct xfs_ail		*ailp,
23427d8d5feSDavid Chinner 	struct xfs_ail_cursor	*cur,
235249a8c11SDavid Chinner 	xfs_lsn_t		lsn)
236249a8c11SDavid Chinner {
237249a8c11SDavid Chinner 	xfs_log_item_t		*lip;
238249a8c11SDavid Chinner 
2395b00f14fSDavid Chinner 	xfs_trans_ail_cursor_init(ailp, cur);
24016b59029SDave Chinner 
24116b59029SDave Chinner 	if (lsn == 0) {
24227d8d5feSDavid Chinner 		lip = xfs_ail_min(ailp);
2435b00f14fSDavid Chinner 		goto out;
24416b59029SDave Chinner 	}
245249a8c11SDavid Chinner 
24657e80956SMatthew Wilcox 	list_for_each_entry(lip, &ailp->ail_head, li_ail) {
2475b00f14fSDavid Chinner 		if (XFS_LSN_CMP(lip->li_lsn, lsn) >= 0)
2487ee49acfSDavid Chinner 			goto out;
2495b00f14fSDavid Chinner 	}
25016b59029SDave Chinner 	return NULL;
25116b59029SDave Chinner 
2525b00f14fSDavid Chinner out:
25316b59029SDave Chinner 	if (lip)
25416b59029SDave Chinner 		cur->item = xfs_ail_next(ailp, lip);
255249a8c11SDavid Chinner 	return lip;
256249a8c11SDavid Chinner }
257535f6b37SJosef 'Jeff' Sipek 
2581d8c95a3SDave Chinner static struct xfs_log_item *
2591d8c95a3SDave Chinner __xfs_trans_ail_cursor_last(
2601d8c95a3SDave Chinner 	struct xfs_ail		*ailp,
2611d8c95a3SDave Chinner 	xfs_lsn_t		lsn)
2621d8c95a3SDave Chinner {
2631d8c95a3SDave Chinner 	xfs_log_item_t		*lip;
2641d8c95a3SDave Chinner 
26557e80956SMatthew Wilcox 	list_for_each_entry_reverse(lip, &ailp->ail_head, li_ail) {
2661d8c95a3SDave Chinner 		if (XFS_LSN_CMP(lip->li_lsn, lsn) <= 0)
2671d8c95a3SDave Chinner 			return lip;
2681d8c95a3SDave Chinner 	}
2691d8c95a3SDave Chinner 	return NULL;
2701d8c95a3SDave Chinner }
2711d8c95a3SDave Chinner 
2721d8c95a3SDave Chinner /*
27316b59029SDave Chinner  * Find the last item in the AIL with the given @lsn by searching in descending
27416b59029SDave Chinner  * LSN order and initialise the cursor to point to that item.  If there is no
27516b59029SDave Chinner  * item with the value of @lsn, then it sets the cursor to the last item with an
27616b59029SDave Chinner  * LSN lower than @lsn.  Returns NULL if the list is empty.
2771d8c95a3SDave Chinner  */
2781d8c95a3SDave Chinner struct xfs_log_item *
2791d8c95a3SDave Chinner xfs_trans_ail_cursor_last(
2801d8c95a3SDave Chinner 	struct xfs_ail		*ailp,
2811d8c95a3SDave Chinner 	struct xfs_ail_cursor	*cur,
2821d8c95a3SDave Chinner 	xfs_lsn_t		lsn)
2831d8c95a3SDave Chinner {
2841d8c95a3SDave Chinner 	xfs_trans_ail_cursor_init(ailp, cur);
2851d8c95a3SDave Chinner 	cur->item = __xfs_trans_ail_cursor_last(ailp, lsn);
2861d8c95a3SDave Chinner 	return cur->item;
2871d8c95a3SDave Chinner }
2881d8c95a3SDave Chinner 
2891d8c95a3SDave Chinner /*
29016b59029SDave Chinner  * Splice the log item list into the AIL at the given LSN. We splice to the
2911d8c95a3SDave Chinner  * tail of the given LSN to maintain insert order for push traversals. The
2921d8c95a3SDave Chinner  * cursor is optional, allowing repeated updates to the same LSN to avoid
293e44f4112SAlex Elder  * repeated traversals.  This should not be called with an empty list.
294cd4a3c50SDave Chinner  */
295cd4a3c50SDave Chinner static void
296cd4a3c50SDave Chinner xfs_ail_splice(
297cd4a3c50SDave Chinner 	struct xfs_ail		*ailp,
2981d8c95a3SDave Chinner 	struct xfs_ail_cursor	*cur,
299cd4a3c50SDave Chinner 	struct list_head	*list,
300cd4a3c50SDave Chinner 	xfs_lsn_t		lsn)
301cd4a3c50SDave Chinner {
302e44f4112SAlex Elder 	struct xfs_log_item	*lip;
303e44f4112SAlex Elder 
304e44f4112SAlex Elder 	ASSERT(!list_empty(list));
305cd4a3c50SDave Chinner 
3061d8c95a3SDave Chinner 	/*
307e44f4112SAlex Elder 	 * Use the cursor to determine the insertion point if one is
308e44f4112SAlex Elder 	 * provided.  If not, or if the one we got is not valid,
309e44f4112SAlex Elder 	 * find the place in the AIL where the items belong.
3101d8c95a3SDave Chinner 	 */
311e44f4112SAlex Elder 	lip = cur ? cur->item : NULL;
312db9d67d6SChristoph Hellwig 	if (!lip || (uintptr_t)lip & 1)
3131d8c95a3SDave Chinner 		lip = __xfs_trans_ail_cursor_last(ailp, lsn);
3141d8c95a3SDave Chinner 
315e44f4112SAlex Elder 	/*
316e44f4112SAlex Elder 	 * If a cursor is provided, we know we're processing the AIL
317e44f4112SAlex Elder 	 * in lsn order, and future items to be spliced in will
318e44f4112SAlex Elder 	 * follow the last one being inserted now.  Update the
319e44f4112SAlex Elder 	 * cursor to point to that last item, now while we have a
320e44f4112SAlex Elder 	 * reliable pointer to it.
321e44f4112SAlex Elder 	 */
3221d8c95a3SDave Chinner 	if (cur)
323e44f4112SAlex Elder 		cur->item = list_entry(list->prev, struct xfs_log_item, li_ail);
324cd4a3c50SDave Chinner 
3251d8c95a3SDave Chinner 	/*
326e44f4112SAlex Elder 	 * Finally perform the splice.  Unless the AIL was empty,
327e44f4112SAlex Elder 	 * lip points to the item in the AIL _after_ which the new
328e44f4112SAlex Elder 	 * items should go.  If lip is null the AIL was empty, so
329e44f4112SAlex Elder 	 * the new items go at the head of the AIL.
3301d8c95a3SDave Chinner 	 */
331e44f4112SAlex Elder 	if (lip)
3321d8c95a3SDave Chinner 		list_splice(list, &lip->li_ail);
333e44f4112SAlex Elder 	else
33457e80956SMatthew Wilcox 		list_splice(list, &ailp->ail_head);
335cd4a3c50SDave Chinner }
336cd4a3c50SDave Chinner 
337cd4a3c50SDave Chinner /*
338cd4a3c50SDave Chinner  * Delete the given item from the AIL.  Return a pointer to the item.
339cd4a3c50SDave Chinner  */
340cd4a3c50SDave Chinner static void
341cd4a3c50SDave Chinner xfs_ail_delete(
342cd4a3c50SDave Chinner 	struct xfs_ail  *ailp,
343cd4a3c50SDave Chinner 	xfs_log_item_t  *lip)
344cd4a3c50SDave Chinner {
345cd4a3c50SDave Chinner 	xfs_ail_check(ailp, lip);
346cd4a3c50SDave Chinner 	list_del(&lip->li_ail);
347cd4a3c50SDave Chinner 	xfs_trans_ail_cursor_clear(ailp, lip);
348cd4a3c50SDave Chinner }
349cd4a3c50SDave Chinner 
3507f4d01f3SBrian Foster static inline uint
3517f4d01f3SBrian Foster xfsaild_push_item(
3527f4d01f3SBrian Foster 	struct xfs_ail		*ailp,
3537f4d01f3SBrian Foster 	struct xfs_log_item	*lip)
3547f4d01f3SBrian Foster {
3557f4d01f3SBrian Foster 	/*
3567f4d01f3SBrian Foster 	 * If log item pinning is enabled, skip the push and track the item as
3577f4d01f3SBrian Foster 	 * pinned. This can help induce head-behind-tail conditions.
3587f4d01f3SBrian Foster 	 */
35957e80956SMatthew Wilcox 	if (XFS_TEST_ERROR(false, ailp->ail_mount, XFS_ERRTAG_LOG_ITEM_PIN))
3607f4d01f3SBrian Foster 		return XFS_ITEM_PINNED;
3617f4d01f3SBrian Foster 
36257e80956SMatthew Wilcox 	return lip->li_ops->iop_push(lip, &ailp->ail_buf_list);
3637f4d01f3SBrian Foster }
3647f4d01f3SBrian Foster 
3650030807cSChristoph Hellwig static long
3660030807cSChristoph Hellwig xfsaild_push(
3670030807cSChristoph Hellwig 	struct xfs_ail		*ailp)
368249a8c11SDavid Chinner {
36957e80956SMatthew Wilcox 	xfs_mount_t		*mp = ailp->ail_mount;
370af3e4022SDave Chinner 	struct xfs_ail_cursor	cur;
3719e7004e7SDave Chinner 	xfs_log_item_t		*lip;
3729e7004e7SDave Chinner 	xfs_lsn_t		lsn;
373fe0da767SDave Chinner 	xfs_lsn_t		target;
37443ff2122SChristoph Hellwig 	long			tout;
3759e7004e7SDave Chinner 	int			stuck = 0;
37643ff2122SChristoph Hellwig 	int			flushing = 0;
3779e7004e7SDave Chinner 	int			count = 0;
3781da177e4SLinus Torvalds 
379670ce93fSDave Chinner 	/*
38043ff2122SChristoph Hellwig 	 * If we encountered pinned items or did not finish writing out all
38143ff2122SChristoph Hellwig 	 * buffers the last time we ran, force the log first and wait for it
38243ff2122SChristoph Hellwig 	 * before pushing again.
383670ce93fSDave Chinner 	 */
38457e80956SMatthew Wilcox 	if (ailp->ail_log_flush && ailp->ail_last_pushed_lsn == 0 &&
38557e80956SMatthew Wilcox 	    (!list_empty_careful(&ailp->ail_buf_list) ||
38643ff2122SChristoph Hellwig 	     xfs_ail_min_lsn(ailp))) {
38757e80956SMatthew Wilcox 		ailp->ail_log_flush = 0;
38843ff2122SChristoph Hellwig 
389ff6d6af2SBill O'Donnell 		XFS_STATS_INC(mp, xs_push_ail_flush);
390670ce93fSDave Chinner 		xfs_log_force(mp, XFS_LOG_SYNC);
391670ce93fSDave Chinner 	}
392670ce93fSDave Chinner 
39357e80956SMatthew Wilcox 	spin_lock(&ailp->ail_lock);
3948375f922SBrian Foster 
39557e80956SMatthew Wilcox 	/* barrier matches the ail_target update in xfs_ail_push() */
3968375f922SBrian Foster 	smp_rmb();
39757e80956SMatthew Wilcox 	target = ailp->ail_target;
39857e80956SMatthew Wilcox 	ailp->ail_target_prev = target;
3998375f922SBrian Foster 
40057e80956SMatthew Wilcox 	lip = xfs_trans_ail_cursor_first(ailp, &cur, ailp->ail_last_pushed_lsn);
401211e4d43SChristoph Hellwig 	if (!lip) {
4021da177e4SLinus Torvalds 		/*
40343ff2122SChristoph Hellwig 		 * If the AIL is empty or our push has reached the end we are
40443ff2122SChristoph Hellwig 		 * done now.
4051da177e4SLinus Torvalds 		 */
406e4a1e29cSEric Sandeen 		xfs_trans_ail_cursor_done(&cur);
40757e80956SMatthew Wilcox 		spin_unlock(&ailp->ail_lock);
4089e7004e7SDave Chinner 		goto out_done;
4091da177e4SLinus Torvalds 	}
4101da177e4SLinus Torvalds 
411ff6d6af2SBill O'Donnell 	XFS_STATS_INC(mp, xs_push_ail);
4121da177e4SLinus Torvalds 
413249a8c11SDavid Chinner 	lsn = lip->li_lsn;
41450e86686SDave Chinner 	while ((XFS_LSN_CMP(lip->li_lsn, target) <= 0)) {
415249a8c11SDavid Chinner 		int	lock_result;
41643ff2122SChristoph Hellwig 
417249a8c11SDavid Chinner 		/*
418904c17e6SDave Chinner 		 * Note that iop_push may unlock and reacquire the AIL lock.  We
41943ff2122SChristoph Hellwig 		 * rely on the AIL cursor implementation to be able to deal with
42043ff2122SChristoph Hellwig 		 * the dropped lock.
4211da177e4SLinus Torvalds 		 */
4227f4d01f3SBrian Foster 		lock_result = xfsaild_push_item(ailp, lip);
4231da177e4SLinus Torvalds 		switch (lock_result) {
4241da177e4SLinus Torvalds 		case XFS_ITEM_SUCCESS:
425ff6d6af2SBill O'Donnell 			XFS_STATS_INC(mp, xs_push_ail_success);
4269e4c109aSChristoph Hellwig 			trace_xfs_ail_push(lip);
4279e4c109aSChristoph Hellwig 
42857e80956SMatthew Wilcox 			ailp->ail_last_pushed_lsn = lsn;
4291da177e4SLinus Torvalds 			break;
4301da177e4SLinus Torvalds 
43143ff2122SChristoph Hellwig 		case XFS_ITEM_FLUSHING:
43243ff2122SChristoph Hellwig 			/*
43343ff2122SChristoph Hellwig 			 * The item or its backing buffer is already beeing
43443ff2122SChristoph Hellwig 			 * flushed.  The typical reason for that is that an
43543ff2122SChristoph Hellwig 			 * inode buffer is locked because we already pushed the
43643ff2122SChristoph Hellwig 			 * updates to it as part of inode clustering.
43743ff2122SChristoph Hellwig 			 *
43843ff2122SChristoph Hellwig 			 * We do not want to to stop flushing just because lots
43943ff2122SChristoph Hellwig 			 * of items are already beeing flushed, but we need to
44043ff2122SChristoph Hellwig 			 * re-try the flushing relatively soon if most of the
44143ff2122SChristoph Hellwig 			 * AIL is beeing flushed.
44243ff2122SChristoph Hellwig 			 */
443ff6d6af2SBill O'Donnell 			XFS_STATS_INC(mp, xs_push_ail_flushing);
44443ff2122SChristoph Hellwig 			trace_xfs_ail_flushing(lip);
44517b38471SChristoph Hellwig 
44643ff2122SChristoph Hellwig 			flushing++;
44757e80956SMatthew Wilcox 			ailp->ail_last_pushed_lsn = lsn;
4481da177e4SLinus Torvalds 			break;
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds 		case XFS_ITEM_PINNED:
451ff6d6af2SBill O'Donnell 			XFS_STATS_INC(mp, xs_push_ail_pinned);
4529e4c109aSChristoph Hellwig 			trace_xfs_ail_pinned(lip);
4539e4c109aSChristoph Hellwig 
454249a8c11SDavid Chinner 			stuck++;
45557e80956SMatthew Wilcox 			ailp->ail_log_flush++;
4561da177e4SLinus Torvalds 			break;
4571da177e4SLinus Torvalds 		case XFS_ITEM_LOCKED:
458ff6d6af2SBill O'Donnell 			XFS_STATS_INC(mp, xs_push_ail_locked);
4599e4c109aSChristoph Hellwig 			trace_xfs_ail_locked(lip);
46043ff2122SChristoph Hellwig 
461249a8c11SDavid Chinner 			stuck++;
4621da177e4SLinus Torvalds 			break;
4631da177e4SLinus Torvalds 		default:
4641da177e4SLinus Torvalds 			ASSERT(0);
4651da177e4SLinus Torvalds 			break;
4661da177e4SLinus Torvalds 		}
4671da177e4SLinus Torvalds 
468249a8c11SDavid Chinner 		count++;
469249a8c11SDavid Chinner 
470249a8c11SDavid Chinner 		/*
471249a8c11SDavid Chinner 		 * Are there too many items we can't do anything with?
47243ff2122SChristoph Hellwig 		 *
473249a8c11SDavid Chinner 		 * If we we are skipping too many items because we can't flush
474249a8c11SDavid Chinner 		 * them or they are already being flushed, we back off and
475249a8c11SDavid Chinner 		 * given them time to complete whatever operation is being
476249a8c11SDavid Chinner 		 * done. i.e. remove pressure from the AIL while we can't make
477249a8c11SDavid Chinner 		 * progress so traversals don't slow down further inserts and
478249a8c11SDavid Chinner 		 * removals to/from the AIL.
479249a8c11SDavid Chinner 		 *
480249a8c11SDavid Chinner 		 * The value of 100 is an arbitrary magic number based on
481249a8c11SDavid Chinner 		 * observation.
482249a8c11SDavid Chinner 		 */
483249a8c11SDavid Chinner 		if (stuck > 100)
484249a8c11SDavid Chinner 			break;
485249a8c11SDavid Chinner 
486af3e4022SDave Chinner 		lip = xfs_trans_ail_cursor_next(ailp, &cur);
487249a8c11SDavid Chinner 		if (lip == NULL)
488249a8c11SDavid Chinner 			break;
489249a8c11SDavid Chinner 		lsn = lip->li_lsn;
4901da177e4SLinus Torvalds 	}
491e4a1e29cSEric Sandeen 	xfs_trans_ail_cursor_done(&cur);
49257e80956SMatthew Wilcox 	spin_unlock(&ailp->ail_lock);
4931da177e4SLinus Torvalds 
49457e80956SMatthew Wilcox 	if (xfs_buf_delwri_submit_nowait(&ailp->ail_buf_list))
49557e80956SMatthew Wilcox 		ailp->ail_log_flush++;
496d808f617SDave Chinner 
49743ff2122SChristoph Hellwig 	if (!count || XFS_LSN_CMP(lsn, target) >= 0) {
4989e7004e7SDave Chinner out_done:
499249a8c11SDavid Chinner 		/*
50043ff2122SChristoph Hellwig 		 * We reached the target or the AIL is empty, so wait a bit
50143ff2122SChristoph Hellwig 		 * longer for I/O to complete and remove pushed items from the
50243ff2122SChristoph Hellwig 		 * AIL before we start the next scan from the start of the AIL.
503249a8c11SDavid Chinner 		 */
504453eac8aSDave Chinner 		tout = 50;
50557e80956SMatthew Wilcox 		ailp->ail_last_pushed_lsn = 0;
50643ff2122SChristoph Hellwig 	} else if (((stuck + flushing) * 100) / count > 90) {
507249a8c11SDavid Chinner 		/*
50843ff2122SChristoph Hellwig 		 * Either there is a lot of contention on the AIL or we are
50943ff2122SChristoph Hellwig 		 * stuck due to operations in progress. "Stuck" in this case
51043ff2122SChristoph Hellwig 		 * is defined as >90% of the items we tried to push were stuck.
511249a8c11SDavid Chinner 		 *
512249a8c11SDavid Chinner 		 * Backoff a bit more to allow some I/O to complete before
51343ff2122SChristoph Hellwig 		 * restarting from the start of the AIL. This prevents us from
51443ff2122SChristoph Hellwig 		 * spinning on the same items, and if they are pinned will all
51543ff2122SChristoph Hellwig 		 * the restart to issue a log force to unpin the stuck items.
516249a8c11SDavid Chinner 		 */
517453eac8aSDave Chinner 		tout = 20;
51857e80956SMatthew Wilcox 		ailp->ail_last_pushed_lsn = 0;
51943ff2122SChristoph Hellwig 	} else {
52043ff2122SChristoph Hellwig 		/*
52143ff2122SChristoph Hellwig 		 * Assume we have more work to do in a short while.
52243ff2122SChristoph Hellwig 		 */
52343ff2122SChristoph Hellwig 		tout = 10;
524453eac8aSDave Chinner 	}
5251da177e4SLinus Torvalds 
5260030807cSChristoph Hellwig 	return tout;
5270030807cSChristoph Hellwig }
5280030807cSChristoph Hellwig 
5290030807cSChristoph Hellwig static int
5300030807cSChristoph Hellwig xfsaild(
5310030807cSChristoph Hellwig 	void		*data)
5320030807cSChristoph Hellwig {
5330030807cSChristoph Hellwig 	struct xfs_ail	*ailp = data;
5340030807cSChristoph Hellwig 	long		tout = 0;	/* milliseconds */
5350030807cSChristoph Hellwig 
53643ff2122SChristoph Hellwig 	current->flags |= PF_MEMALLOC;
53718f1df4eSMichal Hocko 	set_freezable();
53843ff2122SChristoph Hellwig 
5390bd89676SHou Tao 	while (1) {
5400030807cSChristoph Hellwig 		if (tout && tout <= 20)
5410bd89676SHou Tao 			set_current_state(TASK_KILLABLE);
5420030807cSChristoph Hellwig 		else
5430bd89676SHou Tao 			set_current_state(TASK_INTERRUPTIBLE);
5440bd89676SHou Tao 
5450bd89676SHou Tao 		/*
5460bd89676SHou Tao 		 * Check kthread_should_stop() after we set the task state
5470bd89676SHou Tao 		 * to guarantee that we either see the stop bit and exit or
5480bd89676SHou Tao 		 * the task state is reset to runnable such that it's not
5490bd89676SHou Tao 		 * scheduled out indefinitely and detects the stop bit at
5500bd89676SHou Tao 		 * next iteration.
5510bd89676SHou Tao 		 *
5520bd89676SHou Tao 		 * A memory barrier is included in above task state set to
5530bd89676SHou Tao 		 * serialize again kthread_stop().
5540bd89676SHou Tao 		 */
5550bd89676SHou Tao 		if (kthread_should_stop()) {
5560bd89676SHou Tao 			__set_current_state(TASK_RUNNING);
5570bd89676SHou Tao 			break;
5580bd89676SHou Tao 		}
5598375f922SBrian Foster 
56057e80956SMatthew Wilcox 		spin_lock(&ailp->ail_lock);
5618375f922SBrian Foster 
5628375f922SBrian Foster 		/*
5638375f922SBrian Foster 		 * Idle if the AIL is empty and we are not racing with a target
5648375f922SBrian Foster 		 * update. We check the AIL after we set the task to a sleep
56557e80956SMatthew Wilcox 		 * state to guarantee that we either catch an ail_target update
5668375f922SBrian Foster 		 * or that a wake_up resets the state to TASK_RUNNING.
5678375f922SBrian Foster 		 * Otherwise, we run the risk of sleeping indefinitely.
5688375f922SBrian Foster 		 *
56957e80956SMatthew Wilcox 		 * The barrier matches the ail_target update in xfs_ail_push().
5708375f922SBrian Foster 		 */
5718375f922SBrian Foster 		smp_rmb();
5728375f922SBrian Foster 		if (!xfs_ail_min(ailp) &&
57357e80956SMatthew Wilcox 		    ailp->ail_target == ailp->ail_target_prev) {
57457e80956SMatthew Wilcox 			spin_unlock(&ailp->ail_lock);
57518f1df4eSMichal Hocko 			freezable_schedule();
5768375f922SBrian Foster 			tout = 0;
5778375f922SBrian Foster 			continue;
5788375f922SBrian Foster 		}
57957e80956SMatthew Wilcox 		spin_unlock(&ailp->ail_lock);
5808375f922SBrian Foster 
5818375f922SBrian Foster 		if (tout)
58218f1df4eSMichal Hocko 			freezable_schedule_timeout(msecs_to_jiffies(tout));
5838375f922SBrian Foster 
5848375f922SBrian Foster 		__set_current_state(TASK_RUNNING);
5850030807cSChristoph Hellwig 
5860030807cSChristoph Hellwig 		try_to_freeze();
5870030807cSChristoph Hellwig 
5880030807cSChristoph Hellwig 		tout = xfsaild_push(ailp);
5890030807cSChristoph Hellwig 	}
5900030807cSChristoph Hellwig 
5910030807cSChristoph Hellwig 	return 0;
5920bf6a5bdSDave Chinner }
5930bf6a5bdSDave Chinner 
5940bf6a5bdSDave Chinner /*
5950bf6a5bdSDave Chinner  * This routine is called to move the tail of the AIL forward.  It does this by
5960bf6a5bdSDave Chinner  * trying to flush items in the AIL whose lsns are below the given
5970bf6a5bdSDave Chinner  * threshold_lsn.
5980bf6a5bdSDave Chinner  *
5990bf6a5bdSDave Chinner  * The push is run asynchronously in a workqueue, which means the caller needs
6000bf6a5bdSDave Chinner  * to handle waiting on the async flush for space to become available.
6010bf6a5bdSDave Chinner  * We don't want to interrupt any push that is in progress, hence we only queue
6020bf6a5bdSDave Chinner  * work if we set the pushing bit approriately.
6030bf6a5bdSDave Chinner  *
6040bf6a5bdSDave Chinner  * We do this unlocked - we only need to know whether there is anything in the
6050bf6a5bdSDave Chinner  * AIL at the time we are called. We don't need to access the contents of
6060bf6a5bdSDave Chinner  * any of the objects, so the lock is not needed.
6070bf6a5bdSDave Chinner  */
6080bf6a5bdSDave Chinner void
609fd074841SDave Chinner xfs_ail_push(
6100bf6a5bdSDave Chinner 	struct xfs_ail	*ailp,
6110bf6a5bdSDave Chinner 	xfs_lsn_t	threshold_lsn)
6120bf6a5bdSDave Chinner {
6130bf6a5bdSDave Chinner 	xfs_log_item_t	*lip;
6140bf6a5bdSDave Chinner 
6150bf6a5bdSDave Chinner 	lip = xfs_ail_min(ailp);
61657e80956SMatthew Wilcox 	if (!lip || XFS_FORCED_SHUTDOWN(ailp->ail_mount) ||
61757e80956SMatthew Wilcox 	    XFS_LSN_CMP(threshold_lsn, ailp->ail_target) <= 0)
6180bf6a5bdSDave Chinner 		return;
6190bf6a5bdSDave Chinner 
6200bf6a5bdSDave Chinner 	/*
6210bf6a5bdSDave Chinner 	 * Ensure that the new target is noticed in push code before it clears
6220bf6a5bdSDave Chinner 	 * the XFS_AIL_PUSHING_BIT.
6230bf6a5bdSDave Chinner 	 */
6240bf6a5bdSDave Chinner 	smp_wmb();
62557e80956SMatthew Wilcox 	xfs_trans_ail_copy_lsn(ailp, &ailp->ail_target, &threshold_lsn);
6260030807cSChristoph Hellwig 	smp_wmb();
6270030807cSChristoph Hellwig 
62857e80956SMatthew Wilcox 	wake_up_process(ailp->ail_task);
6290bf6a5bdSDave Chinner }
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds /*
632fd074841SDave Chinner  * Push out all items in the AIL immediately
633fd074841SDave Chinner  */
634fd074841SDave Chinner void
635fd074841SDave Chinner xfs_ail_push_all(
636fd074841SDave Chinner 	struct xfs_ail  *ailp)
637fd074841SDave Chinner {
638fd074841SDave Chinner 	xfs_lsn_t       threshold_lsn = xfs_ail_max_lsn(ailp);
639fd074841SDave Chinner 
640fd074841SDave Chinner 	if (threshold_lsn)
641fd074841SDave Chinner 		xfs_ail_push(ailp, threshold_lsn);
642fd074841SDave Chinner }
643fd074841SDave Chinner 
644fd074841SDave Chinner /*
645211e4d43SChristoph Hellwig  * Push out all items in the AIL immediately and wait until the AIL is empty.
646211e4d43SChristoph Hellwig  */
647211e4d43SChristoph Hellwig void
648211e4d43SChristoph Hellwig xfs_ail_push_all_sync(
649211e4d43SChristoph Hellwig 	struct xfs_ail  *ailp)
650211e4d43SChristoph Hellwig {
651211e4d43SChristoph Hellwig 	struct xfs_log_item	*lip;
652211e4d43SChristoph Hellwig 	DEFINE_WAIT(wait);
653211e4d43SChristoph Hellwig 
65457e80956SMatthew Wilcox 	spin_lock(&ailp->ail_lock);
655211e4d43SChristoph Hellwig 	while ((lip = xfs_ail_max(ailp)) != NULL) {
65657e80956SMatthew Wilcox 		prepare_to_wait(&ailp->ail_empty, &wait, TASK_UNINTERRUPTIBLE);
65757e80956SMatthew Wilcox 		ailp->ail_target = lip->li_lsn;
65857e80956SMatthew Wilcox 		wake_up_process(ailp->ail_task);
65957e80956SMatthew Wilcox 		spin_unlock(&ailp->ail_lock);
660211e4d43SChristoph Hellwig 		schedule();
66157e80956SMatthew Wilcox 		spin_lock(&ailp->ail_lock);
662211e4d43SChristoph Hellwig 	}
66357e80956SMatthew Wilcox 	spin_unlock(&ailp->ail_lock);
664211e4d43SChristoph Hellwig 
66557e80956SMatthew Wilcox 	finish_wait(&ailp->ail_empty, &wait);
666211e4d43SChristoph Hellwig }
667211e4d43SChristoph Hellwig 
668211e4d43SChristoph Hellwig /*
6690e57f6a3SDave Chinner  * xfs_trans_ail_update - bulk AIL insertion operation.
6700e57f6a3SDave Chinner  *
6710e57f6a3SDave Chinner  * @xfs_trans_ail_update takes an array of log items that all need to be
6720e57f6a3SDave Chinner  * positioned at the same LSN in the AIL. If an item is not in the AIL, it will
6730e57f6a3SDave Chinner  * be added.  Otherwise, it will be repositioned  by removing it and re-adding
6740e57f6a3SDave Chinner  * it to the AIL. If we move the first item in the AIL, update the log tail to
6750e57f6a3SDave Chinner  * match the new minimum LSN in the AIL.
6760e57f6a3SDave Chinner  *
6770e57f6a3SDave Chinner  * This function takes the AIL lock once to execute the update operations on
6780e57f6a3SDave Chinner  * all the items in the array, and as such should not be called with the AIL
6790e57f6a3SDave Chinner  * lock held. As a result, once we have the AIL lock, we need to check each log
6800e57f6a3SDave Chinner  * item LSN to confirm it needs to be moved forward in the AIL.
6810e57f6a3SDave Chinner  *
6820e57f6a3SDave Chinner  * To optimise the insert operation, we delete all the items from the AIL in
6830e57f6a3SDave Chinner  * the first pass, moving them into a temporary list, then splice the temporary
6840e57f6a3SDave Chinner  * list into the correct position in the AIL. This avoids needing to do an
6850e57f6a3SDave Chinner  * insert operation on every item.
6860e57f6a3SDave Chinner  *
6870e57f6a3SDave Chinner  * This function must be called with the AIL lock held.  The lock is dropped
6880e57f6a3SDave Chinner  * before returning.
6890e57f6a3SDave Chinner  */
6900e57f6a3SDave Chinner void
6910e57f6a3SDave Chinner xfs_trans_ail_update_bulk(
6920e57f6a3SDave Chinner 	struct xfs_ail		*ailp,
6931d8c95a3SDave Chinner 	struct xfs_ail_cursor	*cur,
6940e57f6a3SDave Chinner 	struct xfs_log_item	**log_items,
6950e57f6a3SDave Chinner 	int			nr_items,
69657e80956SMatthew Wilcox 	xfs_lsn_t		lsn) __releases(ailp->ail_lock)
6970e57f6a3SDave Chinner {
6980e57f6a3SDave Chinner 	xfs_log_item_t		*mlip;
6990e57f6a3SDave Chinner 	int			mlip_changed = 0;
7000e57f6a3SDave Chinner 	int			i;
7010e57f6a3SDave Chinner 	LIST_HEAD(tmp);
7020e57f6a3SDave Chinner 
703e44f4112SAlex Elder 	ASSERT(nr_items > 0);		/* Not required, but true. */
7040e57f6a3SDave Chinner 	mlip = xfs_ail_min(ailp);
7050e57f6a3SDave Chinner 
7060e57f6a3SDave Chinner 	for (i = 0; i < nr_items; i++) {
7070e57f6a3SDave Chinner 		struct xfs_log_item *lip = log_items[i];
70822525c17SDave Chinner 		if (test_and_set_bit(XFS_LI_IN_AIL, &lip->li_flags)) {
7090e57f6a3SDave Chinner 			/* check if we really need to move the item */
7100e57f6a3SDave Chinner 			if (XFS_LSN_CMP(lsn, lip->li_lsn) <= 0)
7110e57f6a3SDave Chinner 				continue;
7120e57f6a3SDave Chinner 
713750b9c90SDave Chinner 			trace_xfs_ail_move(lip, lip->li_lsn, lsn);
7140e57f6a3SDave Chinner 			xfs_ail_delete(ailp, lip);
7150e57f6a3SDave Chinner 			if (mlip == lip)
7160e57f6a3SDave Chinner 				mlip_changed = 1;
7170e57f6a3SDave Chinner 		} else {
718750b9c90SDave Chinner 			trace_xfs_ail_insert(lip, 0, lsn);
7190e57f6a3SDave Chinner 		}
7200e57f6a3SDave Chinner 		lip->li_lsn = lsn;
7210e57f6a3SDave Chinner 		list_add(&lip->li_ail, &tmp);
7220e57f6a3SDave Chinner 	}
7230e57f6a3SDave Chinner 
724e44f4112SAlex Elder 	if (!list_empty(&tmp))
7251d8c95a3SDave Chinner 		xfs_ail_splice(ailp, cur, &tmp, lsn);
7261c304625SChristoph Hellwig 
7271c304625SChristoph Hellwig 	if (mlip_changed) {
72857e80956SMatthew Wilcox 		if (!XFS_FORCED_SHUTDOWN(ailp->ail_mount))
72957e80956SMatthew Wilcox 			xlog_assign_tail_lsn_locked(ailp->ail_mount);
73057e80956SMatthew Wilcox 		spin_unlock(&ailp->ail_lock);
73109a423a3SChristoph Hellwig 
73257e80956SMatthew Wilcox 		xfs_log_space_wake(ailp->ail_mount);
7331c304625SChristoph Hellwig 	} else {
73457e80956SMatthew Wilcox 		spin_unlock(&ailp->ail_lock);
7350e57f6a3SDave Chinner 	}
7360e57f6a3SDave Chinner }
7370e57f6a3SDave Chinner 
73827af1bbfSChristoph Hellwig bool
73927af1bbfSChristoph Hellwig xfs_ail_delete_one(
74027af1bbfSChristoph Hellwig 	struct xfs_ail		*ailp,
74127af1bbfSChristoph Hellwig 	struct xfs_log_item	*lip)
74227af1bbfSChristoph Hellwig {
74327af1bbfSChristoph Hellwig 	struct xfs_log_item	*mlip = xfs_ail_min(ailp);
74427af1bbfSChristoph Hellwig 
74527af1bbfSChristoph Hellwig 	trace_xfs_ail_delete(lip, mlip->li_lsn, lip->li_lsn);
74627af1bbfSChristoph Hellwig 	xfs_ail_delete(ailp, lip);
747d3a304b6SCarlos Maiolino 	xfs_clear_li_failed(lip);
74822525c17SDave Chinner 	clear_bit(XFS_LI_IN_AIL, &lip->li_flags);
74927af1bbfSChristoph Hellwig 	lip->li_lsn = 0;
75027af1bbfSChristoph Hellwig 
75127af1bbfSChristoph Hellwig 	return mlip == lip;
75227af1bbfSChristoph Hellwig }
75327af1bbfSChristoph Hellwig 
75427af1bbfSChristoph Hellwig /**
75527af1bbfSChristoph Hellwig  * Remove a log items from the AIL
75630136832SDave Chinner  *
75730136832SDave Chinner  * @xfs_trans_ail_delete_bulk takes an array of log items that all need to
75830136832SDave Chinner  * removed from the AIL. The caller is already holding the AIL lock, and done
75930136832SDave Chinner  * all the checks necessary to ensure the items passed in via @log_items are
76030136832SDave Chinner  * ready for deletion. This includes checking that the items are in the AIL.
76130136832SDave Chinner  *
76230136832SDave Chinner  * For each log item to be removed, unlink it  from the AIL, clear the IN_AIL
76330136832SDave Chinner  * flag from the item and reset the item's lsn to 0. If we remove the first
76430136832SDave Chinner  * item in the AIL, update the log tail to match the new minimum LSN in the
76530136832SDave Chinner  * AIL.
76630136832SDave Chinner  *
76730136832SDave Chinner  * This function will not drop the AIL lock until all items are removed from
76830136832SDave Chinner  * the AIL to minimise the amount of lock traffic on the AIL. This does not
76930136832SDave Chinner  * greatly increase the AIL hold time, but does significantly reduce the amount
77030136832SDave Chinner  * of traffic on the lock, especially during IO completion.
77130136832SDave Chinner  *
77230136832SDave Chinner  * This function must be called with the AIL lock held.  The lock is dropped
77330136832SDave Chinner  * before returning.
77430136832SDave Chinner  */
77530136832SDave Chinner void
77627af1bbfSChristoph Hellwig xfs_trans_ail_delete(
77730136832SDave Chinner 	struct xfs_ail		*ailp,
77827af1bbfSChristoph Hellwig 	struct xfs_log_item	*lip,
77957e80956SMatthew Wilcox 	int			shutdown_type) __releases(ailp->ail_lock)
78030136832SDave Chinner {
78157e80956SMatthew Wilcox 	struct xfs_mount	*mp = ailp->ail_mount;
78227af1bbfSChristoph Hellwig 	bool			mlip_changed;
78330136832SDave Chinner 
78422525c17SDave Chinner 	if (!test_bit(XFS_LI_IN_AIL, &lip->li_flags)) {
78557e80956SMatthew Wilcox 		spin_unlock(&ailp->ail_lock);
78630136832SDave Chinner 		if (!XFS_FORCED_SHUTDOWN(mp)) {
7876a19d939SDave Chinner 			xfs_alert_tag(mp, XFS_PTAG_AILDELETE,
78830136832SDave Chinner 	"%s: attempting to delete a log item that is not in the AIL",
78930136832SDave Chinner 					__func__);
79004913fddSDave Chinner 			xfs_force_shutdown(mp, shutdown_type);
79130136832SDave Chinner 		}
79230136832SDave Chinner 		return;
79330136832SDave Chinner 	}
79430136832SDave Chinner 
79527af1bbfSChristoph Hellwig 	mlip_changed = xfs_ail_delete_one(ailp, lip);
7961c304625SChristoph Hellwig 	if (mlip_changed) {
79727af1bbfSChristoph Hellwig 		if (!XFS_FORCED_SHUTDOWN(mp))
79827af1bbfSChristoph Hellwig 			xlog_assign_tail_lsn_locked(mp);
79957e80956SMatthew Wilcox 		if (list_empty(&ailp->ail_head))
80057e80956SMatthew Wilcox 			wake_up_all(&ailp->ail_empty);
80130136832SDave Chinner 	}
80227af1bbfSChristoph Hellwig 
80357e80956SMatthew Wilcox 	spin_unlock(&ailp->ail_lock);
80427af1bbfSChristoph Hellwig 	if (mlip_changed)
80557e80956SMatthew Wilcox 		xfs_log_space_wake(ailp->ail_mount);
80630136832SDave Chinner }
8071da177e4SLinus Torvalds 
808249a8c11SDavid Chinner int
8091da177e4SLinus Torvalds xfs_trans_ail_init(
8101da177e4SLinus Torvalds 	xfs_mount_t	*mp)
8111da177e4SLinus Torvalds {
81282fa9012SDavid Chinner 	struct xfs_ail	*ailp;
81382fa9012SDavid Chinner 
81482fa9012SDavid Chinner 	ailp = kmem_zalloc(sizeof(struct xfs_ail), KM_MAYFAIL);
81582fa9012SDavid Chinner 	if (!ailp)
8162451337dSDave Chinner 		return -ENOMEM;
81782fa9012SDavid Chinner 
81857e80956SMatthew Wilcox 	ailp->ail_mount = mp;
81957e80956SMatthew Wilcox 	INIT_LIST_HEAD(&ailp->ail_head);
82057e80956SMatthew Wilcox 	INIT_LIST_HEAD(&ailp->ail_cursors);
82157e80956SMatthew Wilcox 	spin_lock_init(&ailp->ail_lock);
82257e80956SMatthew Wilcox 	INIT_LIST_HEAD(&ailp->ail_buf_list);
82357e80956SMatthew Wilcox 	init_waitqueue_head(&ailp->ail_empty);
8240030807cSChristoph Hellwig 
82557e80956SMatthew Wilcox 	ailp->ail_task = kthread_run(xfsaild, ailp, "xfsaild/%s",
82657e80956SMatthew Wilcox 			ailp->ail_mount->m_fsname);
82757e80956SMatthew Wilcox 	if (IS_ERR(ailp->ail_task))
8280030807cSChristoph Hellwig 		goto out_free_ailp;
8290030807cSChristoph Hellwig 
83027d8d5feSDavid Chinner 	mp->m_ail = ailp;
83127d8d5feSDavid Chinner 	return 0;
8320030807cSChristoph Hellwig 
8330030807cSChristoph Hellwig out_free_ailp:
8340030807cSChristoph Hellwig 	kmem_free(ailp);
8352451337dSDave Chinner 	return -ENOMEM;
836249a8c11SDavid Chinner }
837249a8c11SDavid Chinner 
838249a8c11SDavid Chinner void
839249a8c11SDavid Chinner xfs_trans_ail_destroy(
840249a8c11SDavid Chinner 	xfs_mount_t	*mp)
841249a8c11SDavid Chinner {
84282fa9012SDavid Chinner 	struct xfs_ail	*ailp = mp->m_ail;
84382fa9012SDavid Chinner 
84457e80956SMatthew Wilcox 	kthread_stop(ailp->ail_task);
84582fa9012SDavid Chinner 	kmem_free(ailp);
8461da177e4SLinus Torvalds }
847