xref: /openbmc/linux/fs/xfs/xfs_trans_ail.c (revision adb57164)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
4  * Copyright (c) 2008 Dave Chinner
5  * All Rights Reserved.
6  */
7 #include "xfs.h"
8 #include "xfs_fs.h"
9 #include "xfs_shared.h"
10 #include "xfs_format.h"
11 #include "xfs_log_format.h"
12 #include "xfs_trans_resv.h"
13 #include "xfs_mount.h"
14 #include "xfs_trans.h"
15 #include "xfs_trans_priv.h"
16 #include "xfs_trace.h"
17 #include "xfs_errortag.h"
18 #include "xfs_error.h"
19 #include "xfs_log.h"
20 
21 #ifdef DEBUG
22 /*
23  * Check that the list is sorted as it should be.
24  *
25  * Called with the ail lock held, but we don't want to assert fail with it
26  * held otherwise we'll lock everything up and won't be able to debug the
27  * cause. Hence we sample and check the state under the AIL lock and return if
28  * everything is fine, otherwise we drop the lock and run the ASSERT checks.
29  * Asserts may not be fatal, so pick the lock back up and continue onwards.
30  */
31 STATIC void
32 xfs_ail_check(
33 	struct xfs_ail		*ailp,
34 	struct xfs_log_item	*lip)
35 	__must_hold(&ailp->ail_lock)
36 {
37 	struct xfs_log_item	*prev_lip;
38 	struct xfs_log_item	*next_lip;
39 	xfs_lsn_t		prev_lsn = NULLCOMMITLSN;
40 	xfs_lsn_t		next_lsn = NULLCOMMITLSN;
41 	xfs_lsn_t		lsn;
42 	bool			in_ail;
43 
44 
45 	if (list_empty(&ailp->ail_head))
46 		return;
47 
48 	/*
49 	 * Sample then check the next and previous entries are valid.
50 	 */
51 	in_ail = test_bit(XFS_LI_IN_AIL, &lip->li_flags);
52 	prev_lip = list_entry(lip->li_ail.prev, struct xfs_log_item, li_ail);
53 	if (&prev_lip->li_ail != &ailp->ail_head)
54 		prev_lsn = prev_lip->li_lsn;
55 	next_lip = list_entry(lip->li_ail.next, struct xfs_log_item, li_ail);
56 	if (&next_lip->li_ail != &ailp->ail_head)
57 		next_lsn = next_lip->li_lsn;
58 	lsn = lip->li_lsn;
59 
60 	if (in_ail &&
61 	    (prev_lsn == NULLCOMMITLSN || XFS_LSN_CMP(prev_lsn, lsn) <= 0) &&
62 	    (next_lsn == NULLCOMMITLSN || XFS_LSN_CMP(next_lsn, lsn) >= 0))
63 		return;
64 
65 	spin_unlock(&ailp->ail_lock);
66 	ASSERT(in_ail);
67 	ASSERT(prev_lsn == NULLCOMMITLSN || XFS_LSN_CMP(prev_lsn, lsn) <= 0);
68 	ASSERT(next_lsn == NULLCOMMITLSN || XFS_LSN_CMP(next_lsn, lsn) >= 0);
69 	spin_lock(&ailp->ail_lock);
70 }
71 #else /* !DEBUG */
72 #define	xfs_ail_check(a,l)
73 #endif /* DEBUG */
74 
75 /*
76  * Return a pointer to the last item in the AIL.  If the AIL is empty, then
77  * return NULL.
78  */
79 static struct xfs_log_item *
80 xfs_ail_max(
81 	struct xfs_ail  *ailp)
82 {
83 	if (list_empty(&ailp->ail_head))
84 		return NULL;
85 
86 	return list_entry(ailp->ail_head.prev, struct xfs_log_item, li_ail);
87 }
88 
89 /*
90  * Return a pointer to the item which follows the given item in the AIL.  If
91  * the given item is the last item in the list, then return NULL.
92  */
93 static struct xfs_log_item *
94 xfs_ail_next(
95 	struct xfs_ail		*ailp,
96 	struct xfs_log_item	*lip)
97 {
98 	if (lip->li_ail.next == &ailp->ail_head)
99 		return NULL;
100 
101 	return list_first_entry(&lip->li_ail, struct xfs_log_item, li_ail);
102 }
103 
104 /*
105  * This is called by the log manager code to determine the LSN of the tail of
106  * the log.  This is exactly the LSN of the first item in the AIL.  If the AIL
107  * is empty, then this function returns 0.
108  *
109  * We need the AIL lock in order to get a coherent read of the lsn of the last
110  * item in the AIL.
111  */
112 static xfs_lsn_t
113 __xfs_ail_min_lsn(
114 	struct xfs_ail		*ailp)
115 {
116 	struct xfs_log_item	*lip = xfs_ail_min(ailp);
117 
118 	if (lip)
119 		return lip->li_lsn;
120 	return 0;
121 }
122 
123 xfs_lsn_t
124 xfs_ail_min_lsn(
125 	struct xfs_ail		*ailp)
126 {
127 	xfs_lsn_t		lsn;
128 
129 	spin_lock(&ailp->ail_lock);
130 	lsn = __xfs_ail_min_lsn(ailp);
131 	spin_unlock(&ailp->ail_lock);
132 
133 	return lsn;
134 }
135 
136 /*
137  * Return the maximum lsn held in the AIL, or zero if the AIL is empty.
138  */
139 static xfs_lsn_t
140 xfs_ail_max_lsn(
141 	struct xfs_ail		*ailp)
142 {
143 	xfs_lsn_t       	lsn = 0;
144 	struct xfs_log_item	*lip;
145 
146 	spin_lock(&ailp->ail_lock);
147 	lip = xfs_ail_max(ailp);
148 	if (lip)
149 		lsn = lip->li_lsn;
150 	spin_unlock(&ailp->ail_lock);
151 
152 	return lsn;
153 }
154 
155 /*
156  * The cursor keeps track of where our current traversal is up to by tracking
157  * the next item in the list for us. However, for this to be safe, removing an
158  * object from the AIL needs to invalidate any cursor that points to it. hence
159  * the traversal cursor needs to be linked to the struct xfs_ail so that
160  * deletion can search all the active cursors for invalidation.
161  */
162 STATIC void
163 xfs_trans_ail_cursor_init(
164 	struct xfs_ail		*ailp,
165 	struct xfs_ail_cursor	*cur)
166 {
167 	cur->item = NULL;
168 	list_add_tail(&cur->list, &ailp->ail_cursors);
169 }
170 
171 /*
172  * Get the next item in the traversal and advance the cursor.  If the cursor
173  * was invalidated (indicated by a lip of 1), restart the traversal.
174  */
175 struct xfs_log_item *
176 xfs_trans_ail_cursor_next(
177 	struct xfs_ail		*ailp,
178 	struct xfs_ail_cursor	*cur)
179 {
180 	struct xfs_log_item	*lip = cur->item;
181 
182 	if ((uintptr_t)lip & 1)
183 		lip = xfs_ail_min(ailp);
184 	if (lip)
185 		cur->item = xfs_ail_next(ailp, lip);
186 	return lip;
187 }
188 
189 /*
190  * When the traversal is complete, we need to remove the cursor from the list
191  * of traversing cursors.
192  */
193 void
194 xfs_trans_ail_cursor_done(
195 	struct xfs_ail_cursor	*cur)
196 {
197 	cur->item = NULL;
198 	list_del_init(&cur->list);
199 }
200 
201 /*
202  * Invalidate any cursor that is pointing to this item. This is called when an
203  * item is removed from the AIL. Any cursor pointing to this object is now
204  * invalid and the traversal needs to be terminated so it doesn't reference a
205  * freed object. We set the low bit of the cursor item pointer so we can
206  * distinguish between an invalidation and the end of the list when getting the
207  * next item from the cursor.
208  */
209 STATIC void
210 xfs_trans_ail_cursor_clear(
211 	struct xfs_ail		*ailp,
212 	struct xfs_log_item	*lip)
213 {
214 	struct xfs_ail_cursor	*cur;
215 
216 	list_for_each_entry(cur, &ailp->ail_cursors, list) {
217 		if (cur->item == lip)
218 			cur->item = (struct xfs_log_item *)
219 					((uintptr_t)cur->item | 1);
220 	}
221 }
222 
223 /*
224  * Find the first item in the AIL with the given @lsn by searching in ascending
225  * LSN order and initialise the cursor to point to the next item for a
226  * ascending traversal.  Pass a @lsn of zero to initialise the cursor to the
227  * first item in the AIL. Returns NULL if the list is empty.
228  */
229 struct xfs_log_item *
230 xfs_trans_ail_cursor_first(
231 	struct xfs_ail		*ailp,
232 	struct xfs_ail_cursor	*cur,
233 	xfs_lsn_t		lsn)
234 {
235 	struct xfs_log_item	*lip;
236 
237 	xfs_trans_ail_cursor_init(ailp, cur);
238 
239 	if (lsn == 0) {
240 		lip = xfs_ail_min(ailp);
241 		goto out;
242 	}
243 
244 	list_for_each_entry(lip, &ailp->ail_head, li_ail) {
245 		if (XFS_LSN_CMP(lip->li_lsn, lsn) >= 0)
246 			goto out;
247 	}
248 	return NULL;
249 
250 out:
251 	if (lip)
252 		cur->item = xfs_ail_next(ailp, lip);
253 	return lip;
254 }
255 
256 static struct xfs_log_item *
257 __xfs_trans_ail_cursor_last(
258 	struct xfs_ail		*ailp,
259 	xfs_lsn_t		lsn)
260 {
261 	struct xfs_log_item	*lip;
262 
263 	list_for_each_entry_reverse(lip, &ailp->ail_head, li_ail) {
264 		if (XFS_LSN_CMP(lip->li_lsn, lsn) <= 0)
265 			return lip;
266 	}
267 	return NULL;
268 }
269 
270 /*
271  * Find the last item in the AIL with the given @lsn by searching in descending
272  * LSN order and initialise the cursor to point to that item.  If there is no
273  * item with the value of @lsn, then it sets the cursor to the last item with an
274  * LSN lower than @lsn.  Returns NULL if the list is empty.
275  */
276 struct xfs_log_item *
277 xfs_trans_ail_cursor_last(
278 	struct xfs_ail		*ailp,
279 	struct xfs_ail_cursor	*cur,
280 	xfs_lsn_t		lsn)
281 {
282 	xfs_trans_ail_cursor_init(ailp, cur);
283 	cur->item = __xfs_trans_ail_cursor_last(ailp, lsn);
284 	return cur->item;
285 }
286 
287 /*
288  * Splice the log item list into the AIL at the given LSN. We splice to the
289  * tail of the given LSN to maintain insert order for push traversals. The
290  * cursor is optional, allowing repeated updates to the same LSN to avoid
291  * repeated traversals.  This should not be called with an empty list.
292  */
293 static void
294 xfs_ail_splice(
295 	struct xfs_ail		*ailp,
296 	struct xfs_ail_cursor	*cur,
297 	struct list_head	*list,
298 	xfs_lsn_t		lsn)
299 {
300 	struct xfs_log_item	*lip;
301 
302 	ASSERT(!list_empty(list));
303 
304 	/*
305 	 * Use the cursor to determine the insertion point if one is
306 	 * provided.  If not, or if the one we got is not valid,
307 	 * find the place in the AIL where the items belong.
308 	 */
309 	lip = cur ? cur->item : NULL;
310 	if (!lip || (uintptr_t)lip & 1)
311 		lip = __xfs_trans_ail_cursor_last(ailp, lsn);
312 
313 	/*
314 	 * If a cursor is provided, we know we're processing the AIL
315 	 * in lsn order, and future items to be spliced in will
316 	 * follow the last one being inserted now.  Update the
317 	 * cursor to point to that last item, now while we have a
318 	 * reliable pointer to it.
319 	 */
320 	if (cur)
321 		cur->item = list_entry(list->prev, struct xfs_log_item, li_ail);
322 
323 	/*
324 	 * Finally perform the splice.  Unless the AIL was empty,
325 	 * lip points to the item in the AIL _after_ which the new
326 	 * items should go.  If lip is null the AIL was empty, so
327 	 * the new items go at the head of the AIL.
328 	 */
329 	if (lip)
330 		list_splice(list, &lip->li_ail);
331 	else
332 		list_splice(list, &ailp->ail_head);
333 }
334 
335 /*
336  * Delete the given item from the AIL.  Return a pointer to the item.
337  */
338 static void
339 xfs_ail_delete(
340 	struct xfs_ail		*ailp,
341 	struct xfs_log_item	*lip)
342 {
343 	xfs_ail_check(ailp, lip);
344 	list_del(&lip->li_ail);
345 	xfs_trans_ail_cursor_clear(ailp, lip);
346 }
347 
348 static inline uint
349 xfsaild_push_item(
350 	struct xfs_ail		*ailp,
351 	struct xfs_log_item	*lip)
352 {
353 	/*
354 	 * If log item pinning is enabled, skip the push and track the item as
355 	 * pinned. This can help induce head-behind-tail conditions.
356 	 */
357 	if (XFS_TEST_ERROR(false, ailp->ail_mount, XFS_ERRTAG_LOG_ITEM_PIN))
358 		return XFS_ITEM_PINNED;
359 
360 	/*
361 	 * Consider the item pinned if a push callback is not defined so the
362 	 * caller will force the log. This should only happen for intent items
363 	 * as they are unpinned once the associated done item is committed to
364 	 * the on-disk log.
365 	 */
366 	if (!lip->li_ops->iop_push)
367 		return XFS_ITEM_PINNED;
368 	return lip->li_ops->iop_push(lip, &ailp->ail_buf_list);
369 }
370 
371 static long
372 xfsaild_push(
373 	struct xfs_ail		*ailp)
374 {
375 	xfs_mount_t		*mp = ailp->ail_mount;
376 	struct xfs_ail_cursor	cur;
377 	struct xfs_log_item	*lip;
378 	xfs_lsn_t		lsn;
379 	xfs_lsn_t		target;
380 	long			tout;
381 	int			stuck = 0;
382 	int			flushing = 0;
383 	int			count = 0;
384 
385 	/*
386 	 * If we encountered pinned items or did not finish writing out all
387 	 * buffers the last time we ran, force the log first and wait for it
388 	 * before pushing again.
389 	 */
390 	if (ailp->ail_log_flush && ailp->ail_last_pushed_lsn == 0 &&
391 	    (!list_empty_careful(&ailp->ail_buf_list) ||
392 	     xfs_ail_min_lsn(ailp))) {
393 		ailp->ail_log_flush = 0;
394 
395 		XFS_STATS_INC(mp, xs_push_ail_flush);
396 		xfs_log_force(mp, XFS_LOG_SYNC);
397 	}
398 
399 	spin_lock(&ailp->ail_lock);
400 
401 	/* barrier matches the ail_target update in xfs_ail_push() */
402 	smp_rmb();
403 	target = ailp->ail_target;
404 	ailp->ail_target_prev = target;
405 
406 	lip = xfs_trans_ail_cursor_first(ailp, &cur, ailp->ail_last_pushed_lsn);
407 	if (!lip) {
408 		/*
409 		 * If the AIL is empty or our push has reached the end we are
410 		 * done now.
411 		 */
412 		xfs_trans_ail_cursor_done(&cur);
413 		spin_unlock(&ailp->ail_lock);
414 		goto out_done;
415 	}
416 
417 	XFS_STATS_INC(mp, xs_push_ail);
418 
419 	lsn = lip->li_lsn;
420 	while ((XFS_LSN_CMP(lip->li_lsn, target) <= 0)) {
421 		int	lock_result;
422 
423 		/*
424 		 * Note that iop_push may unlock and reacquire the AIL lock.  We
425 		 * rely on the AIL cursor implementation to be able to deal with
426 		 * the dropped lock.
427 		 */
428 		lock_result = xfsaild_push_item(ailp, lip);
429 		switch (lock_result) {
430 		case XFS_ITEM_SUCCESS:
431 			XFS_STATS_INC(mp, xs_push_ail_success);
432 			trace_xfs_ail_push(lip);
433 
434 			ailp->ail_last_pushed_lsn = lsn;
435 			break;
436 
437 		case XFS_ITEM_FLUSHING:
438 			/*
439 			 * The item or its backing buffer is already being
440 			 * flushed.  The typical reason for that is that an
441 			 * inode buffer is locked because we already pushed the
442 			 * updates to it as part of inode clustering.
443 			 *
444 			 * We do not want to to stop flushing just because lots
445 			 * of items are already being flushed, but we need to
446 			 * re-try the flushing relatively soon if most of the
447 			 * AIL is being flushed.
448 			 */
449 			XFS_STATS_INC(mp, xs_push_ail_flushing);
450 			trace_xfs_ail_flushing(lip);
451 
452 			flushing++;
453 			ailp->ail_last_pushed_lsn = lsn;
454 			break;
455 
456 		case XFS_ITEM_PINNED:
457 			XFS_STATS_INC(mp, xs_push_ail_pinned);
458 			trace_xfs_ail_pinned(lip);
459 
460 			stuck++;
461 			ailp->ail_log_flush++;
462 			break;
463 		case XFS_ITEM_LOCKED:
464 			XFS_STATS_INC(mp, xs_push_ail_locked);
465 			trace_xfs_ail_locked(lip);
466 
467 			stuck++;
468 			break;
469 		default:
470 			ASSERT(0);
471 			break;
472 		}
473 
474 		count++;
475 
476 		/*
477 		 * Are there too many items we can't do anything with?
478 		 *
479 		 * If we we are skipping too many items because we can't flush
480 		 * them or they are already being flushed, we back off and
481 		 * given them time to complete whatever operation is being
482 		 * done. i.e. remove pressure from the AIL while we can't make
483 		 * progress so traversals don't slow down further inserts and
484 		 * removals to/from the AIL.
485 		 *
486 		 * The value of 100 is an arbitrary magic number based on
487 		 * observation.
488 		 */
489 		if (stuck > 100)
490 			break;
491 
492 		lip = xfs_trans_ail_cursor_next(ailp, &cur);
493 		if (lip == NULL)
494 			break;
495 		lsn = lip->li_lsn;
496 	}
497 	xfs_trans_ail_cursor_done(&cur);
498 	spin_unlock(&ailp->ail_lock);
499 
500 	if (xfs_buf_delwri_submit_nowait(&ailp->ail_buf_list))
501 		ailp->ail_log_flush++;
502 
503 	if (!count || XFS_LSN_CMP(lsn, target) >= 0) {
504 out_done:
505 		/*
506 		 * We reached the target or the AIL is empty, so wait a bit
507 		 * longer for I/O to complete and remove pushed items from the
508 		 * AIL before we start the next scan from the start of the AIL.
509 		 */
510 		tout = 50;
511 		ailp->ail_last_pushed_lsn = 0;
512 	} else if (((stuck + flushing) * 100) / count > 90) {
513 		/*
514 		 * Either there is a lot of contention on the AIL or we are
515 		 * stuck due to operations in progress. "Stuck" in this case
516 		 * is defined as >90% of the items we tried to push were stuck.
517 		 *
518 		 * Backoff a bit more to allow some I/O to complete before
519 		 * restarting from the start of the AIL. This prevents us from
520 		 * spinning on the same items, and if they are pinned will all
521 		 * the restart to issue a log force to unpin the stuck items.
522 		 */
523 		tout = 20;
524 		ailp->ail_last_pushed_lsn = 0;
525 	} else {
526 		/*
527 		 * Assume we have more work to do in a short while.
528 		 */
529 		tout = 10;
530 	}
531 
532 	return tout;
533 }
534 
535 static int
536 xfsaild(
537 	void		*data)
538 {
539 	struct xfs_ail	*ailp = data;
540 	long		tout = 0;	/* milliseconds */
541 	unsigned int	noreclaim_flag;
542 
543 	noreclaim_flag = memalloc_noreclaim_save();
544 	set_freezable();
545 
546 	while (1) {
547 		if (tout && tout <= 20)
548 			set_current_state(TASK_KILLABLE);
549 		else
550 			set_current_state(TASK_INTERRUPTIBLE);
551 
552 		/*
553 		 * Check kthread_should_stop() after we set the task state to
554 		 * guarantee that we either see the stop bit and exit or the
555 		 * task state is reset to runnable such that it's not scheduled
556 		 * out indefinitely and detects the stop bit at next iteration.
557 		 * A memory barrier is included in above task state set to
558 		 * serialize again kthread_stop().
559 		 */
560 		if (kthread_should_stop()) {
561 			__set_current_state(TASK_RUNNING);
562 
563 			/*
564 			 * The caller forces out the AIL before stopping the
565 			 * thread in the common case, which means the delwri
566 			 * queue is drained. In the shutdown case, the queue may
567 			 * still hold relogged buffers that haven't been
568 			 * submitted because they were pinned since added to the
569 			 * queue.
570 			 *
571 			 * Log I/O error processing stales the underlying buffer
572 			 * and clears the delwri state, expecting the buf to be
573 			 * removed on the next submission attempt. That won't
574 			 * happen if we're shutting down, so this is the last
575 			 * opportunity to release such buffers from the queue.
576 			 */
577 			ASSERT(list_empty(&ailp->ail_buf_list) ||
578 			       XFS_FORCED_SHUTDOWN(ailp->ail_mount));
579 			xfs_buf_delwri_cancel(&ailp->ail_buf_list);
580 			break;
581 		}
582 
583 		spin_lock(&ailp->ail_lock);
584 
585 		/*
586 		 * Idle if the AIL is empty and we are not racing with a target
587 		 * update. We check the AIL after we set the task to a sleep
588 		 * state to guarantee that we either catch an ail_target update
589 		 * or that a wake_up resets the state to TASK_RUNNING.
590 		 * Otherwise, we run the risk of sleeping indefinitely.
591 		 *
592 		 * The barrier matches the ail_target update in xfs_ail_push().
593 		 */
594 		smp_rmb();
595 		if (!xfs_ail_min(ailp) &&
596 		    ailp->ail_target == ailp->ail_target_prev) {
597 			spin_unlock(&ailp->ail_lock);
598 			freezable_schedule();
599 			tout = 0;
600 			continue;
601 		}
602 		spin_unlock(&ailp->ail_lock);
603 
604 		if (tout)
605 			freezable_schedule_timeout(msecs_to_jiffies(tout));
606 
607 		__set_current_state(TASK_RUNNING);
608 
609 		try_to_freeze();
610 
611 		tout = xfsaild_push(ailp);
612 	}
613 
614 	memalloc_noreclaim_restore(noreclaim_flag);
615 	return 0;
616 }
617 
618 /*
619  * This routine is called to move the tail of the AIL forward.  It does this by
620  * trying to flush items in the AIL whose lsns are below the given
621  * threshold_lsn.
622  *
623  * The push is run asynchronously in a workqueue, which means the caller needs
624  * to handle waiting on the async flush for space to become available.
625  * We don't want to interrupt any push that is in progress, hence we only queue
626  * work if we set the pushing bit appropriately.
627  *
628  * We do this unlocked - we only need to know whether there is anything in the
629  * AIL at the time we are called. We don't need to access the contents of
630  * any of the objects, so the lock is not needed.
631  */
632 void
633 xfs_ail_push(
634 	struct xfs_ail		*ailp,
635 	xfs_lsn_t		threshold_lsn)
636 {
637 	struct xfs_log_item	*lip;
638 
639 	lip = xfs_ail_min(ailp);
640 	if (!lip || XFS_FORCED_SHUTDOWN(ailp->ail_mount) ||
641 	    XFS_LSN_CMP(threshold_lsn, ailp->ail_target) <= 0)
642 		return;
643 
644 	/*
645 	 * Ensure that the new target is noticed in push code before it clears
646 	 * the XFS_AIL_PUSHING_BIT.
647 	 */
648 	smp_wmb();
649 	xfs_trans_ail_copy_lsn(ailp, &ailp->ail_target, &threshold_lsn);
650 	smp_wmb();
651 
652 	wake_up_process(ailp->ail_task);
653 }
654 
655 /*
656  * Push out all items in the AIL immediately
657  */
658 void
659 xfs_ail_push_all(
660 	struct xfs_ail  *ailp)
661 {
662 	xfs_lsn_t       threshold_lsn = xfs_ail_max_lsn(ailp);
663 
664 	if (threshold_lsn)
665 		xfs_ail_push(ailp, threshold_lsn);
666 }
667 
668 /*
669  * Push out all items in the AIL immediately and wait until the AIL is empty.
670  */
671 void
672 xfs_ail_push_all_sync(
673 	struct xfs_ail  *ailp)
674 {
675 	struct xfs_log_item	*lip;
676 	DEFINE_WAIT(wait);
677 
678 	spin_lock(&ailp->ail_lock);
679 	while ((lip = xfs_ail_max(ailp)) != NULL) {
680 		prepare_to_wait(&ailp->ail_empty, &wait, TASK_UNINTERRUPTIBLE);
681 		ailp->ail_target = lip->li_lsn;
682 		wake_up_process(ailp->ail_task);
683 		spin_unlock(&ailp->ail_lock);
684 		schedule();
685 		spin_lock(&ailp->ail_lock);
686 	}
687 	spin_unlock(&ailp->ail_lock);
688 
689 	finish_wait(&ailp->ail_empty, &wait);
690 }
691 
692 void
693 xfs_ail_update_finish(
694 	struct xfs_ail		*ailp,
695 	xfs_lsn_t		old_lsn) __releases(ailp->ail_lock)
696 {
697 	struct xfs_mount	*mp = ailp->ail_mount;
698 
699 	/* if the tail lsn hasn't changed, don't do updates or wakeups. */
700 	if (!old_lsn || old_lsn == __xfs_ail_min_lsn(ailp)) {
701 		spin_unlock(&ailp->ail_lock);
702 		return;
703 	}
704 
705 	if (!XFS_FORCED_SHUTDOWN(mp))
706 		xlog_assign_tail_lsn_locked(mp);
707 
708 	if (list_empty(&ailp->ail_head))
709 		wake_up_all(&ailp->ail_empty);
710 	spin_unlock(&ailp->ail_lock);
711 	xfs_log_space_wake(mp);
712 }
713 
714 /*
715  * xfs_trans_ail_update - bulk AIL insertion operation.
716  *
717  * @xfs_trans_ail_update takes an array of log items that all need to be
718  * positioned at the same LSN in the AIL. If an item is not in the AIL, it will
719  * be added.  Otherwise, it will be repositioned  by removing it and re-adding
720  * it to the AIL. If we move the first item in the AIL, update the log tail to
721  * match the new minimum LSN in the AIL.
722  *
723  * This function takes the AIL lock once to execute the update operations on
724  * all the items in the array, and as such should not be called with the AIL
725  * lock held. As a result, once we have the AIL lock, we need to check each log
726  * item LSN to confirm it needs to be moved forward in the AIL.
727  *
728  * To optimise the insert operation, we delete all the items from the AIL in
729  * the first pass, moving them into a temporary list, then splice the temporary
730  * list into the correct position in the AIL. This avoids needing to do an
731  * insert operation on every item.
732  *
733  * This function must be called with the AIL lock held.  The lock is dropped
734  * before returning.
735  */
736 void
737 xfs_trans_ail_update_bulk(
738 	struct xfs_ail		*ailp,
739 	struct xfs_ail_cursor	*cur,
740 	struct xfs_log_item	**log_items,
741 	int			nr_items,
742 	xfs_lsn_t		lsn) __releases(ailp->ail_lock)
743 {
744 	struct xfs_log_item	*mlip;
745 	xfs_lsn_t		tail_lsn = 0;
746 	int			i;
747 	LIST_HEAD(tmp);
748 
749 	ASSERT(nr_items > 0);		/* Not required, but true. */
750 	mlip = xfs_ail_min(ailp);
751 
752 	for (i = 0; i < nr_items; i++) {
753 		struct xfs_log_item *lip = log_items[i];
754 		if (test_and_set_bit(XFS_LI_IN_AIL, &lip->li_flags)) {
755 			/* check if we really need to move the item */
756 			if (XFS_LSN_CMP(lsn, lip->li_lsn) <= 0)
757 				continue;
758 
759 			trace_xfs_ail_move(lip, lip->li_lsn, lsn);
760 			if (mlip == lip && !tail_lsn)
761 				tail_lsn = lip->li_lsn;
762 
763 			xfs_ail_delete(ailp, lip);
764 		} else {
765 			trace_xfs_ail_insert(lip, 0, lsn);
766 		}
767 		lip->li_lsn = lsn;
768 		list_add(&lip->li_ail, &tmp);
769 	}
770 
771 	if (!list_empty(&tmp))
772 		xfs_ail_splice(ailp, cur, &tmp, lsn);
773 
774 	xfs_ail_update_finish(ailp, tail_lsn);
775 }
776 
777 /*
778  * Delete one log item from the AIL.
779  *
780  * If this item was at the tail of the AIL, return the LSN of the log item so
781  * that we can use it to check if the LSN of the tail of the log has moved
782  * when finishing up the AIL delete process in xfs_ail_update_finish().
783  */
784 xfs_lsn_t
785 xfs_ail_delete_one(
786 	struct xfs_ail		*ailp,
787 	struct xfs_log_item	*lip)
788 {
789 	struct xfs_log_item	*mlip = xfs_ail_min(ailp);
790 	xfs_lsn_t		lsn = lip->li_lsn;
791 
792 	trace_xfs_ail_delete(lip, mlip->li_lsn, lip->li_lsn);
793 	xfs_ail_delete(ailp, lip);
794 	xfs_clear_li_failed(lip);
795 	clear_bit(XFS_LI_IN_AIL, &lip->li_flags);
796 	lip->li_lsn = 0;
797 
798 	if (mlip == lip)
799 		return lsn;
800 	return 0;
801 }
802 
803 /**
804  * Remove a log items from the AIL
805  *
806  * @xfs_trans_ail_delete_bulk takes an array of log items that all need to
807  * removed from the AIL. The caller is already holding the AIL lock, and done
808  * all the checks necessary to ensure the items passed in via @log_items are
809  * ready for deletion. This includes checking that the items are in the AIL.
810  *
811  * For each log item to be removed, unlink it  from the AIL, clear the IN_AIL
812  * flag from the item and reset the item's lsn to 0. If we remove the first
813  * item in the AIL, update the log tail to match the new minimum LSN in the
814  * AIL.
815  *
816  * This function will not drop the AIL lock until all items are removed from
817  * the AIL to minimise the amount of lock traffic on the AIL. This does not
818  * greatly increase the AIL hold time, but does significantly reduce the amount
819  * of traffic on the lock, especially during IO completion.
820  *
821  * This function must be called with the AIL lock held.  The lock is dropped
822  * before returning.
823  */
824 void
825 xfs_trans_ail_delete(
826 	struct xfs_ail		*ailp,
827 	struct xfs_log_item	*lip,
828 	int			shutdown_type)
829 {
830 	struct xfs_mount	*mp = ailp->ail_mount;
831 	xfs_lsn_t		tail_lsn;
832 
833 	if (!test_bit(XFS_LI_IN_AIL, &lip->li_flags)) {
834 		spin_unlock(&ailp->ail_lock);
835 		if (!XFS_FORCED_SHUTDOWN(mp)) {
836 			xfs_alert_tag(mp, XFS_PTAG_AILDELETE,
837 	"%s: attempting to delete a log item that is not in the AIL",
838 					__func__);
839 			xfs_force_shutdown(mp, shutdown_type);
840 		}
841 		return;
842 	}
843 
844 	tail_lsn = xfs_ail_delete_one(ailp, lip);
845 	xfs_ail_update_finish(ailp, tail_lsn);
846 }
847 
848 int
849 xfs_trans_ail_init(
850 	xfs_mount_t	*mp)
851 {
852 	struct xfs_ail	*ailp;
853 
854 	ailp = kmem_zalloc(sizeof(struct xfs_ail), KM_MAYFAIL);
855 	if (!ailp)
856 		return -ENOMEM;
857 
858 	ailp->ail_mount = mp;
859 	INIT_LIST_HEAD(&ailp->ail_head);
860 	INIT_LIST_HEAD(&ailp->ail_cursors);
861 	spin_lock_init(&ailp->ail_lock);
862 	INIT_LIST_HEAD(&ailp->ail_buf_list);
863 	init_waitqueue_head(&ailp->ail_empty);
864 
865 	ailp->ail_task = kthread_run(xfsaild, ailp, "xfsaild/%s",
866 			ailp->ail_mount->m_super->s_id);
867 	if (IS_ERR(ailp->ail_task))
868 		goto out_free_ailp;
869 
870 	mp->m_ail = ailp;
871 	return 0;
872 
873 out_free_ailp:
874 	kmem_free(ailp);
875 	return -ENOMEM;
876 }
877 
878 void
879 xfs_trans_ail_destroy(
880 	xfs_mount_t	*mp)
881 {
882 	struct xfs_ail	*ailp = mp->m_ail;
883 
884 	kthread_stop(ailp->ail_task);
885 	kmem_free(ailp);
886 }
887