blob: d3a4e89bf4a0ddb916ed4f5d395285e2e2188869 [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"
Christoph Hellwig4fb6e8a2014-11-28 14:25:04 +11009#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110010#include "xfs_log_format.h"
11#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "xfs_mount.h"
Dave Chinner239880e2013-10-23 10:50:10 +110013#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "xfs_trans_priv.h"
Christoph Hellwig9e4c1092011-10-11 15:14:11 +000015#include "xfs_trace.h"
Darrick J. Wonge9e899a2017-10-31 12:04:49 -070016#include "xfs_errortag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "xfs_error.h"
Dave Chinner239880e2013-10-23 10:50:10 +110018#include "xfs_log.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#ifdef DEBUG
Dave Chinnercd4a3c52011-04-08 12:45:07 +100021/*
22 * Check that the list is sorted as it should be.
Dave Chinnerd686d122018-05-09 07:49:09 -070023 *
24 * Called with the ail lock held, but we don't want to assert fail with it
25 * held otherwise we'll lock everything up and won't be able to debug the
26 * cause. Hence we sample and check the state under the AIL lock and return if
27 * everything is fine, otherwise we drop the lock and run the ASSERT checks.
28 * Asserts may not be fatal, so pick the lock back up and continue onwards.
Dave Chinnercd4a3c52011-04-08 12:45:07 +100029 */
30STATIC void
31xfs_ail_check(
Dave Chinnerd686d122018-05-09 07:49:09 -070032 struct xfs_ail *ailp,
33 struct xfs_log_item *lip)
Dave Chinnercd4a3c52011-04-08 12:45:07 +100034{
Dave Chinnerd686d122018-05-09 07:49:09 -070035 struct xfs_log_item *prev_lip;
36 struct xfs_log_item *next_lip;
37 xfs_lsn_t prev_lsn = NULLCOMMITLSN;
38 xfs_lsn_t next_lsn = NULLCOMMITLSN;
39 xfs_lsn_t lsn;
40 bool in_ail;
41
Dave Chinnercd4a3c52011-04-08 12:45:07 +100042
Matthew Wilcox57e80952018-03-07 14:59:39 -080043 if (list_empty(&ailp->ail_head))
Dave Chinnercd4a3c52011-04-08 12:45:07 +100044 return;
45
46 /*
Dave Chinnerd686d122018-05-09 07:49:09 -070047 * Sample then check the next and previous entries are valid.
Dave Chinnercd4a3c52011-04-08 12:45:07 +100048 */
Dave Chinnerd686d122018-05-09 07:49:09 -070049 in_ail = test_bit(XFS_LI_IN_AIL, &lip->li_flags);
50 prev_lip = list_entry(lip->li_ail.prev, struct xfs_log_item, li_ail);
Matthew Wilcox57e80952018-03-07 14:59:39 -080051 if (&prev_lip->li_ail != &ailp->ail_head)
Dave Chinnerd686d122018-05-09 07:49:09 -070052 prev_lsn = prev_lip->li_lsn;
53 next_lip = list_entry(lip->li_ail.next, struct xfs_log_item, li_ail);
54 if (&next_lip->li_ail != &ailp->ail_head)
55 next_lsn = next_lip->li_lsn;
56 lsn = lip->li_lsn;
Dave Chinnercd4a3c52011-04-08 12:45:07 +100057
Dave Chinnerd686d122018-05-09 07:49:09 -070058 if (in_ail &&
59 (prev_lsn == NULLCOMMITLSN || XFS_LSN_CMP(prev_lsn, lsn) <= 0) &&
60 (next_lsn == NULLCOMMITLSN || XFS_LSN_CMP(next_lsn, lsn) >= 0))
61 return;
Dave Chinnercd4a3c52011-04-08 12:45:07 +100062
Dave Chinnerd686d122018-05-09 07:49:09 -070063 spin_unlock(&ailp->ail_lock);
64 ASSERT(in_ail);
65 ASSERT(prev_lsn == NULLCOMMITLSN || XFS_LSN_CMP(prev_lsn, lsn) <= 0);
66 ASSERT(next_lsn == NULLCOMMITLSN || XFS_LSN_CMP(next_lsn, lsn) >= 0);
67 spin_lock(&ailp->ail_lock);
Dave Chinnercd4a3c52011-04-08 12:45:07 +100068}
69#else /* !DEBUG */
David Chinnerde08dbc2008-02-05 12:13:38 +110070#define xfs_ail_check(a,l)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#endif /* DEBUG */
72
Dave Chinnercd4a3c52011-04-08 12:45:07 +100073/*
Dave Chinnerfd074842011-04-08 12:45:07 +100074 * Return a pointer to the last item in the AIL. If the AIL is empty, then
75 * return NULL.
76 */
77static xfs_log_item_t *
78xfs_ail_max(
79 struct xfs_ail *ailp)
80{
Matthew Wilcox57e80952018-03-07 14:59:39 -080081 if (list_empty(&ailp->ail_head))
Dave Chinnerfd074842011-04-08 12:45:07 +100082 return NULL;
83
Matthew Wilcox57e80952018-03-07 14:59:39 -080084 return list_entry(ailp->ail_head.prev, xfs_log_item_t, li_ail);
Dave Chinnerfd074842011-04-08 12:45:07 +100085}
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/*
Dave Chinnercd4a3c52011-04-08 12:45:07 +100088 * Return a pointer to the item which follows the given item in the AIL. If
89 * the given item is the last item in the list, then return NULL.
90 */
91static xfs_log_item_t *
92xfs_ail_next(
93 struct xfs_ail *ailp,
94 xfs_log_item_t *lip)
95{
Matthew Wilcox57e80952018-03-07 14:59:39 -080096 if (lip->li_ail.next == &ailp->ail_head)
Dave Chinnercd4a3c52011-04-08 12:45:07 +100097 return NULL;
98
99 return list_first_entry(&lip->li_ail, xfs_log_item_t, li_ail);
100}
101
102/*
103 * This is called by the log manager code to determine the LSN of the tail of
104 * the log. This is exactly the LSN of the first item in the AIL. If the AIL
105 * is empty, then this function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 *
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000107 * We need the AIL lock in order to get a coherent read of the lsn of the last
108 * item in the AIL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 */
110xfs_lsn_t
Dave Chinnerfd074842011-04-08 12:45:07 +1000111xfs_ail_min_lsn(
David Chinner5b00f142008-10-30 17:39:00 +1100112 struct xfs_ail *ailp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000114 xfs_lsn_t lsn = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 xfs_log_item_t *lip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Matthew Wilcox57e80952018-03-07 14:59:39 -0800117 spin_lock(&ailp->ail_lock);
David Chinner5b00f142008-10-30 17:39:00 +1100118 lip = xfs_ail_min(ailp);
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000119 if (lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 lsn = lip->li_lsn;
Matthew Wilcox57e80952018-03-07 14:59:39 -0800121 spin_unlock(&ailp->ail_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 return lsn;
124}
125
126/*
Dave Chinnerfd074842011-04-08 12:45:07 +1000127 * Return the maximum lsn held in the AIL, or zero if the AIL is empty.
128 */
129static xfs_lsn_t
130xfs_ail_max_lsn(
131 struct xfs_ail *ailp)
132{
133 xfs_lsn_t lsn = 0;
134 xfs_log_item_t *lip;
135
Matthew Wilcox57e80952018-03-07 14:59:39 -0800136 spin_lock(&ailp->ail_lock);
Dave Chinnerfd074842011-04-08 12:45:07 +1000137 lip = xfs_ail_max(ailp);
138 if (lip)
139 lsn = lip->li_lsn;
Matthew Wilcox57e80952018-03-07 14:59:39 -0800140 spin_unlock(&ailp->ail_lock);
Dave Chinnerfd074842011-04-08 12:45:07 +1000141
142 return lsn;
143}
144
145/*
Dave Chinneraf3e4022011-07-18 03:40:18 +0000146 * The cursor keeps track of where our current traversal is up to by tracking
147 * the next item in the list for us. However, for this to be safe, removing an
148 * object from the AIL needs to invalidate any cursor that points to it. hence
149 * the traversal cursor needs to be linked to the struct xfs_ail so that
150 * deletion can search all the active cursors for invalidation.
David Chinner27d8d5f2008-10-30 17:38:39 +1100151 */
David Chinner5b00f142008-10-30 17:39:00 +1100152STATIC void
David Chinner27d8d5f2008-10-30 17:38:39 +1100153xfs_trans_ail_cursor_init(
154 struct xfs_ail *ailp,
155 struct xfs_ail_cursor *cur)
156{
157 cur->item = NULL;
Matthew Wilcox57e80952018-03-07 14:59:39 -0800158 list_add_tail(&cur->list, &ailp->ail_cursors);
David Chinner27d8d5f2008-10-30 17:38:39 +1100159}
160
161/*
Dave Chinneraf3e4022011-07-18 03:40:18 +0000162 * Get the next item in the traversal and advance the cursor. If the cursor
163 * was invalidated (indicated by a lip of 1), restart the traversal.
David Chinner27d8d5f2008-10-30 17:38:39 +1100164 */
David Chinner5b00f142008-10-30 17:39:00 +1100165struct xfs_log_item *
David Chinner27d8d5f2008-10-30 17:38:39 +1100166xfs_trans_ail_cursor_next(
167 struct xfs_ail *ailp,
168 struct xfs_ail_cursor *cur)
169{
170 struct xfs_log_item *lip = cur->item;
171
Christoph Hellwigdb9d67d2015-06-22 09:43:32 +1000172 if ((uintptr_t)lip & 1)
David Chinner27d8d5f2008-10-30 17:38:39 +1100173 lip = xfs_ail_min(ailp);
Dave Chinner16b59022011-07-18 03:40:17 +0000174 if (lip)
175 cur->item = xfs_ail_next(ailp, lip);
David Chinner27d8d5f2008-10-30 17:38:39 +1100176 return lip;
177}
178
179/*
Dave Chinneraf3e4022011-07-18 03:40:18 +0000180 * When the traversal is complete, we need to remove the cursor from the list
181 * of traversing cursors.
David Chinner27d8d5f2008-10-30 17:38:39 +1100182 */
183void
184xfs_trans_ail_cursor_done(
Dave Chinneraf3e4022011-07-18 03:40:18 +0000185 struct xfs_ail_cursor *cur)
David Chinner27d8d5f2008-10-30 17:38:39 +1100186{
Dave Chinneraf3e4022011-07-18 03:40:18 +0000187 cur->item = NULL;
188 list_del_init(&cur->list);
David Chinner27d8d5f2008-10-30 17:38:39 +1100189}
190
191/*
Dave Chinneraf3e4022011-07-18 03:40:18 +0000192 * Invalidate any cursor that is pointing to this item. This is called when an
193 * item is removed from the AIL. Any cursor pointing to this object is now
194 * invalid and the traversal needs to be terminated so it doesn't reference a
195 * freed object. We set the low bit of the cursor item pointer so we can
196 * distinguish between an invalidation and the end of the list when getting the
197 * next item from the cursor.
David Chinner5b00f142008-10-30 17:39:00 +1100198 */
199STATIC void
200xfs_trans_ail_cursor_clear(
201 struct xfs_ail *ailp,
202 struct xfs_log_item *lip)
203{
204 struct xfs_ail_cursor *cur;
205
Matthew Wilcox57e80952018-03-07 14:59:39 -0800206 list_for_each_entry(cur, &ailp->ail_cursors, list) {
David Chinner5b00f142008-10-30 17:39:00 +1100207 if (cur->item == lip)
208 cur->item = (struct xfs_log_item *)
Christoph Hellwigdb9d67d2015-06-22 09:43:32 +1000209 ((uintptr_t)cur->item | 1);
David Chinner5b00f142008-10-30 17:39:00 +1100210 }
211}
212
213/*
Dave Chinner16b59022011-07-18 03:40:17 +0000214 * Find the first item in the AIL with the given @lsn by searching in ascending
215 * LSN order and initialise the cursor to point to the next item for a
216 * ascending traversal. Pass a @lsn of zero to initialise the cursor to the
217 * first item in the AIL. Returns NULL if the list is empty.
David Chinner249a8c12008-02-05 12:13:32 +1100218 */
David Chinner5b00f142008-10-30 17:39:00 +1100219xfs_log_item_t *
220xfs_trans_ail_cursor_first(
David Chinner27d8d5f2008-10-30 17:38:39 +1100221 struct xfs_ail *ailp,
222 struct xfs_ail_cursor *cur,
223 xfs_lsn_t lsn)
David Chinner249a8c12008-02-05 12:13:32 +1100224{
David Chinner27d8d5f2008-10-30 17:38:39 +1100225 xfs_log_item_t *lip;
David Chinner249a8c12008-02-05 12:13:32 +1100226
David Chinner5b00f142008-10-30 17:39:00 +1100227 xfs_trans_ail_cursor_init(ailp, cur);
Dave Chinner16b59022011-07-18 03:40:17 +0000228
229 if (lsn == 0) {
230 lip = xfs_ail_min(ailp);
David Chinner5b00f142008-10-30 17:39:00 +1100231 goto out;
Dave Chinner16b59022011-07-18 03:40:17 +0000232 }
David Chinner249a8c12008-02-05 12:13:32 +1100233
Matthew Wilcox57e80952018-03-07 14:59:39 -0800234 list_for_each_entry(lip, &ailp->ail_head, li_ail) {
David Chinner5b00f142008-10-30 17:39:00 +1100235 if (XFS_LSN_CMP(lip->li_lsn, lsn) >= 0)
David Chinner7ee49ac2008-10-30 18:26:51 +1100236 goto out;
Josef 'Jeff' Sipek535f6b32008-03-27 17:58:27 +1100237 }
Dave Chinner16b59022011-07-18 03:40:17 +0000238 return NULL;
239
David Chinner5b00f142008-10-30 17:39:00 +1100240out:
Dave Chinner16b59022011-07-18 03:40:17 +0000241 if (lip)
242 cur->item = xfs_ail_next(ailp, lip);
David Chinner5b00f142008-10-30 17:39:00 +1100243 return lip;
David Chinner249a8c12008-02-05 12:13:32 +1100244}
245
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000246static struct xfs_log_item *
247__xfs_trans_ail_cursor_last(
248 struct xfs_ail *ailp,
249 xfs_lsn_t lsn)
250{
251 xfs_log_item_t *lip;
252
Matthew Wilcox57e80952018-03-07 14:59:39 -0800253 list_for_each_entry_reverse(lip, &ailp->ail_head, li_ail) {
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000254 if (XFS_LSN_CMP(lip->li_lsn, lsn) <= 0)
255 return lip;
256 }
257 return NULL;
258}
259
260/*
Dave Chinner16b59022011-07-18 03:40:17 +0000261 * Find the last item in the AIL with the given @lsn by searching in descending
262 * LSN order and initialise the cursor to point to that item. If there is no
263 * item with the value of @lsn, then it sets the cursor to the last item with an
264 * LSN lower than @lsn. Returns NULL if the list is empty.
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000265 */
266struct xfs_log_item *
267xfs_trans_ail_cursor_last(
268 struct xfs_ail *ailp,
269 struct xfs_ail_cursor *cur,
270 xfs_lsn_t lsn)
271{
272 xfs_trans_ail_cursor_init(ailp, cur);
273 cur->item = __xfs_trans_ail_cursor_last(ailp, lsn);
274 return cur->item;
275}
276
277/*
Dave Chinner16b59022011-07-18 03:40:17 +0000278 * Splice the log item list into the AIL at the given LSN. We splice to the
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000279 * tail of the given LSN to maintain insert order for push traversals. The
280 * cursor is optional, allowing repeated updates to the same LSN to avoid
Alex Eldere44f4112011-07-22 16:04:41 +0000281 * repeated traversals. This should not be called with an empty list.
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000282 */
283static void
284xfs_ail_splice(
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000285 struct xfs_ail *ailp,
286 struct xfs_ail_cursor *cur,
287 struct list_head *list,
288 xfs_lsn_t lsn)
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000289{
Alex Eldere44f4112011-07-22 16:04:41 +0000290 struct xfs_log_item *lip;
291
292 ASSERT(!list_empty(list));
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000293
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000294 /*
Alex Eldere44f4112011-07-22 16:04:41 +0000295 * Use the cursor to determine the insertion point if one is
296 * provided. If not, or if the one we got is not valid,
297 * find the place in the AIL where the items belong.
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000298 */
Alex Eldere44f4112011-07-22 16:04:41 +0000299 lip = cur ? cur->item : NULL;
Christoph Hellwigdb9d67d2015-06-22 09:43:32 +1000300 if (!lip || (uintptr_t)lip & 1)
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000301 lip = __xfs_trans_ail_cursor_last(ailp, lsn);
302
Alex Eldere44f4112011-07-22 16:04:41 +0000303 /*
304 * If a cursor is provided, we know we're processing the AIL
305 * in lsn order, and future items to be spliced in will
306 * follow the last one being inserted now. Update the
307 * cursor to point to that last item, now while we have a
308 * reliable pointer to it.
309 */
310 if (cur)
311 cur->item = list_entry(list->prev, struct xfs_log_item, li_ail);
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000312
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000313 /*
Alex Eldere44f4112011-07-22 16:04:41 +0000314 * Finally perform the splice. Unless the AIL was empty,
315 * lip points to the item in the AIL _after_ which the new
316 * items should go. If lip is null the AIL was empty, so
317 * the new items go at the head of the AIL.
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000318 */
Alex Eldere44f4112011-07-22 16:04:41 +0000319 if (lip)
320 list_splice(list, &lip->li_ail);
321 else
Matthew Wilcox57e80952018-03-07 14:59:39 -0800322 list_splice(list, &ailp->ail_head);
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000323}
324
325/*
326 * Delete the given item from the AIL. Return a pointer to the item.
327 */
328static void
329xfs_ail_delete(
330 struct xfs_ail *ailp,
331 xfs_log_item_t *lip)
332{
333 xfs_ail_check(ailp, lip);
334 list_del(&lip->li_ail);
335 xfs_trans_ail_cursor_clear(ailp, lip);
336}
337
Brian Foster7f4d01f2017-08-08 18:21:52 -0700338static inline uint
339xfsaild_push_item(
340 struct xfs_ail *ailp,
341 struct xfs_log_item *lip)
342{
343 /*
344 * If log item pinning is enabled, skip the push and track the item as
345 * pinned. This can help induce head-behind-tail conditions.
346 */
Matthew Wilcox57e80952018-03-07 14:59:39 -0800347 if (XFS_TEST_ERROR(false, ailp->ail_mount, XFS_ERRTAG_LOG_ITEM_PIN))
Brian Foster7f4d01f2017-08-08 18:21:52 -0700348 return XFS_ITEM_PINNED;
349
Matthew Wilcox57e80952018-03-07 14:59:39 -0800350 return lip->li_ops->iop_push(lip, &ailp->ail_buf_list);
Brian Foster7f4d01f2017-08-08 18:21:52 -0700351}
352
Christoph Hellwig00308072011-10-11 11:14:10 -0400353static long
354xfsaild_push(
355 struct xfs_ail *ailp)
David Chinner249a8c12008-02-05 12:13:32 +1100356{
Matthew Wilcox57e80952018-03-07 14:59:39 -0800357 xfs_mount_t *mp = ailp->ail_mount;
Dave Chinneraf3e4022011-07-18 03:40:18 +0000358 struct xfs_ail_cursor cur;
Dave Chinner9e7004e2011-05-06 02:54:05 +0000359 xfs_log_item_t *lip;
360 xfs_lsn_t lsn;
Dave Chinnerfe0da762011-05-06 02:54:07 +0000361 xfs_lsn_t target;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000362 long tout;
Dave Chinner9e7004e2011-05-06 02:54:05 +0000363 int stuck = 0;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000364 int flushing = 0;
Dave Chinner9e7004e2011-05-06 02:54:05 +0000365 int count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Dave Chinner670ce932011-09-30 04:45:03 +0000367 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000368 * If we encountered pinned items or did not finish writing out all
369 * buffers the last time we ran, force the log first and wait for it
370 * before pushing again.
Dave Chinner670ce932011-09-30 04:45:03 +0000371 */
Matthew Wilcox57e80952018-03-07 14:59:39 -0800372 if (ailp->ail_log_flush && ailp->ail_last_pushed_lsn == 0 &&
373 (!list_empty_careful(&ailp->ail_buf_list) ||
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000374 xfs_ail_min_lsn(ailp))) {
Matthew Wilcox57e80952018-03-07 14:59:39 -0800375 ailp->ail_log_flush = 0;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000376
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100377 XFS_STATS_INC(mp, xs_push_ail_flush);
Dave Chinner670ce932011-09-30 04:45:03 +0000378 xfs_log_force(mp, XFS_LOG_SYNC);
Dave Chinner670ce932011-09-30 04:45:03 +0000379 }
380
Matthew Wilcox57e80952018-03-07 14:59:39 -0800381 spin_lock(&ailp->ail_lock);
Brian Foster8375f922012-06-28 06:52:56 -0400382
Matthew Wilcox57e80952018-03-07 14:59:39 -0800383 /* barrier matches the ail_target update in xfs_ail_push() */
Brian Foster8375f922012-06-28 06:52:56 -0400384 smp_rmb();
Matthew Wilcox57e80952018-03-07 14:59:39 -0800385 target = ailp->ail_target;
386 ailp->ail_target_prev = target;
Brian Foster8375f922012-06-28 06:52:56 -0400387
Matthew Wilcox57e80952018-03-07 14:59:39 -0800388 lip = xfs_trans_ail_cursor_first(ailp, &cur, ailp->ail_last_pushed_lsn);
Christoph Hellwig211e4d42012-04-23 15:58:34 +1000389 if (!lip) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000391 * If the AIL is empty or our push has reached the end we are
392 * done now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 */
Eric Sandeene4a1e292014-04-14 19:06:05 +1000394 xfs_trans_ail_cursor_done(&cur);
Matthew Wilcox57e80952018-03-07 14:59:39 -0800395 spin_unlock(&ailp->ail_lock);
Dave Chinner9e7004e2011-05-06 02:54:05 +0000396 goto out_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100399 XFS_STATS_INC(mp, xs_push_ail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
David Chinner249a8c12008-02-05 12:13:32 +1100401 lsn = lip->li_lsn;
Dave Chinner50e86682011-05-06 02:54:06 +0000402 while ((XFS_LSN_CMP(lip->li_lsn, target) <= 0)) {
David Chinner249a8c12008-02-05 12:13:32 +1100403 int lock_result;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 /*
Dave Chinner904c17e2013-08-28 21:12:03 +1000406 * Note that iop_push may unlock and reacquire the AIL lock. We
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000407 * rely on the AIL cursor implementation to be able to deal with
408 * the dropped lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 */
Brian Foster7f4d01f2017-08-08 18:21:52 -0700410 lock_result = xfsaild_push_item(ailp, lip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 switch (lock_result) {
David Chinner249a8c12008-02-05 12:13:32 +1100412 case XFS_ITEM_SUCCESS:
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100413 XFS_STATS_INC(mp, xs_push_ail_success);
Christoph Hellwig9e4c1092011-10-11 15:14:11 +0000414 trace_xfs_ail_push(lip);
415
Matthew Wilcox57e80952018-03-07 14:59:39 -0800416 ailp->ail_last_pushed_lsn = lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 break;
418
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000419 case XFS_ITEM_FLUSHING:
420 /*
421 * The item or its backing buffer is already beeing
422 * flushed. The typical reason for that is that an
423 * inode buffer is locked because we already pushed the
424 * updates to it as part of inode clustering.
425 *
426 * We do not want to to stop flushing just because lots
427 * of items are already beeing flushed, but we need to
428 * re-try the flushing relatively soon if most of the
429 * AIL is beeing flushed.
430 */
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100431 XFS_STATS_INC(mp, xs_push_ail_flushing);
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000432 trace_xfs_ail_flushing(lip);
Christoph Hellwig17b38472011-10-11 15:14:09 +0000433
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000434 flushing++;
Matthew Wilcox57e80952018-03-07 14:59:39 -0800435 ailp->ail_last_pushed_lsn = lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 break;
437
David Chinner249a8c12008-02-05 12:13:32 +1100438 case XFS_ITEM_PINNED:
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100439 XFS_STATS_INC(mp, xs_push_ail_pinned);
Christoph Hellwig9e4c1092011-10-11 15:14:11 +0000440 trace_xfs_ail_pinned(lip);
441
David Chinner249a8c12008-02-05 12:13:32 +1100442 stuck++;
Matthew Wilcox57e80952018-03-07 14:59:39 -0800443 ailp->ail_log_flush++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 break;
David Chinner249a8c12008-02-05 12:13:32 +1100445 case XFS_ITEM_LOCKED:
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100446 XFS_STATS_INC(mp, xs_push_ail_locked);
Christoph Hellwig9e4c1092011-10-11 15:14:11 +0000447 trace_xfs_ail_locked(lip);
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000448
David Chinner249a8c12008-02-05 12:13:32 +1100449 stuck++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 break;
David Chinner249a8c12008-02-05 12:13:32 +1100451 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 ASSERT(0);
453 break;
454 }
455
David Chinner249a8c12008-02-05 12:13:32 +1100456 count++;
457
458 /*
459 * Are there too many items we can't do anything with?
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000460 *
David Chinner249a8c12008-02-05 12:13:32 +1100461 * If we we are skipping too many items because we can't flush
462 * them or they are already being flushed, we back off and
463 * given them time to complete whatever operation is being
464 * done. i.e. remove pressure from the AIL while we can't make
465 * progress so traversals don't slow down further inserts and
466 * removals to/from the AIL.
467 *
468 * The value of 100 is an arbitrary magic number based on
469 * observation.
470 */
471 if (stuck > 100)
472 break;
473
Dave Chinneraf3e4022011-07-18 03:40:18 +0000474 lip = xfs_trans_ail_cursor_next(ailp, &cur);
David Chinner249a8c12008-02-05 12:13:32 +1100475 if (lip == NULL)
476 break;
David Chinner249a8c12008-02-05 12:13:32 +1100477 lsn = lip->li_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
Eric Sandeene4a1e292014-04-14 19:06:05 +1000479 xfs_trans_ail_cursor_done(&cur);
Matthew Wilcox57e80952018-03-07 14:59:39 -0800480 spin_unlock(&ailp->ail_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Matthew Wilcox57e80952018-03-07 14:59:39 -0800482 if (xfs_buf_delwri_submit_nowait(&ailp->ail_buf_list))
483 ailp->ail_log_flush++;
Dave Chinnerd808f612010-02-02 10:13:42 +1100484
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000485 if (!count || XFS_LSN_CMP(lsn, target) >= 0) {
Dave Chinner9e7004e2011-05-06 02:54:05 +0000486out_done:
David Chinner92d9cd12008-03-06 13:45:10 +1100487 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000488 * We reached the target or the AIL is empty, so wait a bit
489 * longer for I/O to complete and remove pushed items from the
490 * AIL before we start the next scan from the start of the AIL.
David Chinner92d9cd12008-03-06 13:45:10 +1100491 */
Dave Chinner453eac82010-01-11 11:49:58 +0000492 tout = 50;
Matthew Wilcox57e80952018-03-07 14:59:39 -0800493 ailp->ail_last_pushed_lsn = 0;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000494 } else if (((stuck + flushing) * 100) / count > 90) {
David Chinner249a8c12008-02-05 12:13:32 +1100495 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000496 * Either there is a lot of contention on the AIL or we are
497 * stuck due to operations in progress. "Stuck" in this case
498 * is defined as >90% of the items we tried to push were stuck.
David Chinner249a8c12008-02-05 12:13:32 +1100499 *
500 * Backoff a bit more to allow some I/O to complete before
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000501 * restarting from the start of the AIL. This prevents us from
502 * spinning on the same items, and if they are pinned will all
503 * the restart to issue a log force to unpin the stuck items.
David Chinner249a8c12008-02-05 12:13:32 +1100504 */
Dave Chinner453eac82010-01-11 11:49:58 +0000505 tout = 20;
Matthew Wilcox57e80952018-03-07 14:59:39 -0800506 ailp->ail_last_pushed_lsn = 0;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000507 } else {
508 /*
509 * Assume we have more work to do in a short while.
510 */
511 tout = 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
Dave Chinner0bf6a5b2011-04-08 12:45:07 +1000513
Christoph Hellwig00308072011-10-11 11:14:10 -0400514 return tout;
515}
516
517static int
518xfsaild(
519 void *data)
520{
521 struct xfs_ail *ailp = data;
522 long tout = 0; /* milliseconds */
523
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000524 current->flags |= PF_MEMALLOC;
Michal Hocko18f1df42016-02-08 14:59:07 +1100525 set_freezable();
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000526
Hou Tao0bd89672017-10-17 14:16:28 -0700527 while (1) {
Christoph Hellwig00308072011-10-11 11:14:10 -0400528 if (tout && tout <= 20)
Hou Tao0bd89672017-10-17 14:16:28 -0700529 set_current_state(TASK_KILLABLE);
Christoph Hellwig00308072011-10-11 11:14:10 -0400530 else
Hou Tao0bd89672017-10-17 14:16:28 -0700531 set_current_state(TASK_INTERRUPTIBLE);
532
533 /*
Brian Fosterefc32892018-10-18 17:21:49 +1100534 * Check kthread_should_stop() after we set the task state to
535 * guarantee that we either see the stop bit and exit or the
536 * task state is reset to runnable such that it's not scheduled
537 * out indefinitely and detects the stop bit at next iteration.
Hou Tao0bd89672017-10-17 14:16:28 -0700538 * A memory barrier is included in above task state set to
539 * serialize again kthread_stop().
540 */
541 if (kthread_should_stop()) {
542 __set_current_state(TASK_RUNNING);
Brian Fosterefc32892018-10-18 17:21:49 +1100543
544 /*
545 * The caller forces out the AIL before stopping the
546 * thread in the common case, which means the delwri
547 * queue is drained. In the shutdown case, the queue may
548 * still hold relogged buffers that haven't been
549 * submitted because they were pinned since added to the
550 * queue.
551 *
552 * Log I/O error processing stales the underlying buffer
553 * and clears the delwri state, expecting the buf to be
554 * removed on the next submission attempt. That won't
555 * happen if we're shutting down, so this is the last
556 * opportunity to release such buffers from the queue.
557 */
558 ASSERT(list_empty(&ailp->ail_buf_list) ||
559 XFS_FORCED_SHUTDOWN(ailp->ail_mount));
560 xfs_buf_delwri_cancel(&ailp->ail_buf_list);
Hou Tao0bd89672017-10-17 14:16:28 -0700561 break;
562 }
Brian Foster8375f922012-06-28 06:52:56 -0400563
Matthew Wilcox57e80952018-03-07 14:59:39 -0800564 spin_lock(&ailp->ail_lock);
Brian Foster8375f922012-06-28 06:52:56 -0400565
566 /*
567 * Idle if the AIL is empty and we are not racing with a target
568 * update. We check the AIL after we set the task to a sleep
Matthew Wilcox57e80952018-03-07 14:59:39 -0800569 * state to guarantee that we either catch an ail_target update
Brian Foster8375f922012-06-28 06:52:56 -0400570 * or that a wake_up resets the state to TASK_RUNNING.
571 * Otherwise, we run the risk of sleeping indefinitely.
572 *
Matthew Wilcox57e80952018-03-07 14:59:39 -0800573 * The barrier matches the ail_target update in xfs_ail_push().
Brian Foster8375f922012-06-28 06:52:56 -0400574 */
575 smp_rmb();
576 if (!xfs_ail_min(ailp) &&
Matthew Wilcox57e80952018-03-07 14:59:39 -0800577 ailp->ail_target == ailp->ail_target_prev) {
578 spin_unlock(&ailp->ail_lock);
Michal Hocko18f1df42016-02-08 14:59:07 +1100579 freezable_schedule();
Brian Foster8375f922012-06-28 06:52:56 -0400580 tout = 0;
581 continue;
582 }
Matthew Wilcox57e80952018-03-07 14:59:39 -0800583 spin_unlock(&ailp->ail_lock);
Brian Foster8375f922012-06-28 06:52:56 -0400584
585 if (tout)
Michal Hocko18f1df42016-02-08 14:59:07 +1100586 freezable_schedule_timeout(msecs_to_jiffies(tout));
Brian Foster8375f922012-06-28 06:52:56 -0400587
588 __set_current_state(TASK_RUNNING);
Christoph Hellwig00308072011-10-11 11:14:10 -0400589
590 try_to_freeze();
591
592 tout = xfsaild_push(ailp);
593 }
594
595 return 0;
Dave Chinner453eac82010-01-11 11:49:58 +0000596}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Dave Chinner0bf6a5b2011-04-08 12:45:07 +1000598/*
599 * This routine is called to move the tail of the AIL forward. It does this by
600 * trying to flush items in the AIL whose lsns are below the given
601 * threshold_lsn.
602 *
603 * The push is run asynchronously in a workqueue, which means the caller needs
604 * to handle waiting on the async flush for space to become available.
605 * We don't want to interrupt any push that is in progress, hence we only queue
606 * work if we set the pushing bit approriately.
607 *
608 * We do this unlocked - we only need to know whether there is anything in the
609 * AIL at the time we are called. We don't need to access the contents of
610 * any of the objects, so the lock is not needed.
611 */
612void
Dave Chinnerfd074842011-04-08 12:45:07 +1000613xfs_ail_push(
Dave Chinner0bf6a5b2011-04-08 12:45:07 +1000614 struct xfs_ail *ailp,
615 xfs_lsn_t threshold_lsn)
616{
617 xfs_log_item_t *lip;
618
619 lip = xfs_ail_min(ailp);
Matthew Wilcox57e80952018-03-07 14:59:39 -0800620 if (!lip || XFS_FORCED_SHUTDOWN(ailp->ail_mount) ||
621 XFS_LSN_CMP(threshold_lsn, ailp->ail_target) <= 0)
Dave Chinner0bf6a5b2011-04-08 12:45:07 +1000622 return;
623
624 /*
625 * Ensure that the new target is noticed in push code before it clears
626 * the XFS_AIL_PUSHING_BIT.
627 */
628 smp_wmb();
Matthew Wilcox57e80952018-03-07 14:59:39 -0800629 xfs_trans_ail_copy_lsn(ailp, &ailp->ail_target, &threshold_lsn);
Christoph Hellwig00308072011-10-11 11:14:10 -0400630 smp_wmb();
631
Matthew Wilcox57e80952018-03-07 14:59:39 -0800632 wake_up_process(ailp->ail_task);
Dave Chinner0bf6a5b2011-04-08 12:45:07 +1000633}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635/*
Dave Chinnerfd074842011-04-08 12:45:07 +1000636 * Push out all items in the AIL immediately
637 */
638void
639xfs_ail_push_all(
640 struct xfs_ail *ailp)
641{
642 xfs_lsn_t threshold_lsn = xfs_ail_max_lsn(ailp);
643
644 if (threshold_lsn)
645 xfs_ail_push(ailp, threshold_lsn);
646}
647
648/*
Christoph Hellwig211e4d42012-04-23 15:58:34 +1000649 * Push out all items in the AIL immediately and wait until the AIL is empty.
650 */
651void
652xfs_ail_push_all_sync(
653 struct xfs_ail *ailp)
654{
655 struct xfs_log_item *lip;
656 DEFINE_WAIT(wait);
657
Matthew Wilcox57e80952018-03-07 14:59:39 -0800658 spin_lock(&ailp->ail_lock);
Christoph Hellwig211e4d42012-04-23 15:58:34 +1000659 while ((lip = xfs_ail_max(ailp)) != NULL) {
Matthew Wilcox57e80952018-03-07 14:59:39 -0800660 prepare_to_wait(&ailp->ail_empty, &wait, TASK_UNINTERRUPTIBLE);
661 ailp->ail_target = lip->li_lsn;
662 wake_up_process(ailp->ail_task);
663 spin_unlock(&ailp->ail_lock);
Christoph Hellwig211e4d42012-04-23 15:58:34 +1000664 schedule();
Matthew Wilcox57e80952018-03-07 14:59:39 -0800665 spin_lock(&ailp->ail_lock);
Christoph Hellwig211e4d42012-04-23 15:58:34 +1000666 }
Matthew Wilcox57e80952018-03-07 14:59:39 -0800667 spin_unlock(&ailp->ail_lock);
Christoph Hellwig211e4d42012-04-23 15:58:34 +1000668
Matthew Wilcox57e80952018-03-07 14:59:39 -0800669 finish_wait(&ailp->ail_empty, &wait);
Christoph Hellwig211e4d42012-04-23 15:58:34 +1000670}
671
672/*
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100673 * xfs_trans_ail_update - bulk AIL insertion operation.
674 *
675 * @xfs_trans_ail_update takes an array of log items that all need to be
676 * positioned at the same LSN in the AIL. If an item is not in the AIL, it will
677 * be added. Otherwise, it will be repositioned by removing it and re-adding
678 * it to the AIL. If we move the first item in the AIL, update the log tail to
679 * match the new minimum LSN in the AIL.
680 *
681 * This function takes the AIL lock once to execute the update operations on
682 * all the items in the array, and as such should not be called with the AIL
683 * lock held. As a result, once we have the AIL lock, we need to check each log
684 * item LSN to confirm it needs to be moved forward in the AIL.
685 *
686 * To optimise the insert operation, we delete all the items from the AIL in
687 * the first pass, moving them into a temporary list, then splice the temporary
688 * list into the correct position in the AIL. This avoids needing to do an
689 * insert operation on every item.
690 *
691 * This function must be called with the AIL lock held. The lock is dropped
692 * before returning.
693 */
694void
695xfs_trans_ail_update_bulk(
696 struct xfs_ail *ailp,
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000697 struct xfs_ail_cursor *cur,
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100698 struct xfs_log_item **log_items,
699 int nr_items,
Matthew Wilcox57e80952018-03-07 14:59:39 -0800700 xfs_lsn_t lsn) __releases(ailp->ail_lock)
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100701{
702 xfs_log_item_t *mlip;
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100703 int mlip_changed = 0;
704 int i;
705 LIST_HEAD(tmp);
706
Alex Eldere44f4112011-07-22 16:04:41 +0000707 ASSERT(nr_items > 0); /* Not required, but true. */
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100708 mlip = xfs_ail_min(ailp);
709
710 for (i = 0; i < nr_items; i++) {
711 struct xfs_log_item *lip = log_items[i];
Dave Chinner22525c12018-05-09 07:47:34 -0700712 if (test_and_set_bit(XFS_LI_IN_AIL, &lip->li_flags)) {
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100713 /* check if we really need to move the item */
714 if (XFS_LSN_CMP(lsn, lip->li_lsn) <= 0)
715 continue;
716
Dave Chinner750b9c92013-11-01 15:27:18 +1100717 trace_xfs_ail_move(lip, lip->li_lsn, lsn);
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100718 xfs_ail_delete(ailp, lip);
719 if (mlip == lip)
720 mlip_changed = 1;
721 } else {
Dave Chinner750b9c92013-11-01 15:27:18 +1100722 trace_xfs_ail_insert(lip, 0, lsn);
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100723 }
724 lip->li_lsn = lsn;
725 list_add(&lip->li_ail, &tmp);
726 }
727
Alex Eldere44f4112011-07-22 16:04:41 +0000728 if (!list_empty(&tmp))
729 xfs_ail_splice(ailp, cur, &tmp, lsn);
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000730
Christoph Hellwig1c304622012-04-23 15:58:33 +1000731 if (mlip_changed) {
Matthew Wilcox57e80952018-03-07 14:59:39 -0800732 if (!XFS_FORCED_SHUTDOWN(ailp->ail_mount))
733 xlog_assign_tail_lsn_locked(ailp->ail_mount);
734 spin_unlock(&ailp->ail_lock);
Christoph Hellwig1c304622012-04-23 15:58:33 +1000735
Matthew Wilcox57e80952018-03-07 14:59:39 -0800736 xfs_log_space_wake(ailp->ail_mount);
Christoph Hellwig1c304622012-04-23 15:58:33 +1000737 } else {
Matthew Wilcox57e80952018-03-07 14:59:39 -0800738 spin_unlock(&ailp->ail_lock);
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000739 }
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100740}
741
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700742bool
743xfs_ail_delete_one(
744 struct xfs_ail *ailp,
Carlos Maiolinod3a304b2017-08-08 18:21:50 -0700745 struct xfs_log_item *lip)
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700746{
747 struct xfs_log_item *mlip = xfs_ail_min(ailp);
748
749 trace_xfs_ail_delete(lip, mlip->li_lsn, lip->li_lsn);
750 xfs_ail_delete(ailp, lip);
Carlos Maiolinod3a304b2017-08-08 18:21:50 -0700751 xfs_clear_li_failed(lip);
Dave Chinner22525c12018-05-09 07:47:34 -0700752 clear_bit(XFS_LI_IN_AIL, &lip->li_flags);
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700753 lip->li_lsn = 0;
754
755 return mlip == lip;
756}
757
758/**
759 * Remove a log items from the AIL
Dave Chinner30136832010-12-20 12:03:17 +1100760 *
761 * @xfs_trans_ail_delete_bulk takes an array of log items that all need to
762 * removed from the AIL. The caller is already holding the AIL lock, and done
763 * all the checks necessary to ensure the items passed in via @log_items are
764 * ready for deletion. This includes checking that the items are in the AIL.
765 *
766 * For each log item to be removed, unlink it from the AIL, clear the IN_AIL
767 * flag from the item and reset the item's lsn to 0. If we remove the first
768 * item in the AIL, update the log tail to match the new minimum LSN in the
769 * AIL.
770 *
771 * This function will not drop the AIL lock until all items are removed from
772 * the AIL to minimise the amount of lock traffic on the AIL. This does not
773 * greatly increase the AIL hold time, but does significantly reduce the amount
774 * of traffic on the lock, especially during IO completion.
775 *
776 * This function must be called with the AIL lock held. The lock is dropped
777 * before returning.
778 */
779void
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700780xfs_trans_ail_delete(
Dave Chinner30136832010-12-20 12:03:17 +1100781 struct xfs_ail *ailp,
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700782 struct xfs_log_item *lip,
Matthew Wilcox57e80952018-03-07 14:59:39 -0800783 int shutdown_type) __releases(ailp->ail_lock)
Dave Chinner30136832010-12-20 12:03:17 +1100784{
Matthew Wilcox57e80952018-03-07 14:59:39 -0800785 struct xfs_mount *mp = ailp->ail_mount;
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700786 bool mlip_changed;
Dave Chinner30136832010-12-20 12:03:17 +1100787
Dave Chinner22525c12018-05-09 07:47:34 -0700788 if (!test_bit(XFS_LI_IN_AIL, &lip->li_flags)) {
Matthew Wilcox57e80952018-03-07 14:59:39 -0800789 spin_unlock(&ailp->ail_lock);
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700790 if (!XFS_FORCED_SHUTDOWN(mp)) {
791 xfs_alert_tag(mp, XFS_PTAG_AILDELETE,
792 "%s: attempting to delete a log item that is not in the AIL",
793 __func__);
794 xfs_force_shutdown(mp, shutdown_type);
Dave Chinner30136832010-12-20 12:03:17 +1100795 }
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700796 return;
Dave Chinner30136832010-12-20 12:03:17 +1100797 }
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000798
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700799 mlip_changed = xfs_ail_delete_one(ailp, lip);
Christoph Hellwig1c304622012-04-23 15:58:33 +1000800 if (mlip_changed) {
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700801 if (!XFS_FORCED_SHUTDOWN(mp))
802 xlog_assign_tail_lsn_locked(mp);
Matthew Wilcox57e80952018-03-07 14:59:39 -0800803 if (list_empty(&ailp->ail_head))
804 wake_up_all(&ailp->ail_empty);
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000805 }
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700806
Matthew Wilcox57e80952018-03-07 14:59:39 -0800807 spin_unlock(&ailp->ail_lock);
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700808 if (mlip_changed)
Matthew Wilcox57e80952018-03-07 14:59:39 -0800809 xfs_log_space_wake(ailp->ail_mount);
Dave Chinner30136832010-12-20 12:03:17 +1100810}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
David Chinner249a8c12008-02-05 12:13:32 +1100812int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813xfs_trans_ail_init(
814 xfs_mount_t *mp)
815{
David Chinner82fa9012008-10-30 17:38:26 +1100816 struct xfs_ail *ailp;
817
818 ailp = kmem_zalloc(sizeof(struct xfs_ail), KM_MAYFAIL);
819 if (!ailp)
Dave Chinner24513372014-06-25 14:58:08 +1000820 return -ENOMEM;
David Chinner82fa9012008-10-30 17:38:26 +1100821
Matthew Wilcox57e80952018-03-07 14:59:39 -0800822 ailp->ail_mount = mp;
823 INIT_LIST_HEAD(&ailp->ail_head);
824 INIT_LIST_HEAD(&ailp->ail_cursors);
825 spin_lock_init(&ailp->ail_lock);
826 INIT_LIST_HEAD(&ailp->ail_buf_list);
827 init_waitqueue_head(&ailp->ail_empty);
Christoph Hellwig00308072011-10-11 11:14:10 -0400828
Matthew Wilcox57e80952018-03-07 14:59:39 -0800829 ailp->ail_task = kthread_run(xfsaild, ailp, "xfsaild/%s",
830 ailp->ail_mount->m_fsname);
831 if (IS_ERR(ailp->ail_task))
Christoph Hellwig00308072011-10-11 11:14:10 -0400832 goto out_free_ailp;
833
David Chinner27d8d5f2008-10-30 17:38:39 +1100834 mp->m_ail = ailp;
835 return 0;
Christoph Hellwig00308072011-10-11 11:14:10 -0400836
837out_free_ailp:
838 kmem_free(ailp);
Dave Chinner24513372014-06-25 14:58:08 +1000839 return -ENOMEM;
David Chinner249a8c12008-02-05 12:13:32 +1100840}
841
842void
843xfs_trans_ail_destroy(
844 xfs_mount_t *mp)
845{
David Chinner82fa9012008-10-30 17:38:26 +1100846 struct xfs_ail *ailp = mp->m_ail;
847
Matthew Wilcox57e80952018-03-07 14:59:39 -0800848 kthread_stop(ailp->ail_task);
David Chinner82fa9012008-10-30 17:38:26 +1100849 kmem_free(ailp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}