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