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