xref: /openbmc/linux/fs/xfs/xfs_trans_ail.c (revision 27af1bbf)
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"
281da177e4SLinus Torvalds #include "xfs_error.h"
29239880efSDave Chinner #include "xfs_log.h"
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds #ifdef DEBUG
32cd4a3c50SDave Chinner /*
33cd4a3c50SDave Chinner  * Check that the list is sorted as it should be.
34cd4a3c50SDave Chinner  */
35cd4a3c50SDave Chinner STATIC void
36cd4a3c50SDave Chinner xfs_ail_check(
37cd4a3c50SDave Chinner 	struct xfs_ail	*ailp,
38cd4a3c50SDave Chinner 	xfs_log_item_t	*lip)
39cd4a3c50SDave Chinner {
40cd4a3c50SDave Chinner 	xfs_log_item_t	*prev_lip;
41cd4a3c50SDave Chinner 
42cd4a3c50SDave Chinner 	if (list_empty(&ailp->xa_ail))
43cd4a3c50SDave Chinner 		return;
44cd4a3c50SDave Chinner 
45cd4a3c50SDave Chinner 	/*
46cd4a3c50SDave Chinner 	 * Check the next and previous entries are valid.
47cd4a3c50SDave Chinner 	 */
48cd4a3c50SDave Chinner 	ASSERT((lip->li_flags & XFS_LI_IN_AIL) != 0);
49cd4a3c50SDave Chinner 	prev_lip = list_entry(lip->li_ail.prev, xfs_log_item_t, li_ail);
50cd4a3c50SDave Chinner 	if (&prev_lip->li_ail != &ailp->xa_ail)
51cd4a3c50SDave Chinner 		ASSERT(XFS_LSN_CMP(prev_lip->li_lsn, lip->li_lsn) <= 0);
52cd4a3c50SDave Chinner 
53cd4a3c50SDave Chinner 	prev_lip = list_entry(lip->li_ail.next, xfs_log_item_t, li_ail);
54cd4a3c50SDave Chinner 	if (&prev_lip->li_ail != &ailp->xa_ail)
55cd4a3c50SDave Chinner 		ASSERT(XFS_LSN_CMP(prev_lip->li_lsn, lip->li_lsn) >= 0);
56cd4a3c50SDave Chinner 
57cd4a3c50SDave Chinner 
58cd4a3c50SDave Chinner }
59cd4a3c50SDave Chinner #else /* !DEBUG */
60de08dbc1SDavid Chinner #define	xfs_ail_check(a,l)
611da177e4SLinus Torvalds #endif /* DEBUG */
621da177e4SLinus Torvalds 
63cd4a3c50SDave Chinner /*
64fd074841SDave Chinner  * Return a pointer to the last item in the AIL.  If the AIL is empty, then
65fd074841SDave Chinner  * return NULL.
66fd074841SDave Chinner  */
67fd074841SDave Chinner static xfs_log_item_t *
68fd074841SDave Chinner xfs_ail_max(
69fd074841SDave Chinner 	struct xfs_ail  *ailp)
70fd074841SDave Chinner {
71fd074841SDave Chinner 	if (list_empty(&ailp->xa_ail))
72fd074841SDave Chinner 		return NULL;
73fd074841SDave Chinner 
74fd074841SDave Chinner 	return list_entry(ailp->xa_ail.prev, xfs_log_item_t, li_ail);
75fd074841SDave Chinner }
76fd074841SDave Chinner 
77fd074841SDave Chinner /*
78cd4a3c50SDave Chinner  * Return a pointer to the item which follows the given item in the AIL.  If
79cd4a3c50SDave Chinner  * the given item is the last item in the list, then return NULL.
80cd4a3c50SDave Chinner  */
81cd4a3c50SDave Chinner static xfs_log_item_t *
82cd4a3c50SDave Chinner xfs_ail_next(
83cd4a3c50SDave Chinner 	struct xfs_ail  *ailp,
84cd4a3c50SDave Chinner 	xfs_log_item_t  *lip)
85cd4a3c50SDave Chinner {
86cd4a3c50SDave Chinner 	if (lip->li_ail.next == &ailp->xa_ail)
87cd4a3c50SDave Chinner 		return NULL;
88cd4a3c50SDave Chinner 
89cd4a3c50SDave Chinner 	return list_first_entry(&lip->li_ail, xfs_log_item_t, li_ail);
90cd4a3c50SDave Chinner }
91cd4a3c50SDave Chinner 
92cd4a3c50SDave Chinner /*
93cd4a3c50SDave Chinner  * This is called by the log manager code to determine the LSN of the tail of
94cd4a3c50SDave Chinner  * the log.  This is exactly the LSN of the first item in the AIL.  If the AIL
95cd4a3c50SDave Chinner  * is empty, then this function returns 0.
961da177e4SLinus Torvalds  *
97cd4a3c50SDave Chinner  * We need the AIL lock in order to get a coherent read of the lsn of the last
98cd4a3c50SDave Chinner  * item in the AIL.
991da177e4SLinus Torvalds  */
1001da177e4SLinus Torvalds xfs_lsn_t
101fd074841SDave Chinner xfs_ail_min_lsn(
1025b00f14fSDavid Chinner 	struct xfs_ail	*ailp)
1031da177e4SLinus Torvalds {
104cd4a3c50SDave Chinner 	xfs_lsn_t	lsn = 0;
1051da177e4SLinus Torvalds 	xfs_log_item_t	*lip;
1061da177e4SLinus Torvalds 
107c7e8f268SDavid Chinner 	spin_lock(&ailp->xa_lock);
1085b00f14fSDavid Chinner 	lip = xfs_ail_min(ailp);
109cd4a3c50SDave Chinner 	if (lip)
1101da177e4SLinus Torvalds 		lsn = lip->li_lsn;
111c7e8f268SDavid Chinner 	spin_unlock(&ailp->xa_lock);
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds 	return lsn;
1141da177e4SLinus Torvalds }
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds /*
117fd074841SDave Chinner  * Return the maximum lsn held in the AIL, or zero if the AIL is empty.
118fd074841SDave Chinner  */
119fd074841SDave Chinner static xfs_lsn_t
120fd074841SDave Chinner xfs_ail_max_lsn(
121fd074841SDave Chinner 	struct xfs_ail  *ailp)
122fd074841SDave Chinner {
123fd074841SDave Chinner 	xfs_lsn_t       lsn = 0;
124fd074841SDave Chinner 	xfs_log_item_t  *lip;
125fd074841SDave Chinner 
126fd074841SDave Chinner 	spin_lock(&ailp->xa_lock);
127fd074841SDave Chinner 	lip = xfs_ail_max(ailp);
128fd074841SDave Chinner 	if (lip)
129fd074841SDave Chinner 		lsn = lip->li_lsn;
130fd074841SDave Chinner 	spin_unlock(&ailp->xa_lock);
131fd074841SDave Chinner 
132fd074841SDave Chinner 	return lsn;
133fd074841SDave Chinner }
134fd074841SDave Chinner 
135fd074841SDave Chinner /*
136af3e4022SDave Chinner  * The cursor keeps track of where our current traversal is up to by tracking
137af3e4022SDave Chinner  * the next item in the list for us. However, for this to be safe, removing an
138af3e4022SDave Chinner  * object from the AIL needs to invalidate any cursor that points to it. hence
139af3e4022SDave Chinner  * the traversal cursor needs to be linked to the struct xfs_ail so that
140af3e4022SDave Chinner  * deletion can search all the active cursors for invalidation.
14127d8d5feSDavid Chinner  */
1425b00f14fSDavid Chinner STATIC void
14327d8d5feSDavid Chinner xfs_trans_ail_cursor_init(
14427d8d5feSDavid Chinner 	struct xfs_ail		*ailp,
14527d8d5feSDavid Chinner 	struct xfs_ail_cursor	*cur)
14627d8d5feSDavid Chinner {
14727d8d5feSDavid Chinner 	cur->item = NULL;
148af3e4022SDave Chinner 	list_add_tail(&cur->list, &ailp->xa_cursors);
14927d8d5feSDavid Chinner }
15027d8d5feSDavid Chinner 
15127d8d5feSDavid Chinner /*
152af3e4022SDave Chinner  * Get the next item in the traversal and advance the cursor.  If the cursor
153af3e4022SDave Chinner  * was invalidated (indicated by a lip of 1), restart the traversal.
15427d8d5feSDavid Chinner  */
1555b00f14fSDavid Chinner struct xfs_log_item *
15627d8d5feSDavid Chinner xfs_trans_ail_cursor_next(
15727d8d5feSDavid Chinner 	struct xfs_ail		*ailp,
15827d8d5feSDavid Chinner 	struct xfs_ail_cursor	*cur)
15927d8d5feSDavid Chinner {
16027d8d5feSDavid Chinner 	struct xfs_log_item	*lip = cur->item;
16127d8d5feSDavid Chinner 
162db9d67d6SChristoph Hellwig 	if ((uintptr_t)lip & 1)
16327d8d5feSDavid Chinner 		lip = xfs_ail_min(ailp);
16416b59029SDave Chinner 	if (lip)
16516b59029SDave Chinner 		cur->item = xfs_ail_next(ailp, lip);
16627d8d5feSDavid Chinner 	return lip;
16727d8d5feSDavid Chinner }
16827d8d5feSDavid Chinner 
16927d8d5feSDavid Chinner /*
170af3e4022SDave Chinner  * When the traversal is complete, we need to remove the cursor from the list
171af3e4022SDave Chinner  * of traversing cursors.
17227d8d5feSDavid Chinner  */
17327d8d5feSDavid Chinner void
17427d8d5feSDavid Chinner xfs_trans_ail_cursor_done(
175af3e4022SDave Chinner 	struct xfs_ail_cursor	*cur)
17627d8d5feSDavid Chinner {
177af3e4022SDave Chinner 	cur->item = NULL;
178af3e4022SDave Chinner 	list_del_init(&cur->list);
17927d8d5feSDavid Chinner }
18027d8d5feSDavid Chinner 
18127d8d5feSDavid Chinner /*
182af3e4022SDave Chinner  * Invalidate any cursor that is pointing to this item. This is called when an
183af3e4022SDave Chinner  * item is removed from the AIL. Any cursor pointing to this object is now
184af3e4022SDave Chinner  * invalid and the traversal needs to be terminated so it doesn't reference a
185af3e4022SDave Chinner  * freed object. We set the low bit of the cursor item pointer so we can
186af3e4022SDave Chinner  * distinguish between an invalidation and the end of the list when getting the
187af3e4022SDave Chinner  * next item from the cursor.
1885b00f14fSDavid Chinner  */
1895b00f14fSDavid Chinner STATIC void
1905b00f14fSDavid Chinner xfs_trans_ail_cursor_clear(
1915b00f14fSDavid Chinner 	struct xfs_ail		*ailp,
1925b00f14fSDavid Chinner 	struct xfs_log_item	*lip)
1935b00f14fSDavid Chinner {
1945b00f14fSDavid Chinner 	struct xfs_ail_cursor	*cur;
1955b00f14fSDavid Chinner 
196af3e4022SDave Chinner 	list_for_each_entry(cur, &ailp->xa_cursors, list) {
1975b00f14fSDavid Chinner 		if (cur->item == lip)
1985b00f14fSDavid Chinner 			cur->item = (struct xfs_log_item *)
199db9d67d6SChristoph Hellwig 					((uintptr_t)cur->item | 1);
2005b00f14fSDavid Chinner 	}
2015b00f14fSDavid Chinner }
2025b00f14fSDavid Chinner 
2035b00f14fSDavid Chinner /*
20416b59029SDave Chinner  * Find the first item in the AIL with the given @lsn by searching in ascending
20516b59029SDave Chinner  * LSN order and initialise the cursor to point to the next item for a
20616b59029SDave Chinner  * ascending traversal.  Pass a @lsn of zero to initialise the cursor to the
20716b59029SDave Chinner  * first item in the AIL. Returns NULL if the list is empty.
208249a8c11SDavid Chinner  */
2095b00f14fSDavid Chinner xfs_log_item_t *
2105b00f14fSDavid Chinner xfs_trans_ail_cursor_first(
21127d8d5feSDavid Chinner 	struct xfs_ail		*ailp,
21227d8d5feSDavid Chinner 	struct xfs_ail_cursor	*cur,
213249a8c11SDavid Chinner 	xfs_lsn_t		lsn)
214249a8c11SDavid Chinner {
215249a8c11SDavid Chinner 	xfs_log_item_t		*lip;
216249a8c11SDavid Chinner 
2175b00f14fSDavid Chinner 	xfs_trans_ail_cursor_init(ailp, cur);
21816b59029SDave Chinner 
21916b59029SDave Chinner 	if (lsn == 0) {
22027d8d5feSDavid Chinner 		lip = xfs_ail_min(ailp);
2215b00f14fSDavid Chinner 		goto out;
22216b59029SDave Chinner 	}
223249a8c11SDavid Chinner 
22427d8d5feSDavid Chinner 	list_for_each_entry(lip, &ailp->xa_ail, li_ail) {
2255b00f14fSDavid Chinner 		if (XFS_LSN_CMP(lip->li_lsn, lsn) >= 0)
2267ee49acfSDavid Chinner 			goto out;
2275b00f14fSDavid Chinner 	}
22816b59029SDave Chinner 	return NULL;
22916b59029SDave Chinner 
2305b00f14fSDavid Chinner out:
23116b59029SDave Chinner 	if (lip)
23216b59029SDave Chinner 		cur->item = xfs_ail_next(ailp, lip);
233249a8c11SDavid Chinner 	return lip;
234249a8c11SDavid Chinner }
235535f6b37SJosef 'Jeff' Sipek 
2361d8c95a3SDave Chinner static struct xfs_log_item *
2371d8c95a3SDave Chinner __xfs_trans_ail_cursor_last(
2381d8c95a3SDave Chinner 	struct xfs_ail		*ailp,
2391d8c95a3SDave Chinner 	xfs_lsn_t		lsn)
2401d8c95a3SDave Chinner {
2411d8c95a3SDave Chinner 	xfs_log_item_t		*lip;
2421d8c95a3SDave Chinner 
2431d8c95a3SDave Chinner 	list_for_each_entry_reverse(lip, &ailp->xa_ail, li_ail) {
2441d8c95a3SDave Chinner 		if (XFS_LSN_CMP(lip->li_lsn, lsn) <= 0)
2451d8c95a3SDave Chinner 			return lip;
2461d8c95a3SDave Chinner 	}
2471d8c95a3SDave Chinner 	return NULL;
2481d8c95a3SDave Chinner }
2491d8c95a3SDave Chinner 
2501d8c95a3SDave Chinner /*
25116b59029SDave Chinner  * Find the last item in the AIL with the given @lsn by searching in descending
25216b59029SDave Chinner  * LSN order and initialise the cursor to point to that item.  If there is no
25316b59029SDave Chinner  * item with the value of @lsn, then it sets the cursor to the last item with an
25416b59029SDave Chinner  * LSN lower than @lsn.  Returns NULL if the list is empty.
2551d8c95a3SDave Chinner  */
2561d8c95a3SDave Chinner struct xfs_log_item *
2571d8c95a3SDave Chinner xfs_trans_ail_cursor_last(
2581d8c95a3SDave Chinner 	struct xfs_ail		*ailp,
2591d8c95a3SDave Chinner 	struct xfs_ail_cursor	*cur,
2601d8c95a3SDave Chinner 	xfs_lsn_t		lsn)
2611d8c95a3SDave Chinner {
2621d8c95a3SDave Chinner 	xfs_trans_ail_cursor_init(ailp, cur);
2631d8c95a3SDave Chinner 	cur->item = __xfs_trans_ail_cursor_last(ailp, lsn);
2641d8c95a3SDave Chinner 	return cur->item;
2651d8c95a3SDave Chinner }
2661d8c95a3SDave Chinner 
2671d8c95a3SDave Chinner /*
26816b59029SDave Chinner  * Splice the log item list into the AIL at the given LSN. We splice to the
2691d8c95a3SDave Chinner  * tail of the given LSN to maintain insert order for push traversals. The
2701d8c95a3SDave Chinner  * cursor is optional, allowing repeated updates to the same LSN to avoid
271e44f4112SAlex Elder  * repeated traversals.  This should not be called with an empty list.
272cd4a3c50SDave Chinner  */
273cd4a3c50SDave Chinner static void
274cd4a3c50SDave Chinner xfs_ail_splice(
275cd4a3c50SDave Chinner 	struct xfs_ail		*ailp,
2761d8c95a3SDave Chinner 	struct xfs_ail_cursor	*cur,
277cd4a3c50SDave Chinner 	struct list_head	*list,
278cd4a3c50SDave Chinner 	xfs_lsn_t		lsn)
279cd4a3c50SDave Chinner {
280e44f4112SAlex Elder 	struct xfs_log_item	*lip;
281e44f4112SAlex Elder 
282e44f4112SAlex Elder 	ASSERT(!list_empty(list));
283cd4a3c50SDave Chinner 
2841d8c95a3SDave Chinner 	/*
285e44f4112SAlex Elder 	 * Use the cursor to determine the insertion point if one is
286e44f4112SAlex Elder 	 * provided.  If not, or if the one we got is not valid,
287e44f4112SAlex Elder 	 * find the place in the AIL where the items belong.
2881d8c95a3SDave Chinner 	 */
289e44f4112SAlex Elder 	lip = cur ? cur->item : NULL;
290db9d67d6SChristoph Hellwig 	if (!lip || (uintptr_t)lip & 1)
2911d8c95a3SDave Chinner 		lip = __xfs_trans_ail_cursor_last(ailp, lsn);
2921d8c95a3SDave Chinner 
293e44f4112SAlex Elder 	/*
294e44f4112SAlex Elder 	 * If a cursor is provided, we know we're processing the AIL
295e44f4112SAlex Elder 	 * in lsn order, and future items to be spliced in will
296e44f4112SAlex Elder 	 * follow the last one being inserted now.  Update the
297e44f4112SAlex Elder 	 * cursor to point to that last item, now while we have a
298e44f4112SAlex Elder 	 * reliable pointer to it.
299e44f4112SAlex Elder 	 */
3001d8c95a3SDave Chinner 	if (cur)
301e44f4112SAlex Elder 		cur->item = list_entry(list->prev, struct xfs_log_item, li_ail);
302cd4a3c50SDave Chinner 
3031d8c95a3SDave Chinner 	/*
304e44f4112SAlex Elder 	 * Finally perform the splice.  Unless the AIL was empty,
305e44f4112SAlex Elder 	 * lip points to the item in the AIL _after_ which the new
306e44f4112SAlex Elder 	 * items should go.  If lip is null the AIL was empty, so
307e44f4112SAlex Elder 	 * the new items go at the head of the AIL.
3081d8c95a3SDave Chinner 	 */
309e44f4112SAlex Elder 	if (lip)
3101d8c95a3SDave Chinner 		list_splice(list, &lip->li_ail);
311e44f4112SAlex Elder 	else
312e44f4112SAlex Elder 		list_splice(list, &ailp->xa_ail);
313cd4a3c50SDave Chinner }
314cd4a3c50SDave Chinner 
315cd4a3c50SDave Chinner /*
316cd4a3c50SDave Chinner  * Delete the given item from the AIL.  Return a pointer to the item.
317cd4a3c50SDave Chinner  */
318cd4a3c50SDave Chinner static void
319cd4a3c50SDave Chinner xfs_ail_delete(
320cd4a3c50SDave Chinner 	struct xfs_ail  *ailp,
321cd4a3c50SDave Chinner 	xfs_log_item_t  *lip)
322cd4a3c50SDave Chinner {
323cd4a3c50SDave Chinner 	xfs_ail_check(ailp, lip);
324cd4a3c50SDave Chinner 	list_del(&lip->li_ail);
325cd4a3c50SDave Chinner 	xfs_trans_ail_cursor_clear(ailp, lip);
326cd4a3c50SDave Chinner }
327cd4a3c50SDave Chinner 
3280030807cSChristoph Hellwig static long
3290030807cSChristoph Hellwig xfsaild_push(
3300030807cSChristoph Hellwig 	struct xfs_ail		*ailp)
331249a8c11SDavid Chinner {
33282fa9012SDavid Chinner 	xfs_mount_t		*mp = ailp->xa_mount;
333af3e4022SDave Chinner 	struct xfs_ail_cursor	cur;
3349e7004e7SDave Chinner 	xfs_log_item_t		*lip;
3359e7004e7SDave Chinner 	xfs_lsn_t		lsn;
336fe0da767SDave Chinner 	xfs_lsn_t		target;
33743ff2122SChristoph Hellwig 	long			tout;
3389e7004e7SDave Chinner 	int			stuck = 0;
33943ff2122SChristoph Hellwig 	int			flushing = 0;
3409e7004e7SDave Chinner 	int			count = 0;
3411da177e4SLinus Torvalds 
342670ce93fSDave Chinner 	/*
34343ff2122SChristoph Hellwig 	 * If we encountered pinned items or did not finish writing out all
34443ff2122SChristoph Hellwig 	 * buffers the last time we ran, force the log first and wait for it
34543ff2122SChristoph Hellwig 	 * before pushing again.
346670ce93fSDave Chinner 	 */
34743ff2122SChristoph Hellwig 	if (ailp->xa_log_flush && ailp->xa_last_pushed_lsn == 0 &&
34843ff2122SChristoph Hellwig 	    (!list_empty_careful(&ailp->xa_buf_list) ||
34943ff2122SChristoph Hellwig 	     xfs_ail_min_lsn(ailp))) {
350670ce93fSDave Chinner 		ailp->xa_log_flush = 0;
35143ff2122SChristoph Hellwig 
352ff6d6af2SBill O'Donnell 		XFS_STATS_INC(mp, xs_push_ail_flush);
353670ce93fSDave Chinner 		xfs_log_force(mp, XFS_LOG_SYNC);
354670ce93fSDave Chinner 	}
355670ce93fSDave Chinner 
35643ff2122SChristoph Hellwig 	spin_lock(&ailp->xa_lock);
3578375f922SBrian Foster 
3588375f922SBrian Foster 	/* barrier matches the xa_target update in xfs_ail_push() */
3598375f922SBrian Foster 	smp_rmb();
3608375f922SBrian Foster 	target = ailp->xa_target;
3618375f922SBrian Foster 	ailp->xa_target_prev = target;
3628375f922SBrian Foster 
363af3e4022SDave Chinner 	lip = xfs_trans_ail_cursor_first(ailp, &cur, ailp->xa_last_pushed_lsn);
364211e4d43SChristoph Hellwig 	if (!lip) {
3651da177e4SLinus Torvalds 		/*
36643ff2122SChristoph Hellwig 		 * If the AIL is empty or our push has reached the end we are
36743ff2122SChristoph Hellwig 		 * done now.
3681da177e4SLinus Torvalds 		 */
369e4a1e29cSEric Sandeen 		xfs_trans_ail_cursor_done(&cur);
370c7e8f268SDavid Chinner 		spin_unlock(&ailp->xa_lock);
3719e7004e7SDave Chinner 		goto out_done;
3721da177e4SLinus Torvalds 	}
3731da177e4SLinus Torvalds 
374ff6d6af2SBill O'Donnell 	XFS_STATS_INC(mp, xs_push_ail);
3751da177e4SLinus Torvalds 
376249a8c11SDavid Chinner 	lsn = lip->li_lsn;
37750e86686SDave Chinner 	while ((XFS_LSN_CMP(lip->li_lsn, target) <= 0)) {
378249a8c11SDavid Chinner 		int	lock_result;
37943ff2122SChristoph Hellwig 
380249a8c11SDavid Chinner 		/*
381904c17e6SDave Chinner 		 * Note that iop_push may unlock and reacquire the AIL lock.  We
38243ff2122SChristoph Hellwig 		 * rely on the AIL cursor implementation to be able to deal with
38343ff2122SChristoph Hellwig 		 * the dropped lock.
3841da177e4SLinus Torvalds 		 */
385904c17e6SDave Chinner 		lock_result = lip->li_ops->iop_push(lip, &ailp->xa_buf_list);
3861da177e4SLinus Torvalds 		switch (lock_result) {
3871da177e4SLinus Torvalds 		case XFS_ITEM_SUCCESS:
388ff6d6af2SBill O'Donnell 			XFS_STATS_INC(mp, xs_push_ail_success);
3899e4c109aSChristoph Hellwig 			trace_xfs_ail_push(lip);
3909e4c109aSChristoph Hellwig 
3910bf6a5bdSDave Chinner 			ailp->xa_last_pushed_lsn = lsn;
3921da177e4SLinus Torvalds 			break;
3931da177e4SLinus Torvalds 
39443ff2122SChristoph Hellwig 		case XFS_ITEM_FLUSHING:
39543ff2122SChristoph Hellwig 			/*
39643ff2122SChristoph Hellwig 			 * The item or its backing buffer is already beeing
39743ff2122SChristoph Hellwig 			 * flushed.  The typical reason for that is that an
39843ff2122SChristoph Hellwig 			 * inode buffer is locked because we already pushed the
39943ff2122SChristoph Hellwig 			 * updates to it as part of inode clustering.
40043ff2122SChristoph Hellwig 			 *
40143ff2122SChristoph Hellwig 			 * We do not want to to stop flushing just because lots
40243ff2122SChristoph Hellwig 			 * of items are already beeing flushed, but we need to
40343ff2122SChristoph Hellwig 			 * re-try the flushing relatively soon if most of the
40443ff2122SChristoph Hellwig 			 * AIL is beeing flushed.
40543ff2122SChristoph Hellwig 			 */
406ff6d6af2SBill O'Donnell 			XFS_STATS_INC(mp, xs_push_ail_flushing);
40743ff2122SChristoph Hellwig 			trace_xfs_ail_flushing(lip);
40817b38471SChristoph Hellwig 
40943ff2122SChristoph Hellwig 			flushing++;
4100bf6a5bdSDave Chinner 			ailp->xa_last_pushed_lsn = lsn;
4111da177e4SLinus Torvalds 			break;
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 		case XFS_ITEM_PINNED:
414ff6d6af2SBill O'Donnell 			XFS_STATS_INC(mp, xs_push_ail_pinned);
4159e4c109aSChristoph Hellwig 			trace_xfs_ail_pinned(lip);
4169e4c109aSChristoph Hellwig 
417249a8c11SDavid Chinner 			stuck++;
418670ce93fSDave Chinner 			ailp->xa_log_flush++;
4191da177e4SLinus Torvalds 			break;
4201da177e4SLinus Torvalds 		case XFS_ITEM_LOCKED:
421ff6d6af2SBill O'Donnell 			XFS_STATS_INC(mp, xs_push_ail_locked);
4229e4c109aSChristoph Hellwig 			trace_xfs_ail_locked(lip);
42343ff2122SChristoph Hellwig 
424249a8c11SDavid Chinner 			stuck++;
4251da177e4SLinus Torvalds 			break;
4261da177e4SLinus Torvalds 		default:
4271da177e4SLinus Torvalds 			ASSERT(0);
4281da177e4SLinus Torvalds 			break;
4291da177e4SLinus Torvalds 		}
4301da177e4SLinus Torvalds 
431249a8c11SDavid Chinner 		count++;
432249a8c11SDavid Chinner 
433249a8c11SDavid Chinner 		/*
434249a8c11SDavid Chinner 		 * Are there too many items we can't do anything with?
43543ff2122SChristoph Hellwig 		 *
436249a8c11SDavid Chinner 		 * If we we are skipping too many items because we can't flush
437249a8c11SDavid Chinner 		 * them or they are already being flushed, we back off and
438249a8c11SDavid Chinner 		 * given them time to complete whatever operation is being
439249a8c11SDavid Chinner 		 * done. i.e. remove pressure from the AIL while we can't make
440249a8c11SDavid Chinner 		 * progress so traversals don't slow down further inserts and
441249a8c11SDavid Chinner 		 * removals to/from the AIL.
442249a8c11SDavid Chinner 		 *
443249a8c11SDavid Chinner 		 * The value of 100 is an arbitrary magic number based on
444249a8c11SDavid Chinner 		 * observation.
445249a8c11SDavid Chinner 		 */
446249a8c11SDavid Chinner 		if (stuck > 100)
447249a8c11SDavid Chinner 			break;
448249a8c11SDavid Chinner 
449af3e4022SDave Chinner 		lip = xfs_trans_ail_cursor_next(ailp, &cur);
450249a8c11SDavid Chinner 		if (lip == NULL)
451249a8c11SDavid Chinner 			break;
452249a8c11SDavid Chinner 		lsn = lip->li_lsn;
4531da177e4SLinus Torvalds 	}
454e4a1e29cSEric Sandeen 	xfs_trans_ail_cursor_done(&cur);
455c7e8f268SDavid Chinner 	spin_unlock(&ailp->xa_lock);
4561da177e4SLinus Torvalds 
45743ff2122SChristoph Hellwig 	if (xfs_buf_delwri_submit_nowait(&ailp->xa_buf_list))
45843ff2122SChristoph Hellwig 		ailp->xa_log_flush++;
459d808f617SDave Chinner 
46043ff2122SChristoph Hellwig 	if (!count || XFS_LSN_CMP(lsn, target) >= 0) {
4619e7004e7SDave Chinner out_done:
462249a8c11SDavid Chinner 		/*
46343ff2122SChristoph Hellwig 		 * We reached the target or the AIL is empty, so wait a bit
46443ff2122SChristoph Hellwig 		 * longer for I/O to complete and remove pushed items from the
46543ff2122SChristoph Hellwig 		 * AIL before we start the next scan from the start of the AIL.
466249a8c11SDavid Chinner 		 */
467453eac8aSDave Chinner 		tout = 50;
4680bf6a5bdSDave Chinner 		ailp->xa_last_pushed_lsn = 0;
46943ff2122SChristoph Hellwig 	} else if (((stuck + flushing) * 100) / count > 90) {
470249a8c11SDavid Chinner 		/*
47143ff2122SChristoph Hellwig 		 * Either there is a lot of contention on the AIL or we are
47243ff2122SChristoph Hellwig 		 * stuck due to operations in progress. "Stuck" in this case
47343ff2122SChristoph Hellwig 		 * is defined as >90% of the items we tried to push were stuck.
474249a8c11SDavid Chinner 		 *
475249a8c11SDavid Chinner 		 * Backoff a bit more to allow some I/O to complete before
47643ff2122SChristoph Hellwig 		 * restarting from the start of the AIL. This prevents us from
47743ff2122SChristoph Hellwig 		 * spinning on the same items, and if they are pinned will all
47843ff2122SChristoph Hellwig 		 * the restart to issue a log force to unpin the stuck items.
479249a8c11SDavid Chinner 		 */
480453eac8aSDave Chinner 		tout = 20;
481670ce93fSDave Chinner 		ailp->xa_last_pushed_lsn = 0;
48243ff2122SChristoph Hellwig 	} else {
48343ff2122SChristoph Hellwig 		/*
48443ff2122SChristoph Hellwig 		 * Assume we have more work to do in a short while.
48543ff2122SChristoph Hellwig 		 */
48643ff2122SChristoph Hellwig 		tout = 10;
487453eac8aSDave Chinner 	}
4881da177e4SLinus Torvalds 
4890030807cSChristoph Hellwig 	return tout;
4900030807cSChristoph Hellwig }
4910030807cSChristoph Hellwig 
4920030807cSChristoph Hellwig static int
4930030807cSChristoph Hellwig xfsaild(
4940030807cSChristoph Hellwig 	void		*data)
4950030807cSChristoph Hellwig {
4960030807cSChristoph Hellwig 	struct xfs_ail	*ailp = data;
4970030807cSChristoph Hellwig 	long		tout = 0;	/* milliseconds */
4980030807cSChristoph Hellwig 
49943ff2122SChristoph Hellwig 	current->flags |= PF_MEMALLOC;
50018f1df4eSMichal Hocko 	set_freezable();
50143ff2122SChristoph Hellwig 
5020030807cSChristoph Hellwig 	while (!kthread_should_stop()) {
5030030807cSChristoph Hellwig 		if (tout && tout <= 20)
5040030807cSChristoph Hellwig 			__set_current_state(TASK_KILLABLE);
5050030807cSChristoph Hellwig 		else
5060030807cSChristoph Hellwig 			__set_current_state(TASK_INTERRUPTIBLE);
5078375f922SBrian Foster 
5088375f922SBrian Foster 		spin_lock(&ailp->xa_lock);
5098375f922SBrian Foster 
5108375f922SBrian Foster 		/*
5118375f922SBrian Foster 		 * Idle if the AIL is empty and we are not racing with a target
5128375f922SBrian Foster 		 * update. We check the AIL after we set the task to a sleep
5138375f922SBrian Foster 		 * state to guarantee that we either catch an xa_target update
5148375f922SBrian Foster 		 * or that a wake_up resets the state to TASK_RUNNING.
5158375f922SBrian Foster 		 * Otherwise, we run the risk of sleeping indefinitely.
5168375f922SBrian Foster 		 *
5178375f922SBrian Foster 		 * The barrier matches the xa_target update in xfs_ail_push().
5188375f922SBrian Foster 		 */
5198375f922SBrian Foster 		smp_rmb();
5208375f922SBrian Foster 		if (!xfs_ail_min(ailp) &&
5218375f922SBrian Foster 		    ailp->xa_target == ailp->xa_target_prev) {
5228375f922SBrian Foster 			spin_unlock(&ailp->xa_lock);
52318f1df4eSMichal Hocko 			freezable_schedule();
5248375f922SBrian Foster 			tout = 0;
5258375f922SBrian Foster 			continue;
5268375f922SBrian Foster 		}
5278375f922SBrian Foster 		spin_unlock(&ailp->xa_lock);
5288375f922SBrian Foster 
5298375f922SBrian Foster 		if (tout)
53018f1df4eSMichal Hocko 			freezable_schedule_timeout(msecs_to_jiffies(tout));
5318375f922SBrian Foster 
5328375f922SBrian Foster 		__set_current_state(TASK_RUNNING);
5330030807cSChristoph Hellwig 
5340030807cSChristoph Hellwig 		try_to_freeze();
5350030807cSChristoph Hellwig 
5360030807cSChristoph Hellwig 		tout = xfsaild_push(ailp);
5370030807cSChristoph Hellwig 	}
5380030807cSChristoph Hellwig 
5390030807cSChristoph Hellwig 	return 0;
5400bf6a5bdSDave Chinner }
5410bf6a5bdSDave Chinner 
5420bf6a5bdSDave Chinner /*
5430bf6a5bdSDave Chinner  * This routine is called to move the tail of the AIL forward.  It does this by
5440bf6a5bdSDave Chinner  * trying to flush items in the AIL whose lsns are below the given
5450bf6a5bdSDave Chinner  * threshold_lsn.
5460bf6a5bdSDave Chinner  *
5470bf6a5bdSDave Chinner  * The push is run asynchronously in a workqueue, which means the caller needs
5480bf6a5bdSDave Chinner  * to handle waiting on the async flush for space to become available.
5490bf6a5bdSDave Chinner  * We don't want to interrupt any push that is in progress, hence we only queue
5500bf6a5bdSDave Chinner  * work if we set the pushing bit approriately.
5510bf6a5bdSDave Chinner  *
5520bf6a5bdSDave Chinner  * We do this unlocked - we only need to know whether there is anything in the
5530bf6a5bdSDave Chinner  * AIL at the time we are called. We don't need to access the contents of
5540bf6a5bdSDave Chinner  * any of the objects, so the lock is not needed.
5550bf6a5bdSDave Chinner  */
5560bf6a5bdSDave Chinner void
557fd074841SDave Chinner xfs_ail_push(
5580bf6a5bdSDave Chinner 	struct xfs_ail	*ailp,
5590bf6a5bdSDave Chinner 	xfs_lsn_t	threshold_lsn)
5600bf6a5bdSDave Chinner {
5610bf6a5bdSDave Chinner 	xfs_log_item_t	*lip;
5620bf6a5bdSDave Chinner 
5630bf6a5bdSDave Chinner 	lip = xfs_ail_min(ailp);
5640bf6a5bdSDave Chinner 	if (!lip || XFS_FORCED_SHUTDOWN(ailp->xa_mount) ||
5650bf6a5bdSDave Chinner 	    XFS_LSN_CMP(threshold_lsn, ailp->xa_target) <= 0)
5660bf6a5bdSDave Chinner 		return;
5670bf6a5bdSDave Chinner 
5680bf6a5bdSDave Chinner 	/*
5690bf6a5bdSDave Chinner 	 * Ensure that the new target is noticed in push code before it clears
5700bf6a5bdSDave Chinner 	 * the XFS_AIL_PUSHING_BIT.
5710bf6a5bdSDave Chinner 	 */
5720bf6a5bdSDave Chinner 	smp_wmb();
573fe0da767SDave Chinner 	xfs_trans_ail_copy_lsn(ailp, &ailp->xa_target, &threshold_lsn);
5740030807cSChristoph Hellwig 	smp_wmb();
5750030807cSChristoph Hellwig 
5760030807cSChristoph Hellwig 	wake_up_process(ailp->xa_task);
5770bf6a5bdSDave Chinner }
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds /*
580fd074841SDave Chinner  * Push out all items in the AIL immediately
581fd074841SDave Chinner  */
582fd074841SDave Chinner void
583fd074841SDave Chinner xfs_ail_push_all(
584fd074841SDave Chinner 	struct xfs_ail  *ailp)
585fd074841SDave Chinner {
586fd074841SDave Chinner 	xfs_lsn_t       threshold_lsn = xfs_ail_max_lsn(ailp);
587fd074841SDave Chinner 
588fd074841SDave Chinner 	if (threshold_lsn)
589fd074841SDave Chinner 		xfs_ail_push(ailp, threshold_lsn);
590fd074841SDave Chinner }
591fd074841SDave Chinner 
592fd074841SDave Chinner /*
593211e4d43SChristoph Hellwig  * Push out all items in the AIL immediately and wait until the AIL is empty.
594211e4d43SChristoph Hellwig  */
595211e4d43SChristoph Hellwig void
596211e4d43SChristoph Hellwig xfs_ail_push_all_sync(
597211e4d43SChristoph Hellwig 	struct xfs_ail  *ailp)
598211e4d43SChristoph Hellwig {
599211e4d43SChristoph Hellwig 	struct xfs_log_item	*lip;
600211e4d43SChristoph Hellwig 	DEFINE_WAIT(wait);
601211e4d43SChristoph Hellwig 
602211e4d43SChristoph Hellwig 	spin_lock(&ailp->xa_lock);
603211e4d43SChristoph Hellwig 	while ((lip = xfs_ail_max(ailp)) != NULL) {
604211e4d43SChristoph Hellwig 		prepare_to_wait(&ailp->xa_empty, &wait, TASK_UNINTERRUPTIBLE);
605211e4d43SChristoph Hellwig 		ailp->xa_target = lip->li_lsn;
606211e4d43SChristoph Hellwig 		wake_up_process(ailp->xa_task);
607211e4d43SChristoph Hellwig 		spin_unlock(&ailp->xa_lock);
608211e4d43SChristoph Hellwig 		schedule();
609211e4d43SChristoph Hellwig 		spin_lock(&ailp->xa_lock);
610211e4d43SChristoph Hellwig 	}
611211e4d43SChristoph Hellwig 	spin_unlock(&ailp->xa_lock);
612211e4d43SChristoph Hellwig 
613211e4d43SChristoph Hellwig 	finish_wait(&ailp->xa_empty, &wait);
614211e4d43SChristoph Hellwig }
615211e4d43SChristoph Hellwig 
616211e4d43SChristoph Hellwig /*
6170e57f6a3SDave Chinner  * xfs_trans_ail_update - bulk AIL insertion operation.
6180e57f6a3SDave Chinner  *
6190e57f6a3SDave Chinner  * @xfs_trans_ail_update takes an array of log items that all need to be
6200e57f6a3SDave Chinner  * positioned at the same LSN in the AIL. If an item is not in the AIL, it will
6210e57f6a3SDave Chinner  * be added.  Otherwise, it will be repositioned  by removing it and re-adding
6220e57f6a3SDave Chinner  * it to the AIL. If we move the first item in the AIL, update the log tail to
6230e57f6a3SDave Chinner  * match the new minimum LSN in the AIL.
6240e57f6a3SDave Chinner  *
6250e57f6a3SDave Chinner  * This function takes the AIL lock once to execute the update operations on
6260e57f6a3SDave Chinner  * all the items in the array, and as such should not be called with the AIL
6270e57f6a3SDave Chinner  * lock held. As a result, once we have the AIL lock, we need to check each log
6280e57f6a3SDave Chinner  * item LSN to confirm it needs to be moved forward in the AIL.
6290e57f6a3SDave Chinner  *
6300e57f6a3SDave Chinner  * To optimise the insert operation, we delete all the items from the AIL in
6310e57f6a3SDave Chinner  * the first pass, moving them into a temporary list, then splice the temporary
6320e57f6a3SDave Chinner  * list into the correct position in the AIL. This avoids needing to do an
6330e57f6a3SDave Chinner  * insert operation on every item.
6340e57f6a3SDave Chinner  *
6350e57f6a3SDave Chinner  * This function must be called with the AIL lock held.  The lock is dropped
6360e57f6a3SDave Chinner  * before returning.
6370e57f6a3SDave Chinner  */
6380e57f6a3SDave Chinner void
6390e57f6a3SDave Chinner xfs_trans_ail_update_bulk(
6400e57f6a3SDave Chinner 	struct xfs_ail		*ailp,
6411d8c95a3SDave Chinner 	struct xfs_ail_cursor	*cur,
6420e57f6a3SDave Chinner 	struct xfs_log_item	**log_items,
6430e57f6a3SDave Chinner 	int			nr_items,
6440e57f6a3SDave Chinner 	xfs_lsn_t		lsn) __releases(ailp->xa_lock)
6450e57f6a3SDave Chinner {
6460e57f6a3SDave Chinner 	xfs_log_item_t		*mlip;
6470e57f6a3SDave Chinner 	int			mlip_changed = 0;
6480e57f6a3SDave Chinner 	int			i;
6490e57f6a3SDave Chinner 	LIST_HEAD(tmp);
6500e57f6a3SDave Chinner 
651e44f4112SAlex Elder 	ASSERT(nr_items > 0);		/* Not required, but true. */
6520e57f6a3SDave Chinner 	mlip = xfs_ail_min(ailp);
6530e57f6a3SDave Chinner 
6540e57f6a3SDave Chinner 	for (i = 0; i < nr_items; i++) {
6550e57f6a3SDave Chinner 		struct xfs_log_item *lip = log_items[i];
6560e57f6a3SDave Chinner 		if (lip->li_flags & XFS_LI_IN_AIL) {
6570e57f6a3SDave Chinner 			/* check if we really need to move the item */
6580e57f6a3SDave Chinner 			if (XFS_LSN_CMP(lsn, lip->li_lsn) <= 0)
6590e57f6a3SDave Chinner 				continue;
6600e57f6a3SDave Chinner 
661750b9c90SDave Chinner 			trace_xfs_ail_move(lip, lip->li_lsn, lsn);
6620e57f6a3SDave Chinner 			xfs_ail_delete(ailp, lip);
6630e57f6a3SDave Chinner 			if (mlip == lip)
6640e57f6a3SDave Chinner 				mlip_changed = 1;
6650e57f6a3SDave Chinner 		} else {
6660e57f6a3SDave Chinner 			lip->li_flags |= XFS_LI_IN_AIL;
667750b9c90SDave Chinner 			trace_xfs_ail_insert(lip, 0, lsn);
6680e57f6a3SDave Chinner 		}
6690e57f6a3SDave Chinner 		lip->li_lsn = lsn;
6700e57f6a3SDave Chinner 		list_add(&lip->li_ail, &tmp);
6710e57f6a3SDave Chinner 	}
6720e57f6a3SDave Chinner 
673e44f4112SAlex Elder 	if (!list_empty(&tmp))
6741d8c95a3SDave Chinner 		xfs_ail_splice(ailp, cur, &tmp, lsn);
6751c304625SChristoph Hellwig 
6761c304625SChristoph Hellwig 	if (mlip_changed) {
6771c304625SChristoph Hellwig 		if (!XFS_FORCED_SHUTDOWN(ailp->xa_mount))
6781c304625SChristoph Hellwig 			xlog_assign_tail_lsn_locked(ailp->xa_mount);
6790e57f6a3SDave Chinner 		spin_unlock(&ailp->xa_lock);
68009a423a3SChristoph Hellwig 
681cfb7cdcaSChristoph Hellwig 		xfs_log_space_wake(ailp->xa_mount);
6821c304625SChristoph Hellwig 	} else {
6831c304625SChristoph Hellwig 		spin_unlock(&ailp->xa_lock);
6840e57f6a3SDave Chinner 	}
6850e57f6a3SDave Chinner }
6860e57f6a3SDave Chinner 
68727af1bbfSChristoph Hellwig bool
68827af1bbfSChristoph Hellwig xfs_ail_delete_one(
68927af1bbfSChristoph Hellwig 	struct xfs_ail		*ailp,
69027af1bbfSChristoph Hellwig 	struct xfs_log_item 	*lip)
69127af1bbfSChristoph Hellwig {
69227af1bbfSChristoph Hellwig 	struct xfs_log_item	*mlip = xfs_ail_min(ailp);
69327af1bbfSChristoph Hellwig 
69427af1bbfSChristoph Hellwig 	trace_xfs_ail_delete(lip, mlip->li_lsn, lip->li_lsn);
69527af1bbfSChristoph Hellwig 	xfs_ail_delete(ailp, lip);
69627af1bbfSChristoph Hellwig 	lip->li_flags &= ~XFS_LI_IN_AIL;
69727af1bbfSChristoph Hellwig 	lip->li_lsn = 0;
69827af1bbfSChristoph Hellwig 
69927af1bbfSChristoph Hellwig 	return mlip == lip;
70027af1bbfSChristoph Hellwig }
70127af1bbfSChristoph Hellwig 
70227af1bbfSChristoph Hellwig /**
70327af1bbfSChristoph Hellwig  * Remove a log items from the AIL
70430136832SDave Chinner  *
70530136832SDave Chinner  * @xfs_trans_ail_delete_bulk takes an array of log items that all need to
70630136832SDave Chinner  * removed from the AIL. The caller is already holding the AIL lock, and done
70730136832SDave Chinner  * all the checks necessary to ensure the items passed in via @log_items are
70830136832SDave Chinner  * ready for deletion. This includes checking that the items are in the AIL.
70930136832SDave Chinner  *
71030136832SDave Chinner  * For each log item to be removed, unlink it  from the AIL, clear the IN_AIL
71130136832SDave Chinner  * flag from the item and reset the item's lsn to 0. If we remove the first
71230136832SDave Chinner  * item in the AIL, update the log tail to match the new minimum LSN in the
71330136832SDave Chinner  * AIL.
71430136832SDave Chinner  *
71530136832SDave Chinner  * This function will not drop the AIL lock until all items are removed from
71630136832SDave Chinner  * the AIL to minimise the amount of lock traffic on the AIL. This does not
71730136832SDave Chinner  * greatly increase the AIL hold time, but does significantly reduce the amount
71830136832SDave Chinner  * of traffic on the lock, especially during IO completion.
71930136832SDave Chinner  *
72030136832SDave Chinner  * This function must be called with the AIL lock held.  The lock is dropped
72130136832SDave Chinner  * before returning.
72230136832SDave Chinner  */
72330136832SDave Chinner void
72427af1bbfSChristoph Hellwig xfs_trans_ail_delete(
72530136832SDave Chinner 	struct xfs_ail		*ailp,
72627af1bbfSChristoph Hellwig 	struct xfs_log_item	*lip,
72704913fddSDave Chinner 	int			shutdown_type) __releases(ailp->xa_lock)
72830136832SDave Chinner {
72930136832SDave Chinner 	struct xfs_mount	*mp = ailp->xa_mount;
73027af1bbfSChristoph Hellwig 	bool			mlip_changed;
73130136832SDave Chinner 
73227af1bbfSChristoph Hellwig 	if (!(lip->li_flags & XFS_LI_IN_AIL)) {
73330136832SDave Chinner 		spin_unlock(&ailp->xa_lock);
73430136832SDave Chinner 		if (!XFS_FORCED_SHUTDOWN(mp)) {
7356a19d939SDave Chinner 			xfs_alert_tag(mp, XFS_PTAG_AILDELETE,
73630136832SDave Chinner 	"%s: attempting to delete a log item that is not in the AIL",
73730136832SDave Chinner 					__func__);
73804913fddSDave Chinner 			xfs_force_shutdown(mp, shutdown_type);
73930136832SDave Chinner 		}
74030136832SDave Chinner 		return;
74130136832SDave Chinner 	}
74230136832SDave Chinner 
74327af1bbfSChristoph Hellwig 	mlip_changed = xfs_ail_delete_one(ailp, lip);
7441c304625SChristoph Hellwig 	if (mlip_changed) {
74527af1bbfSChristoph Hellwig 		if (!XFS_FORCED_SHUTDOWN(mp))
74627af1bbfSChristoph Hellwig 			xlog_assign_tail_lsn_locked(mp);
747211e4d43SChristoph Hellwig 		if (list_empty(&ailp->xa_ail))
748211e4d43SChristoph Hellwig 			wake_up_all(&ailp->xa_empty);
74930136832SDave Chinner 	}
75027af1bbfSChristoph Hellwig 
75127af1bbfSChristoph Hellwig 	spin_unlock(&ailp->xa_lock);
75227af1bbfSChristoph Hellwig 	if (mlip_changed)
75327af1bbfSChristoph Hellwig 		xfs_log_space_wake(ailp->xa_mount);
75430136832SDave Chinner }
7551da177e4SLinus Torvalds 
756249a8c11SDavid Chinner int
7571da177e4SLinus Torvalds xfs_trans_ail_init(
7581da177e4SLinus Torvalds 	xfs_mount_t	*mp)
7591da177e4SLinus Torvalds {
76082fa9012SDavid Chinner 	struct xfs_ail	*ailp;
76182fa9012SDavid Chinner 
76282fa9012SDavid Chinner 	ailp = kmem_zalloc(sizeof(struct xfs_ail), KM_MAYFAIL);
76382fa9012SDavid Chinner 	if (!ailp)
7642451337dSDave Chinner 		return -ENOMEM;
76582fa9012SDavid Chinner 
76682fa9012SDavid Chinner 	ailp->xa_mount = mp;
76782fa9012SDavid Chinner 	INIT_LIST_HEAD(&ailp->xa_ail);
768af3e4022SDave Chinner 	INIT_LIST_HEAD(&ailp->xa_cursors);
769c7e8f268SDavid Chinner 	spin_lock_init(&ailp->xa_lock);
77043ff2122SChristoph Hellwig 	INIT_LIST_HEAD(&ailp->xa_buf_list);
771211e4d43SChristoph Hellwig 	init_waitqueue_head(&ailp->xa_empty);
7720030807cSChristoph Hellwig 
7730030807cSChristoph Hellwig 	ailp->xa_task = kthread_run(xfsaild, ailp, "xfsaild/%s",
7740030807cSChristoph Hellwig 			ailp->xa_mount->m_fsname);
7750030807cSChristoph Hellwig 	if (IS_ERR(ailp->xa_task))
7760030807cSChristoph Hellwig 		goto out_free_ailp;
7770030807cSChristoph Hellwig 
77827d8d5feSDavid Chinner 	mp->m_ail = ailp;
77927d8d5feSDavid Chinner 	return 0;
7800030807cSChristoph Hellwig 
7810030807cSChristoph Hellwig out_free_ailp:
7820030807cSChristoph Hellwig 	kmem_free(ailp);
7832451337dSDave Chinner 	return -ENOMEM;
784249a8c11SDavid Chinner }
785249a8c11SDavid Chinner 
786249a8c11SDavid Chinner void
787249a8c11SDavid Chinner xfs_trans_ail_destroy(
788249a8c11SDavid Chinner 	xfs_mount_t	*mp)
789249a8c11SDavid Chinner {
79082fa9012SDavid Chinner 	struct xfs_ail	*ailp = mp->m_ail;
79182fa9012SDavid Chinner 
7920030807cSChristoph Hellwig 	kthread_stop(ailp->xa_task);
79382fa9012SDavid Chinner 	kmem_free(ailp);
7941da177e4SLinus Torvalds }
795