xref: /openbmc/linux/fs/xfs/xfs_trans_ail.c (revision 7f4d01f3)
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 
3287f4d01f3SBrian Foster static inline uint
3297f4d01f3SBrian Foster xfsaild_push_item(
3307f4d01f3SBrian Foster 	struct xfs_ail		*ailp,
3317f4d01f3SBrian Foster 	struct xfs_log_item	*lip)
3327f4d01f3SBrian Foster {
3337f4d01f3SBrian Foster 	/*
3347f4d01f3SBrian Foster 	 * If log item pinning is enabled, skip the push and track the item as
3357f4d01f3SBrian Foster 	 * pinned. This can help induce head-behind-tail conditions.
3367f4d01f3SBrian Foster 	 */
3377f4d01f3SBrian Foster 	if (XFS_TEST_ERROR(false, ailp->xa_mount, XFS_ERRTAG_LOG_ITEM_PIN))
3387f4d01f3SBrian Foster 		return XFS_ITEM_PINNED;
3397f4d01f3SBrian Foster 
3407f4d01f3SBrian Foster 	return lip->li_ops->iop_push(lip, &ailp->xa_buf_list);
3417f4d01f3SBrian Foster }
3427f4d01f3SBrian Foster 
3430030807cSChristoph Hellwig static long
3440030807cSChristoph Hellwig xfsaild_push(
3450030807cSChristoph Hellwig 	struct xfs_ail		*ailp)
346249a8c11SDavid Chinner {
34782fa9012SDavid Chinner 	xfs_mount_t		*mp = ailp->xa_mount;
348af3e4022SDave Chinner 	struct xfs_ail_cursor	cur;
3499e7004e7SDave Chinner 	xfs_log_item_t		*lip;
3509e7004e7SDave Chinner 	xfs_lsn_t		lsn;
351fe0da767SDave Chinner 	xfs_lsn_t		target;
35243ff2122SChristoph Hellwig 	long			tout;
3539e7004e7SDave Chinner 	int			stuck = 0;
35443ff2122SChristoph Hellwig 	int			flushing = 0;
3559e7004e7SDave Chinner 	int			count = 0;
3561da177e4SLinus Torvalds 
357670ce93fSDave Chinner 	/*
35843ff2122SChristoph Hellwig 	 * If we encountered pinned items or did not finish writing out all
35943ff2122SChristoph Hellwig 	 * buffers the last time we ran, force the log first and wait for it
36043ff2122SChristoph Hellwig 	 * before pushing again.
361670ce93fSDave Chinner 	 */
36243ff2122SChristoph Hellwig 	if (ailp->xa_log_flush && ailp->xa_last_pushed_lsn == 0 &&
36343ff2122SChristoph Hellwig 	    (!list_empty_careful(&ailp->xa_buf_list) ||
36443ff2122SChristoph Hellwig 	     xfs_ail_min_lsn(ailp))) {
365670ce93fSDave Chinner 		ailp->xa_log_flush = 0;
36643ff2122SChristoph Hellwig 
367ff6d6af2SBill O'Donnell 		XFS_STATS_INC(mp, xs_push_ail_flush);
368670ce93fSDave Chinner 		xfs_log_force(mp, XFS_LOG_SYNC);
369670ce93fSDave Chinner 	}
370670ce93fSDave Chinner 
37143ff2122SChristoph Hellwig 	spin_lock(&ailp->xa_lock);
3728375f922SBrian Foster 
3738375f922SBrian Foster 	/* barrier matches the xa_target update in xfs_ail_push() */
3748375f922SBrian Foster 	smp_rmb();
3758375f922SBrian Foster 	target = ailp->xa_target;
3768375f922SBrian Foster 	ailp->xa_target_prev = target;
3778375f922SBrian Foster 
378af3e4022SDave Chinner 	lip = xfs_trans_ail_cursor_first(ailp, &cur, ailp->xa_last_pushed_lsn);
379211e4d43SChristoph Hellwig 	if (!lip) {
3801da177e4SLinus Torvalds 		/*
38143ff2122SChristoph Hellwig 		 * If the AIL is empty or our push has reached the end we are
38243ff2122SChristoph Hellwig 		 * done now.
3831da177e4SLinus Torvalds 		 */
384e4a1e29cSEric Sandeen 		xfs_trans_ail_cursor_done(&cur);
385c7e8f268SDavid Chinner 		spin_unlock(&ailp->xa_lock);
3869e7004e7SDave Chinner 		goto out_done;
3871da177e4SLinus Torvalds 	}
3881da177e4SLinus Torvalds 
389ff6d6af2SBill O'Donnell 	XFS_STATS_INC(mp, xs_push_ail);
3901da177e4SLinus Torvalds 
391249a8c11SDavid Chinner 	lsn = lip->li_lsn;
39250e86686SDave Chinner 	while ((XFS_LSN_CMP(lip->li_lsn, target) <= 0)) {
393249a8c11SDavid Chinner 		int	lock_result;
39443ff2122SChristoph Hellwig 
395249a8c11SDavid Chinner 		/*
396904c17e6SDave Chinner 		 * Note that iop_push may unlock and reacquire the AIL lock.  We
39743ff2122SChristoph Hellwig 		 * rely on the AIL cursor implementation to be able to deal with
39843ff2122SChristoph Hellwig 		 * the dropped lock.
3991da177e4SLinus Torvalds 		 */
4007f4d01f3SBrian Foster 		lock_result = xfsaild_push_item(ailp, lip);
4011da177e4SLinus Torvalds 		switch (lock_result) {
4021da177e4SLinus Torvalds 		case XFS_ITEM_SUCCESS:
403ff6d6af2SBill O'Donnell 			XFS_STATS_INC(mp, xs_push_ail_success);
4049e4c109aSChristoph Hellwig 			trace_xfs_ail_push(lip);
4059e4c109aSChristoph Hellwig 
4060bf6a5bdSDave Chinner 			ailp->xa_last_pushed_lsn = lsn;
4071da177e4SLinus Torvalds 			break;
4081da177e4SLinus Torvalds 
40943ff2122SChristoph Hellwig 		case XFS_ITEM_FLUSHING:
41043ff2122SChristoph Hellwig 			/*
41143ff2122SChristoph Hellwig 			 * The item or its backing buffer is already beeing
41243ff2122SChristoph Hellwig 			 * flushed.  The typical reason for that is that an
41343ff2122SChristoph Hellwig 			 * inode buffer is locked because we already pushed the
41443ff2122SChristoph Hellwig 			 * updates to it as part of inode clustering.
41543ff2122SChristoph Hellwig 			 *
41643ff2122SChristoph Hellwig 			 * We do not want to to stop flushing just because lots
41743ff2122SChristoph Hellwig 			 * of items are already beeing flushed, but we need to
41843ff2122SChristoph Hellwig 			 * re-try the flushing relatively soon if most of the
41943ff2122SChristoph Hellwig 			 * AIL is beeing flushed.
42043ff2122SChristoph Hellwig 			 */
421ff6d6af2SBill O'Donnell 			XFS_STATS_INC(mp, xs_push_ail_flushing);
42243ff2122SChristoph Hellwig 			trace_xfs_ail_flushing(lip);
42317b38471SChristoph Hellwig 
42443ff2122SChristoph Hellwig 			flushing++;
4250bf6a5bdSDave Chinner 			ailp->xa_last_pushed_lsn = lsn;
4261da177e4SLinus Torvalds 			break;
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds 		case XFS_ITEM_PINNED:
429ff6d6af2SBill O'Donnell 			XFS_STATS_INC(mp, xs_push_ail_pinned);
4309e4c109aSChristoph Hellwig 			trace_xfs_ail_pinned(lip);
4319e4c109aSChristoph Hellwig 
432249a8c11SDavid Chinner 			stuck++;
433670ce93fSDave Chinner 			ailp->xa_log_flush++;
4341da177e4SLinus Torvalds 			break;
4351da177e4SLinus Torvalds 		case XFS_ITEM_LOCKED:
436ff6d6af2SBill O'Donnell 			XFS_STATS_INC(mp, xs_push_ail_locked);
4379e4c109aSChristoph Hellwig 			trace_xfs_ail_locked(lip);
43843ff2122SChristoph Hellwig 
439249a8c11SDavid Chinner 			stuck++;
4401da177e4SLinus Torvalds 			break;
4411da177e4SLinus Torvalds 		default:
4421da177e4SLinus Torvalds 			ASSERT(0);
4431da177e4SLinus Torvalds 			break;
4441da177e4SLinus Torvalds 		}
4451da177e4SLinus Torvalds 
446249a8c11SDavid Chinner 		count++;
447249a8c11SDavid Chinner 
448249a8c11SDavid Chinner 		/*
449249a8c11SDavid Chinner 		 * Are there too many items we can't do anything with?
45043ff2122SChristoph Hellwig 		 *
451249a8c11SDavid Chinner 		 * If we we are skipping too many items because we can't flush
452249a8c11SDavid Chinner 		 * them or they are already being flushed, we back off and
453249a8c11SDavid Chinner 		 * given them time to complete whatever operation is being
454249a8c11SDavid Chinner 		 * done. i.e. remove pressure from the AIL while we can't make
455249a8c11SDavid Chinner 		 * progress so traversals don't slow down further inserts and
456249a8c11SDavid Chinner 		 * removals to/from the AIL.
457249a8c11SDavid Chinner 		 *
458249a8c11SDavid Chinner 		 * The value of 100 is an arbitrary magic number based on
459249a8c11SDavid Chinner 		 * observation.
460249a8c11SDavid Chinner 		 */
461249a8c11SDavid Chinner 		if (stuck > 100)
462249a8c11SDavid Chinner 			break;
463249a8c11SDavid Chinner 
464af3e4022SDave Chinner 		lip = xfs_trans_ail_cursor_next(ailp, &cur);
465249a8c11SDavid Chinner 		if (lip == NULL)
466249a8c11SDavid Chinner 			break;
467249a8c11SDavid Chinner 		lsn = lip->li_lsn;
4681da177e4SLinus Torvalds 	}
469e4a1e29cSEric Sandeen 	xfs_trans_ail_cursor_done(&cur);
470c7e8f268SDavid Chinner 	spin_unlock(&ailp->xa_lock);
4711da177e4SLinus Torvalds 
47243ff2122SChristoph Hellwig 	if (xfs_buf_delwri_submit_nowait(&ailp->xa_buf_list))
47343ff2122SChristoph Hellwig 		ailp->xa_log_flush++;
474d808f617SDave Chinner 
47543ff2122SChristoph Hellwig 	if (!count || XFS_LSN_CMP(lsn, target) >= 0) {
4769e7004e7SDave Chinner out_done:
477249a8c11SDavid Chinner 		/*
47843ff2122SChristoph Hellwig 		 * We reached the target or the AIL is empty, so wait a bit
47943ff2122SChristoph Hellwig 		 * longer for I/O to complete and remove pushed items from the
48043ff2122SChristoph Hellwig 		 * AIL before we start the next scan from the start of the AIL.
481249a8c11SDavid Chinner 		 */
482453eac8aSDave Chinner 		tout = 50;
4830bf6a5bdSDave Chinner 		ailp->xa_last_pushed_lsn = 0;
48443ff2122SChristoph Hellwig 	} else if (((stuck + flushing) * 100) / count > 90) {
485249a8c11SDavid Chinner 		/*
48643ff2122SChristoph Hellwig 		 * Either there is a lot of contention on the AIL or we are
48743ff2122SChristoph Hellwig 		 * stuck due to operations in progress. "Stuck" in this case
48843ff2122SChristoph Hellwig 		 * is defined as >90% of the items we tried to push were stuck.
489249a8c11SDavid Chinner 		 *
490249a8c11SDavid Chinner 		 * Backoff a bit more to allow some I/O to complete before
49143ff2122SChristoph Hellwig 		 * restarting from the start of the AIL. This prevents us from
49243ff2122SChristoph Hellwig 		 * spinning on the same items, and if they are pinned will all
49343ff2122SChristoph Hellwig 		 * the restart to issue a log force to unpin the stuck items.
494249a8c11SDavid Chinner 		 */
495453eac8aSDave Chinner 		tout = 20;
496670ce93fSDave Chinner 		ailp->xa_last_pushed_lsn = 0;
49743ff2122SChristoph Hellwig 	} else {
49843ff2122SChristoph Hellwig 		/*
49943ff2122SChristoph Hellwig 		 * Assume we have more work to do in a short while.
50043ff2122SChristoph Hellwig 		 */
50143ff2122SChristoph Hellwig 		tout = 10;
502453eac8aSDave Chinner 	}
5031da177e4SLinus Torvalds 
5040030807cSChristoph Hellwig 	return tout;
5050030807cSChristoph Hellwig }
5060030807cSChristoph Hellwig 
5070030807cSChristoph Hellwig static int
5080030807cSChristoph Hellwig xfsaild(
5090030807cSChristoph Hellwig 	void		*data)
5100030807cSChristoph Hellwig {
5110030807cSChristoph Hellwig 	struct xfs_ail	*ailp = data;
5120030807cSChristoph Hellwig 	long		tout = 0;	/* milliseconds */
5130030807cSChristoph Hellwig 
51443ff2122SChristoph Hellwig 	current->flags |= PF_MEMALLOC;
51518f1df4eSMichal Hocko 	set_freezable();
51643ff2122SChristoph Hellwig 
5170030807cSChristoph Hellwig 	while (!kthread_should_stop()) {
5180030807cSChristoph Hellwig 		if (tout && tout <= 20)
5190030807cSChristoph Hellwig 			__set_current_state(TASK_KILLABLE);
5200030807cSChristoph Hellwig 		else
5210030807cSChristoph Hellwig 			__set_current_state(TASK_INTERRUPTIBLE);
5228375f922SBrian Foster 
5238375f922SBrian Foster 		spin_lock(&ailp->xa_lock);
5248375f922SBrian Foster 
5258375f922SBrian Foster 		/*
5268375f922SBrian Foster 		 * Idle if the AIL is empty and we are not racing with a target
5278375f922SBrian Foster 		 * update. We check the AIL after we set the task to a sleep
5288375f922SBrian Foster 		 * state to guarantee that we either catch an xa_target update
5298375f922SBrian Foster 		 * or that a wake_up resets the state to TASK_RUNNING.
5308375f922SBrian Foster 		 * Otherwise, we run the risk of sleeping indefinitely.
5318375f922SBrian Foster 		 *
5328375f922SBrian Foster 		 * The barrier matches the xa_target update in xfs_ail_push().
5338375f922SBrian Foster 		 */
5348375f922SBrian Foster 		smp_rmb();
5358375f922SBrian Foster 		if (!xfs_ail_min(ailp) &&
5368375f922SBrian Foster 		    ailp->xa_target == ailp->xa_target_prev) {
5378375f922SBrian Foster 			spin_unlock(&ailp->xa_lock);
53818f1df4eSMichal Hocko 			freezable_schedule();
5398375f922SBrian Foster 			tout = 0;
5408375f922SBrian Foster 			continue;
5418375f922SBrian Foster 		}
5428375f922SBrian Foster 		spin_unlock(&ailp->xa_lock);
5438375f922SBrian Foster 
5448375f922SBrian Foster 		if (tout)
54518f1df4eSMichal Hocko 			freezable_schedule_timeout(msecs_to_jiffies(tout));
5468375f922SBrian Foster 
5478375f922SBrian Foster 		__set_current_state(TASK_RUNNING);
5480030807cSChristoph Hellwig 
5490030807cSChristoph Hellwig 		try_to_freeze();
5500030807cSChristoph Hellwig 
5510030807cSChristoph Hellwig 		tout = xfsaild_push(ailp);
5520030807cSChristoph Hellwig 	}
5530030807cSChristoph Hellwig 
5540030807cSChristoph Hellwig 	return 0;
5550bf6a5bdSDave Chinner }
5560bf6a5bdSDave Chinner 
5570bf6a5bdSDave Chinner /*
5580bf6a5bdSDave Chinner  * This routine is called to move the tail of the AIL forward.  It does this by
5590bf6a5bdSDave Chinner  * trying to flush items in the AIL whose lsns are below the given
5600bf6a5bdSDave Chinner  * threshold_lsn.
5610bf6a5bdSDave Chinner  *
5620bf6a5bdSDave Chinner  * The push is run asynchronously in a workqueue, which means the caller needs
5630bf6a5bdSDave Chinner  * to handle waiting on the async flush for space to become available.
5640bf6a5bdSDave Chinner  * We don't want to interrupt any push that is in progress, hence we only queue
5650bf6a5bdSDave Chinner  * work if we set the pushing bit approriately.
5660bf6a5bdSDave Chinner  *
5670bf6a5bdSDave Chinner  * We do this unlocked - we only need to know whether there is anything in the
5680bf6a5bdSDave Chinner  * AIL at the time we are called. We don't need to access the contents of
5690bf6a5bdSDave Chinner  * any of the objects, so the lock is not needed.
5700bf6a5bdSDave Chinner  */
5710bf6a5bdSDave Chinner void
572fd074841SDave Chinner xfs_ail_push(
5730bf6a5bdSDave Chinner 	struct xfs_ail	*ailp,
5740bf6a5bdSDave Chinner 	xfs_lsn_t	threshold_lsn)
5750bf6a5bdSDave Chinner {
5760bf6a5bdSDave Chinner 	xfs_log_item_t	*lip;
5770bf6a5bdSDave Chinner 
5780bf6a5bdSDave Chinner 	lip = xfs_ail_min(ailp);
5790bf6a5bdSDave Chinner 	if (!lip || XFS_FORCED_SHUTDOWN(ailp->xa_mount) ||
5800bf6a5bdSDave Chinner 	    XFS_LSN_CMP(threshold_lsn, ailp->xa_target) <= 0)
5810bf6a5bdSDave Chinner 		return;
5820bf6a5bdSDave Chinner 
5830bf6a5bdSDave Chinner 	/*
5840bf6a5bdSDave Chinner 	 * Ensure that the new target is noticed in push code before it clears
5850bf6a5bdSDave Chinner 	 * the XFS_AIL_PUSHING_BIT.
5860bf6a5bdSDave Chinner 	 */
5870bf6a5bdSDave Chinner 	smp_wmb();
588fe0da767SDave Chinner 	xfs_trans_ail_copy_lsn(ailp, &ailp->xa_target, &threshold_lsn);
5890030807cSChristoph Hellwig 	smp_wmb();
5900030807cSChristoph Hellwig 
5910030807cSChristoph Hellwig 	wake_up_process(ailp->xa_task);
5920bf6a5bdSDave Chinner }
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds /*
595fd074841SDave Chinner  * Push out all items in the AIL immediately
596fd074841SDave Chinner  */
597fd074841SDave Chinner void
598fd074841SDave Chinner xfs_ail_push_all(
599fd074841SDave Chinner 	struct xfs_ail  *ailp)
600fd074841SDave Chinner {
601fd074841SDave Chinner 	xfs_lsn_t       threshold_lsn = xfs_ail_max_lsn(ailp);
602fd074841SDave Chinner 
603fd074841SDave Chinner 	if (threshold_lsn)
604fd074841SDave Chinner 		xfs_ail_push(ailp, threshold_lsn);
605fd074841SDave Chinner }
606fd074841SDave Chinner 
607fd074841SDave Chinner /*
608211e4d43SChristoph Hellwig  * Push out all items in the AIL immediately and wait until the AIL is empty.
609211e4d43SChristoph Hellwig  */
610211e4d43SChristoph Hellwig void
611211e4d43SChristoph Hellwig xfs_ail_push_all_sync(
612211e4d43SChristoph Hellwig 	struct xfs_ail  *ailp)
613211e4d43SChristoph Hellwig {
614211e4d43SChristoph Hellwig 	struct xfs_log_item	*lip;
615211e4d43SChristoph Hellwig 	DEFINE_WAIT(wait);
616211e4d43SChristoph Hellwig 
617211e4d43SChristoph Hellwig 	spin_lock(&ailp->xa_lock);
618211e4d43SChristoph Hellwig 	while ((lip = xfs_ail_max(ailp)) != NULL) {
619211e4d43SChristoph Hellwig 		prepare_to_wait(&ailp->xa_empty, &wait, TASK_UNINTERRUPTIBLE);
620211e4d43SChristoph Hellwig 		ailp->xa_target = lip->li_lsn;
621211e4d43SChristoph Hellwig 		wake_up_process(ailp->xa_task);
622211e4d43SChristoph Hellwig 		spin_unlock(&ailp->xa_lock);
623211e4d43SChristoph Hellwig 		schedule();
624211e4d43SChristoph Hellwig 		spin_lock(&ailp->xa_lock);
625211e4d43SChristoph Hellwig 	}
626211e4d43SChristoph Hellwig 	spin_unlock(&ailp->xa_lock);
627211e4d43SChristoph Hellwig 
628211e4d43SChristoph Hellwig 	finish_wait(&ailp->xa_empty, &wait);
629211e4d43SChristoph Hellwig }
630211e4d43SChristoph Hellwig 
631211e4d43SChristoph Hellwig /*
6320e57f6a3SDave Chinner  * xfs_trans_ail_update - bulk AIL insertion operation.
6330e57f6a3SDave Chinner  *
6340e57f6a3SDave Chinner  * @xfs_trans_ail_update takes an array of log items that all need to be
6350e57f6a3SDave Chinner  * positioned at the same LSN in the AIL. If an item is not in the AIL, it will
6360e57f6a3SDave Chinner  * be added.  Otherwise, it will be repositioned  by removing it and re-adding
6370e57f6a3SDave Chinner  * it to the AIL. If we move the first item in the AIL, update the log tail to
6380e57f6a3SDave Chinner  * match the new minimum LSN in the AIL.
6390e57f6a3SDave Chinner  *
6400e57f6a3SDave Chinner  * This function takes the AIL lock once to execute the update operations on
6410e57f6a3SDave Chinner  * all the items in the array, and as such should not be called with the AIL
6420e57f6a3SDave Chinner  * lock held. As a result, once we have the AIL lock, we need to check each log
6430e57f6a3SDave Chinner  * item LSN to confirm it needs to be moved forward in the AIL.
6440e57f6a3SDave Chinner  *
6450e57f6a3SDave Chinner  * To optimise the insert operation, we delete all the items from the AIL in
6460e57f6a3SDave Chinner  * the first pass, moving them into a temporary list, then splice the temporary
6470e57f6a3SDave Chinner  * list into the correct position in the AIL. This avoids needing to do an
6480e57f6a3SDave Chinner  * insert operation on every item.
6490e57f6a3SDave Chinner  *
6500e57f6a3SDave Chinner  * This function must be called with the AIL lock held.  The lock is dropped
6510e57f6a3SDave Chinner  * before returning.
6520e57f6a3SDave Chinner  */
6530e57f6a3SDave Chinner void
6540e57f6a3SDave Chinner xfs_trans_ail_update_bulk(
6550e57f6a3SDave Chinner 	struct xfs_ail		*ailp,
6561d8c95a3SDave Chinner 	struct xfs_ail_cursor	*cur,
6570e57f6a3SDave Chinner 	struct xfs_log_item	**log_items,
6580e57f6a3SDave Chinner 	int			nr_items,
6590e57f6a3SDave Chinner 	xfs_lsn_t		lsn) __releases(ailp->xa_lock)
6600e57f6a3SDave Chinner {
6610e57f6a3SDave Chinner 	xfs_log_item_t		*mlip;
6620e57f6a3SDave Chinner 	int			mlip_changed = 0;
6630e57f6a3SDave Chinner 	int			i;
6640e57f6a3SDave Chinner 	LIST_HEAD(tmp);
6650e57f6a3SDave Chinner 
666e44f4112SAlex Elder 	ASSERT(nr_items > 0);		/* Not required, but true. */
6670e57f6a3SDave Chinner 	mlip = xfs_ail_min(ailp);
6680e57f6a3SDave Chinner 
6690e57f6a3SDave Chinner 	for (i = 0; i < nr_items; i++) {
6700e57f6a3SDave Chinner 		struct xfs_log_item *lip = log_items[i];
6710e57f6a3SDave Chinner 		if (lip->li_flags & XFS_LI_IN_AIL) {
6720e57f6a3SDave Chinner 			/* check if we really need to move the item */
6730e57f6a3SDave Chinner 			if (XFS_LSN_CMP(lsn, lip->li_lsn) <= 0)
6740e57f6a3SDave Chinner 				continue;
6750e57f6a3SDave Chinner 
676750b9c90SDave Chinner 			trace_xfs_ail_move(lip, lip->li_lsn, lsn);
6770e57f6a3SDave Chinner 			xfs_ail_delete(ailp, lip);
6780e57f6a3SDave Chinner 			if (mlip == lip)
6790e57f6a3SDave Chinner 				mlip_changed = 1;
6800e57f6a3SDave Chinner 		} else {
6810e57f6a3SDave Chinner 			lip->li_flags |= XFS_LI_IN_AIL;
682750b9c90SDave Chinner 			trace_xfs_ail_insert(lip, 0, lsn);
6830e57f6a3SDave Chinner 		}
6840e57f6a3SDave Chinner 		lip->li_lsn = lsn;
6850e57f6a3SDave Chinner 		list_add(&lip->li_ail, &tmp);
6860e57f6a3SDave Chinner 	}
6870e57f6a3SDave Chinner 
688e44f4112SAlex Elder 	if (!list_empty(&tmp))
6891d8c95a3SDave Chinner 		xfs_ail_splice(ailp, cur, &tmp, lsn);
6901c304625SChristoph Hellwig 
6911c304625SChristoph Hellwig 	if (mlip_changed) {
6921c304625SChristoph Hellwig 		if (!XFS_FORCED_SHUTDOWN(ailp->xa_mount))
6931c304625SChristoph Hellwig 			xlog_assign_tail_lsn_locked(ailp->xa_mount);
6940e57f6a3SDave Chinner 		spin_unlock(&ailp->xa_lock);
69509a423a3SChristoph Hellwig 
696cfb7cdcaSChristoph Hellwig 		xfs_log_space_wake(ailp->xa_mount);
6971c304625SChristoph Hellwig 	} else {
6981c304625SChristoph Hellwig 		spin_unlock(&ailp->xa_lock);
6990e57f6a3SDave Chinner 	}
7000e57f6a3SDave Chinner }
7010e57f6a3SDave Chinner 
70227af1bbfSChristoph Hellwig bool
70327af1bbfSChristoph Hellwig xfs_ail_delete_one(
70427af1bbfSChristoph Hellwig 	struct xfs_ail		*ailp,
70527af1bbfSChristoph Hellwig 	struct xfs_log_item	*lip)
70627af1bbfSChristoph Hellwig {
70727af1bbfSChristoph Hellwig 	struct xfs_log_item	*mlip = xfs_ail_min(ailp);
70827af1bbfSChristoph Hellwig 
70927af1bbfSChristoph Hellwig 	trace_xfs_ail_delete(lip, mlip->li_lsn, lip->li_lsn);
71027af1bbfSChristoph Hellwig 	xfs_ail_delete(ailp, lip);
711d3a304b6SCarlos Maiolino 	xfs_clear_li_failed(lip);
71227af1bbfSChristoph Hellwig 	lip->li_flags &= ~XFS_LI_IN_AIL;
71327af1bbfSChristoph Hellwig 	lip->li_lsn = 0;
71427af1bbfSChristoph Hellwig 
71527af1bbfSChristoph Hellwig 	return mlip == lip;
71627af1bbfSChristoph Hellwig }
71727af1bbfSChristoph Hellwig 
71827af1bbfSChristoph Hellwig /**
71927af1bbfSChristoph Hellwig  * Remove a log items from the AIL
72030136832SDave Chinner  *
72130136832SDave Chinner  * @xfs_trans_ail_delete_bulk takes an array of log items that all need to
72230136832SDave Chinner  * removed from the AIL. The caller is already holding the AIL lock, and done
72330136832SDave Chinner  * all the checks necessary to ensure the items passed in via @log_items are
72430136832SDave Chinner  * ready for deletion. This includes checking that the items are in the AIL.
72530136832SDave Chinner  *
72630136832SDave Chinner  * For each log item to be removed, unlink it  from the AIL, clear the IN_AIL
72730136832SDave Chinner  * flag from the item and reset the item's lsn to 0. If we remove the first
72830136832SDave Chinner  * item in the AIL, update the log tail to match the new minimum LSN in the
72930136832SDave Chinner  * AIL.
73030136832SDave Chinner  *
73130136832SDave Chinner  * This function will not drop the AIL lock until all items are removed from
73230136832SDave Chinner  * the AIL to minimise the amount of lock traffic on the AIL. This does not
73330136832SDave Chinner  * greatly increase the AIL hold time, but does significantly reduce the amount
73430136832SDave Chinner  * of traffic on the lock, especially during IO completion.
73530136832SDave Chinner  *
73630136832SDave Chinner  * This function must be called with the AIL lock held.  The lock is dropped
73730136832SDave Chinner  * before returning.
73830136832SDave Chinner  */
73930136832SDave Chinner void
74027af1bbfSChristoph Hellwig xfs_trans_ail_delete(
74130136832SDave Chinner 	struct xfs_ail		*ailp,
74227af1bbfSChristoph Hellwig 	struct xfs_log_item	*lip,
74304913fddSDave Chinner 	int			shutdown_type) __releases(ailp->xa_lock)
74430136832SDave Chinner {
74530136832SDave Chinner 	struct xfs_mount	*mp = ailp->xa_mount;
74627af1bbfSChristoph Hellwig 	bool			mlip_changed;
74730136832SDave Chinner 
74827af1bbfSChristoph Hellwig 	if (!(lip->li_flags & XFS_LI_IN_AIL)) {
74930136832SDave Chinner 		spin_unlock(&ailp->xa_lock);
75030136832SDave Chinner 		if (!XFS_FORCED_SHUTDOWN(mp)) {
7516a19d939SDave Chinner 			xfs_alert_tag(mp, XFS_PTAG_AILDELETE,
75230136832SDave Chinner 	"%s: attempting to delete a log item that is not in the AIL",
75330136832SDave Chinner 					__func__);
75404913fddSDave Chinner 			xfs_force_shutdown(mp, shutdown_type);
75530136832SDave Chinner 		}
75630136832SDave Chinner 		return;
75730136832SDave Chinner 	}
75830136832SDave Chinner 
75927af1bbfSChristoph Hellwig 	mlip_changed = xfs_ail_delete_one(ailp, lip);
7601c304625SChristoph Hellwig 	if (mlip_changed) {
76127af1bbfSChristoph Hellwig 		if (!XFS_FORCED_SHUTDOWN(mp))
76227af1bbfSChristoph Hellwig 			xlog_assign_tail_lsn_locked(mp);
763211e4d43SChristoph Hellwig 		if (list_empty(&ailp->xa_ail))
764211e4d43SChristoph Hellwig 			wake_up_all(&ailp->xa_empty);
76530136832SDave Chinner 	}
76627af1bbfSChristoph Hellwig 
76727af1bbfSChristoph Hellwig 	spin_unlock(&ailp->xa_lock);
76827af1bbfSChristoph Hellwig 	if (mlip_changed)
76927af1bbfSChristoph Hellwig 		xfs_log_space_wake(ailp->xa_mount);
77030136832SDave Chinner }
7711da177e4SLinus Torvalds 
772249a8c11SDavid Chinner int
7731da177e4SLinus Torvalds xfs_trans_ail_init(
7741da177e4SLinus Torvalds 	xfs_mount_t	*mp)
7751da177e4SLinus Torvalds {
77682fa9012SDavid Chinner 	struct xfs_ail	*ailp;
77782fa9012SDavid Chinner 
77882fa9012SDavid Chinner 	ailp = kmem_zalloc(sizeof(struct xfs_ail), KM_MAYFAIL);
77982fa9012SDavid Chinner 	if (!ailp)
7802451337dSDave Chinner 		return -ENOMEM;
78182fa9012SDavid Chinner 
78282fa9012SDavid Chinner 	ailp->xa_mount = mp;
78382fa9012SDavid Chinner 	INIT_LIST_HEAD(&ailp->xa_ail);
784af3e4022SDave Chinner 	INIT_LIST_HEAD(&ailp->xa_cursors);
785c7e8f268SDavid Chinner 	spin_lock_init(&ailp->xa_lock);
78643ff2122SChristoph Hellwig 	INIT_LIST_HEAD(&ailp->xa_buf_list);
787211e4d43SChristoph Hellwig 	init_waitqueue_head(&ailp->xa_empty);
7880030807cSChristoph Hellwig 
7890030807cSChristoph Hellwig 	ailp->xa_task = kthread_run(xfsaild, ailp, "xfsaild/%s",
7900030807cSChristoph Hellwig 			ailp->xa_mount->m_fsname);
7910030807cSChristoph Hellwig 	if (IS_ERR(ailp->xa_task))
7920030807cSChristoph Hellwig 		goto out_free_ailp;
7930030807cSChristoph Hellwig 
79427d8d5feSDavid Chinner 	mp->m_ail = ailp;
79527d8d5feSDavid Chinner 	return 0;
7960030807cSChristoph Hellwig 
7970030807cSChristoph Hellwig out_free_ailp:
7980030807cSChristoph Hellwig 	kmem_free(ailp);
7992451337dSDave Chinner 	return -ENOMEM;
800249a8c11SDavid Chinner }
801249a8c11SDavid Chinner 
802249a8c11SDavid Chinner void
803249a8c11SDavid Chinner xfs_trans_ail_destroy(
804249a8c11SDavid Chinner 	xfs_mount_t	*mp)
805249a8c11SDavid Chinner {
80682fa9012SDavid Chinner 	struct xfs_ail	*ailp = mp->m_ail;
80782fa9012SDavid Chinner 
8080030807cSChristoph Hellwig 	kthread_stop(ailp->xa_task);
80982fa9012SDavid Chinner 	kmem_free(ailp);
8101da177e4SLinus Torvalds }
811