blob: 2a8c8dc54c9585fb34bef8dc4d1c97d5e2c2477e [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"
Dave Chinner0020a192021-08-10 18:00:44 -070020#include "xfs_log_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#ifdef DEBUG
Dave Chinnercd4a3c52011-04-08 12:45:07 +100023/*
24 * Check that the list is sorted as it should be.
Dave Chinnerd686d122018-05-09 07:49:09 -070025 *
26 * Called with the ail lock held, but we don't want to assert fail with it
27 * held otherwise we'll lock everything up and won't be able to debug the
28 * cause. Hence we sample and check the state under the AIL lock and return if
29 * everything is fine, otherwise we drop the lock and run the ASSERT checks.
30 * Asserts may not be fatal, so pick the lock back up and continue onwards.
Dave Chinnercd4a3c52011-04-08 12:45:07 +100031 */
32STATIC void
33xfs_ail_check(
Dave Chinnerd686d122018-05-09 07:49:09 -070034 struct xfs_ail *ailp,
35 struct xfs_log_item *lip)
Jules Irengedaebba12020-02-26 09:37:15 -080036 __must_hold(&ailp->ail_lock)
Dave Chinnercd4a3c52011-04-08 12:45:07 +100037{
Dave Chinnerd686d122018-05-09 07:49:09 -070038 struct xfs_log_item *prev_lip;
39 struct xfs_log_item *next_lip;
40 xfs_lsn_t prev_lsn = NULLCOMMITLSN;
41 xfs_lsn_t next_lsn = NULLCOMMITLSN;
42 xfs_lsn_t lsn;
43 bool in_ail;
44
Dave Chinnercd4a3c52011-04-08 12:45:07 +100045
Matthew Wilcox57e80952018-03-07 14:59:39 -080046 if (list_empty(&ailp->ail_head))
Dave Chinnercd4a3c52011-04-08 12:45:07 +100047 return;
48
49 /*
Dave Chinnerd686d122018-05-09 07:49:09 -070050 * Sample then check the next and previous entries are valid.
Dave Chinnercd4a3c52011-04-08 12:45:07 +100051 */
Dave Chinnerd686d122018-05-09 07:49:09 -070052 in_ail = test_bit(XFS_LI_IN_AIL, &lip->li_flags);
53 prev_lip = list_entry(lip->li_ail.prev, struct xfs_log_item, li_ail);
Matthew Wilcox57e80952018-03-07 14:59:39 -080054 if (&prev_lip->li_ail != &ailp->ail_head)
Dave Chinnerd686d122018-05-09 07:49:09 -070055 prev_lsn = prev_lip->li_lsn;
56 next_lip = list_entry(lip->li_ail.next, struct xfs_log_item, li_ail);
57 if (&next_lip->li_ail != &ailp->ail_head)
58 next_lsn = next_lip->li_lsn;
59 lsn = lip->li_lsn;
Dave Chinnercd4a3c52011-04-08 12:45:07 +100060
Dave Chinnerd686d122018-05-09 07:49:09 -070061 if (in_ail &&
62 (prev_lsn == NULLCOMMITLSN || XFS_LSN_CMP(prev_lsn, lsn) <= 0) &&
63 (next_lsn == NULLCOMMITLSN || XFS_LSN_CMP(next_lsn, lsn) >= 0))
64 return;
Dave Chinnercd4a3c52011-04-08 12:45:07 +100065
Dave Chinnerd686d122018-05-09 07:49:09 -070066 spin_unlock(&ailp->ail_lock);
67 ASSERT(in_ail);
68 ASSERT(prev_lsn == NULLCOMMITLSN || XFS_LSN_CMP(prev_lsn, lsn) <= 0);
69 ASSERT(next_lsn == NULLCOMMITLSN || XFS_LSN_CMP(next_lsn, lsn) >= 0);
70 spin_lock(&ailp->ail_lock);
Dave Chinnercd4a3c52011-04-08 12:45:07 +100071}
72#else /* !DEBUG */
David Chinnerde08dbc2008-02-05 12:13:38 +110073#define xfs_ail_check(a,l)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#endif /* DEBUG */
75
Dave Chinnercd4a3c52011-04-08 12:45:07 +100076/*
Dave Chinnerfd074842011-04-08 12:45:07 +100077 * Return a pointer to the last item in the AIL. If the AIL is empty, then
78 * return NULL.
79 */
Christoph Hellwigefe23302019-06-28 19:27:33 -070080static struct xfs_log_item *
Dave Chinnerfd074842011-04-08 12:45:07 +100081xfs_ail_max(
82 struct xfs_ail *ailp)
83{
Matthew Wilcox57e80952018-03-07 14:59:39 -080084 if (list_empty(&ailp->ail_head))
Dave Chinnerfd074842011-04-08 12:45:07 +100085 return NULL;
86
Christoph Hellwigefe23302019-06-28 19:27:33 -070087 return list_entry(ailp->ail_head.prev, struct xfs_log_item, li_ail);
Dave Chinnerfd074842011-04-08 12:45:07 +100088}
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/*
Dave Chinnercd4a3c52011-04-08 12:45:07 +100091 * Return a pointer to the item which follows the given item in the AIL. If
92 * the given item is the last item in the list, then return NULL.
93 */
Christoph Hellwigefe23302019-06-28 19:27:33 -070094static struct xfs_log_item *
Dave Chinnercd4a3c52011-04-08 12:45:07 +100095xfs_ail_next(
Christoph Hellwigefe23302019-06-28 19:27:33 -070096 struct xfs_ail *ailp,
97 struct xfs_log_item *lip)
Dave Chinnercd4a3c52011-04-08 12:45:07 +100098{
Matthew Wilcox57e80952018-03-07 14:59:39 -080099 if (lip->li_ail.next == &ailp->ail_head)
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000100 return NULL;
101
Christoph Hellwigefe23302019-06-28 19:27:33 -0700102 return list_first_entry(&lip->li_ail, struct xfs_log_item, li_ail);
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000103}
104
105/*
106 * This is called by the log manager code to determine the LSN of the tail of
107 * the log. This is exactly the LSN of the first item in the AIL. If the AIL
108 * is empty, then this function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 *
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000110 * We need the AIL lock in order to get a coherent read of the lsn of the last
111 * item in the AIL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 */
Dave Chinner8eb807b2020-03-24 20:10:29 -0700113static xfs_lsn_t
114__xfs_ail_min_lsn(
115 struct xfs_ail *ailp)
116{
117 struct xfs_log_item *lip = xfs_ail_min(ailp);
118
119 if (lip)
120 return lip->li_lsn;
121 return 0;
122}
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124xfs_lsn_t
Dave Chinnerfd074842011-04-08 12:45:07 +1000125xfs_ail_min_lsn(
Christoph Hellwigefe23302019-06-28 19:27:33 -0700126 struct xfs_ail *ailp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Dave Chinner8eb807b2020-03-24 20:10:29 -0700128 xfs_lsn_t lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Matthew Wilcox57e80952018-03-07 14:59:39 -0800130 spin_lock(&ailp->ail_lock);
Dave Chinner8eb807b2020-03-24 20:10:29 -0700131 lsn = __xfs_ail_min_lsn(ailp);
Matthew Wilcox57e80952018-03-07 14:59:39 -0800132 spin_unlock(&ailp->ail_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 return lsn;
135}
136
137/*
Dave Chinnerfd074842011-04-08 12:45:07 +1000138 * Return the maximum lsn held in the AIL, or zero if the AIL is empty.
139 */
140static xfs_lsn_t
141xfs_ail_max_lsn(
Christoph Hellwigefe23302019-06-28 19:27:33 -0700142 struct xfs_ail *ailp)
Dave Chinnerfd074842011-04-08 12:45:07 +1000143{
Christoph Hellwigefe23302019-06-28 19:27:33 -0700144 xfs_lsn_t lsn = 0;
145 struct xfs_log_item *lip;
Dave Chinnerfd074842011-04-08 12:45:07 +1000146
Matthew Wilcox57e80952018-03-07 14:59:39 -0800147 spin_lock(&ailp->ail_lock);
Dave Chinnerfd074842011-04-08 12:45:07 +1000148 lip = xfs_ail_max(ailp);
149 if (lip)
150 lsn = lip->li_lsn;
Matthew Wilcox57e80952018-03-07 14:59:39 -0800151 spin_unlock(&ailp->ail_lock);
Dave Chinnerfd074842011-04-08 12:45:07 +1000152
153 return lsn;
154}
155
156/*
Dave Chinneraf3e4022011-07-18 03:40:18 +0000157 * The cursor keeps track of where our current traversal is up to by tracking
158 * the next item in the list for us. However, for this to be safe, removing an
159 * object from the AIL needs to invalidate any cursor that points to it. hence
160 * the traversal cursor needs to be linked to the struct xfs_ail so that
161 * deletion can search all the active cursors for invalidation.
David Chinner27d8d5f2008-10-30 17:38:39 +1100162 */
David Chinner5b00f142008-10-30 17:39:00 +1100163STATIC void
David Chinner27d8d5f2008-10-30 17:38:39 +1100164xfs_trans_ail_cursor_init(
165 struct xfs_ail *ailp,
166 struct xfs_ail_cursor *cur)
167{
168 cur->item = NULL;
Matthew Wilcox57e80952018-03-07 14:59:39 -0800169 list_add_tail(&cur->list, &ailp->ail_cursors);
David Chinner27d8d5f2008-10-30 17:38:39 +1100170}
171
172/*
Dave Chinneraf3e4022011-07-18 03:40:18 +0000173 * Get the next item in the traversal and advance the cursor. If the cursor
174 * was invalidated (indicated by a lip of 1), restart the traversal.
David Chinner27d8d5f2008-10-30 17:38:39 +1100175 */
David Chinner5b00f142008-10-30 17:39:00 +1100176struct xfs_log_item *
David Chinner27d8d5f2008-10-30 17:38:39 +1100177xfs_trans_ail_cursor_next(
178 struct xfs_ail *ailp,
179 struct xfs_ail_cursor *cur)
180{
181 struct xfs_log_item *lip = cur->item;
182
Christoph Hellwigdb9d67d2015-06-22 09:43:32 +1000183 if ((uintptr_t)lip & 1)
David Chinner27d8d5f2008-10-30 17:38:39 +1100184 lip = xfs_ail_min(ailp);
Dave Chinner16b59022011-07-18 03:40:17 +0000185 if (lip)
186 cur->item = xfs_ail_next(ailp, lip);
David Chinner27d8d5f2008-10-30 17:38:39 +1100187 return lip;
188}
189
190/*
Dave Chinneraf3e4022011-07-18 03:40:18 +0000191 * When the traversal is complete, we need to remove the cursor from the list
192 * of traversing cursors.
David Chinner27d8d5f2008-10-30 17:38:39 +1100193 */
194void
195xfs_trans_ail_cursor_done(
Dave Chinneraf3e4022011-07-18 03:40:18 +0000196 struct xfs_ail_cursor *cur)
David Chinner27d8d5f2008-10-30 17:38:39 +1100197{
Dave Chinneraf3e4022011-07-18 03:40:18 +0000198 cur->item = NULL;
199 list_del_init(&cur->list);
David Chinner27d8d5f2008-10-30 17:38:39 +1100200}
201
202/*
Dave Chinneraf3e4022011-07-18 03:40:18 +0000203 * Invalidate any cursor that is pointing to this item. This is called when an
204 * item is removed from the AIL. Any cursor pointing to this object is now
205 * invalid and the traversal needs to be terminated so it doesn't reference a
206 * freed object. We set the low bit of the cursor item pointer so we can
207 * distinguish between an invalidation and the end of the list when getting the
208 * next item from the cursor.
David Chinner5b00f142008-10-30 17:39:00 +1100209 */
210STATIC void
211xfs_trans_ail_cursor_clear(
212 struct xfs_ail *ailp,
213 struct xfs_log_item *lip)
214{
215 struct xfs_ail_cursor *cur;
216
Matthew Wilcox57e80952018-03-07 14:59:39 -0800217 list_for_each_entry(cur, &ailp->ail_cursors, list) {
David Chinner5b00f142008-10-30 17:39:00 +1100218 if (cur->item == lip)
219 cur->item = (struct xfs_log_item *)
Christoph Hellwigdb9d67d2015-06-22 09:43:32 +1000220 ((uintptr_t)cur->item | 1);
David Chinner5b00f142008-10-30 17:39:00 +1100221 }
222}
223
224/*
Dave Chinner16b59022011-07-18 03:40:17 +0000225 * Find the first item in the AIL with the given @lsn by searching in ascending
226 * LSN order and initialise the cursor to point to the next item for a
227 * ascending traversal. Pass a @lsn of zero to initialise the cursor to the
228 * first item in the AIL. Returns NULL if the list is empty.
David Chinner249a8c12008-02-05 12:13:32 +1100229 */
Christoph Hellwigefe23302019-06-28 19:27:33 -0700230struct xfs_log_item *
David Chinner5b00f142008-10-30 17:39:00 +1100231xfs_trans_ail_cursor_first(
David Chinner27d8d5f2008-10-30 17:38:39 +1100232 struct xfs_ail *ailp,
233 struct xfs_ail_cursor *cur,
234 xfs_lsn_t lsn)
David Chinner249a8c12008-02-05 12:13:32 +1100235{
Christoph Hellwigefe23302019-06-28 19:27:33 -0700236 struct xfs_log_item *lip;
David Chinner249a8c12008-02-05 12:13:32 +1100237
David Chinner5b00f142008-10-30 17:39:00 +1100238 xfs_trans_ail_cursor_init(ailp, cur);
Dave Chinner16b59022011-07-18 03:40:17 +0000239
240 if (lsn == 0) {
241 lip = xfs_ail_min(ailp);
David Chinner5b00f142008-10-30 17:39:00 +1100242 goto out;
Dave Chinner16b59022011-07-18 03:40:17 +0000243 }
David Chinner249a8c12008-02-05 12:13:32 +1100244
Matthew Wilcox57e80952018-03-07 14:59:39 -0800245 list_for_each_entry(lip, &ailp->ail_head, li_ail) {
David Chinner5b00f142008-10-30 17:39:00 +1100246 if (XFS_LSN_CMP(lip->li_lsn, lsn) >= 0)
David Chinner7ee49ac2008-10-30 18:26:51 +1100247 goto out;
Josef 'Jeff' Sipek535f6b32008-03-27 17:58:27 +1100248 }
Dave Chinner16b59022011-07-18 03:40:17 +0000249 return NULL;
250
David Chinner5b00f142008-10-30 17:39:00 +1100251out:
Dave Chinner16b59022011-07-18 03:40:17 +0000252 if (lip)
253 cur->item = xfs_ail_next(ailp, lip);
David Chinner5b00f142008-10-30 17:39:00 +1100254 return lip;
David Chinner249a8c12008-02-05 12:13:32 +1100255}
256
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000257static struct xfs_log_item *
258__xfs_trans_ail_cursor_last(
259 struct xfs_ail *ailp,
260 xfs_lsn_t lsn)
261{
Christoph Hellwigefe23302019-06-28 19:27:33 -0700262 struct xfs_log_item *lip;
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000263
Matthew Wilcox57e80952018-03-07 14:59:39 -0800264 list_for_each_entry_reverse(lip, &ailp->ail_head, li_ail) {
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000265 if (XFS_LSN_CMP(lip->li_lsn, lsn) <= 0)
266 return lip;
267 }
268 return NULL;
269}
270
271/*
Dave Chinner16b59022011-07-18 03:40:17 +0000272 * Find the last item in the AIL with the given @lsn by searching in descending
273 * LSN order and initialise the cursor to point to that item. If there is no
274 * item with the value of @lsn, then it sets the cursor to the last item with an
275 * LSN lower than @lsn. Returns NULL if the list is empty.
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000276 */
277struct xfs_log_item *
278xfs_trans_ail_cursor_last(
279 struct xfs_ail *ailp,
280 struct xfs_ail_cursor *cur,
281 xfs_lsn_t lsn)
282{
283 xfs_trans_ail_cursor_init(ailp, cur);
284 cur->item = __xfs_trans_ail_cursor_last(ailp, lsn);
285 return cur->item;
286}
287
288/*
Dave Chinner16b59022011-07-18 03:40:17 +0000289 * Splice the log item list into the AIL at the given LSN. We splice to the
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000290 * tail of the given LSN to maintain insert order for push traversals. The
291 * cursor is optional, allowing repeated updates to the same LSN to avoid
Alex Eldere44f4112011-07-22 16:04:41 +0000292 * repeated traversals. This should not be called with an empty list.
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000293 */
294static void
295xfs_ail_splice(
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000296 struct xfs_ail *ailp,
297 struct xfs_ail_cursor *cur,
298 struct list_head *list,
299 xfs_lsn_t lsn)
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000300{
Alex Eldere44f4112011-07-22 16:04:41 +0000301 struct xfs_log_item *lip;
302
303 ASSERT(!list_empty(list));
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000304
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000305 /*
Alex Eldere44f4112011-07-22 16:04:41 +0000306 * Use the cursor to determine the insertion point if one is
307 * provided. If not, or if the one we got is not valid,
308 * find the place in the AIL where the items belong.
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000309 */
Alex Eldere44f4112011-07-22 16:04:41 +0000310 lip = cur ? cur->item : NULL;
Christoph Hellwigdb9d67d2015-06-22 09:43:32 +1000311 if (!lip || (uintptr_t)lip & 1)
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000312 lip = __xfs_trans_ail_cursor_last(ailp, lsn);
313
Alex Eldere44f4112011-07-22 16:04:41 +0000314 /*
315 * If a cursor is provided, we know we're processing the AIL
316 * in lsn order, and future items to be spliced in will
317 * follow the last one being inserted now. Update the
318 * cursor to point to that last item, now while we have a
319 * reliable pointer to it.
320 */
321 if (cur)
322 cur->item = list_entry(list->prev, struct xfs_log_item, li_ail);
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000323
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000324 /*
Alex Eldere44f4112011-07-22 16:04:41 +0000325 * Finally perform the splice. Unless the AIL was empty,
326 * lip points to the item in the AIL _after_ which the new
327 * items should go. If lip is null the AIL was empty, so
328 * the new items go at the head of the AIL.
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000329 */
Alex Eldere44f4112011-07-22 16:04:41 +0000330 if (lip)
331 list_splice(list, &lip->li_ail);
332 else
Matthew Wilcox57e80952018-03-07 14:59:39 -0800333 list_splice(list, &ailp->ail_head);
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000334}
335
336/*
337 * Delete the given item from the AIL. Return a pointer to the item.
338 */
339static void
340xfs_ail_delete(
Christoph Hellwigefe23302019-06-28 19:27:33 -0700341 struct xfs_ail *ailp,
342 struct xfs_log_item *lip)
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000343{
344 xfs_ail_check(ailp, lip);
345 list_del(&lip->li_ail);
346 xfs_trans_ail_cursor_clear(ailp, lip);
347}
348
Brian Fostercb6ad092020-05-06 13:25:19 -0700349/*
350 * Requeue a failed buffer for writeback.
351 *
352 * We clear the log item failed state here as well, but we have to be careful
353 * about reference counts because the only active reference counts on the buffer
354 * may be the failed log items. Hence if we clear the log item failed state
355 * before queuing the buffer for IO we can release all active references to
356 * the buffer and free it, leading to use after free problems in
357 * xfs_buf_delwri_queue. It makes no difference to the buffer or log items which
358 * order we process them in - the buffer is locked, and we own the buffer list
359 * so nothing on them is going to change while we are performing this action.
360 *
361 * Hence we can safely queue the buffer for IO before we clear the failed log
362 * item state, therefore always having an active reference to the buffer and
363 * avoiding the transient zero-reference state that leads to use-after-free.
364 */
365static inline int
366xfsaild_resubmit_item(
367 struct xfs_log_item *lip,
368 struct list_head *buffer_list)
369{
370 struct xfs_buf *bp = lip->li_buf;
371
372 if (!xfs_buf_trylock(bp))
373 return XFS_ITEM_LOCKED;
374
375 if (!xfs_buf_delwri_queue(bp, buffer_list)) {
376 xfs_buf_unlock(bp);
377 return XFS_ITEM_FLUSHING;
378 }
379
380 /* protected by ail_lock */
Dave Chinner298f7be2020-06-29 14:49:15 -0700381 list_for_each_entry(lip, &bp->b_li_list, li_bio_list) {
382 if (bp->b_flags & _XBF_INODES)
383 clear_bit(XFS_LI_FAILED, &lip->li_flags);
384 else
385 xfs_clear_li_failed(lip);
386 }
Brian Fostercb6ad092020-05-06 13:25:19 -0700387
388 xfs_buf_unlock(bp);
389 return XFS_ITEM_SUCCESS;
390}
391
Brian Foster7f4d01f2017-08-08 18:21:52 -0700392static inline uint
393xfsaild_push_item(
394 struct xfs_ail *ailp,
395 struct xfs_log_item *lip)
396{
397 /*
398 * If log item pinning is enabled, skip the push and track the item as
399 * pinned. This can help induce head-behind-tail conditions.
400 */
Matthew Wilcox57e80952018-03-07 14:59:39 -0800401 if (XFS_TEST_ERROR(false, ailp->ail_mount, XFS_ERRTAG_LOG_ITEM_PIN))
Brian Foster7f4d01f2017-08-08 18:21:52 -0700402 return XFS_ITEM_PINNED;
403
Christoph Hellwige8b78db2019-06-28 19:27:30 -0700404 /*
405 * Consider the item pinned if a push callback is not defined so the
406 * caller will force the log. This should only happen for intent items
407 * as they are unpinned once the associated done item is committed to
408 * the on-disk log.
409 */
410 if (!lip->li_ops->iop_push)
411 return XFS_ITEM_PINNED;
Brian Fostercb6ad092020-05-06 13:25:19 -0700412 if (test_bit(XFS_LI_FAILED, &lip->li_flags))
413 return xfsaild_resubmit_item(lip, &ailp->ail_buf_list);
Matthew Wilcox57e80952018-03-07 14:59:39 -0800414 return lip->li_ops->iop_push(lip, &ailp->ail_buf_list);
Brian Foster7f4d01f2017-08-08 18:21:52 -0700415}
416
Christoph Hellwig00308072011-10-11 11:14:10 -0400417static long
418xfsaild_push(
419 struct xfs_ail *ailp)
David Chinner249a8c12008-02-05 12:13:32 +1100420{
Matthew Wilcox57e80952018-03-07 14:59:39 -0800421 xfs_mount_t *mp = ailp->ail_mount;
Dave Chinneraf3e4022011-07-18 03:40:18 +0000422 struct xfs_ail_cursor cur;
Christoph Hellwigefe23302019-06-28 19:27:33 -0700423 struct xfs_log_item *lip;
Dave Chinner9e7004e2011-05-06 02:54:05 +0000424 xfs_lsn_t lsn;
Dave Chinnerfe0da762011-05-06 02:54:07 +0000425 xfs_lsn_t target;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000426 long tout;
Dave Chinner9e7004e2011-05-06 02:54:05 +0000427 int stuck = 0;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000428 int flushing = 0;
Dave Chinner9e7004e2011-05-06 02:54:05 +0000429 int count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Dave Chinner670ce932011-09-30 04:45:03 +0000431 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000432 * If we encountered pinned items or did not finish writing out all
Dave Chinner0020a192021-08-10 18:00:44 -0700433 * buffers the last time we ran, force a background CIL push to get the
434 * items unpinned in the near future. We do not wait on the CIL push as
435 * that could stall us for seconds if there is enough background IO
436 * load. Stalling for that long when the tail of the log is pinned and
437 * needs flushing will hard stop the transaction subsystem when log
438 * space runs out.
Dave Chinner670ce932011-09-30 04:45:03 +0000439 */
Matthew Wilcox57e80952018-03-07 14:59:39 -0800440 if (ailp->ail_log_flush && ailp->ail_last_pushed_lsn == 0 &&
441 (!list_empty_careful(&ailp->ail_buf_list) ||
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000442 xfs_ail_min_lsn(ailp))) {
Matthew Wilcox57e80952018-03-07 14:59:39 -0800443 ailp->ail_log_flush = 0;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000444
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100445 XFS_STATS_INC(mp, xs_push_ail_flush);
Dave Chinner0020a192021-08-10 18:00:44 -0700446 xlog_cil_flush(mp->m_log);
Dave Chinner670ce932011-09-30 04:45:03 +0000447 }
448
Matthew Wilcox57e80952018-03-07 14:59:39 -0800449 spin_lock(&ailp->ail_lock);
Brian Foster8375f922012-06-28 06:52:56 -0400450
Matthew Wilcox57e80952018-03-07 14:59:39 -0800451 /* barrier matches the ail_target update in xfs_ail_push() */
Brian Foster8375f922012-06-28 06:52:56 -0400452 smp_rmb();
Matthew Wilcox57e80952018-03-07 14:59:39 -0800453 target = ailp->ail_target;
454 ailp->ail_target_prev = target;
Brian Foster8375f922012-06-28 06:52:56 -0400455
Brian Fosterf376b452020-07-16 07:39:29 -0700456 /* we're done if the AIL is empty or our push has reached the end */
Matthew Wilcox57e80952018-03-07 14:59:39 -0800457 lip = xfs_trans_ail_cursor_first(ailp, &cur, ailp->ail_last_pushed_lsn);
Brian Fosterf376b452020-07-16 07:39:29 -0700458 if (!lip)
Dave Chinner9e7004e2011-05-06 02:54:05 +0000459 goto out_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100461 XFS_STATS_INC(mp, xs_push_ail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
David Chinner249a8c12008-02-05 12:13:32 +1100463 lsn = lip->li_lsn;
Dave Chinner50e86682011-05-06 02:54:06 +0000464 while ((XFS_LSN_CMP(lip->li_lsn, target) <= 0)) {
David Chinner249a8c12008-02-05 12:13:32 +1100465 int lock_result;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 /*
Dave Chinner904c17e2013-08-28 21:12:03 +1000468 * Note that iop_push may unlock and reacquire the AIL lock. We
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000469 * rely on the AIL cursor implementation to be able to deal with
470 * the dropped lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 */
Brian Foster7f4d01f2017-08-08 18:21:52 -0700472 lock_result = xfsaild_push_item(ailp, lip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 switch (lock_result) {
David Chinner249a8c12008-02-05 12:13:32 +1100474 case XFS_ITEM_SUCCESS:
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100475 XFS_STATS_INC(mp, xs_push_ail_success);
Christoph Hellwig9e4c1092011-10-11 15:14:11 +0000476 trace_xfs_ail_push(lip);
477
Matthew Wilcox57e80952018-03-07 14:59:39 -0800478 ailp->ail_last_pushed_lsn = lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 break;
480
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000481 case XFS_ITEM_FLUSHING:
482 /*
Joe Perchescf085a12019-11-07 13:24:52 -0800483 * The item or its backing buffer is already being
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000484 * flushed. The typical reason for that is that an
485 * inode buffer is locked because we already pushed the
486 * updates to it as part of inode clustering.
487 *
Randy Dunlapb63da6c2020-08-05 08:49:58 -0700488 * We do not want to stop flushing just because lots
Joe Perchescf085a12019-11-07 13:24:52 -0800489 * of items are already being flushed, but we need to
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000490 * re-try the flushing relatively soon if most of the
Joe Perchescf085a12019-11-07 13:24:52 -0800491 * AIL is being flushed.
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000492 */
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100493 XFS_STATS_INC(mp, xs_push_ail_flushing);
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000494 trace_xfs_ail_flushing(lip);
Christoph Hellwig17b38472011-10-11 15:14:09 +0000495
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000496 flushing++;
Matthew Wilcox57e80952018-03-07 14:59:39 -0800497 ailp->ail_last_pushed_lsn = lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 break;
499
David Chinner249a8c12008-02-05 12:13:32 +1100500 case XFS_ITEM_PINNED:
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100501 XFS_STATS_INC(mp, xs_push_ail_pinned);
Christoph Hellwig9e4c1092011-10-11 15:14:11 +0000502 trace_xfs_ail_pinned(lip);
503
David Chinner249a8c12008-02-05 12:13:32 +1100504 stuck++;
Matthew Wilcox57e80952018-03-07 14:59:39 -0800505 ailp->ail_log_flush++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 break;
David Chinner249a8c12008-02-05 12:13:32 +1100507 case XFS_ITEM_LOCKED:
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100508 XFS_STATS_INC(mp, xs_push_ail_locked);
Christoph Hellwig9e4c1092011-10-11 15:14:11 +0000509 trace_xfs_ail_locked(lip);
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000510
David Chinner249a8c12008-02-05 12:13:32 +1100511 stuck++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 break;
David Chinner249a8c12008-02-05 12:13:32 +1100513 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 ASSERT(0);
515 break;
516 }
517
David Chinner249a8c12008-02-05 12:13:32 +1100518 count++;
519
520 /*
521 * Are there too many items we can't do anything with?
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000522 *
Randy Dunlapb63da6c2020-08-05 08:49:58 -0700523 * If we are skipping too many items because we can't flush
David Chinner249a8c12008-02-05 12:13:32 +1100524 * them or they are already being flushed, we back off and
525 * given them time to complete whatever operation is being
526 * done. i.e. remove pressure from the AIL while we can't make
527 * progress so traversals don't slow down further inserts and
528 * removals to/from the AIL.
529 *
530 * The value of 100 is an arbitrary magic number based on
531 * observation.
532 */
533 if (stuck > 100)
534 break;
535
Dave Chinneraf3e4022011-07-18 03:40:18 +0000536 lip = xfs_trans_ail_cursor_next(ailp, &cur);
David Chinner249a8c12008-02-05 12:13:32 +1100537 if (lip == NULL)
538 break;
David Chinner249a8c12008-02-05 12:13:32 +1100539 lsn = lip->li_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
Brian Fosterf376b452020-07-16 07:39:29 -0700541
542out_done:
Eric Sandeene4a1e292014-04-14 19:06:05 +1000543 xfs_trans_ail_cursor_done(&cur);
Matthew Wilcox57e80952018-03-07 14:59:39 -0800544 spin_unlock(&ailp->ail_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Matthew Wilcox57e80952018-03-07 14:59:39 -0800546 if (xfs_buf_delwri_submit_nowait(&ailp->ail_buf_list))
547 ailp->ail_log_flush++;
Dave Chinnerd808f612010-02-02 10:13:42 +1100548
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000549 if (!count || XFS_LSN_CMP(lsn, target) >= 0) {
David Chinner92d9cd12008-03-06 13:45:10 +1100550 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000551 * We reached the target or the AIL is empty, so wait a bit
552 * longer for I/O to complete and remove pushed items from the
553 * AIL before we start the next scan from the start of the AIL.
David Chinner92d9cd12008-03-06 13:45:10 +1100554 */
Dave Chinner453eac82010-01-11 11:49:58 +0000555 tout = 50;
Matthew Wilcox57e80952018-03-07 14:59:39 -0800556 ailp->ail_last_pushed_lsn = 0;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000557 } else if (((stuck + flushing) * 100) / count > 90) {
David Chinner249a8c12008-02-05 12:13:32 +1100558 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000559 * Either there is a lot of contention on the AIL or we are
560 * stuck due to operations in progress. "Stuck" in this case
561 * is defined as >90% of the items we tried to push were stuck.
David Chinner249a8c12008-02-05 12:13:32 +1100562 *
563 * Backoff a bit more to allow some I/O to complete before
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000564 * restarting from the start of the AIL. This prevents us from
565 * spinning on the same items, and if they are pinned will all
566 * the restart to issue a log force to unpin the stuck items.
David Chinner249a8c12008-02-05 12:13:32 +1100567 */
Dave Chinner453eac82010-01-11 11:49:58 +0000568 tout = 20;
Matthew Wilcox57e80952018-03-07 14:59:39 -0800569 ailp->ail_last_pushed_lsn = 0;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000570 } else {
571 /*
572 * Assume we have more work to do in a short while.
573 */
574 tout = 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
Dave Chinner0bf6a5b2011-04-08 12:45:07 +1000576
Christoph Hellwig00308072011-10-11 11:14:10 -0400577 return tout;
578}
579
580static int
581xfsaild(
582 void *data)
583{
584 struct xfs_ail *ailp = data;
585 long tout = 0; /* milliseconds */
Eric Biggers10a98cb2020-03-10 08:57:27 -0700586 unsigned int noreclaim_flag;
Christoph Hellwig00308072011-10-11 11:14:10 -0400587
Eric Biggers10a98cb2020-03-10 08:57:27 -0700588 noreclaim_flag = memalloc_noreclaim_save();
Michal Hocko18f1df42016-02-08 14:59:07 +1100589 set_freezable();
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000590
Hou Tao0bd89672017-10-17 14:16:28 -0700591 while (1) {
Christoph Hellwig00308072011-10-11 11:14:10 -0400592 if (tout && tout <= 20)
Hou Tao0bd89672017-10-17 14:16:28 -0700593 set_current_state(TASK_KILLABLE);
Christoph Hellwig00308072011-10-11 11:14:10 -0400594 else
Hou Tao0bd89672017-10-17 14:16:28 -0700595 set_current_state(TASK_INTERRUPTIBLE);
596
597 /*
Brian Fosterefc32892018-10-18 17:21:49 +1100598 * Check kthread_should_stop() after we set the task state to
599 * guarantee that we either see the stop bit and exit or the
600 * task state is reset to runnable such that it's not scheduled
601 * out indefinitely and detects the stop bit at next iteration.
Hou Tao0bd89672017-10-17 14:16:28 -0700602 * A memory barrier is included in above task state set to
603 * serialize again kthread_stop().
604 */
605 if (kthread_should_stop()) {
606 __set_current_state(TASK_RUNNING);
Brian Fosterefc32892018-10-18 17:21:49 +1100607
608 /*
609 * The caller forces out the AIL before stopping the
610 * thread in the common case, which means the delwri
611 * queue is drained. In the shutdown case, the queue may
612 * still hold relogged buffers that haven't been
613 * submitted because they were pinned since added to the
614 * queue.
615 *
616 * Log I/O error processing stales the underlying buffer
617 * and clears the delwri state, expecting the buf to be
618 * removed on the next submission attempt. That won't
619 * happen if we're shutting down, so this is the last
620 * opportunity to release such buffers from the queue.
621 */
622 ASSERT(list_empty(&ailp->ail_buf_list) ||
Dave Chinner75c8c50f2021-08-18 18:46:53 -0700623 xfs_is_shutdown(ailp->ail_mount));
Brian Fosterefc32892018-10-18 17:21:49 +1100624 xfs_buf_delwri_cancel(&ailp->ail_buf_list);
Hou Tao0bd89672017-10-17 14:16:28 -0700625 break;
626 }
Brian Foster8375f922012-06-28 06:52:56 -0400627
Matthew Wilcox57e80952018-03-07 14:59:39 -0800628 spin_lock(&ailp->ail_lock);
Brian Foster8375f922012-06-28 06:52:56 -0400629
630 /*
631 * Idle if the AIL is empty and we are not racing with a target
632 * update. We check the AIL after we set the task to a sleep
Matthew Wilcox57e80952018-03-07 14:59:39 -0800633 * state to guarantee that we either catch an ail_target update
Brian Foster8375f922012-06-28 06:52:56 -0400634 * or that a wake_up resets the state to TASK_RUNNING.
635 * Otherwise, we run the risk of sleeping indefinitely.
636 *
Matthew Wilcox57e80952018-03-07 14:59:39 -0800637 * The barrier matches the ail_target update in xfs_ail_push().
Brian Foster8375f922012-06-28 06:52:56 -0400638 */
639 smp_rmb();
640 if (!xfs_ail_min(ailp) &&
Brian Fosterf376b452020-07-16 07:39:29 -0700641 ailp->ail_target == ailp->ail_target_prev &&
642 list_empty(&ailp->ail_buf_list)) {
Matthew Wilcox57e80952018-03-07 14:59:39 -0800643 spin_unlock(&ailp->ail_lock);
Michal Hocko18f1df42016-02-08 14:59:07 +1100644 freezable_schedule();
Brian Foster8375f922012-06-28 06:52:56 -0400645 tout = 0;
646 continue;
647 }
Matthew Wilcox57e80952018-03-07 14:59:39 -0800648 spin_unlock(&ailp->ail_lock);
Brian Foster8375f922012-06-28 06:52:56 -0400649
650 if (tout)
Michal Hocko18f1df42016-02-08 14:59:07 +1100651 freezable_schedule_timeout(msecs_to_jiffies(tout));
Brian Foster8375f922012-06-28 06:52:56 -0400652
653 __set_current_state(TASK_RUNNING);
Christoph Hellwig00308072011-10-11 11:14:10 -0400654
655 try_to_freeze();
656
657 tout = xfsaild_push(ailp);
658 }
659
Eric Biggers10a98cb2020-03-10 08:57:27 -0700660 memalloc_noreclaim_restore(noreclaim_flag);
Christoph Hellwig00308072011-10-11 11:14:10 -0400661 return 0;
Dave Chinner453eac82010-01-11 11:49:58 +0000662}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Dave Chinner0bf6a5b2011-04-08 12:45:07 +1000664/*
665 * This routine is called to move the tail of the AIL forward. It does this by
666 * trying to flush items in the AIL whose lsns are below the given
667 * threshold_lsn.
668 *
669 * The push is run asynchronously in a workqueue, which means the caller needs
670 * to handle waiting on the async flush for space to become available.
671 * We don't want to interrupt any push that is in progress, hence we only queue
Joe Perchescf085a12019-11-07 13:24:52 -0800672 * work if we set the pushing bit appropriately.
Dave Chinner0bf6a5b2011-04-08 12:45:07 +1000673 *
674 * We do this unlocked - we only need to know whether there is anything in the
675 * AIL at the time we are called. We don't need to access the contents of
676 * any of the objects, so the lock is not needed.
677 */
678void
Dave Chinnerfd074842011-04-08 12:45:07 +1000679xfs_ail_push(
Christoph Hellwigefe23302019-06-28 19:27:33 -0700680 struct xfs_ail *ailp,
681 xfs_lsn_t threshold_lsn)
Dave Chinner0bf6a5b2011-04-08 12:45:07 +1000682{
Christoph Hellwigefe23302019-06-28 19:27:33 -0700683 struct xfs_log_item *lip;
Dave Chinner0bf6a5b2011-04-08 12:45:07 +1000684
685 lip = xfs_ail_min(ailp);
Dave Chinner75c8c50f2021-08-18 18:46:53 -0700686 if (!lip || xfs_is_shutdown(ailp->ail_mount) ||
Matthew Wilcox57e80952018-03-07 14:59:39 -0800687 XFS_LSN_CMP(threshold_lsn, ailp->ail_target) <= 0)
Dave Chinner0bf6a5b2011-04-08 12:45:07 +1000688 return;
689
690 /*
691 * Ensure that the new target is noticed in push code before it clears
692 * the XFS_AIL_PUSHING_BIT.
693 */
694 smp_wmb();
Matthew Wilcox57e80952018-03-07 14:59:39 -0800695 xfs_trans_ail_copy_lsn(ailp, &ailp->ail_target, &threshold_lsn);
Christoph Hellwig00308072011-10-11 11:14:10 -0400696 smp_wmb();
697
Matthew Wilcox57e80952018-03-07 14:59:39 -0800698 wake_up_process(ailp->ail_task);
Dave Chinner0bf6a5b2011-04-08 12:45:07 +1000699}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701/*
Dave Chinnerfd074842011-04-08 12:45:07 +1000702 * Push out all items in the AIL immediately
703 */
704void
705xfs_ail_push_all(
706 struct xfs_ail *ailp)
707{
708 xfs_lsn_t threshold_lsn = xfs_ail_max_lsn(ailp);
709
710 if (threshold_lsn)
711 xfs_ail_push(ailp, threshold_lsn);
712}
713
714/*
Christoph Hellwig211e4d42012-04-23 15:58:34 +1000715 * Push out all items in the AIL immediately and wait until the AIL is empty.
716 */
717void
718xfs_ail_push_all_sync(
719 struct xfs_ail *ailp)
720{
721 struct xfs_log_item *lip;
722 DEFINE_WAIT(wait);
723
Matthew Wilcox57e80952018-03-07 14:59:39 -0800724 spin_lock(&ailp->ail_lock);
Christoph Hellwig211e4d42012-04-23 15:58:34 +1000725 while ((lip = xfs_ail_max(ailp)) != NULL) {
Matthew Wilcox57e80952018-03-07 14:59:39 -0800726 prepare_to_wait(&ailp->ail_empty, &wait, TASK_UNINTERRUPTIBLE);
727 ailp->ail_target = lip->li_lsn;
728 wake_up_process(ailp->ail_task);
729 spin_unlock(&ailp->ail_lock);
Christoph Hellwig211e4d42012-04-23 15:58:34 +1000730 schedule();
Matthew Wilcox57e80952018-03-07 14:59:39 -0800731 spin_lock(&ailp->ail_lock);
Christoph Hellwig211e4d42012-04-23 15:58:34 +1000732 }
Matthew Wilcox57e80952018-03-07 14:59:39 -0800733 spin_unlock(&ailp->ail_lock);
Christoph Hellwig211e4d42012-04-23 15:58:34 +1000734
Matthew Wilcox57e80952018-03-07 14:59:39 -0800735 finish_wait(&ailp->ail_empty, &wait);
Christoph Hellwig211e4d42012-04-23 15:58:34 +1000736}
737
Dave Chinner41659942020-03-24 20:10:29 -0700738void
739xfs_ail_update_finish(
740 struct xfs_ail *ailp,
Dave Chinner8eb807b2020-03-24 20:10:29 -0700741 xfs_lsn_t old_lsn) __releases(ailp->ail_lock)
Dave Chinner41659942020-03-24 20:10:29 -0700742{
743 struct xfs_mount *mp = ailp->ail_mount;
744
Dave Chinner8eb807b2020-03-24 20:10:29 -0700745 /* if the tail lsn hasn't changed, don't do updates or wakeups. */
746 if (!old_lsn || old_lsn == __xfs_ail_min_lsn(ailp)) {
Dave Chinner41659942020-03-24 20:10:29 -0700747 spin_unlock(&ailp->ail_lock);
748 return;
749 }
750
Dave Chinner75c8c50f2021-08-18 18:46:53 -0700751 if (!xfs_is_shutdown(mp))
Dave Chinner41659942020-03-24 20:10:29 -0700752 xlog_assign_tail_lsn_locked(mp);
753
754 if (list_empty(&ailp->ail_head))
755 wake_up_all(&ailp->ail_empty);
756 spin_unlock(&ailp->ail_lock);
757 xfs_log_space_wake(mp);
758}
759
Christoph Hellwig211e4d42012-04-23 15:58:34 +1000760/*
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100761 * xfs_trans_ail_update - bulk AIL insertion operation.
762 *
763 * @xfs_trans_ail_update takes an array of log items that all need to be
764 * positioned at the same LSN in the AIL. If an item is not in the AIL, it will
765 * be added. Otherwise, it will be repositioned by removing it and re-adding
766 * it to the AIL. If we move the first item in the AIL, update the log tail to
767 * match the new minimum LSN in the AIL.
768 *
769 * This function takes the AIL lock once to execute the update operations on
770 * all the items in the array, and as such should not be called with the AIL
771 * lock held. As a result, once we have the AIL lock, we need to check each log
772 * item LSN to confirm it needs to be moved forward in the AIL.
773 *
774 * To optimise the insert operation, we delete all the items from the AIL in
775 * the first pass, moving them into a temporary list, then splice the temporary
776 * list into the correct position in the AIL. This avoids needing to do an
777 * insert operation on every item.
778 *
779 * This function must be called with the AIL lock held. The lock is dropped
780 * before returning.
781 */
782void
783xfs_trans_ail_update_bulk(
784 struct xfs_ail *ailp,
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000785 struct xfs_ail_cursor *cur,
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100786 struct xfs_log_item **log_items,
787 int nr_items,
Matthew Wilcox57e80952018-03-07 14:59:39 -0800788 xfs_lsn_t lsn) __releases(ailp->ail_lock)
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100789{
Christoph Hellwigefe23302019-06-28 19:27:33 -0700790 struct xfs_log_item *mlip;
Dave Chinner8eb807b2020-03-24 20:10:29 -0700791 xfs_lsn_t tail_lsn = 0;
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100792 int i;
793 LIST_HEAD(tmp);
794
Alex Eldere44f4112011-07-22 16:04:41 +0000795 ASSERT(nr_items > 0); /* Not required, but true. */
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100796 mlip = xfs_ail_min(ailp);
797
798 for (i = 0; i < nr_items; i++) {
799 struct xfs_log_item *lip = log_items[i];
Dave Chinner22525c12018-05-09 07:47:34 -0700800 if (test_and_set_bit(XFS_LI_IN_AIL, &lip->li_flags)) {
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100801 /* check if we really need to move the item */
802 if (XFS_LSN_CMP(lsn, lip->li_lsn) <= 0)
803 continue;
804
Dave Chinner750b9c92013-11-01 15:27:18 +1100805 trace_xfs_ail_move(lip, lip->li_lsn, lsn);
Dave Chinner8eb807b2020-03-24 20:10:29 -0700806 if (mlip == lip && !tail_lsn)
807 tail_lsn = lip->li_lsn;
808
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100809 xfs_ail_delete(ailp, lip);
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100810 } else {
Dave Chinner750b9c92013-11-01 15:27:18 +1100811 trace_xfs_ail_insert(lip, 0, lsn);
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100812 }
813 lip->li_lsn = lsn;
814 list_add(&lip->li_ail, &tmp);
815 }
816
Alex Eldere44f4112011-07-22 16:04:41 +0000817 if (!list_empty(&tmp))
818 xfs_ail_splice(ailp, cur, &tmp, lsn);
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000819
Dave Chinner8eb807b2020-03-24 20:10:29 -0700820 xfs_ail_update_finish(ailp, tail_lsn);
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100821}
822
Darrick J. Wong86a37172020-05-01 16:00:54 -0700823/* Insert a log item into the AIL. */
824void
825xfs_trans_ail_insert(
826 struct xfs_ail *ailp,
827 struct xfs_log_item *lip,
828 xfs_lsn_t lsn)
829{
830 spin_lock(&ailp->ail_lock);
831 xfs_trans_ail_update_bulk(ailp, NULL, &lip, 1, lsn);
832}
833
Dave Chinner8eb807b2020-03-24 20:10:29 -0700834/*
835 * Delete one log item from the AIL.
836 *
837 * If this item was at the tail of the AIL, return the LSN of the log item so
838 * that we can use it to check if the LSN of the tail of the log has moved
839 * when finishing up the AIL delete process in xfs_ail_update_finish().
840 */
841xfs_lsn_t
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700842xfs_ail_delete_one(
843 struct xfs_ail *ailp,
Carlos Maiolinod3a304b2017-08-08 18:21:50 -0700844 struct xfs_log_item *lip)
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700845{
846 struct xfs_log_item *mlip = xfs_ail_min(ailp);
Dave Chinner8eb807b2020-03-24 20:10:29 -0700847 xfs_lsn_t lsn = lip->li_lsn;
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700848
849 trace_xfs_ail_delete(lip, mlip->li_lsn, lip->li_lsn);
850 xfs_ail_delete(ailp, lip);
Dave Chinner22525c12018-05-09 07:47:34 -0700851 clear_bit(XFS_LI_IN_AIL, &lip->li_flags);
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700852 lip->li_lsn = 0;
853
Dave Chinner8eb807b2020-03-24 20:10:29 -0700854 if (mlip == lip)
855 return lsn;
856 return 0;
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700857}
858
Dave Chinner30136832010-12-20 12:03:17 +1100859void
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700860xfs_trans_ail_delete(
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700861 struct xfs_log_item *lip,
Dave Chinner41659942020-03-24 20:10:29 -0700862 int shutdown_type)
Dave Chinner30136832010-12-20 12:03:17 +1100863{
Brian Foster849274c2020-05-06 13:25:23 -0700864 struct xfs_ail *ailp = lip->li_ailp;
Matthew Wilcox57e80952018-03-07 14:59:39 -0800865 struct xfs_mount *mp = ailp->ail_mount;
Dave Chinner8eb807b2020-03-24 20:10:29 -0700866 xfs_lsn_t tail_lsn;
Dave Chinner30136832010-12-20 12:03:17 +1100867
Brian Foster849274c2020-05-06 13:25:23 -0700868 spin_lock(&ailp->ail_lock);
Dave Chinner22525c12018-05-09 07:47:34 -0700869 if (!test_bit(XFS_LI_IN_AIL, &lip->li_flags)) {
Matthew Wilcox57e80952018-03-07 14:59:39 -0800870 spin_unlock(&ailp->ail_lock);
Dave Chinner75c8c50f2021-08-18 18:46:53 -0700871 if (shutdown_type && !xfs_is_shutdown(mp)) {
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700872 xfs_alert_tag(mp, XFS_PTAG_AILDELETE,
873 "%s: attempting to delete a log item that is not in the AIL",
874 __func__);
875 xfs_force_shutdown(mp, shutdown_type);
Dave Chinner30136832010-12-20 12:03:17 +1100876 }
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700877 return;
Dave Chinner30136832010-12-20 12:03:17 +1100878 }
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000879
Brian Foster2b3cf092020-05-06 13:27:04 -0700880 /* xfs_ail_update_finish() drops the AIL lock */
Dave Chinnere98084b2020-06-29 14:49:15 -0700881 xfs_clear_li_failed(lip);
Dave Chinner8eb807b2020-03-24 20:10:29 -0700882 tail_lsn = xfs_ail_delete_one(ailp, lip);
883 xfs_ail_update_finish(ailp, tail_lsn);
Dave Chinner30136832010-12-20 12:03:17 +1100884}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
David Chinner249a8c12008-02-05 12:13:32 +1100886int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887xfs_trans_ail_init(
888 xfs_mount_t *mp)
889{
David Chinner82fa9012008-10-30 17:38:26 +1100890 struct xfs_ail *ailp;
891
892 ailp = kmem_zalloc(sizeof(struct xfs_ail), KM_MAYFAIL);
893 if (!ailp)
Dave Chinner24513372014-06-25 14:58:08 +1000894 return -ENOMEM;
David Chinner82fa9012008-10-30 17:38:26 +1100895
Matthew Wilcox57e80952018-03-07 14:59:39 -0800896 ailp->ail_mount = mp;
897 INIT_LIST_HEAD(&ailp->ail_head);
898 INIT_LIST_HEAD(&ailp->ail_cursors);
899 spin_lock_init(&ailp->ail_lock);
900 INIT_LIST_HEAD(&ailp->ail_buf_list);
901 init_waitqueue_head(&ailp->ail_empty);
Christoph Hellwig00308072011-10-11 11:14:10 -0400902
Matthew Wilcox57e80952018-03-07 14:59:39 -0800903 ailp->ail_task = kthread_run(xfsaild, ailp, "xfsaild/%s",
Ian Kente1d3d212019-11-04 13:58:40 -0800904 ailp->ail_mount->m_super->s_id);
Matthew Wilcox57e80952018-03-07 14:59:39 -0800905 if (IS_ERR(ailp->ail_task))
Christoph Hellwig00308072011-10-11 11:14:10 -0400906 goto out_free_ailp;
907
David Chinner27d8d5f2008-10-30 17:38:39 +1100908 mp->m_ail = ailp;
909 return 0;
Christoph Hellwig00308072011-10-11 11:14:10 -0400910
911out_free_ailp:
912 kmem_free(ailp);
Dave Chinner24513372014-06-25 14:58:08 +1000913 return -ENOMEM;
David Chinner249a8c12008-02-05 12:13:32 +1100914}
915
916void
917xfs_trans_ail_destroy(
918 xfs_mount_t *mp)
919{
David Chinner82fa9012008-10-30 17:38:26 +1100920 struct xfs_ail *ailp = mp->m_ail;
921
Matthew Wilcox57e80952018-03-07 14:59:39 -0800922 kthread_stop(ailp->ail_task);
David Chinner82fa9012008-10-30 17:38:26 +1100923 kmem_free(ailp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}