xref: /openbmc/linux/fs/xfs/xfs_trans_ail.c (revision 8375f922)
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"
211da177e4SLinus Torvalds #include "xfs_types.h"
221da177e4SLinus Torvalds #include "xfs_log.h"
231da177e4SLinus Torvalds #include "xfs_trans.h"
241da177e4SLinus Torvalds #include "xfs_sb.h"
25da353b0dSDavid Chinner #include "xfs_ag.h"
261da177e4SLinus Torvalds #include "xfs_mount.h"
271da177e4SLinus Torvalds #include "xfs_trans_priv.h"
289e4c109aSChristoph Hellwig #include "xfs_trace.h"
291da177e4SLinus Torvalds #include "xfs_error.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 #ifdef XFS_TRANS_DEBUG
59cd4a3c50SDave Chinner 	/*
60cd4a3c50SDave Chinner 	 * Walk the list checking lsn ordering, and that every entry has the
61cd4a3c50SDave Chinner 	 * XFS_LI_IN_AIL flag set. This is really expensive, so only do it
62cd4a3c50SDave Chinner 	 * when specifically debugging the transaction subsystem.
63cd4a3c50SDave Chinner 	 */
64cd4a3c50SDave Chinner 	prev_lip = list_entry(&ailp->xa_ail, xfs_log_item_t, li_ail);
65cd4a3c50SDave Chinner 	list_for_each_entry(lip, &ailp->xa_ail, li_ail) {
66cd4a3c50SDave Chinner 		if (&prev_lip->li_ail != &ailp->xa_ail)
67cd4a3c50SDave Chinner 			ASSERT(XFS_LSN_CMP(prev_lip->li_lsn, lip->li_lsn) <= 0);
68cd4a3c50SDave Chinner 		ASSERT((lip->li_flags & XFS_LI_IN_AIL) != 0);
69cd4a3c50SDave Chinner 		prev_lip = lip;
70cd4a3c50SDave Chinner 	}
71cd4a3c50SDave Chinner #endif /* XFS_TRANS_DEBUG */
72cd4a3c50SDave Chinner }
73cd4a3c50SDave Chinner #else /* !DEBUG */
74de08dbc1SDavid Chinner #define	xfs_ail_check(a,l)
751da177e4SLinus Torvalds #endif /* DEBUG */
761da177e4SLinus Torvalds 
77cd4a3c50SDave Chinner /*
78cd4a3c50SDave Chinner  * Return a pointer to the first item in the AIL.  If the AIL is empty, then
79cd4a3c50SDave Chinner  * return NULL.
80cd4a3c50SDave Chinner  */
811c304625SChristoph Hellwig xfs_log_item_t *
82cd4a3c50SDave Chinner xfs_ail_min(
83cd4a3c50SDave Chinner 	struct xfs_ail  *ailp)
84cd4a3c50SDave Chinner {
85cd4a3c50SDave Chinner 	if (list_empty(&ailp->xa_ail))
86cd4a3c50SDave Chinner 		return NULL;
87cd4a3c50SDave Chinner 
88cd4a3c50SDave Chinner 	return list_first_entry(&ailp->xa_ail, xfs_log_item_t, li_ail);
89cd4a3c50SDave Chinner }
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds  /*
92fd074841SDave Chinner  * Return a pointer to the last item in the AIL.  If the AIL is empty, then
93fd074841SDave Chinner  * return NULL.
94fd074841SDave Chinner  */
95fd074841SDave Chinner static xfs_log_item_t *
96fd074841SDave Chinner xfs_ail_max(
97fd074841SDave Chinner 	struct xfs_ail  *ailp)
98fd074841SDave Chinner {
99fd074841SDave Chinner 	if (list_empty(&ailp->xa_ail))
100fd074841SDave Chinner 		return NULL;
101fd074841SDave Chinner 
102fd074841SDave Chinner 	return list_entry(ailp->xa_ail.prev, xfs_log_item_t, li_ail);
103fd074841SDave Chinner }
104fd074841SDave Chinner 
105fd074841SDave Chinner /*
106cd4a3c50SDave Chinner  * Return a pointer to the item which follows the given item in the AIL.  If
107cd4a3c50SDave Chinner  * the given item is the last item in the list, then return NULL.
108cd4a3c50SDave Chinner  */
109cd4a3c50SDave Chinner static xfs_log_item_t *
110cd4a3c50SDave Chinner xfs_ail_next(
111cd4a3c50SDave Chinner 	struct xfs_ail  *ailp,
112cd4a3c50SDave Chinner 	xfs_log_item_t  *lip)
113cd4a3c50SDave Chinner {
114cd4a3c50SDave Chinner 	if (lip->li_ail.next == &ailp->xa_ail)
115cd4a3c50SDave Chinner 		return NULL;
116cd4a3c50SDave Chinner 
117cd4a3c50SDave Chinner 	return list_first_entry(&lip->li_ail, xfs_log_item_t, li_ail);
118cd4a3c50SDave Chinner }
119cd4a3c50SDave Chinner 
120cd4a3c50SDave Chinner /*
121cd4a3c50SDave Chinner  * This is called by the log manager code to determine the LSN of the tail of
122cd4a3c50SDave Chinner  * the log.  This is exactly the LSN of the first item in the AIL.  If the AIL
123cd4a3c50SDave Chinner  * is empty, then this function returns 0.
1241da177e4SLinus Torvalds  *
125cd4a3c50SDave Chinner  * We need the AIL lock in order to get a coherent read of the lsn of the last
126cd4a3c50SDave Chinner  * item in the AIL.
1271da177e4SLinus Torvalds  */
1281da177e4SLinus Torvalds xfs_lsn_t
129fd074841SDave Chinner xfs_ail_min_lsn(
1305b00f14fSDavid Chinner 	struct xfs_ail	*ailp)
1311da177e4SLinus Torvalds {
132cd4a3c50SDave Chinner 	xfs_lsn_t	lsn = 0;
1331da177e4SLinus Torvalds 	xfs_log_item_t	*lip;
1341da177e4SLinus Torvalds 
135c7e8f268SDavid Chinner 	spin_lock(&ailp->xa_lock);
1365b00f14fSDavid Chinner 	lip = xfs_ail_min(ailp);
137cd4a3c50SDave Chinner 	if (lip)
1381da177e4SLinus Torvalds 		lsn = lip->li_lsn;
139c7e8f268SDavid Chinner 	spin_unlock(&ailp->xa_lock);
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds 	return lsn;
1421da177e4SLinus Torvalds }
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds /*
145fd074841SDave Chinner  * Return the maximum lsn held in the AIL, or zero if the AIL is empty.
146fd074841SDave Chinner  */
147fd074841SDave Chinner static xfs_lsn_t
148fd074841SDave Chinner xfs_ail_max_lsn(
149fd074841SDave Chinner 	struct xfs_ail  *ailp)
150fd074841SDave Chinner {
151fd074841SDave Chinner 	xfs_lsn_t       lsn = 0;
152fd074841SDave Chinner 	xfs_log_item_t  *lip;
153fd074841SDave Chinner 
154fd074841SDave Chinner 	spin_lock(&ailp->xa_lock);
155fd074841SDave Chinner 	lip = xfs_ail_max(ailp);
156fd074841SDave Chinner 	if (lip)
157fd074841SDave Chinner 		lsn = lip->li_lsn;
158fd074841SDave Chinner 	spin_unlock(&ailp->xa_lock);
159fd074841SDave Chinner 
160fd074841SDave Chinner 	return lsn;
161fd074841SDave Chinner }
162fd074841SDave Chinner 
163fd074841SDave Chinner /*
164af3e4022SDave Chinner  * The cursor keeps track of where our current traversal is up to by tracking
165af3e4022SDave Chinner  * the next item in the list for us. However, for this to be safe, removing an
166af3e4022SDave Chinner  * object from the AIL needs to invalidate any cursor that points to it. hence
167af3e4022SDave Chinner  * the traversal cursor needs to be linked to the struct xfs_ail so that
168af3e4022SDave Chinner  * deletion can search all the active cursors for invalidation.
16927d8d5feSDavid Chinner  */
1705b00f14fSDavid Chinner STATIC void
17127d8d5feSDavid Chinner xfs_trans_ail_cursor_init(
17227d8d5feSDavid Chinner 	struct xfs_ail		*ailp,
17327d8d5feSDavid Chinner 	struct xfs_ail_cursor	*cur)
17427d8d5feSDavid Chinner {
17527d8d5feSDavid Chinner 	cur->item = NULL;
176af3e4022SDave Chinner 	list_add_tail(&cur->list, &ailp->xa_cursors);
17727d8d5feSDavid Chinner }
17827d8d5feSDavid Chinner 
17927d8d5feSDavid Chinner /*
180af3e4022SDave Chinner  * Get the next item in the traversal and advance the cursor.  If the cursor
181af3e4022SDave Chinner  * was invalidated (indicated by a lip of 1), restart the traversal.
18227d8d5feSDavid Chinner  */
1835b00f14fSDavid Chinner struct xfs_log_item *
18427d8d5feSDavid Chinner xfs_trans_ail_cursor_next(
18527d8d5feSDavid Chinner 	struct xfs_ail		*ailp,
18627d8d5feSDavid Chinner 	struct xfs_ail_cursor	*cur)
18727d8d5feSDavid Chinner {
18827d8d5feSDavid Chinner 	struct xfs_log_item	*lip = cur->item;
18927d8d5feSDavid Chinner 
19027d8d5feSDavid Chinner 	if ((__psint_t)lip & 1)
19127d8d5feSDavid Chinner 		lip = xfs_ail_min(ailp);
19216b59029SDave Chinner 	if (lip)
19316b59029SDave Chinner 		cur->item = xfs_ail_next(ailp, lip);
19427d8d5feSDavid Chinner 	return lip;
19527d8d5feSDavid Chinner }
19627d8d5feSDavid Chinner 
19727d8d5feSDavid Chinner /*
198af3e4022SDave Chinner  * When the traversal is complete, we need to remove the cursor from the list
199af3e4022SDave Chinner  * of traversing cursors.
20027d8d5feSDavid Chinner  */
20127d8d5feSDavid Chinner void
20227d8d5feSDavid Chinner xfs_trans_ail_cursor_done(
20327d8d5feSDavid Chinner 	struct xfs_ail		*ailp,
204af3e4022SDave Chinner 	struct xfs_ail_cursor	*cur)
20527d8d5feSDavid Chinner {
206af3e4022SDave Chinner 	cur->item = NULL;
207af3e4022SDave Chinner 	list_del_init(&cur->list);
20827d8d5feSDavid Chinner }
20927d8d5feSDavid Chinner 
21027d8d5feSDavid Chinner /*
211af3e4022SDave Chinner  * Invalidate any cursor that is pointing to this item. This is called when an
212af3e4022SDave Chinner  * item is removed from the AIL. Any cursor pointing to this object is now
213af3e4022SDave Chinner  * invalid and the traversal needs to be terminated so it doesn't reference a
214af3e4022SDave Chinner  * freed object. We set the low bit of the cursor item pointer so we can
215af3e4022SDave Chinner  * distinguish between an invalidation and the end of the list when getting the
216af3e4022SDave Chinner  * next item from the cursor.
2175b00f14fSDavid Chinner  */
2185b00f14fSDavid Chinner STATIC void
2195b00f14fSDavid Chinner xfs_trans_ail_cursor_clear(
2205b00f14fSDavid Chinner 	struct xfs_ail		*ailp,
2215b00f14fSDavid Chinner 	struct xfs_log_item	*lip)
2225b00f14fSDavid Chinner {
2235b00f14fSDavid Chinner 	struct xfs_ail_cursor	*cur;
2245b00f14fSDavid Chinner 
225af3e4022SDave Chinner 	list_for_each_entry(cur, &ailp->xa_cursors, list) {
2265b00f14fSDavid Chinner 		if (cur->item == lip)
2275b00f14fSDavid Chinner 			cur->item = (struct xfs_log_item *)
2285b00f14fSDavid Chinner 					((__psint_t)cur->item | 1);
2295b00f14fSDavid Chinner 	}
2305b00f14fSDavid Chinner }
2315b00f14fSDavid Chinner 
2325b00f14fSDavid Chinner /*
23316b59029SDave Chinner  * Find the first item in the AIL with the given @lsn by searching in ascending
23416b59029SDave Chinner  * LSN order and initialise the cursor to point to the next item for a
23516b59029SDave Chinner  * ascending traversal.  Pass a @lsn of zero to initialise the cursor to the
23616b59029SDave Chinner  * first item in the AIL. Returns NULL if the list is empty.
237249a8c11SDavid Chinner  */
2385b00f14fSDavid Chinner xfs_log_item_t *
2395b00f14fSDavid Chinner xfs_trans_ail_cursor_first(
24027d8d5feSDavid Chinner 	struct xfs_ail		*ailp,
24127d8d5feSDavid Chinner 	struct xfs_ail_cursor	*cur,
242249a8c11SDavid Chinner 	xfs_lsn_t		lsn)
243249a8c11SDavid Chinner {
244249a8c11SDavid Chinner 	xfs_log_item_t		*lip;
245249a8c11SDavid Chinner 
2465b00f14fSDavid Chinner 	xfs_trans_ail_cursor_init(ailp, cur);
24716b59029SDave Chinner 
24816b59029SDave Chinner 	if (lsn == 0) {
24927d8d5feSDavid Chinner 		lip = xfs_ail_min(ailp);
2505b00f14fSDavid Chinner 		goto out;
25116b59029SDave Chinner 	}
252249a8c11SDavid Chinner 
25327d8d5feSDavid Chinner 	list_for_each_entry(lip, &ailp->xa_ail, li_ail) {
2545b00f14fSDavid Chinner 		if (XFS_LSN_CMP(lip->li_lsn, lsn) >= 0)
2557ee49acfSDavid Chinner 			goto out;
2565b00f14fSDavid Chinner 	}
25716b59029SDave Chinner 	return NULL;
25816b59029SDave Chinner 
2595b00f14fSDavid Chinner out:
26016b59029SDave Chinner 	if (lip)
26116b59029SDave Chinner 		cur->item = xfs_ail_next(ailp, lip);
262249a8c11SDavid Chinner 	return lip;
263249a8c11SDavid Chinner }
264535f6b37SJosef 'Jeff' Sipek 
2651d8c95a3SDave Chinner static struct xfs_log_item *
2661d8c95a3SDave Chinner __xfs_trans_ail_cursor_last(
2671d8c95a3SDave Chinner 	struct xfs_ail		*ailp,
2681d8c95a3SDave Chinner 	xfs_lsn_t		lsn)
2691d8c95a3SDave Chinner {
2701d8c95a3SDave Chinner 	xfs_log_item_t		*lip;
2711d8c95a3SDave Chinner 
2721d8c95a3SDave Chinner 	list_for_each_entry_reverse(lip, &ailp->xa_ail, li_ail) {
2731d8c95a3SDave Chinner 		if (XFS_LSN_CMP(lip->li_lsn, lsn) <= 0)
2741d8c95a3SDave Chinner 			return lip;
2751d8c95a3SDave Chinner 	}
2761d8c95a3SDave Chinner 	return NULL;
2771d8c95a3SDave Chinner }
2781d8c95a3SDave Chinner 
2791d8c95a3SDave Chinner /*
28016b59029SDave Chinner  * Find the last item in the AIL with the given @lsn by searching in descending
28116b59029SDave Chinner  * LSN order and initialise the cursor to point to that item.  If there is no
28216b59029SDave Chinner  * item with the value of @lsn, then it sets the cursor to the last item with an
28316b59029SDave Chinner  * LSN lower than @lsn.  Returns NULL if the list is empty.
2841d8c95a3SDave Chinner  */
2851d8c95a3SDave Chinner struct xfs_log_item *
2861d8c95a3SDave Chinner xfs_trans_ail_cursor_last(
2871d8c95a3SDave Chinner 	struct xfs_ail		*ailp,
2881d8c95a3SDave Chinner 	struct xfs_ail_cursor	*cur,
2891d8c95a3SDave Chinner 	xfs_lsn_t		lsn)
2901d8c95a3SDave Chinner {
2911d8c95a3SDave Chinner 	xfs_trans_ail_cursor_init(ailp, cur);
2921d8c95a3SDave Chinner 	cur->item = __xfs_trans_ail_cursor_last(ailp, lsn);
2931d8c95a3SDave Chinner 	return cur->item;
2941d8c95a3SDave Chinner }
2951d8c95a3SDave Chinner 
2961d8c95a3SDave Chinner /*
29716b59029SDave Chinner  * Splice the log item list into the AIL at the given LSN. We splice to the
2981d8c95a3SDave Chinner  * tail of the given LSN to maintain insert order for push traversals. The
2991d8c95a3SDave Chinner  * cursor is optional, allowing repeated updates to the same LSN to avoid
300e44f4112SAlex Elder  * repeated traversals.  This should not be called with an empty list.
301cd4a3c50SDave Chinner  */
302cd4a3c50SDave Chinner static void
303cd4a3c50SDave Chinner xfs_ail_splice(
304cd4a3c50SDave Chinner 	struct xfs_ail		*ailp,
3051d8c95a3SDave Chinner 	struct xfs_ail_cursor	*cur,
306cd4a3c50SDave Chinner 	struct list_head	*list,
307cd4a3c50SDave Chinner 	xfs_lsn_t		lsn)
308cd4a3c50SDave Chinner {
309e44f4112SAlex Elder 	struct xfs_log_item	*lip;
310e44f4112SAlex Elder 
311e44f4112SAlex Elder 	ASSERT(!list_empty(list));
312cd4a3c50SDave Chinner 
3131d8c95a3SDave Chinner 	/*
314e44f4112SAlex Elder 	 * Use the cursor to determine the insertion point if one is
315e44f4112SAlex Elder 	 * provided.  If not, or if the one we got is not valid,
316e44f4112SAlex Elder 	 * find the place in the AIL where the items belong.
3171d8c95a3SDave Chinner 	 */
318e44f4112SAlex Elder 	lip = cur ? cur->item : NULL;
319e44f4112SAlex Elder 	if (!lip || (__psint_t) lip & 1)
3201d8c95a3SDave Chinner 		lip = __xfs_trans_ail_cursor_last(ailp, lsn);
3211d8c95a3SDave Chinner 
322e44f4112SAlex Elder 	/*
323e44f4112SAlex Elder 	 * If a cursor is provided, we know we're processing the AIL
324e44f4112SAlex Elder 	 * in lsn order, and future items to be spliced in will
325e44f4112SAlex Elder 	 * follow the last one being inserted now.  Update the
326e44f4112SAlex Elder 	 * cursor to point to that last item, now while we have a
327e44f4112SAlex Elder 	 * reliable pointer to it.
328e44f4112SAlex Elder 	 */
3291d8c95a3SDave Chinner 	if (cur)
330e44f4112SAlex Elder 		cur->item = list_entry(list->prev, struct xfs_log_item, li_ail);
331cd4a3c50SDave Chinner 
3321d8c95a3SDave Chinner 	/*
333e44f4112SAlex Elder 	 * Finally perform the splice.  Unless the AIL was empty,
334e44f4112SAlex Elder 	 * lip points to the item in the AIL _after_ which the new
335e44f4112SAlex Elder 	 * items should go.  If lip is null the AIL was empty, so
336e44f4112SAlex Elder 	 * the new items go at the head of the AIL.
3371d8c95a3SDave Chinner 	 */
338e44f4112SAlex Elder 	if (lip)
3391d8c95a3SDave Chinner 		list_splice(list, &lip->li_ail);
340e44f4112SAlex Elder 	else
341e44f4112SAlex Elder 		list_splice(list, &ailp->xa_ail);
342cd4a3c50SDave Chinner }
343cd4a3c50SDave Chinner 
344cd4a3c50SDave Chinner /*
345cd4a3c50SDave Chinner  * Delete the given item from the AIL.  Return a pointer to the item.
346cd4a3c50SDave Chinner  */
347cd4a3c50SDave Chinner static void
348cd4a3c50SDave Chinner xfs_ail_delete(
349cd4a3c50SDave Chinner 	struct xfs_ail  *ailp,
350cd4a3c50SDave Chinner 	xfs_log_item_t  *lip)
351cd4a3c50SDave Chinner {
352cd4a3c50SDave Chinner 	xfs_ail_check(ailp, lip);
353cd4a3c50SDave Chinner 	list_del(&lip->li_ail);
354cd4a3c50SDave Chinner 	xfs_trans_ail_cursor_clear(ailp, lip);
355cd4a3c50SDave Chinner }
356cd4a3c50SDave Chinner 
3570030807cSChristoph Hellwig static long
3580030807cSChristoph Hellwig xfsaild_push(
3590030807cSChristoph Hellwig 	struct xfs_ail		*ailp)
360249a8c11SDavid Chinner {
36182fa9012SDavid Chinner 	xfs_mount_t		*mp = ailp->xa_mount;
362af3e4022SDave Chinner 	struct xfs_ail_cursor	cur;
3639e7004e7SDave Chinner 	xfs_log_item_t		*lip;
3649e7004e7SDave Chinner 	xfs_lsn_t		lsn;
365fe0da767SDave Chinner 	xfs_lsn_t		target;
36643ff2122SChristoph Hellwig 	long			tout;
3679e7004e7SDave Chinner 	int			stuck = 0;
36843ff2122SChristoph Hellwig 	int			flushing = 0;
3699e7004e7SDave Chinner 	int			count = 0;
3701da177e4SLinus Torvalds 
371670ce93fSDave Chinner 	/*
37243ff2122SChristoph Hellwig 	 * If we encountered pinned items or did not finish writing out all
37343ff2122SChristoph Hellwig 	 * buffers the last time we ran, force the log first and wait for it
37443ff2122SChristoph Hellwig 	 * before pushing again.
375670ce93fSDave Chinner 	 */
37643ff2122SChristoph Hellwig 	if (ailp->xa_log_flush && ailp->xa_last_pushed_lsn == 0 &&
37743ff2122SChristoph Hellwig 	    (!list_empty_careful(&ailp->xa_buf_list) ||
37843ff2122SChristoph Hellwig 	     xfs_ail_min_lsn(ailp))) {
379670ce93fSDave Chinner 		ailp->xa_log_flush = 0;
38043ff2122SChristoph Hellwig 
381670ce93fSDave Chinner 		XFS_STATS_INC(xs_push_ail_flush);
382670ce93fSDave Chinner 		xfs_log_force(mp, XFS_LOG_SYNC);
383670ce93fSDave Chinner 	}
384670ce93fSDave Chinner 
38543ff2122SChristoph Hellwig 	spin_lock(&ailp->xa_lock);
3868375f922SBrian Foster 
3878375f922SBrian Foster 	/* barrier matches the xa_target update in xfs_ail_push() */
3888375f922SBrian Foster 	smp_rmb();
3898375f922SBrian Foster 	target = ailp->xa_target;
3908375f922SBrian Foster 	ailp->xa_target_prev = target;
3918375f922SBrian Foster 
392af3e4022SDave Chinner 	lip = xfs_trans_ail_cursor_first(ailp, &cur, ailp->xa_last_pushed_lsn);
393211e4d43SChristoph Hellwig 	if (!lip) {
3941da177e4SLinus Torvalds 		/*
39543ff2122SChristoph Hellwig 		 * If the AIL is empty or our push has reached the end we are
39643ff2122SChristoph Hellwig 		 * done now.
3971da177e4SLinus Torvalds 		 */
398af3e4022SDave Chinner 		xfs_trans_ail_cursor_done(ailp, &cur);
399c7e8f268SDavid Chinner 		spin_unlock(&ailp->xa_lock);
4009e7004e7SDave Chinner 		goto out_done;
4011da177e4SLinus Torvalds 	}
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds 	XFS_STATS_INC(xs_push_ail);
4041da177e4SLinus Torvalds 
405249a8c11SDavid Chinner 	lsn = lip->li_lsn;
40650e86686SDave Chinner 	while ((XFS_LSN_CMP(lip->li_lsn, target) <= 0)) {
407249a8c11SDavid Chinner 		int	lock_result;
40843ff2122SChristoph Hellwig 
409249a8c11SDavid Chinner 		/*
41043ff2122SChristoph Hellwig 		 * Note that IOP_PUSH may unlock and reacquire the AIL lock.  We
41143ff2122SChristoph Hellwig 		 * rely on the AIL cursor implementation to be able to deal with
41243ff2122SChristoph Hellwig 		 * the dropped lock.
4131da177e4SLinus Torvalds 		 */
41443ff2122SChristoph Hellwig 		lock_result = IOP_PUSH(lip, &ailp->xa_buf_list);
4151da177e4SLinus Torvalds 		switch (lock_result) {
4161da177e4SLinus Torvalds 		case XFS_ITEM_SUCCESS:
4171da177e4SLinus Torvalds 			XFS_STATS_INC(xs_push_ail_success);
4189e4c109aSChristoph Hellwig 			trace_xfs_ail_push(lip);
4199e4c109aSChristoph Hellwig 
4200bf6a5bdSDave Chinner 			ailp->xa_last_pushed_lsn = lsn;
4211da177e4SLinus Torvalds 			break;
4221da177e4SLinus Torvalds 
42343ff2122SChristoph Hellwig 		case XFS_ITEM_FLUSHING:
42443ff2122SChristoph Hellwig 			/*
42543ff2122SChristoph Hellwig 			 * The item or its backing buffer is already beeing
42643ff2122SChristoph Hellwig 			 * flushed.  The typical reason for that is that an
42743ff2122SChristoph Hellwig 			 * inode buffer is locked because we already pushed the
42843ff2122SChristoph Hellwig 			 * updates to it as part of inode clustering.
42943ff2122SChristoph Hellwig 			 *
43043ff2122SChristoph Hellwig 			 * We do not want to to stop flushing just because lots
43143ff2122SChristoph Hellwig 			 * of items are already beeing flushed, but we need to
43243ff2122SChristoph Hellwig 			 * re-try the flushing relatively soon if most of the
43343ff2122SChristoph Hellwig 			 * AIL is beeing flushed.
43443ff2122SChristoph Hellwig 			 */
43543ff2122SChristoph Hellwig 			XFS_STATS_INC(xs_push_ail_flushing);
43643ff2122SChristoph Hellwig 			trace_xfs_ail_flushing(lip);
43717b38471SChristoph Hellwig 
43843ff2122SChristoph Hellwig 			flushing++;
4390bf6a5bdSDave Chinner 			ailp->xa_last_pushed_lsn = lsn;
4401da177e4SLinus Torvalds 			break;
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds 		case XFS_ITEM_PINNED:
4431da177e4SLinus Torvalds 			XFS_STATS_INC(xs_push_ail_pinned);
4449e4c109aSChristoph Hellwig 			trace_xfs_ail_pinned(lip);
4459e4c109aSChristoph Hellwig 
446249a8c11SDavid Chinner 			stuck++;
447670ce93fSDave Chinner 			ailp->xa_log_flush++;
4481da177e4SLinus Torvalds 			break;
4491da177e4SLinus Torvalds 		case XFS_ITEM_LOCKED:
4501da177e4SLinus Torvalds 			XFS_STATS_INC(xs_push_ail_locked);
4519e4c109aSChristoph Hellwig 			trace_xfs_ail_locked(lip);
45243ff2122SChristoph Hellwig 
453249a8c11SDavid Chinner 			stuck++;
4541da177e4SLinus Torvalds 			break;
4551da177e4SLinus Torvalds 		default:
4561da177e4SLinus Torvalds 			ASSERT(0);
4571da177e4SLinus Torvalds 			break;
4581da177e4SLinus Torvalds 		}
4591da177e4SLinus Torvalds 
460249a8c11SDavid Chinner 		count++;
461249a8c11SDavid Chinner 
462249a8c11SDavid Chinner 		/*
463249a8c11SDavid Chinner 		 * Are there too many items we can't do anything with?
46443ff2122SChristoph Hellwig 		 *
465249a8c11SDavid Chinner 		 * If we we are skipping too many items because we can't flush
466249a8c11SDavid Chinner 		 * them or they are already being flushed, we back off and
467249a8c11SDavid Chinner 		 * given them time to complete whatever operation is being
468249a8c11SDavid Chinner 		 * done. i.e. remove pressure from the AIL while we can't make
469249a8c11SDavid Chinner 		 * progress so traversals don't slow down further inserts and
470249a8c11SDavid Chinner 		 * removals to/from the AIL.
471249a8c11SDavid Chinner 		 *
472249a8c11SDavid Chinner 		 * The value of 100 is an arbitrary magic number based on
473249a8c11SDavid Chinner 		 * observation.
474249a8c11SDavid Chinner 		 */
475249a8c11SDavid Chinner 		if (stuck > 100)
476249a8c11SDavid Chinner 			break;
477249a8c11SDavid Chinner 
478af3e4022SDave Chinner 		lip = xfs_trans_ail_cursor_next(ailp, &cur);
479249a8c11SDavid Chinner 		if (lip == NULL)
480249a8c11SDavid Chinner 			break;
481249a8c11SDavid Chinner 		lsn = lip->li_lsn;
4821da177e4SLinus Torvalds 	}
483af3e4022SDave Chinner 	xfs_trans_ail_cursor_done(ailp, &cur);
484c7e8f268SDavid Chinner 	spin_unlock(&ailp->xa_lock);
4851da177e4SLinus Torvalds 
48643ff2122SChristoph Hellwig 	if (xfs_buf_delwri_submit_nowait(&ailp->xa_buf_list))
48743ff2122SChristoph Hellwig 		ailp->xa_log_flush++;
488d808f617SDave Chinner 
48943ff2122SChristoph Hellwig 	if (!count || XFS_LSN_CMP(lsn, target) >= 0) {
4909e7004e7SDave Chinner out_done:
491249a8c11SDavid Chinner 		/*
49243ff2122SChristoph Hellwig 		 * We reached the target or the AIL is empty, so wait a bit
49343ff2122SChristoph Hellwig 		 * longer for I/O to complete and remove pushed items from the
49443ff2122SChristoph Hellwig 		 * AIL before we start the next scan from the start of the AIL.
495249a8c11SDavid Chinner 		 */
496453eac8aSDave Chinner 		tout = 50;
4970bf6a5bdSDave Chinner 		ailp->xa_last_pushed_lsn = 0;
49843ff2122SChristoph Hellwig 	} else if (((stuck + flushing) * 100) / count > 90) {
499249a8c11SDavid Chinner 		/*
50043ff2122SChristoph Hellwig 		 * Either there is a lot of contention on the AIL or we are
50143ff2122SChristoph Hellwig 		 * stuck due to operations in progress. "Stuck" in this case
50243ff2122SChristoph Hellwig 		 * is defined as >90% of the items we tried to push were stuck.
503249a8c11SDavid Chinner 		 *
504249a8c11SDavid Chinner 		 * Backoff a bit more to allow some I/O to complete before
50543ff2122SChristoph Hellwig 		 * restarting from the start of the AIL. This prevents us from
50643ff2122SChristoph Hellwig 		 * spinning on the same items, and if they are pinned will all
50743ff2122SChristoph Hellwig 		 * the restart to issue a log force to unpin the stuck items.
508249a8c11SDavid Chinner 		 */
509453eac8aSDave Chinner 		tout = 20;
510670ce93fSDave Chinner 		ailp->xa_last_pushed_lsn = 0;
51143ff2122SChristoph Hellwig 	} else {
51243ff2122SChristoph Hellwig 		/*
51343ff2122SChristoph Hellwig 		 * Assume we have more work to do in a short while.
51443ff2122SChristoph Hellwig 		 */
51543ff2122SChristoph Hellwig 		tout = 10;
516453eac8aSDave Chinner 	}
5171da177e4SLinus Torvalds 
5180030807cSChristoph Hellwig 	return tout;
5190030807cSChristoph Hellwig }
5200030807cSChristoph Hellwig 
5210030807cSChristoph Hellwig static int
5220030807cSChristoph Hellwig xfsaild(
5230030807cSChristoph Hellwig 	void		*data)
5240030807cSChristoph Hellwig {
5250030807cSChristoph Hellwig 	struct xfs_ail	*ailp = data;
5260030807cSChristoph Hellwig 	long		tout = 0;	/* milliseconds */
5270030807cSChristoph Hellwig 
52843ff2122SChristoph Hellwig 	current->flags |= PF_MEMALLOC;
52943ff2122SChristoph Hellwig 
5300030807cSChristoph Hellwig 	while (!kthread_should_stop()) {
5310030807cSChristoph Hellwig 		if (tout && tout <= 20)
5320030807cSChristoph Hellwig 			__set_current_state(TASK_KILLABLE);
5330030807cSChristoph Hellwig 		else
5340030807cSChristoph Hellwig 			__set_current_state(TASK_INTERRUPTIBLE);
5358375f922SBrian Foster 
5368375f922SBrian Foster 		spin_lock(&ailp->xa_lock);
5378375f922SBrian Foster 
5388375f922SBrian Foster 		/*
5398375f922SBrian Foster 		 * Idle if the AIL is empty and we are not racing with a target
5408375f922SBrian Foster 		 * update. We check the AIL after we set the task to a sleep
5418375f922SBrian Foster 		 * state to guarantee that we either catch an xa_target update
5428375f922SBrian Foster 		 * or that a wake_up resets the state to TASK_RUNNING.
5438375f922SBrian Foster 		 * Otherwise, we run the risk of sleeping indefinitely.
5448375f922SBrian Foster 		 *
5458375f922SBrian Foster 		 * The barrier matches the xa_target update in xfs_ail_push().
5468375f922SBrian Foster 		 */
5478375f922SBrian Foster 		smp_rmb();
5488375f922SBrian Foster 		if (!xfs_ail_min(ailp) &&
5498375f922SBrian Foster 		    ailp->xa_target == ailp->xa_target_prev) {
5508375f922SBrian Foster 			spin_unlock(&ailp->xa_lock);
5518375f922SBrian Foster 			schedule();
5528375f922SBrian Foster 			tout = 0;
5538375f922SBrian Foster 			continue;
5548375f922SBrian Foster 		}
5558375f922SBrian Foster 		spin_unlock(&ailp->xa_lock);
5568375f922SBrian Foster 
5578375f922SBrian Foster 		if (tout)
5588375f922SBrian Foster 			schedule_timeout(msecs_to_jiffies(tout));
5598375f922SBrian Foster 
5608375f922SBrian Foster 		__set_current_state(TASK_RUNNING);
5610030807cSChristoph Hellwig 
5620030807cSChristoph Hellwig 		try_to_freeze();
5630030807cSChristoph Hellwig 
5640030807cSChristoph Hellwig 		tout = xfsaild_push(ailp);
5650030807cSChristoph Hellwig 	}
5660030807cSChristoph Hellwig 
5670030807cSChristoph Hellwig 	return 0;
5680bf6a5bdSDave Chinner }
5690bf6a5bdSDave Chinner 
5700bf6a5bdSDave Chinner /*
5710bf6a5bdSDave Chinner  * This routine is called to move the tail of the AIL forward.  It does this by
5720bf6a5bdSDave Chinner  * trying to flush items in the AIL whose lsns are below the given
5730bf6a5bdSDave Chinner  * threshold_lsn.
5740bf6a5bdSDave Chinner  *
5750bf6a5bdSDave Chinner  * The push is run asynchronously in a workqueue, which means the caller needs
5760bf6a5bdSDave Chinner  * to handle waiting on the async flush for space to become available.
5770bf6a5bdSDave Chinner  * We don't want to interrupt any push that is in progress, hence we only queue
5780bf6a5bdSDave Chinner  * work if we set the pushing bit approriately.
5790bf6a5bdSDave Chinner  *
5800bf6a5bdSDave Chinner  * We do this unlocked - we only need to know whether there is anything in the
5810bf6a5bdSDave Chinner  * AIL at the time we are called. We don't need to access the contents of
5820bf6a5bdSDave Chinner  * any of the objects, so the lock is not needed.
5830bf6a5bdSDave Chinner  */
5840bf6a5bdSDave Chinner void
585fd074841SDave Chinner xfs_ail_push(
5860bf6a5bdSDave Chinner 	struct xfs_ail	*ailp,
5870bf6a5bdSDave Chinner 	xfs_lsn_t	threshold_lsn)
5880bf6a5bdSDave Chinner {
5890bf6a5bdSDave Chinner 	xfs_log_item_t	*lip;
5900bf6a5bdSDave Chinner 
5910bf6a5bdSDave Chinner 	lip = xfs_ail_min(ailp);
5920bf6a5bdSDave Chinner 	if (!lip || XFS_FORCED_SHUTDOWN(ailp->xa_mount) ||
5930bf6a5bdSDave Chinner 	    XFS_LSN_CMP(threshold_lsn, ailp->xa_target) <= 0)
5940bf6a5bdSDave Chinner 		return;
5950bf6a5bdSDave Chinner 
5960bf6a5bdSDave Chinner 	/*
5970bf6a5bdSDave Chinner 	 * Ensure that the new target is noticed in push code before it clears
5980bf6a5bdSDave Chinner 	 * the XFS_AIL_PUSHING_BIT.
5990bf6a5bdSDave Chinner 	 */
6000bf6a5bdSDave Chinner 	smp_wmb();
601fe0da767SDave Chinner 	xfs_trans_ail_copy_lsn(ailp, &ailp->xa_target, &threshold_lsn);
6020030807cSChristoph Hellwig 	smp_wmb();
6030030807cSChristoph Hellwig 
6040030807cSChristoph Hellwig 	wake_up_process(ailp->xa_task);
6050bf6a5bdSDave Chinner }
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds /*
608fd074841SDave Chinner  * Push out all items in the AIL immediately
609fd074841SDave Chinner  */
610fd074841SDave Chinner void
611fd074841SDave Chinner xfs_ail_push_all(
612fd074841SDave Chinner 	struct xfs_ail  *ailp)
613fd074841SDave Chinner {
614fd074841SDave Chinner 	xfs_lsn_t       threshold_lsn = xfs_ail_max_lsn(ailp);
615fd074841SDave Chinner 
616fd074841SDave Chinner 	if (threshold_lsn)
617fd074841SDave Chinner 		xfs_ail_push(ailp, threshold_lsn);
618fd074841SDave Chinner }
619fd074841SDave Chinner 
620fd074841SDave Chinner /*
621211e4d43SChristoph Hellwig  * Push out all items in the AIL immediately and wait until the AIL is empty.
622211e4d43SChristoph Hellwig  */
623211e4d43SChristoph Hellwig void
624211e4d43SChristoph Hellwig xfs_ail_push_all_sync(
625211e4d43SChristoph Hellwig 	struct xfs_ail  *ailp)
626211e4d43SChristoph Hellwig {
627211e4d43SChristoph Hellwig 	struct xfs_log_item	*lip;
628211e4d43SChristoph Hellwig 	DEFINE_WAIT(wait);
629211e4d43SChristoph Hellwig 
630211e4d43SChristoph Hellwig 	spin_lock(&ailp->xa_lock);
631211e4d43SChristoph Hellwig 	while ((lip = xfs_ail_max(ailp)) != NULL) {
632211e4d43SChristoph Hellwig 		prepare_to_wait(&ailp->xa_empty, &wait, TASK_UNINTERRUPTIBLE);
633211e4d43SChristoph Hellwig 		ailp->xa_target = lip->li_lsn;
634211e4d43SChristoph Hellwig 		wake_up_process(ailp->xa_task);
635211e4d43SChristoph Hellwig 		spin_unlock(&ailp->xa_lock);
636211e4d43SChristoph Hellwig 		schedule();
637211e4d43SChristoph Hellwig 		spin_lock(&ailp->xa_lock);
638211e4d43SChristoph Hellwig 	}
639211e4d43SChristoph Hellwig 	spin_unlock(&ailp->xa_lock);
640211e4d43SChristoph Hellwig 
641211e4d43SChristoph Hellwig 	finish_wait(&ailp->xa_empty, &wait);
642211e4d43SChristoph Hellwig }
643211e4d43SChristoph Hellwig 
644211e4d43SChristoph Hellwig /*
6450e57f6a3SDave Chinner  * xfs_trans_ail_update - bulk AIL insertion operation.
6460e57f6a3SDave Chinner  *
6470e57f6a3SDave Chinner  * @xfs_trans_ail_update takes an array of log items that all need to be
6480e57f6a3SDave Chinner  * positioned at the same LSN in the AIL. If an item is not in the AIL, it will
6490e57f6a3SDave Chinner  * be added.  Otherwise, it will be repositioned  by removing it and re-adding
6500e57f6a3SDave Chinner  * it to the AIL. If we move the first item in the AIL, update the log tail to
6510e57f6a3SDave Chinner  * match the new minimum LSN in the AIL.
6520e57f6a3SDave Chinner  *
6530e57f6a3SDave Chinner  * This function takes the AIL lock once to execute the update operations on
6540e57f6a3SDave Chinner  * all the items in the array, and as such should not be called with the AIL
6550e57f6a3SDave Chinner  * lock held. As a result, once we have the AIL lock, we need to check each log
6560e57f6a3SDave Chinner  * item LSN to confirm it needs to be moved forward in the AIL.
6570e57f6a3SDave Chinner  *
6580e57f6a3SDave Chinner  * To optimise the insert operation, we delete all the items from the AIL in
6590e57f6a3SDave Chinner  * the first pass, moving them into a temporary list, then splice the temporary
6600e57f6a3SDave Chinner  * list into the correct position in the AIL. This avoids needing to do an
6610e57f6a3SDave Chinner  * insert operation on every item.
6620e57f6a3SDave Chinner  *
6630e57f6a3SDave Chinner  * This function must be called with the AIL lock held.  The lock is dropped
6640e57f6a3SDave Chinner  * before returning.
6650e57f6a3SDave Chinner  */
6660e57f6a3SDave Chinner void
6670e57f6a3SDave Chinner xfs_trans_ail_update_bulk(
6680e57f6a3SDave Chinner 	struct xfs_ail		*ailp,
6691d8c95a3SDave Chinner 	struct xfs_ail_cursor	*cur,
6700e57f6a3SDave Chinner 	struct xfs_log_item	**log_items,
6710e57f6a3SDave Chinner 	int			nr_items,
6720e57f6a3SDave Chinner 	xfs_lsn_t		lsn) __releases(ailp->xa_lock)
6730e57f6a3SDave Chinner {
6740e57f6a3SDave Chinner 	xfs_log_item_t		*mlip;
6750e57f6a3SDave Chinner 	int			mlip_changed = 0;
6760e57f6a3SDave Chinner 	int			i;
6770e57f6a3SDave Chinner 	LIST_HEAD(tmp);
6780e57f6a3SDave Chinner 
679e44f4112SAlex Elder 	ASSERT(nr_items > 0);		/* Not required, but true. */
6800e57f6a3SDave Chinner 	mlip = xfs_ail_min(ailp);
6810e57f6a3SDave Chinner 
6820e57f6a3SDave Chinner 	for (i = 0; i < nr_items; i++) {
6830e57f6a3SDave Chinner 		struct xfs_log_item *lip = log_items[i];
6840e57f6a3SDave Chinner 		if (lip->li_flags & XFS_LI_IN_AIL) {
6850e57f6a3SDave Chinner 			/* check if we really need to move the item */
6860e57f6a3SDave Chinner 			if (XFS_LSN_CMP(lsn, lip->li_lsn) <= 0)
6870e57f6a3SDave Chinner 				continue;
6880e57f6a3SDave Chinner 
6890e57f6a3SDave Chinner 			xfs_ail_delete(ailp, lip);
6900e57f6a3SDave Chinner 			if (mlip == lip)
6910e57f6a3SDave Chinner 				mlip_changed = 1;
6920e57f6a3SDave Chinner 		} else {
6930e57f6a3SDave Chinner 			lip->li_flags |= XFS_LI_IN_AIL;
6940e57f6a3SDave Chinner 		}
6950e57f6a3SDave Chinner 		lip->li_lsn = lsn;
6960e57f6a3SDave Chinner 		list_add(&lip->li_ail, &tmp);
6970e57f6a3SDave Chinner 	}
6980e57f6a3SDave Chinner 
699e44f4112SAlex Elder 	if (!list_empty(&tmp))
7001d8c95a3SDave Chinner 		xfs_ail_splice(ailp, cur, &tmp, lsn);
7011c304625SChristoph Hellwig 
7021c304625SChristoph Hellwig 	if (mlip_changed) {
7031c304625SChristoph Hellwig 		if (!XFS_FORCED_SHUTDOWN(ailp->xa_mount))
7041c304625SChristoph Hellwig 			xlog_assign_tail_lsn_locked(ailp->xa_mount);
7050e57f6a3SDave Chinner 		spin_unlock(&ailp->xa_lock);
70609a423a3SChristoph Hellwig 
707cfb7cdcaSChristoph Hellwig 		xfs_log_space_wake(ailp->xa_mount);
7081c304625SChristoph Hellwig 	} else {
7091c304625SChristoph Hellwig 		spin_unlock(&ailp->xa_lock);
7100e57f6a3SDave Chinner 	}
7110e57f6a3SDave Chinner }
7120e57f6a3SDave Chinner 
7130e57f6a3SDave Chinner /*
71430136832SDave Chinner  * xfs_trans_ail_delete_bulk - remove multiple log items from the AIL
71530136832SDave Chinner  *
71630136832SDave Chinner  * @xfs_trans_ail_delete_bulk takes an array of log items that all need to
71730136832SDave Chinner  * removed from the AIL. The caller is already holding the AIL lock, and done
71830136832SDave Chinner  * all the checks necessary to ensure the items passed in via @log_items are
71930136832SDave Chinner  * ready for deletion. This includes checking that the items are in the AIL.
72030136832SDave Chinner  *
72130136832SDave Chinner  * For each log item to be removed, unlink it  from the AIL, clear the IN_AIL
72230136832SDave Chinner  * flag from the item and reset the item's lsn to 0. If we remove the first
72330136832SDave Chinner  * item in the AIL, update the log tail to match the new minimum LSN in the
72430136832SDave Chinner  * AIL.
72530136832SDave Chinner  *
72630136832SDave Chinner  * This function will not drop the AIL lock until all items are removed from
72730136832SDave Chinner  * the AIL to minimise the amount of lock traffic on the AIL. This does not
72830136832SDave Chinner  * greatly increase the AIL hold time, but does significantly reduce the amount
72930136832SDave Chinner  * of traffic on the lock, especially during IO completion.
73030136832SDave Chinner  *
73130136832SDave Chinner  * This function must be called with the AIL lock held.  The lock is dropped
73230136832SDave Chinner  * before returning.
73330136832SDave Chinner  */
73430136832SDave Chinner void
73530136832SDave Chinner xfs_trans_ail_delete_bulk(
73630136832SDave Chinner 	struct xfs_ail		*ailp,
73730136832SDave Chinner 	struct xfs_log_item	**log_items,
73804913fddSDave Chinner 	int			nr_items,
73904913fddSDave Chinner 	int			shutdown_type) __releases(ailp->xa_lock)
74030136832SDave Chinner {
74130136832SDave Chinner 	xfs_log_item_t		*mlip;
74230136832SDave Chinner 	int			mlip_changed = 0;
74330136832SDave Chinner 	int			i;
74430136832SDave Chinner 
74530136832SDave Chinner 	mlip = xfs_ail_min(ailp);
74630136832SDave Chinner 
74730136832SDave Chinner 	for (i = 0; i < nr_items; i++) {
74830136832SDave Chinner 		struct xfs_log_item *lip = log_items[i];
74930136832SDave Chinner 		if (!(lip->li_flags & XFS_LI_IN_AIL)) {
75030136832SDave Chinner 			struct xfs_mount	*mp = ailp->xa_mount;
75130136832SDave Chinner 
75230136832SDave Chinner 			spin_unlock(&ailp->xa_lock);
75330136832SDave Chinner 			if (!XFS_FORCED_SHUTDOWN(mp)) {
7546a19d939SDave Chinner 				xfs_alert_tag(mp, XFS_PTAG_AILDELETE,
75530136832SDave Chinner 		"%s: attempting to delete a log item that is not in the AIL",
75630136832SDave Chinner 						__func__);
75704913fddSDave Chinner 				xfs_force_shutdown(mp, shutdown_type);
75830136832SDave Chinner 			}
75930136832SDave Chinner 			return;
76030136832SDave Chinner 		}
76130136832SDave Chinner 
76230136832SDave Chinner 		xfs_ail_delete(ailp, lip);
76330136832SDave Chinner 		lip->li_flags &= ~XFS_LI_IN_AIL;
76430136832SDave Chinner 		lip->li_lsn = 0;
76530136832SDave Chinner 		if (mlip == lip)
76630136832SDave Chinner 			mlip_changed = 1;
76730136832SDave Chinner 	}
7681c304625SChristoph Hellwig 
7691c304625SChristoph Hellwig 	if (mlip_changed) {
7701c304625SChristoph Hellwig 		if (!XFS_FORCED_SHUTDOWN(ailp->xa_mount))
7711c304625SChristoph Hellwig 			xlog_assign_tail_lsn_locked(ailp->xa_mount);
772211e4d43SChristoph Hellwig 		if (list_empty(&ailp->xa_ail))
773211e4d43SChristoph Hellwig 			wake_up_all(&ailp->xa_empty);
77430136832SDave Chinner 		spin_unlock(&ailp->xa_lock);
77509a423a3SChristoph Hellwig 
776cfb7cdcaSChristoph Hellwig 		xfs_log_space_wake(ailp->xa_mount);
7771c304625SChristoph Hellwig 	} else {
7781c304625SChristoph Hellwig 		spin_unlock(&ailp->xa_lock);
77930136832SDave Chinner 	}
78030136832SDave Chinner }
7811da177e4SLinus Torvalds 
782249a8c11SDavid Chinner int
7831da177e4SLinus Torvalds xfs_trans_ail_init(
7841da177e4SLinus Torvalds 	xfs_mount_t	*mp)
7851da177e4SLinus Torvalds {
78682fa9012SDavid Chinner 	struct xfs_ail	*ailp;
78782fa9012SDavid Chinner 
78882fa9012SDavid Chinner 	ailp = kmem_zalloc(sizeof(struct xfs_ail), KM_MAYFAIL);
78982fa9012SDavid Chinner 	if (!ailp)
79082fa9012SDavid Chinner 		return ENOMEM;
79182fa9012SDavid Chinner 
79282fa9012SDavid Chinner 	ailp->xa_mount = mp;
79382fa9012SDavid Chinner 	INIT_LIST_HEAD(&ailp->xa_ail);
794af3e4022SDave Chinner 	INIT_LIST_HEAD(&ailp->xa_cursors);
795c7e8f268SDavid Chinner 	spin_lock_init(&ailp->xa_lock);
79643ff2122SChristoph Hellwig 	INIT_LIST_HEAD(&ailp->xa_buf_list);
797211e4d43SChristoph Hellwig 	init_waitqueue_head(&ailp->xa_empty);
7980030807cSChristoph Hellwig 
7990030807cSChristoph Hellwig 	ailp->xa_task = kthread_run(xfsaild, ailp, "xfsaild/%s",
8000030807cSChristoph Hellwig 			ailp->xa_mount->m_fsname);
8010030807cSChristoph Hellwig 	if (IS_ERR(ailp->xa_task))
8020030807cSChristoph Hellwig 		goto out_free_ailp;
8030030807cSChristoph Hellwig 
80427d8d5feSDavid Chinner 	mp->m_ail = ailp;
80527d8d5feSDavid Chinner 	return 0;
8060030807cSChristoph Hellwig 
8070030807cSChristoph Hellwig out_free_ailp:
8080030807cSChristoph Hellwig 	kmem_free(ailp);
8090030807cSChristoph Hellwig 	return ENOMEM;
810249a8c11SDavid Chinner }
811249a8c11SDavid Chinner 
812249a8c11SDavid Chinner void
813249a8c11SDavid Chinner xfs_trans_ail_destroy(
814249a8c11SDavid Chinner 	xfs_mount_t	*mp)
815249a8c11SDavid Chinner {
81682fa9012SDavid Chinner 	struct xfs_ail	*ailp = mp->m_ail;
81782fa9012SDavid Chinner 
8180030807cSChristoph Hellwig 	kthread_stop(ailp->xa_task);
81982fa9012SDavid Chinner 	kmem_free(ailp);
8201da177e4SLinus Torvalds }
821