10b61f8a4SDave Chinner // SPDX-License-Identifier: GPL-2.0 21da177e4SLinus Torvalds /* 37b718769SNathan Scott * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc. 4c7e8f268SDavid Chinner * Copyright (c) 2008 Dave Chinner 57b718769SNathan Scott * All Rights Reserved. 61da177e4SLinus Torvalds */ 71da177e4SLinus Torvalds #include "xfs.h" 8a844f451SNathan Scott #include "xfs_fs.h" 95467b34bSDarrick J. Wong #include "xfs_shared.h" 104fb6e8adSChristoph Hellwig #include "xfs_format.h" 11239880efSDave Chinner #include "xfs_log_format.h" 12239880efSDave Chinner #include "xfs_trans_resv.h" 131da177e4SLinus Torvalds #include "xfs_mount.h" 14239880efSDave Chinner #include "xfs_trans.h" 151da177e4SLinus Torvalds #include "xfs_trans_priv.h" 169e4c109aSChristoph Hellwig #include "xfs_trace.h" 17e9e899a2SDarrick J. Wong #include "xfs_errortag.h" 181da177e4SLinus Torvalds #include "xfs_error.h" 19239880efSDave Chinner #include "xfs_log.h" 201da177e4SLinus Torvalds 211da177e4SLinus Torvalds #ifdef DEBUG 22cd4a3c50SDave Chinner /* 23cd4a3c50SDave Chinner * Check that the list is sorted as it should be. 24d686d12dSDave Chinner * 25d686d12dSDave Chinner * Called with the ail lock held, but we don't want to assert fail with it 26d686d12dSDave Chinner * held otherwise we'll lock everything up and won't be able to debug the 27d686d12dSDave Chinner * cause. Hence we sample and check the state under the AIL lock and return if 28d686d12dSDave Chinner * everything is fine, otherwise we drop the lock and run the ASSERT checks. 29d686d12dSDave Chinner * Asserts may not be fatal, so pick the lock back up and continue onwards. 30cd4a3c50SDave Chinner */ 31cd4a3c50SDave Chinner STATIC void 32cd4a3c50SDave Chinner xfs_ail_check( 33cd4a3c50SDave Chinner struct xfs_ail *ailp, 34d686d12dSDave Chinner struct xfs_log_item *lip) 35daebba1bSJules Irenge __must_hold(&ailp->ail_lock) 36cd4a3c50SDave Chinner { 37d686d12dSDave Chinner struct xfs_log_item *prev_lip; 38d686d12dSDave Chinner struct xfs_log_item *next_lip; 39d686d12dSDave Chinner xfs_lsn_t prev_lsn = NULLCOMMITLSN; 40d686d12dSDave Chinner xfs_lsn_t next_lsn = NULLCOMMITLSN; 41d686d12dSDave Chinner xfs_lsn_t lsn; 42d686d12dSDave Chinner bool in_ail; 43d686d12dSDave Chinner 44cd4a3c50SDave Chinner 4557e80956SMatthew Wilcox if (list_empty(&ailp->ail_head)) 46cd4a3c50SDave Chinner return; 47cd4a3c50SDave Chinner 48cd4a3c50SDave Chinner /* 49d686d12dSDave Chinner * Sample then check the next and previous entries are valid. 50cd4a3c50SDave Chinner */ 51d686d12dSDave Chinner in_ail = test_bit(XFS_LI_IN_AIL, &lip->li_flags); 52d686d12dSDave Chinner prev_lip = list_entry(lip->li_ail.prev, struct xfs_log_item, li_ail); 5357e80956SMatthew Wilcox if (&prev_lip->li_ail != &ailp->ail_head) 54d686d12dSDave Chinner prev_lsn = prev_lip->li_lsn; 55d686d12dSDave Chinner next_lip = list_entry(lip->li_ail.next, struct xfs_log_item, li_ail); 56d686d12dSDave Chinner if (&next_lip->li_ail != &ailp->ail_head) 57d686d12dSDave Chinner next_lsn = next_lip->li_lsn; 58d686d12dSDave Chinner lsn = lip->li_lsn; 59cd4a3c50SDave Chinner 60d686d12dSDave Chinner if (in_ail && 61d686d12dSDave Chinner (prev_lsn == NULLCOMMITLSN || XFS_LSN_CMP(prev_lsn, lsn) <= 0) && 62d686d12dSDave Chinner (next_lsn == NULLCOMMITLSN || XFS_LSN_CMP(next_lsn, lsn) >= 0)) 63d686d12dSDave Chinner return; 64cd4a3c50SDave Chinner 65d686d12dSDave Chinner spin_unlock(&ailp->ail_lock); 66d686d12dSDave Chinner ASSERT(in_ail); 67d686d12dSDave Chinner ASSERT(prev_lsn == NULLCOMMITLSN || XFS_LSN_CMP(prev_lsn, lsn) <= 0); 68d686d12dSDave Chinner ASSERT(next_lsn == NULLCOMMITLSN || XFS_LSN_CMP(next_lsn, lsn) >= 0); 69d686d12dSDave Chinner spin_lock(&ailp->ail_lock); 70cd4a3c50SDave Chinner } 71cd4a3c50SDave Chinner #else /* !DEBUG */ 72de08dbc1SDavid Chinner #define xfs_ail_check(a,l) 731da177e4SLinus Torvalds #endif /* DEBUG */ 741da177e4SLinus Torvalds 75cd4a3c50SDave Chinner /* 76fd074841SDave Chinner * Return a pointer to the last item in the AIL. If the AIL is empty, then 77fd074841SDave Chinner * return NULL. 78fd074841SDave Chinner */ 79efe2330fSChristoph Hellwig static struct xfs_log_item * 80fd074841SDave Chinner xfs_ail_max( 81fd074841SDave Chinner struct xfs_ail *ailp) 82fd074841SDave Chinner { 8357e80956SMatthew Wilcox if (list_empty(&ailp->ail_head)) 84fd074841SDave Chinner return NULL; 85fd074841SDave Chinner 86efe2330fSChristoph Hellwig return list_entry(ailp->ail_head.prev, struct xfs_log_item, li_ail); 87fd074841SDave Chinner } 88fd074841SDave Chinner 89fd074841SDave Chinner /* 90cd4a3c50SDave Chinner * Return a pointer to the item which follows the given item in the AIL. If 91cd4a3c50SDave Chinner * the given item is the last item in the list, then return NULL. 92cd4a3c50SDave Chinner */ 93efe2330fSChristoph Hellwig static struct xfs_log_item * 94cd4a3c50SDave Chinner xfs_ail_next( 95cd4a3c50SDave Chinner struct xfs_ail *ailp, 96efe2330fSChristoph Hellwig struct xfs_log_item *lip) 97cd4a3c50SDave Chinner { 9857e80956SMatthew Wilcox if (lip->li_ail.next == &ailp->ail_head) 99cd4a3c50SDave Chinner return NULL; 100cd4a3c50SDave Chinner 101efe2330fSChristoph Hellwig return list_first_entry(&lip->li_ail, struct xfs_log_item, li_ail); 102cd4a3c50SDave Chinner } 103cd4a3c50SDave Chinner 104cd4a3c50SDave Chinner /* 105cd4a3c50SDave Chinner * This is called by the log manager code to determine the LSN of the tail of 106cd4a3c50SDave Chinner * the log. This is exactly the LSN of the first item in the AIL. If the AIL 107cd4a3c50SDave Chinner * is empty, then this function returns 0. 1081da177e4SLinus Torvalds * 109cd4a3c50SDave Chinner * We need the AIL lock in order to get a coherent read of the lsn of the last 110cd4a3c50SDave Chinner * item in the AIL. 1111da177e4SLinus Torvalds */ 1128eb807bdSDave Chinner static xfs_lsn_t 1138eb807bdSDave Chinner __xfs_ail_min_lsn( 1148eb807bdSDave Chinner struct xfs_ail *ailp) 1158eb807bdSDave Chinner { 1168eb807bdSDave Chinner struct xfs_log_item *lip = xfs_ail_min(ailp); 1178eb807bdSDave Chinner 1188eb807bdSDave Chinner if (lip) 1198eb807bdSDave Chinner return lip->li_lsn; 1208eb807bdSDave Chinner return 0; 1218eb807bdSDave Chinner } 1228eb807bdSDave Chinner 1231da177e4SLinus Torvalds xfs_lsn_t 124fd074841SDave Chinner xfs_ail_min_lsn( 1255b00f14fSDavid Chinner struct xfs_ail *ailp) 1261da177e4SLinus Torvalds { 1278eb807bdSDave Chinner xfs_lsn_t lsn; 1281da177e4SLinus Torvalds 12957e80956SMatthew Wilcox spin_lock(&ailp->ail_lock); 1308eb807bdSDave Chinner lsn = __xfs_ail_min_lsn(ailp); 13157e80956SMatthew Wilcox spin_unlock(&ailp->ail_lock); 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds return lsn; 1341da177e4SLinus Torvalds } 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds /* 137fd074841SDave Chinner * Return the maximum lsn held in the AIL, or zero if the AIL is empty. 138fd074841SDave Chinner */ 139fd074841SDave Chinner static xfs_lsn_t 140fd074841SDave Chinner xfs_ail_max_lsn( 141fd074841SDave Chinner struct xfs_ail *ailp) 142fd074841SDave Chinner { 143fd074841SDave Chinner xfs_lsn_t lsn = 0; 144efe2330fSChristoph Hellwig struct xfs_log_item *lip; 145fd074841SDave Chinner 14657e80956SMatthew Wilcox spin_lock(&ailp->ail_lock); 147fd074841SDave Chinner lip = xfs_ail_max(ailp); 148fd074841SDave Chinner if (lip) 149fd074841SDave Chinner lsn = lip->li_lsn; 15057e80956SMatthew Wilcox spin_unlock(&ailp->ail_lock); 151fd074841SDave Chinner 152fd074841SDave Chinner return lsn; 153fd074841SDave Chinner } 154fd074841SDave Chinner 155fd074841SDave Chinner /* 156af3e4022SDave Chinner * The cursor keeps track of where our current traversal is up to by tracking 157af3e4022SDave Chinner * the next item in the list for us. However, for this to be safe, removing an 158af3e4022SDave Chinner * object from the AIL needs to invalidate any cursor that points to it. hence 159af3e4022SDave Chinner * the traversal cursor needs to be linked to the struct xfs_ail so that 160af3e4022SDave Chinner * deletion can search all the active cursors for invalidation. 16127d8d5feSDavid Chinner */ 1625b00f14fSDavid Chinner STATIC void 16327d8d5feSDavid Chinner xfs_trans_ail_cursor_init( 16427d8d5feSDavid Chinner struct xfs_ail *ailp, 16527d8d5feSDavid Chinner struct xfs_ail_cursor *cur) 16627d8d5feSDavid Chinner { 16727d8d5feSDavid Chinner cur->item = NULL; 16857e80956SMatthew Wilcox list_add_tail(&cur->list, &ailp->ail_cursors); 16927d8d5feSDavid Chinner } 17027d8d5feSDavid Chinner 17127d8d5feSDavid Chinner /* 172af3e4022SDave Chinner * Get the next item in the traversal and advance the cursor. If the cursor 173af3e4022SDave Chinner * was invalidated (indicated by a lip of 1), restart the traversal. 17427d8d5feSDavid Chinner */ 1755b00f14fSDavid Chinner struct xfs_log_item * 17627d8d5feSDavid Chinner xfs_trans_ail_cursor_next( 17727d8d5feSDavid Chinner struct xfs_ail *ailp, 17827d8d5feSDavid Chinner struct xfs_ail_cursor *cur) 17927d8d5feSDavid Chinner { 18027d8d5feSDavid Chinner struct xfs_log_item *lip = cur->item; 18127d8d5feSDavid Chinner 182db9d67d6SChristoph Hellwig if ((uintptr_t)lip & 1) 18327d8d5feSDavid Chinner lip = xfs_ail_min(ailp); 18416b59029SDave Chinner if (lip) 18516b59029SDave Chinner cur->item = xfs_ail_next(ailp, lip); 18627d8d5feSDavid Chinner return lip; 18727d8d5feSDavid Chinner } 18827d8d5feSDavid Chinner 18927d8d5feSDavid Chinner /* 190af3e4022SDave Chinner * When the traversal is complete, we need to remove the cursor from the list 191af3e4022SDave Chinner * of traversing cursors. 19227d8d5feSDavid Chinner */ 19327d8d5feSDavid Chinner void 19427d8d5feSDavid Chinner xfs_trans_ail_cursor_done( 195af3e4022SDave Chinner struct xfs_ail_cursor *cur) 19627d8d5feSDavid Chinner { 197af3e4022SDave Chinner cur->item = NULL; 198af3e4022SDave Chinner list_del_init(&cur->list); 19927d8d5feSDavid Chinner } 20027d8d5feSDavid Chinner 20127d8d5feSDavid Chinner /* 202af3e4022SDave Chinner * Invalidate any cursor that is pointing to this item. This is called when an 203af3e4022SDave Chinner * item is removed from the AIL. Any cursor pointing to this object is now 204af3e4022SDave Chinner * invalid and the traversal needs to be terminated so it doesn't reference a 205af3e4022SDave Chinner * freed object. We set the low bit of the cursor item pointer so we can 206af3e4022SDave Chinner * distinguish between an invalidation and the end of the list when getting the 207af3e4022SDave Chinner * next item from the cursor. 2085b00f14fSDavid Chinner */ 2095b00f14fSDavid Chinner STATIC void 2105b00f14fSDavid Chinner xfs_trans_ail_cursor_clear( 2115b00f14fSDavid Chinner struct xfs_ail *ailp, 2125b00f14fSDavid Chinner struct xfs_log_item *lip) 2135b00f14fSDavid Chinner { 2145b00f14fSDavid Chinner struct xfs_ail_cursor *cur; 2155b00f14fSDavid Chinner 21657e80956SMatthew Wilcox list_for_each_entry(cur, &ailp->ail_cursors, list) { 2175b00f14fSDavid Chinner if (cur->item == lip) 2185b00f14fSDavid Chinner cur->item = (struct xfs_log_item *) 219db9d67d6SChristoph Hellwig ((uintptr_t)cur->item | 1); 2205b00f14fSDavid Chinner } 2215b00f14fSDavid Chinner } 2225b00f14fSDavid Chinner 2235b00f14fSDavid Chinner /* 22416b59029SDave Chinner * Find the first item in the AIL with the given @lsn by searching in ascending 22516b59029SDave Chinner * LSN order and initialise the cursor to point to the next item for a 22616b59029SDave Chinner * ascending traversal. Pass a @lsn of zero to initialise the cursor to the 22716b59029SDave Chinner * first item in the AIL. Returns NULL if the list is empty. 228249a8c11SDavid Chinner */ 229efe2330fSChristoph Hellwig struct xfs_log_item * 2305b00f14fSDavid Chinner xfs_trans_ail_cursor_first( 23127d8d5feSDavid Chinner struct xfs_ail *ailp, 23227d8d5feSDavid Chinner struct xfs_ail_cursor *cur, 233249a8c11SDavid Chinner xfs_lsn_t lsn) 234249a8c11SDavid Chinner { 235efe2330fSChristoph Hellwig struct xfs_log_item *lip; 236249a8c11SDavid Chinner 2375b00f14fSDavid Chinner xfs_trans_ail_cursor_init(ailp, cur); 23816b59029SDave Chinner 23916b59029SDave Chinner if (lsn == 0) { 24027d8d5feSDavid Chinner lip = xfs_ail_min(ailp); 2415b00f14fSDavid Chinner goto out; 24216b59029SDave Chinner } 243249a8c11SDavid Chinner 24457e80956SMatthew Wilcox list_for_each_entry(lip, &ailp->ail_head, li_ail) { 2455b00f14fSDavid Chinner if (XFS_LSN_CMP(lip->li_lsn, lsn) >= 0) 2467ee49acfSDavid Chinner goto out; 2475b00f14fSDavid Chinner } 24816b59029SDave Chinner return NULL; 24916b59029SDave Chinner 2505b00f14fSDavid Chinner out: 25116b59029SDave Chinner if (lip) 25216b59029SDave Chinner cur->item = xfs_ail_next(ailp, lip); 253249a8c11SDavid Chinner return lip; 254249a8c11SDavid Chinner } 255535f6b37SJosef 'Jeff' Sipek 2561d8c95a3SDave Chinner static struct xfs_log_item * 2571d8c95a3SDave Chinner __xfs_trans_ail_cursor_last( 2581d8c95a3SDave Chinner struct xfs_ail *ailp, 2591d8c95a3SDave Chinner xfs_lsn_t lsn) 2601d8c95a3SDave Chinner { 261efe2330fSChristoph Hellwig struct xfs_log_item *lip; 2621d8c95a3SDave Chinner 26357e80956SMatthew Wilcox list_for_each_entry_reverse(lip, &ailp->ail_head, li_ail) { 2641d8c95a3SDave Chinner if (XFS_LSN_CMP(lip->li_lsn, lsn) <= 0) 2651d8c95a3SDave Chinner return lip; 2661d8c95a3SDave Chinner } 2671d8c95a3SDave Chinner return NULL; 2681d8c95a3SDave Chinner } 2691d8c95a3SDave Chinner 2701d8c95a3SDave Chinner /* 27116b59029SDave Chinner * Find the last item in the AIL with the given @lsn by searching in descending 27216b59029SDave Chinner * LSN order and initialise the cursor to point to that item. If there is no 27316b59029SDave Chinner * item with the value of @lsn, then it sets the cursor to the last item with an 27416b59029SDave Chinner * LSN lower than @lsn. Returns NULL if the list is empty. 2751d8c95a3SDave Chinner */ 2761d8c95a3SDave Chinner struct xfs_log_item * 2771d8c95a3SDave Chinner xfs_trans_ail_cursor_last( 2781d8c95a3SDave Chinner struct xfs_ail *ailp, 2791d8c95a3SDave Chinner struct xfs_ail_cursor *cur, 2801d8c95a3SDave Chinner xfs_lsn_t lsn) 2811d8c95a3SDave Chinner { 2821d8c95a3SDave Chinner xfs_trans_ail_cursor_init(ailp, cur); 2831d8c95a3SDave Chinner cur->item = __xfs_trans_ail_cursor_last(ailp, lsn); 2841d8c95a3SDave Chinner return cur->item; 2851d8c95a3SDave Chinner } 2861d8c95a3SDave Chinner 2871d8c95a3SDave Chinner /* 28816b59029SDave Chinner * Splice the log item list into the AIL at the given LSN. We splice to the 2891d8c95a3SDave Chinner * tail of the given LSN to maintain insert order for push traversals. The 2901d8c95a3SDave Chinner * cursor is optional, allowing repeated updates to the same LSN to avoid 291e44f4112SAlex Elder * repeated traversals. This should not be called with an empty list. 292cd4a3c50SDave Chinner */ 293cd4a3c50SDave Chinner static void 294cd4a3c50SDave Chinner xfs_ail_splice( 295cd4a3c50SDave Chinner struct xfs_ail *ailp, 2961d8c95a3SDave Chinner struct xfs_ail_cursor *cur, 297cd4a3c50SDave Chinner struct list_head *list, 298cd4a3c50SDave Chinner xfs_lsn_t lsn) 299cd4a3c50SDave Chinner { 300e44f4112SAlex Elder struct xfs_log_item *lip; 301e44f4112SAlex Elder 302e44f4112SAlex Elder ASSERT(!list_empty(list)); 303cd4a3c50SDave Chinner 3041d8c95a3SDave Chinner /* 305e44f4112SAlex Elder * Use the cursor to determine the insertion point if one is 306e44f4112SAlex Elder * provided. If not, or if the one we got is not valid, 307e44f4112SAlex Elder * find the place in the AIL where the items belong. 3081d8c95a3SDave Chinner */ 309e44f4112SAlex Elder lip = cur ? cur->item : NULL; 310db9d67d6SChristoph Hellwig if (!lip || (uintptr_t)lip & 1) 3111d8c95a3SDave Chinner lip = __xfs_trans_ail_cursor_last(ailp, lsn); 3121d8c95a3SDave Chinner 313e44f4112SAlex Elder /* 314e44f4112SAlex Elder * If a cursor is provided, we know we're processing the AIL 315e44f4112SAlex Elder * in lsn order, and future items to be spliced in will 316e44f4112SAlex Elder * follow the last one being inserted now. Update the 317e44f4112SAlex Elder * cursor to point to that last item, now while we have a 318e44f4112SAlex Elder * reliable pointer to it. 319e44f4112SAlex Elder */ 3201d8c95a3SDave Chinner if (cur) 321e44f4112SAlex Elder cur->item = list_entry(list->prev, struct xfs_log_item, li_ail); 322cd4a3c50SDave Chinner 3231d8c95a3SDave Chinner /* 324e44f4112SAlex Elder * Finally perform the splice. Unless the AIL was empty, 325e44f4112SAlex Elder * lip points to the item in the AIL _after_ which the new 326e44f4112SAlex Elder * items should go. If lip is null the AIL was empty, so 327e44f4112SAlex Elder * the new items go at the head of the AIL. 3281d8c95a3SDave Chinner */ 329e44f4112SAlex Elder if (lip) 3301d8c95a3SDave Chinner list_splice(list, &lip->li_ail); 331e44f4112SAlex Elder else 33257e80956SMatthew Wilcox list_splice(list, &ailp->ail_head); 333cd4a3c50SDave Chinner } 334cd4a3c50SDave Chinner 335cd4a3c50SDave Chinner /* 336cd4a3c50SDave Chinner * Delete the given item from the AIL. Return a pointer to the item. 337cd4a3c50SDave Chinner */ 338cd4a3c50SDave Chinner static void 339cd4a3c50SDave Chinner xfs_ail_delete( 340cd4a3c50SDave Chinner struct xfs_ail *ailp, 341efe2330fSChristoph Hellwig struct xfs_log_item *lip) 342cd4a3c50SDave Chinner { 343cd4a3c50SDave Chinner xfs_ail_check(ailp, lip); 344cd4a3c50SDave Chinner list_del(&lip->li_ail); 345cd4a3c50SDave Chinner xfs_trans_ail_cursor_clear(ailp, lip); 346cd4a3c50SDave Chinner } 347cd4a3c50SDave Chinner 348*cb6ad099SBrian Foster /* 349*cb6ad099SBrian Foster * Requeue a failed buffer for writeback. 350*cb6ad099SBrian Foster * 351*cb6ad099SBrian Foster * We clear the log item failed state here as well, but we have to be careful 352*cb6ad099SBrian Foster * about reference counts because the only active reference counts on the buffer 353*cb6ad099SBrian Foster * may be the failed log items. Hence if we clear the log item failed state 354*cb6ad099SBrian Foster * before queuing the buffer for IO we can release all active references to 355*cb6ad099SBrian Foster * the buffer and free it, leading to use after free problems in 356*cb6ad099SBrian Foster * xfs_buf_delwri_queue. It makes no difference to the buffer or log items which 357*cb6ad099SBrian Foster * order we process them in - the buffer is locked, and we own the buffer list 358*cb6ad099SBrian Foster * so nothing on them is going to change while we are performing this action. 359*cb6ad099SBrian Foster * 360*cb6ad099SBrian Foster * Hence we can safely queue the buffer for IO before we clear the failed log 361*cb6ad099SBrian Foster * item state, therefore always having an active reference to the buffer and 362*cb6ad099SBrian Foster * avoiding the transient zero-reference state that leads to use-after-free. 363*cb6ad099SBrian Foster */ 364*cb6ad099SBrian Foster static inline int 365*cb6ad099SBrian Foster xfsaild_resubmit_item( 366*cb6ad099SBrian Foster struct xfs_log_item *lip, 367*cb6ad099SBrian Foster struct list_head *buffer_list) 368*cb6ad099SBrian Foster { 369*cb6ad099SBrian Foster struct xfs_buf *bp = lip->li_buf; 370*cb6ad099SBrian Foster 371*cb6ad099SBrian Foster if (!xfs_buf_trylock(bp)) 372*cb6ad099SBrian Foster return XFS_ITEM_LOCKED; 373*cb6ad099SBrian Foster 374*cb6ad099SBrian Foster if (!xfs_buf_delwri_queue(bp, buffer_list)) { 375*cb6ad099SBrian Foster xfs_buf_unlock(bp); 376*cb6ad099SBrian Foster return XFS_ITEM_FLUSHING; 377*cb6ad099SBrian Foster } 378*cb6ad099SBrian Foster 379*cb6ad099SBrian Foster /* protected by ail_lock */ 380*cb6ad099SBrian Foster list_for_each_entry(lip, &bp->b_li_list, li_bio_list) 381*cb6ad099SBrian Foster xfs_clear_li_failed(lip); 382*cb6ad099SBrian Foster 383*cb6ad099SBrian Foster xfs_buf_unlock(bp); 384*cb6ad099SBrian Foster return XFS_ITEM_SUCCESS; 385*cb6ad099SBrian Foster } 386*cb6ad099SBrian Foster 3877f4d01f3SBrian Foster static inline uint 3887f4d01f3SBrian Foster xfsaild_push_item( 3897f4d01f3SBrian Foster struct xfs_ail *ailp, 3907f4d01f3SBrian Foster struct xfs_log_item *lip) 3917f4d01f3SBrian Foster { 3927f4d01f3SBrian Foster /* 3937f4d01f3SBrian Foster * If log item pinning is enabled, skip the push and track the item as 3947f4d01f3SBrian Foster * pinned. This can help induce head-behind-tail conditions. 3957f4d01f3SBrian Foster */ 39657e80956SMatthew Wilcox if (XFS_TEST_ERROR(false, ailp->ail_mount, XFS_ERRTAG_LOG_ITEM_PIN)) 3977f4d01f3SBrian Foster return XFS_ITEM_PINNED; 3987f4d01f3SBrian Foster 399e8b78db7SChristoph Hellwig /* 400e8b78db7SChristoph Hellwig * Consider the item pinned if a push callback is not defined so the 401e8b78db7SChristoph Hellwig * caller will force the log. This should only happen for intent items 402e8b78db7SChristoph Hellwig * as they are unpinned once the associated done item is committed to 403e8b78db7SChristoph Hellwig * the on-disk log. 404e8b78db7SChristoph Hellwig */ 405e8b78db7SChristoph Hellwig if (!lip->li_ops->iop_push) 406e8b78db7SChristoph Hellwig return XFS_ITEM_PINNED; 407*cb6ad099SBrian Foster if (test_bit(XFS_LI_FAILED, &lip->li_flags)) 408*cb6ad099SBrian Foster return xfsaild_resubmit_item(lip, &ailp->ail_buf_list); 40957e80956SMatthew Wilcox return lip->li_ops->iop_push(lip, &ailp->ail_buf_list); 4107f4d01f3SBrian Foster } 4117f4d01f3SBrian Foster 4120030807cSChristoph Hellwig static long 4130030807cSChristoph Hellwig xfsaild_push( 4140030807cSChristoph Hellwig struct xfs_ail *ailp) 415249a8c11SDavid Chinner { 41657e80956SMatthew Wilcox xfs_mount_t *mp = ailp->ail_mount; 417af3e4022SDave Chinner struct xfs_ail_cursor cur; 418efe2330fSChristoph Hellwig struct xfs_log_item *lip; 4199e7004e7SDave Chinner xfs_lsn_t lsn; 420fe0da767SDave Chinner xfs_lsn_t target; 42143ff2122SChristoph Hellwig long tout; 4229e7004e7SDave Chinner int stuck = 0; 42343ff2122SChristoph Hellwig int flushing = 0; 4249e7004e7SDave Chinner int count = 0; 4251da177e4SLinus Torvalds 426670ce93fSDave Chinner /* 42743ff2122SChristoph Hellwig * If we encountered pinned items or did not finish writing out all 42843ff2122SChristoph Hellwig * buffers the last time we ran, force the log first and wait for it 42943ff2122SChristoph Hellwig * before pushing again. 430670ce93fSDave Chinner */ 43157e80956SMatthew Wilcox if (ailp->ail_log_flush && ailp->ail_last_pushed_lsn == 0 && 43257e80956SMatthew Wilcox (!list_empty_careful(&ailp->ail_buf_list) || 43343ff2122SChristoph Hellwig xfs_ail_min_lsn(ailp))) { 43457e80956SMatthew Wilcox ailp->ail_log_flush = 0; 43543ff2122SChristoph Hellwig 436ff6d6af2SBill O'Donnell XFS_STATS_INC(mp, xs_push_ail_flush); 437670ce93fSDave Chinner xfs_log_force(mp, XFS_LOG_SYNC); 438670ce93fSDave Chinner } 439670ce93fSDave Chinner 44057e80956SMatthew Wilcox spin_lock(&ailp->ail_lock); 4418375f922SBrian Foster 44257e80956SMatthew Wilcox /* barrier matches the ail_target update in xfs_ail_push() */ 4438375f922SBrian Foster smp_rmb(); 44457e80956SMatthew Wilcox target = ailp->ail_target; 44557e80956SMatthew Wilcox ailp->ail_target_prev = target; 4468375f922SBrian Foster 44757e80956SMatthew Wilcox lip = xfs_trans_ail_cursor_first(ailp, &cur, ailp->ail_last_pushed_lsn); 448211e4d43SChristoph Hellwig if (!lip) { 4491da177e4SLinus Torvalds /* 45043ff2122SChristoph Hellwig * If the AIL is empty or our push has reached the end we are 45143ff2122SChristoph Hellwig * done now. 4521da177e4SLinus Torvalds */ 453e4a1e29cSEric Sandeen xfs_trans_ail_cursor_done(&cur); 45457e80956SMatthew Wilcox spin_unlock(&ailp->ail_lock); 4559e7004e7SDave Chinner goto out_done; 4561da177e4SLinus Torvalds } 4571da177e4SLinus Torvalds 458ff6d6af2SBill O'Donnell XFS_STATS_INC(mp, xs_push_ail); 4591da177e4SLinus Torvalds 460249a8c11SDavid Chinner lsn = lip->li_lsn; 46150e86686SDave Chinner while ((XFS_LSN_CMP(lip->li_lsn, target) <= 0)) { 462249a8c11SDavid Chinner int lock_result; 46343ff2122SChristoph Hellwig 464249a8c11SDavid Chinner /* 465904c17e6SDave Chinner * Note that iop_push may unlock and reacquire the AIL lock. We 46643ff2122SChristoph Hellwig * rely on the AIL cursor implementation to be able to deal with 46743ff2122SChristoph Hellwig * the dropped lock. 4681da177e4SLinus Torvalds */ 4697f4d01f3SBrian Foster lock_result = xfsaild_push_item(ailp, lip); 4701da177e4SLinus Torvalds switch (lock_result) { 4711da177e4SLinus Torvalds case XFS_ITEM_SUCCESS: 472ff6d6af2SBill O'Donnell XFS_STATS_INC(mp, xs_push_ail_success); 4739e4c109aSChristoph Hellwig trace_xfs_ail_push(lip); 4749e4c109aSChristoph Hellwig 47557e80956SMatthew Wilcox ailp->ail_last_pushed_lsn = lsn; 4761da177e4SLinus Torvalds break; 4771da177e4SLinus Torvalds 47843ff2122SChristoph Hellwig case XFS_ITEM_FLUSHING: 47943ff2122SChristoph Hellwig /* 480cf085a1bSJoe Perches * The item or its backing buffer is already being 48143ff2122SChristoph Hellwig * flushed. The typical reason for that is that an 48243ff2122SChristoph Hellwig * inode buffer is locked because we already pushed the 48343ff2122SChristoph Hellwig * updates to it as part of inode clustering. 48443ff2122SChristoph Hellwig * 48543ff2122SChristoph Hellwig * We do not want to to stop flushing just because lots 486cf085a1bSJoe Perches * of items are already being flushed, but we need to 48743ff2122SChristoph Hellwig * re-try the flushing relatively soon if most of the 488cf085a1bSJoe Perches * AIL is being flushed. 48943ff2122SChristoph Hellwig */ 490ff6d6af2SBill O'Donnell XFS_STATS_INC(mp, xs_push_ail_flushing); 49143ff2122SChristoph Hellwig trace_xfs_ail_flushing(lip); 49217b38471SChristoph Hellwig 49343ff2122SChristoph Hellwig flushing++; 49457e80956SMatthew Wilcox ailp->ail_last_pushed_lsn = lsn; 4951da177e4SLinus Torvalds break; 4961da177e4SLinus Torvalds 4971da177e4SLinus Torvalds case XFS_ITEM_PINNED: 498ff6d6af2SBill O'Donnell XFS_STATS_INC(mp, xs_push_ail_pinned); 4999e4c109aSChristoph Hellwig trace_xfs_ail_pinned(lip); 5009e4c109aSChristoph Hellwig 501249a8c11SDavid Chinner stuck++; 50257e80956SMatthew Wilcox ailp->ail_log_flush++; 5031da177e4SLinus Torvalds break; 5041da177e4SLinus Torvalds case XFS_ITEM_LOCKED: 505ff6d6af2SBill O'Donnell XFS_STATS_INC(mp, xs_push_ail_locked); 5069e4c109aSChristoph Hellwig trace_xfs_ail_locked(lip); 50743ff2122SChristoph Hellwig 508249a8c11SDavid Chinner stuck++; 5091da177e4SLinus Torvalds break; 5101da177e4SLinus Torvalds default: 5111da177e4SLinus Torvalds ASSERT(0); 5121da177e4SLinus Torvalds break; 5131da177e4SLinus Torvalds } 5141da177e4SLinus Torvalds 515249a8c11SDavid Chinner count++; 516249a8c11SDavid Chinner 517249a8c11SDavid Chinner /* 518249a8c11SDavid Chinner * Are there too many items we can't do anything with? 51943ff2122SChristoph Hellwig * 520249a8c11SDavid Chinner * If we we are skipping too many items because we can't flush 521249a8c11SDavid Chinner * them or they are already being flushed, we back off and 522249a8c11SDavid Chinner * given them time to complete whatever operation is being 523249a8c11SDavid Chinner * done. i.e. remove pressure from the AIL while we can't make 524249a8c11SDavid Chinner * progress so traversals don't slow down further inserts and 525249a8c11SDavid Chinner * removals to/from the AIL. 526249a8c11SDavid Chinner * 527249a8c11SDavid Chinner * The value of 100 is an arbitrary magic number based on 528249a8c11SDavid Chinner * observation. 529249a8c11SDavid Chinner */ 530249a8c11SDavid Chinner if (stuck > 100) 531249a8c11SDavid Chinner break; 532249a8c11SDavid Chinner 533af3e4022SDave Chinner lip = xfs_trans_ail_cursor_next(ailp, &cur); 534249a8c11SDavid Chinner if (lip == NULL) 535249a8c11SDavid Chinner break; 536249a8c11SDavid Chinner lsn = lip->li_lsn; 5371da177e4SLinus Torvalds } 538e4a1e29cSEric Sandeen xfs_trans_ail_cursor_done(&cur); 53957e80956SMatthew Wilcox spin_unlock(&ailp->ail_lock); 5401da177e4SLinus Torvalds 54157e80956SMatthew Wilcox if (xfs_buf_delwri_submit_nowait(&ailp->ail_buf_list)) 54257e80956SMatthew Wilcox ailp->ail_log_flush++; 543d808f617SDave Chinner 54443ff2122SChristoph Hellwig if (!count || XFS_LSN_CMP(lsn, target) >= 0) { 5459e7004e7SDave Chinner out_done: 546249a8c11SDavid Chinner /* 54743ff2122SChristoph Hellwig * We reached the target or the AIL is empty, so wait a bit 54843ff2122SChristoph Hellwig * longer for I/O to complete and remove pushed items from the 54943ff2122SChristoph Hellwig * AIL before we start the next scan from the start of the AIL. 550249a8c11SDavid Chinner */ 551453eac8aSDave Chinner tout = 50; 55257e80956SMatthew Wilcox ailp->ail_last_pushed_lsn = 0; 55343ff2122SChristoph Hellwig } else if (((stuck + flushing) * 100) / count > 90) { 554249a8c11SDavid Chinner /* 55543ff2122SChristoph Hellwig * Either there is a lot of contention on the AIL or we are 55643ff2122SChristoph Hellwig * stuck due to operations in progress. "Stuck" in this case 55743ff2122SChristoph Hellwig * is defined as >90% of the items we tried to push were stuck. 558249a8c11SDavid Chinner * 559249a8c11SDavid Chinner * Backoff a bit more to allow some I/O to complete before 56043ff2122SChristoph Hellwig * restarting from the start of the AIL. This prevents us from 56143ff2122SChristoph Hellwig * spinning on the same items, and if they are pinned will all 56243ff2122SChristoph Hellwig * the restart to issue a log force to unpin the stuck items. 563249a8c11SDavid Chinner */ 564453eac8aSDave Chinner tout = 20; 56557e80956SMatthew Wilcox ailp->ail_last_pushed_lsn = 0; 56643ff2122SChristoph Hellwig } else { 56743ff2122SChristoph Hellwig /* 56843ff2122SChristoph Hellwig * Assume we have more work to do in a short while. 56943ff2122SChristoph Hellwig */ 57043ff2122SChristoph Hellwig tout = 10; 571453eac8aSDave Chinner } 5721da177e4SLinus Torvalds 5730030807cSChristoph Hellwig return tout; 5740030807cSChristoph Hellwig } 5750030807cSChristoph Hellwig 5760030807cSChristoph Hellwig static int 5770030807cSChristoph Hellwig xfsaild( 5780030807cSChristoph Hellwig void *data) 5790030807cSChristoph Hellwig { 5800030807cSChristoph Hellwig struct xfs_ail *ailp = data; 5810030807cSChristoph Hellwig long tout = 0; /* milliseconds */ 58210a98cb1SEric Biggers unsigned int noreclaim_flag; 5830030807cSChristoph Hellwig 58410a98cb1SEric Biggers noreclaim_flag = memalloc_noreclaim_save(); 58518f1df4eSMichal Hocko set_freezable(); 58643ff2122SChristoph Hellwig 5870bd89676SHou Tao while (1) { 5880030807cSChristoph Hellwig if (tout && tout <= 20) 5890bd89676SHou Tao set_current_state(TASK_KILLABLE); 5900030807cSChristoph Hellwig else 5910bd89676SHou Tao set_current_state(TASK_INTERRUPTIBLE); 5920bd89676SHou Tao 5930bd89676SHou Tao /* 594efc3289cSBrian Foster * Check kthread_should_stop() after we set the task state to 595efc3289cSBrian Foster * guarantee that we either see the stop bit and exit or the 596efc3289cSBrian Foster * task state is reset to runnable such that it's not scheduled 597efc3289cSBrian Foster * out indefinitely and detects the stop bit at next iteration. 5980bd89676SHou Tao * A memory barrier is included in above task state set to 5990bd89676SHou Tao * serialize again kthread_stop(). 6000bd89676SHou Tao */ 6010bd89676SHou Tao if (kthread_should_stop()) { 6020bd89676SHou Tao __set_current_state(TASK_RUNNING); 603efc3289cSBrian Foster 604efc3289cSBrian Foster /* 605efc3289cSBrian Foster * The caller forces out the AIL before stopping the 606efc3289cSBrian Foster * thread in the common case, which means the delwri 607efc3289cSBrian Foster * queue is drained. In the shutdown case, the queue may 608efc3289cSBrian Foster * still hold relogged buffers that haven't been 609efc3289cSBrian Foster * submitted because they were pinned since added to the 610efc3289cSBrian Foster * queue. 611efc3289cSBrian Foster * 612efc3289cSBrian Foster * Log I/O error processing stales the underlying buffer 613efc3289cSBrian Foster * and clears the delwri state, expecting the buf to be 614efc3289cSBrian Foster * removed on the next submission attempt. That won't 615efc3289cSBrian Foster * happen if we're shutting down, so this is the last 616efc3289cSBrian Foster * opportunity to release such buffers from the queue. 617efc3289cSBrian Foster */ 618efc3289cSBrian Foster ASSERT(list_empty(&ailp->ail_buf_list) || 619efc3289cSBrian Foster XFS_FORCED_SHUTDOWN(ailp->ail_mount)); 620efc3289cSBrian Foster xfs_buf_delwri_cancel(&ailp->ail_buf_list); 6210bd89676SHou Tao break; 6220bd89676SHou Tao } 6238375f922SBrian Foster 62457e80956SMatthew Wilcox spin_lock(&ailp->ail_lock); 6258375f922SBrian Foster 6268375f922SBrian Foster /* 6278375f922SBrian Foster * Idle if the AIL is empty and we are not racing with a target 6288375f922SBrian Foster * update. We check the AIL after we set the task to a sleep 62957e80956SMatthew Wilcox * state to guarantee that we either catch an ail_target update 6308375f922SBrian Foster * or that a wake_up resets the state to TASK_RUNNING. 6318375f922SBrian Foster * Otherwise, we run the risk of sleeping indefinitely. 6328375f922SBrian Foster * 63357e80956SMatthew Wilcox * The barrier matches the ail_target update in xfs_ail_push(). 6348375f922SBrian Foster */ 6358375f922SBrian Foster smp_rmb(); 6368375f922SBrian Foster if (!xfs_ail_min(ailp) && 63757e80956SMatthew Wilcox ailp->ail_target == ailp->ail_target_prev) { 63857e80956SMatthew Wilcox spin_unlock(&ailp->ail_lock); 63918f1df4eSMichal Hocko freezable_schedule(); 6408375f922SBrian Foster tout = 0; 6418375f922SBrian Foster continue; 6428375f922SBrian Foster } 64357e80956SMatthew Wilcox spin_unlock(&ailp->ail_lock); 6448375f922SBrian Foster 6458375f922SBrian Foster if (tout) 64618f1df4eSMichal Hocko freezable_schedule_timeout(msecs_to_jiffies(tout)); 6478375f922SBrian Foster 6488375f922SBrian Foster __set_current_state(TASK_RUNNING); 6490030807cSChristoph Hellwig 6500030807cSChristoph Hellwig try_to_freeze(); 6510030807cSChristoph Hellwig 6520030807cSChristoph Hellwig tout = xfsaild_push(ailp); 6530030807cSChristoph Hellwig } 6540030807cSChristoph Hellwig 65510a98cb1SEric Biggers memalloc_noreclaim_restore(noreclaim_flag); 6560030807cSChristoph Hellwig return 0; 6570bf6a5bdSDave Chinner } 6580bf6a5bdSDave Chinner 6590bf6a5bdSDave Chinner /* 6600bf6a5bdSDave Chinner * This routine is called to move the tail of the AIL forward. It does this by 6610bf6a5bdSDave Chinner * trying to flush items in the AIL whose lsns are below the given 6620bf6a5bdSDave Chinner * threshold_lsn. 6630bf6a5bdSDave Chinner * 6640bf6a5bdSDave Chinner * The push is run asynchronously in a workqueue, which means the caller needs 6650bf6a5bdSDave Chinner * to handle waiting on the async flush for space to become available. 6660bf6a5bdSDave Chinner * We don't want to interrupt any push that is in progress, hence we only queue 667cf085a1bSJoe Perches * work if we set the pushing bit appropriately. 6680bf6a5bdSDave Chinner * 6690bf6a5bdSDave Chinner * We do this unlocked - we only need to know whether there is anything in the 6700bf6a5bdSDave Chinner * AIL at the time we are called. We don't need to access the contents of 6710bf6a5bdSDave Chinner * any of the objects, so the lock is not needed. 6720bf6a5bdSDave Chinner */ 6730bf6a5bdSDave Chinner void 674fd074841SDave Chinner xfs_ail_push( 6750bf6a5bdSDave Chinner struct xfs_ail *ailp, 6760bf6a5bdSDave Chinner xfs_lsn_t threshold_lsn) 6770bf6a5bdSDave Chinner { 678efe2330fSChristoph Hellwig struct xfs_log_item *lip; 6790bf6a5bdSDave Chinner 6800bf6a5bdSDave Chinner lip = xfs_ail_min(ailp); 68157e80956SMatthew Wilcox if (!lip || XFS_FORCED_SHUTDOWN(ailp->ail_mount) || 68257e80956SMatthew Wilcox XFS_LSN_CMP(threshold_lsn, ailp->ail_target) <= 0) 6830bf6a5bdSDave Chinner return; 6840bf6a5bdSDave Chinner 6850bf6a5bdSDave Chinner /* 6860bf6a5bdSDave Chinner * Ensure that the new target is noticed in push code before it clears 6870bf6a5bdSDave Chinner * the XFS_AIL_PUSHING_BIT. 6880bf6a5bdSDave Chinner */ 6890bf6a5bdSDave Chinner smp_wmb(); 69057e80956SMatthew Wilcox xfs_trans_ail_copy_lsn(ailp, &ailp->ail_target, &threshold_lsn); 6910030807cSChristoph Hellwig smp_wmb(); 6920030807cSChristoph Hellwig 69357e80956SMatthew Wilcox wake_up_process(ailp->ail_task); 6940bf6a5bdSDave Chinner } 6951da177e4SLinus Torvalds 6961da177e4SLinus Torvalds /* 697fd074841SDave Chinner * Push out all items in the AIL immediately 698fd074841SDave Chinner */ 699fd074841SDave Chinner void 700fd074841SDave Chinner xfs_ail_push_all( 701fd074841SDave Chinner struct xfs_ail *ailp) 702fd074841SDave Chinner { 703fd074841SDave Chinner xfs_lsn_t threshold_lsn = xfs_ail_max_lsn(ailp); 704fd074841SDave Chinner 705fd074841SDave Chinner if (threshold_lsn) 706fd074841SDave Chinner xfs_ail_push(ailp, threshold_lsn); 707fd074841SDave Chinner } 708fd074841SDave Chinner 709fd074841SDave Chinner /* 710211e4d43SChristoph Hellwig * Push out all items in the AIL immediately and wait until the AIL is empty. 711211e4d43SChristoph Hellwig */ 712211e4d43SChristoph Hellwig void 713211e4d43SChristoph Hellwig xfs_ail_push_all_sync( 714211e4d43SChristoph Hellwig struct xfs_ail *ailp) 715211e4d43SChristoph Hellwig { 716211e4d43SChristoph Hellwig struct xfs_log_item *lip; 717211e4d43SChristoph Hellwig DEFINE_WAIT(wait); 718211e4d43SChristoph Hellwig 71957e80956SMatthew Wilcox spin_lock(&ailp->ail_lock); 720211e4d43SChristoph Hellwig while ((lip = xfs_ail_max(ailp)) != NULL) { 72157e80956SMatthew Wilcox prepare_to_wait(&ailp->ail_empty, &wait, TASK_UNINTERRUPTIBLE); 72257e80956SMatthew Wilcox ailp->ail_target = lip->li_lsn; 72357e80956SMatthew Wilcox wake_up_process(ailp->ail_task); 72457e80956SMatthew Wilcox spin_unlock(&ailp->ail_lock); 725211e4d43SChristoph Hellwig schedule(); 72657e80956SMatthew Wilcox spin_lock(&ailp->ail_lock); 727211e4d43SChristoph Hellwig } 72857e80956SMatthew Wilcox spin_unlock(&ailp->ail_lock); 729211e4d43SChristoph Hellwig 73057e80956SMatthew Wilcox finish_wait(&ailp->ail_empty, &wait); 731211e4d43SChristoph Hellwig } 732211e4d43SChristoph Hellwig 7334165994aSDave Chinner void 7344165994aSDave Chinner xfs_ail_update_finish( 7354165994aSDave Chinner struct xfs_ail *ailp, 7368eb807bdSDave Chinner xfs_lsn_t old_lsn) __releases(ailp->ail_lock) 7374165994aSDave Chinner { 7384165994aSDave Chinner struct xfs_mount *mp = ailp->ail_mount; 7394165994aSDave Chinner 7408eb807bdSDave Chinner /* if the tail lsn hasn't changed, don't do updates or wakeups. */ 7418eb807bdSDave Chinner if (!old_lsn || old_lsn == __xfs_ail_min_lsn(ailp)) { 7424165994aSDave Chinner spin_unlock(&ailp->ail_lock); 7434165994aSDave Chinner return; 7444165994aSDave Chinner } 7454165994aSDave Chinner 7464165994aSDave Chinner if (!XFS_FORCED_SHUTDOWN(mp)) 7474165994aSDave Chinner xlog_assign_tail_lsn_locked(mp); 7484165994aSDave Chinner 7494165994aSDave Chinner if (list_empty(&ailp->ail_head)) 7504165994aSDave Chinner wake_up_all(&ailp->ail_empty); 7514165994aSDave Chinner spin_unlock(&ailp->ail_lock); 7524165994aSDave Chinner xfs_log_space_wake(mp); 7534165994aSDave Chinner } 7544165994aSDave Chinner 755211e4d43SChristoph Hellwig /* 7560e57f6a3SDave Chinner * xfs_trans_ail_update - bulk AIL insertion operation. 7570e57f6a3SDave Chinner * 7580e57f6a3SDave Chinner * @xfs_trans_ail_update takes an array of log items that all need to be 7590e57f6a3SDave Chinner * positioned at the same LSN in the AIL. If an item is not in the AIL, it will 7600e57f6a3SDave Chinner * be added. Otherwise, it will be repositioned by removing it and re-adding 7610e57f6a3SDave Chinner * it to the AIL. If we move the first item in the AIL, update the log tail to 7620e57f6a3SDave Chinner * match the new minimum LSN in the AIL. 7630e57f6a3SDave Chinner * 7640e57f6a3SDave Chinner * This function takes the AIL lock once to execute the update operations on 7650e57f6a3SDave Chinner * all the items in the array, and as such should not be called with the AIL 7660e57f6a3SDave Chinner * lock held. As a result, once we have the AIL lock, we need to check each log 7670e57f6a3SDave Chinner * item LSN to confirm it needs to be moved forward in the AIL. 7680e57f6a3SDave Chinner * 7690e57f6a3SDave Chinner * To optimise the insert operation, we delete all the items from the AIL in 7700e57f6a3SDave Chinner * the first pass, moving them into a temporary list, then splice the temporary 7710e57f6a3SDave Chinner * list into the correct position in the AIL. This avoids needing to do an 7720e57f6a3SDave Chinner * insert operation on every item. 7730e57f6a3SDave Chinner * 7740e57f6a3SDave Chinner * This function must be called with the AIL lock held. The lock is dropped 7750e57f6a3SDave Chinner * before returning. 7760e57f6a3SDave Chinner */ 7770e57f6a3SDave Chinner void 7780e57f6a3SDave Chinner xfs_trans_ail_update_bulk( 7790e57f6a3SDave Chinner struct xfs_ail *ailp, 7801d8c95a3SDave Chinner struct xfs_ail_cursor *cur, 7810e57f6a3SDave Chinner struct xfs_log_item **log_items, 7820e57f6a3SDave Chinner int nr_items, 78357e80956SMatthew Wilcox xfs_lsn_t lsn) __releases(ailp->ail_lock) 7840e57f6a3SDave Chinner { 785efe2330fSChristoph Hellwig struct xfs_log_item *mlip; 7868eb807bdSDave Chinner xfs_lsn_t tail_lsn = 0; 7870e57f6a3SDave Chinner int i; 7880e57f6a3SDave Chinner LIST_HEAD(tmp); 7890e57f6a3SDave Chinner 790e44f4112SAlex Elder ASSERT(nr_items > 0); /* Not required, but true. */ 7910e57f6a3SDave Chinner mlip = xfs_ail_min(ailp); 7920e57f6a3SDave Chinner 7930e57f6a3SDave Chinner for (i = 0; i < nr_items; i++) { 7940e57f6a3SDave Chinner struct xfs_log_item *lip = log_items[i]; 79522525c17SDave Chinner if (test_and_set_bit(XFS_LI_IN_AIL, &lip->li_flags)) { 7960e57f6a3SDave Chinner /* check if we really need to move the item */ 7970e57f6a3SDave Chinner if (XFS_LSN_CMP(lsn, lip->li_lsn) <= 0) 7980e57f6a3SDave Chinner continue; 7990e57f6a3SDave Chinner 800750b9c90SDave Chinner trace_xfs_ail_move(lip, lip->li_lsn, lsn); 8018eb807bdSDave Chinner if (mlip == lip && !tail_lsn) 8028eb807bdSDave Chinner tail_lsn = lip->li_lsn; 8038eb807bdSDave Chinner 8040e57f6a3SDave Chinner xfs_ail_delete(ailp, lip); 8050e57f6a3SDave Chinner } else { 806750b9c90SDave Chinner trace_xfs_ail_insert(lip, 0, lsn); 8070e57f6a3SDave Chinner } 8080e57f6a3SDave Chinner lip->li_lsn = lsn; 8090e57f6a3SDave Chinner list_add(&lip->li_ail, &tmp); 8100e57f6a3SDave Chinner } 8110e57f6a3SDave Chinner 812e44f4112SAlex Elder if (!list_empty(&tmp)) 8131d8c95a3SDave Chinner xfs_ail_splice(ailp, cur, &tmp, lsn); 8141c304625SChristoph Hellwig 8158eb807bdSDave Chinner xfs_ail_update_finish(ailp, tail_lsn); 8160e57f6a3SDave Chinner } 8170e57f6a3SDave Chinner 8188eb807bdSDave Chinner /* 8198eb807bdSDave Chinner * Delete one log item from the AIL. 8208eb807bdSDave Chinner * 8218eb807bdSDave Chinner * If this item was at the tail of the AIL, return the LSN of the log item so 8228eb807bdSDave Chinner * that we can use it to check if the LSN of the tail of the log has moved 8238eb807bdSDave Chinner * when finishing up the AIL delete process in xfs_ail_update_finish(). 8248eb807bdSDave Chinner */ 8258eb807bdSDave Chinner xfs_lsn_t 82627af1bbfSChristoph Hellwig xfs_ail_delete_one( 82727af1bbfSChristoph Hellwig struct xfs_ail *ailp, 82827af1bbfSChristoph Hellwig struct xfs_log_item *lip) 82927af1bbfSChristoph Hellwig { 83027af1bbfSChristoph Hellwig struct xfs_log_item *mlip = xfs_ail_min(ailp); 8318eb807bdSDave Chinner xfs_lsn_t lsn = lip->li_lsn; 83227af1bbfSChristoph Hellwig 83327af1bbfSChristoph Hellwig trace_xfs_ail_delete(lip, mlip->li_lsn, lip->li_lsn); 83427af1bbfSChristoph Hellwig xfs_ail_delete(ailp, lip); 835d3a304b6SCarlos Maiolino xfs_clear_li_failed(lip); 83622525c17SDave Chinner clear_bit(XFS_LI_IN_AIL, &lip->li_flags); 83727af1bbfSChristoph Hellwig lip->li_lsn = 0; 83827af1bbfSChristoph Hellwig 8398eb807bdSDave Chinner if (mlip == lip) 8408eb807bdSDave Chinner return lsn; 8418eb807bdSDave Chinner return 0; 84227af1bbfSChristoph Hellwig } 84327af1bbfSChristoph Hellwig 84427af1bbfSChristoph Hellwig /** 84527af1bbfSChristoph Hellwig * Remove a log items from the AIL 84630136832SDave Chinner * 84730136832SDave Chinner * @xfs_trans_ail_delete_bulk takes an array of log items that all need to 84830136832SDave Chinner * removed from the AIL. The caller is already holding the AIL lock, and done 84930136832SDave Chinner * all the checks necessary to ensure the items passed in via @log_items are 85030136832SDave Chinner * ready for deletion. This includes checking that the items are in the AIL. 85130136832SDave Chinner * 85230136832SDave Chinner * For each log item to be removed, unlink it from the AIL, clear the IN_AIL 85330136832SDave Chinner * flag from the item and reset the item's lsn to 0. If we remove the first 85430136832SDave Chinner * item in the AIL, update the log tail to match the new minimum LSN in the 85530136832SDave Chinner * AIL. 85630136832SDave Chinner * 85730136832SDave Chinner * This function will not drop the AIL lock until all items are removed from 85830136832SDave Chinner * the AIL to minimise the amount of lock traffic on the AIL. This does not 85930136832SDave Chinner * greatly increase the AIL hold time, but does significantly reduce the amount 86030136832SDave Chinner * of traffic on the lock, especially during IO completion. 86130136832SDave Chinner * 86230136832SDave Chinner * This function must be called with the AIL lock held. The lock is dropped 86330136832SDave Chinner * before returning. 86430136832SDave Chinner */ 86530136832SDave Chinner void 86627af1bbfSChristoph Hellwig xfs_trans_ail_delete( 86730136832SDave Chinner struct xfs_ail *ailp, 86827af1bbfSChristoph Hellwig struct xfs_log_item *lip, 8694165994aSDave Chinner int shutdown_type) 87030136832SDave Chinner { 87157e80956SMatthew Wilcox struct xfs_mount *mp = ailp->ail_mount; 8728eb807bdSDave Chinner xfs_lsn_t tail_lsn; 87330136832SDave Chinner 87422525c17SDave Chinner if (!test_bit(XFS_LI_IN_AIL, &lip->li_flags)) { 87557e80956SMatthew Wilcox spin_unlock(&ailp->ail_lock); 87630136832SDave Chinner if (!XFS_FORCED_SHUTDOWN(mp)) { 8776a19d939SDave Chinner xfs_alert_tag(mp, XFS_PTAG_AILDELETE, 87830136832SDave Chinner "%s: attempting to delete a log item that is not in the AIL", 87930136832SDave Chinner __func__); 88004913fddSDave Chinner xfs_force_shutdown(mp, shutdown_type); 88130136832SDave Chinner } 88230136832SDave Chinner return; 88330136832SDave Chinner } 88430136832SDave Chinner 8858eb807bdSDave Chinner tail_lsn = xfs_ail_delete_one(ailp, lip); 8868eb807bdSDave Chinner xfs_ail_update_finish(ailp, tail_lsn); 88730136832SDave Chinner } 8881da177e4SLinus Torvalds 889249a8c11SDavid Chinner int 8901da177e4SLinus Torvalds xfs_trans_ail_init( 8911da177e4SLinus Torvalds xfs_mount_t *mp) 8921da177e4SLinus Torvalds { 89382fa9012SDavid Chinner struct xfs_ail *ailp; 89482fa9012SDavid Chinner 89582fa9012SDavid Chinner ailp = kmem_zalloc(sizeof(struct xfs_ail), KM_MAYFAIL); 89682fa9012SDavid Chinner if (!ailp) 8972451337dSDave Chinner return -ENOMEM; 89882fa9012SDavid Chinner 89957e80956SMatthew Wilcox ailp->ail_mount = mp; 90057e80956SMatthew Wilcox INIT_LIST_HEAD(&ailp->ail_head); 90157e80956SMatthew Wilcox INIT_LIST_HEAD(&ailp->ail_cursors); 90257e80956SMatthew Wilcox spin_lock_init(&ailp->ail_lock); 90357e80956SMatthew Wilcox INIT_LIST_HEAD(&ailp->ail_buf_list); 90457e80956SMatthew Wilcox init_waitqueue_head(&ailp->ail_empty); 9050030807cSChristoph Hellwig 90657e80956SMatthew Wilcox ailp->ail_task = kthread_run(xfsaild, ailp, "xfsaild/%s", 907e1d3d218SIan Kent ailp->ail_mount->m_super->s_id); 90857e80956SMatthew Wilcox if (IS_ERR(ailp->ail_task)) 9090030807cSChristoph Hellwig goto out_free_ailp; 9100030807cSChristoph Hellwig 91127d8d5feSDavid Chinner mp->m_ail = ailp; 91227d8d5feSDavid Chinner return 0; 9130030807cSChristoph Hellwig 9140030807cSChristoph Hellwig out_free_ailp: 9150030807cSChristoph Hellwig kmem_free(ailp); 9162451337dSDave Chinner return -ENOMEM; 917249a8c11SDavid Chinner } 918249a8c11SDavid Chinner 919249a8c11SDavid Chinner void 920249a8c11SDavid Chinner xfs_trans_ail_destroy( 921249a8c11SDavid Chinner xfs_mount_t *mp) 922249a8c11SDavid Chinner { 92382fa9012SDavid Chinner struct xfs_ail *ailp = mp->m_ail; 92482fa9012SDavid Chinner 92557e80956SMatthew Wilcox kthread_stop(ailp->ail_task); 92682fa9012SDavid Chinner kmem_free(ailp); 9271da177e4SLinus Torvalds } 928