Dave Chinner | 0b61f8a | 2018-06-05 19:42:14 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 3 | * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc. |
David Chinner | c7e8f26 | 2008-10-30 17:39:23 +1100 | [diff] [blame] | 4 | * Copyright (c) 2008 Dave Chinner |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 5 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include "xfs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 8 | #include "xfs_fs.h" |
Darrick J. Wong | 5467b34 | 2019-06-28 19:25:35 -0700 | [diff] [blame] | 9 | #include "xfs_shared.h" |
Christoph Hellwig | 4fb6e8a | 2014-11-28 14:25:04 +1100 | [diff] [blame] | 10 | #include "xfs_format.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 11 | #include "xfs_log_format.h" |
| 12 | #include "xfs_trans_resv.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include "xfs_mount.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 14 | #include "xfs_trans.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include "xfs_trans_priv.h" |
Christoph Hellwig | 9e4c109 | 2011-10-11 15:14:11 +0000 | [diff] [blame] | 16 | #include "xfs_trace.h" |
Darrick J. Wong | e9e899a | 2017-10-31 12:04:49 -0700 | [diff] [blame] | 17 | #include "xfs_errortag.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "xfs_error.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 19 | #include "xfs_log.h" |
Dave Chinner | 0020a19 | 2021-08-10 18:00:44 -0700 | [diff] [blame] | 20 | #include "xfs_log_priv.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #ifdef DEBUG |
Dave Chinner | cd4a3c5 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 23 | /* |
| 24 | * Check that the list is sorted as it should be. |
Dave Chinner | d686d12 | 2018-05-09 07:49:09 -0700 | [diff] [blame] | 25 | * |
| 26 | * Called with the ail lock held, but we don't want to assert fail with it |
| 27 | * held otherwise we'll lock everything up and won't be able to debug the |
| 28 | * cause. Hence we sample and check the state under the AIL lock and return if |
| 29 | * everything is fine, otherwise we drop the lock and run the ASSERT checks. |
| 30 | * Asserts may not be fatal, so pick the lock back up and continue onwards. |
Dave Chinner | cd4a3c5 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 31 | */ |
| 32 | STATIC void |
| 33 | xfs_ail_check( |
Dave Chinner | d686d12 | 2018-05-09 07:49:09 -0700 | [diff] [blame] | 34 | struct xfs_ail *ailp, |
| 35 | struct xfs_log_item *lip) |
Jules Irenge | daebba1 | 2020-02-26 09:37:15 -0800 | [diff] [blame] | 36 | __must_hold(&ailp->ail_lock) |
Dave Chinner | cd4a3c5 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 37 | { |
Dave Chinner | d686d12 | 2018-05-09 07:49:09 -0700 | [diff] [blame] | 38 | struct xfs_log_item *prev_lip; |
| 39 | struct xfs_log_item *next_lip; |
| 40 | xfs_lsn_t prev_lsn = NULLCOMMITLSN; |
| 41 | xfs_lsn_t next_lsn = NULLCOMMITLSN; |
| 42 | xfs_lsn_t lsn; |
| 43 | bool in_ail; |
| 44 | |
Dave Chinner | cd4a3c5 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 45 | |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 46 | if (list_empty(&ailp->ail_head)) |
Dave Chinner | cd4a3c5 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 47 | return; |
| 48 | |
| 49 | /* |
Dave Chinner | d686d12 | 2018-05-09 07:49:09 -0700 | [diff] [blame] | 50 | * Sample then check the next and previous entries are valid. |
Dave Chinner | cd4a3c5 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 51 | */ |
Dave Chinner | d686d12 | 2018-05-09 07:49:09 -0700 | [diff] [blame] | 52 | in_ail = test_bit(XFS_LI_IN_AIL, &lip->li_flags); |
| 53 | prev_lip = list_entry(lip->li_ail.prev, struct xfs_log_item, li_ail); |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 54 | if (&prev_lip->li_ail != &ailp->ail_head) |
Dave Chinner | d686d12 | 2018-05-09 07:49:09 -0700 | [diff] [blame] | 55 | prev_lsn = prev_lip->li_lsn; |
| 56 | next_lip = list_entry(lip->li_ail.next, struct xfs_log_item, li_ail); |
| 57 | if (&next_lip->li_ail != &ailp->ail_head) |
| 58 | next_lsn = next_lip->li_lsn; |
| 59 | lsn = lip->li_lsn; |
Dave Chinner | cd4a3c5 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 60 | |
Dave Chinner | d686d12 | 2018-05-09 07:49:09 -0700 | [diff] [blame] | 61 | if (in_ail && |
| 62 | (prev_lsn == NULLCOMMITLSN || XFS_LSN_CMP(prev_lsn, lsn) <= 0) && |
| 63 | (next_lsn == NULLCOMMITLSN || XFS_LSN_CMP(next_lsn, lsn) >= 0)) |
| 64 | return; |
Dave Chinner | cd4a3c5 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 65 | |
Dave Chinner | d686d12 | 2018-05-09 07:49:09 -0700 | [diff] [blame] | 66 | spin_unlock(&ailp->ail_lock); |
| 67 | ASSERT(in_ail); |
| 68 | ASSERT(prev_lsn == NULLCOMMITLSN || XFS_LSN_CMP(prev_lsn, lsn) <= 0); |
| 69 | ASSERT(next_lsn == NULLCOMMITLSN || XFS_LSN_CMP(next_lsn, lsn) >= 0); |
| 70 | spin_lock(&ailp->ail_lock); |
Dave Chinner | cd4a3c5 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 71 | } |
| 72 | #else /* !DEBUG */ |
David Chinner | de08dbc | 2008-02-05 12:13:38 +1100 | [diff] [blame] | 73 | #define xfs_ail_check(a,l) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | #endif /* DEBUG */ |
| 75 | |
Dave Chinner | cd4a3c5 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 76 | /* |
Dave Chinner | fd07484 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 77 | * Return a pointer to the last item in the AIL. If the AIL is empty, then |
| 78 | * return NULL. |
| 79 | */ |
Christoph Hellwig | efe2330 | 2019-06-28 19:27:33 -0700 | [diff] [blame] | 80 | static struct xfs_log_item * |
Dave Chinner | fd07484 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 81 | xfs_ail_max( |
| 82 | struct xfs_ail *ailp) |
| 83 | { |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 84 | if (list_empty(&ailp->ail_head)) |
Dave Chinner | fd07484 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 85 | return NULL; |
| 86 | |
Christoph Hellwig | efe2330 | 2019-06-28 19:27:33 -0700 | [diff] [blame] | 87 | return list_entry(ailp->ail_head.prev, struct xfs_log_item, li_ail); |
Dave Chinner | fd07484 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 88 | } |
| 89 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | /* |
Dave Chinner | cd4a3c5 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 91 | * Return a pointer to the item which follows the given item in the AIL. If |
| 92 | * the given item is the last item in the list, then return NULL. |
| 93 | */ |
Christoph Hellwig | efe2330 | 2019-06-28 19:27:33 -0700 | [diff] [blame] | 94 | static struct xfs_log_item * |
Dave Chinner | cd4a3c5 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 95 | xfs_ail_next( |
Christoph Hellwig | efe2330 | 2019-06-28 19:27:33 -0700 | [diff] [blame] | 96 | struct xfs_ail *ailp, |
| 97 | struct xfs_log_item *lip) |
Dave Chinner | cd4a3c5 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 98 | { |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 99 | if (lip->li_ail.next == &ailp->ail_head) |
Dave Chinner | cd4a3c5 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 100 | return NULL; |
| 101 | |
Christoph Hellwig | efe2330 | 2019-06-28 19:27:33 -0700 | [diff] [blame] | 102 | return list_first_entry(&lip->li_ail, struct xfs_log_item, li_ail); |
Dave Chinner | cd4a3c5 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /* |
| 106 | * This is called by the log manager code to determine the LSN of the tail of |
| 107 | * the log. This is exactly the LSN of the first item in the AIL. If the AIL |
| 108 | * is empty, then this function returns 0. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | * |
Dave Chinner | cd4a3c5 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 110 | * We need the AIL lock in order to get a coherent read of the lsn of the last |
| 111 | * item in the AIL. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | */ |
Dave Chinner | 8eb807b | 2020-03-24 20:10:29 -0700 | [diff] [blame] | 113 | static xfs_lsn_t |
| 114 | __xfs_ail_min_lsn( |
| 115 | struct xfs_ail *ailp) |
| 116 | { |
| 117 | struct xfs_log_item *lip = xfs_ail_min(ailp); |
| 118 | |
| 119 | if (lip) |
| 120 | return lip->li_lsn; |
| 121 | return 0; |
| 122 | } |
| 123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | xfs_lsn_t |
Dave Chinner | fd07484 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 125 | xfs_ail_min_lsn( |
Christoph Hellwig | efe2330 | 2019-06-28 19:27:33 -0700 | [diff] [blame] | 126 | struct xfs_ail *ailp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | { |
Dave Chinner | 8eb807b | 2020-03-24 20:10:29 -0700 | [diff] [blame] | 128 | xfs_lsn_t lsn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 130 | spin_lock(&ailp->ail_lock); |
Dave Chinner | 8eb807b | 2020-03-24 20:10:29 -0700 | [diff] [blame] | 131 | lsn = __xfs_ail_min_lsn(ailp); |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 132 | spin_unlock(&ailp->ail_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
| 134 | return lsn; |
| 135 | } |
| 136 | |
| 137 | /* |
Dave Chinner | fd07484 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 138 | * Return the maximum lsn held in the AIL, or zero if the AIL is empty. |
| 139 | */ |
| 140 | static xfs_lsn_t |
| 141 | xfs_ail_max_lsn( |
Christoph Hellwig | efe2330 | 2019-06-28 19:27:33 -0700 | [diff] [blame] | 142 | struct xfs_ail *ailp) |
Dave Chinner | fd07484 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 143 | { |
Christoph Hellwig | efe2330 | 2019-06-28 19:27:33 -0700 | [diff] [blame] | 144 | xfs_lsn_t lsn = 0; |
| 145 | struct xfs_log_item *lip; |
Dave Chinner | fd07484 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 146 | |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 147 | spin_lock(&ailp->ail_lock); |
Dave Chinner | fd07484 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 148 | lip = xfs_ail_max(ailp); |
| 149 | if (lip) |
| 150 | lsn = lip->li_lsn; |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 151 | spin_unlock(&ailp->ail_lock); |
Dave Chinner | fd07484 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 152 | |
| 153 | return lsn; |
| 154 | } |
| 155 | |
| 156 | /* |
Dave Chinner | af3e402 | 2011-07-18 03:40:18 +0000 | [diff] [blame] | 157 | * The cursor keeps track of where our current traversal is up to by tracking |
| 158 | * the next item in the list for us. However, for this to be safe, removing an |
| 159 | * object from the AIL needs to invalidate any cursor that points to it. hence |
| 160 | * the traversal cursor needs to be linked to the struct xfs_ail so that |
| 161 | * deletion can search all the active cursors for invalidation. |
David Chinner | 27d8d5f | 2008-10-30 17:38:39 +1100 | [diff] [blame] | 162 | */ |
David Chinner | 5b00f14 | 2008-10-30 17:39:00 +1100 | [diff] [blame] | 163 | STATIC void |
David Chinner | 27d8d5f | 2008-10-30 17:38:39 +1100 | [diff] [blame] | 164 | xfs_trans_ail_cursor_init( |
| 165 | struct xfs_ail *ailp, |
| 166 | struct xfs_ail_cursor *cur) |
| 167 | { |
| 168 | cur->item = NULL; |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 169 | list_add_tail(&cur->list, &ailp->ail_cursors); |
David Chinner | 27d8d5f | 2008-10-30 17:38:39 +1100 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /* |
Dave Chinner | af3e402 | 2011-07-18 03:40:18 +0000 | [diff] [blame] | 173 | * Get the next item in the traversal and advance the cursor. If the cursor |
| 174 | * was invalidated (indicated by a lip of 1), restart the traversal. |
David Chinner | 27d8d5f | 2008-10-30 17:38:39 +1100 | [diff] [blame] | 175 | */ |
David Chinner | 5b00f14 | 2008-10-30 17:39:00 +1100 | [diff] [blame] | 176 | struct xfs_log_item * |
David Chinner | 27d8d5f | 2008-10-30 17:38:39 +1100 | [diff] [blame] | 177 | xfs_trans_ail_cursor_next( |
| 178 | struct xfs_ail *ailp, |
| 179 | struct xfs_ail_cursor *cur) |
| 180 | { |
| 181 | struct xfs_log_item *lip = cur->item; |
| 182 | |
Christoph Hellwig | db9d67d | 2015-06-22 09:43:32 +1000 | [diff] [blame] | 183 | if ((uintptr_t)lip & 1) |
David Chinner | 27d8d5f | 2008-10-30 17:38:39 +1100 | [diff] [blame] | 184 | lip = xfs_ail_min(ailp); |
Dave Chinner | 16b5902 | 2011-07-18 03:40:17 +0000 | [diff] [blame] | 185 | if (lip) |
| 186 | cur->item = xfs_ail_next(ailp, lip); |
David Chinner | 27d8d5f | 2008-10-30 17:38:39 +1100 | [diff] [blame] | 187 | return lip; |
| 188 | } |
| 189 | |
| 190 | /* |
Dave Chinner | af3e402 | 2011-07-18 03:40:18 +0000 | [diff] [blame] | 191 | * When the traversal is complete, we need to remove the cursor from the list |
| 192 | * of traversing cursors. |
David Chinner | 27d8d5f | 2008-10-30 17:38:39 +1100 | [diff] [blame] | 193 | */ |
| 194 | void |
| 195 | xfs_trans_ail_cursor_done( |
Dave Chinner | af3e402 | 2011-07-18 03:40:18 +0000 | [diff] [blame] | 196 | struct xfs_ail_cursor *cur) |
David Chinner | 27d8d5f | 2008-10-30 17:38:39 +1100 | [diff] [blame] | 197 | { |
Dave Chinner | af3e402 | 2011-07-18 03:40:18 +0000 | [diff] [blame] | 198 | cur->item = NULL; |
| 199 | list_del_init(&cur->list); |
David Chinner | 27d8d5f | 2008-10-30 17:38:39 +1100 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /* |
Dave Chinner | af3e402 | 2011-07-18 03:40:18 +0000 | [diff] [blame] | 203 | * Invalidate any cursor that is pointing to this item. This is called when an |
| 204 | * item is removed from the AIL. Any cursor pointing to this object is now |
| 205 | * invalid and the traversal needs to be terminated so it doesn't reference a |
| 206 | * freed object. We set the low bit of the cursor item pointer so we can |
| 207 | * distinguish between an invalidation and the end of the list when getting the |
| 208 | * next item from the cursor. |
David Chinner | 5b00f14 | 2008-10-30 17:39:00 +1100 | [diff] [blame] | 209 | */ |
| 210 | STATIC void |
| 211 | xfs_trans_ail_cursor_clear( |
| 212 | struct xfs_ail *ailp, |
| 213 | struct xfs_log_item *lip) |
| 214 | { |
| 215 | struct xfs_ail_cursor *cur; |
| 216 | |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 217 | list_for_each_entry(cur, &ailp->ail_cursors, list) { |
David Chinner | 5b00f14 | 2008-10-30 17:39:00 +1100 | [diff] [blame] | 218 | if (cur->item == lip) |
| 219 | cur->item = (struct xfs_log_item *) |
Christoph Hellwig | db9d67d | 2015-06-22 09:43:32 +1000 | [diff] [blame] | 220 | ((uintptr_t)cur->item | 1); |
David Chinner | 5b00f14 | 2008-10-30 17:39:00 +1100 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
| 224 | /* |
Dave Chinner | 16b5902 | 2011-07-18 03:40:17 +0000 | [diff] [blame] | 225 | * Find the first item in the AIL with the given @lsn by searching in ascending |
| 226 | * LSN order and initialise the cursor to point to the next item for a |
| 227 | * ascending traversal. Pass a @lsn of zero to initialise the cursor to the |
| 228 | * first item in the AIL. Returns NULL if the list is empty. |
David Chinner | 249a8c1 | 2008-02-05 12:13:32 +1100 | [diff] [blame] | 229 | */ |
Christoph Hellwig | efe2330 | 2019-06-28 19:27:33 -0700 | [diff] [blame] | 230 | struct xfs_log_item * |
David Chinner | 5b00f14 | 2008-10-30 17:39:00 +1100 | [diff] [blame] | 231 | xfs_trans_ail_cursor_first( |
David Chinner | 27d8d5f | 2008-10-30 17:38:39 +1100 | [diff] [blame] | 232 | struct xfs_ail *ailp, |
| 233 | struct xfs_ail_cursor *cur, |
| 234 | xfs_lsn_t lsn) |
David Chinner | 249a8c1 | 2008-02-05 12:13:32 +1100 | [diff] [blame] | 235 | { |
Christoph Hellwig | efe2330 | 2019-06-28 19:27:33 -0700 | [diff] [blame] | 236 | struct xfs_log_item *lip; |
David Chinner | 249a8c1 | 2008-02-05 12:13:32 +1100 | [diff] [blame] | 237 | |
David Chinner | 5b00f14 | 2008-10-30 17:39:00 +1100 | [diff] [blame] | 238 | xfs_trans_ail_cursor_init(ailp, cur); |
Dave Chinner | 16b5902 | 2011-07-18 03:40:17 +0000 | [diff] [blame] | 239 | |
| 240 | if (lsn == 0) { |
| 241 | lip = xfs_ail_min(ailp); |
David Chinner | 5b00f14 | 2008-10-30 17:39:00 +1100 | [diff] [blame] | 242 | goto out; |
Dave Chinner | 16b5902 | 2011-07-18 03:40:17 +0000 | [diff] [blame] | 243 | } |
David Chinner | 249a8c1 | 2008-02-05 12:13:32 +1100 | [diff] [blame] | 244 | |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 245 | list_for_each_entry(lip, &ailp->ail_head, li_ail) { |
David Chinner | 5b00f14 | 2008-10-30 17:39:00 +1100 | [diff] [blame] | 246 | if (XFS_LSN_CMP(lip->li_lsn, lsn) >= 0) |
David Chinner | 7ee49ac | 2008-10-30 18:26:51 +1100 | [diff] [blame] | 247 | goto out; |
Josef 'Jeff' Sipek | 535f6b3 | 2008-03-27 17:58:27 +1100 | [diff] [blame] | 248 | } |
Dave Chinner | 16b5902 | 2011-07-18 03:40:17 +0000 | [diff] [blame] | 249 | return NULL; |
| 250 | |
David Chinner | 5b00f14 | 2008-10-30 17:39:00 +1100 | [diff] [blame] | 251 | out: |
Dave Chinner | 16b5902 | 2011-07-18 03:40:17 +0000 | [diff] [blame] | 252 | if (lip) |
| 253 | cur->item = xfs_ail_next(ailp, lip); |
David Chinner | 5b00f14 | 2008-10-30 17:39:00 +1100 | [diff] [blame] | 254 | return lip; |
David Chinner | 249a8c1 | 2008-02-05 12:13:32 +1100 | [diff] [blame] | 255 | } |
| 256 | |
Dave Chinner | 1d8c95a | 2011-07-18 03:40:16 +0000 | [diff] [blame] | 257 | static struct xfs_log_item * |
| 258 | __xfs_trans_ail_cursor_last( |
| 259 | struct xfs_ail *ailp, |
| 260 | xfs_lsn_t lsn) |
| 261 | { |
Christoph Hellwig | efe2330 | 2019-06-28 19:27:33 -0700 | [diff] [blame] | 262 | struct xfs_log_item *lip; |
Dave Chinner | 1d8c95a | 2011-07-18 03:40:16 +0000 | [diff] [blame] | 263 | |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 264 | list_for_each_entry_reverse(lip, &ailp->ail_head, li_ail) { |
Dave Chinner | 1d8c95a | 2011-07-18 03:40:16 +0000 | [diff] [blame] | 265 | if (XFS_LSN_CMP(lip->li_lsn, lsn) <= 0) |
| 266 | return lip; |
| 267 | } |
| 268 | return NULL; |
| 269 | } |
| 270 | |
| 271 | /* |
Dave Chinner | 16b5902 | 2011-07-18 03:40:17 +0000 | [diff] [blame] | 272 | * Find the last item in the AIL with the given @lsn by searching in descending |
| 273 | * LSN order and initialise the cursor to point to that item. If there is no |
| 274 | * item with the value of @lsn, then it sets the cursor to the last item with an |
| 275 | * LSN lower than @lsn. Returns NULL if the list is empty. |
Dave Chinner | 1d8c95a | 2011-07-18 03:40:16 +0000 | [diff] [blame] | 276 | */ |
| 277 | struct xfs_log_item * |
| 278 | xfs_trans_ail_cursor_last( |
| 279 | struct xfs_ail *ailp, |
| 280 | struct xfs_ail_cursor *cur, |
| 281 | xfs_lsn_t lsn) |
| 282 | { |
| 283 | xfs_trans_ail_cursor_init(ailp, cur); |
| 284 | cur->item = __xfs_trans_ail_cursor_last(ailp, lsn); |
| 285 | return cur->item; |
| 286 | } |
| 287 | |
| 288 | /* |
Dave Chinner | 16b5902 | 2011-07-18 03:40:17 +0000 | [diff] [blame] | 289 | * Splice the log item list into the AIL at the given LSN. We splice to the |
Dave Chinner | 1d8c95a | 2011-07-18 03:40:16 +0000 | [diff] [blame] | 290 | * tail of the given LSN to maintain insert order for push traversals. The |
| 291 | * cursor is optional, allowing repeated updates to the same LSN to avoid |
Alex Elder | e44f411 | 2011-07-22 16:04:41 +0000 | [diff] [blame] | 292 | * repeated traversals. This should not be called with an empty list. |
Dave Chinner | cd4a3c5 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 293 | */ |
| 294 | static void |
| 295 | xfs_ail_splice( |
Dave Chinner | 1d8c95a | 2011-07-18 03:40:16 +0000 | [diff] [blame] | 296 | struct xfs_ail *ailp, |
| 297 | struct xfs_ail_cursor *cur, |
| 298 | struct list_head *list, |
| 299 | xfs_lsn_t lsn) |
Dave Chinner | cd4a3c5 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 300 | { |
Alex Elder | e44f411 | 2011-07-22 16:04:41 +0000 | [diff] [blame] | 301 | struct xfs_log_item *lip; |
| 302 | |
| 303 | ASSERT(!list_empty(list)); |
Dave Chinner | cd4a3c5 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 304 | |
Dave Chinner | 1d8c95a | 2011-07-18 03:40:16 +0000 | [diff] [blame] | 305 | /* |
Alex Elder | e44f411 | 2011-07-22 16:04:41 +0000 | [diff] [blame] | 306 | * Use the cursor to determine the insertion point if one is |
| 307 | * provided. If not, or if the one we got is not valid, |
| 308 | * find the place in the AIL where the items belong. |
Dave Chinner | 1d8c95a | 2011-07-18 03:40:16 +0000 | [diff] [blame] | 309 | */ |
Alex Elder | e44f411 | 2011-07-22 16:04:41 +0000 | [diff] [blame] | 310 | lip = cur ? cur->item : NULL; |
Christoph Hellwig | db9d67d | 2015-06-22 09:43:32 +1000 | [diff] [blame] | 311 | if (!lip || (uintptr_t)lip & 1) |
Dave Chinner | 1d8c95a | 2011-07-18 03:40:16 +0000 | [diff] [blame] | 312 | lip = __xfs_trans_ail_cursor_last(ailp, lsn); |
| 313 | |
Alex Elder | e44f411 | 2011-07-22 16:04:41 +0000 | [diff] [blame] | 314 | /* |
| 315 | * If a cursor is provided, we know we're processing the AIL |
| 316 | * in lsn order, and future items to be spliced in will |
| 317 | * follow the last one being inserted now. Update the |
| 318 | * cursor to point to that last item, now while we have a |
| 319 | * reliable pointer to it. |
| 320 | */ |
| 321 | if (cur) |
| 322 | cur->item = list_entry(list->prev, struct xfs_log_item, li_ail); |
Dave Chinner | cd4a3c5 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 323 | |
Dave Chinner | 1d8c95a | 2011-07-18 03:40:16 +0000 | [diff] [blame] | 324 | /* |
Alex Elder | e44f411 | 2011-07-22 16:04:41 +0000 | [diff] [blame] | 325 | * Finally perform the splice. Unless the AIL was empty, |
| 326 | * lip points to the item in the AIL _after_ which the new |
| 327 | * items should go. If lip is null the AIL was empty, so |
| 328 | * the new items go at the head of the AIL. |
Dave Chinner | 1d8c95a | 2011-07-18 03:40:16 +0000 | [diff] [blame] | 329 | */ |
Alex Elder | e44f411 | 2011-07-22 16:04:41 +0000 | [diff] [blame] | 330 | if (lip) |
| 331 | list_splice(list, &lip->li_ail); |
| 332 | else |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 333 | list_splice(list, &ailp->ail_head); |
Dave Chinner | cd4a3c5 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | /* |
| 337 | * Delete the given item from the AIL. Return a pointer to the item. |
| 338 | */ |
| 339 | static void |
| 340 | xfs_ail_delete( |
Christoph Hellwig | efe2330 | 2019-06-28 19:27:33 -0700 | [diff] [blame] | 341 | struct xfs_ail *ailp, |
| 342 | struct xfs_log_item *lip) |
Dave Chinner | cd4a3c5 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 343 | { |
| 344 | xfs_ail_check(ailp, lip); |
| 345 | list_del(&lip->li_ail); |
| 346 | xfs_trans_ail_cursor_clear(ailp, lip); |
| 347 | } |
| 348 | |
Brian Foster | cb6ad09 | 2020-05-06 13:25:19 -0700 | [diff] [blame] | 349 | /* |
| 350 | * Requeue a failed buffer for writeback. |
| 351 | * |
| 352 | * We clear the log item failed state here as well, but we have to be careful |
| 353 | * about reference counts because the only active reference counts on the buffer |
| 354 | * may be the failed log items. Hence if we clear the log item failed state |
| 355 | * before queuing the buffer for IO we can release all active references to |
| 356 | * the buffer and free it, leading to use after free problems in |
| 357 | * xfs_buf_delwri_queue. It makes no difference to the buffer or log items which |
| 358 | * order we process them in - the buffer is locked, and we own the buffer list |
| 359 | * so nothing on them is going to change while we are performing this action. |
| 360 | * |
| 361 | * Hence we can safely queue the buffer for IO before we clear the failed log |
| 362 | * item state, therefore always having an active reference to the buffer and |
| 363 | * avoiding the transient zero-reference state that leads to use-after-free. |
| 364 | */ |
| 365 | static inline int |
| 366 | xfsaild_resubmit_item( |
| 367 | struct xfs_log_item *lip, |
| 368 | struct list_head *buffer_list) |
| 369 | { |
| 370 | struct xfs_buf *bp = lip->li_buf; |
| 371 | |
| 372 | if (!xfs_buf_trylock(bp)) |
| 373 | return XFS_ITEM_LOCKED; |
| 374 | |
| 375 | if (!xfs_buf_delwri_queue(bp, buffer_list)) { |
| 376 | xfs_buf_unlock(bp); |
| 377 | return XFS_ITEM_FLUSHING; |
| 378 | } |
| 379 | |
| 380 | /* protected by ail_lock */ |
Dave Chinner | 298f7be | 2020-06-29 14:49:15 -0700 | [diff] [blame] | 381 | list_for_each_entry(lip, &bp->b_li_list, li_bio_list) { |
| 382 | if (bp->b_flags & _XBF_INODES) |
| 383 | clear_bit(XFS_LI_FAILED, &lip->li_flags); |
| 384 | else |
| 385 | xfs_clear_li_failed(lip); |
| 386 | } |
Brian Foster | cb6ad09 | 2020-05-06 13:25:19 -0700 | [diff] [blame] | 387 | |
| 388 | xfs_buf_unlock(bp); |
| 389 | return XFS_ITEM_SUCCESS; |
| 390 | } |
| 391 | |
Brian Foster | 7f4d01f | 2017-08-08 18:21:52 -0700 | [diff] [blame] | 392 | static inline uint |
| 393 | xfsaild_push_item( |
| 394 | struct xfs_ail *ailp, |
| 395 | struct xfs_log_item *lip) |
| 396 | { |
| 397 | /* |
| 398 | * If log item pinning is enabled, skip the push and track the item as |
| 399 | * pinned. This can help induce head-behind-tail conditions. |
| 400 | */ |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 401 | if (XFS_TEST_ERROR(false, ailp->ail_mount, XFS_ERRTAG_LOG_ITEM_PIN)) |
Brian Foster | 7f4d01f | 2017-08-08 18:21:52 -0700 | [diff] [blame] | 402 | return XFS_ITEM_PINNED; |
| 403 | |
Christoph Hellwig | e8b78db | 2019-06-28 19:27:30 -0700 | [diff] [blame] | 404 | /* |
| 405 | * Consider the item pinned if a push callback is not defined so the |
| 406 | * caller will force the log. This should only happen for intent items |
| 407 | * as they are unpinned once the associated done item is committed to |
| 408 | * the on-disk log. |
| 409 | */ |
| 410 | if (!lip->li_ops->iop_push) |
| 411 | return XFS_ITEM_PINNED; |
Brian Foster | cb6ad09 | 2020-05-06 13:25:19 -0700 | [diff] [blame] | 412 | if (test_bit(XFS_LI_FAILED, &lip->li_flags)) |
| 413 | return xfsaild_resubmit_item(lip, &ailp->ail_buf_list); |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 414 | return lip->li_ops->iop_push(lip, &ailp->ail_buf_list); |
Brian Foster | 7f4d01f | 2017-08-08 18:21:52 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Christoph Hellwig | 0030807 | 2011-10-11 11:14:10 -0400 | [diff] [blame] | 417 | static long |
| 418 | xfsaild_push( |
| 419 | struct xfs_ail *ailp) |
David Chinner | 249a8c1 | 2008-02-05 12:13:32 +1100 | [diff] [blame] | 420 | { |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 421 | xfs_mount_t *mp = ailp->ail_mount; |
Dave Chinner | af3e402 | 2011-07-18 03:40:18 +0000 | [diff] [blame] | 422 | struct xfs_ail_cursor cur; |
Christoph Hellwig | efe2330 | 2019-06-28 19:27:33 -0700 | [diff] [blame] | 423 | struct xfs_log_item *lip; |
Dave Chinner | 9e7004e | 2011-05-06 02:54:05 +0000 | [diff] [blame] | 424 | xfs_lsn_t lsn; |
Dave Chinner | fe0da76 | 2011-05-06 02:54:07 +0000 | [diff] [blame] | 425 | xfs_lsn_t target; |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 426 | long tout; |
Dave Chinner | 9e7004e | 2011-05-06 02:54:05 +0000 | [diff] [blame] | 427 | int stuck = 0; |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 428 | int flushing = 0; |
Dave Chinner | 9e7004e | 2011-05-06 02:54:05 +0000 | [diff] [blame] | 429 | int count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | |
Dave Chinner | 670ce93 | 2011-09-30 04:45:03 +0000 | [diff] [blame] | 431 | /* |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 432 | * If we encountered pinned items or did not finish writing out all |
Dave Chinner | 0020a19 | 2021-08-10 18:00:44 -0700 | [diff] [blame] | 433 | * buffers the last time we ran, force a background CIL push to get the |
| 434 | * items unpinned in the near future. We do not wait on the CIL push as |
| 435 | * that could stall us for seconds if there is enough background IO |
| 436 | * load. Stalling for that long when the tail of the log is pinned and |
| 437 | * needs flushing will hard stop the transaction subsystem when log |
| 438 | * space runs out. |
Dave Chinner | 670ce93 | 2011-09-30 04:45:03 +0000 | [diff] [blame] | 439 | */ |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 440 | if (ailp->ail_log_flush && ailp->ail_last_pushed_lsn == 0 && |
| 441 | (!list_empty_careful(&ailp->ail_buf_list) || |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 442 | xfs_ail_min_lsn(ailp))) { |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 443 | ailp->ail_log_flush = 0; |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 444 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 445 | XFS_STATS_INC(mp, xs_push_ail_flush); |
Dave Chinner | 0020a19 | 2021-08-10 18:00:44 -0700 | [diff] [blame] | 446 | xlog_cil_flush(mp->m_log); |
Dave Chinner | 670ce93 | 2011-09-30 04:45:03 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 449 | spin_lock(&ailp->ail_lock); |
Brian Foster | 8375f92 | 2012-06-28 06:52:56 -0400 | [diff] [blame] | 450 | |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 451 | /* barrier matches the ail_target update in xfs_ail_push() */ |
Brian Foster | 8375f92 | 2012-06-28 06:52:56 -0400 | [diff] [blame] | 452 | smp_rmb(); |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 453 | target = ailp->ail_target; |
| 454 | ailp->ail_target_prev = target; |
Brian Foster | 8375f92 | 2012-06-28 06:52:56 -0400 | [diff] [blame] | 455 | |
Brian Foster | f376b45 | 2020-07-16 07:39:29 -0700 | [diff] [blame] | 456 | /* we're done if the AIL is empty or our push has reached the end */ |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 457 | lip = xfs_trans_ail_cursor_first(ailp, &cur, ailp->ail_last_pushed_lsn); |
Brian Foster | f376b45 | 2020-07-16 07:39:29 -0700 | [diff] [blame] | 458 | if (!lip) |
Dave Chinner | 9e7004e | 2011-05-06 02:54:05 +0000 | [diff] [blame] | 459 | goto out_done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 461 | XFS_STATS_INC(mp, xs_push_ail); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | |
David Chinner | 249a8c1 | 2008-02-05 12:13:32 +1100 | [diff] [blame] | 463 | lsn = lip->li_lsn; |
Dave Chinner | 50e8668 | 2011-05-06 02:54:06 +0000 | [diff] [blame] | 464 | while ((XFS_LSN_CMP(lip->li_lsn, target) <= 0)) { |
David Chinner | 249a8c1 | 2008-02-05 12:13:32 +1100 | [diff] [blame] | 465 | int lock_result; |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 466 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | /* |
Dave Chinner | 904c17e | 2013-08-28 21:12:03 +1000 | [diff] [blame] | 468 | * Note that iop_push may unlock and reacquire the AIL lock. We |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 469 | * rely on the AIL cursor implementation to be able to deal with |
| 470 | * the dropped lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | */ |
Brian Foster | 7f4d01f | 2017-08-08 18:21:52 -0700 | [diff] [blame] | 472 | lock_result = xfsaild_push_item(ailp, lip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | switch (lock_result) { |
David Chinner | 249a8c1 | 2008-02-05 12:13:32 +1100 | [diff] [blame] | 474 | case XFS_ITEM_SUCCESS: |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 475 | XFS_STATS_INC(mp, xs_push_ail_success); |
Christoph Hellwig | 9e4c109 | 2011-10-11 15:14:11 +0000 | [diff] [blame] | 476 | trace_xfs_ail_push(lip); |
| 477 | |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 478 | ailp->ail_last_pushed_lsn = lsn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | break; |
| 480 | |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 481 | case XFS_ITEM_FLUSHING: |
| 482 | /* |
Joe Perches | cf085a1 | 2019-11-07 13:24:52 -0800 | [diff] [blame] | 483 | * The item or its backing buffer is already being |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 484 | * flushed. The typical reason for that is that an |
| 485 | * inode buffer is locked because we already pushed the |
| 486 | * updates to it as part of inode clustering. |
| 487 | * |
Randy Dunlap | b63da6c | 2020-08-05 08:49:58 -0700 | [diff] [blame] | 488 | * We do not want to stop flushing just because lots |
Joe Perches | cf085a1 | 2019-11-07 13:24:52 -0800 | [diff] [blame] | 489 | * of items are already being flushed, but we need to |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 490 | * re-try the flushing relatively soon if most of the |
Joe Perches | cf085a1 | 2019-11-07 13:24:52 -0800 | [diff] [blame] | 491 | * AIL is being flushed. |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 492 | */ |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 493 | XFS_STATS_INC(mp, xs_push_ail_flushing); |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 494 | trace_xfs_ail_flushing(lip); |
Christoph Hellwig | 17b3847 | 2011-10-11 15:14:09 +0000 | [diff] [blame] | 495 | |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 496 | flushing++; |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 497 | ailp->ail_last_pushed_lsn = lsn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | break; |
| 499 | |
David Chinner | 249a8c1 | 2008-02-05 12:13:32 +1100 | [diff] [blame] | 500 | case XFS_ITEM_PINNED: |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 501 | XFS_STATS_INC(mp, xs_push_ail_pinned); |
Christoph Hellwig | 9e4c109 | 2011-10-11 15:14:11 +0000 | [diff] [blame] | 502 | trace_xfs_ail_pinned(lip); |
| 503 | |
David Chinner | 249a8c1 | 2008-02-05 12:13:32 +1100 | [diff] [blame] | 504 | stuck++; |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 505 | ailp->ail_log_flush++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | break; |
David Chinner | 249a8c1 | 2008-02-05 12:13:32 +1100 | [diff] [blame] | 507 | case XFS_ITEM_LOCKED: |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 508 | XFS_STATS_INC(mp, xs_push_ail_locked); |
Christoph Hellwig | 9e4c109 | 2011-10-11 15:14:11 +0000 | [diff] [blame] | 509 | trace_xfs_ail_locked(lip); |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 510 | |
David Chinner | 249a8c1 | 2008-02-05 12:13:32 +1100 | [diff] [blame] | 511 | stuck++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | break; |
David Chinner | 249a8c1 | 2008-02-05 12:13:32 +1100 | [diff] [blame] | 513 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | ASSERT(0); |
| 515 | break; |
| 516 | } |
| 517 | |
David Chinner | 249a8c1 | 2008-02-05 12:13:32 +1100 | [diff] [blame] | 518 | count++; |
| 519 | |
| 520 | /* |
| 521 | * Are there too many items we can't do anything with? |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 522 | * |
Randy Dunlap | b63da6c | 2020-08-05 08:49:58 -0700 | [diff] [blame] | 523 | * If we are skipping too many items because we can't flush |
David Chinner | 249a8c1 | 2008-02-05 12:13:32 +1100 | [diff] [blame] | 524 | * them or they are already being flushed, we back off and |
| 525 | * given them time to complete whatever operation is being |
| 526 | * done. i.e. remove pressure from the AIL while we can't make |
| 527 | * progress so traversals don't slow down further inserts and |
| 528 | * removals to/from the AIL. |
| 529 | * |
| 530 | * The value of 100 is an arbitrary magic number based on |
| 531 | * observation. |
| 532 | */ |
| 533 | if (stuck > 100) |
| 534 | break; |
| 535 | |
Dave Chinner | af3e402 | 2011-07-18 03:40:18 +0000 | [diff] [blame] | 536 | lip = xfs_trans_ail_cursor_next(ailp, &cur); |
David Chinner | 249a8c1 | 2008-02-05 12:13:32 +1100 | [diff] [blame] | 537 | if (lip == NULL) |
| 538 | break; |
David Chinner | 249a8c1 | 2008-02-05 12:13:32 +1100 | [diff] [blame] | 539 | lsn = lip->li_lsn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | } |
Brian Foster | f376b45 | 2020-07-16 07:39:29 -0700 | [diff] [blame] | 541 | |
| 542 | out_done: |
Eric Sandeen | e4a1e29 | 2014-04-14 19:06:05 +1000 | [diff] [blame] | 543 | xfs_trans_ail_cursor_done(&cur); |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 544 | spin_unlock(&ailp->ail_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 546 | if (xfs_buf_delwri_submit_nowait(&ailp->ail_buf_list)) |
| 547 | ailp->ail_log_flush++; |
Dave Chinner | d808f61 | 2010-02-02 10:13:42 +1100 | [diff] [blame] | 548 | |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 549 | if (!count || XFS_LSN_CMP(lsn, target) >= 0) { |
David Chinner | 92d9cd1 | 2008-03-06 13:45:10 +1100 | [diff] [blame] | 550 | /* |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 551 | * We reached the target or the AIL is empty, so wait a bit |
| 552 | * longer for I/O to complete and remove pushed items from the |
| 553 | * AIL before we start the next scan from the start of the AIL. |
David Chinner | 92d9cd1 | 2008-03-06 13:45:10 +1100 | [diff] [blame] | 554 | */ |
Dave Chinner | 453eac8 | 2010-01-11 11:49:58 +0000 | [diff] [blame] | 555 | tout = 50; |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 556 | ailp->ail_last_pushed_lsn = 0; |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 557 | } else if (((stuck + flushing) * 100) / count > 90) { |
David Chinner | 249a8c1 | 2008-02-05 12:13:32 +1100 | [diff] [blame] | 558 | /* |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 559 | * Either there is a lot of contention on the AIL or we are |
| 560 | * stuck due to operations in progress. "Stuck" in this case |
| 561 | * is defined as >90% of the items we tried to push were stuck. |
David Chinner | 249a8c1 | 2008-02-05 12:13:32 +1100 | [diff] [blame] | 562 | * |
| 563 | * Backoff a bit more to allow some I/O to complete before |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 564 | * restarting from the start of the AIL. This prevents us from |
| 565 | * spinning on the same items, and if they are pinned will all |
| 566 | * the restart to issue a log force to unpin the stuck items. |
David Chinner | 249a8c1 | 2008-02-05 12:13:32 +1100 | [diff] [blame] | 567 | */ |
Dave Chinner | 453eac8 | 2010-01-11 11:49:58 +0000 | [diff] [blame] | 568 | tout = 20; |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 569 | ailp->ail_last_pushed_lsn = 0; |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 570 | } else { |
| 571 | /* |
| 572 | * Assume we have more work to do in a short while. |
| 573 | */ |
| 574 | tout = 10; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | } |
Dave Chinner | 0bf6a5b | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 576 | |
Christoph Hellwig | 0030807 | 2011-10-11 11:14:10 -0400 | [diff] [blame] | 577 | return tout; |
| 578 | } |
| 579 | |
| 580 | static int |
| 581 | xfsaild( |
| 582 | void *data) |
| 583 | { |
| 584 | struct xfs_ail *ailp = data; |
| 585 | long tout = 0; /* milliseconds */ |
Eric Biggers | 10a98cb | 2020-03-10 08:57:27 -0700 | [diff] [blame] | 586 | unsigned int noreclaim_flag; |
Christoph Hellwig | 0030807 | 2011-10-11 11:14:10 -0400 | [diff] [blame] | 587 | |
Eric Biggers | 10a98cb | 2020-03-10 08:57:27 -0700 | [diff] [blame] | 588 | noreclaim_flag = memalloc_noreclaim_save(); |
Michal Hocko | 18f1df4 | 2016-02-08 14:59:07 +1100 | [diff] [blame] | 589 | set_freezable(); |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 590 | |
Hou Tao | 0bd8967 | 2017-10-17 14:16:28 -0700 | [diff] [blame] | 591 | while (1) { |
Christoph Hellwig | 0030807 | 2011-10-11 11:14:10 -0400 | [diff] [blame] | 592 | if (tout && tout <= 20) |
Hou Tao | 0bd8967 | 2017-10-17 14:16:28 -0700 | [diff] [blame] | 593 | set_current_state(TASK_KILLABLE); |
Christoph Hellwig | 0030807 | 2011-10-11 11:14:10 -0400 | [diff] [blame] | 594 | else |
Hou Tao | 0bd8967 | 2017-10-17 14:16:28 -0700 | [diff] [blame] | 595 | set_current_state(TASK_INTERRUPTIBLE); |
| 596 | |
| 597 | /* |
Brian Foster | efc3289 | 2018-10-18 17:21:49 +1100 | [diff] [blame] | 598 | * Check kthread_should_stop() after we set the task state to |
| 599 | * guarantee that we either see the stop bit and exit or the |
| 600 | * task state is reset to runnable such that it's not scheduled |
| 601 | * out indefinitely and detects the stop bit at next iteration. |
Hou Tao | 0bd8967 | 2017-10-17 14:16:28 -0700 | [diff] [blame] | 602 | * A memory barrier is included in above task state set to |
| 603 | * serialize again kthread_stop(). |
| 604 | */ |
| 605 | if (kthread_should_stop()) { |
| 606 | __set_current_state(TASK_RUNNING); |
Brian Foster | efc3289 | 2018-10-18 17:21:49 +1100 | [diff] [blame] | 607 | |
| 608 | /* |
| 609 | * The caller forces out the AIL before stopping the |
| 610 | * thread in the common case, which means the delwri |
| 611 | * queue is drained. In the shutdown case, the queue may |
| 612 | * still hold relogged buffers that haven't been |
| 613 | * submitted because they were pinned since added to the |
| 614 | * queue. |
| 615 | * |
| 616 | * Log I/O error processing stales the underlying buffer |
| 617 | * and clears the delwri state, expecting the buf to be |
| 618 | * removed on the next submission attempt. That won't |
| 619 | * happen if we're shutting down, so this is the last |
| 620 | * opportunity to release such buffers from the queue. |
| 621 | */ |
| 622 | ASSERT(list_empty(&ailp->ail_buf_list) || |
Dave Chinner | 75c8c50f | 2021-08-18 18:46:53 -0700 | [diff] [blame^] | 623 | xfs_is_shutdown(ailp->ail_mount)); |
Brian Foster | efc3289 | 2018-10-18 17:21:49 +1100 | [diff] [blame] | 624 | xfs_buf_delwri_cancel(&ailp->ail_buf_list); |
Hou Tao | 0bd8967 | 2017-10-17 14:16:28 -0700 | [diff] [blame] | 625 | break; |
| 626 | } |
Brian Foster | 8375f92 | 2012-06-28 06:52:56 -0400 | [diff] [blame] | 627 | |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 628 | spin_lock(&ailp->ail_lock); |
Brian Foster | 8375f92 | 2012-06-28 06:52:56 -0400 | [diff] [blame] | 629 | |
| 630 | /* |
| 631 | * Idle if the AIL is empty and we are not racing with a target |
| 632 | * update. We check the AIL after we set the task to a sleep |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 633 | * state to guarantee that we either catch an ail_target update |
Brian Foster | 8375f92 | 2012-06-28 06:52:56 -0400 | [diff] [blame] | 634 | * or that a wake_up resets the state to TASK_RUNNING. |
| 635 | * Otherwise, we run the risk of sleeping indefinitely. |
| 636 | * |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 637 | * The barrier matches the ail_target update in xfs_ail_push(). |
Brian Foster | 8375f92 | 2012-06-28 06:52:56 -0400 | [diff] [blame] | 638 | */ |
| 639 | smp_rmb(); |
| 640 | if (!xfs_ail_min(ailp) && |
Brian Foster | f376b45 | 2020-07-16 07:39:29 -0700 | [diff] [blame] | 641 | ailp->ail_target == ailp->ail_target_prev && |
| 642 | list_empty(&ailp->ail_buf_list)) { |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 643 | spin_unlock(&ailp->ail_lock); |
Michal Hocko | 18f1df4 | 2016-02-08 14:59:07 +1100 | [diff] [blame] | 644 | freezable_schedule(); |
Brian Foster | 8375f92 | 2012-06-28 06:52:56 -0400 | [diff] [blame] | 645 | tout = 0; |
| 646 | continue; |
| 647 | } |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 648 | spin_unlock(&ailp->ail_lock); |
Brian Foster | 8375f92 | 2012-06-28 06:52:56 -0400 | [diff] [blame] | 649 | |
| 650 | if (tout) |
Michal Hocko | 18f1df4 | 2016-02-08 14:59:07 +1100 | [diff] [blame] | 651 | freezable_schedule_timeout(msecs_to_jiffies(tout)); |
Brian Foster | 8375f92 | 2012-06-28 06:52:56 -0400 | [diff] [blame] | 652 | |
| 653 | __set_current_state(TASK_RUNNING); |
Christoph Hellwig | 0030807 | 2011-10-11 11:14:10 -0400 | [diff] [blame] | 654 | |
| 655 | try_to_freeze(); |
| 656 | |
| 657 | tout = xfsaild_push(ailp); |
| 658 | } |
| 659 | |
Eric Biggers | 10a98cb | 2020-03-10 08:57:27 -0700 | [diff] [blame] | 660 | memalloc_noreclaim_restore(noreclaim_flag); |
Christoph Hellwig | 0030807 | 2011-10-11 11:14:10 -0400 | [diff] [blame] | 661 | return 0; |
Dave Chinner | 453eac8 | 2010-01-11 11:49:58 +0000 | [diff] [blame] | 662 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | |
Dave Chinner | 0bf6a5b | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 664 | /* |
| 665 | * This routine is called to move the tail of the AIL forward. It does this by |
| 666 | * trying to flush items in the AIL whose lsns are below the given |
| 667 | * threshold_lsn. |
| 668 | * |
| 669 | * The push is run asynchronously in a workqueue, which means the caller needs |
| 670 | * to handle waiting on the async flush for space to become available. |
| 671 | * We don't want to interrupt any push that is in progress, hence we only queue |
Joe Perches | cf085a1 | 2019-11-07 13:24:52 -0800 | [diff] [blame] | 672 | * work if we set the pushing bit appropriately. |
Dave Chinner | 0bf6a5b | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 673 | * |
| 674 | * We do this unlocked - we only need to know whether there is anything in the |
| 675 | * AIL at the time we are called. We don't need to access the contents of |
| 676 | * any of the objects, so the lock is not needed. |
| 677 | */ |
| 678 | void |
Dave Chinner | fd07484 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 679 | xfs_ail_push( |
Christoph Hellwig | efe2330 | 2019-06-28 19:27:33 -0700 | [diff] [blame] | 680 | struct xfs_ail *ailp, |
| 681 | xfs_lsn_t threshold_lsn) |
Dave Chinner | 0bf6a5b | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 682 | { |
Christoph Hellwig | efe2330 | 2019-06-28 19:27:33 -0700 | [diff] [blame] | 683 | struct xfs_log_item *lip; |
Dave Chinner | 0bf6a5b | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 684 | |
| 685 | lip = xfs_ail_min(ailp); |
Dave Chinner | 75c8c50f | 2021-08-18 18:46:53 -0700 | [diff] [blame^] | 686 | if (!lip || xfs_is_shutdown(ailp->ail_mount) || |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 687 | XFS_LSN_CMP(threshold_lsn, ailp->ail_target) <= 0) |
Dave Chinner | 0bf6a5b | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 688 | return; |
| 689 | |
| 690 | /* |
| 691 | * Ensure that the new target is noticed in push code before it clears |
| 692 | * the XFS_AIL_PUSHING_BIT. |
| 693 | */ |
| 694 | smp_wmb(); |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 695 | xfs_trans_ail_copy_lsn(ailp, &ailp->ail_target, &threshold_lsn); |
Christoph Hellwig | 0030807 | 2011-10-11 11:14:10 -0400 | [diff] [blame] | 696 | smp_wmb(); |
| 697 | |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 698 | wake_up_process(ailp->ail_task); |
Dave Chinner | 0bf6a5b | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 699 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | |
| 701 | /* |
Dave Chinner | fd07484 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 702 | * Push out all items in the AIL immediately |
| 703 | */ |
| 704 | void |
| 705 | xfs_ail_push_all( |
| 706 | struct xfs_ail *ailp) |
| 707 | { |
| 708 | xfs_lsn_t threshold_lsn = xfs_ail_max_lsn(ailp); |
| 709 | |
| 710 | if (threshold_lsn) |
| 711 | xfs_ail_push(ailp, threshold_lsn); |
| 712 | } |
| 713 | |
| 714 | /* |
Christoph Hellwig | 211e4d4 | 2012-04-23 15:58:34 +1000 | [diff] [blame] | 715 | * Push out all items in the AIL immediately and wait until the AIL is empty. |
| 716 | */ |
| 717 | void |
| 718 | xfs_ail_push_all_sync( |
| 719 | struct xfs_ail *ailp) |
| 720 | { |
| 721 | struct xfs_log_item *lip; |
| 722 | DEFINE_WAIT(wait); |
| 723 | |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 724 | spin_lock(&ailp->ail_lock); |
Christoph Hellwig | 211e4d4 | 2012-04-23 15:58:34 +1000 | [diff] [blame] | 725 | while ((lip = xfs_ail_max(ailp)) != NULL) { |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 726 | prepare_to_wait(&ailp->ail_empty, &wait, TASK_UNINTERRUPTIBLE); |
| 727 | ailp->ail_target = lip->li_lsn; |
| 728 | wake_up_process(ailp->ail_task); |
| 729 | spin_unlock(&ailp->ail_lock); |
Christoph Hellwig | 211e4d4 | 2012-04-23 15:58:34 +1000 | [diff] [blame] | 730 | schedule(); |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 731 | spin_lock(&ailp->ail_lock); |
Christoph Hellwig | 211e4d4 | 2012-04-23 15:58:34 +1000 | [diff] [blame] | 732 | } |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 733 | spin_unlock(&ailp->ail_lock); |
Christoph Hellwig | 211e4d4 | 2012-04-23 15:58:34 +1000 | [diff] [blame] | 734 | |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 735 | finish_wait(&ailp->ail_empty, &wait); |
Christoph Hellwig | 211e4d4 | 2012-04-23 15:58:34 +1000 | [diff] [blame] | 736 | } |
| 737 | |
Dave Chinner | 4165994 | 2020-03-24 20:10:29 -0700 | [diff] [blame] | 738 | void |
| 739 | xfs_ail_update_finish( |
| 740 | struct xfs_ail *ailp, |
Dave Chinner | 8eb807b | 2020-03-24 20:10:29 -0700 | [diff] [blame] | 741 | xfs_lsn_t old_lsn) __releases(ailp->ail_lock) |
Dave Chinner | 4165994 | 2020-03-24 20:10:29 -0700 | [diff] [blame] | 742 | { |
| 743 | struct xfs_mount *mp = ailp->ail_mount; |
| 744 | |
Dave Chinner | 8eb807b | 2020-03-24 20:10:29 -0700 | [diff] [blame] | 745 | /* if the tail lsn hasn't changed, don't do updates or wakeups. */ |
| 746 | if (!old_lsn || old_lsn == __xfs_ail_min_lsn(ailp)) { |
Dave Chinner | 4165994 | 2020-03-24 20:10:29 -0700 | [diff] [blame] | 747 | spin_unlock(&ailp->ail_lock); |
| 748 | return; |
| 749 | } |
| 750 | |
Dave Chinner | 75c8c50f | 2021-08-18 18:46:53 -0700 | [diff] [blame^] | 751 | if (!xfs_is_shutdown(mp)) |
Dave Chinner | 4165994 | 2020-03-24 20:10:29 -0700 | [diff] [blame] | 752 | xlog_assign_tail_lsn_locked(mp); |
| 753 | |
| 754 | if (list_empty(&ailp->ail_head)) |
| 755 | wake_up_all(&ailp->ail_empty); |
| 756 | spin_unlock(&ailp->ail_lock); |
| 757 | xfs_log_space_wake(mp); |
| 758 | } |
| 759 | |
Christoph Hellwig | 211e4d4 | 2012-04-23 15:58:34 +1000 | [diff] [blame] | 760 | /* |
Dave Chinner | 0e57f6a | 2010-12-20 12:02:19 +1100 | [diff] [blame] | 761 | * xfs_trans_ail_update - bulk AIL insertion operation. |
| 762 | * |
| 763 | * @xfs_trans_ail_update takes an array of log items that all need to be |
| 764 | * positioned at the same LSN in the AIL. If an item is not in the AIL, it will |
| 765 | * be added. Otherwise, it will be repositioned by removing it and re-adding |
| 766 | * it to the AIL. If we move the first item in the AIL, update the log tail to |
| 767 | * match the new minimum LSN in the AIL. |
| 768 | * |
| 769 | * This function takes the AIL lock once to execute the update operations on |
| 770 | * all the items in the array, and as such should not be called with the AIL |
| 771 | * lock held. As a result, once we have the AIL lock, we need to check each log |
| 772 | * item LSN to confirm it needs to be moved forward in the AIL. |
| 773 | * |
| 774 | * To optimise the insert operation, we delete all the items from the AIL in |
| 775 | * the first pass, moving them into a temporary list, then splice the temporary |
| 776 | * list into the correct position in the AIL. This avoids needing to do an |
| 777 | * insert operation on every item. |
| 778 | * |
| 779 | * This function must be called with the AIL lock held. The lock is dropped |
| 780 | * before returning. |
| 781 | */ |
| 782 | void |
| 783 | xfs_trans_ail_update_bulk( |
| 784 | struct xfs_ail *ailp, |
Dave Chinner | 1d8c95a | 2011-07-18 03:40:16 +0000 | [diff] [blame] | 785 | struct xfs_ail_cursor *cur, |
Dave Chinner | 0e57f6a | 2010-12-20 12:02:19 +1100 | [diff] [blame] | 786 | struct xfs_log_item **log_items, |
| 787 | int nr_items, |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 788 | xfs_lsn_t lsn) __releases(ailp->ail_lock) |
Dave Chinner | 0e57f6a | 2010-12-20 12:02:19 +1100 | [diff] [blame] | 789 | { |
Christoph Hellwig | efe2330 | 2019-06-28 19:27:33 -0700 | [diff] [blame] | 790 | struct xfs_log_item *mlip; |
Dave Chinner | 8eb807b | 2020-03-24 20:10:29 -0700 | [diff] [blame] | 791 | xfs_lsn_t tail_lsn = 0; |
Dave Chinner | 0e57f6a | 2010-12-20 12:02:19 +1100 | [diff] [blame] | 792 | int i; |
| 793 | LIST_HEAD(tmp); |
| 794 | |
Alex Elder | e44f411 | 2011-07-22 16:04:41 +0000 | [diff] [blame] | 795 | ASSERT(nr_items > 0); /* Not required, but true. */ |
Dave Chinner | 0e57f6a | 2010-12-20 12:02:19 +1100 | [diff] [blame] | 796 | mlip = xfs_ail_min(ailp); |
| 797 | |
| 798 | for (i = 0; i < nr_items; i++) { |
| 799 | struct xfs_log_item *lip = log_items[i]; |
Dave Chinner | 22525c1 | 2018-05-09 07:47:34 -0700 | [diff] [blame] | 800 | if (test_and_set_bit(XFS_LI_IN_AIL, &lip->li_flags)) { |
Dave Chinner | 0e57f6a | 2010-12-20 12:02:19 +1100 | [diff] [blame] | 801 | /* check if we really need to move the item */ |
| 802 | if (XFS_LSN_CMP(lsn, lip->li_lsn) <= 0) |
| 803 | continue; |
| 804 | |
Dave Chinner | 750b9c9 | 2013-11-01 15:27:18 +1100 | [diff] [blame] | 805 | trace_xfs_ail_move(lip, lip->li_lsn, lsn); |
Dave Chinner | 8eb807b | 2020-03-24 20:10:29 -0700 | [diff] [blame] | 806 | if (mlip == lip && !tail_lsn) |
| 807 | tail_lsn = lip->li_lsn; |
| 808 | |
Dave Chinner | 0e57f6a | 2010-12-20 12:02:19 +1100 | [diff] [blame] | 809 | xfs_ail_delete(ailp, lip); |
Dave Chinner | 0e57f6a | 2010-12-20 12:02:19 +1100 | [diff] [blame] | 810 | } else { |
Dave Chinner | 750b9c9 | 2013-11-01 15:27:18 +1100 | [diff] [blame] | 811 | trace_xfs_ail_insert(lip, 0, lsn); |
Dave Chinner | 0e57f6a | 2010-12-20 12:02:19 +1100 | [diff] [blame] | 812 | } |
| 813 | lip->li_lsn = lsn; |
| 814 | list_add(&lip->li_ail, &tmp); |
| 815 | } |
| 816 | |
Alex Elder | e44f411 | 2011-07-22 16:04:41 +0000 | [diff] [blame] | 817 | if (!list_empty(&tmp)) |
| 818 | xfs_ail_splice(ailp, cur, &tmp, lsn); |
Christoph Hellwig | 09a423a | 2012-02-20 02:31:20 +0000 | [diff] [blame] | 819 | |
Dave Chinner | 8eb807b | 2020-03-24 20:10:29 -0700 | [diff] [blame] | 820 | xfs_ail_update_finish(ailp, tail_lsn); |
Dave Chinner | 0e57f6a | 2010-12-20 12:02:19 +1100 | [diff] [blame] | 821 | } |
| 822 | |
Darrick J. Wong | 86a3717 | 2020-05-01 16:00:54 -0700 | [diff] [blame] | 823 | /* Insert a log item into the AIL. */ |
| 824 | void |
| 825 | xfs_trans_ail_insert( |
| 826 | struct xfs_ail *ailp, |
| 827 | struct xfs_log_item *lip, |
| 828 | xfs_lsn_t lsn) |
| 829 | { |
| 830 | spin_lock(&ailp->ail_lock); |
| 831 | xfs_trans_ail_update_bulk(ailp, NULL, &lip, 1, lsn); |
| 832 | } |
| 833 | |
Dave Chinner | 8eb807b | 2020-03-24 20:10:29 -0700 | [diff] [blame] | 834 | /* |
| 835 | * Delete one log item from the AIL. |
| 836 | * |
| 837 | * If this item was at the tail of the AIL, return the LSN of the log item so |
| 838 | * that we can use it to check if the LSN of the tail of the log has moved |
| 839 | * when finishing up the AIL delete process in xfs_ail_update_finish(). |
| 840 | */ |
| 841 | xfs_lsn_t |
Christoph Hellwig | 27af1bb | 2017-04-21 11:24:42 -0700 | [diff] [blame] | 842 | xfs_ail_delete_one( |
| 843 | struct xfs_ail *ailp, |
Carlos Maiolino | d3a304b | 2017-08-08 18:21:50 -0700 | [diff] [blame] | 844 | struct xfs_log_item *lip) |
Christoph Hellwig | 27af1bb | 2017-04-21 11:24:42 -0700 | [diff] [blame] | 845 | { |
| 846 | struct xfs_log_item *mlip = xfs_ail_min(ailp); |
Dave Chinner | 8eb807b | 2020-03-24 20:10:29 -0700 | [diff] [blame] | 847 | xfs_lsn_t lsn = lip->li_lsn; |
Christoph Hellwig | 27af1bb | 2017-04-21 11:24:42 -0700 | [diff] [blame] | 848 | |
| 849 | trace_xfs_ail_delete(lip, mlip->li_lsn, lip->li_lsn); |
| 850 | xfs_ail_delete(ailp, lip); |
Dave Chinner | 22525c1 | 2018-05-09 07:47:34 -0700 | [diff] [blame] | 851 | clear_bit(XFS_LI_IN_AIL, &lip->li_flags); |
Christoph Hellwig | 27af1bb | 2017-04-21 11:24:42 -0700 | [diff] [blame] | 852 | lip->li_lsn = 0; |
| 853 | |
Dave Chinner | 8eb807b | 2020-03-24 20:10:29 -0700 | [diff] [blame] | 854 | if (mlip == lip) |
| 855 | return lsn; |
| 856 | return 0; |
Christoph Hellwig | 27af1bb | 2017-04-21 11:24:42 -0700 | [diff] [blame] | 857 | } |
| 858 | |
Dave Chinner | 3013683 | 2010-12-20 12:03:17 +1100 | [diff] [blame] | 859 | void |
Christoph Hellwig | 27af1bb | 2017-04-21 11:24:42 -0700 | [diff] [blame] | 860 | xfs_trans_ail_delete( |
Christoph Hellwig | 27af1bb | 2017-04-21 11:24:42 -0700 | [diff] [blame] | 861 | struct xfs_log_item *lip, |
Dave Chinner | 4165994 | 2020-03-24 20:10:29 -0700 | [diff] [blame] | 862 | int shutdown_type) |
Dave Chinner | 3013683 | 2010-12-20 12:03:17 +1100 | [diff] [blame] | 863 | { |
Brian Foster | 849274c | 2020-05-06 13:25:23 -0700 | [diff] [blame] | 864 | struct xfs_ail *ailp = lip->li_ailp; |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 865 | struct xfs_mount *mp = ailp->ail_mount; |
Dave Chinner | 8eb807b | 2020-03-24 20:10:29 -0700 | [diff] [blame] | 866 | xfs_lsn_t tail_lsn; |
Dave Chinner | 3013683 | 2010-12-20 12:03:17 +1100 | [diff] [blame] | 867 | |
Brian Foster | 849274c | 2020-05-06 13:25:23 -0700 | [diff] [blame] | 868 | spin_lock(&ailp->ail_lock); |
Dave Chinner | 22525c1 | 2018-05-09 07:47:34 -0700 | [diff] [blame] | 869 | if (!test_bit(XFS_LI_IN_AIL, &lip->li_flags)) { |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 870 | spin_unlock(&ailp->ail_lock); |
Dave Chinner | 75c8c50f | 2021-08-18 18:46:53 -0700 | [diff] [blame^] | 871 | if (shutdown_type && !xfs_is_shutdown(mp)) { |
Christoph Hellwig | 27af1bb | 2017-04-21 11:24:42 -0700 | [diff] [blame] | 872 | xfs_alert_tag(mp, XFS_PTAG_AILDELETE, |
| 873 | "%s: attempting to delete a log item that is not in the AIL", |
| 874 | __func__); |
| 875 | xfs_force_shutdown(mp, shutdown_type); |
Dave Chinner | 3013683 | 2010-12-20 12:03:17 +1100 | [diff] [blame] | 876 | } |
Christoph Hellwig | 27af1bb | 2017-04-21 11:24:42 -0700 | [diff] [blame] | 877 | return; |
Dave Chinner | 3013683 | 2010-12-20 12:03:17 +1100 | [diff] [blame] | 878 | } |
Christoph Hellwig | 09a423a | 2012-02-20 02:31:20 +0000 | [diff] [blame] | 879 | |
Brian Foster | 2b3cf09 | 2020-05-06 13:27:04 -0700 | [diff] [blame] | 880 | /* xfs_ail_update_finish() drops the AIL lock */ |
Dave Chinner | e98084b | 2020-06-29 14:49:15 -0700 | [diff] [blame] | 881 | xfs_clear_li_failed(lip); |
Dave Chinner | 8eb807b | 2020-03-24 20:10:29 -0700 | [diff] [blame] | 882 | tail_lsn = xfs_ail_delete_one(ailp, lip); |
| 883 | xfs_ail_update_finish(ailp, tail_lsn); |
Dave Chinner | 3013683 | 2010-12-20 12:03:17 +1100 | [diff] [blame] | 884 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | |
David Chinner | 249a8c1 | 2008-02-05 12:13:32 +1100 | [diff] [blame] | 886 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | xfs_trans_ail_init( |
| 888 | xfs_mount_t *mp) |
| 889 | { |
David Chinner | 82fa901 | 2008-10-30 17:38:26 +1100 | [diff] [blame] | 890 | struct xfs_ail *ailp; |
| 891 | |
| 892 | ailp = kmem_zalloc(sizeof(struct xfs_ail), KM_MAYFAIL); |
| 893 | if (!ailp) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 894 | return -ENOMEM; |
David Chinner | 82fa901 | 2008-10-30 17:38:26 +1100 | [diff] [blame] | 895 | |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 896 | ailp->ail_mount = mp; |
| 897 | INIT_LIST_HEAD(&ailp->ail_head); |
| 898 | INIT_LIST_HEAD(&ailp->ail_cursors); |
| 899 | spin_lock_init(&ailp->ail_lock); |
| 900 | INIT_LIST_HEAD(&ailp->ail_buf_list); |
| 901 | init_waitqueue_head(&ailp->ail_empty); |
Christoph Hellwig | 0030807 | 2011-10-11 11:14:10 -0400 | [diff] [blame] | 902 | |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 903 | ailp->ail_task = kthread_run(xfsaild, ailp, "xfsaild/%s", |
Ian Kent | e1d3d21 | 2019-11-04 13:58:40 -0800 | [diff] [blame] | 904 | ailp->ail_mount->m_super->s_id); |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 905 | if (IS_ERR(ailp->ail_task)) |
Christoph Hellwig | 0030807 | 2011-10-11 11:14:10 -0400 | [diff] [blame] | 906 | goto out_free_ailp; |
| 907 | |
David Chinner | 27d8d5f | 2008-10-30 17:38:39 +1100 | [diff] [blame] | 908 | mp->m_ail = ailp; |
| 909 | return 0; |
Christoph Hellwig | 0030807 | 2011-10-11 11:14:10 -0400 | [diff] [blame] | 910 | |
| 911 | out_free_ailp: |
| 912 | kmem_free(ailp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 913 | return -ENOMEM; |
David Chinner | 249a8c1 | 2008-02-05 12:13:32 +1100 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | void |
| 917 | xfs_trans_ail_destroy( |
| 918 | xfs_mount_t *mp) |
| 919 | { |
David Chinner | 82fa901 | 2008-10-30 17:38:26 +1100 | [diff] [blame] | 920 | struct xfs_ail *ailp = mp->m_ail; |
| 921 | |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 922 | kthread_stop(ailp->ail_task); |
David Chinner | 82fa901 | 2008-10-30 17:38:26 +1100 | [diff] [blame] | 923 | kmem_free(ailp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | } |