blob: 41e280ef148381741157fc63459198cf06cdcbaf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
David Chinnerc7e8f262008-10-30 17:39:23 +11003 * Copyright (c) 2008 Dave Chinner
Nathan Scott7b718762005-11-02 14:58:39 +11004 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Nathan Scott7b718762005-11-02 14:58:39 +11006 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * published by the Free Software Foundation.
9 *
Nathan Scott7b718762005-11-02 14:58:39 +110010 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
Nathan Scott7b718762005-11-02 14:58:39 +110015 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_fs.h"
Christoph Hellwig4fb6e8a2014-11-28 14:25:04 +110021#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110022#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_mount.h"
Dave Chinner239880e2013-10-23 10:50:10 +110025#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_trans_priv.h"
Christoph Hellwig9e4c1092011-10-11 15:14:11 +000027#include "xfs_trace.h"
Darrick J. Wonge9e899a2017-10-31 12:04:49 -070028#include "xfs_errortag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_error.h"
Dave Chinner239880e2013-10-23 10:50:10 +110030#include "xfs_log.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#ifdef DEBUG
Dave Chinnercd4a3c52011-04-08 12:45:07 +100033/*
34 * Check that the list is sorted as it should be.
Dave Chinnerd686d122018-05-09 07:49:09 -070035 *
36 * Called with the ail lock held, but we don't want to assert fail with it
37 * held otherwise we'll lock everything up and won't be able to debug the
38 * cause. Hence we sample and check the state under the AIL lock and return if
39 * everything is fine, otherwise we drop the lock and run the ASSERT checks.
40 * Asserts may not be fatal, so pick the lock back up and continue onwards.
Dave Chinnercd4a3c52011-04-08 12:45:07 +100041 */
42STATIC void
43xfs_ail_check(
Dave Chinnerd686d122018-05-09 07:49:09 -070044 struct xfs_ail *ailp,
45 struct xfs_log_item *lip)
Dave Chinnercd4a3c52011-04-08 12:45:07 +100046{
Dave Chinnerd686d122018-05-09 07:49:09 -070047 struct xfs_log_item *prev_lip;
48 struct xfs_log_item *next_lip;
49 xfs_lsn_t prev_lsn = NULLCOMMITLSN;
50 xfs_lsn_t next_lsn = NULLCOMMITLSN;
51 xfs_lsn_t lsn;
52 bool in_ail;
53
Dave Chinnercd4a3c52011-04-08 12:45:07 +100054
Matthew Wilcox57e80952018-03-07 14:59:39 -080055 if (list_empty(&ailp->ail_head))
Dave Chinnercd4a3c52011-04-08 12:45:07 +100056 return;
57
58 /*
Dave Chinnerd686d122018-05-09 07:49:09 -070059 * Sample then check the next and previous entries are valid.
Dave Chinnercd4a3c52011-04-08 12:45:07 +100060 */
Dave Chinnerd686d122018-05-09 07:49:09 -070061 in_ail = test_bit(XFS_LI_IN_AIL, &lip->li_flags);
62 prev_lip = list_entry(lip->li_ail.prev, struct xfs_log_item, li_ail);
Matthew Wilcox57e80952018-03-07 14:59:39 -080063 if (&prev_lip->li_ail != &ailp->ail_head)
Dave Chinnerd686d122018-05-09 07:49:09 -070064 prev_lsn = prev_lip->li_lsn;
65 next_lip = list_entry(lip->li_ail.next, struct xfs_log_item, li_ail);
66 if (&next_lip->li_ail != &ailp->ail_head)
67 next_lsn = next_lip->li_lsn;
68 lsn = lip->li_lsn;
Dave Chinnercd4a3c52011-04-08 12:45:07 +100069
Dave Chinnerd686d122018-05-09 07:49:09 -070070 if (in_ail &&
71 (prev_lsn == NULLCOMMITLSN || XFS_LSN_CMP(prev_lsn, lsn) <= 0) &&
72 (next_lsn == NULLCOMMITLSN || XFS_LSN_CMP(next_lsn, lsn) >= 0))
73 return;
Dave Chinnercd4a3c52011-04-08 12:45:07 +100074
Dave Chinnerd686d122018-05-09 07:49:09 -070075 spin_unlock(&ailp->ail_lock);
76 ASSERT(in_ail);
77 ASSERT(prev_lsn == NULLCOMMITLSN || XFS_LSN_CMP(prev_lsn, lsn) <= 0);
78 ASSERT(next_lsn == NULLCOMMITLSN || XFS_LSN_CMP(next_lsn, lsn) >= 0);
79 spin_lock(&ailp->ail_lock);
Dave Chinnercd4a3c52011-04-08 12:45:07 +100080}
81#else /* !DEBUG */
David Chinnerde08dbc2008-02-05 12:13:38 +110082#define xfs_ail_check(a,l)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#endif /* DEBUG */
84
Dave Chinnercd4a3c52011-04-08 12:45:07 +100085/*
Dave Chinnerfd074842011-04-08 12:45:07 +100086 * Return a pointer to the last item in the AIL. If the AIL is empty, then
87 * return NULL.
88 */
89static xfs_log_item_t *
90xfs_ail_max(
91 struct xfs_ail *ailp)
92{
Matthew Wilcox57e80952018-03-07 14:59:39 -080093 if (list_empty(&ailp->ail_head))
Dave Chinnerfd074842011-04-08 12:45:07 +100094 return NULL;
95
Matthew Wilcox57e80952018-03-07 14:59:39 -080096 return list_entry(ailp->ail_head.prev, xfs_log_item_t, li_ail);
Dave Chinnerfd074842011-04-08 12:45:07 +100097}
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099/*
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000100 * Return a pointer to the item which follows the given item in the AIL. If
101 * the given item is the last item in the list, then return NULL.
102 */
103static xfs_log_item_t *
104xfs_ail_next(
105 struct xfs_ail *ailp,
106 xfs_log_item_t *lip)
107{
Matthew Wilcox57e80952018-03-07 14:59:39 -0800108 if (lip->li_ail.next == &ailp->ail_head)
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000109 return NULL;
110
111 return list_first_entry(&lip->li_ail, xfs_log_item_t, li_ail);
112}
113
114/*
115 * This is called by the log manager code to determine the LSN of the tail of
116 * the log. This is exactly the LSN of the first item in the AIL. If the AIL
117 * is empty, then this function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 *
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000119 * We need the AIL lock in order to get a coherent read of the lsn of the last
120 * item in the AIL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 */
122xfs_lsn_t
Dave Chinnerfd074842011-04-08 12:45:07 +1000123xfs_ail_min_lsn(
David Chinner5b00f142008-10-30 17:39:00 +1100124 struct xfs_ail *ailp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000126 xfs_lsn_t lsn = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 xfs_log_item_t *lip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Matthew Wilcox57e80952018-03-07 14:59:39 -0800129 spin_lock(&ailp->ail_lock);
David Chinner5b00f142008-10-30 17:39:00 +1100130 lip = xfs_ail_min(ailp);
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000131 if (lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 lsn = lip->li_lsn;
Matthew Wilcox57e80952018-03-07 14:59:39 -0800133 spin_unlock(&ailp->ail_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 return lsn;
136}
137
138/*
Dave Chinnerfd074842011-04-08 12:45:07 +1000139 * Return the maximum lsn held in the AIL, or zero if the AIL is empty.
140 */
141static xfs_lsn_t
142xfs_ail_max_lsn(
143 struct xfs_ail *ailp)
144{
145 xfs_lsn_t lsn = 0;
146 xfs_log_item_t *lip;
147
Matthew Wilcox57e80952018-03-07 14:59:39 -0800148 spin_lock(&ailp->ail_lock);
Dave Chinnerfd074842011-04-08 12:45:07 +1000149 lip = xfs_ail_max(ailp);
150 if (lip)
151 lsn = lip->li_lsn;
Matthew Wilcox57e80952018-03-07 14:59:39 -0800152 spin_unlock(&ailp->ail_lock);
Dave Chinnerfd074842011-04-08 12:45:07 +1000153
154 return lsn;
155}
156
157/*
Dave Chinneraf3e4022011-07-18 03:40:18 +0000158 * The cursor keeps track of where our current traversal is up to by tracking
159 * the next item in the list for us. However, for this to be safe, removing an
160 * object from the AIL needs to invalidate any cursor that points to it. hence
161 * the traversal cursor needs to be linked to the struct xfs_ail so that
162 * deletion can search all the active cursors for invalidation.
David Chinner27d8d5f2008-10-30 17:38:39 +1100163 */
David Chinner5b00f142008-10-30 17:39:00 +1100164STATIC void
David Chinner27d8d5f2008-10-30 17:38:39 +1100165xfs_trans_ail_cursor_init(
166 struct xfs_ail *ailp,
167 struct xfs_ail_cursor *cur)
168{
169 cur->item = NULL;
Matthew Wilcox57e80952018-03-07 14:59:39 -0800170 list_add_tail(&cur->list, &ailp->ail_cursors);
David Chinner27d8d5f2008-10-30 17:38:39 +1100171}
172
173/*
Dave Chinneraf3e4022011-07-18 03:40:18 +0000174 * Get the next item in the traversal and advance the cursor. If the cursor
175 * was invalidated (indicated by a lip of 1), restart the traversal.
David Chinner27d8d5f2008-10-30 17:38:39 +1100176 */
David Chinner5b00f142008-10-30 17:39:00 +1100177struct xfs_log_item *
David Chinner27d8d5f2008-10-30 17:38:39 +1100178xfs_trans_ail_cursor_next(
179 struct xfs_ail *ailp,
180 struct xfs_ail_cursor *cur)
181{
182 struct xfs_log_item *lip = cur->item;
183
Christoph Hellwigdb9d67d2015-06-22 09:43:32 +1000184 if ((uintptr_t)lip & 1)
David Chinner27d8d5f2008-10-30 17:38:39 +1100185 lip = xfs_ail_min(ailp);
Dave Chinner16b59022011-07-18 03:40:17 +0000186 if (lip)
187 cur->item = xfs_ail_next(ailp, lip);
David Chinner27d8d5f2008-10-30 17:38:39 +1100188 return lip;
189}
190
191/*
Dave Chinneraf3e4022011-07-18 03:40:18 +0000192 * When the traversal is complete, we need to remove the cursor from the list
193 * of traversing cursors.
David Chinner27d8d5f2008-10-30 17:38:39 +1100194 */
195void
196xfs_trans_ail_cursor_done(
Dave Chinneraf3e4022011-07-18 03:40:18 +0000197 struct xfs_ail_cursor *cur)
David Chinner27d8d5f2008-10-30 17:38:39 +1100198{
Dave Chinneraf3e4022011-07-18 03:40:18 +0000199 cur->item = NULL;
200 list_del_init(&cur->list);
David Chinner27d8d5f2008-10-30 17:38:39 +1100201}
202
203/*
Dave Chinneraf3e4022011-07-18 03:40:18 +0000204 * Invalidate any cursor that is pointing to this item. This is called when an
205 * item is removed from the AIL. Any cursor pointing to this object is now
206 * invalid and the traversal needs to be terminated so it doesn't reference a
207 * freed object. We set the low bit of the cursor item pointer so we can
208 * distinguish between an invalidation and the end of the list when getting the
209 * next item from the cursor.
David Chinner5b00f142008-10-30 17:39:00 +1100210 */
211STATIC void
212xfs_trans_ail_cursor_clear(
213 struct xfs_ail *ailp,
214 struct xfs_log_item *lip)
215{
216 struct xfs_ail_cursor *cur;
217
Matthew Wilcox57e80952018-03-07 14:59:39 -0800218 list_for_each_entry(cur, &ailp->ail_cursors, list) {
David Chinner5b00f142008-10-30 17:39:00 +1100219 if (cur->item == lip)
220 cur->item = (struct xfs_log_item *)
Christoph Hellwigdb9d67d2015-06-22 09:43:32 +1000221 ((uintptr_t)cur->item | 1);
David Chinner5b00f142008-10-30 17:39:00 +1100222 }
223}
224
225/*
Dave Chinner16b59022011-07-18 03:40:17 +0000226 * Find the first item in the AIL with the given @lsn by searching in ascending
227 * LSN order and initialise the cursor to point to the next item for a
228 * ascending traversal. Pass a @lsn of zero to initialise the cursor to the
229 * first item in the AIL. Returns NULL if the list is empty.
David Chinner249a8c12008-02-05 12:13:32 +1100230 */
David Chinner5b00f142008-10-30 17:39:00 +1100231xfs_log_item_t *
232xfs_trans_ail_cursor_first(
David Chinner27d8d5f2008-10-30 17:38:39 +1100233 struct xfs_ail *ailp,
234 struct xfs_ail_cursor *cur,
235 xfs_lsn_t lsn)
David Chinner249a8c12008-02-05 12:13:32 +1100236{
David Chinner27d8d5f2008-10-30 17:38:39 +1100237 xfs_log_item_t *lip;
David Chinner249a8c12008-02-05 12:13:32 +1100238
David Chinner5b00f142008-10-30 17:39:00 +1100239 xfs_trans_ail_cursor_init(ailp, cur);
Dave Chinner16b59022011-07-18 03:40:17 +0000240
241 if (lsn == 0) {
242 lip = xfs_ail_min(ailp);
David Chinner5b00f142008-10-30 17:39:00 +1100243 goto out;
Dave Chinner16b59022011-07-18 03:40:17 +0000244 }
David Chinner249a8c12008-02-05 12:13:32 +1100245
Matthew Wilcox57e80952018-03-07 14:59:39 -0800246 list_for_each_entry(lip, &ailp->ail_head, li_ail) {
David Chinner5b00f142008-10-30 17:39:00 +1100247 if (XFS_LSN_CMP(lip->li_lsn, lsn) >= 0)
David Chinner7ee49ac2008-10-30 18:26:51 +1100248 goto out;
Josef 'Jeff' Sipek535f6b32008-03-27 17:58:27 +1100249 }
Dave Chinner16b59022011-07-18 03:40:17 +0000250 return NULL;
251
David Chinner5b00f142008-10-30 17:39:00 +1100252out:
Dave Chinner16b59022011-07-18 03:40:17 +0000253 if (lip)
254 cur->item = xfs_ail_next(ailp, lip);
David Chinner5b00f142008-10-30 17:39:00 +1100255 return lip;
David Chinner249a8c12008-02-05 12:13:32 +1100256}
257
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000258static struct xfs_log_item *
259__xfs_trans_ail_cursor_last(
260 struct xfs_ail *ailp,
261 xfs_lsn_t lsn)
262{
263 xfs_log_item_t *lip;
264
Matthew Wilcox57e80952018-03-07 14:59:39 -0800265 list_for_each_entry_reverse(lip, &ailp->ail_head, li_ail) {
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000266 if (XFS_LSN_CMP(lip->li_lsn, lsn) <= 0)
267 return lip;
268 }
269 return NULL;
270}
271
272/*
Dave Chinner16b59022011-07-18 03:40:17 +0000273 * Find the last item in the AIL with the given @lsn by searching in descending
274 * LSN order and initialise the cursor to point to that item. If there is no
275 * item with the value of @lsn, then it sets the cursor to the last item with an
276 * LSN lower than @lsn. Returns NULL if the list is empty.
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000277 */
278struct xfs_log_item *
279xfs_trans_ail_cursor_last(
280 struct xfs_ail *ailp,
281 struct xfs_ail_cursor *cur,
282 xfs_lsn_t lsn)
283{
284 xfs_trans_ail_cursor_init(ailp, cur);
285 cur->item = __xfs_trans_ail_cursor_last(ailp, lsn);
286 return cur->item;
287}
288
289/*
Dave Chinner16b59022011-07-18 03:40:17 +0000290 * Splice the log item list into the AIL at the given LSN. We splice to the
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000291 * tail of the given LSN to maintain insert order for push traversals. The
292 * cursor is optional, allowing repeated updates to the same LSN to avoid
Alex Eldere44f4112011-07-22 16:04:41 +0000293 * repeated traversals. This should not be called with an empty list.
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000294 */
295static void
296xfs_ail_splice(
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000297 struct xfs_ail *ailp,
298 struct xfs_ail_cursor *cur,
299 struct list_head *list,
300 xfs_lsn_t lsn)
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000301{
Alex Eldere44f4112011-07-22 16:04:41 +0000302 struct xfs_log_item *lip;
303
304 ASSERT(!list_empty(list));
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000305
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000306 /*
Alex Eldere44f4112011-07-22 16:04:41 +0000307 * Use the cursor to determine the insertion point if one is
308 * provided. If not, or if the one we got is not valid,
309 * find the place in the AIL where the items belong.
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000310 */
Alex Eldere44f4112011-07-22 16:04:41 +0000311 lip = cur ? cur->item : NULL;
Christoph Hellwigdb9d67d2015-06-22 09:43:32 +1000312 if (!lip || (uintptr_t)lip & 1)
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000313 lip = __xfs_trans_ail_cursor_last(ailp, lsn);
314
Alex Eldere44f4112011-07-22 16:04:41 +0000315 /*
316 * If a cursor is provided, we know we're processing the AIL
317 * in lsn order, and future items to be spliced in will
318 * follow the last one being inserted now. Update the
319 * cursor to point to that last item, now while we have a
320 * reliable pointer to it.
321 */
322 if (cur)
323 cur->item = list_entry(list->prev, struct xfs_log_item, li_ail);
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000324
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000325 /*
Alex Eldere44f4112011-07-22 16:04:41 +0000326 * Finally perform the splice. Unless the AIL was empty,
327 * lip points to the item in the AIL _after_ which the new
328 * items should go. If lip is null the AIL was empty, so
329 * the new items go at the head of the AIL.
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000330 */
Alex Eldere44f4112011-07-22 16:04:41 +0000331 if (lip)
332 list_splice(list, &lip->li_ail);
333 else
Matthew Wilcox57e80952018-03-07 14:59:39 -0800334 list_splice(list, &ailp->ail_head);
Dave Chinnercd4a3c52011-04-08 12:45:07 +1000335}
336
337/*
338 * Delete the given item from the AIL. Return a pointer to the item.
339 */
340static void
341xfs_ail_delete(
342 struct xfs_ail *ailp,
343 xfs_log_item_t *lip)
344{
345 xfs_ail_check(ailp, lip);
346 list_del(&lip->li_ail);
347 xfs_trans_ail_cursor_clear(ailp, lip);
348}
349
Brian Foster7f4d01f2017-08-08 18:21:52 -0700350static inline uint
351xfsaild_push_item(
352 struct xfs_ail *ailp,
353 struct xfs_log_item *lip)
354{
355 /*
356 * If log item pinning is enabled, skip the push and track the item as
357 * pinned. This can help induce head-behind-tail conditions.
358 */
Matthew Wilcox57e80952018-03-07 14:59:39 -0800359 if (XFS_TEST_ERROR(false, ailp->ail_mount, XFS_ERRTAG_LOG_ITEM_PIN))
Brian Foster7f4d01f2017-08-08 18:21:52 -0700360 return XFS_ITEM_PINNED;
361
Matthew Wilcox57e80952018-03-07 14:59:39 -0800362 return lip->li_ops->iop_push(lip, &ailp->ail_buf_list);
Brian Foster7f4d01f2017-08-08 18:21:52 -0700363}
364
Christoph Hellwig00308072011-10-11 11:14:10 -0400365static long
366xfsaild_push(
367 struct xfs_ail *ailp)
David Chinner249a8c12008-02-05 12:13:32 +1100368{
Matthew Wilcox57e80952018-03-07 14:59:39 -0800369 xfs_mount_t *mp = ailp->ail_mount;
Dave Chinneraf3e4022011-07-18 03:40:18 +0000370 struct xfs_ail_cursor cur;
Dave Chinner9e7004e2011-05-06 02:54:05 +0000371 xfs_log_item_t *lip;
372 xfs_lsn_t lsn;
Dave Chinnerfe0da762011-05-06 02:54:07 +0000373 xfs_lsn_t target;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000374 long tout;
Dave Chinner9e7004e2011-05-06 02:54:05 +0000375 int stuck = 0;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000376 int flushing = 0;
Dave Chinner9e7004e2011-05-06 02:54:05 +0000377 int count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Dave Chinner670ce932011-09-30 04:45:03 +0000379 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000380 * If we encountered pinned items or did not finish writing out all
381 * buffers the last time we ran, force the log first and wait for it
382 * before pushing again.
Dave Chinner670ce932011-09-30 04:45:03 +0000383 */
Matthew Wilcox57e80952018-03-07 14:59:39 -0800384 if (ailp->ail_log_flush && ailp->ail_last_pushed_lsn == 0 &&
385 (!list_empty_careful(&ailp->ail_buf_list) ||
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000386 xfs_ail_min_lsn(ailp))) {
Matthew Wilcox57e80952018-03-07 14:59:39 -0800387 ailp->ail_log_flush = 0;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000388
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100389 XFS_STATS_INC(mp, xs_push_ail_flush);
Dave Chinner670ce932011-09-30 04:45:03 +0000390 xfs_log_force(mp, XFS_LOG_SYNC);
Dave Chinner670ce932011-09-30 04:45:03 +0000391 }
392
Matthew Wilcox57e80952018-03-07 14:59:39 -0800393 spin_lock(&ailp->ail_lock);
Brian Foster8375f922012-06-28 06:52:56 -0400394
Matthew Wilcox57e80952018-03-07 14:59:39 -0800395 /* barrier matches the ail_target update in xfs_ail_push() */
Brian Foster8375f922012-06-28 06:52:56 -0400396 smp_rmb();
Matthew Wilcox57e80952018-03-07 14:59:39 -0800397 target = ailp->ail_target;
398 ailp->ail_target_prev = target;
Brian Foster8375f922012-06-28 06:52:56 -0400399
Matthew Wilcox57e80952018-03-07 14:59:39 -0800400 lip = xfs_trans_ail_cursor_first(ailp, &cur, ailp->ail_last_pushed_lsn);
Christoph Hellwig211e4d42012-04-23 15:58:34 +1000401 if (!lip) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000403 * If the AIL is empty or our push has reached the end we are
404 * done now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 */
Eric Sandeene4a1e292014-04-14 19:06:05 +1000406 xfs_trans_ail_cursor_done(&cur);
Matthew Wilcox57e80952018-03-07 14:59:39 -0800407 spin_unlock(&ailp->ail_lock);
Dave Chinner9e7004e2011-05-06 02:54:05 +0000408 goto out_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
410
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100411 XFS_STATS_INC(mp, xs_push_ail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
David Chinner249a8c12008-02-05 12:13:32 +1100413 lsn = lip->li_lsn;
Dave Chinner50e86682011-05-06 02:54:06 +0000414 while ((XFS_LSN_CMP(lip->li_lsn, target) <= 0)) {
David Chinner249a8c12008-02-05 12:13:32 +1100415 int lock_result;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 /*
Dave Chinner904c17e2013-08-28 21:12:03 +1000418 * Note that iop_push may unlock and reacquire the AIL lock. We
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000419 * rely on the AIL cursor implementation to be able to deal with
420 * the dropped lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 */
Brian Foster7f4d01f2017-08-08 18:21:52 -0700422 lock_result = xfsaild_push_item(ailp, lip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 switch (lock_result) {
David Chinner249a8c12008-02-05 12:13:32 +1100424 case XFS_ITEM_SUCCESS:
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100425 XFS_STATS_INC(mp, xs_push_ail_success);
Christoph Hellwig9e4c1092011-10-11 15:14:11 +0000426 trace_xfs_ail_push(lip);
427
Matthew Wilcox57e80952018-03-07 14:59:39 -0800428 ailp->ail_last_pushed_lsn = lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 break;
430
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000431 case XFS_ITEM_FLUSHING:
432 /*
433 * The item or its backing buffer is already beeing
434 * flushed. The typical reason for that is that an
435 * inode buffer is locked because we already pushed the
436 * updates to it as part of inode clustering.
437 *
438 * We do not want to to stop flushing just because lots
439 * of items are already beeing flushed, but we need to
440 * re-try the flushing relatively soon if most of the
441 * AIL is beeing flushed.
442 */
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100443 XFS_STATS_INC(mp, xs_push_ail_flushing);
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000444 trace_xfs_ail_flushing(lip);
Christoph Hellwig17b38472011-10-11 15:14:09 +0000445
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000446 flushing++;
Matthew Wilcox57e80952018-03-07 14:59:39 -0800447 ailp->ail_last_pushed_lsn = lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 break;
449
David Chinner249a8c12008-02-05 12:13:32 +1100450 case XFS_ITEM_PINNED:
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100451 XFS_STATS_INC(mp, xs_push_ail_pinned);
Christoph Hellwig9e4c1092011-10-11 15:14:11 +0000452 trace_xfs_ail_pinned(lip);
453
David Chinner249a8c12008-02-05 12:13:32 +1100454 stuck++;
Matthew Wilcox57e80952018-03-07 14:59:39 -0800455 ailp->ail_log_flush++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 break;
David Chinner249a8c12008-02-05 12:13:32 +1100457 case XFS_ITEM_LOCKED:
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100458 XFS_STATS_INC(mp, xs_push_ail_locked);
Christoph Hellwig9e4c1092011-10-11 15:14:11 +0000459 trace_xfs_ail_locked(lip);
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000460
David Chinner249a8c12008-02-05 12:13:32 +1100461 stuck++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 break;
David Chinner249a8c12008-02-05 12:13:32 +1100463 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 ASSERT(0);
465 break;
466 }
467
David Chinner249a8c12008-02-05 12:13:32 +1100468 count++;
469
470 /*
471 * Are there too many items we can't do anything with?
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000472 *
David Chinner249a8c12008-02-05 12:13:32 +1100473 * If we we are skipping too many items because we can't flush
474 * them or they are already being flushed, we back off and
475 * given them time to complete whatever operation is being
476 * done. i.e. remove pressure from the AIL while we can't make
477 * progress so traversals don't slow down further inserts and
478 * removals to/from the AIL.
479 *
480 * The value of 100 is an arbitrary magic number based on
481 * observation.
482 */
483 if (stuck > 100)
484 break;
485
Dave Chinneraf3e4022011-07-18 03:40:18 +0000486 lip = xfs_trans_ail_cursor_next(ailp, &cur);
David Chinner249a8c12008-02-05 12:13:32 +1100487 if (lip == NULL)
488 break;
David Chinner249a8c12008-02-05 12:13:32 +1100489 lsn = lip->li_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
Eric Sandeene4a1e292014-04-14 19:06:05 +1000491 xfs_trans_ail_cursor_done(&cur);
Matthew Wilcox57e80952018-03-07 14:59:39 -0800492 spin_unlock(&ailp->ail_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Matthew Wilcox57e80952018-03-07 14:59:39 -0800494 if (xfs_buf_delwri_submit_nowait(&ailp->ail_buf_list))
495 ailp->ail_log_flush++;
Dave Chinnerd808f612010-02-02 10:13:42 +1100496
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000497 if (!count || XFS_LSN_CMP(lsn, target) >= 0) {
Dave Chinner9e7004e2011-05-06 02:54:05 +0000498out_done:
David Chinner92d9cd12008-03-06 13:45:10 +1100499 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000500 * We reached the target or the AIL is empty, so wait a bit
501 * longer for I/O to complete and remove pushed items from the
502 * AIL before we start the next scan from the start of the AIL.
David Chinner92d9cd12008-03-06 13:45:10 +1100503 */
Dave Chinner453eac82010-01-11 11:49:58 +0000504 tout = 50;
Matthew Wilcox57e80952018-03-07 14:59:39 -0800505 ailp->ail_last_pushed_lsn = 0;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000506 } else if (((stuck + flushing) * 100) / count > 90) {
David Chinner249a8c12008-02-05 12:13:32 +1100507 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000508 * Either there is a lot of contention on the AIL or we are
509 * stuck due to operations in progress. "Stuck" in this case
510 * is defined as >90% of the items we tried to push were stuck.
David Chinner249a8c12008-02-05 12:13:32 +1100511 *
512 * Backoff a bit more to allow some I/O to complete before
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000513 * restarting from the start of the AIL. This prevents us from
514 * spinning on the same items, and if they are pinned will all
515 * the restart to issue a log force to unpin the stuck items.
David Chinner249a8c12008-02-05 12:13:32 +1100516 */
Dave Chinner453eac82010-01-11 11:49:58 +0000517 tout = 20;
Matthew Wilcox57e80952018-03-07 14:59:39 -0800518 ailp->ail_last_pushed_lsn = 0;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000519 } else {
520 /*
521 * Assume we have more work to do in a short while.
522 */
523 tout = 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
Dave Chinner0bf6a5b2011-04-08 12:45:07 +1000525
Christoph Hellwig00308072011-10-11 11:14:10 -0400526 return tout;
527}
528
529static int
530xfsaild(
531 void *data)
532{
533 struct xfs_ail *ailp = data;
534 long tout = 0; /* milliseconds */
535
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000536 current->flags |= PF_MEMALLOC;
Michal Hocko18f1df42016-02-08 14:59:07 +1100537 set_freezable();
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000538
Hou Tao0bd89672017-10-17 14:16:28 -0700539 while (1) {
Christoph Hellwig00308072011-10-11 11:14:10 -0400540 if (tout && tout <= 20)
Hou Tao0bd89672017-10-17 14:16:28 -0700541 set_current_state(TASK_KILLABLE);
Christoph Hellwig00308072011-10-11 11:14:10 -0400542 else
Hou Tao0bd89672017-10-17 14:16:28 -0700543 set_current_state(TASK_INTERRUPTIBLE);
544
545 /*
546 * Check kthread_should_stop() after we set the task state
547 * to guarantee that we either see the stop bit and exit or
548 * the task state is reset to runnable such that it's not
549 * scheduled out indefinitely and detects the stop bit at
550 * next iteration.
551 *
552 * A memory barrier is included in above task state set to
553 * serialize again kthread_stop().
554 */
555 if (kthread_should_stop()) {
556 __set_current_state(TASK_RUNNING);
557 break;
558 }
Brian Foster8375f922012-06-28 06:52:56 -0400559
Matthew Wilcox57e80952018-03-07 14:59:39 -0800560 spin_lock(&ailp->ail_lock);
Brian Foster8375f922012-06-28 06:52:56 -0400561
562 /*
563 * Idle if the AIL is empty and we are not racing with a target
564 * update. We check the AIL after we set the task to a sleep
Matthew Wilcox57e80952018-03-07 14:59:39 -0800565 * state to guarantee that we either catch an ail_target update
Brian Foster8375f922012-06-28 06:52:56 -0400566 * or that a wake_up resets the state to TASK_RUNNING.
567 * Otherwise, we run the risk of sleeping indefinitely.
568 *
Matthew Wilcox57e80952018-03-07 14:59:39 -0800569 * The barrier matches the ail_target update in xfs_ail_push().
Brian Foster8375f922012-06-28 06:52:56 -0400570 */
571 smp_rmb();
572 if (!xfs_ail_min(ailp) &&
Matthew Wilcox57e80952018-03-07 14:59:39 -0800573 ailp->ail_target == ailp->ail_target_prev) {
574 spin_unlock(&ailp->ail_lock);
Michal Hocko18f1df42016-02-08 14:59:07 +1100575 freezable_schedule();
Brian Foster8375f922012-06-28 06:52:56 -0400576 tout = 0;
577 continue;
578 }
Matthew Wilcox57e80952018-03-07 14:59:39 -0800579 spin_unlock(&ailp->ail_lock);
Brian Foster8375f922012-06-28 06:52:56 -0400580
581 if (tout)
Michal Hocko18f1df42016-02-08 14:59:07 +1100582 freezable_schedule_timeout(msecs_to_jiffies(tout));
Brian Foster8375f922012-06-28 06:52:56 -0400583
584 __set_current_state(TASK_RUNNING);
Christoph Hellwig00308072011-10-11 11:14:10 -0400585
586 try_to_freeze();
587
588 tout = xfsaild_push(ailp);
589 }
590
591 return 0;
Dave Chinner453eac82010-01-11 11:49:58 +0000592}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Dave Chinner0bf6a5b2011-04-08 12:45:07 +1000594/*
595 * This routine is called to move the tail of the AIL forward. It does this by
596 * trying to flush items in the AIL whose lsns are below the given
597 * threshold_lsn.
598 *
599 * The push is run asynchronously in a workqueue, which means the caller needs
600 * to handle waiting on the async flush for space to become available.
601 * We don't want to interrupt any push that is in progress, hence we only queue
602 * work if we set the pushing bit approriately.
603 *
604 * We do this unlocked - we only need to know whether there is anything in the
605 * AIL at the time we are called. We don't need to access the contents of
606 * any of the objects, so the lock is not needed.
607 */
608void
Dave Chinnerfd074842011-04-08 12:45:07 +1000609xfs_ail_push(
Dave Chinner0bf6a5b2011-04-08 12:45:07 +1000610 struct xfs_ail *ailp,
611 xfs_lsn_t threshold_lsn)
612{
613 xfs_log_item_t *lip;
614
615 lip = xfs_ail_min(ailp);
Matthew Wilcox57e80952018-03-07 14:59:39 -0800616 if (!lip || XFS_FORCED_SHUTDOWN(ailp->ail_mount) ||
617 XFS_LSN_CMP(threshold_lsn, ailp->ail_target) <= 0)
Dave Chinner0bf6a5b2011-04-08 12:45:07 +1000618 return;
619
620 /*
621 * Ensure that the new target is noticed in push code before it clears
622 * the XFS_AIL_PUSHING_BIT.
623 */
624 smp_wmb();
Matthew Wilcox57e80952018-03-07 14:59:39 -0800625 xfs_trans_ail_copy_lsn(ailp, &ailp->ail_target, &threshold_lsn);
Christoph Hellwig00308072011-10-11 11:14:10 -0400626 smp_wmb();
627
Matthew Wilcox57e80952018-03-07 14:59:39 -0800628 wake_up_process(ailp->ail_task);
Dave Chinner0bf6a5b2011-04-08 12:45:07 +1000629}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631/*
Dave Chinnerfd074842011-04-08 12:45:07 +1000632 * Push out all items in the AIL immediately
633 */
634void
635xfs_ail_push_all(
636 struct xfs_ail *ailp)
637{
638 xfs_lsn_t threshold_lsn = xfs_ail_max_lsn(ailp);
639
640 if (threshold_lsn)
641 xfs_ail_push(ailp, threshold_lsn);
642}
643
644/*
Christoph Hellwig211e4d42012-04-23 15:58:34 +1000645 * Push out all items in the AIL immediately and wait until the AIL is empty.
646 */
647void
648xfs_ail_push_all_sync(
649 struct xfs_ail *ailp)
650{
651 struct xfs_log_item *lip;
652 DEFINE_WAIT(wait);
653
Matthew Wilcox57e80952018-03-07 14:59:39 -0800654 spin_lock(&ailp->ail_lock);
Christoph Hellwig211e4d42012-04-23 15:58:34 +1000655 while ((lip = xfs_ail_max(ailp)) != NULL) {
Matthew Wilcox57e80952018-03-07 14:59:39 -0800656 prepare_to_wait(&ailp->ail_empty, &wait, TASK_UNINTERRUPTIBLE);
657 ailp->ail_target = lip->li_lsn;
658 wake_up_process(ailp->ail_task);
659 spin_unlock(&ailp->ail_lock);
Christoph Hellwig211e4d42012-04-23 15:58:34 +1000660 schedule();
Matthew Wilcox57e80952018-03-07 14:59:39 -0800661 spin_lock(&ailp->ail_lock);
Christoph Hellwig211e4d42012-04-23 15:58:34 +1000662 }
Matthew Wilcox57e80952018-03-07 14:59:39 -0800663 spin_unlock(&ailp->ail_lock);
Christoph Hellwig211e4d42012-04-23 15:58:34 +1000664
Matthew Wilcox57e80952018-03-07 14:59:39 -0800665 finish_wait(&ailp->ail_empty, &wait);
Christoph Hellwig211e4d42012-04-23 15:58:34 +1000666}
667
668/*
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100669 * xfs_trans_ail_update - bulk AIL insertion operation.
670 *
671 * @xfs_trans_ail_update takes an array of log items that all need to be
672 * positioned at the same LSN in the AIL. If an item is not in the AIL, it will
673 * be added. Otherwise, it will be repositioned by removing it and re-adding
674 * it to the AIL. If we move the first item in the AIL, update the log tail to
675 * match the new minimum LSN in the AIL.
676 *
677 * This function takes the AIL lock once to execute the update operations on
678 * all the items in the array, and as such should not be called with the AIL
679 * lock held. As a result, once we have the AIL lock, we need to check each log
680 * item LSN to confirm it needs to be moved forward in the AIL.
681 *
682 * To optimise the insert operation, we delete all the items from the AIL in
683 * the first pass, moving them into a temporary list, then splice the temporary
684 * list into the correct position in the AIL. This avoids needing to do an
685 * insert operation on every item.
686 *
687 * This function must be called with the AIL lock held. The lock is dropped
688 * before returning.
689 */
690void
691xfs_trans_ail_update_bulk(
692 struct xfs_ail *ailp,
Dave Chinner1d8c95a2011-07-18 03:40:16 +0000693 struct xfs_ail_cursor *cur,
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100694 struct xfs_log_item **log_items,
695 int nr_items,
Matthew Wilcox57e80952018-03-07 14:59:39 -0800696 xfs_lsn_t lsn) __releases(ailp->ail_lock)
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100697{
698 xfs_log_item_t *mlip;
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100699 int mlip_changed = 0;
700 int i;
701 LIST_HEAD(tmp);
702
Alex Eldere44f4112011-07-22 16:04:41 +0000703 ASSERT(nr_items > 0); /* Not required, but true. */
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100704 mlip = xfs_ail_min(ailp);
705
706 for (i = 0; i < nr_items; i++) {
707 struct xfs_log_item *lip = log_items[i];
Dave Chinner22525c12018-05-09 07:47:34 -0700708 if (test_and_set_bit(XFS_LI_IN_AIL, &lip->li_flags)) {
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100709 /* check if we really need to move the item */
710 if (XFS_LSN_CMP(lsn, lip->li_lsn) <= 0)
711 continue;
712
Dave Chinner750b9c92013-11-01 15:27:18 +1100713 trace_xfs_ail_move(lip, lip->li_lsn, lsn);
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100714 xfs_ail_delete(ailp, lip);
715 if (mlip == lip)
716 mlip_changed = 1;
717 } else {
Dave Chinner750b9c92013-11-01 15:27:18 +1100718 trace_xfs_ail_insert(lip, 0, lsn);
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100719 }
720 lip->li_lsn = lsn;
721 list_add(&lip->li_ail, &tmp);
722 }
723
Alex Eldere44f4112011-07-22 16:04:41 +0000724 if (!list_empty(&tmp))
725 xfs_ail_splice(ailp, cur, &tmp, lsn);
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000726
Christoph Hellwig1c304622012-04-23 15:58:33 +1000727 if (mlip_changed) {
Matthew Wilcox57e80952018-03-07 14:59:39 -0800728 if (!XFS_FORCED_SHUTDOWN(ailp->ail_mount))
729 xlog_assign_tail_lsn_locked(ailp->ail_mount);
730 spin_unlock(&ailp->ail_lock);
Christoph Hellwig1c304622012-04-23 15:58:33 +1000731
Matthew Wilcox57e80952018-03-07 14:59:39 -0800732 xfs_log_space_wake(ailp->ail_mount);
Christoph Hellwig1c304622012-04-23 15:58:33 +1000733 } else {
Matthew Wilcox57e80952018-03-07 14:59:39 -0800734 spin_unlock(&ailp->ail_lock);
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000735 }
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100736}
737
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700738bool
739xfs_ail_delete_one(
740 struct xfs_ail *ailp,
Carlos Maiolinod3a304b2017-08-08 18:21:50 -0700741 struct xfs_log_item *lip)
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700742{
743 struct xfs_log_item *mlip = xfs_ail_min(ailp);
744
745 trace_xfs_ail_delete(lip, mlip->li_lsn, lip->li_lsn);
746 xfs_ail_delete(ailp, lip);
Carlos Maiolinod3a304b2017-08-08 18:21:50 -0700747 xfs_clear_li_failed(lip);
Dave Chinner22525c12018-05-09 07:47:34 -0700748 clear_bit(XFS_LI_IN_AIL, &lip->li_flags);
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700749 lip->li_lsn = 0;
750
751 return mlip == lip;
752}
753
754/**
755 * Remove a log items from the AIL
Dave Chinner30136832010-12-20 12:03:17 +1100756 *
757 * @xfs_trans_ail_delete_bulk takes an array of log items that all need to
758 * removed from the AIL. The caller is already holding the AIL lock, and done
759 * all the checks necessary to ensure the items passed in via @log_items are
760 * ready for deletion. This includes checking that the items are in the AIL.
761 *
762 * For each log item to be removed, unlink it from the AIL, clear the IN_AIL
763 * flag from the item and reset the item's lsn to 0. If we remove the first
764 * item in the AIL, update the log tail to match the new minimum LSN in the
765 * AIL.
766 *
767 * This function will not drop the AIL lock until all items are removed from
768 * the AIL to minimise the amount of lock traffic on the AIL. This does not
769 * greatly increase the AIL hold time, but does significantly reduce the amount
770 * of traffic on the lock, especially during IO completion.
771 *
772 * This function must be called with the AIL lock held. The lock is dropped
773 * before returning.
774 */
775void
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700776xfs_trans_ail_delete(
Dave Chinner30136832010-12-20 12:03:17 +1100777 struct xfs_ail *ailp,
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700778 struct xfs_log_item *lip,
Matthew Wilcox57e80952018-03-07 14:59:39 -0800779 int shutdown_type) __releases(ailp->ail_lock)
Dave Chinner30136832010-12-20 12:03:17 +1100780{
Matthew Wilcox57e80952018-03-07 14:59:39 -0800781 struct xfs_mount *mp = ailp->ail_mount;
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700782 bool mlip_changed;
Dave Chinner30136832010-12-20 12:03:17 +1100783
Dave Chinner22525c12018-05-09 07:47:34 -0700784 if (!test_bit(XFS_LI_IN_AIL, &lip->li_flags)) {
Matthew Wilcox57e80952018-03-07 14:59:39 -0800785 spin_unlock(&ailp->ail_lock);
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700786 if (!XFS_FORCED_SHUTDOWN(mp)) {
787 xfs_alert_tag(mp, XFS_PTAG_AILDELETE,
788 "%s: attempting to delete a log item that is not in the AIL",
789 __func__);
790 xfs_force_shutdown(mp, shutdown_type);
Dave Chinner30136832010-12-20 12:03:17 +1100791 }
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700792 return;
Dave Chinner30136832010-12-20 12:03:17 +1100793 }
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000794
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700795 mlip_changed = xfs_ail_delete_one(ailp, lip);
Christoph Hellwig1c304622012-04-23 15:58:33 +1000796 if (mlip_changed) {
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700797 if (!XFS_FORCED_SHUTDOWN(mp))
798 xlog_assign_tail_lsn_locked(mp);
Matthew Wilcox57e80952018-03-07 14:59:39 -0800799 if (list_empty(&ailp->ail_head))
800 wake_up_all(&ailp->ail_empty);
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000801 }
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700802
Matthew Wilcox57e80952018-03-07 14:59:39 -0800803 spin_unlock(&ailp->ail_lock);
Christoph Hellwig27af1bb2017-04-21 11:24:42 -0700804 if (mlip_changed)
Matthew Wilcox57e80952018-03-07 14:59:39 -0800805 xfs_log_space_wake(ailp->ail_mount);
Dave Chinner30136832010-12-20 12:03:17 +1100806}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
David Chinner249a8c12008-02-05 12:13:32 +1100808int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809xfs_trans_ail_init(
810 xfs_mount_t *mp)
811{
David Chinner82fa9012008-10-30 17:38:26 +1100812 struct xfs_ail *ailp;
813
814 ailp = kmem_zalloc(sizeof(struct xfs_ail), KM_MAYFAIL);
815 if (!ailp)
Dave Chinner24513372014-06-25 14:58:08 +1000816 return -ENOMEM;
David Chinner82fa9012008-10-30 17:38:26 +1100817
Matthew Wilcox57e80952018-03-07 14:59:39 -0800818 ailp->ail_mount = mp;
819 INIT_LIST_HEAD(&ailp->ail_head);
820 INIT_LIST_HEAD(&ailp->ail_cursors);
821 spin_lock_init(&ailp->ail_lock);
822 INIT_LIST_HEAD(&ailp->ail_buf_list);
823 init_waitqueue_head(&ailp->ail_empty);
Christoph Hellwig00308072011-10-11 11:14:10 -0400824
Matthew Wilcox57e80952018-03-07 14:59:39 -0800825 ailp->ail_task = kthread_run(xfsaild, ailp, "xfsaild/%s",
826 ailp->ail_mount->m_fsname);
827 if (IS_ERR(ailp->ail_task))
Christoph Hellwig00308072011-10-11 11:14:10 -0400828 goto out_free_ailp;
829
David Chinner27d8d5f2008-10-30 17:38:39 +1100830 mp->m_ail = ailp;
831 return 0;
Christoph Hellwig00308072011-10-11 11:14:10 -0400832
833out_free_ailp:
834 kmem_free(ailp);
Dave Chinner24513372014-06-25 14:58:08 +1000835 return -ENOMEM;
David Chinner249a8c12008-02-05 12:13:32 +1100836}
837
838void
839xfs_trans_ail_destroy(
840 xfs_mount_t *mp)
841{
David Chinner82fa9012008-10-30 17:38:26 +1100842 struct xfs_ail *ailp = mp->m_ail;
843
Matthew Wilcox57e80952018-03-07 14:59:39 -0800844 kthread_stop(ailp->ail_task);
David Chinner82fa9012008-10-30 17:38:26 +1100845 kmem_free(ailp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}