]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - fs/xfs/xfs_trans_ail.c
xfs: use offsetof() in place of offset macros for __xfsstats
[mirror_ubuntu-hirsute-kernel.git] / fs / xfs / xfs_trans_ail.c
CommitLineData
0b61f8a4 1// SPDX-License-Identifier: GPL-2.0
1da177e4 2/*
7b718769 3 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
c7e8f268 4 * Copyright (c) 2008 Dave Chinner
7b718769 5 * All Rights Reserved.
1da177e4 6 */
1da177e4 7#include "xfs.h"
a844f451 8#include "xfs_fs.h"
4fb6e8ad 9#include "xfs_format.h"
239880ef
DC
10#include "xfs_log_format.h"
11#include "xfs_trans_resv.h"
1da177e4 12#include "xfs_mount.h"
239880ef 13#include "xfs_trans.h"
1da177e4 14#include "xfs_trans_priv.h"
9e4c109a 15#include "xfs_trace.h"
e9e899a2 16#include "xfs_errortag.h"
1da177e4 17#include "xfs_error.h"
239880ef 18#include "xfs_log.h"
1da177e4 19
1da177e4 20#ifdef DEBUG
cd4a3c50
DC
21/*
22 * Check that the list is sorted as it should be.
d686d12d
DC
23 *
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.
cd4a3c50
DC
29 */
30STATIC void
31xfs_ail_check(
d686d12d
DC
32 struct xfs_ail *ailp,
33 struct xfs_log_item *lip)
cd4a3c50 34{
d686d12d
DC
35 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
cd4a3c50 42
57e80956 43 if (list_empty(&ailp->ail_head))
cd4a3c50
DC
44 return;
45
46 /*
d686d12d 47 * Sample then check the next and previous entries are valid.
cd4a3c50 48 */
d686d12d
DC
49 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);
57e80956 51 if (&prev_lip->li_ail != &ailp->ail_head)
d686d12d
DC
52 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;
cd4a3c50 57
d686d12d
DC
58 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;
cd4a3c50 62
d686d12d
DC
63 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);
cd4a3c50
DC
68}
69#else /* !DEBUG */
de08dbc1 70#define xfs_ail_check(a,l)
1da177e4
LT
71#endif /* DEBUG */
72
cd4a3c50 73/*
fd074841
DC
74 * 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{
57e80956 81 if (list_empty(&ailp->ail_head))
fd074841
DC
82 return NULL;
83
57e80956 84 return list_entry(ailp->ail_head.prev, xfs_log_item_t, li_ail);
fd074841
DC
85}
86
cd4a3c50
DC
87/*
88 * 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{
57e80956 96 if (lip->li_ail.next == &ailp->ail_head)
cd4a3c50
DC
97 return NULL;
98
99 return list_first_entry(&lip->li_ail, xfs_log_item_t, li_ail);
100}
1da177e4
LT
101
102/*
cd4a3c50
DC
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.
1da177e4 106 *
cd4a3c50
DC
107 * We need the AIL lock in order to get a coherent read of the lsn of the last
108 * item in the AIL.
1da177e4
LT
109 */
110xfs_lsn_t
fd074841 111xfs_ail_min_lsn(
5b00f14f 112 struct xfs_ail *ailp)
1da177e4 113{
cd4a3c50 114 xfs_lsn_t lsn = 0;
1da177e4 115 xfs_log_item_t *lip;
1da177e4 116
57e80956 117 spin_lock(&ailp->ail_lock);
5b00f14f 118 lip = xfs_ail_min(ailp);
cd4a3c50 119 if (lip)
1da177e4 120 lsn = lip->li_lsn;
57e80956 121 spin_unlock(&ailp->ail_lock);
1da177e4
LT
122
123 return lsn;
124}
125
fd074841
DC
126/*
127 * 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
57e80956 136 spin_lock(&ailp->ail_lock);
fd074841
DC
137 lip = xfs_ail_max(ailp);
138 if (lip)
139 lsn = lip->li_lsn;
57e80956 140 spin_unlock(&ailp->ail_lock);
fd074841
DC
141
142 return lsn;
143}
144
27d8d5fe 145/*
af3e4022
DC
146 * 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.
27d8d5fe 151 */
5b00f14f 152STATIC void
27d8d5fe
DC
153xfs_trans_ail_cursor_init(
154 struct xfs_ail *ailp,
155 struct xfs_ail_cursor *cur)
156{
157 cur->item = NULL;
57e80956 158 list_add_tail(&cur->list, &ailp->ail_cursors);
27d8d5fe
DC
159}
160
27d8d5fe 161/*
af3e4022
DC
162 * 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.
27d8d5fe 164 */
5b00f14f 165struct xfs_log_item *
27d8d5fe
DC
166xfs_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
db9d67d6 172 if ((uintptr_t)lip & 1)
27d8d5fe 173 lip = xfs_ail_min(ailp);
16b59029
DC
174 if (lip)
175 cur->item = xfs_ail_next(ailp, lip);
27d8d5fe
DC
176 return lip;
177}
178
27d8d5fe 179/*
af3e4022
DC
180 * When the traversal is complete, we need to remove the cursor from the list
181 * of traversing cursors.
27d8d5fe
DC
182 */
183void
184xfs_trans_ail_cursor_done(
af3e4022 185 struct xfs_ail_cursor *cur)
27d8d5fe 186{
af3e4022
DC
187 cur->item = NULL;
188 list_del_init(&cur->list);
27d8d5fe
DC
189}
190
5b00f14f 191/*
af3e4022
DC
192 * 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.
5b00f14f
DC
198 */
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
57e80956 206 list_for_each_entry(cur, &ailp->ail_cursors, list) {
5b00f14f
DC
207 if (cur->item == lip)
208 cur->item = (struct xfs_log_item *)
db9d67d6 209 ((uintptr_t)cur->item | 1);
5b00f14f
DC
210 }
211}
212
249a8c11 213/*
16b59029
DC
214 * 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.
249a8c11 218 */
5b00f14f
DC
219xfs_log_item_t *
220xfs_trans_ail_cursor_first(
27d8d5fe
DC
221 struct xfs_ail *ailp,
222 struct xfs_ail_cursor *cur,
223 xfs_lsn_t lsn)
249a8c11 224{
27d8d5fe 225 xfs_log_item_t *lip;
249a8c11 226
5b00f14f 227 xfs_trans_ail_cursor_init(ailp, cur);
16b59029
DC
228
229 if (lsn == 0) {
230 lip = xfs_ail_min(ailp);
5b00f14f 231 goto out;
16b59029 232 }
249a8c11 233
57e80956 234 list_for_each_entry(lip, &ailp->ail_head, li_ail) {
5b00f14f 235 if (XFS_LSN_CMP(lip->li_lsn, lsn) >= 0)
7ee49acf 236 goto out;
535f6b37 237 }
16b59029
DC
238 return NULL;
239
5b00f14f 240out:
16b59029
DC
241 if (lip)
242 cur->item = xfs_ail_next(ailp, lip);
5b00f14f 243 return lip;
249a8c11
DC
244}
245
1d8c95a3
DC
246static 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
57e80956 253 list_for_each_entry_reverse(lip, &ailp->ail_head, li_ail) {
1d8c95a3
DC
254 if (XFS_LSN_CMP(lip->li_lsn, lsn) <= 0)
255 return lip;
256 }
257 return NULL;
258}
259
260/*
16b59029
DC
261 * 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.
1d8c95a3
DC
265 */
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/*
16b59029 278 * Splice the log item list into the AIL at the given LSN. We splice to the
1d8c95a3
DC
279 * 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
e44f4112 281 * repeated traversals. This should not be called with an empty list.
cd4a3c50
DC
282 */
283static void
284xfs_ail_splice(
1d8c95a3
DC
285 struct xfs_ail *ailp,
286 struct xfs_ail_cursor *cur,
287 struct list_head *list,
288 xfs_lsn_t lsn)
cd4a3c50 289{
e44f4112
AE
290 struct xfs_log_item *lip;
291
292 ASSERT(!list_empty(list));
cd4a3c50 293
1d8c95a3 294 /*
e44f4112
AE
295 * 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.
1d8c95a3 298 */
e44f4112 299 lip = cur ? cur->item : NULL;
db9d67d6 300 if (!lip || (uintptr_t)lip & 1)
1d8c95a3
DC
301 lip = __xfs_trans_ail_cursor_last(ailp, lsn);
302
e44f4112
AE
303 /*
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);
cd4a3c50 312
1d8c95a3 313 /*
e44f4112
AE
314 * 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.
1d8c95a3 318 */
e44f4112
AE
319 if (lip)
320 list_splice(list, &lip->li_ail);
321 else
57e80956 322 list_splice(list, &ailp->ail_head);
cd4a3c50
DC
323}
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
7f4d01f3
BF
338static 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 */
57e80956 347 if (XFS_TEST_ERROR(false, ailp->ail_mount, XFS_ERRTAG_LOG_ITEM_PIN))
7f4d01f3
BF
348 return XFS_ITEM_PINNED;
349
57e80956 350 return lip->li_ops->iop_push(lip, &ailp->ail_buf_list);
7f4d01f3
BF
351}
352
0030807c
CH
353static long
354xfsaild_push(
355 struct xfs_ail *ailp)
249a8c11 356{
57e80956 357 xfs_mount_t *mp = ailp->ail_mount;
af3e4022 358 struct xfs_ail_cursor cur;
9e7004e7
DC
359 xfs_log_item_t *lip;
360 xfs_lsn_t lsn;
fe0da767 361 xfs_lsn_t target;
43ff2122 362 long tout;
9e7004e7 363 int stuck = 0;
43ff2122 364 int flushing = 0;
9e7004e7 365 int count = 0;
1da177e4 366
670ce93f 367 /*
43ff2122
CH
368 * 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.
670ce93f 371 */
57e80956
MW
372 if (ailp->ail_log_flush && ailp->ail_last_pushed_lsn == 0 &&
373 (!list_empty_careful(&ailp->ail_buf_list) ||
43ff2122 374 xfs_ail_min_lsn(ailp))) {
57e80956 375 ailp->ail_log_flush = 0;
43ff2122 376
ff6d6af2 377 XFS_STATS_INC(mp, xs_push_ail_flush);
670ce93f 378 xfs_log_force(mp, XFS_LOG_SYNC);
670ce93f
DC
379 }
380
57e80956 381 spin_lock(&ailp->ail_lock);
8375f922 382
57e80956 383 /* barrier matches the ail_target update in xfs_ail_push() */
8375f922 384 smp_rmb();
57e80956
MW
385 target = ailp->ail_target;
386 ailp->ail_target_prev = target;
8375f922 387
57e80956 388 lip = xfs_trans_ail_cursor_first(ailp, &cur, ailp->ail_last_pushed_lsn);
211e4d43 389 if (!lip) {
1da177e4 390 /*
43ff2122
CH
391 * If the AIL is empty or our push has reached the end we are
392 * done now.
1da177e4 393 */
e4a1e29c 394 xfs_trans_ail_cursor_done(&cur);
57e80956 395 spin_unlock(&ailp->ail_lock);
9e7004e7 396 goto out_done;
1da177e4
LT
397 }
398
ff6d6af2 399 XFS_STATS_INC(mp, xs_push_ail);
1da177e4 400
249a8c11 401 lsn = lip->li_lsn;
50e86686 402 while ((XFS_LSN_CMP(lip->li_lsn, target) <= 0)) {
249a8c11 403 int lock_result;
43ff2122 404
1da177e4 405 /*
904c17e6 406 * Note that iop_push may unlock and reacquire the AIL lock. We
43ff2122
CH
407 * rely on the AIL cursor implementation to be able to deal with
408 * the dropped lock.
1da177e4 409 */
7f4d01f3 410 lock_result = xfsaild_push_item(ailp, lip);
1da177e4 411 switch (lock_result) {
249a8c11 412 case XFS_ITEM_SUCCESS:
ff6d6af2 413 XFS_STATS_INC(mp, xs_push_ail_success);
9e4c109a
CH
414 trace_xfs_ail_push(lip);
415
57e80956 416 ailp->ail_last_pushed_lsn = lsn;
1da177e4
LT
417 break;
418
43ff2122
CH
419 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 */
ff6d6af2 431 XFS_STATS_INC(mp, xs_push_ail_flushing);
43ff2122
CH
432 trace_xfs_ail_flushing(lip);
433
434 flushing++;
57e80956 435 ailp->ail_last_pushed_lsn = lsn;
1da177e4
LT
436 break;
437
249a8c11 438 case XFS_ITEM_PINNED:
ff6d6af2 439 XFS_STATS_INC(mp, xs_push_ail_pinned);
9e4c109a
CH
440 trace_xfs_ail_pinned(lip);
441
249a8c11 442 stuck++;
57e80956 443 ailp->ail_log_flush++;
1da177e4 444 break;
249a8c11 445 case XFS_ITEM_LOCKED:
ff6d6af2 446 XFS_STATS_INC(mp, xs_push_ail_locked);
9e4c109a 447 trace_xfs_ail_locked(lip);
43ff2122 448
249a8c11 449 stuck++;
1da177e4 450 break;
249a8c11 451 default:
1da177e4
LT
452 ASSERT(0);
453 break;
454 }
455
249a8c11 456 count++;
1da177e4 457
249a8c11
DC
458 /*
459 * Are there too many items we can't do anything with?
43ff2122 460 *
249a8c11
DC
461 * 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
af3e4022 474 lip = xfs_trans_ail_cursor_next(ailp, &cur);
249a8c11
DC
475 if (lip == NULL)
476 break;
249a8c11 477 lsn = lip->li_lsn;
1da177e4 478 }
e4a1e29c 479 xfs_trans_ail_cursor_done(&cur);
57e80956 480 spin_unlock(&ailp->ail_lock);
1da177e4 481
57e80956
MW
482 if (xfs_buf_delwri_submit_nowait(&ailp->ail_buf_list))
483 ailp->ail_log_flush++;
d808f617 484
43ff2122 485 if (!count || XFS_LSN_CMP(lsn, target) >= 0) {
9e7004e7 486out_done:
92d9cd10 487 /*
43ff2122
CH
488 * 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.
92d9cd10 491 */
453eac8a 492 tout = 50;
57e80956 493 ailp->ail_last_pushed_lsn = 0;
43ff2122 494 } else if (((stuck + flushing) * 100) / count > 90) {
249a8c11 495 /*
43ff2122
CH
496 * 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.
249a8c11
DC
499 *
500 * Backoff a bit more to allow some I/O to complete before
43ff2122
CH
501 * 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.
249a8c11 504 */
453eac8a 505 tout = 20;
57e80956 506 ailp->ail_last_pushed_lsn = 0;
43ff2122
CH
507 } else {
508 /*
509 * Assume we have more work to do in a short while.
510 */
511 tout = 10;
1da177e4 512 }
0bf6a5bd 513
0030807c
CH
514 return tout;
515}
516
517static int
518xfsaild(
519 void *data)
520{
521 struct xfs_ail *ailp = data;
522 long tout = 0; /* milliseconds */
523
43ff2122 524 current->flags |= PF_MEMALLOC;
18f1df4e 525 set_freezable();
43ff2122 526
0bd89676 527 while (1) {
0030807c 528 if (tout && tout <= 20)
0bd89676 529 set_current_state(TASK_KILLABLE);
0030807c 530 else
0bd89676
HT
531 set_current_state(TASK_INTERRUPTIBLE);
532
533 /*
534 * Check kthread_should_stop() after we set the task state
535 * to guarantee that we either see the stop bit and exit or
536 * the task state is reset to runnable such that it's not
537 * scheduled out indefinitely and detects the stop bit at
538 * next iteration.
539 *
540 * A memory barrier is included in above task state set to
541 * serialize again kthread_stop().
542 */
543 if (kthread_should_stop()) {
544 __set_current_state(TASK_RUNNING);
545 break;
546 }
8375f922 547
57e80956 548 spin_lock(&ailp->ail_lock);
8375f922
BF
549
550 /*
551 * Idle if the AIL is empty and we are not racing with a target
552 * update. We check the AIL after we set the task to a sleep
57e80956 553 * state to guarantee that we either catch an ail_target update
8375f922
BF
554 * or that a wake_up resets the state to TASK_RUNNING.
555 * Otherwise, we run the risk of sleeping indefinitely.
556 *
57e80956 557 * The barrier matches the ail_target update in xfs_ail_push().
8375f922
BF
558 */
559 smp_rmb();
560 if (!xfs_ail_min(ailp) &&
57e80956
MW
561 ailp->ail_target == ailp->ail_target_prev) {
562 spin_unlock(&ailp->ail_lock);
18f1df4e 563 freezable_schedule();
8375f922
BF
564 tout = 0;
565 continue;
566 }
57e80956 567 spin_unlock(&ailp->ail_lock);
8375f922
BF
568
569 if (tout)
18f1df4e 570 freezable_schedule_timeout(msecs_to_jiffies(tout));
8375f922
BF
571
572 __set_current_state(TASK_RUNNING);
0030807c
CH
573
574 try_to_freeze();
575
576 tout = xfsaild_push(ailp);
577 }
578
579 return 0;
453eac8a 580}
1da177e4 581
0bf6a5bd
DC
582/*
583 * This routine is called to move the tail of the AIL forward. It does this by
584 * trying to flush items in the AIL whose lsns are below the given
585 * threshold_lsn.
586 *
587 * The push is run asynchronously in a workqueue, which means the caller needs
588 * to handle waiting on the async flush for space to become available.
589 * We don't want to interrupt any push that is in progress, hence we only queue
590 * work if we set the pushing bit approriately.
591 *
592 * We do this unlocked - we only need to know whether there is anything in the
593 * AIL at the time we are called. We don't need to access the contents of
594 * any of the objects, so the lock is not needed.
595 */
596void
fd074841 597xfs_ail_push(
0bf6a5bd
DC
598 struct xfs_ail *ailp,
599 xfs_lsn_t threshold_lsn)
600{
601 xfs_log_item_t *lip;
602
603 lip = xfs_ail_min(ailp);
57e80956
MW
604 if (!lip || XFS_FORCED_SHUTDOWN(ailp->ail_mount) ||
605 XFS_LSN_CMP(threshold_lsn, ailp->ail_target) <= 0)
0bf6a5bd
DC
606 return;
607
608 /*
609 * Ensure that the new target is noticed in push code before it clears
610 * the XFS_AIL_PUSHING_BIT.
611 */
612 smp_wmb();
57e80956 613 xfs_trans_ail_copy_lsn(ailp, &ailp->ail_target, &threshold_lsn);
0030807c
CH
614 smp_wmb();
615
57e80956 616 wake_up_process(ailp->ail_task);
0bf6a5bd 617}
1da177e4 618
fd074841
DC
619/*
620 * Push out all items in the AIL immediately
621 */
622void
623xfs_ail_push_all(
624 struct xfs_ail *ailp)
625{
626 xfs_lsn_t threshold_lsn = xfs_ail_max_lsn(ailp);
627
628 if (threshold_lsn)
629 xfs_ail_push(ailp, threshold_lsn);
630}
631
211e4d43
CH
632/*
633 * Push out all items in the AIL immediately and wait until the AIL is empty.
634 */
635void
636xfs_ail_push_all_sync(
637 struct xfs_ail *ailp)
638{
639 struct xfs_log_item *lip;
640 DEFINE_WAIT(wait);
641
57e80956 642 spin_lock(&ailp->ail_lock);
211e4d43 643 while ((lip = xfs_ail_max(ailp)) != NULL) {
57e80956
MW
644 prepare_to_wait(&ailp->ail_empty, &wait, TASK_UNINTERRUPTIBLE);
645 ailp->ail_target = lip->li_lsn;
646 wake_up_process(ailp->ail_task);
647 spin_unlock(&ailp->ail_lock);
211e4d43 648 schedule();
57e80956 649 spin_lock(&ailp->ail_lock);
211e4d43 650 }
57e80956 651 spin_unlock(&ailp->ail_lock);
211e4d43 652
57e80956 653 finish_wait(&ailp->ail_empty, &wait);
211e4d43
CH
654}
655
0e57f6a3
DC
656/*
657 * xfs_trans_ail_update - bulk AIL insertion operation.
658 *
659 * @xfs_trans_ail_update takes an array of log items that all need to be
660 * positioned at the same LSN in the AIL. If an item is not in the AIL, it will
661 * be added. Otherwise, it will be repositioned by removing it and re-adding
662 * it to the AIL. If we move the first item in the AIL, update the log tail to
663 * match the new minimum LSN in the AIL.
664 *
665 * This function takes the AIL lock once to execute the update operations on
666 * all the items in the array, and as such should not be called with the AIL
667 * lock held. As a result, once we have the AIL lock, we need to check each log
668 * item LSN to confirm it needs to be moved forward in the AIL.
669 *
670 * To optimise the insert operation, we delete all the items from the AIL in
671 * the first pass, moving them into a temporary list, then splice the temporary
672 * list into the correct position in the AIL. This avoids needing to do an
673 * insert operation on every item.
674 *
675 * This function must be called with the AIL lock held. The lock is dropped
676 * before returning.
677 */
678void
679xfs_trans_ail_update_bulk(
680 struct xfs_ail *ailp,
1d8c95a3 681 struct xfs_ail_cursor *cur,
0e57f6a3
DC
682 struct xfs_log_item **log_items,
683 int nr_items,
57e80956 684 xfs_lsn_t lsn) __releases(ailp->ail_lock)
0e57f6a3
DC
685{
686 xfs_log_item_t *mlip;
0e57f6a3
DC
687 int mlip_changed = 0;
688 int i;
689 LIST_HEAD(tmp);
690
e44f4112 691 ASSERT(nr_items > 0); /* Not required, but true. */
0e57f6a3
DC
692 mlip = xfs_ail_min(ailp);
693
694 for (i = 0; i < nr_items; i++) {
695 struct xfs_log_item *lip = log_items[i];
22525c17 696 if (test_and_set_bit(XFS_LI_IN_AIL, &lip->li_flags)) {
0e57f6a3
DC
697 /* check if we really need to move the item */
698 if (XFS_LSN_CMP(lsn, lip->li_lsn) <= 0)
699 continue;
700
750b9c90 701 trace_xfs_ail_move(lip, lip->li_lsn, lsn);
0e57f6a3
DC
702 xfs_ail_delete(ailp, lip);
703 if (mlip == lip)
704 mlip_changed = 1;
705 } else {
750b9c90 706 trace_xfs_ail_insert(lip, 0, lsn);
0e57f6a3
DC
707 }
708 lip->li_lsn = lsn;
709 list_add(&lip->li_ail, &tmp);
710 }
711
e44f4112
AE
712 if (!list_empty(&tmp))
713 xfs_ail_splice(ailp, cur, &tmp, lsn);
0e57f6a3 714
1c304625 715 if (mlip_changed) {
57e80956
MW
716 if (!XFS_FORCED_SHUTDOWN(ailp->ail_mount))
717 xlog_assign_tail_lsn_locked(ailp->ail_mount);
718 spin_unlock(&ailp->ail_lock);
1c304625 719
57e80956 720 xfs_log_space_wake(ailp->ail_mount);
1c304625 721 } else {
57e80956 722 spin_unlock(&ailp->ail_lock);
0e57f6a3 723 }
0e57f6a3
DC
724}
725
27af1bbf
CH
726bool
727xfs_ail_delete_one(
728 struct xfs_ail *ailp,
d3a304b6 729 struct xfs_log_item *lip)
27af1bbf
CH
730{
731 struct xfs_log_item *mlip = xfs_ail_min(ailp);
732
733 trace_xfs_ail_delete(lip, mlip->li_lsn, lip->li_lsn);
734 xfs_ail_delete(ailp, lip);
d3a304b6 735 xfs_clear_li_failed(lip);
22525c17 736 clear_bit(XFS_LI_IN_AIL, &lip->li_flags);
27af1bbf
CH
737 lip->li_lsn = 0;
738
739 return mlip == lip;
740}
741
742/**
743 * Remove a log items from the AIL
30136832
DC
744 *
745 * @xfs_trans_ail_delete_bulk takes an array of log items that all need to
746 * removed from the AIL. The caller is already holding the AIL lock, and done
747 * all the checks necessary to ensure the items passed in via @log_items are
748 * ready for deletion. This includes checking that the items are in the AIL.
749 *
750 * For each log item to be removed, unlink it from the AIL, clear the IN_AIL
751 * flag from the item and reset the item's lsn to 0. If we remove the first
752 * item in the AIL, update the log tail to match the new minimum LSN in the
753 * AIL.
754 *
755 * This function will not drop the AIL lock until all items are removed from
756 * the AIL to minimise the amount of lock traffic on the AIL. This does not
757 * greatly increase the AIL hold time, but does significantly reduce the amount
758 * of traffic on the lock, especially during IO completion.
759 *
760 * This function must be called with the AIL lock held. The lock is dropped
761 * before returning.
762 */
763void
27af1bbf 764xfs_trans_ail_delete(
30136832 765 struct xfs_ail *ailp,
27af1bbf 766 struct xfs_log_item *lip,
57e80956 767 int shutdown_type) __releases(ailp->ail_lock)
30136832 768{
57e80956 769 struct xfs_mount *mp = ailp->ail_mount;
27af1bbf 770 bool mlip_changed;
30136832 771
22525c17 772 if (!test_bit(XFS_LI_IN_AIL, &lip->li_flags)) {
57e80956 773 spin_unlock(&ailp->ail_lock);
27af1bbf
CH
774 if (!XFS_FORCED_SHUTDOWN(mp)) {
775 xfs_alert_tag(mp, XFS_PTAG_AILDELETE,
776 "%s: attempting to delete a log item that is not in the AIL",
777 __func__);
778 xfs_force_shutdown(mp, shutdown_type);
30136832 779 }
27af1bbf 780 return;
30136832
DC
781 }
782
27af1bbf 783 mlip_changed = xfs_ail_delete_one(ailp, lip);
1c304625 784 if (mlip_changed) {
27af1bbf
CH
785 if (!XFS_FORCED_SHUTDOWN(mp))
786 xlog_assign_tail_lsn_locked(mp);
57e80956
MW
787 if (list_empty(&ailp->ail_head))
788 wake_up_all(&ailp->ail_empty);
27af1bbf 789 }
1c304625 790
57e80956 791 spin_unlock(&ailp->ail_lock);
27af1bbf 792 if (mlip_changed)
57e80956 793 xfs_log_space_wake(ailp->ail_mount);
30136832 794}
1da177e4 795
249a8c11 796int
1da177e4
LT
797xfs_trans_ail_init(
798 xfs_mount_t *mp)
799{
82fa9012
DC
800 struct xfs_ail *ailp;
801
802 ailp = kmem_zalloc(sizeof(struct xfs_ail), KM_MAYFAIL);
803 if (!ailp)
2451337d 804 return -ENOMEM;
82fa9012 805
57e80956
MW
806 ailp->ail_mount = mp;
807 INIT_LIST_HEAD(&ailp->ail_head);
808 INIT_LIST_HEAD(&ailp->ail_cursors);
809 spin_lock_init(&ailp->ail_lock);
810 INIT_LIST_HEAD(&ailp->ail_buf_list);
811 init_waitqueue_head(&ailp->ail_empty);
0030807c 812
57e80956
MW
813 ailp->ail_task = kthread_run(xfsaild, ailp, "xfsaild/%s",
814 ailp->ail_mount->m_fsname);
815 if (IS_ERR(ailp->ail_task))
0030807c
CH
816 goto out_free_ailp;
817
27d8d5fe
DC
818 mp->m_ail = ailp;
819 return 0;
0030807c
CH
820
821out_free_ailp:
822 kmem_free(ailp);
2451337d 823 return -ENOMEM;
249a8c11
DC
824}
825
826void
827xfs_trans_ail_destroy(
828 xfs_mount_t *mp)
829{
82fa9012
DC
830 struct xfs_ail *ailp = mp->m_ail;
831
57e80956 832 kthread_stop(ailp->ail_task);
82fa9012 833 kmem_free(ailp);
1da177e4 834}