11da177e4SLinus Torvalds /* 27b718769SNathan Scott * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc. 37b718769SNathan Scott * All Rights Reserved. 41da177e4SLinus Torvalds * 57b718769SNathan Scott * This program is free software; you can redistribute it and/or 67b718769SNathan Scott * modify it under the terms of the GNU General Public License as 71da177e4SLinus Torvalds * published by the Free Software Foundation. 81da177e4SLinus Torvalds * 97b718769SNathan Scott * This program is distributed in the hope that it would be useful, 107b718769SNathan Scott * but WITHOUT ANY WARRANTY; without even the implied warranty of 117b718769SNathan Scott * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 127b718769SNathan Scott * GNU General Public License for more details. 131da177e4SLinus Torvalds * 147b718769SNathan Scott * You should have received a copy of the GNU General Public License 157b718769SNathan Scott * along with this program; if not, write the Free Software Foundation, 167b718769SNathan Scott * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 171da177e4SLinus Torvalds */ 181da177e4SLinus Torvalds #include "xfs.h" 19a844f451SNathan Scott #include "xfs_fs.h" 201da177e4SLinus Torvalds #include "xfs_types.h" 211da177e4SLinus Torvalds #include "xfs_log.h" 22a844f451SNathan Scott #include "xfs_inum.h" 231da177e4SLinus Torvalds #include "xfs_trans.h" 241da177e4SLinus Torvalds #include "xfs_sb.h" 25da353b0dSDavid Chinner #include "xfs_ag.h" 261da177e4SLinus Torvalds #include "xfs_dmapi.h" 271da177e4SLinus Torvalds #include "xfs_mount.h" 281da177e4SLinus Torvalds #include "xfs_trans_priv.h" 291da177e4SLinus Torvalds #include "xfs_error.h" 301da177e4SLinus Torvalds 31*82fa9012SDavid Chinner STATIC void xfs_ail_insert(struct xfs_ail *, xfs_log_item_t *); 32*82fa9012SDavid Chinner STATIC xfs_log_item_t * xfs_ail_delete(struct xfs_ail *, xfs_log_item_t *); 33*82fa9012SDavid Chinner STATIC xfs_log_item_t * xfs_ail_min(struct xfs_ail *); 34*82fa9012SDavid Chinner STATIC xfs_log_item_t * xfs_ail_next(struct xfs_ail *, xfs_log_item_t *); 351da177e4SLinus Torvalds 361da177e4SLinus Torvalds #ifdef DEBUG 37*82fa9012SDavid Chinner STATIC void xfs_ail_check(struct xfs_ail *, xfs_log_item_t *); 381da177e4SLinus Torvalds #else 39de08dbc1SDavid Chinner #define xfs_ail_check(a,l) 401da177e4SLinus Torvalds #endif /* DEBUG */ 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds /* 441da177e4SLinus Torvalds * This is called by the log manager code to determine the LSN 451da177e4SLinus Torvalds * of the tail of the log. This is exactly the LSN of the first 461da177e4SLinus Torvalds * item in the AIL. If the AIL is empty, then this function 471da177e4SLinus Torvalds * returns 0. 481da177e4SLinus Torvalds * 491da177e4SLinus Torvalds * We need the AIL lock in order to get a coherent read of the 501da177e4SLinus Torvalds * lsn of the last item in the AIL. 511da177e4SLinus Torvalds */ 521da177e4SLinus Torvalds xfs_lsn_t 531da177e4SLinus Torvalds xfs_trans_tail_ail( 541da177e4SLinus Torvalds xfs_mount_t *mp) 551da177e4SLinus Torvalds { 561da177e4SLinus Torvalds xfs_lsn_t lsn; 571da177e4SLinus Torvalds xfs_log_item_t *lip; 581da177e4SLinus Torvalds 59287f3dadSDonald Douwsma spin_lock(&mp->m_ail_lock); 60*82fa9012SDavid Chinner lip = xfs_ail_min(mp->m_ail); 611da177e4SLinus Torvalds if (lip == NULL) { 621da177e4SLinus Torvalds lsn = (xfs_lsn_t)0; 631da177e4SLinus Torvalds } else { 641da177e4SLinus Torvalds lsn = lip->li_lsn; 651da177e4SLinus Torvalds } 66287f3dadSDonald Douwsma spin_unlock(&mp->m_ail_lock); 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds return lsn; 691da177e4SLinus Torvalds } 701da177e4SLinus Torvalds 711da177e4SLinus Torvalds /* 721da177e4SLinus Torvalds * xfs_trans_push_ail 731da177e4SLinus Torvalds * 74249a8c11SDavid Chinner * This routine is called to move the tail of the AIL forward. It does this by 75249a8c11SDavid Chinner * trying to flush items in the AIL whose lsns are below the given 76249a8c11SDavid Chinner * threshold_lsn. 771da177e4SLinus Torvalds * 78249a8c11SDavid Chinner * the push is run asynchronously in a separate thread, so we return the tail 79249a8c11SDavid Chinner * of the log right now instead of the tail after the push. This means we will 80249a8c11SDavid Chinner * either continue right away, or we will sleep waiting on the async thread to 81249a8c11SDavid Chinner * do it's work. 82249a8c11SDavid Chinner * 83249a8c11SDavid Chinner * We do this unlocked - we only need to know whether there is anything in the 84249a8c11SDavid Chinner * AIL at the time we are called. We don't need to access the contents of 85249a8c11SDavid Chinner * any of the objects, so the lock is not needed. 861da177e4SLinus Torvalds */ 87249a8c11SDavid Chinner void 881da177e4SLinus Torvalds xfs_trans_push_ail( 891da177e4SLinus Torvalds xfs_mount_t *mp, 901da177e4SLinus Torvalds xfs_lsn_t threshold_lsn) 911da177e4SLinus Torvalds { 92249a8c11SDavid Chinner xfs_log_item_t *lip; 93249a8c11SDavid Chinner 94*82fa9012SDavid Chinner lip = xfs_ail_min(mp->m_ail); 95249a8c11SDavid Chinner if (lip && !XFS_FORCED_SHUTDOWN(mp)) { 96*82fa9012SDavid Chinner if (XFS_LSN_CMP(threshold_lsn, mp->m_ail->xa_target) > 0) 97*82fa9012SDavid Chinner xfsaild_wakeup(mp->m_ail, threshold_lsn); 98249a8c11SDavid Chinner } 99249a8c11SDavid Chinner } 100249a8c11SDavid Chinner 101249a8c11SDavid Chinner /* 102249a8c11SDavid Chinner * Return the item in the AIL with the current lsn. 103249a8c11SDavid Chinner * Return the current tree generation number for use 104249a8c11SDavid Chinner * in calls to xfs_trans_next_ail(). 105249a8c11SDavid Chinner */ 106249a8c11SDavid Chinner STATIC xfs_log_item_t * 107249a8c11SDavid Chinner xfs_trans_first_push_ail( 108249a8c11SDavid Chinner xfs_mount_t *mp, 109249a8c11SDavid Chinner int *gen, 110249a8c11SDavid Chinner xfs_lsn_t lsn) 111249a8c11SDavid Chinner { 112249a8c11SDavid Chinner xfs_log_item_t *lip; 113249a8c11SDavid Chinner 114*82fa9012SDavid Chinner lip = xfs_ail_min(mp->m_ail); 115*82fa9012SDavid Chinner *gen = (int)mp->m_ail->xa_gen; 116249a8c11SDavid Chinner if (lsn == 0) 117249a8c11SDavid Chinner return lip; 118249a8c11SDavid Chinner 119*82fa9012SDavid Chinner list_for_each_entry(lip, &mp->m_ail->xa_ail, li_ail) { 120535f6b37SJosef 'Jeff' Sipek if (XFS_LSN_CMP(lip->li_lsn, lsn) >= 0) 121249a8c11SDavid Chinner return lip; 122249a8c11SDavid Chinner } 123249a8c11SDavid Chinner 124535f6b37SJosef 'Jeff' Sipek return NULL; 125535f6b37SJosef 'Jeff' Sipek } 126535f6b37SJosef 'Jeff' Sipek 127249a8c11SDavid Chinner /* 128249a8c11SDavid Chinner * Function that does the work of pushing on the AIL 129249a8c11SDavid Chinner */ 130249a8c11SDavid Chinner long 131249a8c11SDavid Chinner xfsaild_push( 132*82fa9012SDavid Chinner struct xfs_ail *ailp, 133249a8c11SDavid Chinner xfs_lsn_t *last_lsn) 134249a8c11SDavid Chinner { 135249a8c11SDavid Chinner long tout = 1000; /* milliseconds */ 136249a8c11SDavid Chinner xfs_lsn_t last_pushed_lsn = *last_lsn; 137*82fa9012SDavid Chinner xfs_lsn_t target = ailp->xa_target; 1381da177e4SLinus Torvalds xfs_lsn_t lsn; 1391da177e4SLinus Torvalds xfs_log_item_t *lip; 1401da177e4SLinus Torvalds int gen; 1411da177e4SLinus Torvalds int restarts; 142249a8c11SDavid Chinner int flush_log, count, stuck; 143*82fa9012SDavid Chinner xfs_mount_t *mp = ailp->xa_mount; 1441da177e4SLinus Torvalds 145249a8c11SDavid Chinner #define XFS_TRANS_PUSH_AIL_RESTARTS 10 1461da177e4SLinus Torvalds 147287f3dadSDonald Douwsma spin_lock(&mp->m_ail_lock); 148249a8c11SDavid Chinner lip = xfs_trans_first_push_ail(mp, &gen, *last_lsn); 149249a8c11SDavid Chinner if (!lip || XFS_FORCED_SHUTDOWN(mp)) { 1501da177e4SLinus Torvalds /* 151249a8c11SDavid Chinner * AIL is empty or our push has reached the end. 1521da177e4SLinus Torvalds */ 153287f3dadSDonald Douwsma spin_unlock(&mp->m_ail_lock); 154249a8c11SDavid Chinner last_pushed_lsn = 0; 155249a8c11SDavid Chinner goto out; 1561da177e4SLinus Torvalds } 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds XFS_STATS_INC(xs_push_ail); 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds /* 1611da177e4SLinus Torvalds * While the item we are looking at is below the given threshold 162249a8c11SDavid Chinner * try to flush it out. We'd like not to stop until we've at least 1631da177e4SLinus Torvalds * tried to push on everything in the AIL with an LSN less than 164249a8c11SDavid Chinner * the given threshold. 1651da177e4SLinus Torvalds * 166249a8c11SDavid Chinner * However, we will stop after a certain number of pushes and wait 167249a8c11SDavid Chinner * for a reduced timeout to fire before pushing further. This 168249a8c11SDavid Chinner * prevents use from spinning when we can't do anything or there is 169249a8c11SDavid Chinner * lots of contention on the AIL lists. 170249a8c11SDavid Chinner */ 171249a8c11SDavid Chinner tout = 10; 172249a8c11SDavid Chinner lsn = lip->li_lsn; 173249a8c11SDavid Chinner flush_log = stuck = count = restarts = 0; 174249a8c11SDavid Chinner while ((XFS_LSN_CMP(lip->li_lsn, target) < 0)) { 175249a8c11SDavid Chinner int lock_result; 176249a8c11SDavid Chinner /* 177249a8c11SDavid Chinner * If we can lock the item without sleeping, unlock the AIL 178249a8c11SDavid Chinner * lock and flush the item. Then re-grab the AIL lock so we 179249a8c11SDavid Chinner * can look for the next item on the AIL. List changes are 180249a8c11SDavid Chinner * handled by the AIL lookup functions internally 181249a8c11SDavid Chinner * 182249a8c11SDavid Chinner * If we can't lock the item, either its holder will flush it 183249a8c11SDavid Chinner * or it is already being flushed or it is being relogged. In 184249a8c11SDavid Chinner * any of these case it is being taken care of and we can just 185249a8c11SDavid Chinner * skip to the next item in the list. 1861da177e4SLinus Torvalds */ 1871da177e4SLinus Torvalds lock_result = IOP_TRYLOCK(lip); 188249a8c11SDavid Chinner spin_unlock(&mp->m_ail_lock); 1891da177e4SLinus Torvalds switch (lock_result) { 1901da177e4SLinus Torvalds case XFS_ITEM_SUCCESS: 1911da177e4SLinus Torvalds XFS_STATS_INC(xs_push_ail_success); 1921da177e4SLinus Torvalds IOP_PUSH(lip); 193249a8c11SDavid Chinner last_pushed_lsn = lsn; 1941da177e4SLinus Torvalds break; 1951da177e4SLinus Torvalds 1961da177e4SLinus Torvalds case XFS_ITEM_PUSHBUF: 1971da177e4SLinus Torvalds XFS_STATS_INC(xs_push_ail_pushbuf); 1981da177e4SLinus Torvalds IOP_PUSHBUF(lip); 199249a8c11SDavid Chinner last_pushed_lsn = lsn; 2001da177e4SLinus Torvalds break; 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds case XFS_ITEM_PINNED: 2031da177e4SLinus Torvalds XFS_STATS_INC(xs_push_ail_pinned); 204249a8c11SDavid Chinner stuck++; 2051da177e4SLinus Torvalds flush_log = 1; 2061da177e4SLinus Torvalds break; 2071da177e4SLinus Torvalds 2081da177e4SLinus Torvalds case XFS_ITEM_LOCKED: 2091da177e4SLinus Torvalds XFS_STATS_INC(xs_push_ail_locked); 210249a8c11SDavid Chinner last_pushed_lsn = lsn; 211249a8c11SDavid Chinner stuck++; 2121da177e4SLinus Torvalds break; 2131da177e4SLinus Torvalds 2141da177e4SLinus Torvalds case XFS_ITEM_FLUSHING: 2151da177e4SLinus Torvalds XFS_STATS_INC(xs_push_ail_flushing); 216249a8c11SDavid Chinner last_pushed_lsn = lsn; 217249a8c11SDavid Chinner stuck++; 2181da177e4SLinus Torvalds break; 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds default: 2211da177e4SLinus Torvalds ASSERT(0); 2221da177e4SLinus Torvalds break; 2231da177e4SLinus Torvalds } 2241da177e4SLinus Torvalds 225249a8c11SDavid Chinner spin_lock(&mp->m_ail_lock); 226249a8c11SDavid Chinner /* should we bother continuing? */ 227249a8c11SDavid Chinner if (XFS_FORCED_SHUTDOWN(mp)) 2281da177e4SLinus Torvalds break; 229249a8c11SDavid Chinner ASSERT(mp->m_log); 2301da177e4SLinus Torvalds 231249a8c11SDavid Chinner count++; 232249a8c11SDavid Chinner 233249a8c11SDavid Chinner /* 234249a8c11SDavid Chinner * Are there too many items we can't do anything with? 235249a8c11SDavid Chinner * If we we are skipping too many items because we can't flush 236249a8c11SDavid Chinner * them or they are already being flushed, we back off and 237249a8c11SDavid Chinner * given them time to complete whatever operation is being 238249a8c11SDavid Chinner * done. i.e. remove pressure from the AIL while we can't make 239249a8c11SDavid Chinner * progress so traversals don't slow down further inserts and 240249a8c11SDavid Chinner * removals to/from the AIL. 241249a8c11SDavid Chinner * 242249a8c11SDavid Chinner * The value of 100 is an arbitrary magic number based on 243249a8c11SDavid Chinner * observation. 244249a8c11SDavid Chinner */ 245249a8c11SDavid Chinner if (stuck > 100) 246249a8c11SDavid Chinner break; 247249a8c11SDavid Chinner 248249a8c11SDavid Chinner lip = xfs_trans_next_ail(mp, lip, &gen, &restarts); 249249a8c11SDavid Chinner if (lip == NULL) 250249a8c11SDavid Chinner break; 251249a8c11SDavid Chinner if (restarts > XFS_TRANS_PUSH_AIL_RESTARTS) 252249a8c11SDavid Chinner break; 253249a8c11SDavid Chinner lsn = lip->li_lsn; 2541da177e4SLinus Torvalds } 255249a8c11SDavid Chinner spin_unlock(&mp->m_ail_lock); 2561da177e4SLinus Torvalds 2571da177e4SLinus Torvalds if (flush_log) { 2581da177e4SLinus Torvalds /* 2591da177e4SLinus Torvalds * If something we need to push out was pinned, then 2601da177e4SLinus Torvalds * push out the log so it will become unpinned and 2611da177e4SLinus Torvalds * move forward in the AIL. 2621da177e4SLinus Torvalds */ 2631da177e4SLinus Torvalds XFS_STATS_INC(xs_push_ail_flush); 2641da177e4SLinus Torvalds xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE); 2651da177e4SLinus Torvalds } 2661da177e4SLinus Torvalds 26792d9cd10SDavid Chinner if (!count) { 26892d9cd10SDavid Chinner /* We're past our target or empty, so idle */ 26992d9cd10SDavid Chinner tout = 1000; 27092d9cd10SDavid Chinner } else if (XFS_LSN_CMP(lsn, target) >= 0) { 271249a8c11SDavid Chinner /* 27292d9cd10SDavid Chinner * We reached the target so wait a bit longer for I/O to 27392d9cd10SDavid Chinner * complete and remove pushed items from the AIL before we 27492d9cd10SDavid Chinner * start the next scan from the start of the AIL. 275249a8c11SDavid Chinner */ 276249a8c11SDavid Chinner tout += 20; 277249a8c11SDavid Chinner last_pushed_lsn = 0; 278249a8c11SDavid Chinner } else if ((restarts > XFS_TRANS_PUSH_AIL_RESTARTS) || 27992d9cd10SDavid Chinner ((stuck * 100) / count > 90)) { 280249a8c11SDavid Chinner /* 281249a8c11SDavid Chinner * Either there is a lot of contention on the AIL or we 282249a8c11SDavid Chinner * are stuck due to operations in progress. "Stuck" in this 283249a8c11SDavid Chinner * case is defined as >90% of the items we tried to push 284249a8c11SDavid Chinner * were stuck. 285249a8c11SDavid Chinner * 286249a8c11SDavid Chinner * Backoff a bit more to allow some I/O to complete before 287249a8c11SDavid Chinner * continuing from where we were. 288249a8c11SDavid Chinner */ 289249a8c11SDavid Chinner tout += 10; 2901da177e4SLinus Torvalds } 291249a8c11SDavid Chinner out: 292249a8c11SDavid Chinner *last_lsn = last_pushed_lsn; 293249a8c11SDavid Chinner return tout; 294249a8c11SDavid Chinner } /* xfsaild_push */ 2951da177e4SLinus Torvalds 2961da177e4SLinus Torvalds 2971da177e4SLinus Torvalds /* 2981da177e4SLinus Torvalds * This is to be called when an item is unlocked that may have 2991da177e4SLinus Torvalds * been in the AIL. It will wake up the first member of the AIL 3001da177e4SLinus Torvalds * wait list if this item's unlocking might allow it to progress. 3011da177e4SLinus Torvalds * If the item is in the AIL, then we need to get the AIL lock 3021da177e4SLinus Torvalds * while doing our checking so we don't race with someone going 3031da177e4SLinus Torvalds * to sleep waiting for this event in xfs_trans_push_ail(). 3041da177e4SLinus Torvalds */ 3051da177e4SLinus Torvalds void 3061da177e4SLinus Torvalds xfs_trans_unlocked_item( 3071da177e4SLinus Torvalds xfs_mount_t *mp, 3081da177e4SLinus Torvalds xfs_log_item_t *lip) 3091da177e4SLinus Torvalds { 3101da177e4SLinus Torvalds xfs_log_item_t *min_lip; 3111da177e4SLinus Torvalds 3121da177e4SLinus Torvalds /* 3131da177e4SLinus Torvalds * If we're forcibly shutting down, we may have 3141da177e4SLinus Torvalds * unlocked log items arbitrarily. The last thing 3151da177e4SLinus Torvalds * we want to do is to move the tail of the log 3161da177e4SLinus Torvalds * over some potentially valid data. 3171da177e4SLinus Torvalds */ 3181da177e4SLinus Torvalds if (!(lip->li_flags & XFS_LI_IN_AIL) || 3191da177e4SLinus Torvalds XFS_FORCED_SHUTDOWN(mp)) { 3201da177e4SLinus Torvalds return; 3211da177e4SLinus Torvalds } 3221da177e4SLinus Torvalds 3231da177e4SLinus Torvalds /* 3241da177e4SLinus Torvalds * This is the one case where we can call into xfs_ail_min() 3251da177e4SLinus Torvalds * without holding the AIL lock because we only care about the 3261da177e4SLinus Torvalds * case where we are at the tail of the AIL. If the object isn't 3271da177e4SLinus Torvalds * at the tail, it doesn't matter what result we get back. This 3281da177e4SLinus Torvalds * is slightly racy because since we were just unlocked, we could 3291da177e4SLinus Torvalds * go to sleep between the call to xfs_ail_min and the call to 3301da177e4SLinus Torvalds * xfs_log_move_tail, have someone else lock us, commit to us disk, 3311da177e4SLinus Torvalds * move us out of the tail of the AIL, and then we wake up. However, 3321da177e4SLinus Torvalds * the call to xfs_log_move_tail() doesn't do anything if there's 3331da177e4SLinus Torvalds * not enough free space to wake people up so we're safe calling it. 3341da177e4SLinus Torvalds */ 335*82fa9012SDavid Chinner min_lip = xfs_ail_min(mp->m_ail); 3361da177e4SLinus Torvalds 3371da177e4SLinus Torvalds if (min_lip == lip) 3381da177e4SLinus Torvalds xfs_log_move_tail(mp, 1); 3391da177e4SLinus Torvalds } /* xfs_trans_unlocked_item */ 3401da177e4SLinus Torvalds 3411da177e4SLinus Torvalds 3421da177e4SLinus Torvalds /* 3431da177e4SLinus Torvalds * Update the position of the item in the AIL with the new 3441da177e4SLinus Torvalds * lsn. If it is not yet in the AIL, add it. Otherwise, move 3451da177e4SLinus Torvalds * it to its new position by removing it and re-adding it. 3461da177e4SLinus Torvalds * 3471da177e4SLinus Torvalds * Wakeup anyone with an lsn less than the item's lsn. If the item 3481da177e4SLinus Torvalds * we move in the AIL is the minimum one, update the tail lsn in the 3491da177e4SLinus Torvalds * log manager. 3501da177e4SLinus Torvalds * 3511da177e4SLinus Torvalds * Increment the AIL's generation count to indicate that the tree 3521da177e4SLinus Torvalds * has changed. 3531da177e4SLinus Torvalds * 3541da177e4SLinus Torvalds * This function must be called with the AIL lock held. The lock 355287f3dadSDonald Douwsma * is dropped before returning. 3561da177e4SLinus Torvalds */ 3571da177e4SLinus Torvalds void 3581da177e4SLinus Torvalds xfs_trans_update_ail( 3591da177e4SLinus Torvalds xfs_mount_t *mp, 3601da177e4SLinus Torvalds xfs_log_item_t *lip, 361287f3dadSDonald Douwsma xfs_lsn_t lsn) __releases(mp->m_ail_lock) 3621da177e4SLinus Torvalds { 3631da177e4SLinus Torvalds xfs_log_item_t *dlip=NULL; 3641da177e4SLinus Torvalds xfs_log_item_t *mlip; /* ptr to minimum lip */ 3651da177e4SLinus Torvalds 366*82fa9012SDavid Chinner mlip = xfs_ail_min(mp->m_ail); 3671da177e4SLinus Torvalds 3681da177e4SLinus Torvalds if (lip->li_flags & XFS_LI_IN_AIL) { 369*82fa9012SDavid Chinner dlip = xfs_ail_delete(mp->m_ail, lip); 3701da177e4SLinus Torvalds ASSERT(dlip == lip); 3711da177e4SLinus Torvalds } else { 3721da177e4SLinus Torvalds lip->li_flags |= XFS_LI_IN_AIL; 3731da177e4SLinus Torvalds } 3741da177e4SLinus Torvalds 3751da177e4SLinus Torvalds lip->li_lsn = lsn; 3761da177e4SLinus Torvalds 377*82fa9012SDavid Chinner xfs_ail_insert(mp->m_ail, lip); 378*82fa9012SDavid Chinner mp->m_ail->xa_gen++; 3791da177e4SLinus Torvalds 3801da177e4SLinus Torvalds if (mlip == dlip) { 381*82fa9012SDavid Chinner mlip = xfs_ail_min(mp->m_ail); 382287f3dadSDonald Douwsma spin_unlock(&mp->m_ail_lock); 3831da177e4SLinus Torvalds xfs_log_move_tail(mp, mlip->li_lsn); 3841da177e4SLinus Torvalds } else { 385287f3dadSDonald Douwsma spin_unlock(&mp->m_ail_lock); 3861da177e4SLinus Torvalds } 3871da177e4SLinus Torvalds 3881da177e4SLinus Torvalds 3891da177e4SLinus Torvalds } /* xfs_trans_update_ail */ 3901da177e4SLinus Torvalds 3911da177e4SLinus Torvalds /* 3921da177e4SLinus Torvalds * Delete the given item from the AIL. It must already be in 3931da177e4SLinus Torvalds * the AIL. 3941da177e4SLinus Torvalds * 3951da177e4SLinus Torvalds * Wakeup anyone with an lsn less than item's lsn. If the item 3961da177e4SLinus Torvalds * we delete in the AIL is the minimum one, update the tail lsn in the 3971da177e4SLinus Torvalds * log manager. 3981da177e4SLinus Torvalds * 3991da177e4SLinus Torvalds * Clear the IN_AIL flag from the item, reset its lsn to 0, and 4001da177e4SLinus Torvalds * bump the AIL's generation count to indicate that the tree 4011da177e4SLinus Torvalds * has changed. 4021da177e4SLinus Torvalds * 4031da177e4SLinus Torvalds * This function must be called with the AIL lock held. The lock 404287f3dadSDonald Douwsma * is dropped before returning. 4051da177e4SLinus Torvalds */ 4061da177e4SLinus Torvalds void 4071da177e4SLinus Torvalds xfs_trans_delete_ail( 4081da177e4SLinus Torvalds xfs_mount_t *mp, 409287f3dadSDonald Douwsma xfs_log_item_t *lip) __releases(mp->m_ail_lock) 4101da177e4SLinus Torvalds { 4111da177e4SLinus Torvalds xfs_log_item_t *dlip; 4121da177e4SLinus Torvalds xfs_log_item_t *mlip; 4131da177e4SLinus Torvalds 4141da177e4SLinus Torvalds if (lip->li_flags & XFS_LI_IN_AIL) { 415*82fa9012SDavid Chinner mlip = xfs_ail_min(mp->m_ail); 416*82fa9012SDavid Chinner dlip = xfs_ail_delete(mp->m_ail, lip); 4171da177e4SLinus Torvalds ASSERT(dlip == lip); 4181da177e4SLinus Torvalds 4191da177e4SLinus Torvalds 4201da177e4SLinus Torvalds lip->li_flags &= ~XFS_LI_IN_AIL; 4211da177e4SLinus Torvalds lip->li_lsn = 0; 422*82fa9012SDavid Chinner mp->m_ail->xa_gen++; 4231da177e4SLinus Torvalds 4241da177e4SLinus Torvalds if (mlip == dlip) { 425*82fa9012SDavid Chinner mlip = xfs_ail_min(mp->m_ail); 426287f3dadSDonald Douwsma spin_unlock(&mp->m_ail_lock); 4271da177e4SLinus Torvalds xfs_log_move_tail(mp, (mlip ? mlip->li_lsn : 0)); 4281da177e4SLinus Torvalds } else { 429287f3dadSDonald Douwsma spin_unlock(&mp->m_ail_lock); 4301da177e4SLinus Torvalds } 4311da177e4SLinus Torvalds } 4321da177e4SLinus Torvalds else { 4331da177e4SLinus Torvalds /* 4341da177e4SLinus Torvalds * If the file system is not being shutdown, we are in 4351da177e4SLinus Torvalds * serious trouble if we get to this stage. 4361da177e4SLinus Torvalds */ 4371da177e4SLinus Torvalds if (XFS_FORCED_SHUTDOWN(mp)) 438287f3dadSDonald Douwsma spin_unlock(&mp->m_ail_lock); 4391da177e4SLinus Torvalds else { 4401da177e4SLinus Torvalds xfs_cmn_err(XFS_PTAG_AILDELETE, CE_ALERT, mp, 4417d04a335SNathan Scott "%s: attempting to delete a log item that is not in the AIL", 44234a622b2SHarvey Harrison __func__); 443287f3dadSDonald Douwsma spin_unlock(&mp->m_ail_lock); 4447d04a335SNathan Scott xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); 4451da177e4SLinus Torvalds } 4461da177e4SLinus Torvalds } 4471da177e4SLinus Torvalds } 4481da177e4SLinus Torvalds 4491da177e4SLinus Torvalds 4501da177e4SLinus Torvalds 4511da177e4SLinus Torvalds /* 4521da177e4SLinus Torvalds * Return the item in the AIL with the smallest lsn. 4531da177e4SLinus Torvalds * Return the current tree generation number for use 4541da177e4SLinus Torvalds * in calls to xfs_trans_next_ail(). 4551da177e4SLinus Torvalds */ 4561da177e4SLinus Torvalds xfs_log_item_t * 4571da177e4SLinus Torvalds xfs_trans_first_ail( 4581da177e4SLinus Torvalds xfs_mount_t *mp, 4591da177e4SLinus Torvalds int *gen) 4601da177e4SLinus Torvalds { 4611da177e4SLinus Torvalds xfs_log_item_t *lip; 4621da177e4SLinus Torvalds 463*82fa9012SDavid Chinner lip = xfs_ail_min(mp->m_ail); 464*82fa9012SDavid Chinner *gen = (int)mp->m_ail->xa_gen; 4651da177e4SLinus Torvalds 466249a8c11SDavid Chinner return lip; 4671da177e4SLinus Torvalds } 4681da177e4SLinus Torvalds 4691da177e4SLinus Torvalds /* 4701da177e4SLinus Torvalds * If the generation count of the tree has not changed since the 4711da177e4SLinus Torvalds * caller last took something from the AIL, then return the elmt 4721da177e4SLinus Torvalds * in the tree which follows the one given. If the count has changed, 4731da177e4SLinus Torvalds * then return the minimum elmt of the AIL and bump the restarts counter 4741da177e4SLinus Torvalds * if one is given. 4751da177e4SLinus Torvalds */ 4761da177e4SLinus Torvalds xfs_log_item_t * 4771da177e4SLinus Torvalds xfs_trans_next_ail( 4781da177e4SLinus Torvalds xfs_mount_t *mp, 4791da177e4SLinus Torvalds xfs_log_item_t *lip, 4801da177e4SLinus Torvalds int *gen, 4811da177e4SLinus Torvalds int *restarts) 4821da177e4SLinus Torvalds { 4831da177e4SLinus Torvalds xfs_log_item_t *nlip; 4841da177e4SLinus Torvalds 4851da177e4SLinus Torvalds ASSERT(mp && lip && gen); 486*82fa9012SDavid Chinner if (mp->m_ail->xa_gen == *gen) { 487*82fa9012SDavid Chinner nlip = xfs_ail_next(mp->m_ail, lip); 4881da177e4SLinus Torvalds } else { 489*82fa9012SDavid Chinner nlip = xfs_ail_min(mp->m_ail); 490*82fa9012SDavid Chinner *gen = (int)mp->m_ail->xa_gen; 4911da177e4SLinus Torvalds if (restarts != NULL) { 4921da177e4SLinus Torvalds XFS_STATS_INC(xs_push_ail_restarts); 4931da177e4SLinus Torvalds (*restarts)++; 4941da177e4SLinus Torvalds } 4951da177e4SLinus Torvalds } 4961da177e4SLinus Torvalds 4971da177e4SLinus Torvalds return (nlip); 4981da177e4SLinus Torvalds } 4991da177e4SLinus Torvalds 5001da177e4SLinus Torvalds 5011da177e4SLinus Torvalds /* 5021da177e4SLinus Torvalds * The active item list (AIL) is a doubly linked list of log 5031da177e4SLinus Torvalds * items sorted by ascending lsn. The base of the list is 5041da177e4SLinus Torvalds * a forw/back pointer pair embedded in the xfs mount structure. 5051da177e4SLinus Torvalds * The base is initialized with both pointers pointing to the 5061da177e4SLinus Torvalds * base. This case always needs to be distinguished, because 5071da177e4SLinus Torvalds * the base has no lsn to look at. We almost always insert 5081da177e4SLinus Torvalds * at the end of the list, so on inserts we search from the 5091da177e4SLinus Torvalds * end of the list to find where the new item belongs. 5101da177e4SLinus Torvalds */ 5111da177e4SLinus Torvalds 5121da177e4SLinus Torvalds /* 5131da177e4SLinus Torvalds * Initialize the doubly linked list to point only to itself. 5141da177e4SLinus Torvalds */ 515249a8c11SDavid Chinner int 5161da177e4SLinus Torvalds xfs_trans_ail_init( 5171da177e4SLinus Torvalds xfs_mount_t *mp) 5181da177e4SLinus Torvalds { 519*82fa9012SDavid Chinner struct xfs_ail *ailp; 520*82fa9012SDavid Chinner 521*82fa9012SDavid Chinner ailp = kmem_zalloc(sizeof(struct xfs_ail), KM_MAYFAIL); 522*82fa9012SDavid Chinner if (!ailp) 523*82fa9012SDavid Chinner return ENOMEM; 524*82fa9012SDavid Chinner 525*82fa9012SDavid Chinner ailp->xa_mount = mp; 526*82fa9012SDavid Chinner INIT_LIST_HEAD(&ailp->xa_ail); 527*82fa9012SDavid Chinner return xfsaild_start(ailp); 528249a8c11SDavid Chinner } 529249a8c11SDavid Chinner 530249a8c11SDavid Chinner void 531249a8c11SDavid Chinner xfs_trans_ail_destroy( 532249a8c11SDavid Chinner xfs_mount_t *mp) 533249a8c11SDavid Chinner { 534*82fa9012SDavid Chinner struct xfs_ail *ailp = mp->m_ail; 535*82fa9012SDavid Chinner 536*82fa9012SDavid Chinner xfsaild_stop(ailp); 537*82fa9012SDavid Chinner kmem_free(ailp); 5381da177e4SLinus Torvalds } 5391da177e4SLinus Torvalds 5401da177e4SLinus Torvalds /* 5411da177e4SLinus Torvalds * Insert the given log item into the AIL. 5421da177e4SLinus Torvalds * We almost always insert at the end of the list, so on inserts 5431da177e4SLinus Torvalds * we search from the end of the list to find where the 5441da177e4SLinus Torvalds * new item belongs. 5451da177e4SLinus Torvalds */ 5461da177e4SLinus Torvalds STATIC void 5471da177e4SLinus Torvalds xfs_ail_insert( 548*82fa9012SDavid Chinner struct xfs_ail *ailp, 5491da177e4SLinus Torvalds xfs_log_item_t *lip) 5501da177e4SLinus Torvalds /* ARGSUSED */ 5511da177e4SLinus Torvalds { 5521da177e4SLinus Torvalds xfs_log_item_t *next_lip; 5531da177e4SLinus Torvalds 5541da177e4SLinus Torvalds /* 5551da177e4SLinus Torvalds * If the list is empty, just insert the item. 5561da177e4SLinus Torvalds */ 557535f6b37SJosef 'Jeff' Sipek if (list_empty(&ailp->xa_ail)) { 558535f6b37SJosef 'Jeff' Sipek list_add(&lip->li_ail, &ailp->xa_ail); 5591da177e4SLinus Torvalds return; 5601da177e4SLinus Torvalds } 5611da177e4SLinus Torvalds 562535f6b37SJosef 'Jeff' Sipek list_for_each_entry_reverse(next_lip, &ailp->xa_ail, li_ail) { 563535f6b37SJosef 'Jeff' Sipek if (XFS_LSN_CMP(next_lip->li_lsn, lip->li_lsn) <= 0) 564535f6b37SJosef 'Jeff' Sipek break; 5651da177e4SLinus Torvalds } 5661da177e4SLinus Torvalds 567535f6b37SJosef 'Jeff' Sipek ASSERT((&next_lip->li_ail == &ailp->xa_ail) || 568535f6b37SJosef 'Jeff' Sipek (XFS_LSN_CMP(next_lip->li_lsn, lip->li_lsn) <= 0)); 569535f6b37SJosef 'Jeff' Sipek 570535f6b37SJosef 'Jeff' Sipek list_add(&lip->li_ail, &next_lip->li_ail); 571535f6b37SJosef 'Jeff' Sipek 572535f6b37SJosef 'Jeff' Sipek xfs_ail_check(ailp, lip); 5731da177e4SLinus Torvalds return; 5741da177e4SLinus Torvalds } 5751da177e4SLinus Torvalds 5761da177e4SLinus Torvalds /* 5771da177e4SLinus Torvalds * Delete the given item from the AIL. Return a pointer to the item. 5781da177e4SLinus Torvalds */ 5791da177e4SLinus Torvalds /*ARGSUSED*/ 5801da177e4SLinus Torvalds STATIC xfs_log_item_t * 5811da177e4SLinus Torvalds xfs_ail_delete( 582*82fa9012SDavid Chinner struct xfs_ail *ailp, 5831da177e4SLinus Torvalds xfs_log_item_t *lip) 5841da177e4SLinus Torvalds /* ARGSUSED */ 5851da177e4SLinus Torvalds { 586535f6b37SJosef 'Jeff' Sipek xfs_ail_check(ailp, lip); 587535f6b37SJosef 'Jeff' Sipek 588535f6b37SJosef 'Jeff' Sipek list_del(&lip->li_ail); 5891da177e4SLinus Torvalds 5901da177e4SLinus Torvalds return lip; 5911da177e4SLinus Torvalds } 5921da177e4SLinus Torvalds 5931da177e4SLinus Torvalds /* 5941da177e4SLinus Torvalds * Return a pointer to the first item in the AIL. 5951da177e4SLinus Torvalds * If the AIL is empty, then return NULL. 5961da177e4SLinus Torvalds */ 5971da177e4SLinus Torvalds STATIC xfs_log_item_t * 5981da177e4SLinus Torvalds xfs_ail_min( 599*82fa9012SDavid Chinner struct xfs_ail *ailp) 6001da177e4SLinus Torvalds /* ARGSUSED */ 6011da177e4SLinus Torvalds { 602535f6b37SJosef 'Jeff' Sipek if (list_empty(&ailp->xa_ail)) 6031da177e4SLinus Torvalds return NULL; 604535f6b37SJosef 'Jeff' Sipek 605535f6b37SJosef 'Jeff' Sipek return list_first_entry(&ailp->xa_ail, xfs_log_item_t, li_ail); 6061da177e4SLinus Torvalds } 6071da177e4SLinus Torvalds 6081da177e4SLinus Torvalds /* 6091da177e4SLinus Torvalds * Return a pointer to the item which follows 6101da177e4SLinus Torvalds * the given item in the AIL. If the given item 6111da177e4SLinus Torvalds * is the last item in the list, then return NULL. 6121da177e4SLinus Torvalds */ 6131da177e4SLinus Torvalds STATIC xfs_log_item_t * 6141da177e4SLinus Torvalds xfs_ail_next( 615*82fa9012SDavid Chinner struct xfs_ail *ailp, 6161da177e4SLinus Torvalds xfs_log_item_t *lip) 6171da177e4SLinus Torvalds /* ARGSUSED */ 6181da177e4SLinus Torvalds { 619535f6b37SJosef 'Jeff' Sipek if (lip->li_ail.next == &ailp->xa_ail) 6201da177e4SLinus Torvalds return NULL; 6211da177e4SLinus Torvalds 622535f6b37SJosef 'Jeff' Sipek return list_first_entry(&lip->li_ail, xfs_log_item_t, li_ail); 6231da177e4SLinus Torvalds } 6241da177e4SLinus Torvalds 6251da177e4SLinus Torvalds #ifdef DEBUG 6261da177e4SLinus Torvalds /* 6271da177e4SLinus Torvalds * Check that the list is sorted as it should be. 6281da177e4SLinus Torvalds */ 6291da177e4SLinus Torvalds STATIC void 6301da177e4SLinus Torvalds xfs_ail_check( 631*82fa9012SDavid Chinner struct xfs_ail *ailp, 632de08dbc1SDavid Chinner xfs_log_item_t *lip) 6331da177e4SLinus Torvalds { 6341da177e4SLinus Torvalds xfs_log_item_t *prev_lip; 6351da177e4SLinus Torvalds 636535f6b37SJosef 'Jeff' Sipek if (list_empty(&ailp->xa_ail)) 6371da177e4SLinus Torvalds return; 6381da177e4SLinus Torvalds 6391da177e4SLinus Torvalds /* 640de08dbc1SDavid Chinner * Check the next and previous entries are valid. 641de08dbc1SDavid Chinner */ 642de08dbc1SDavid Chinner ASSERT((lip->li_flags & XFS_LI_IN_AIL) != 0); 643535f6b37SJosef 'Jeff' Sipek prev_lip = list_entry(lip->li_ail.prev, xfs_log_item_t, li_ail); 644535f6b37SJosef 'Jeff' Sipek if (&prev_lip->li_ail != &ailp->xa_ail) 645de08dbc1SDavid Chinner ASSERT(XFS_LSN_CMP(prev_lip->li_lsn, lip->li_lsn) <= 0); 646535f6b37SJosef 'Jeff' Sipek 647535f6b37SJosef 'Jeff' Sipek prev_lip = list_entry(lip->li_ail.next, xfs_log_item_t, li_ail); 648535f6b37SJosef 'Jeff' Sipek if (&prev_lip->li_ail != &ailp->xa_ail) 649de08dbc1SDavid Chinner ASSERT(XFS_LSN_CMP(prev_lip->li_lsn, lip->li_lsn) >= 0); 650de08dbc1SDavid Chinner 651de08dbc1SDavid Chinner 652de08dbc1SDavid Chinner #ifdef XFS_TRANS_DEBUG 653de08dbc1SDavid Chinner /* 654535f6b37SJosef 'Jeff' Sipek * Walk the list checking lsn ordering, and that every entry has the 655535f6b37SJosef 'Jeff' Sipek * XFS_LI_IN_AIL flag set. This is really expensive, so only do it 656535f6b37SJosef 'Jeff' Sipek * when specifically debugging the transaction subsystem. 6571da177e4SLinus Torvalds */ 658535f6b37SJosef 'Jeff' Sipek prev_lip = list_entry(&ailp->xa_ail, xfs_log_item_t, li_ail); 659535f6b37SJosef 'Jeff' Sipek list_for_each_entry(lip, &ailp->xa_ail, li_ail) { 660535f6b37SJosef 'Jeff' Sipek if (&prev_lip->li_ail != &ailp->xa_ail) 6611da177e4SLinus Torvalds ASSERT(XFS_LSN_CMP(prev_lip->li_lsn, lip->li_lsn) <= 0); 6621da177e4SLinus Torvalds ASSERT((lip->li_flags & XFS_LI_IN_AIL) != 0); 6631da177e4SLinus Torvalds prev_lip = lip; 6641da177e4SLinus Torvalds } 665de08dbc1SDavid Chinner #endif /* XFS_TRANS_DEBUG */ 6661da177e4SLinus Torvalds } 6671da177e4SLinus Torvalds #endif /* DEBUG */ 668