2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * Copyright (c) 2008 Dave Chinner
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation.
10 * 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.
15 * 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
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
24 #include "xfs_mount.h"
25 #include "xfs_trans.h"
26 #include "xfs_trans_priv.h"
27 #include "xfs_trace.h"
28 #include "xfs_errortag.h"
29 #include "xfs_error.h"
34 * Check that the list is sorted as it should be.
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.
45 struct xfs_log_item *lip)
47 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;
55 if (list_empty(&ailp->ail_head))
59 * Sample then check the next and previous entries are valid.
61 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);
63 if (&prev_lip->li_ail != &ailp->ail_head)
64 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;
71 (prev_lsn == NULLCOMMITLSN || XFS_LSN_CMP(prev_lsn, lsn) <= 0) &&
72 (next_lsn == NULLCOMMITLSN || XFS_LSN_CMP(next_lsn, lsn) >= 0))
75 spin_unlock(&ailp->ail_lock);
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);
82 #define xfs_ail_check(a,l)
86 * Return a pointer to the last item in the AIL. If the AIL is empty, then
89 static xfs_log_item_t *
93 if (list_empty(&ailp->ail_head))
96 return list_entry(ailp->ail_head.prev, xfs_log_item_t, li_ail);
100 * 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.
103 static xfs_log_item_t *
105 struct xfs_ail *ailp,
108 if (lip->li_ail.next == &ailp->ail_head)
111 return list_first_entry(&lip->li_ail, xfs_log_item_t, li_ail);
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.
119 * We need the AIL lock in order to get a coherent read of the lsn of the last
124 struct xfs_ail *ailp)
129 spin_lock(&ailp->ail_lock);
130 lip = xfs_ail_min(ailp);
133 spin_unlock(&ailp->ail_lock);
139 * Return the maximum lsn held in the AIL, or zero if the AIL is empty.
143 struct xfs_ail *ailp)
148 spin_lock(&ailp->ail_lock);
149 lip = xfs_ail_max(ailp);
152 spin_unlock(&ailp->ail_lock);
158 * 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.
165 xfs_trans_ail_cursor_init(
166 struct xfs_ail *ailp,
167 struct xfs_ail_cursor *cur)
170 list_add_tail(&cur->list, &ailp->ail_cursors);
174 * 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.
177 struct xfs_log_item *
178 xfs_trans_ail_cursor_next(
179 struct xfs_ail *ailp,
180 struct xfs_ail_cursor *cur)
182 struct xfs_log_item *lip = cur->item;
184 if ((uintptr_t)lip & 1)
185 lip = xfs_ail_min(ailp);
187 cur->item = xfs_ail_next(ailp, lip);
192 * When the traversal is complete, we need to remove the cursor from the list
193 * of traversing cursors.
196 xfs_trans_ail_cursor_done(
197 struct xfs_ail_cursor *cur)
200 list_del_init(&cur->list);
204 * 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.
212 xfs_trans_ail_cursor_clear(
213 struct xfs_ail *ailp,
214 struct xfs_log_item *lip)
216 struct xfs_ail_cursor *cur;
218 list_for_each_entry(cur, &ailp->ail_cursors, list) {
219 if (cur->item == lip)
220 cur->item = (struct xfs_log_item *)
221 ((uintptr_t)cur->item | 1);
226 * 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.
232 xfs_trans_ail_cursor_first(
233 struct xfs_ail *ailp,
234 struct xfs_ail_cursor *cur,
239 xfs_trans_ail_cursor_init(ailp, cur);
242 lip = xfs_ail_min(ailp);
246 list_for_each_entry(lip, &ailp->ail_head, li_ail) {
247 if (XFS_LSN_CMP(lip->li_lsn, lsn) >= 0)
254 cur->item = xfs_ail_next(ailp, lip);
258 static struct xfs_log_item *
259 __xfs_trans_ail_cursor_last(
260 struct xfs_ail *ailp,
265 list_for_each_entry_reverse(lip, &ailp->ail_head, li_ail) {
266 if (XFS_LSN_CMP(lip->li_lsn, lsn) <= 0)
273 * 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.
278 struct xfs_log_item *
279 xfs_trans_ail_cursor_last(
280 struct xfs_ail *ailp,
281 struct xfs_ail_cursor *cur,
284 xfs_trans_ail_cursor_init(ailp, cur);
285 cur->item = __xfs_trans_ail_cursor_last(ailp, lsn);
290 * Splice the log item list into the AIL at the given LSN. We splice to the
291 * 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
293 * repeated traversals. This should not be called with an empty list.
297 struct xfs_ail *ailp,
298 struct xfs_ail_cursor *cur,
299 struct list_head *list,
302 struct xfs_log_item *lip;
304 ASSERT(!list_empty(list));
307 * 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.
311 lip = cur ? cur->item : NULL;
312 if (!lip || (uintptr_t)lip & 1)
313 lip = __xfs_trans_ail_cursor_last(ailp, lsn);
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.
323 cur->item = list_entry(list->prev, struct xfs_log_item, li_ail);
326 * 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.
332 list_splice(list, &lip->li_ail);
334 list_splice(list, &ailp->ail_head);
338 * Delete the given item from the AIL. Return a pointer to the item.
342 struct xfs_ail *ailp,
345 xfs_ail_check(ailp, lip);
346 list_del(&lip->li_ail);
347 xfs_trans_ail_cursor_clear(ailp, lip);
352 struct xfs_ail *ailp,
353 struct xfs_log_item *lip)
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.
359 if (XFS_TEST_ERROR(false, ailp->ail_mount, XFS_ERRTAG_LOG_ITEM_PIN))
360 return XFS_ITEM_PINNED;
362 return lip->li_ops->iop_push(lip, &ailp->ail_buf_list);
367 struct xfs_ail *ailp)
369 xfs_mount_t *mp = ailp->ail_mount;
370 struct xfs_ail_cursor cur;
380 * 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.
384 if (ailp->ail_log_flush && ailp->ail_last_pushed_lsn == 0 &&
385 (!list_empty_careful(&ailp->ail_buf_list) ||
386 xfs_ail_min_lsn(ailp))) {
387 ailp->ail_log_flush = 0;
389 XFS_STATS_INC(mp, xs_push_ail_flush);
390 xfs_log_force(mp, XFS_LOG_SYNC);
393 spin_lock(&ailp->ail_lock);
395 /* barrier matches the ail_target update in xfs_ail_push() */
397 target = ailp->ail_target;
398 ailp->ail_target_prev = target;
400 lip = xfs_trans_ail_cursor_first(ailp, &cur, ailp->ail_last_pushed_lsn);
403 * If the AIL is empty or our push has reached the end we are
406 xfs_trans_ail_cursor_done(&cur);
407 spin_unlock(&ailp->ail_lock);
411 XFS_STATS_INC(mp, xs_push_ail);
414 while ((XFS_LSN_CMP(lip->li_lsn, target) <= 0)) {
418 * Note that iop_push may unlock and reacquire the AIL lock. We
419 * rely on the AIL cursor implementation to be able to deal with
422 lock_result = xfsaild_push_item(ailp, lip);
423 switch (lock_result) {
424 case XFS_ITEM_SUCCESS:
425 XFS_STATS_INC(mp, xs_push_ail_success);
426 trace_xfs_ail_push(lip);
428 ailp->ail_last_pushed_lsn = lsn;
431 case XFS_ITEM_FLUSHING:
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.
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.
443 XFS_STATS_INC(mp, xs_push_ail_flushing);
444 trace_xfs_ail_flushing(lip);
447 ailp->ail_last_pushed_lsn = lsn;
450 case XFS_ITEM_PINNED:
451 XFS_STATS_INC(mp, xs_push_ail_pinned);
452 trace_xfs_ail_pinned(lip);
455 ailp->ail_log_flush++;
457 case XFS_ITEM_LOCKED:
458 XFS_STATS_INC(mp, xs_push_ail_locked);
459 trace_xfs_ail_locked(lip);
471 * Are there too many items we can't do anything with?
473 * 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.
480 * The value of 100 is an arbitrary magic number based on
486 lip = xfs_trans_ail_cursor_next(ailp, &cur);
491 xfs_trans_ail_cursor_done(&cur);
492 spin_unlock(&ailp->ail_lock);
494 if (xfs_buf_delwri_submit_nowait(&ailp->ail_buf_list))
495 ailp->ail_log_flush++;
497 if (!count || XFS_LSN_CMP(lsn, target) >= 0) {
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.
505 ailp->ail_last_pushed_lsn = 0;
506 } else if (((stuck + flushing) * 100) / count > 90) {
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.
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.
518 ailp->ail_last_pushed_lsn = 0;
521 * Assume we have more work to do in a short while.
533 struct xfs_ail *ailp = data;
534 long tout = 0; /* milliseconds */
536 current->flags |= PF_MEMALLOC;
540 if (tout && tout <= 20)
541 set_current_state(TASK_KILLABLE);
543 set_current_state(TASK_INTERRUPTIBLE);
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
552 * A memory barrier is included in above task state set to
553 * serialize again kthread_stop().
555 if (kthread_should_stop()) {
556 __set_current_state(TASK_RUNNING);
560 spin_lock(&ailp->ail_lock);
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
565 * state to guarantee that we either catch an ail_target update
566 * or that a wake_up resets the state to TASK_RUNNING.
567 * Otherwise, we run the risk of sleeping indefinitely.
569 * The barrier matches the ail_target update in xfs_ail_push().
572 if (!xfs_ail_min(ailp) &&
573 ailp->ail_target == ailp->ail_target_prev) {
574 spin_unlock(&ailp->ail_lock);
575 freezable_schedule();
579 spin_unlock(&ailp->ail_lock);
582 freezable_schedule_timeout(msecs_to_jiffies(tout));
584 __set_current_state(TASK_RUNNING);
588 tout = xfsaild_push(ailp);
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
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.
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.
610 struct xfs_ail *ailp,
611 xfs_lsn_t threshold_lsn)
615 lip = xfs_ail_min(ailp);
616 if (!lip || XFS_FORCED_SHUTDOWN(ailp->ail_mount) ||
617 XFS_LSN_CMP(threshold_lsn, ailp->ail_target) <= 0)
621 * Ensure that the new target is noticed in push code before it clears
622 * the XFS_AIL_PUSHING_BIT.
625 xfs_trans_ail_copy_lsn(ailp, &ailp->ail_target, &threshold_lsn);
628 wake_up_process(ailp->ail_task);
632 * Push out all items in the AIL immediately
636 struct xfs_ail *ailp)
638 xfs_lsn_t threshold_lsn = xfs_ail_max_lsn(ailp);
641 xfs_ail_push(ailp, threshold_lsn);
645 * Push out all items in the AIL immediately and wait until the AIL is empty.
648 xfs_ail_push_all_sync(
649 struct xfs_ail *ailp)
651 struct xfs_log_item *lip;
654 spin_lock(&ailp->ail_lock);
655 while ((lip = xfs_ail_max(ailp)) != NULL) {
656 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);
661 spin_lock(&ailp->ail_lock);
663 spin_unlock(&ailp->ail_lock);
665 finish_wait(&ailp->ail_empty, &wait);
669 * xfs_trans_ail_update - bulk AIL insertion operation.
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.
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.
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.
687 * This function must be called with the AIL lock held. The lock is dropped
691 xfs_trans_ail_update_bulk(
692 struct xfs_ail *ailp,
693 struct xfs_ail_cursor *cur,
694 struct xfs_log_item **log_items,
696 xfs_lsn_t lsn) __releases(ailp->ail_lock)
698 xfs_log_item_t *mlip;
699 int mlip_changed = 0;
703 ASSERT(nr_items > 0); /* Not required, but true. */
704 mlip = xfs_ail_min(ailp);
706 for (i = 0; i < nr_items; i++) {
707 struct xfs_log_item *lip = log_items[i];
708 if (test_and_set_bit(XFS_LI_IN_AIL, &lip->li_flags)) {
709 /* check if we really need to move the item */
710 if (XFS_LSN_CMP(lsn, lip->li_lsn) <= 0)
713 trace_xfs_ail_move(lip, lip->li_lsn, lsn);
714 xfs_ail_delete(ailp, lip);
718 trace_xfs_ail_insert(lip, 0, lsn);
721 list_add(&lip->li_ail, &tmp);
724 if (!list_empty(&tmp))
725 xfs_ail_splice(ailp, cur, &tmp, lsn);
728 if (!XFS_FORCED_SHUTDOWN(ailp->ail_mount))
729 xlog_assign_tail_lsn_locked(ailp->ail_mount);
730 spin_unlock(&ailp->ail_lock);
732 xfs_log_space_wake(ailp->ail_mount);
734 spin_unlock(&ailp->ail_lock);
740 struct xfs_ail *ailp,
741 struct xfs_log_item *lip)
743 struct xfs_log_item *mlip = xfs_ail_min(ailp);
745 trace_xfs_ail_delete(lip, mlip->li_lsn, lip->li_lsn);
746 xfs_ail_delete(ailp, lip);
747 xfs_clear_li_failed(lip);
748 clear_bit(XFS_LI_IN_AIL, &lip->li_flags);
755 * Remove a log items from the AIL
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.
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
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.
772 * This function must be called with the AIL lock held. The lock is dropped
776 xfs_trans_ail_delete(
777 struct xfs_ail *ailp,
778 struct xfs_log_item *lip,
779 int shutdown_type) __releases(ailp->ail_lock)
781 struct xfs_mount *mp = ailp->ail_mount;
784 if (!test_bit(XFS_LI_IN_AIL, &lip->li_flags)) {
785 spin_unlock(&ailp->ail_lock);
786 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",
790 xfs_force_shutdown(mp, shutdown_type);
795 mlip_changed = xfs_ail_delete_one(ailp, lip);
797 if (!XFS_FORCED_SHUTDOWN(mp))
798 xlog_assign_tail_lsn_locked(mp);
799 if (list_empty(&ailp->ail_head))
800 wake_up_all(&ailp->ail_empty);
803 spin_unlock(&ailp->ail_lock);
805 xfs_log_space_wake(ailp->ail_mount);
812 struct xfs_ail *ailp;
814 ailp = kmem_zalloc(sizeof(struct xfs_ail), KM_MAYFAIL);
818 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);
825 ailp->ail_task = kthread_run(xfsaild, ailp, "xfsaild/%s",
826 ailp->ail_mount->m_fsname);
827 if (IS_ERR(ailp->ail_task))
839 xfs_trans_ail_destroy(
842 struct xfs_ail *ailp = mp->m_ail;
844 kthread_stop(ailp->ail_task);