Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
0004  * Copyright (c) 2008 Dave Chinner
0005  * All Rights Reserved.
0006  */
0007 #include "xfs.h"
0008 #include "xfs_fs.h"
0009 #include "xfs_shared.h"
0010 #include "xfs_format.h"
0011 #include "xfs_log_format.h"
0012 #include "xfs_trans_resv.h"
0013 #include "xfs_mount.h"
0014 #include "xfs_trans.h"
0015 #include "xfs_trans_priv.h"
0016 #include "xfs_trace.h"
0017 #include "xfs_errortag.h"
0018 #include "xfs_error.h"
0019 #include "xfs_log.h"
0020 #include "xfs_log_priv.h"
0021 
0022 #ifdef DEBUG
0023 /*
0024  * Check that the list is sorted as it should be.
0025  *
0026  * Called with the ail lock held, but we don't want to assert fail with it
0027  * held otherwise we'll lock everything up and won't be able to debug the
0028  * cause. Hence we sample and check the state under the AIL lock and return if
0029  * everything is fine, otherwise we drop the lock and run the ASSERT checks.
0030  * Asserts may not be fatal, so pick the lock back up and continue onwards.
0031  */
0032 STATIC void
0033 xfs_ail_check(
0034     struct xfs_ail      *ailp,
0035     struct xfs_log_item *lip)
0036     __must_hold(&ailp->ail_lock)
0037 {
0038     struct xfs_log_item *prev_lip;
0039     struct xfs_log_item *next_lip;
0040     xfs_lsn_t       prev_lsn = NULLCOMMITLSN;
0041     xfs_lsn_t       next_lsn = NULLCOMMITLSN;
0042     xfs_lsn_t       lsn;
0043     bool            in_ail;
0044 
0045 
0046     if (list_empty(&ailp->ail_head))
0047         return;
0048 
0049     /*
0050      * Sample then check the next and previous entries are valid.
0051      */
0052     in_ail = test_bit(XFS_LI_IN_AIL, &lip->li_flags);
0053     prev_lip = list_entry(lip->li_ail.prev, struct xfs_log_item, li_ail);
0054     if (&prev_lip->li_ail != &ailp->ail_head)
0055         prev_lsn = prev_lip->li_lsn;
0056     next_lip = list_entry(lip->li_ail.next, struct xfs_log_item, li_ail);
0057     if (&next_lip->li_ail != &ailp->ail_head)
0058         next_lsn = next_lip->li_lsn;
0059     lsn = lip->li_lsn;
0060 
0061     if (in_ail &&
0062         (prev_lsn == NULLCOMMITLSN || XFS_LSN_CMP(prev_lsn, lsn) <= 0) &&
0063         (next_lsn == NULLCOMMITLSN || XFS_LSN_CMP(next_lsn, lsn) >= 0))
0064         return;
0065 
0066     spin_unlock(&ailp->ail_lock);
0067     ASSERT(in_ail);
0068     ASSERT(prev_lsn == NULLCOMMITLSN || XFS_LSN_CMP(prev_lsn, lsn) <= 0);
0069     ASSERT(next_lsn == NULLCOMMITLSN || XFS_LSN_CMP(next_lsn, lsn) >= 0);
0070     spin_lock(&ailp->ail_lock);
0071 }
0072 #else /* !DEBUG */
0073 #define xfs_ail_check(a,l)
0074 #endif /* DEBUG */
0075 
0076 /*
0077  * Return a pointer to the last item in the AIL.  If the AIL is empty, then
0078  * return NULL.
0079  */
0080 static struct xfs_log_item *
0081 xfs_ail_max(
0082     struct xfs_ail  *ailp)
0083 {
0084     if (list_empty(&ailp->ail_head))
0085         return NULL;
0086 
0087     return list_entry(ailp->ail_head.prev, struct xfs_log_item, li_ail);
0088 }
0089 
0090 /*
0091  * Return a pointer to the item which follows the given item in the AIL.  If
0092  * the given item is the last item in the list, then return NULL.
0093  */
0094 static struct xfs_log_item *
0095 xfs_ail_next(
0096     struct xfs_ail      *ailp,
0097     struct xfs_log_item *lip)
0098 {
0099     if (lip->li_ail.next == &ailp->ail_head)
0100         return NULL;
0101 
0102     return list_first_entry(&lip->li_ail, struct xfs_log_item, li_ail);
0103 }
0104 
0105 /*
0106  * This is called by the log manager code to determine the LSN of the tail of
0107  * the log.  This is exactly the LSN of the first item in the AIL.  If the AIL
0108  * is empty, then this function returns 0.
0109  *
0110  * We need the AIL lock in order to get a coherent read of the lsn of the last
0111  * item in the AIL.
0112  */
0113 static xfs_lsn_t
0114 __xfs_ail_min_lsn(
0115     struct xfs_ail      *ailp)
0116 {
0117     struct xfs_log_item *lip = xfs_ail_min(ailp);
0118 
0119     if (lip)
0120         return lip->li_lsn;
0121     return 0;
0122 }
0123 
0124 xfs_lsn_t
0125 xfs_ail_min_lsn(
0126     struct xfs_ail      *ailp)
0127 {
0128     xfs_lsn_t       lsn;
0129 
0130     spin_lock(&ailp->ail_lock);
0131     lsn = __xfs_ail_min_lsn(ailp);
0132     spin_unlock(&ailp->ail_lock);
0133 
0134     return lsn;
0135 }
0136 
0137 /*
0138  * Return the maximum lsn held in the AIL, or zero if the AIL is empty.
0139  */
0140 static xfs_lsn_t
0141 xfs_ail_max_lsn(
0142     struct xfs_ail      *ailp)
0143 {
0144     xfs_lsn_t           lsn = 0;
0145     struct xfs_log_item *lip;
0146 
0147     spin_lock(&ailp->ail_lock);
0148     lip = xfs_ail_max(ailp);
0149     if (lip)
0150         lsn = lip->li_lsn;
0151     spin_unlock(&ailp->ail_lock);
0152 
0153     return lsn;
0154 }
0155 
0156 /*
0157  * The cursor keeps track of where our current traversal is up to by tracking
0158  * the next item in the list for us. However, for this to be safe, removing an
0159  * object from the AIL needs to invalidate any cursor that points to it. hence
0160  * the traversal cursor needs to be linked to the struct xfs_ail so that
0161  * deletion can search all the active cursors for invalidation.
0162  */
0163 STATIC void
0164 xfs_trans_ail_cursor_init(
0165     struct xfs_ail      *ailp,
0166     struct xfs_ail_cursor   *cur)
0167 {
0168     cur->item = NULL;
0169     list_add_tail(&cur->list, &ailp->ail_cursors);
0170 }
0171 
0172 /*
0173  * Get the next item in the traversal and advance the cursor.  If the cursor
0174  * was invalidated (indicated by a lip of 1), restart the traversal.
0175  */
0176 struct xfs_log_item *
0177 xfs_trans_ail_cursor_next(
0178     struct xfs_ail      *ailp,
0179     struct xfs_ail_cursor   *cur)
0180 {
0181     struct xfs_log_item *lip = cur->item;
0182 
0183     if ((uintptr_t)lip & 1)
0184         lip = xfs_ail_min(ailp);
0185     if (lip)
0186         cur->item = xfs_ail_next(ailp, lip);
0187     return lip;
0188 }
0189 
0190 /*
0191  * When the traversal is complete, we need to remove the cursor from the list
0192  * of traversing cursors.
0193  */
0194 void
0195 xfs_trans_ail_cursor_done(
0196     struct xfs_ail_cursor   *cur)
0197 {
0198     cur->item = NULL;
0199     list_del_init(&cur->list);
0200 }
0201 
0202 /*
0203  * Invalidate any cursor that is pointing to this item. This is called when an
0204  * item is removed from the AIL. Any cursor pointing to this object is now
0205  * invalid and the traversal needs to be terminated so it doesn't reference a
0206  * freed object. We set the low bit of the cursor item pointer so we can
0207  * distinguish between an invalidation and the end of the list when getting the
0208  * next item from the cursor.
0209  */
0210 STATIC void
0211 xfs_trans_ail_cursor_clear(
0212     struct xfs_ail      *ailp,
0213     struct xfs_log_item *lip)
0214 {
0215     struct xfs_ail_cursor   *cur;
0216 
0217     list_for_each_entry(cur, &ailp->ail_cursors, list) {
0218         if (cur->item == lip)
0219             cur->item = (struct xfs_log_item *)
0220                     ((uintptr_t)cur->item | 1);
0221     }
0222 }
0223 
0224 /*
0225  * Find the first item in the AIL with the given @lsn by searching in ascending
0226  * LSN order and initialise the cursor to point to the next item for a
0227  * ascending traversal.  Pass a @lsn of zero to initialise the cursor to the
0228  * first item in the AIL. Returns NULL if the list is empty.
0229  */
0230 struct xfs_log_item *
0231 xfs_trans_ail_cursor_first(
0232     struct xfs_ail      *ailp,
0233     struct xfs_ail_cursor   *cur,
0234     xfs_lsn_t       lsn)
0235 {
0236     struct xfs_log_item *lip;
0237 
0238     xfs_trans_ail_cursor_init(ailp, cur);
0239 
0240     if (lsn == 0) {
0241         lip = xfs_ail_min(ailp);
0242         goto out;
0243     }
0244 
0245     list_for_each_entry(lip, &ailp->ail_head, li_ail) {
0246         if (XFS_LSN_CMP(lip->li_lsn, lsn) >= 0)
0247             goto out;
0248     }
0249     return NULL;
0250 
0251 out:
0252     if (lip)
0253         cur->item = xfs_ail_next(ailp, lip);
0254     return lip;
0255 }
0256 
0257 static struct xfs_log_item *
0258 __xfs_trans_ail_cursor_last(
0259     struct xfs_ail      *ailp,
0260     xfs_lsn_t       lsn)
0261 {
0262     struct xfs_log_item *lip;
0263 
0264     list_for_each_entry_reverse(lip, &ailp->ail_head, li_ail) {
0265         if (XFS_LSN_CMP(lip->li_lsn, lsn) <= 0)
0266             return lip;
0267     }
0268     return NULL;
0269 }
0270 
0271 /*
0272  * Find the last item in the AIL with the given @lsn by searching in descending
0273  * LSN order and initialise the cursor to point to that item.  If there is no
0274  * item with the value of @lsn, then it sets the cursor to the last item with an
0275  * LSN lower than @lsn.  Returns NULL if the list is empty.
0276  */
0277 struct xfs_log_item *
0278 xfs_trans_ail_cursor_last(
0279     struct xfs_ail      *ailp,
0280     struct xfs_ail_cursor   *cur,
0281     xfs_lsn_t       lsn)
0282 {
0283     xfs_trans_ail_cursor_init(ailp, cur);
0284     cur->item = __xfs_trans_ail_cursor_last(ailp, lsn);
0285     return cur->item;
0286 }
0287 
0288 /*
0289  * Splice the log item list into the AIL at the given LSN. We splice to the
0290  * tail of the given LSN to maintain insert order for push traversals. The
0291  * cursor is optional, allowing repeated updates to the same LSN to avoid
0292  * repeated traversals.  This should not be called with an empty list.
0293  */
0294 static void
0295 xfs_ail_splice(
0296     struct xfs_ail      *ailp,
0297     struct xfs_ail_cursor   *cur,
0298     struct list_head    *list,
0299     xfs_lsn_t       lsn)
0300 {
0301     struct xfs_log_item *lip;
0302 
0303     ASSERT(!list_empty(list));
0304 
0305     /*
0306      * Use the cursor to determine the insertion point if one is
0307      * provided.  If not, or if the one we got is not valid,
0308      * find the place in the AIL where the items belong.
0309      */
0310     lip = cur ? cur->item : NULL;
0311     if (!lip || (uintptr_t)lip & 1)
0312         lip = __xfs_trans_ail_cursor_last(ailp, lsn);
0313 
0314     /*
0315      * If a cursor is provided, we know we're processing the AIL
0316      * in lsn order, and future items to be spliced in will
0317      * follow the last one being inserted now.  Update the
0318      * cursor to point to that last item, now while we have a
0319      * reliable pointer to it.
0320      */
0321     if (cur)
0322         cur->item = list_entry(list->prev, struct xfs_log_item, li_ail);
0323 
0324     /*
0325      * Finally perform the splice.  Unless the AIL was empty,
0326      * lip points to the item in the AIL _after_ which the new
0327      * items should go.  If lip is null the AIL was empty, so
0328      * the new items go at the head of the AIL.
0329      */
0330     if (lip)
0331         list_splice(list, &lip->li_ail);
0332     else
0333         list_splice(list, &ailp->ail_head);
0334 }
0335 
0336 /*
0337  * Delete the given item from the AIL.  Return a pointer to the item.
0338  */
0339 static void
0340 xfs_ail_delete(
0341     struct xfs_ail      *ailp,
0342     struct xfs_log_item *lip)
0343 {
0344     xfs_ail_check(ailp, lip);
0345     list_del(&lip->li_ail);
0346     xfs_trans_ail_cursor_clear(ailp, lip);
0347 }
0348 
0349 /*
0350  * Requeue a failed buffer for writeback.
0351  *
0352  * We clear the log item failed state here as well, but we have to be careful
0353  * about reference counts because the only active reference counts on the buffer
0354  * may be the failed log items. Hence if we clear the log item failed state
0355  * before queuing the buffer for IO we can release all active references to
0356  * the buffer and free it, leading to use after free problems in
0357  * xfs_buf_delwri_queue. It makes no difference to the buffer or log items which
0358  * order we process them in - the buffer is locked, and we own the buffer list
0359  * so nothing on them is going to change while we are performing this action.
0360  *
0361  * Hence we can safely queue the buffer for IO before we clear the failed log
0362  * item state, therefore  always having an active reference to the buffer and
0363  * avoiding the transient zero-reference state that leads to use-after-free.
0364  */
0365 static inline int
0366 xfsaild_resubmit_item(
0367     struct xfs_log_item *lip,
0368     struct list_head    *buffer_list)
0369 {
0370     struct xfs_buf      *bp = lip->li_buf;
0371 
0372     if (!xfs_buf_trylock(bp))
0373         return XFS_ITEM_LOCKED;
0374 
0375     if (!xfs_buf_delwri_queue(bp, buffer_list)) {
0376         xfs_buf_unlock(bp);
0377         return XFS_ITEM_FLUSHING;
0378     }
0379 
0380     /* protected by ail_lock */
0381     list_for_each_entry(lip, &bp->b_li_list, li_bio_list) {
0382         if (bp->b_flags & _XBF_INODES)
0383             clear_bit(XFS_LI_FAILED, &lip->li_flags);
0384         else
0385             xfs_clear_li_failed(lip);
0386     }
0387 
0388     xfs_buf_unlock(bp);
0389     return XFS_ITEM_SUCCESS;
0390 }
0391 
0392 static inline uint
0393 xfsaild_push_item(
0394     struct xfs_ail      *ailp,
0395     struct xfs_log_item *lip)
0396 {
0397     /*
0398      * If log item pinning is enabled, skip the push and track the item as
0399      * pinned. This can help induce head-behind-tail conditions.
0400      */
0401     if (XFS_TEST_ERROR(false, ailp->ail_log->l_mp, XFS_ERRTAG_LOG_ITEM_PIN))
0402         return XFS_ITEM_PINNED;
0403 
0404     /*
0405      * Consider the item pinned if a push callback is not defined so the
0406      * caller will force the log. This should only happen for intent items
0407      * as they are unpinned once the associated done item is committed to
0408      * the on-disk log.
0409      */
0410     if (!lip->li_ops->iop_push)
0411         return XFS_ITEM_PINNED;
0412     if (test_bit(XFS_LI_FAILED, &lip->li_flags))
0413         return xfsaild_resubmit_item(lip, &ailp->ail_buf_list);
0414     return lip->li_ops->iop_push(lip, &ailp->ail_buf_list);
0415 }
0416 
0417 static long
0418 xfsaild_push(
0419     struct xfs_ail      *ailp)
0420 {
0421     struct xfs_mount    *mp = ailp->ail_log->l_mp;
0422     struct xfs_ail_cursor   cur;
0423     struct xfs_log_item *lip;
0424     xfs_lsn_t       lsn;
0425     xfs_lsn_t       target;
0426     long            tout;
0427     int         stuck = 0;
0428     int         flushing = 0;
0429     int         count = 0;
0430 
0431     /*
0432      * If we encountered pinned items or did not finish writing out all
0433      * buffers the last time we ran, force a background CIL push to get the
0434      * items unpinned in the near future. We do not wait on the CIL push as
0435      * that could stall us for seconds if there is enough background IO
0436      * load. Stalling for that long when the tail of the log is pinned and
0437      * needs flushing will hard stop the transaction subsystem when log
0438      * space runs out.
0439      */
0440     if (ailp->ail_log_flush && ailp->ail_last_pushed_lsn == 0 &&
0441         (!list_empty_careful(&ailp->ail_buf_list) ||
0442          xfs_ail_min_lsn(ailp))) {
0443         ailp->ail_log_flush = 0;
0444 
0445         XFS_STATS_INC(mp, xs_push_ail_flush);
0446         xlog_cil_flush(ailp->ail_log);
0447     }
0448 
0449     spin_lock(&ailp->ail_lock);
0450 
0451     /*
0452      * If we have a sync push waiter, we always have to push till the AIL is
0453      * empty. Update the target to point to the end of the AIL so that
0454      * capture updates that occur after the sync push waiter has gone to
0455      * sleep.
0456      */
0457     if (waitqueue_active(&ailp->ail_empty)) {
0458         lip = xfs_ail_max(ailp);
0459         if (lip)
0460             target = lip->li_lsn;
0461     } else {
0462         /* barrier matches the ail_target update in xfs_ail_push() */
0463         smp_rmb();
0464         target = ailp->ail_target;
0465         ailp->ail_target_prev = target;
0466     }
0467 
0468     /* we're done if the AIL is empty or our push has reached the end */
0469     lip = xfs_trans_ail_cursor_first(ailp, &cur, ailp->ail_last_pushed_lsn);
0470     if (!lip)
0471         goto out_done;
0472 
0473     XFS_STATS_INC(mp, xs_push_ail);
0474 
0475     lsn = lip->li_lsn;
0476     while ((XFS_LSN_CMP(lip->li_lsn, target) <= 0)) {
0477         int lock_result;
0478 
0479         /*
0480          * Note that iop_push may unlock and reacquire the AIL lock.  We
0481          * rely on the AIL cursor implementation to be able to deal with
0482          * the dropped lock.
0483          */
0484         lock_result = xfsaild_push_item(ailp, lip);
0485         switch (lock_result) {
0486         case XFS_ITEM_SUCCESS:
0487             XFS_STATS_INC(mp, xs_push_ail_success);
0488             trace_xfs_ail_push(lip);
0489 
0490             ailp->ail_last_pushed_lsn = lsn;
0491             break;
0492 
0493         case XFS_ITEM_FLUSHING:
0494             /*
0495              * The item or its backing buffer is already being
0496              * flushed.  The typical reason for that is that an
0497              * inode buffer is locked because we already pushed the
0498              * updates to it as part of inode clustering.
0499              *
0500              * We do not want to stop flushing just because lots
0501              * of items are already being flushed, but we need to
0502              * re-try the flushing relatively soon if most of the
0503              * AIL is being flushed.
0504              */
0505             XFS_STATS_INC(mp, xs_push_ail_flushing);
0506             trace_xfs_ail_flushing(lip);
0507 
0508             flushing++;
0509             ailp->ail_last_pushed_lsn = lsn;
0510             break;
0511 
0512         case XFS_ITEM_PINNED:
0513             XFS_STATS_INC(mp, xs_push_ail_pinned);
0514             trace_xfs_ail_pinned(lip);
0515 
0516             stuck++;
0517             ailp->ail_log_flush++;
0518             break;
0519         case XFS_ITEM_LOCKED:
0520             XFS_STATS_INC(mp, xs_push_ail_locked);
0521             trace_xfs_ail_locked(lip);
0522 
0523             stuck++;
0524             break;
0525         default:
0526             ASSERT(0);
0527             break;
0528         }
0529 
0530         count++;
0531 
0532         /*
0533          * Are there too many items we can't do anything with?
0534          *
0535          * If we are skipping too many items because we can't flush
0536          * them or they are already being flushed, we back off and
0537          * given them time to complete whatever operation is being
0538          * done. i.e. remove pressure from the AIL while we can't make
0539          * progress so traversals don't slow down further inserts and
0540          * removals to/from the AIL.
0541          *
0542          * The value of 100 is an arbitrary magic number based on
0543          * observation.
0544          */
0545         if (stuck > 100)
0546             break;
0547 
0548         lip = xfs_trans_ail_cursor_next(ailp, &cur);
0549         if (lip == NULL)
0550             break;
0551         lsn = lip->li_lsn;
0552     }
0553 
0554 out_done:
0555     xfs_trans_ail_cursor_done(&cur);
0556     spin_unlock(&ailp->ail_lock);
0557 
0558     if (xfs_buf_delwri_submit_nowait(&ailp->ail_buf_list))
0559         ailp->ail_log_flush++;
0560 
0561     if (!count || XFS_LSN_CMP(lsn, target) >= 0) {
0562         /*
0563          * We reached the target or the AIL is empty, so wait a bit
0564          * longer for I/O to complete and remove pushed items from the
0565          * AIL before we start the next scan from the start of the AIL.
0566          */
0567         tout = 50;
0568         ailp->ail_last_pushed_lsn = 0;
0569     } else if (((stuck + flushing) * 100) / count > 90) {
0570         /*
0571          * Either there is a lot of contention on the AIL or we are
0572          * stuck due to operations in progress. "Stuck" in this case
0573          * is defined as >90% of the items we tried to push were stuck.
0574          *
0575          * Backoff a bit more to allow some I/O to complete before
0576          * restarting from the start of the AIL. This prevents us from
0577          * spinning on the same items, and if they are pinned will all
0578          * the restart to issue a log force to unpin the stuck items.
0579          */
0580         tout = 20;
0581         ailp->ail_last_pushed_lsn = 0;
0582     } else {
0583         /*
0584          * Assume we have more work to do in a short while.
0585          */
0586         tout = 10;
0587     }
0588 
0589     return tout;
0590 }
0591 
0592 static int
0593 xfsaild(
0594     void        *data)
0595 {
0596     struct xfs_ail  *ailp = data;
0597     long        tout = 0;   /* milliseconds */
0598     unsigned int    noreclaim_flag;
0599 
0600     noreclaim_flag = memalloc_noreclaim_save();
0601     set_freezable();
0602 
0603     while (1) {
0604         if (tout && tout <= 20)
0605             set_current_state(TASK_KILLABLE);
0606         else
0607             set_current_state(TASK_INTERRUPTIBLE);
0608 
0609         /*
0610          * Check kthread_should_stop() after we set the task state to
0611          * guarantee that we either see the stop bit and exit or the
0612          * task state is reset to runnable such that it's not scheduled
0613          * out indefinitely and detects the stop bit at next iteration.
0614          * A memory barrier is included in above task state set to
0615          * serialize again kthread_stop().
0616          */
0617         if (kthread_should_stop()) {
0618             __set_current_state(TASK_RUNNING);
0619 
0620             /*
0621              * The caller forces out the AIL before stopping the
0622              * thread in the common case, which means the delwri
0623              * queue is drained. In the shutdown case, the queue may
0624              * still hold relogged buffers that haven't been
0625              * submitted because they were pinned since added to the
0626              * queue.
0627              *
0628              * Log I/O error processing stales the underlying buffer
0629              * and clears the delwri state, expecting the buf to be
0630              * removed on the next submission attempt. That won't
0631              * happen if we're shutting down, so this is the last
0632              * opportunity to release such buffers from the queue.
0633              */
0634             ASSERT(list_empty(&ailp->ail_buf_list) ||
0635                    xlog_is_shutdown(ailp->ail_log));
0636             xfs_buf_delwri_cancel(&ailp->ail_buf_list);
0637             break;
0638         }
0639 
0640         spin_lock(&ailp->ail_lock);
0641 
0642         /*
0643          * Idle if the AIL is empty and we are not racing with a target
0644          * update. We check the AIL after we set the task to a sleep
0645          * state to guarantee that we either catch an ail_target update
0646          * or that a wake_up resets the state to TASK_RUNNING.
0647          * Otherwise, we run the risk of sleeping indefinitely.
0648          *
0649          * The barrier matches the ail_target update in xfs_ail_push().
0650          */
0651         smp_rmb();
0652         if (!xfs_ail_min(ailp) &&
0653             ailp->ail_target == ailp->ail_target_prev &&
0654             list_empty(&ailp->ail_buf_list)) {
0655             spin_unlock(&ailp->ail_lock);
0656             freezable_schedule();
0657             tout = 0;
0658             continue;
0659         }
0660         spin_unlock(&ailp->ail_lock);
0661 
0662         if (tout)
0663             freezable_schedule_timeout(msecs_to_jiffies(tout));
0664 
0665         __set_current_state(TASK_RUNNING);
0666 
0667         try_to_freeze();
0668 
0669         tout = xfsaild_push(ailp);
0670     }
0671 
0672     memalloc_noreclaim_restore(noreclaim_flag);
0673     return 0;
0674 }
0675 
0676 /*
0677  * This routine is called to move the tail of the AIL forward.  It does this by
0678  * trying to flush items in the AIL whose lsns are below the given
0679  * threshold_lsn.
0680  *
0681  * The push is run asynchronously in a workqueue, which means the caller needs
0682  * to handle waiting on the async flush for space to become available.
0683  * We don't want to interrupt any push that is in progress, hence we only queue
0684  * work if we set the pushing bit appropriately.
0685  *
0686  * We do this unlocked - we only need to know whether there is anything in the
0687  * AIL at the time we are called. We don't need to access the contents of
0688  * any of the objects, so the lock is not needed.
0689  */
0690 void
0691 xfs_ail_push(
0692     struct xfs_ail      *ailp,
0693     xfs_lsn_t       threshold_lsn)
0694 {
0695     struct xfs_log_item *lip;
0696 
0697     lip = xfs_ail_min(ailp);
0698     if (!lip || xlog_is_shutdown(ailp->ail_log) ||
0699         XFS_LSN_CMP(threshold_lsn, ailp->ail_target) <= 0)
0700         return;
0701 
0702     /*
0703      * Ensure that the new target is noticed in push code before it clears
0704      * the XFS_AIL_PUSHING_BIT.
0705      */
0706     smp_wmb();
0707     xfs_trans_ail_copy_lsn(ailp, &ailp->ail_target, &threshold_lsn);
0708     smp_wmb();
0709 
0710     wake_up_process(ailp->ail_task);
0711 }
0712 
0713 /*
0714  * Push out all items in the AIL immediately
0715  */
0716 void
0717 xfs_ail_push_all(
0718     struct xfs_ail  *ailp)
0719 {
0720     xfs_lsn_t       threshold_lsn = xfs_ail_max_lsn(ailp);
0721 
0722     if (threshold_lsn)
0723         xfs_ail_push(ailp, threshold_lsn);
0724 }
0725 
0726 /*
0727  * Push out all items in the AIL immediately and wait until the AIL is empty.
0728  */
0729 void
0730 xfs_ail_push_all_sync(
0731     struct xfs_ail  *ailp)
0732 {
0733     struct xfs_log_item *lip;
0734     DEFINE_WAIT(wait);
0735 
0736     spin_lock(&ailp->ail_lock);
0737     while ((lip = xfs_ail_max(ailp)) != NULL) {
0738         prepare_to_wait(&ailp->ail_empty, &wait, TASK_UNINTERRUPTIBLE);
0739         wake_up_process(ailp->ail_task);
0740         spin_unlock(&ailp->ail_lock);
0741         schedule();
0742         spin_lock(&ailp->ail_lock);
0743     }
0744     spin_unlock(&ailp->ail_lock);
0745 
0746     finish_wait(&ailp->ail_empty, &wait);
0747 }
0748 
0749 void
0750 xfs_ail_update_finish(
0751     struct xfs_ail      *ailp,
0752     xfs_lsn_t       old_lsn) __releases(ailp->ail_lock)
0753 {
0754     struct xlog     *log = ailp->ail_log;
0755 
0756     /* if the tail lsn hasn't changed, don't do updates or wakeups. */
0757     if (!old_lsn || old_lsn == __xfs_ail_min_lsn(ailp)) {
0758         spin_unlock(&ailp->ail_lock);
0759         return;
0760     }
0761 
0762     if (!xlog_is_shutdown(log))
0763         xlog_assign_tail_lsn_locked(log->l_mp);
0764 
0765     if (list_empty(&ailp->ail_head))
0766         wake_up_all(&ailp->ail_empty);
0767     spin_unlock(&ailp->ail_lock);
0768     xfs_log_space_wake(log->l_mp);
0769 }
0770 
0771 /*
0772  * xfs_trans_ail_update - bulk AIL insertion operation.
0773  *
0774  * @xfs_trans_ail_update takes an array of log items that all need to be
0775  * positioned at the same LSN in the AIL. If an item is not in the AIL, it will
0776  * be added.  Otherwise, it will be repositioned  by removing it and re-adding
0777  * it to the AIL. If we move the first item in the AIL, update the log tail to
0778  * match the new minimum LSN in the AIL.
0779  *
0780  * This function takes the AIL lock once to execute the update operations on
0781  * all the items in the array, and as such should not be called with the AIL
0782  * lock held. As a result, once we have the AIL lock, we need to check each log
0783  * item LSN to confirm it needs to be moved forward in the AIL.
0784  *
0785  * To optimise the insert operation, we delete all the items from the AIL in
0786  * the first pass, moving them into a temporary list, then splice the temporary
0787  * list into the correct position in the AIL. This avoids needing to do an
0788  * insert operation on every item.
0789  *
0790  * This function must be called with the AIL lock held.  The lock is dropped
0791  * before returning.
0792  */
0793 void
0794 xfs_trans_ail_update_bulk(
0795     struct xfs_ail      *ailp,
0796     struct xfs_ail_cursor   *cur,
0797     struct xfs_log_item **log_items,
0798     int         nr_items,
0799     xfs_lsn_t       lsn) __releases(ailp->ail_lock)
0800 {
0801     struct xfs_log_item *mlip;
0802     xfs_lsn_t       tail_lsn = 0;
0803     int         i;
0804     LIST_HEAD(tmp);
0805 
0806     ASSERT(nr_items > 0);       /* Not required, but true. */
0807     mlip = xfs_ail_min(ailp);
0808 
0809     for (i = 0; i < nr_items; i++) {
0810         struct xfs_log_item *lip = log_items[i];
0811         if (test_and_set_bit(XFS_LI_IN_AIL, &lip->li_flags)) {
0812             /* check if we really need to move the item */
0813             if (XFS_LSN_CMP(lsn, lip->li_lsn) <= 0)
0814                 continue;
0815 
0816             trace_xfs_ail_move(lip, lip->li_lsn, lsn);
0817             if (mlip == lip && !tail_lsn)
0818                 tail_lsn = lip->li_lsn;
0819 
0820             xfs_ail_delete(ailp, lip);
0821         } else {
0822             trace_xfs_ail_insert(lip, 0, lsn);
0823         }
0824         lip->li_lsn = lsn;
0825         list_add(&lip->li_ail, &tmp);
0826     }
0827 
0828     if (!list_empty(&tmp))
0829         xfs_ail_splice(ailp, cur, &tmp, lsn);
0830 
0831     xfs_ail_update_finish(ailp, tail_lsn);
0832 }
0833 
0834 /* Insert a log item into the AIL. */
0835 void
0836 xfs_trans_ail_insert(
0837     struct xfs_ail      *ailp,
0838     struct xfs_log_item *lip,
0839     xfs_lsn_t       lsn)
0840 {
0841     spin_lock(&ailp->ail_lock);
0842     xfs_trans_ail_update_bulk(ailp, NULL, &lip, 1, lsn);
0843 }
0844 
0845 /*
0846  * Delete one log item from the AIL.
0847  *
0848  * If this item was at the tail of the AIL, return the LSN of the log item so
0849  * that we can use it to check if the LSN of the tail of the log has moved
0850  * when finishing up the AIL delete process in xfs_ail_update_finish().
0851  */
0852 xfs_lsn_t
0853 xfs_ail_delete_one(
0854     struct xfs_ail      *ailp,
0855     struct xfs_log_item *lip)
0856 {
0857     struct xfs_log_item *mlip = xfs_ail_min(ailp);
0858     xfs_lsn_t       lsn = lip->li_lsn;
0859 
0860     trace_xfs_ail_delete(lip, mlip->li_lsn, lip->li_lsn);
0861     xfs_ail_delete(ailp, lip);
0862     clear_bit(XFS_LI_IN_AIL, &lip->li_flags);
0863     lip->li_lsn = 0;
0864 
0865     if (mlip == lip)
0866         return lsn;
0867     return 0;
0868 }
0869 
0870 void
0871 xfs_trans_ail_delete(
0872     struct xfs_log_item *lip,
0873     int         shutdown_type)
0874 {
0875     struct xfs_ail      *ailp = lip->li_ailp;
0876     struct xlog     *log = ailp->ail_log;
0877     xfs_lsn_t       tail_lsn;
0878 
0879     spin_lock(&ailp->ail_lock);
0880     if (!test_bit(XFS_LI_IN_AIL, &lip->li_flags)) {
0881         spin_unlock(&ailp->ail_lock);
0882         if (shutdown_type && !xlog_is_shutdown(log)) {
0883             xfs_alert_tag(log->l_mp, XFS_PTAG_AILDELETE,
0884     "%s: attempting to delete a log item that is not in the AIL",
0885                     __func__);
0886             xlog_force_shutdown(log, shutdown_type);
0887         }
0888         return;
0889     }
0890 
0891     /* xfs_ail_update_finish() drops the AIL lock */
0892     xfs_clear_li_failed(lip);
0893     tail_lsn = xfs_ail_delete_one(ailp, lip);
0894     xfs_ail_update_finish(ailp, tail_lsn);
0895 }
0896 
0897 int
0898 xfs_trans_ail_init(
0899     xfs_mount_t *mp)
0900 {
0901     struct xfs_ail  *ailp;
0902 
0903     ailp = kmem_zalloc(sizeof(struct xfs_ail), KM_MAYFAIL);
0904     if (!ailp)
0905         return -ENOMEM;
0906 
0907     ailp->ail_log = mp->m_log;
0908     INIT_LIST_HEAD(&ailp->ail_head);
0909     INIT_LIST_HEAD(&ailp->ail_cursors);
0910     spin_lock_init(&ailp->ail_lock);
0911     INIT_LIST_HEAD(&ailp->ail_buf_list);
0912     init_waitqueue_head(&ailp->ail_empty);
0913 
0914     ailp->ail_task = kthread_run(xfsaild, ailp, "xfsaild/%s",
0915                 mp->m_super->s_id);
0916     if (IS_ERR(ailp->ail_task))
0917         goto out_free_ailp;
0918 
0919     mp->m_ail = ailp;
0920     return 0;
0921 
0922 out_free_ailp:
0923     kmem_free(ailp);
0924     return -ENOMEM;
0925 }
0926 
0927 void
0928 xfs_trans_ail_destroy(
0929     xfs_mount_t *mp)
0930 {
0931     struct xfs_ail  *ailp = mp->m_ail;
0932 
0933     kthread_stop(ailp->ail_task);
0934     kmem_free(ailp);
0935 }