blob: 00cc5b8734be817ac3fe4d2982afff61b1cf7f36 [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Nathan Scott7b718762005-11-02 14:58:39 +11003 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
David Chinnerc7e8f262008-10-30 17:39:23 +11004 * Copyright (c) 2008 Dave Chinner
Nathan Scott7b718762005-11-02 14:58:39 +11005 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +11008#include "xfs_fs.h"
Darrick J. Wong5467b342019-06-28 19:25:35 -07009#include "xfs_shared.h"
Christoph Hellwig4fb6e8a2014-11-28 14:25:04 +110010#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110011#include "xfs_log_format.h"
12#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "xfs_mount.h"
Dave Chinner239880e2013-10-23 10:50:10 +110014#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include "xfs_trans_priv.h"
Christoph Hellwig9e4c1092011-10-11 15:14:11 +000016#include "xfs_trace.h"
Darrick J. Wonge9e899a2017-10-31 12:04:49 -070017#include "xfs_errortag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs_error.h"
Dave Chinner239880e2013-10-23 10:50:10 +110019#include "xfs_log.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#ifdef DEBUG
Dave Chinnercd4a3c52011-04-08 12:45:07 +100022/*
23 * Check that the list is sorted as it should be.
Dave Chinnerd686d122018-05-09 07:49:09 -070024 *
25 * Called with the ail lock held, but we don't want to assert fail with it
26 * held otherwise we'll lock everything up and won't be able to debug the
27 * cause. Hence we sample and check the state under the AIL lock and return if
28 * everything is fine, otherwise we drop the lock and run the ASSERT checks.
29 * Asserts may not be fatal, so pick the lock back up and continue onwards.
Dave Chinnercd4a3c52011-04-08 12:45:07 +100030 */
31STATIC void
32xfs_ail_check(
Dave Chinnerd686d122018-05-09 07:49:09 -070033 struct xfs_ail *ailp,
34 struct xfs_log_item *lip)
Dave Chinnercd4a3c52011-04-08 12:45:07 +100035{
Dave Chinnerd686d122018-05-09 07:49:09 -070036 struct xfs_log_item *prev_lip;
37 struct xfs_log_item *next_lip;
38 xfs_lsn_t prev_lsn = NULLCOMMITLSN;
39 xfs_lsn_t next_lsn = NULLCOMMITLSN;
40 xfs_lsn_t lsn;
41 bool in_ail;
42
Dave Chinnercd4a3c52011-04-08 12:45:07 +100043
Matthew Wilcox57e80952018-03-07 14:59:39 -080044 if (list_empty(&ailp->ail_head))
Dave Chinnercd4a3c52011-04-08 12:45:07 +100045 return;
46
47 /*
Dave Chinnerd686d122018-05-09 07:49:09 -070048 * Sample then check the next and previous entries are valid.
Dave Chinnercd4a3c52011-04-08 12:45:07 +100049 */
Dave Chinnerd686d122018-05-09 07:49:09 -070050 in_ail = test_bit(XFS_LI_IN_AIL, &lip->li_flags);
51 prev_lip = list_entry(lip->li_ail.prev, struct xfs_log_item, li_ail);
Matthew Wilcox57e80952018-03-07 14:59:39 -080052 if (&prev_lip->li_ail != &ailp->ail_head)
Dave Chinnerd686d122018-05-09 07:49:09 -070053 prev_lsn = prev_lip->li_lsn;
54 next_lip = list_entry(lip->li_ail.next, struct xfs_log_item, li_ail);
55 if (&next_lip->li_ail != &ailp->ail_head)
56 next_lsn = next_lip->li_lsn;
57 lsn = lip->li_lsn;
Dave Chinnercd4a3c52011-04-08 12:45:07 +100058
Dave Chinnerd686d122018-05-09 07:49:09 -070059 if (in_ail &&
60 (prev_lsn == NULLCOMMITLSN || XFS_LSN_CMP(prev_lsn, lsn) <= 0) &&
61 (next_lsn == NULLCOMMITLSN || XFS_LSN_CMP(next_lsn, lsn) >= 0))
62 return;
Dave Chinnercd4a3c52011-04-08 12:45:07 +100063
Dave Chinnerd686d122018-05-09 07:49:09 -070064 spin_unlock(&ailp->ail_lock);
65 ASSERT(in_ail);
66 ASSERT(prev_lsn == NULLCOMMITLSN || XFS_LSN_CMP(prev_lsn, lsn) <= 0);
67 ASSERT(next_lsn == NULLCOMMITLSN || XFS_LSN_CMP(next_lsn, lsn) >= 0);
68 spin_lock(&ailp->ail_lock);
Dave Chinnercd4a3c52011-04-08 12:45:07 +100069}
70#else /* !DEBUG */
David Chinnerde08dbc2008-02-05 12:13:38 +110071#define xfs_ail_check(a,l)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#endif /* DEBUG */
73
Dave Chinnercd4a3c52011-04-08 12:45:07 +100074/*
Dave Chinnerfd074842011-04-08 12:45:07 +100075 * Return a pointer to the last item in the AIL. If the AIL is empty, then
76 * return NULL.
77 */
Christoph Hellwigefe23302019-06-28 19:27:33 -070078static struct xfs_log_item *
Dave Chinnerfd074842011-04-08 12:45:07 +100079xfs_ail_max(
80 struct xfs_ail *ailp)
81{
Matthew Wilcox57e80952018-03-07 14:59:39 -080082 if (list_empty(&ailp->ail_head))
Dave Chinnerfd074842011-04-08 12:45:07 +100083 return NULL;
84
Christoph Hellwigefe23302019-06-28 19:27:33 -070085 return list_entry(ailp->ail_head.prev, struct xfs_log_item, li_ail);
Dave Chinnerfd074842011-04-08 12:45:07 +100086}
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088/*
Dave Chinnercd4a3c52011-04-08 12:45:07 +100089 * Return a pointer to the item which follows the given item in the AIL. If
90 * the given item is the last item in the list, then return NULL.
91 */
Christoph Hellwigefe23302019-06-28 19:27:33 -070092static struct xfs_log_item *
Dave Chinnercd4a3c52011-04-08 12:45:07 +100093xfs_ail_next(
Christoph Hellwigefe23302019-06-28 19:27:33 -070094 struct xfs_ail *ailp,
95 struct xfs_log_item *lip)
Dave Chinnercd4a3c52011-04-08 12:45:07 +100096{
Matthew Wilcox57e80952018-03-07 14:59:39 -080097 if (lip->li_ail.next == &ailp->ail_head)
Dave Chinnercd4a3c52011-04-08 12:45:07 +100098 return NULL;
99
Christoph Hellwigefe23302019-06-28 19:27:33 -0700100 return list_first_entry(&lip->li_ail, struct xfs_log_item, li_ail);
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000101}
102
103/*
104 * This is called by the log manager code to determine the LSN of the tail of
105 * the log. This is exactly the LSN of the first item in the AIL. If the AIL
106 * is empty, then this function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 *
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000108 * We need the AIL lock in order to get a coherent read of the lsn of the last
109 * item in the AIL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 */
111xfs_lsn_t
Dave Chinnerfd074842011-04-08 12:45:07 +1000112xfs_ail_min_lsn(
Christoph Hellwigefe23302019-06-28 19:27:33 -0700113 struct xfs_ail *ailp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Christoph Hellwigefe23302019-06-28 19:27:33 -0700115 xfs_lsn_t lsn = 0;
116 struct xfs_log_item *lip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Matthew Wilcox57e80952018-03-07 14:59:39 -0800118 spin_lock(&ailp->ail_lock);
David Chinner5b00f142008-10-30 17:39:00 +1100119 lip = xfs_ail_min(ailp);
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000120 if (lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 lsn = lip->li_lsn;
Matthew Wilcox57e80952018-03-07 14:59:39 -0800122 spin_unlock(&ailp->ail_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 return lsn;
125}
126
127/*
Dave Chinnerfd074842011-04-08 12:45:07 +1000128 * Return the maximum lsn held in the AIL, or zero if the AIL is empty.
129 */
130static xfs_lsn_t
131xfs_ail_max_lsn(
Christoph Hellwigefe23302019-06-28 19:27:33 -0700132 struct xfs_ail *ailp)
Dave Chinnerfd074842011-04-08 12:45:07 +1000133{
Christoph Hellwigefe23302019-06-28 19:27:33 -0700134 xfs_lsn_t lsn = 0;
135 struct xfs_log_item *lip;
Dave Chinnerfd074842011-04-08 12:45:07 +1000136
Matthew Wilcox57e80952018-03-07 14:59:39 -0800137 spin_lock(&ailp->ail_lock);
Dave Chinnerfd074842011-04-08 12:45:07 +1000138 lip = xfs_ail_max(ailp);
139 if (lip)
140 lsn = lip->li_lsn;
Matthew Wilcox57e80952018-03-07 14:59:39 -0800141 spin_unlock(&ailp->ail_lock);
Dave Chinnerfd074842011-04-08 12:45:07 +1000142
143 return lsn;
144}
145
146/*
Dave Chinneraf3e4022011-07-18 03:40:18 +0000147 * The cursor keeps track of where our current traversal is up to by tracking
148 * the next item in the list for us. However, for this to be safe, removing an
149 * object from the AIL needs to invalidate any cursor that points to it. hence
150 * the traversal cursor needs to be linked to the struct xfs_ail so that
151 * deletion can search all the active cursors for invalidation.
David Chinner27d8d5f2008-10-30 17:38:39 +1100152 */
David Chinner5b00f142008-10-30 17:39:00 +1100153STATIC void
David Chinner27d8d5f2008-10-30 17:38:39 +1100154xfs_trans_ail_cursor_init(
155 struct xfs_ail *ailp,
156 struct xfs_ail_cursor *cur)
157{
158 cur->item = NULL;
Matthew Wilcox57e80952018-03-07 14:59:39 -0800159 list_add_tail(&cur->list, &ailp->ail_cursors);
David Chinner27d8d5f2008-10-30 17:38:39 +1100160}
161
162/*
Dave Chinneraf3e4022011-07-18 03:40:18 +0000163 * Get the next item in the traversal and advance the cursor. If the cursor
164 * was invalidated (indicated by a lip of 1), restart the traversal.
David Chinner27d8d5f2008-10-30 17:38:39 +1100165 */
David Chinner5b00f142008-10-30 17:39:00 +1100166struct xfs_log_item *
David Chinner27d8d5f2008-10-30 17:38:39 +1100167xfs_trans_ail_cursor_next(
168 struct xfs_ail *ailp,
169 struct xfs_ail_cursor *cur)
170{
171 struct xfs_log_item *lip = cur->item;
172
Christoph Hellwigdb9d67d2015-06-22 09:43:32 +1000173 if ((uintptr_t)lip & 1)
David Chinner27d8d5f2008-10-30 17:38:39 +1100174 lip = xfs_ail_min(ailp);
Dave Chinner16b59022011-07-18 03:40:17 +0000175 if (lip)
176 cur->item = xfs_ail_next(ailp, lip);
David Chinner27d8d5f2008-10-30 17:38:39 +1100177 return lip;
178}
179
180/*
Dave Chinneraf3e4022011-07-18 03:40:18 +0000181 * When the traversal is complete, we need to remove the cursor from the list
182 * of traversing cursors.
David Chinner27d8d5f2008-10-30 17:38:39 +1100183 */
184void
185xfs_trans_ail_cursor_done(
Dave Chinneraf3e4022011-07-18 03:40:18 +0000186 struct xfs_ail_cursor *cur)
David Chinner27d8d5f2008-10-30 17:38:39 +1100187{
Dave Chinneraf3e4022011-07-18 03:40:18 +0000188 cur->item = NULL;
189 list_del_init(&cur->list);
David Chinner27d8d5f2008-10-30 17:38:39 +1100190}
191
192/*
Dave Chinneraf3e4022011-07-18 03:40:18 +0000193 * Invalidate any cursor that is pointing to this item. This is called when an
194 * item is removed from the AIL. Any cursor pointing to this object is now
195 * invalid and the traversal needs to be terminated so it doesn't reference a
196 * freed object. We set the low bit of the cursor item pointer so we can
197 * distinguish between an invalidation and the end of the list when getting the
198 * next item from the cursor.
David Chinner5b00f142008-10-30 17:39:00 +1100199 */
200STATIC void
201xfs_trans_ail_cursor_clear(
202 struct xfs_ail *ailp,
203 struct xfs_log_item *lip)
204{
205 struct xfs_ail_cursor *cur;
206
Matthew Wilcox57e80952018-03-07 14:59:39 -0800207 list_for_each_entry(cur, &ailp->ail_cursors, list) {
David Chinner5b00f142008-10-30 17:39:00 +1100208 if (cur->item == lip)
209 cur->item = (struct xfs_log_item *)
Christoph Hellwigdb9d67d2015-06-22 09:43:32 +1000210 ((uintptr_t)cur->item | 1);
David Chinner5b00f142008-10-30 17:39:00 +1100211 }
212}
213
214/*
Dave Chinner16b59022011-07-18 03:40:17 +0000215 * Find the first item in the AIL with the given @lsn by searching in ascending
216 * LSN order and initialise the cursor to point to the next item for a
217 * ascending traversal. Pass a @lsn of zero to initialise the cursor to the
218 * first item in the AIL. Returns NULL if the list is empty.
David Chinner249a8c12008-02-05 12:13:32 +1100219 */
Christoph Hellwigefe23302019-06-28 19:27:33 -0700220struct xfs_log_item *
David Chinner5b00f142008-10-30 17:39:00 +1100221xfs_trans_ail_cursor_first(
David Chinner27d8d5f2008-10-30 17:38:39 +1100222 struct xfs_ail *ailp,
223 struct xfs_ail_cursor *cur,
224 xfs_lsn_t lsn)
David Chinner249a8c12008-02-05 12:13:32 +1100225{
Christoph Hellwigefe23302019-06-28 19:27:33 -0700226 struct xfs_log_item *lip;
David Chinner249a8c12008-02-05 12:13:32 +1100227
David Chinner5b00f142008-10-30 17:39:00 +1100228 xfs_trans_ail_cursor_init(ailp, cur);
Dave Chinner16b59022011-07-18 03:40:17 +0000229
230 if (lsn == 0) {
231 lip = xfs_ail_min(ailp);
David Chinner5b00f142008-10-30 17:39:00 +1100232 goto out;
Dave Chinner16b59022011-07-18 03:40:17 +0000233 }
David Chinner249a8c12008-02-05 12:13:32 +1100234
Matthew Wilcox57e80952018-03-07 14:59:39 -0800235 list_for_each_entry(lip, &ailp->ail_head, li_ail) {
David Chinner5b00f142008-10-30 17:39:00 +1100236 if (XFS_LSN_CMP(lip->li_lsn, lsn) >= 0)
David Chinner7ee49ac2008-10-30 18:26:51 +1100237 goto out;
Josef 'Jeff' Sipek535f6b32008-03-27 17:58:27 +1100238 }
Dave Chinner16b59022011-07-18 03:40:17 +0000239 return NULL;
240
David Chinner5b00f142008-10-30 17:39:00 +1100241out:
Dave Chinner16b59022011-07-18 03:40:17 +0000242 if (lip)
243 cur->item = xfs_ail_next(ailp, lip);
David Chinner5b00f142008-10-30 17:39:00 +1100244 return lip;
David Chinner249a8c12008-02-05 12:13:32 +1100245}
246
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000247static struct xfs_log_item *
248__xfs_trans_ail_cursor_last(
249 struct xfs_ail *ailp,
250 xfs_lsn_t lsn)
251{
Christoph Hellwigefe23302019-06-28 19:27:33 -0700252 struct xfs_log_item *lip;
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000253
Matthew Wilcox57e80952018-03-07 14:59:39 -0800254 list_for_each_entry_reverse(lip, &ailp->ail_head, li_ail) {
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000255 if (XFS_LSN_CMP(lip->li_lsn, lsn) <= 0)
256 return lip;
257 }
258 return NULL;
259}
260
261/*
Dave Chinner16b59022011-07-18 03:40:17 +0000262 * Find the last item in the AIL with the given @lsn by searching in descending
263 * LSN order and initialise the cursor to point to that item. If there is no
264 * item with the value of @lsn, then it sets the cursor to the last item with an
265 * LSN lower than @lsn. Returns NULL if the list is empty.
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000266 */
267struct xfs_log_item *
268xfs_trans_ail_cursor_last(
269 struct xfs_ail *ailp,
270 struct xfs_ail_cursor *cur,
271 xfs_lsn_t lsn)
272{
273 xfs_trans_ail_cursor_init(ailp, cur);
274 cur->item = __xfs_trans_ail_cursor_last(ailp, lsn);
275 return cur->item;
276}
277
278/*
Dave Chinner16b59022011-07-18 03:40:17 +0000279 * Splice the log item list into the AIL at the given LSN. We splice to the
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000280 * tail of the given LSN to maintain insert order for push traversals. The
281 * cursor is optional, allowing repeated updates to the same LSN to avoid
Alex Eldere44f4112011-07-22 16:04:41 +0000282 * repeated traversals. This should not be called with an empty list.
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000283 */
284static void
285xfs_ail_splice(
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000286 struct xfs_ail *ailp,
287 struct xfs_ail_cursor *cur,
288 struct list_head *list,
289 xfs_lsn_t lsn)
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000290{
Alex Eldere44f4112011-07-22 16:04:41 +0000291 struct xfs_log_item *lip;
292
293 ASSERT(!list_empty(list));
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000294
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000295 /*
Alex Eldere44f4112011-07-22 16:04:41 +0000296 * Use the cursor to determine the insertion point if one is
297 * provided. If not, or if the one we got is not valid,
298 * find the place in the AIL where the items belong.
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000299 */
Alex Eldere44f4112011-07-22 16:04:41 +0000300 lip = cur ? cur->item : NULL;
Christoph Hellwigdb9d67d2015-06-22 09:43:32 +1000301 if (!lip || (uintptr_t)lip & 1)
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000302 lip = __xfs_trans_ail_cursor_last(ailp, lsn);
303
Alex Eldere44f4112011-07-22 16:04:41 +0000304 /*
305 * If a cursor is provided, we know we're processing the AIL
306 * in lsn order, and future items to be spliced in will
307 * follow the last one being inserted now. Update the
308 * cursor to point to that last item, now while we have a
309 * reliable pointer to it.
310 */
311 if (cur)
312 cur->item = list_entry(list->prev, struct xfs_log_item, li_ail);
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000313
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000314 /*
Alex Eldere44f4112011-07-22 16:04:41 +0000315 * Finally perform the splice. Unless the AIL was empty,
316 * lip points to the item in the AIL _after_ which the new
317 * items should go. If lip is null the AIL was empty, so
318 * the new items go at the head of the AIL.
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000319 */
Alex Eldere44f4112011-07-22 16:04:41 +0000320 if (lip)
321 list_splice(list, &lip->li_ail);
322 else
Matthew Wilcox57e80952018-03-07 14:59:39 -0800323 list_splice(list, &ailp->ail_head);
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000324}
325
326/*
327 * Delete the given item from the AIL. Return a pointer to the item.
328 */
329static void
330xfs_ail_delete(
Christoph Hellwigefe23302019-06-28 19:27:33 -0700331 struct xfs_ail *ailp,
332 struct xfs_log_item *lip)
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000333{
334 xfs_ail_check(ailp, lip);
335 list_del(&lip->li_ail);
336 xfs_trans_ail_cursor_clear(ailp, lip);
337}
338
Brian Foster7f4d01f2017-08-08 18:21:52 -0700339static inline uint
340xfsaild_push_item(
341 struct xfs_ail *ailp,
342 struct xfs_log_item *lip)
343{
344 /*
345 * If log item pinning is enabled, skip the push and track the item as
346 * pinned. This can help induce head-behind-tail conditions.
347 */
Matthew Wilcox57e80952018-03-07 14:59:39 -0800348 if (XFS_TEST_ERROR(false, ailp->ail_mount, XFS_ERRTAG_LOG_ITEM_PIN))
Brian Foster7f4d01f2017-08-08 18:21:52 -0700349 return XFS_ITEM_PINNED;
350
Christoph Hellwige8b78db2019-06-28 19:27:30 -0700351 /*
352 * Consider the item pinned if a push callback is not defined so the
353 * caller will force the log. This should only happen for intent items
354 * as they are unpinned once the associated done item is committed to
355 * the on-disk log.
356 */
357 if (!lip->li_ops->iop_push)
358 return XFS_ITEM_PINNED;
Matthew Wilcox57e80952018-03-07 14:59:39 -0800359 return lip->li_ops->iop_push(lip, &ailp->ail_buf_list);
Brian Foster7f4d01f2017-08-08 18:21:52 -0700360}
361
Christoph Hellwig00308072011-10-11 11:14:10 -0400362static long
363xfsaild_push(
364 struct xfs_ail *ailp)
David Chinner249a8c12008-02-05 12:13:32 +1100365{
Matthew Wilcox57e80952018-03-07 14:59:39 -0800366 xfs_mount_t *mp = ailp->ail_mount;
Dave Chinneraf3e4022011-07-18 03:40:18 +0000367 struct xfs_ail_cursor cur;
Christoph Hellwigefe23302019-06-28 19:27:33 -0700368 struct xfs_log_item *lip;
Dave Chinner9e7004e2011-05-06 02:54:05 +0000369 xfs_lsn_t lsn;
Dave Chinnerfe0da762011-05-06 02:54:07 +0000370 xfs_lsn_t target;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000371 long tout;
Dave Chinner9e7004e2011-05-06 02:54:05 +0000372 int stuck = 0;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000373 int flushing = 0;
Dave Chinner9e7004e2011-05-06 02:54:05 +0000374 int count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Dave Chinner670ce932011-09-30 04:45:03 +0000376 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000377 * If we encountered pinned items or did not finish writing out all
378 * buffers the last time we ran, force the log first and wait for it
379 * before pushing again.
Dave Chinner670ce932011-09-30 04:45:03 +0000380 */
Matthew Wilcox57e80952018-03-07 14:59:39 -0800381 if (ailp->ail_log_flush && ailp->ail_last_pushed_lsn == 0 &&
382 (!list_empty_careful(&ailp->ail_buf_list) ||
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000383 xfs_ail_min_lsn(ailp))) {
Matthew Wilcox57e80952018-03-07 14:59:39 -0800384 ailp->ail_log_flush = 0;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000385
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100386 XFS_STATS_INC(mp, xs_push_ail_flush);
Dave Chinner670ce932011-09-30 04:45:03 +0000387 xfs_log_force(mp, XFS_LOG_SYNC);
Dave Chinner670ce932011-09-30 04:45:03 +0000388 }
389
Matthew Wilcox57e80952018-03-07 14:59:39 -0800390 spin_lock(&ailp->ail_lock);
Brian Foster8375f922012-06-28 06:52:56 -0400391
Matthew Wilcox57e80952018-03-07 14:59:39 -0800392 /* barrier matches the ail_target update in xfs_ail_push() */
Brian Foster8375f922012-06-28 06:52:56 -0400393 smp_rmb();
Matthew Wilcox57e80952018-03-07 14:59:39 -0800394 target = ailp->ail_target;
395 ailp->ail_target_prev = target;
Brian Foster8375f922012-06-28 06:52:56 -0400396
Matthew Wilcox57e80952018-03-07 14:59:39 -0800397 lip = xfs_trans_ail_cursor_first(ailp, &cur, ailp->ail_last_pushed_lsn);
Christoph Hellwig211e4d42012-04-23 15:58:34 +1000398 if (!lip) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000400 * If the AIL is empty or our push has reached the end we are
401 * done now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 */
Eric Sandeene4a1e292014-04-14 19:06:05 +1000403 xfs_trans_ail_cursor_done(&cur);
Matthew Wilcox57e80952018-03-07 14:59:39 -0800404 spin_unlock(&ailp->ail_lock);
Dave Chinner9e7004e2011-05-06 02:54:05 +0000405 goto out_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 }
407
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100408 XFS_STATS_INC(mp, xs_push_ail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
David Chinner249a8c12008-02-05 12:13:32 +1100410 lsn = lip->li_lsn;
Dave Chinner50e86682011-05-06 02:54:06 +0000411 while ((XFS_LSN_CMP(lip->li_lsn, target) <= 0)) {
David Chinner249a8c12008-02-05 12:13:32 +1100412 int lock_result;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 /*
Dave Chinner904c17e2013-08-28 21:12:03 +1000415 * Note that iop_push may unlock and reacquire the AIL lock. We
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000416 * rely on the AIL cursor implementation to be able to deal with
417 * the dropped lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 */
Brian Foster7f4d01f2017-08-08 18:21:52 -0700419 lock_result = xfsaild_push_item(ailp, lip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 switch (lock_result) {
David Chinner249a8c12008-02-05 12:13:32 +1100421 case XFS_ITEM_SUCCESS:
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100422 XFS_STATS_INC(mp, xs_push_ail_success);
Christoph Hellwig9e4c1092011-10-11 15:14:11 +0000423 trace_xfs_ail_push(lip);
424
Matthew Wilcox57e80952018-03-07 14:59:39 -0800425 ailp->ail_last_pushed_lsn = lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 break;
427
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000428 case XFS_ITEM_FLUSHING:
429 /*
Joe Perchescf085a12019-11-07 13:24:52 -0800430 * The item or its backing buffer is already being
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000431 * flushed. The typical reason for that is that an
432 * inode buffer is locked because we already pushed the
433 * updates to it as part of inode clustering.
434 *
435 * We do not want to to stop flushing just because lots
Joe Perchescf085a12019-11-07 13:24:52 -0800436 * of items are already being flushed, but we need to
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000437 * re-try the flushing relatively soon if most of the
Joe Perchescf085a12019-11-07 13:24:52 -0800438 * AIL is being flushed.
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000439 */
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100440 XFS_STATS_INC(mp, xs_push_ail_flushing);
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000441 trace_xfs_ail_flushing(lip);
Christoph Hellwig17b38472011-10-11 15:14:09 +0000442
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000443 flushing++;
Matthew Wilcox57e80952018-03-07 14:59:39 -0800444 ailp->ail_last_pushed_lsn = lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 break;
446
David Chinner249a8c12008-02-05 12:13:32 +1100447 case XFS_ITEM_PINNED:
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100448 XFS_STATS_INC(mp, xs_push_ail_pinned);
Christoph Hellwig9e4c1092011-10-11 15:14:11 +0000449 trace_xfs_ail_pinned(lip);
450
David Chinner249a8c12008-02-05 12:13:32 +1100451 stuck++;
Matthew Wilcox57e80952018-03-07 14:59:39 -0800452 ailp->ail_log_flush++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 break;
David Chinner249a8c12008-02-05 12:13:32 +1100454 case XFS_ITEM_LOCKED:
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100455 XFS_STATS_INC(mp, xs_push_ail_locked);
Christoph Hellwig9e4c1092011-10-11 15:14:11 +0000456 trace_xfs_ail_locked(lip);
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000457
David Chinner249a8c12008-02-05 12:13:32 +1100458 stuck++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 break;
David Chinner249a8c12008-02-05 12:13:32 +1100460 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 ASSERT(0);
462 break;
463 }
464
David Chinner249a8c12008-02-05 12:13:32 +1100465 count++;
466
467 /*
468 * Are there too many items we can't do anything with?
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000469 *
David Chinner249a8c12008-02-05 12:13:32 +1100470 * If we we are skipping too many items because we can't flush
471 * them or they are already being flushed, we back off and
472 * given them time to complete whatever operation is being
473 * done. i.e. remove pressure from the AIL while we can't make
474 * progress so traversals don't slow down further inserts and
475 * removals to/from the AIL.
476 *
477 * The value of 100 is an arbitrary magic number based on
478 * observation.
479 */
480 if (stuck > 100)
481 break;
482
Dave Chinneraf3e4022011-07-18 03:40:18 +0000483 lip = xfs_trans_ail_cursor_next(ailp, &cur);
David Chinner249a8c12008-02-05 12:13:32 +1100484 if (lip == NULL)
485 break;
David Chinner249a8c12008-02-05 12:13:32 +1100486 lsn = lip->li_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
Eric Sandeene4a1e292014-04-14 19:06:05 +1000488 xfs_trans_ail_cursor_done(&cur);
Matthew Wilcox57e80952018-03-07 14:59:39 -0800489 spin_unlock(&ailp->ail_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Matthew Wilcox57e80952018-03-07 14:59:39 -0800491 if (xfs_buf_delwri_submit_nowait(&ailp->ail_buf_list))
492 ailp->ail_log_flush++;
Dave Chinnerd808f612010-02-02 10:13:42 +1100493
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000494 if (!count || XFS_LSN_CMP(lsn, target) >= 0) {
Dave Chinner9e7004e2011-05-06 02:54:05 +0000495out_done:
David Chinner92d9cd12008-03-06 13:45:10 +1100496 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000497 * We reached the target or the AIL is empty, so wait a bit
498 * longer for I/O to complete and remove pushed items from the
499 * AIL before we start the next scan from the start of the AIL.
David Chinner92d9cd12008-03-06 13:45:10 +1100500 */
Dave Chinner453eac82010-01-11 11:49:58 +0000501 tout = 50;
Matthew Wilcox57e80952018-03-07 14:59:39 -0800502 ailp->ail_last_pushed_lsn = 0;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000503 } else if (((stuck + flushing) * 100) / count > 90) {
David Chinner249a8c12008-02-05 12:13:32 +1100504 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000505 * Either there is a lot of contention on the AIL or we are
506 * stuck due to operations in progress. "Stuck" in this case
507 * is defined as >90% of the items we tried to push were stuck.
David Chinner249a8c12008-02-05 12:13:32 +1100508 *
509 * Backoff a bit more to allow some I/O to complete before
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000510 * restarting from the start of the AIL. This prevents us from
511 * spinning on the same items, and if they are pinned will all
512 * the restart to issue a log force to unpin the stuck items.
David Chinner249a8c12008-02-05 12:13:32 +1100513 */
Dave Chinner453eac82010-01-11 11:49:58 +0000514 tout = 20;
Matthew Wilcox57e80952018-03-07 14:59:39 -0800515 ailp->ail_last_pushed_lsn = 0;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000516 } else {
517 /*
518 * Assume we have more work to do in a short while.
519 */
520 tout = 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
Dave Chinner0bf6a5b2011-04-08 12:45:07 +1000522
Christoph Hellwig00308072011-10-11 11:14:10 -0400523 return tout;
524}
525
526static int
527xfsaild(
528 void *data)
529{
530 struct xfs_ail *ailp = data;
531 long tout = 0; /* milliseconds */
532
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000533 current->flags |= PF_MEMALLOC;
Michal Hocko18f1df42016-02-08 14:59:07 +1100534 set_freezable();
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000535
Hou Tao0bd89672017-10-17 14:16:28 -0700536 while (1) {
Christoph Hellwig00308072011-10-11 11:14:10 -0400537 if (tout && tout <= 20)
Hou Tao0bd89672017-10-17 14:16:28 -0700538 set_current_state(TASK_KILLABLE);
Christoph Hellwig00308072011-10-11 11:14:10 -0400539 else
Hou Tao0bd89672017-10-17 14:16:28 -0700540 set_current_state(TASK_INTERRUPTIBLE);
541
542 /*
Brian Fosterefc32892018-10-18 17:21:49 +1100543 * Check kthread_should_stop() after we set the task state to
544 * guarantee that we either see the stop bit and exit or the
545 * task state is reset to runnable such that it's not scheduled
546 * out indefinitely and detects the stop bit at next iteration.
Hou Tao0bd89672017-10-17 14:16:28 -0700547 * A memory barrier is included in above task state set to
548 * serialize again kthread_stop().
549 */
550 if (kthread_should_stop()) {
551 __set_current_state(TASK_RUNNING);
Brian Fosterefc32892018-10-18 17:21:49 +1100552
553 /*
554 * The caller forces out the AIL before stopping the
555 * thread in the common case, which means the delwri
556 * queue is drained. In the shutdown case, the queue may
557 * still hold relogged buffers that haven't been
558 * submitted because they were pinned since added to the
559 * queue.
560 *
561 * Log I/O error processing stales the underlying buffer
562 * and clears the delwri state, expecting the buf to be
563 * removed on the next submission attempt. That won't
564 * happen if we're shutting down, so this is the last
565 * opportunity to release such buffers from the queue.
566 */
567 ASSERT(list_empty(&ailp->ail_buf_list) ||
568 XFS_FORCED_SHUTDOWN(ailp->ail_mount));
569 xfs_buf_delwri_cancel(&ailp->ail_buf_list);
Hou Tao0bd89672017-10-17 14:16:28 -0700570 break;
571 }
Brian Foster8375f922012-06-28 06:52:56 -0400572
Matthew Wilcox57e80952018-03-07 14:59:39 -0800573 spin_lock(&ailp->ail_lock);
Brian Foster8375f922012-06-28 06:52:56 -0400574
575 /*
576 * Idle if the AIL is empty and we are not racing with a target
577 * update. We check the AIL after we set the task to a sleep
Matthew Wilcox57e80952018-03-07 14:59:39 -0800578 * state to guarantee that we either catch an ail_target update
Brian Foster8375f922012-06-28 06:52:56 -0400579 * or that a wake_up resets the state to TASK_RUNNING.
580 * Otherwise, we run the risk of sleeping indefinitely.
581 *
Matthew Wilcox57e80952018-03-07 14:59:39 -0800582 * The barrier matches the ail_target update in xfs_ail_push().
Brian Foster8375f922012-06-28 06:52:56 -0400583 */
584 smp_rmb();
585 if (!xfs_ail_min(ailp) &&
Matthew Wilcox57e80952018-03-07 14:59:39 -0800586 ailp->ail_target == ailp->ail_target_prev) {
587 spin_unlock(&ailp->ail_lock);
Michal Hocko18f1df42016-02-08 14:59:07 +1100588 freezable_schedule();
Brian Foster8375f922012-06-28 06:52:56 -0400589 tout = 0;
590 continue;
591 }
Matthew Wilcox57e80952018-03-07 14:59:39 -0800592 spin_unlock(&ailp->ail_lock);
Brian Foster8375f922012-06-28 06:52:56 -0400593
594 if (tout)
Michal Hocko18f1df42016-02-08 14:59:07 +1100595 freezable_schedule_timeout(msecs_to_jiffies(tout));
Brian Foster8375f922012-06-28 06:52:56 -0400596
597 __set_current_state(TASK_RUNNING);
Christoph Hellwig00308072011-10-11 11:14:10 -0400598
599 try_to_freeze();
600
601 tout = xfsaild_push(ailp);
602 }
603
604 return 0;
Dave Chinner453eac82010-01-11 11:49:58 +0000605}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Dave Chinner0bf6a5b2011-04-08 12:45:07 +1000607/*
608 * This routine is called to move the tail of the AIL forward. It does this by
609 * trying to flush items in the AIL whose lsns are below the given
610 * threshold_lsn.
611 *
612 * The push is run asynchronously in a workqueue, which means the caller needs
613 * to handle waiting on the async flush for space to become available.
614 * We don't want to interrupt any push that is in progress, hence we only queue
Joe Perchescf085a12019-11-07 13:24:52 -0800615 * work if we set the pushing bit appropriately.
Dave Chinner0bf6a5b2011-04-08 12:45:07 +1000616 *
617 * We do this unlocked - we only need to know whether there is anything in the
618 * AIL at the time we are called. We don't need to access the contents of
619 * any of the objects, so the lock is not needed.
620 */
621void
Dave Chinnerfd074842011-04-08 12:45:07 +1000622xfs_ail_push(
Christoph Hellwigefe23302019-06-28 19:27:33 -0700623 struct xfs_ail *ailp,
624 xfs_lsn_t threshold_lsn)
Dave Chinner0bf6a5b2011-04-08 12:45:07 +1000625{
Christoph Hellwigefe23302019-06-28 19:27:33 -0700626 struct xfs_log_item *lip;
Dave Chinner0bf6a5b2011-04-08 12:45:07 +1000627
628 lip = xfs_ail_min(ailp);
Matthew Wilcox57e80952018-03-07 14:59:39 -0800629 if (!lip || XFS_FORCED_SHUTDOWN(ailp->ail_mount) ||
630 XFS_LSN_CMP(threshold_lsn, ailp->ail_target) <= 0)
Dave Chinner0bf6a5b2011-04-08 12:45:07 +1000631 return;
632
633 /*
634 * Ensure that the new target is noticed in push code before it clears
635 * the XFS_AIL_PUSHING_BIT.
636 */
637 smp_wmb();
Matthew Wilcox57e80952018-03-07 14:59:39 -0800638 xfs_trans_ail_copy_lsn(ailp, &ailp->ail_target, &threshold_lsn);
Christoph Hellwig00308072011-10-11 11:14:10 -0400639 smp_wmb();
640
Matthew Wilcox57e80952018-03-07 14:59:39 -0800641 wake_up_process(ailp->ail_task);
Dave Chinner0bf6a5b2011-04-08 12:45:07 +1000642}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644/*
Dave Chinnerfd074842011-04-08 12:45:07 +1000645 * Push out all items in the AIL immediately
646 */
647void
648xfs_ail_push_all(
649 struct xfs_ail *ailp)
650{
651 xfs_lsn_t threshold_lsn = xfs_ail_max_lsn(ailp);
652
653 if (threshold_lsn)
654 xfs_ail_push(ailp, threshold_lsn);
655}
656
657/*
Christoph Hellwig211e4d42012-04-23 15:58:34 +1000658 * Push out all items in the AIL immediately and wait until the AIL is empty.
659 */
660void
661xfs_ail_push_all_sync(
662 struct xfs_ail *ailp)
663{
664 struct xfs_log_item *lip;
665 DEFINE_WAIT(wait);
666
Matthew Wilcox57e80952018-03-07 14:59:39 -0800667 spin_lock(&ailp->ail_lock);
Christoph Hellwig211e4d42012-04-23 15:58:34 +1000668 while ((lip = xfs_ail_max(ailp)) != NULL) {
Matthew Wilcox57e80952018-03-07 14:59:39 -0800669 prepare_to_wait(&ailp->ail_empty, &wait, TASK_UNINTERRUPTIBLE);
670 ailp->ail_target = lip->li_lsn;
671 wake_up_process(ailp->ail_task);
672 spin_unlock(&ailp->ail_lock);
Christoph Hellwig211e4d42012-04-23 15:58:34 +1000673 schedule();
Matthew Wilcox57e80952018-03-07 14:59:39 -0800674 spin_lock(&ailp->ail_lock);
Christoph Hellwig211e4d42012-04-23 15:58:34 +1000675 }
Matthew Wilcox57e80952018-03-07 14:59:39 -0800676 spin_unlock(&ailp->ail_lock);
Christoph Hellwig211e4d42012-04-23 15:58:34 +1000677
Matthew Wilcox57e80952018-03-07 14:59:39 -0800678 finish_wait(&ailp->ail_empty, &wait);
Christoph Hellwig211e4d42012-04-23 15:58:34 +1000679}
680
681/*
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100682 * xfs_trans_ail_update - bulk AIL insertion operation.
683 *
684 * @xfs_trans_ail_update takes an array of log items that all need to be
685 * positioned at the same LSN in the AIL. If an item is not in the AIL, it will
686 * be added. Otherwise, it will be repositioned by removing it and re-adding
687 * it to the AIL. If we move the first item in the AIL, update the log tail to
688 * match the new minimum LSN in the AIL.
689 *
690 * This function takes the AIL lock once to execute the update operations on
691 * all the items in the array, and as such should not be called with the AIL
692 * lock held. As a result, once we have the AIL lock, we need to check each log
693 * item LSN to confirm it needs to be moved forward in the AIL.
694 *
695 * To optimise the insert operation, we delete all the items from the AIL in
696 * the first pass, moving them into a temporary list, then splice the temporary
697 * list into the correct position in the AIL. This avoids needing to do an
698 * insert operation on every item.
699 *
700 * This function must be called with the AIL lock held. The lock is dropped
701 * before returning.
702 */
703void
704xfs_trans_ail_update_bulk(
705 struct xfs_ail *ailp,
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000706 struct xfs_ail_cursor *cur,
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100707 struct xfs_log_item **log_items,
708 int nr_items,
Matthew Wilcox57e80952018-03-07 14:59:39 -0800709 xfs_lsn_t lsn) __releases(ailp->ail_lock)
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100710{
Christoph Hellwigefe23302019-06-28 19:27:33 -0700711 struct xfs_log_item *mlip;
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100712 int mlip_changed = 0;
713 int i;
714 LIST_HEAD(tmp);
715
Alex Eldere44f4112011-07-22 16:04:41 +0000716 ASSERT(nr_items > 0); /* Not required, but true. */
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100717 mlip = xfs_ail_min(ailp);
718
719 for (i = 0; i < nr_items; i++) {
720 struct xfs_log_item *lip = log_items[i];
Dave Chinner22525c12018-05-09 07:47:34 -0700721 if (test_and_set_bit(XFS_LI_IN_AIL, &lip->li_flags)) {
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100722 /* check if we really need to move the item */
723 if (XFS_LSN_CMP(lsn, lip->li_lsn) <= 0)
724 continue;
725
Dave Chinner750b9c92013-11-01 15:27:18 +1100726 trace_xfs_ail_move(lip, lip->li_lsn, lsn);
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100727 xfs_ail_delete(ailp, lip);
728 if (mlip == lip)
729 mlip_changed = 1;
730 } else {
Dave Chinner750b9c92013-11-01 15:27:18 +1100731 trace_xfs_ail_insert(lip, 0, lsn);
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100732 }
733 lip->li_lsn = lsn;
734 list_add(&lip->li_ail, &tmp);
735 }
736
Alex Eldere44f4112011-07-22 16:04:41 +0000737 if (!list_empty(&tmp))
738 xfs_ail_splice(ailp, cur, &tmp, lsn);
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000739
Christoph Hellwig1c304622012-04-23 15:58:33 +1000740 if (mlip_changed) {
Matthew Wilcox57e80952018-03-07 14:59:39 -0800741 if (!XFS_FORCED_SHUTDOWN(ailp->ail_mount))
742 xlog_assign_tail_lsn_locked(ailp->ail_mount);
743 spin_unlock(&ailp->ail_lock);
Christoph Hellwig1c304622012-04-23 15:58:33 +1000744
Matthew Wilcox57e80952018-03-07 14:59:39 -0800745 xfs_log_space_wake(ailp->ail_mount);
Christoph Hellwig1c304622012-04-23 15:58:33 +1000746 } else {
Matthew Wilcox57e80952018-03-07 14:59:39 -0800747 spin_unlock(&ailp->ail_lock);
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000748 }
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100749}
750
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700751bool
752xfs_ail_delete_one(
753 struct xfs_ail *ailp,
Carlos Maiolinod3a304b2017-08-08 18:21:50 -0700754 struct xfs_log_item *lip)
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700755{
756 struct xfs_log_item *mlip = xfs_ail_min(ailp);
757
758 trace_xfs_ail_delete(lip, mlip->li_lsn, lip->li_lsn);
759 xfs_ail_delete(ailp, lip);
Carlos Maiolinod3a304b2017-08-08 18:21:50 -0700760 xfs_clear_li_failed(lip);
Dave Chinner22525c12018-05-09 07:47:34 -0700761 clear_bit(XFS_LI_IN_AIL, &lip->li_flags);
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700762 lip->li_lsn = 0;
763
764 return mlip == lip;
765}
766
767/**
768 * Remove a log items from the AIL
Dave Chinner30136832010-12-20 12:03:17 +1100769 *
770 * @xfs_trans_ail_delete_bulk takes an array of log items that all need to
771 * removed from the AIL. The caller is already holding the AIL lock, and done
772 * all the checks necessary to ensure the items passed in via @log_items are
773 * ready for deletion. This includes checking that the items are in the AIL.
774 *
775 * For each log item to be removed, unlink it from the AIL, clear the IN_AIL
776 * flag from the item and reset the item's lsn to 0. If we remove the first
777 * item in the AIL, update the log tail to match the new minimum LSN in the
778 * AIL.
779 *
780 * This function will not drop the AIL lock until all items are removed from
781 * the AIL to minimise the amount of lock traffic on the AIL. This does not
782 * greatly increase the AIL hold time, but does significantly reduce the amount
783 * of traffic on the lock, especially during IO completion.
784 *
785 * This function must be called with the AIL lock held. The lock is dropped
786 * before returning.
787 */
788void
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700789xfs_trans_ail_delete(
Dave Chinner30136832010-12-20 12:03:17 +1100790 struct xfs_ail *ailp,
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700791 struct xfs_log_item *lip,
Matthew Wilcox57e80952018-03-07 14:59:39 -0800792 int shutdown_type) __releases(ailp->ail_lock)
Dave Chinner30136832010-12-20 12:03:17 +1100793{
Matthew Wilcox57e80952018-03-07 14:59:39 -0800794 struct xfs_mount *mp = ailp->ail_mount;
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700795 bool mlip_changed;
Dave Chinner30136832010-12-20 12:03:17 +1100796
Dave Chinner22525c12018-05-09 07:47:34 -0700797 if (!test_bit(XFS_LI_IN_AIL, &lip->li_flags)) {
Matthew Wilcox57e80952018-03-07 14:59:39 -0800798 spin_unlock(&ailp->ail_lock);
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700799 if (!XFS_FORCED_SHUTDOWN(mp)) {
800 xfs_alert_tag(mp, XFS_PTAG_AILDELETE,
801 "%s: attempting to delete a log item that is not in the AIL",
802 __func__);
803 xfs_force_shutdown(mp, shutdown_type);
Dave Chinner30136832010-12-20 12:03:17 +1100804 }
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700805 return;
Dave Chinner30136832010-12-20 12:03:17 +1100806 }
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000807
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700808 mlip_changed = xfs_ail_delete_one(ailp, lip);
Christoph Hellwig1c304622012-04-23 15:58:33 +1000809 if (mlip_changed) {
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700810 if (!XFS_FORCED_SHUTDOWN(mp))
811 xlog_assign_tail_lsn_locked(mp);
Matthew Wilcox57e80952018-03-07 14:59:39 -0800812 if (list_empty(&ailp->ail_head))
813 wake_up_all(&ailp->ail_empty);
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000814 }
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700815
Matthew Wilcox57e80952018-03-07 14:59:39 -0800816 spin_unlock(&ailp->ail_lock);
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700817 if (mlip_changed)
Matthew Wilcox57e80952018-03-07 14:59:39 -0800818 xfs_log_space_wake(ailp->ail_mount);
Dave Chinner30136832010-12-20 12:03:17 +1100819}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
David Chinner249a8c12008-02-05 12:13:32 +1100821int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822xfs_trans_ail_init(
823 xfs_mount_t *mp)
824{
David Chinner82fa9012008-10-30 17:38:26 +1100825 struct xfs_ail *ailp;
826
827 ailp = kmem_zalloc(sizeof(struct xfs_ail), KM_MAYFAIL);
828 if (!ailp)
Dave Chinner24513372014-06-25 14:58:08 +1000829 return -ENOMEM;
David Chinner82fa9012008-10-30 17:38:26 +1100830
Matthew Wilcox57e80952018-03-07 14:59:39 -0800831 ailp->ail_mount = mp;
832 INIT_LIST_HEAD(&ailp->ail_head);
833 INIT_LIST_HEAD(&ailp->ail_cursors);
834 spin_lock_init(&ailp->ail_lock);
835 INIT_LIST_HEAD(&ailp->ail_buf_list);
836 init_waitqueue_head(&ailp->ail_empty);
Christoph Hellwig00308072011-10-11 11:14:10 -0400837
Matthew Wilcox57e80952018-03-07 14:59:39 -0800838 ailp->ail_task = kthread_run(xfsaild, ailp, "xfsaild/%s",
Ian Kente1d3d212019-11-04 13:58:40 -0800839 ailp->ail_mount->m_super->s_id);
Matthew Wilcox57e80952018-03-07 14:59:39 -0800840 if (IS_ERR(ailp->ail_task))
Christoph Hellwig00308072011-10-11 11:14:10 -0400841 goto out_free_ailp;
842
David Chinner27d8d5f2008-10-30 17:38:39 +1100843 mp->m_ail = ailp;
844 return 0;
Christoph Hellwig00308072011-10-11 11:14:10 -0400845
846out_free_ailp:
847 kmem_free(ailp);
Dave Chinner24513372014-06-25 14:58:08 +1000848 return -ENOMEM;
David Chinner249a8c12008-02-05 12:13:32 +1100849}
850
851void
852xfs_trans_ail_destroy(
853 xfs_mount_t *mp)
854{
David Chinner82fa9012008-10-30 17:38:26 +1100855 struct xfs_ail *ailp = mp->m_ail;
856
Matthew Wilcox57e80952018-03-07 14:59:39 -0800857 kthread_stop(ailp->ail_task);
David Chinner82fa9012008-10-30 17:38:26 +1100858 kmem_free(ailp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}