blob: 2574d01e4a834c6b6404acca87f1f273bc540e2d [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)
Jules Irengedaebba12020-02-26 09:37:15 -080035 __must_hold(&ailp->ail_lock)
Dave Chinnercd4a3c52011-04-08 12:45:07 +100036{
Dave Chinnerd686d122018-05-09 07:49:09 -070037 struct xfs_log_item *prev_lip;
38 struct xfs_log_item *next_lip;
39 xfs_lsn_t prev_lsn = NULLCOMMITLSN;
40 xfs_lsn_t next_lsn = NULLCOMMITLSN;
41 xfs_lsn_t lsn;
42 bool in_ail;
43
Dave Chinnercd4a3c52011-04-08 12:45:07 +100044
Matthew Wilcox57e80952018-03-07 14:59:39 -080045 if (list_empty(&ailp->ail_head))
Dave Chinnercd4a3c52011-04-08 12:45:07 +100046 return;
47
48 /*
Dave Chinnerd686d122018-05-09 07:49:09 -070049 * Sample then check the next and previous entries are valid.
Dave Chinnercd4a3c52011-04-08 12:45:07 +100050 */
Dave Chinnerd686d122018-05-09 07:49:09 -070051 in_ail = test_bit(XFS_LI_IN_AIL, &lip->li_flags);
52 prev_lip = list_entry(lip->li_ail.prev, struct xfs_log_item, li_ail);
Matthew Wilcox57e80952018-03-07 14:59:39 -080053 if (&prev_lip->li_ail != &ailp->ail_head)
Dave Chinnerd686d122018-05-09 07:49:09 -070054 prev_lsn = prev_lip->li_lsn;
55 next_lip = list_entry(lip->li_ail.next, struct xfs_log_item, li_ail);
56 if (&next_lip->li_ail != &ailp->ail_head)
57 next_lsn = next_lip->li_lsn;
58 lsn = lip->li_lsn;
Dave Chinnercd4a3c52011-04-08 12:45:07 +100059
Dave Chinnerd686d122018-05-09 07:49:09 -070060 if (in_ail &&
61 (prev_lsn == NULLCOMMITLSN || XFS_LSN_CMP(prev_lsn, lsn) <= 0) &&
62 (next_lsn == NULLCOMMITLSN || XFS_LSN_CMP(next_lsn, lsn) >= 0))
63 return;
Dave Chinnercd4a3c52011-04-08 12:45:07 +100064
Dave Chinnerd686d122018-05-09 07:49:09 -070065 spin_unlock(&ailp->ail_lock);
66 ASSERT(in_ail);
67 ASSERT(prev_lsn == NULLCOMMITLSN || XFS_LSN_CMP(prev_lsn, lsn) <= 0);
68 ASSERT(next_lsn == NULLCOMMITLSN || XFS_LSN_CMP(next_lsn, lsn) >= 0);
69 spin_lock(&ailp->ail_lock);
Dave Chinnercd4a3c52011-04-08 12:45:07 +100070}
71#else /* !DEBUG */
David Chinnerde08dbc2008-02-05 12:13:38 +110072#define xfs_ail_check(a,l)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#endif /* DEBUG */
74
Dave Chinnercd4a3c52011-04-08 12:45:07 +100075/*
Dave Chinnerfd074842011-04-08 12:45:07 +100076 * Return a pointer to the last item in the AIL. If the AIL is empty, then
77 * return NULL.
78 */
Christoph Hellwigefe23302019-06-28 19:27:33 -070079static struct xfs_log_item *
Dave Chinnerfd074842011-04-08 12:45:07 +100080xfs_ail_max(
81 struct xfs_ail *ailp)
82{
Matthew Wilcox57e80952018-03-07 14:59:39 -080083 if (list_empty(&ailp->ail_head))
Dave Chinnerfd074842011-04-08 12:45:07 +100084 return NULL;
85
Christoph Hellwigefe23302019-06-28 19:27:33 -070086 return list_entry(ailp->ail_head.prev, struct xfs_log_item, li_ail);
Dave Chinnerfd074842011-04-08 12:45:07 +100087}
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/*
Dave Chinnercd4a3c52011-04-08 12:45:07 +100090 * Return a pointer to the item which follows the given item in the AIL. If
91 * the given item is the last item in the list, then return NULL.
92 */
Christoph Hellwigefe23302019-06-28 19:27:33 -070093static struct xfs_log_item *
Dave Chinnercd4a3c52011-04-08 12:45:07 +100094xfs_ail_next(
Christoph Hellwigefe23302019-06-28 19:27:33 -070095 struct xfs_ail *ailp,
96 struct xfs_log_item *lip)
Dave Chinnercd4a3c52011-04-08 12:45:07 +100097{
Matthew Wilcox57e80952018-03-07 14:59:39 -080098 if (lip->li_ail.next == &ailp->ail_head)
Dave Chinnercd4a3c52011-04-08 12:45:07 +100099 return NULL;
100
Christoph Hellwigefe23302019-06-28 19:27:33 -0700101 return list_first_entry(&lip->li_ail, struct xfs_log_item, li_ail);
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000102}
103
104/*
105 * This is called by the log manager code to determine the LSN of the tail of
106 * the log. This is exactly the LSN of the first item in the AIL. If the AIL
107 * is empty, then this function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 *
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000109 * We need the AIL lock in order to get a coherent read of the lsn of the last
110 * item in the AIL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 */
Dave Chinner8eb807b2020-03-24 20:10:29 -0700112static xfs_lsn_t
113__xfs_ail_min_lsn(
114 struct xfs_ail *ailp)
115{
116 struct xfs_log_item *lip = xfs_ail_min(ailp);
117
118 if (lip)
119 return lip->li_lsn;
120 return 0;
121}
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123xfs_lsn_t
Dave Chinnerfd074842011-04-08 12:45:07 +1000124xfs_ail_min_lsn(
Christoph Hellwigefe23302019-06-28 19:27:33 -0700125 struct xfs_ail *ailp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Dave Chinner8eb807b2020-03-24 20:10:29 -0700127 xfs_lsn_t lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Matthew Wilcox57e80952018-03-07 14:59:39 -0800129 spin_lock(&ailp->ail_lock);
Dave Chinner8eb807b2020-03-24 20:10:29 -0700130 lsn = __xfs_ail_min_lsn(ailp);
Matthew Wilcox57e80952018-03-07 14:59:39 -0800131 spin_unlock(&ailp->ail_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 return lsn;
134}
135
136/*
Dave Chinnerfd074842011-04-08 12:45:07 +1000137 * Return the maximum lsn held in the AIL, or zero if the AIL is empty.
138 */
139static xfs_lsn_t
140xfs_ail_max_lsn(
Christoph Hellwigefe23302019-06-28 19:27:33 -0700141 struct xfs_ail *ailp)
Dave Chinnerfd074842011-04-08 12:45:07 +1000142{
Christoph Hellwigefe23302019-06-28 19:27:33 -0700143 xfs_lsn_t lsn = 0;
144 struct xfs_log_item *lip;
Dave Chinnerfd074842011-04-08 12:45:07 +1000145
Matthew Wilcox57e80952018-03-07 14:59:39 -0800146 spin_lock(&ailp->ail_lock);
Dave Chinnerfd074842011-04-08 12:45:07 +1000147 lip = xfs_ail_max(ailp);
148 if (lip)
149 lsn = lip->li_lsn;
Matthew Wilcox57e80952018-03-07 14:59:39 -0800150 spin_unlock(&ailp->ail_lock);
Dave Chinnerfd074842011-04-08 12:45:07 +1000151
152 return lsn;
153}
154
155/*
Dave Chinneraf3e4022011-07-18 03:40:18 +0000156 * The cursor keeps track of where our current traversal is up to by tracking
157 * the next item in the list for us. However, for this to be safe, removing an
158 * object from the AIL needs to invalidate any cursor that points to it. hence
159 * the traversal cursor needs to be linked to the struct xfs_ail so that
160 * deletion can search all the active cursors for invalidation.
David Chinner27d8d5f2008-10-30 17:38:39 +1100161 */
David Chinner5b00f142008-10-30 17:39:00 +1100162STATIC void
David Chinner27d8d5f2008-10-30 17:38:39 +1100163xfs_trans_ail_cursor_init(
164 struct xfs_ail *ailp,
165 struct xfs_ail_cursor *cur)
166{
167 cur->item = NULL;
Matthew Wilcox57e80952018-03-07 14:59:39 -0800168 list_add_tail(&cur->list, &ailp->ail_cursors);
David Chinner27d8d5f2008-10-30 17:38:39 +1100169}
170
171/*
Dave Chinneraf3e4022011-07-18 03:40:18 +0000172 * Get the next item in the traversal and advance the cursor. If the cursor
173 * was invalidated (indicated by a lip of 1), restart the traversal.
David Chinner27d8d5f2008-10-30 17:38:39 +1100174 */
David Chinner5b00f142008-10-30 17:39:00 +1100175struct xfs_log_item *
David Chinner27d8d5f2008-10-30 17:38:39 +1100176xfs_trans_ail_cursor_next(
177 struct xfs_ail *ailp,
178 struct xfs_ail_cursor *cur)
179{
180 struct xfs_log_item *lip = cur->item;
181
Christoph Hellwigdb9d67d2015-06-22 09:43:32 +1000182 if ((uintptr_t)lip & 1)
David Chinner27d8d5f2008-10-30 17:38:39 +1100183 lip = xfs_ail_min(ailp);
Dave Chinner16b59022011-07-18 03:40:17 +0000184 if (lip)
185 cur->item = xfs_ail_next(ailp, lip);
David Chinner27d8d5f2008-10-30 17:38:39 +1100186 return lip;
187}
188
189/*
Dave Chinneraf3e4022011-07-18 03:40:18 +0000190 * When the traversal is complete, we need to remove the cursor from the list
191 * of traversing cursors.
David Chinner27d8d5f2008-10-30 17:38:39 +1100192 */
193void
194xfs_trans_ail_cursor_done(
Dave Chinneraf3e4022011-07-18 03:40:18 +0000195 struct xfs_ail_cursor *cur)
David Chinner27d8d5f2008-10-30 17:38:39 +1100196{
Dave Chinneraf3e4022011-07-18 03:40:18 +0000197 cur->item = NULL;
198 list_del_init(&cur->list);
David Chinner27d8d5f2008-10-30 17:38:39 +1100199}
200
201/*
Dave Chinneraf3e4022011-07-18 03:40:18 +0000202 * Invalidate any cursor that is pointing to this item. This is called when an
203 * item is removed from the AIL. Any cursor pointing to this object is now
204 * invalid and the traversal needs to be terminated so it doesn't reference a
205 * freed object. We set the low bit of the cursor item pointer so we can
206 * distinguish between an invalidation and the end of the list when getting the
207 * next item from the cursor.
David Chinner5b00f142008-10-30 17:39:00 +1100208 */
209STATIC void
210xfs_trans_ail_cursor_clear(
211 struct xfs_ail *ailp,
212 struct xfs_log_item *lip)
213{
214 struct xfs_ail_cursor *cur;
215
Matthew Wilcox57e80952018-03-07 14:59:39 -0800216 list_for_each_entry(cur, &ailp->ail_cursors, list) {
David Chinner5b00f142008-10-30 17:39:00 +1100217 if (cur->item == lip)
218 cur->item = (struct xfs_log_item *)
Christoph Hellwigdb9d67d2015-06-22 09:43:32 +1000219 ((uintptr_t)cur->item | 1);
David Chinner5b00f142008-10-30 17:39:00 +1100220 }
221}
222
223/*
Dave Chinner16b59022011-07-18 03:40:17 +0000224 * Find the first item in the AIL with the given @lsn by searching in ascending
225 * LSN order and initialise the cursor to point to the next item for a
226 * ascending traversal. Pass a @lsn of zero to initialise the cursor to the
227 * first item in the AIL. Returns NULL if the list is empty.
David Chinner249a8c12008-02-05 12:13:32 +1100228 */
Christoph Hellwigefe23302019-06-28 19:27:33 -0700229struct xfs_log_item *
David Chinner5b00f142008-10-30 17:39:00 +1100230xfs_trans_ail_cursor_first(
David Chinner27d8d5f2008-10-30 17:38:39 +1100231 struct xfs_ail *ailp,
232 struct xfs_ail_cursor *cur,
233 xfs_lsn_t lsn)
David Chinner249a8c12008-02-05 12:13:32 +1100234{
Christoph Hellwigefe23302019-06-28 19:27:33 -0700235 struct xfs_log_item *lip;
David Chinner249a8c12008-02-05 12:13:32 +1100236
David Chinner5b00f142008-10-30 17:39:00 +1100237 xfs_trans_ail_cursor_init(ailp, cur);
Dave Chinner16b59022011-07-18 03:40:17 +0000238
239 if (lsn == 0) {
240 lip = xfs_ail_min(ailp);
David Chinner5b00f142008-10-30 17:39:00 +1100241 goto out;
Dave Chinner16b59022011-07-18 03:40:17 +0000242 }
David Chinner249a8c12008-02-05 12:13:32 +1100243
Matthew Wilcox57e80952018-03-07 14:59:39 -0800244 list_for_each_entry(lip, &ailp->ail_head, li_ail) {
David Chinner5b00f142008-10-30 17:39:00 +1100245 if (XFS_LSN_CMP(lip->li_lsn, lsn) >= 0)
David Chinner7ee49ac2008-10-30 18:26:51 +1100246 goto out;
Josef 'Jeff' Sipek535f6b32008-03-27 17:58:27 +1100247 }
Dave Chinner16b59022011-07-18 03:40:17 +0000248 return NULL;
249
David Chinner5b00f142008-10-30 17:39:00 +1100250out:
Dave Chinner16b59022011-07-18 03:40:17 +0000251 if (lip)
252 cur->item = xfs_ail_next(ailp, lip);
David Chinner5b00f142008-10-30 17:39:00 +1100253 return lip;
David Chinner249a8c12008-02-05 12:13:32 +1100254}
255
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000256static struct xfs_log_item *
257__xfs_trans_ail_cursor_last(
258 struct xfs_ail *ailp,
259 xfs_lsn_t lsn)
260{
Christoph Hellwigefe23302019-06-28 19:27:33 -0700261 struct xfs_log_item *lip;
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000262
Matthew Wilcox57e80952018-03-07 14:59:39 -0800263 list_for_each_entry_reverse(lip, &ailp->ail_head, li_ail) {
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000264 if (XFS_LSN_CMP(lip->li_lsn, lsn) <= 0)
265 return lip;
266 }
267 return NULL;
268}
269
270/*
Dave Chinner16b59022011-07-18 03:40:17 +0000271 * Find the last item in the AIL with the given @lsn by searching in descending
272 * LSN order and initialise the cursor to point to that item. If there is no
273 * item with the value of @lsn, then it sets the cursor to the last item with an
274 * LSN lower than @lsn. Returns NULL if the list is empty.
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000275 */
276struct xfs_log_item *
277xfs_trans_ail_cursor_last(
278 struct xfs_ail *ailp,
279 struct xfs_ail_cursor *cur,
280 xfs_lsn_t lsn)
281{
282 xfs_trans_ail_cursor_init(ailp, cur);
283 cur->item = __xfs_trans_ail_cursor_last(ailp, lsn);
284 return cur->item;
285}
286
287/*
Dave Chinner16b59022011-07-18 03:40:17 +0000288 * Splice the log item list into the AIL at the given LSN. We splice to the
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000289 * tail of the given LSN to maintain insert order for push traversals. The
290 * cursor is optional, allowing repeated updates to the same LSN to avoid
Alex Eldere44f4112011-07-22 16:04:41 +0000291 * repeated traversals. This should not be called with an empty list.
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000292 */
293static void
294xfs_ail_splice(
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000295 struct xfs_ail *ailp,
296 struct xfs_ail_cursor *cur,
297 struct list_head *list,
298 xfs_lsn_t lsn)
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000299{
Alex Eldere44f4112011-07-22 16:04:41 +0000300 struct xfs_log_item *lip;
301
302 ASSERT(!list_empty(list));
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000303
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000304 /*
Alex Eldere44f4112011-07-22 16:04:41 +0000305 * Use the cursor to determine the insertion point if one is
306 * provided. If not, or if the one we got is not valid,
307 * find the place in the AIL where the items belong.
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000308 */
Alex Eldere44f4112011-07-22 16:04:41 +0000309 lip = cur ? cur->item : NULL;
Christoph Hellwigdb9d67d2015-06-22 09:43:32 +1000310 if (!lip || (uintptr_t)lip & 1)
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000311 lip = __xfs_trans_ail_cursor_last(ailp, lsn);
312
Alex Eldere44f4112011-07-22 16:04:41 +0000313 /*
314 * If a cursor is provided, we know we're processing the AIL
315 * in lsn order, and future items to be spliced in will
316 * follow the last one being inserted now. Update the
317 * cursor to point to that last item, now while we have a
318 * reliable pointer to it.
319 */
320 if (cur)
321 cur->item = list_entry(list->prev, struct xfs_log_item, li_ail);
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000322
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000323 /*
Alex Eldere44f4112011-07-22 16:04:41 +0000324 * Finally perform the splice. Unless the AIL was empty,
325 * lip points to the item in the AIL _after_ which the new
326 * items should go. If lip is null the AIL was empty, so
327 * the new items go at the head of the AIL.
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000328 */
Alex Eldere44f4112011-07-22 16:04:41 +0000329 if (lip)
330 list_splice(list, &lip->li_ail);
331 else
Matthew Wilcox57e80952018-03-07 14:59:39 -0800332 list_splice(list, &ailp->ail_head);
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000333}
334
335/*
336 * Delete the given item from the AIL. Return a pointer to the item.
337 */
338static void
339xfs_ail_delete(
Christoph Hellwigefe23302019-06-28 19:27:33 -0700340 struct xfs_ail *ailp,
341 struct xfs_log_item *lip)
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000342{
343 xfs_ail_check(ailp, lip);
344 list_del(&lip->li_ail);
345 xfs_trans_ail_cursor_clear(ailp, lip);
346}
347
Brian Fostercb6ad092020-05-06 13:25:19 -0700348/*
349 * Requeue a failed buffer for writeback.
350 *
351 * We clear the log item failed state here as well, but we have to be careful
352 * about reference counts because the only active reference counts on the buffer
353 * may be the failed log items. Hence if we clear the log item failed state
354 * before queuing the buffer for IO we can release all active references to
355 * the buffer and free it, leading to use after free problems in
356 * xfs_buf_delwri_queue. It makes no difference to the buffer or log items which
357 * order we process them in - the buffer is locked, and we own the buffer list
358 * so nothing on them is going to change while we are performing this action.
359 *
360 * Hence we can safely queue the buffer for IO before we clear the failed log
361 * item state, therefore always having an active reference to the buffer and
362 * avoiding the transient zero-reference state that leads to use-after-free.
363 */
364static inline int
365xfsaild_resubmit_item(
366 struct xfs_log_item *lip,
367 struct list_head *buffer_list)
368{
369 struct xfs_buf *bp = lip->li_buf;
370
371 if (!xfs_buf_trylock(bp))
372 return XFS_ITEM_LOCKED;
373
374 if (!xfs_buf_delwri_queue(bp, buffer_list)) {
375 xfs_buf_unlock(bp);
376 return XFS_ITEM_FLUSHING;
377 }
378
379 /* protected by ail_lock */
380 list_for_each_entry(lip, &bp->b_li_list, li_bio_list)
381 xfs_clear_li_failed(lip);
382
383 xfs_buf_unlock(bp);
384 return XFS_ITEM_SUCCESS;
385}
386
Brian Foster7f4d01f2017-08-08 18:21:52 -0700387static inline uint
388xfsaild_push_item(
389 struct xfs_ail *ailp,
390 struct xfs_log_item *lip)
391{
392 /*
393 * If log item pinning is enabled, skip the push and track the item as
394 * pinned. This can help induce head-behind-tail conditions.
395 */
Matthew Wilcox57e80952018-03-07 14:59:39 -0800396 if (XFS_TEST_ERROR(false, ailp->ail_mount, XFS_ERRTAG_LOG_ITEM_PIN))
Brian Foster7f4d01f2017-08-08 18:21:52 -0700397 return XFS_ITEM_PINNED;
398
Christoph Hellwige8b78db2019-06-28 19:27:30 -0700399 /*
400 * Consider the item pinned if a push callback is not defined so the
401 * caller will force the log. This should only happen for intent items
402 * as they are unpinned once the associated done item is committed to
403 * the on-disk log.
404 */
405 if (!lip->li_ops->iop_push)
406 return XFS_ITEM_PINNED;
Brian Fostercb6ad092020-05-06 13:25:19 -0700407 if (test_bit(XFS_LI_FAILED, &lip->li_flags))
408 return xfsaild_resubmit_item(lip, &ailp->ail_buf_list);
Matthew Wilcox57e80952018-03-07 14:59:39 -0800409 return lip->li_ops->iop_push(lip, &ailp->ail_buf_list);
Brian Foster7f4d01f2017-08-08 18:21:52 -0700410}
411
Christoph Hellwig00308072011-10-11 11:14:10 -0400412static long
413xfsaild_push(
414 struct xfs_ail *ailp)
David Chinner249a8c12008-02-05 12:13:32 +1100415{
Matthew Wilcox57e80952018-03-07 14:59:39 -0800416 xfs_mount_t *mp = ailp->ail_mount;
Dave Chinneraf3e4022011-07-18 03:40:18 +0000417 struct xfs_ail_cursor cur;
Christoph Hellwigefe23302019-06-28 19:27:33 -0700418 struct xfs_log_item *lip;
Dave Chinner9e7004e2011-05-06 02:54:05 +0000419 xfs_lsn_t lsn;
Dave Chinnerfe0da762011-05-06 02:54:07 +0000420 xfs_lsn_t target;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000421 long tout;
Dave Chinner9e7004e2011-05-06 02:54:05 +0000422 int stuck = 0;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000423 int flushing = 0;
Dave Chinner9e7004e2011-05-06 02:54:05 +0000424 int count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Dave Chinner670ce932011-09-30 04:45:03 +0000426 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000427 * If we encountered pinned items or did not finish writing out all
428 * buffers the last time we ran, force the log first and wait for it
429 * before pushing again.
Dave Chinner670ce932011-09-30 04:45:03 +0000430 */
Matthew Wilcox57e80952018-03-07 14:59:39 -0800431 if (ailp->ail_log_flush && ailp->ail_last_pushed_lsn == 0 &&
432 (!list_empty_careful(&ailp->ail_buf_list) ||
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000433 xfs_ail_min_lsn(ailp))) {
Matthew Wilcox57e80952018-03-07 14:59:39 -0800434 ailp->ail_log_flush = 0;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000435
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100436 XFS_STATS_INC(mp, xs_push_ail_flush);
Dave Chinner670ce932011-09-30 04:45:03 +0000437 xfs_log_force(mp, XFS_LOG_SYNC);
Dave Chinner670ce932011-09-30 04:45:03 +0000438 }
439
Matthew Wilcox57e80952018-03-07 14:59:39 -0800440 spin_lock(&ailp->ail_lock);
Brian Foster8375f922012-06-28 06:52:56 -0400441
Matthew Wilcox57e80952018-03-07 14:59:39 -0800442 /* barrier matches the ail_target update in xfs_ail_push() */
Brian Foster8375f922012-06-28 06:52:56 -0400443 smp_rmb();
Matthew Wilcox57e80952018-03-07 14:59:39 -0800444 target = ailp->ail_target;
445 ailp->ail_target_prev = target;
Brian Foster8375f922012-06-28 06:52:56 -0400446
Matthew Wilcox57e80952018-03-07 14:59:39 -0800447 lip = xfs_trans_ail_cursor_first(ailp, &cur, ailp->ail_last_pushed_lsn);
Christoph Hellwig211e4d42012-04-23 15:58:34 +1000448 if (!lip) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000450 * If the AIL is empty or our push has reached the end we are
451 * done now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 */
Eric Sandeene4a1e292014-04-14 19:06:05 +1000453 xfs_trans_ail_cursor_done(&cur);
Matthew Wilcox57e80952018-03-07 14:59:39 -0800454 spin_unlock(&ailp->ail_lock);
Dave Chinner9e7004e2011-05-06 02:54:05 +0000455 goto out_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
457
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100458 XFS_STATS_INC(mp, xs_push_ail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
David Chinner249a8c12008-02-05 12:13:32 +1100460 lsn = lip->li_lsn;
Dave Chinner50e86682011-05-06 02:54:06 +0000461 while ((XFS_LSN_CMP(lip->li_lsn, target) <= 0)) {
David Chinner249a8c12008-02-05 12:13:32 +1100462 int lock_result;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 /*
Dave Chinner904c17e2013-08-28 21:12:03 +1000465 * Note that iop_push may unlock and reacquire the AIL lock. We
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000466 * rely on the AIL cursor implementation to be able to deal with
467 * the dropped lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 */
Brian Foster7f4d01f2017-08-08 18:21:52 -0700469 lock_result = xfsaild_push_item(ailp, lip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 switch (lock_result) {
David Chinner249a8c12008-02-05 12:13:32 +1100471 case XFS_ITEM_SUCCESS:
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100472 XFS_STATS_INC(mp, xs_push_ail_success);
Christoph Hellwig9e4c1092011-10-11 15:14:11 +0000473 trace_xfs_ail_push(lip);
474
Matthew Wilcox57e80952018-03-07 14:59:39 -0800475 ailp->ail_last_pushed_lsn = lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 break;
477
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000478 case XFS_ITEM_FLUSHING:
479 /*
Joe Perchescf085a12019-11-07 13:24:52 -0800480 * The item or its backing buffer is already being
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000481 * flushed. The typical reason for that is that an
482 * inode buffer is locked because we already pushed the
483 * updates to it as part of inode clustering.
484 *
485 * We do not want to to stop flushing just because lots
Joe Perchescf085a12019-11-07 13:24:52 -0800486 * of items are already being flushed, but we need to
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000487 * re-try the flushing relatively soon if most of the
Joe Perchescf085a12019-11-07 13:24:52 -0800488 * AIL is being flushed.
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000489 */
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100490 XFS_STATS_INC(mp, xs_push_ail_flushing);
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000491 trace_xfs_ail_flushing(lip);
Christoph Hellwig17b38472011-10-11 15:14:09 +0000492
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000493 flushing++;
Matthew Wilcox57e80952018-03-07 14:59:39 -0800494 ailp->ail_last_pushed_lsn = lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 break;
496
David Chinner249a8c12008-02-05 12:13:32 +1100497 case XFS_ITEM_PINNED:
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100498 XFS_STATS_INC(mp, xs_push_ail_pinned);
Christoph Hellwig9e4c1092011-10-11 15:14:11 +0000499 trace_xfs_ail_pinned(lip);
500
David Chinner249a8c12008-02-05 12:13:32 +1100501 stuck++;
Matthew Wilcox57e80952018-03-07 14:59:39 -0800502 ailp->ail_log_flush++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 break;
David Chinner249a8c12008-02-05 12:13:32 +1100504 case XFS_ITEM_LOCKED:
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100505 XFS_STATS_INC(mp, xs_push_ail_locked);
Christoph Hellwig9e4c1092011-10-11 15:14:11 +0000506 trace_xfs_ail_locked(lip);
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000507
David Chinner249a8c12008-02-05 12:13:32 +1100508 stuck++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 break;
David Chinner249a8c12008-02-05 12:13:32 +1100510 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 ASSERT(0);
512 break;
513 }
514
David Chinner249a8c12008-02-05 12:13:32 +1100515 count++;
516
517 /*
518 * Are there too many items we can't do anything with?
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000519 *
David Chinner249a8c12008-02-05 12:13:32 +1100520 * If we we are skipping too many items because we can't flush
521 * them or they are already being flushed, we back off and
522 * given them time to complete whatever operation is being
523 * done. i.e. remove pressure from the AIL while we can't make
524 * progress so traversals don't slow down further inserts and
525 * removals to/from the AIL.
526 *
527 * The value of 100 is an arbitrary magic number based on
528 * observation.
529 */
530 if (stuck > 100)
531 break;
532
Dave Chinneraf3e4022011-07-18 03:40:18 +0000533 lip = xfs_trans_ail_cursor_next(ailp, &cur);
David Chinner249a8c12008-02-05 12:13:32 +1100534 if (lip == NULL)
535 break;
David Chinner249a8c12008-02-05 12:13:32 +1100536 lsn = lip->li_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
Eric Sandeene4a1e292014-04-14 19:06:05 +1000538 xfs_trans_ail_cursor_done(&cur);
Matthew Wilcox57e80952018-03-07 14:59:39 -0800539 spin_unlock(&ailp->ail_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Matthew Wilcox57e80952018-03-07 14:59:39 -0800541 if (xfs_buf_delwri_submit_nowait(&ailp->ail_buf_list))
542 ailp->ail_log_flush++;
Dave Chinnerd808f612010-02-02 10:13:42 +1100543
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000544 if (!count || XFS_LSN_CMP(lsn, target) >= 0) {
Dave Chinner9e7004e2011-05-06 02:54:05 +0000545out_done:
David Chinner92d9cd12008-03-06 13:45:10 +1100546 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000547 * We reached the target or the AIL is empty, so wait a bit
548 * longer for I/O to complete and remove pushed items from the
549 * AIL before we start the next scan from the start of the AIL.
David Chinner92d9cd12008-03-06 13:45:10 +1100550 */
Dave Chinner453eac82010-01-11 11:49:58 +0000551 tout = 50;
Matthew Wilcox57e80952018-03-07 14:59:39 -0800552 ailp->ail_last_pushed_lsn = 0;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000553 } else if (((stuck + flushing) * 100) / count > 90) {
David Chinner249a8c12008-02-05 12:13:32 +1100554 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000555 * Either there is a lot of contention on the AIL or we are
556 * stuck due to operations in progress. "Stuck" in this case
557 * is defined as >90% of the items we tried to push were stuck.
David Chinner249a8c12008-02-05 12:13:32 +1100558 *
559 * Backoff a bit more to allow some I/O to complete before
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000560 * restarting from the start of the AIL. This prevents us from
561 * spinning on the same items, and if they are pinned will all
562 * the restart to issue a log force to unpin the stuck items.
David Chinner249a8c12008-02-05 12:13:32 +1100563 */
Dave Chinner453eac82010-01-11 11:49:58 +0000564 tout = 20;
Matthew Wilcox57e80952018-03-07 14:59:39 -0800565 ailp->ail_last_pushed_lsn = 0;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000566 } else {
567 /*
568 * Assume we have more work to do in a short while.
569 */
570 tout = 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
Dave Chinner0bf6a5b2011-04-08 12:45:07 +1000572
Christoph Hellwig00308072011-10-11 11:14:10 -0400573 return tout;
574}
575
576static int
577xfsaild(
578 void *data)
579{
580 struct xfs_ail *ailp = data;
581 long tout = 0; /* milliseconds */
Eric Biggers10a98cb2020-03-10 08:57:27 -0700582 unsigned int noreclaim_flag;
Christoph Hellwig00308072011-10-11 11:14:10 -0400583
Eric Biggers10a98cb2020-03-10 08:57:27 -0700584 noreclaim_flag = memalloc_noreclaim_save();
Michal Hocko18f1df42016-02-08 14:59:07 +1100585 set_freezable();
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000586
Hou Tao0bd89672017-10-17 14:16:28 -0700587 while (1) {
Christoph Hellwig00308072011-10-11 11:14:10 -0400588 if (tout && tout <= 20)
Hou Tao0bd89672017-10-17 14:16:28 -0700589 set_current_state(TASK_KILLABLE);
Christoph Hellwig00308072011-10-11 11:14:10 -0400590 else
Hou Tao0bd89672017-10-17 14:16:28 -0700591 set_current_state(TASK_INTERRUPTIBLE);
592
593 /*
Brian Fosterefc32892018-10-18 17:21:49 +1100594 * Check kthread_should_stop() after we set the task state to
595 * guarantee that we either see the stop bit and exit or the
596 * task state is reset to runnable such that it's not scheduled
597 * out indefinitely and detects the stop bit at next iteration.
Hou Tao0bd89672017-10-17 14:16:28 -0700598 * A memory barrier is included in above task state set to
599 * serialize again kthread_stop().
600 */
601 if (kthread_should_stop()) {
602 __set_current_state(TASK_RUNNING);
Brian Fosterefc32892018-10-18 17:21:49 +1100603
604 /*
605 * The caller forces out the AIL before stopping the
606 * thread in the common case, which means the delwri
607 * queue is drained. In the shutdown case, the queue may
608 * still hold relogged buffers that haven't been
609 * submitted because they were pinned since added to the
610 * queue.
611 *
612 * Log I/O error processing stales the underlying buffer
613 * and clears the delwri state, expecting the buf to be
614 * removed on the next submission attempt. That won't
615 * happen if we're shutting down, so this is the last
616 * opportunity to release such buffers from the queue.
617 */
618 ASSERT(list_empty(&ailp->ail_buf_list) ||
619 XFS_FORCED_SHUTDOWN(ailp->ail_mount));
620 xfs_buf_delwri_cancel(&ailp->ail_buf_list);
Hou Tao0bd89672017-10-17 14:16:28 -0700621 break;
622 }
Brian Foster8375f922012-06-28 06:52:56 -0400623
Matthew Wilcox57e80952018-03-07 14:59:39 -0800624 spin_lock(&ailp->ail_lock);
Brian Foster8375f922012-06-28 06:52:56 -0400625
626 /*
627 * Idle if the AIL is empty and we are not racing with a target
628 * update. We check the AIL after we set the task to a sleep
Matthew Wilcox57e80952018-03-07 14:59:39 -0800629 * state to guarantee that we either catch an ail_target update
Brian Foster8375f922012-06-28 06:52:56 -0400630 * or that a wake_up resets the state to TASK_RUNNING.
631 * Otherwise, we run the risk of sleeping indefinitely.
632 *
Matthew Wilcox57e80952018-03-07 14:59:39 -0800633 * The barrier matches the ail_target update in xfs_ail_push().
Brian Foster8375f922012-06-28 06:52:56 -0400634 */
635 smp_rmb();
636 if (!xfs_ail_min(ailp) &&
Matthew Wilcox57e80952018-03-07 14:59:39 -0800637 ailp->ail_target == ailp->ail_target_prev) {
638 spin_unlock(&ailp->ail_lock);
Michal Hocko18f1df42016-02-08 14:59:07 +1100639 freezable_schedule();
Brian Foster8375f922012-06-28 06:52:56 -0400640 tout = 0;
641 continue;
642 }
Matthew Wilcox57e80952018-03-07 14:59:39 -0800643 spin_unlock(&ailp->ail_lock);
Brian Foster8375f922012-06-28 06:52:56 -0400644
645 if (tout)
Michal Hocko18f1df42016-02-08 14:59:07 +1100646 freezable_schedule_timeout(msecs_to_jiffies(tout));
Brian Foster8375f922012-06-28 06:52:56 -0400647
648 __set_current_state(TASK_RUNNING);
Christoph Hellwig00308072011-10-11 11:14:10 -0400649
650 try_to_freeze();
651
652 tout = xfsaild_push(ailp);
653 }
654
Eric Biggers10a98cb2020-03-10 08:57:27 -0700655 memalloc_noreclaim_restore(noreclaim_flag);
Christoph Hellwig00308072011-10-11 11:14:10 -0400656 return 0;
Dave Chinner453eac82010-01-11 11:49:58 +0000657}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Dave Chinner0bf6a5b2011-04-08 12:45:07 +1000659/*
660 * This routine is called to move the tail of the AIL forward. It does this by
661 * trying to flush items in the AIL whose lsns are below the given
662 * threshold_lsn.
663 *
664 * The push is run asynchronously in a workqueue, which means the caller needs
665 * to handle waiting on the async flush for space to become available.
666 * We don't want to interrupt any push that is in progress, hence we only queue
Joe Perchescf085a12019-11-07 13:24:52 -0800667 * work if we set the pushing bit appropriately.
Dave Chinner0bf6a5b2011-04-08 12:45:07 +1000668 *
669 * We do this unlocked - we only need to know whether there is anything in the
670 * AIL at the time we are called. We don't need to access the contents of
671 * any of the objects, so the lock is not needed.
672 */
673void
Dave Chinnerfd074842011-04-08 12:45:07 +1000674xfs_ail_push(
Christoph Hellwigefe23302019-06-28 19:27:33 -0700675 struct xfs_ail *ailp,
676 xfs_lsn_t threshold_lsn)
Dave Chinner0bf6a5b2011-04-08 12:45:07 +1000677{
Christoph Hellwigefe23302019-06-28 19:27:33 -0700678 struct xfs_log_item *lip;
Dave Chinner0bf6a5b2011-04-08 12:45:07 +1000679
680 lip = xfs_ail_min(ailp);
Matthew Wilcox57e80952018-03-07 14:59:39 -0800681 if (!lip || XFS_FORCED_SHUTDOWN(ailp->ail_mount) ||
682 XFS_LSN_CMP(threshold_lsn, ailp->ail_target) <= 0)
Dave Chinner0bf6a5b2011-04-08 12:45:07 +1000683 return;
684
685 /*
686 * Ensure that the new target is noticed in push code before it clears
687 * the XFS_AIL_PUSHING_BIT.
688 */
689 smp_wmb();
Matthew Wilcox57e80952018-03-07 14:59:39 -0800690 xfs_trans_ail_copy_lsn(ailp, &ailp->ail_target, &threshold_lsn);
Christoph Hellwig00308072011-10-11 11:14:10 -0400691 smp_wmb();
692
Matthew Wilcox57e80952018-03-07 14:59:39 -0800693 wake_up_process(ailp->ail_task);
Dave Chinner0bf6a5b2011-04-08 12:45:07 +1000694}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696/*
Dave Chinnerfd074842011-04-08 12:45:07 +1000697 * Push out all items in the AIL immediately
698 */
699void
700xfs_ail_push_all(
701 struct xfs_ail *ailp)
702{
703 xfs_lsn_t threshold_lsn = xfs_ail_max_lsn(ailp);
704
705 if (threshold_lsn)
706 xfs_ail_push(ailp, threshold_lsn);
707}
708
709/*
Christoph Hellwig211e4d42012-04-23 15:58:34 +1000710 * Push out all items in the AIL immediately and wait until the AIL is empty.
711 */
712void
713xfs_ail_push_all_sync(
714 struct xfs_ail *ailp)
715{
716 struct xfs_log_item *lip;
717 DEFINE_WAIT(wait);
718
Matthew Wilcox57e80952018-03-07 14:59:39 -0800719 spin_lock(&ailp->ail_lock);
Christoph Hellwig211e4d42012-04-23 15:58:34 +1000720 while ((lip = xfs_ail_max(ailp)) != NULL) {
Matthew Wilcox57e80952018-03-07 14:59:39 -0800721 prepare_to_wait(&ailp->ail_empty, &wait, TASK_UNINTERRUPTIBLE);
722 ailp->ail_target = lip->li_lsn;
723 wake_up_process(ailp->ail_task);
724 spin_unlock(&ailp->ail_lock);
Christoph Hellwig211e4d42012-04-23 15:58:34 +1000725 schedule();
Matthew Wilcox57e80952018-03-07 14:59:39 -0800726 spin_lock(&ailp->ail_lock);
Christoph Hellwig211e4d42012-04-23 15:58:34 +1000727 }
Matthew Wilcox57e80952018-03-07 14:59:39 -0800728 spin_unlock(&ailp->ail_lock);
Christoph Hellwig211e4d42012-04-23 15:58:34 +1000729
Matthew Wilcox57e80952018-03-07 14:59:39 -0800730 finish_wait(&ailp->ail_empty, &wait);
Christoph Hellwig211e4d42012-04-23 15:58:34 +1000731}
732
Dave Chinner41659942020-03-24 20:10:29 -0700733void
734xfs_ail_update_finish(
735 struct xfs_ail *ailp,
Dave Chinner8eb807b2020-03-24 20:10:29 -0700736 xfs_lsn_t old_lsn) __releases(ailp->ail_lock)
Dave Chinner41659942020-03-24 20:10:29 -0700737{
738 struct xfs_mount *mp = ailp->ail_mount;
739
Dave Chinner8eb807b2020-03-24 20:10:29 -0700740 /* if the tail lsn hasn't changed, don't do updates or wakeups. */
741 if (!old_lsn || old_lsn == __xfs_ail_min_lsn(ailp)) {
Dave Chinner41659942020-03-24 20:10:29 -0700742 spin_unlock(&ailp->ail_lock);
743 return;
744 }
745
746 if (!XFS_FORCED_SHUTDOWN(mp))
747 xlog_assign_tail_lsn_locked(mp);
748
749 if (list_empty(&ailp->ail_head))
750 wake_up_all(&ailp->ail_empty);
751 spin_unlock(&ailp->ail_lock);
752 xfs_log_space_wake(mp);
753}
754
Christoph Hellwig211e4d42012-04-23 15:58:34 +1000755/*
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100756 * xfs_trans_ail_update - bulk AIL insertion operation.
757 *
758 * @xfs_trans_ail_update takes an array of log items that all need to be
759 * positioned at the same LSN in the AIL. If an item is not in the AIL, it will
760 * be added. Otherwise, it will be repositioned by removing it and re-adding
761 * it to the AIL. If we move the first item in the AIL, update the log tail to
762 * match the new minimum LSN in the AIL.
763 *
764 * This function takes the AIL lock once to execute the update operations on
765 * all the items in the array, and as such should not be called with the AIL
766 * lock held. As a result, once we have the AIL lock, we need to check each log
767 * item LSN to confirm it needs to be moved forward in the AIL.
768 *
769 * To optimise the insert operation, we delete all the items from the AIL in
770 * the first pass, moving them into a temporary list, then splice the temporary
771 * list into the correct position in the AIL. This avoids needing to do an
772 * insert operation on every item.
773 *
774 * This function must be called with the AIL lock held. The lock is dropped
775 * before returning.
776 */
777void
778xfs_trans_ail_update_bulk(
779 struct xfs_ail *ailp,
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000780 struct xfs_ail_cursor *cur,
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100781 struct xfs_log_item **log_items,
782 int nr_items,
Matthew Wilcox57e80952018-03-07 14:59:39 -0800783 xfs_lsn_t lsn) __releases(ailp->ail_lock)
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100784{
Christoph Hellwigefe23302019-06-28 19:27:33 -0700785 struct xfs_log_item *mlip;
Dave Chinner8eb807b2020-03-24 20:10:29 -0700786 xfs_lsn_t tail_lsn = 0;
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100787 int i;
788 LIST_HEAD(tmp);
789
Alex Eldere44f4112011-07-22 16:04:41 +0000790 ASSERT(nr_items > 0); /* Not required, but true. */
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100791 mlip = xfs_ail_min(ailp);
792
793 for (i = 0; i < nr_items; i++) {
794 struct xfs_log_item *lip = log_items[i];
Dave Chinner22525c12018-05-09 07:47:34 -0700795 if (test_and_set_bit(XFS_LI_IN_AIL, &lip->li_flags)) {
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100796 /* check if we really need to move the item */
797 if (XFS_LSN_CMP(lsn, lip->li_lsn) <= 0)
798 continue;
799
Dave Chinner750b9c92013-11-01 15:27:18 +1100800 trace_xfs_ail_move(lip, lip->li_lsn, lsn);
Dave Chinner8eb807b2020-03-24 20:10:29 -0700801 if (mlip == lip && !tail_lsn)
802 tail_lsn = lip->li_lsn;
803
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100804 xfs_ail_delete(ailp, lip);
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100805 } else {
Dave Chinner750b9c92013-11-01 15:27:18 +1100806 trace_xfs_ail_insert(lip, 0, lsn);
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100807 }
808 lip->li_lsn = lsn;
809 list_add(&lip->li_ail, &tmp);
810 }
811
Alex Eldere44f4112011-07-22 16:04:41 +0000812 if (!list_empty(&tmp))
813 xfs_ail_splice(ailp, cur, &tmp, lsn);
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000814
Dave Chinner8eb807b2020-03-24 20:10:29 -0700815 xfs_ail_update_finish(ailp, tail_lsn);
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100816}
817
Dave Chinner8eb807b2020-03-24 20:10:29 -0700818/*
819 * Delete one log item from the AIL.
820 *
821 * If this item was at the tail of the AIL, return the LSN of the log item so
822 * that we can use it to check if the LSN of the tail of the log has moved
823 * when finishing up the AIL delete process in xfs_ail_update_finish().
824 */
825xfs_lsn_t
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700826xfs_ail_delete_one(
827 struct xfs_ail *ailp,
Carlos Maiolinod3a304b2017-08-08 18:21:50 -0700828 struct xfs_log_item *lip)
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700829{
830 struct xfs_log_item *mlip = xfs_ail_min(ailp);
Dave Chinner8eb807b2020-03-24 20:10:29 -0700831 xfs_lsn_t lsn = lip->li_lsn;
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700832
833 trace_xfs_ail_delete(lip, mlip->li_lsn, lip->li_lsn);
834 xfs_ail_delete(ailp, lip);
Carlos Maiolinod3a304b2017-08-08 18:21:50 -0700835 xfs_clear_li_failed(lip);
Dave Chinner22525c12018-05-09 07:47:34 -0700836 clear_bit(XFS_LI_IN_AIL, &lip->li_flags);
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700837 lip->li_lsn = 0;
838
Dave Chinner8eb807b2020-03-24 20:10:29 -0700839 if (mlip == lip)
840 return lsn;
841 return 0;
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700842}
843
844/**
845 * Remove a log items from the AIL
Dave Chinner30136832010-12-20 12:03:17 +1100846 *
847 * @xfs_trans_ail_delete_bulk takes an array of log items that all need to
848 * removed from the AIL. The caller is already holding the AIL lock, and done
849 * all the checks necessary to ensure the items passed in via @log_items are
850 * ready for deletion. This includes checking that the items are in the AIL.
851 *
852 * For each log item to be removed, unlink it from the AIL, clear the IN_AIL
853 * flag from the item and reset the item's lsn to 0. If we remove the first
854 * item in the AIL, update the log tail to match the new minimum LSN in the
855 * AIL.
856 *
857 * This function will not drop the AIL lock until all items are removed from
858 * the AIL to minimise the amount of lock traffic on the AIL. This does not
859 * greatly increase the AIL hold time, but does significantly reduce the amount
860 * of traffic on the lock, especially during IO completion.
861 *
862 * This function must be called with the AIL lock held. The lock is dropped
863 * before returning.
864 */
865void
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700866xfs_trans_ail_delete(
Dave Chinner30136832010-12-20 12:03:17 +1100867 struct xfs_ail *ailp,
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700868 struct xfs_log_item *lip,
Dave Chinner41659942020-03-24 20:10:29 -0700869 int shutdown_type)
Dave Chinner30136832010-12-20 12:03:17 +1100870{
Matthew Wilcox57e80952018-03-07 14:59:39 -0800871 struct xfs_mount *mp = ailp->ail_mount;
Dave Chinner8eb807b2020-03-24 20:10:29 -0700872 xfs_lsn_t tail_lsn;
Dave Chinner30136832010-12-20 12:03:17 +1100873
Dave Chinner22525c12018-05-09 07:47:34 -0700874 if (!test_bit(XFS_LI_IN_AIL, &lip->li_flags)) {
Matthew Wilcox57e80952018-03-07 14:59:39 -0800875 spin_unlock(&ailp->ail_lock);
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700876 if (!XFS_FORCED_SHUTDOWN(mp)) {
877 xfs_alert_tag(mp, XFS_PTAG_AILDELETE,
878 "%s: attempting to delete a log item that is not in the AIL",
879 __func__);
880 xfs_force_shutdown(mp, shutdown_type);
Dave Chinner30136832010-12-20 12:03:17 +1100881 }
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700882 return;
Dave Chinner30136832010-12-20 12:03:17 +1100883 }
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000884
Dave Chinner8eb807b2020-03-24 20:10:29 -0700885 tail_lsn = xfs_ail_delete_one(ailp, lip);
886 xfs_ail_update_finish(ailp, tail_lsn);
Dave Chinner30136832010-12-20 12:03:17 +1100887}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
David Chinner249a8c12008-02-05 12:13:32 +1100889int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890xfs_trans_ail_init(
891 xfs_mount_t *mp)
892{
David Chinner82fa9012008-10-30 17:38:26 +1100893 struct xfs_ail *ailp;
894
895 ailp = kmem_zalloc(sizeof(struct xfs_ail), KM_MAYFAIL);
896 if (!ailp)
Dave Chinner24513372014-06-25 14:58:08 +1000897 return -ENOMEM;
David Chinner82fa9012008-10-30 17:38:26 +1100898
Matthew Wilcox57e80952018-03-07 14:59:39 -0800899 ailp->ail_mount = mp;
900 INIT_LIST_HEAD(&ailp->ail_head);
901 INIT_LIST_HEAD(&ailp->ail_cursors);
902 spin_lock_init(&ailp->ail_lock);
903 INIT_LIST_HEAD(&ailp->ail_buf_list);
904 init_waitqueue_head(&ailp->ail_empty);
Christoph Hellwig00308072011-10-11 11:14:10 -0400905
Matthew Wilcox57e80952018-03-07 14:59:39 -0800906 ailp->ail_task = kthread_run(xfsaild, ailp, "xfsaild/%s",
Ian Kente1d3d212019-11-04 13:58:40 -0800907 ailp->ail_mount->m_super->s_id);
Matthew Wilcox57e80952018-03-07 14:59:39 -0800908 if (IS_ERR(ailp->ail_task))
Christoph Hellwig00308072011-10-11 11:14:10 -0400909 goto out_free_ailp;
910
David Chinner27d8d5f2008-10-30 17:38:39 +1100911 mp->m_ail = ailp;
912 return 0;
Christoph Hellwig00308072011-10-11 11:14:10 -0400913
914out_free_ailp:
915 kmem_free(ailp);
Dave Chinner24513372014-06-25 14:58:08 +1000916 return -ENOMEM;
David Chinner249a8c12008-02-05 12:13:32 +1100917}
918
919void
920xfs_trans_ail_destroy(
921 xfs_mount_t *mp)
922{
David Chinner82fa9012008-10-30 17:38:26 +1100923 struct xfs_ail *ailp = mp->m_ail;
924
Matthew Wilcox57e80952018-03-07 14:59:39 -0800925 kthread_stop(ailp->ail_task);
David Chinner82fa9012008-10-30 17:38:26 +1100926 kmem_free(ailp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}