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-2001,2005 Silicon Graphics, Inc. |
| 4 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include "xfs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 7 | #include "xfs_fs.h" |
Christoph Hellwig | 4fb6e8a | 2014-11-28 14:25:04 +1100 | [diff] [blame] | 8 | #include "xfs_format.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 9 | #include "xfs_log_format.h" |
| 10 | #include "xfs_trans_resv.h" |
Darrick J. Wong | dc42375d | 2016-08-03 11:23:49 +1000 | [diff] [blame] | 11 | #include "xfs_bit.h" |
Darrick J. Wong | 5467b34 | 2019-06-28 19:25:35 -0700 | [diff] [blame] | 12 | #include "xfs_shared.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include "xfs_mount.h" |
Christoph Hellwig | 81f4004 | 2019-06-28 19:28:17 -0700 | [diff] [blame] | 14 | #include "xfs_defer.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 15 | #include "xfs_trans.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include "xfs_trans_priv.h" |
| 17 | #include "xfs_extfree_item.h" |
Christoph Hellwig | 1234351 | 2013-12-13 11:00:43 +1100 | [diff] [blame] | 18 | #include "xfs_log.h" |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 19 | #include "xfs_btree.h" |
| 20 | #include "xfs_rmap.h" |
Christoph Hellwig | 81f4004 | 2019-06-28 19:28:17 -0700 | [diff] [blame] | 21 | #include "xfs_alloc.h" |
| 22 | #include "xfs_bmap.h" |
| 23 | #include "xfs_trace.h" |
Darrick J. Wong | a5155b8 | 2019-11-02 09:40:53 -0700 | [diff] [blame] | 24 | #include "xfs_error.h" |
Darrick J. Wong | 9817aa8 | 2020-05-01 16:00:48 -0700 | [diff] [blame] | 25 | #include "xfs_log_priv.h" |
Darrick J. Wong | 86ffa47 | 2020-05-01 16:00:45 -0700 | [diff] [blame] | 26 | #include "xfs_log_recover.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
Darrick J. Wong | 182696f | 2021-10-12 11:09:23 -0700 | [diff] [blame] | 28 | struct kmem_cache *xfs_efi_cache; |
| 29 | struct kmem_cache *xfs_efd_cache; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Darrick J. Wong | 10d0c6e | 2020-05-01 16:00:50 -0700 | [diff] [blame] | 31 | static const struct xfs_item_ops xfs_efi_item_ops; |
| 32 | |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 33 | static inline struct xfs_efi_log_item *EFI_ITEM(struct xfs_log_item *lip) |
| 34 | { |
| 35 | return container_of(lip, struct xfs_efi_log_item, efi_item); |
| 36 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Darrick J. Wong | 9817aa8 | 2020-05-01 16:00:48 -0700 | [diff] [blame] | 38 | STATIC void |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 39 | xfs_efi_item_free( |
| 40 | struct xfs_efi_log_item *efip) |
Christoph Hellwig | 7d795ca | 2005-06-21 15:41:19 +1000 | [diff] [blame] | 41 | { |
Dave Chinner | b1c5ebb | 2016-07-22 09:52:35 +1000 | [diff] [blame] | 42 | kmem_free(efip->efi_item.li_lv_shadow); |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 43 | if (efip->efi_format.efi_nextents > XFS_EFI_MAX_FAST_EXTENTS) |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 44 | kmem_free(efip); |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 45 | else |
Darrick J. Wong | 182696f | 2021-10-12 11:09:23 -0700 | [diff] [blame] | 46 | kmem_cache_free(xfs_efi_cache, efip); |
Christoph Hellwig | 7d795ca | 2005-06-21 15:41:19 +1000 | [diff] [blame] | 47 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
| 49 | /* |
Dave Chinner | 0612d11 | 2018-04-02 20:08:27 -0700 | [diff] [blame] | 50 | * Freeing the efi requires that we remove it from the AIL if it has already |
| 51 | * been placed there. However, the EFI may not yet have been placed in the AIL |
| 52 | * when called by xfs_efi_release() from EFD processing due to the ordering of |
| 53 | * committed vs unpin operations in bulk insert operations. Hence the reference |
| 54 | * count to ensure only the last caller frees the EFI. |
| 55 | */ |
Darrick J. Wong | 10d0c6e | 2020-05-01 16:00:50 -0700 | [diff] [blame] | 56 | STATIC void |
Dave Chinner | 0612d11 | 2018-04-02 20:08:27 -0700 | [diff] [blame] | 57 | xfs_efi_release( |
| 58 | struct xfs_efi_log_item *efip) |
| 59 | { |
| 60 | ASSERT(atomic_read(&efip->efi_refcount) > 0); |
| 61 | if (atomic_dec_and_test(&efip->efi_refcount)) { |
Brian Foster | 6558792 | 2020-05-06 13:25:23 -0700 | [diff] [blame] | 62 | xfs_trans_ail_delete(&efip->efi_item, SHUTDOWN_LOG_IO_ERROR); |
Dave Chinner | 0612d11 | 2018-04-02 20:08:27 -0700 | [diff] [blame] | 63 | xfs_efi_item_free(efip); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | * This returns the number of iovecs needed to log the given efi item. |
| 69 | * We only need 1 iovec for an efi item. It just logs the efi_log_format |
| 70 | * structure. |
| 71 | */ |
Dave Chinner | 166d136 | 2013-08-12 20:50:04 +1000 | [diff] [blame] | 72 | static inline int |
| 73 | xfs_efi_item_sizeof( |
| 74 | struct xfs_efi_log_item *efip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | { |
Dave Chinner | 166d136 | 2013-08-12 20:50:04 +1000 | [diff] [blame] | 76 | return sizeof(struct xfs_efi_log_format) + |
| 77 | (efip->efi_format.efi_nextents - 1) * sizeof(xfs_extent_t); |
| 78 | } |
| 79 | |
| 80 | STATIC void |
| 81 | xfs_efi_item_size( |
| 82 | struct xfs_log_item *lip, |
| 83 | int *nvecs, |
| 84 | int *nbytes) |
| 85 | { |
| 86 | *nvecs += 1; |
| 87 | *nbytes += xfs_efi_item_sizeof(EFI_ITEM(lip)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /* |
| 91 | * This is called to fill in the vector of log iovecs for the |
| 92 | * given efi log item. We use only 1 iovec, and we point that |
| 93 | * at the efi_log_format structure embedded in the efi item. |
| 94 | * It is at this point that we assert that all of the extent |
| 95 | * slots in the efi item have been filled. |
| 96 | */ |
| 97 | STATIC void |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 98 | xfs_efi_item_format( |
| 99 | struct xfs_log_item *lip, |
Christoph Hellwig | bde7cff | 2013-12-13 11:34:02 +1100 | [diff] [blame] | 100 | struct xfs_log_vec *lv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | { |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 102 | struct xfs_efi_log_item *efip = EFI_ITEM(lip); |
Christoph Hellwig | bde7cff | 2013-12-13 11:34:02 +1100 | [diff] [blame] | 103 | struct xfs_log_iovec *vecp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
Dave Chinner | b199c8a | 2010-12-20 11:59:49 +1100 | [diff] [blame] | 105 | ASSERT(atomic_read(&efip->efi_next_extent) == |
| 106 | efip->efi_format.efi_nextents); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
| 108 | efip->efi_format.efi_type = XFS_LI_EFI; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | efip->efi_format.efi_size = 1; |
| 110 | |
Christoph Hellwig | bde7cff | 2013-12-13 11:34:02 +1100 | [diff] [blame] | 111 | xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFI_FORMAT, |
Christoph Hellwig | 1234351 | 2013-12-13 11:00:43 +1100 | [diff] [blame] | 112 | &efip->efi_format, |
| 113 | xfs_efi_item_sizeof(efip)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | |
| 117 | /* |
Brian Foster | 8d99fe9 | 2015-08-19 09:51:16 +1000 | [diff] [blame] | 118 | * The unpin operation is the last place an EFI is manipulated in the log. It is |
| 119 | * either inserted in the AIL or aborted in the event of a log I/O error. In |
| 120 | * either case, the EFI transaction has been successfully committed to make it |
| 121 | * this far. Therefore, we expect whoever committed the EFI to either construct |
| 122 | * and commit the EFD or drop the EFD's reference in the event of error. Simply |
| 123 | * drop the log's EFI reference now that the log is done with it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | STATIC void |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 126 | xfs_efi_item_unpin( |
| 127 | struct xfs_log_item *lip, |
| 128 | int remove) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | { |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 130 | struct xfs_efi_log_item *efip = EFI_ITEM(lip); |
Brian Foster | 5e4b538 | 2015-08-19 09:50:12 +1000 | [diff] [blame] | 131 | xfs_efi_release(efip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /* |
Brian Foster | 8d99fe9 | 2015-08-19 09:51:16 +1000 | [diff] [blame] | 135 | * The EFI has been either committed or aborted if the transaction has been |
| 136 | * cancelled. If the transaction was cancelled, an EFD isn't going to be |
| 137 | * constructed and thus we free the EFI here directly. |
| 138 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | STATIC void |
Christoph Hellwig | ddf9205 | 2019-06-28 19:27:32 -0700 | [diff] [blame] | 140 | xfs_efi_item_release( |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 141 | struct xfs_log_item *lip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | { |
Christoph Hellwig | ddf9205 | 2019-06-28 19:27:32 -0700 | [diff] [blame] | 143 | xfs_efi_release(EFI_ITEM(lip)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | /* |
| 147 | * Allocate and initialize an efi item with the given number of extents. |
| 148 | */ |
Darrick J. Wong | 9817aa8 | 2020-05-01 16:00:48 -0700 | [diff] [blame] | 149 | STATIC struct xfs_efi_log_item * |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 150 | xfs_efi_init( |
| 151 | struct xfs_mount *mp, |
| 152 | uint nextents) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
| 154 | { |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 155 | struct xfs_efi_log_item *efip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | uint size; |
| 157 | |
| 158 | ASSERT(nextents > 0); |
| 159 | if (nextents > XFS_EFI_MAX_FAST_EXTENTS) { |
Christoph Hellwig | 82ff450 | 2020-04-30 12:52:18 -0700 | [diff] [blame] | 160 | size = (uint)(sizeof(struct xfs_efi_log_item) + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | ((nextents - 1) * sizeof(xfs_extent_t))); |
Tetsuo Handa | 707e0dd | 2019-08-26 12:06:22 -0700 | [diff] [blame] | 162 | efip = kmem_zalloc(size, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | } else { |
Darrick J. Wong | 182696f | 2021-10-12 11:09:23 -0700 | [diff] [blame] | 164 | efip = kmem_cache_zalloc(xfs_efi_cache, |
Carlos Maiolino | 32a2b11 | 2020-07-22 09:23:10 -0700 | [diff] [blame] | 165 | GFP_KERNEL | __GFP_NOFAIL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Dave Chinner | 43f5efc | 2010-03-23 10:10:00 +1100 | [diff] [blame] | 168 | xfs_log_item_init(mp, &efip->efi_item, XFS_LI_EFI, &xfs_efi_item_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | efip->efi_format.efi_nextents = nextents; |
Christoph Hellwig | db9d67d | 2015-06-22 09:43:32 +1000 | [diff] [blame] | 170 | efip->efi_format.efi_id = (uintptr_t)(void *)efip; |
Dave Chinner | b199c8a | 2010-12-20 11:59:49 +1100 | [diff] [blame] | 171 | atomic_set(&efip->efi_next_extent, 0); |
Dave Chinner | 666d644 | 2013-04-03 14:09:21 +1100 | [diff] [blame] | 172 | atomic_set(&efip->efi_refcount, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 174 | return efip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /* |
Tim Shimmin | 6d192a9 | 2006-06-09 14:55:38 +1000 | [diff] [blame] | 178 | * Copy an EFI format buffer from the given buf, and into the destination |
| 179 | * EFI format structure. |
| 180 | * The given buffer can be in 32 bit or 64 bit form (which has different padding), |
| 181 | * one of which will be the native format for this kernel. |
| 182 | * It will handle the conversion of formats if necessary. |
| 183 | */ |
Darrick J. Wong | 9817aa8 | 2020-05-01 16:00:48 -0700 | [diff] [blame] | 184 | STATIC int |
Tim Shimmin | 6d192a9 | 2006-06-09 14:55:38 +1000 | [diff] [blame] | 185 | xfs_efi_copy_format(xfs_log_iovec_t *buf, xfs_efi_log_format_t *dst_efi_fmt) |
| 186 | { |
Christoph Hellwig | 4e0d5f9 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 187 | xfs_efi_log_format_t *src_efi_fmt = buf->i_addr; |
Tim Shimmin | 6d192a9 | 2006-06-09 14:55:38 +1000 | [diff] [blame] | 188 | uint i; |
| 189 | uint len = sizeof(xfs_efi_log_format_t) + |
| 190 | (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_t); |
| 191 | uint len32 = sizeof(xfs_efi_log_format_32_t) + |
| 192 | (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_32_t); |
| 193 | uint len64 = sizeof(xfs_efi_log_format_64_t) + |
| 194 | (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_64_t); |
| 195 | |
| 196 | if (buf->i_len == len) { |
| 197 | memcpy((char *)dst_efi_fmt, (char*)src_efi_fmt, len); |
| 198 | return 0; |
| 199 | } else if (buf->i_len == len32) { |
Christoph Hellwig | 4e0d5f9 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 200 | xfs_efi_log_format_32_t *src_efi_fmt_32 = buf->i_addr; |
Tim Shimmin | 6d192a9 | 2006-06-09 14:55:38 +1000 | [diff] [blame] | 201 | |
| 202 | dst_efi_fmt->efi_type = src_efi_fmt_32->efi_type; |
| 203 | dst_efi_fmt->efi_size = src_efi_fmt_32->efi_size; |
| 204 | dst_efi_fmt->efi_nextents = src_efi_fmt_32->efi_nextents; |
| 205 | dst_efi_fmt->efi_id = src_efi_fmt_32->efi_id; |
| 206 | for (i = 0; i < dst_efi_fmt->efi_nextents; i++) { |
| 207 | dst_efi_fmt->efi_extents[i].ext_start = |
| 208 | src_efi_fmt_32->efi_extents[i].ext_start; |
| 209 | dst_efi_fmt->efi_extents[i].ext_len = |
| 210 | src_efi_fmt_32->efi_extents[i].ext_len; |
| 211 | } |
| 212 | return 0; |
| 213 | } else if (buf->i_len == len64) { |
Christoph Hellwig | 4e0d5f9 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 214 | xfs_efi_log_format_64_t *src_efi_fmt_64 = buf->i_addr; |
Tim Shimmin | 6d192a9 | 2006-06-09 14:55:38 +1000 | [diff] [blame] | 215 | |
| 216 | dst_efi_fmt->efi_type = src_efi_fmt_64->efi_type; |
| 217 | dst_efi_fmt->efi_size = src_efi_fmt_64->efi_size; |
| 218 | dst_efi_fmt->efi_nextents = src_efi_fmt_64->efi_nextents; |
| 219 | dst_efi_fmt->efi_id = src_efi_fmt_64->efi_id; |
| 220 | for (i = 0; i < dst_efi_fmt->efi_nextents; i++) { |
| 221 | dst_efi_fmt->efi_extents[i].ext_start = |
| 222 | src_efi_fmt_64->efi_extents[i].ext_start; |
| 223 | dst_efi_fmt->efi_extents[i].ext_len = |
| 224 | src_efi_fmt_64->efi_extents[i].ext_len; |
| 225 | } |
| 226 | return 0; |
| 227 | } |
Darrick J. Wong | a5155b8 | 2019-11-02 09:40:53 -0700 | [diff] [blame] | 228 | XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, NULL); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 229 | return -EFSCORRUPTED; |
Tim Shimmin | 6d192a9 | 2006-06-09 14:55:38 +1000 | [diff] [blame] | 230 | } |
| 231 | |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 232 | static inline struct xfs_efd_log_item *EFD_ITEM(struct xfs_log_item *lip) |
Christoph Hellwig | 7d795ca | 2005-06-21 15:41:19 +1000 | [diff] [blame] | 233 | { |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 234 | return container_of(lip, struct xfs_efd_log_item, efd_item); |
| 235 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 237 | STATIC void |
| 238 | xfs_efd_item_free(struct xfs_efd_log_item *efdp) |
| 239 | { |
Dave Chinner | b1c5ebb | 2016-07-22 09:52:35 +1000 | [diff] [blame] | 240 | kmem_free(efdp->efd_item.li_lv_shadow); |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 241 | if (efdp->efd_format.efd_nextents > XFS_EFD_MAX_FAST_EXTENTS) |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 242 | kmem_free(efdp); |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 243 | else |
Darrick J. Wong | 182696f | 2021-10-12 11:09:23 -0700 | [diff] [blame] | 244 | kmem_cache_free(xfs_efd_cache, efdp); |
Christoph Hellwig | 7d795ca | 2005-06-21 15:41:19 +1000 | [diff] [blame] | 245 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | |
| 247 | /* |
| 248 | * This returns the number of iovecs needed to log the given efd item. |
| 249 | * We only need 1 iovec for an efd item. It just logs the efd_log_format |
| 250 | * structure. |
| 251 | */ |
Dave Chinner | 166d136 | 2013-08-12 20:50:04 +1000 | [diff] [blame] | 252 | static inline int |
| 253 | xfs_efd_item_sizeof( |
| 254 | struct xfs_efd_log_item *efdp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | { |
Dave Chinner | 166d136 | 2013-08-12 20:50:04 +1000 | [diff] [blame] | 256 | return sizeof(xfs_efd_log_format_t) + |
| 257 | (efdp->efd_format.efd_nextents - 1) * sizeof(xfs_extent_t); |
| 258 | } |
| 259 | |
| 260 | STATIC void |
| 261 | xfs_efd_item_size( |
| 262 | struct xfs_log_item *lip, |
| 263 | int *nvecs, |
| 264 | int *nbytes) |
| 265 | { |
| 266 | *nvecs += 1; |
| 267 | *nbytes += xfs_efd_item_sizeof(EFD_ITEM(lip)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | /* |
| 271 | * This is called to fill in the vector of log iovecs for the |
| 272 | * given efd log item. We use only 1 iovec, and we point that |
| 273 | * at the efd_log_format structure embedded in the efd item. |
| 274 | * It is at this point that we assert that all of the extent |
| 275 | * slots in the efd item have been filled. |
| 276 | */ |
| 277 | STATIC void |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 278 | xfs_efd_item_format( |
| 279 | struct xfs_log_item *lip, |
Christoph Hellwig | bde7cff | 2013-12-13 11:34:02 +1100 | [diff] [blame] | 280 | struct xfs_log_vec *lv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | { |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 282 | struct xfs_efd_log_item *efdp = EFD_ITEM(lip); |
Christoph Hellwig | bde7cff | 2013-12-13 11:34:02 +1100 | [diff] [blame] | 283 | struct xfs_log_iovec *vecp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | |
| 285 | ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents); |
| 286 | |
| 287 | efdp->efd_format.efd_type = XFS_LI_EFD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | efdp->efd_format.efd_size = 1; |
| 289 | |
Christoph Hellwig | bde7cff | 2013-12-13 11:34:02 +1100 | [diff] [blame] | 290 | xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFD_FORMAT, |
Christoph Hellwig | 1234351 | 2013-12-13 11:00:43 +1100 | [diff] [blame] | 291 | &efdp->efd_format, |
| 292 | xfs_efd_item_sizeof(efdp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | /* |
Brian Foster | 8d99fe9 | 2015-08-19 09:51:16 +1000 | [diff] [blame] | 296 | * The EFD is either committed or aborted if the transaction is cancelled. If |
| 297 | * the transaction is cancelled, drop our reference to the EFI and free the EFD. |
| 298 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | STATIC void |
Christoph Hellwig | ddf9205 | 2019-06-28 19:27:32 -0700 | [diff] [blame] | 300 | xfs_efd_item_release( |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 301 | struct xfs_log_item *lip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | { |
Brian Foster | 8d99fe9 | 2015-08-19 09:51:16 +1000 | [diff] [blame] | 303 | struct xfs_efd_log_item *efdp = EFD_ITEM(lip); |
| 304 | |
Christoph Hellwig | ddf9205 | 2019-06-28 19:27:32 -0700 | [diff] [blame] | 305 | xfs_efi_release(efdp->efd_efip); |
| 306 | xfs_efd_item_free(efdp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Christoph Hellwig | 272e42b | 2011-10-28 09:54:24 +0000 | [diff] [blame] | 309 | static const struct xfs_item_ops xfs_efd_item_ops = { |
Christoph Hellwig | 9ce632a | 2019-06-28 19:27:32 -0700 | [diff] [blame] | 310 | .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED, |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 311 | .iop_size = xfs_efd_item_size, |
| 312 | .iop_format = xfs_efd_item_format, |
Christoph Hellwig | ddf9205 | 2019-06-28 19:27:32 -0700 | [diff] [blame] | 313 | .iop_release = xfs_efd_item_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | }; |
| 315 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | /* |
Christoph Hellwig | 9c5e7c2 | 2019-06-28 19:27:35 -0700 | [diff] [blame] | 317 | * Allocate an "extent free done" log item that will hold nextents worth of |
| 318 | * extents. The caller must use all nextents extents, because we are not |
| 319 | * flexible about this at all. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | */ |
Christoph Hellwig | 81f4004 | 2019-06-28 19:28:17 -0700 | [diff] [blame] | 321 | static struct xfs_efd_log_item * |
Christoph Hellwig | 9c5e7c2 | 2019-06-28 19:27:35 -0700 | [diff] [blame] | 322 | xfs_trans_get_efd( |
| 323 | struct xfs_trans *tp, |
| 324 | struct xfs_efi_log_item *efip, |
| 325 | unsigned int nextents) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | { |
Christoph Hellwig | 9c5e7c2 | 2019-06-28 19:27:35 -0700 | [diff] [blame] | 327 | struct xfs_efd_log_item *efdp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | |
| 329 | ASSERT(nextents > 0); |
Christoph Hellwig | 9c5e7c2 | 2019-06-28 19:27:35 -0700 | [diff] [blame] | 330 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | if (nextents > XFS_EFD_MAX_FAST_EXTENTS) { |
Christoph Hellwig | 9c5e7c2 | 2019-06-28 19:27:35 -0700 | [diff] [blame] | 332 | efdp = kmem_zalloc(sizeof(struct xfs_efd_log_item) + |
| 333 | (nextents - 1) * sizeof(struct xfs_extent), |
Tetsuo Handa | 707e0dd | 2019-08-26 12:06:22 -0700 | [diff] [blame] | 334 | 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | } else { |
Darrick J. Wong | 182696f | 2021-10-12 11:09:23 -0700 | [diff] [blame] | 336 | efdp = kmem_cache_zalloc(xfs_efd_cache, |
Carlos Maiolino | 32a2b11 | 2020-07-22 09:23:10 -0700 | [diff] [blame] | 337 | GFP_KERNEL | __GFP_NOFAIL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Christoph Hellwig | 9c5e7c2 | 2019-06-28 19:27:35 -0700 | [diff] [blame] | 340 | xfs_log_item_init(tp->t_mountp, &efdp->efd_item, XFS_LI_EFD, |
| 341 | &xfs_efd_item_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | efdp->efd_efip = efip; |
| 343 | efdp->efd_format.efd_nextents = nextents; |
| 344 | efdp->efd_format.efd_efi_id = efip->efi_format.efi_id; |
| 345 | |
Christoph Hellwig | 9c5e7c2 | 2019-06-28 19:27:35 -0700 | [diff] [blame] | 346 | xfs_trans_add_item(tp, &efdp->efd_item); |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 347 | return efdp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | } |
Darrick J. Wong | dc42375d | 2016-08-03 11:23:49 +1000 | [diff] [blame] | 349 | |
| 350 | /* |
Christoph Hellwig | 81f4004 | 2019-06-28 19:28:17 -0700 | [diff] [blame] | 351 | * Free an extent and log it to the EFD. Note that the transaction is marked |
| 352 | * dirty regardless of whether the extent free succeeds or fails to support the |
| 353 | * EFI/EFD lifecycle rules. |
| 354 | */ |
| 355 | static int |
| 356 | xfs_trans_free_extent( |
| 357 | struct xfs_trans *tp, |
| 358 | struct xfs_efd_log_item *efdp, |
| 359 | xfs_fsblock_t start_block, |
| 360 | xfs_extlen_t ext_len, |
| 361 | const struct xfs_owner_info *oinfo, |
| 362 | bool skip_discard) |
| 363 | { |
| 364 | struct xfs_mount *mp = tp->t_mountp; |
| 365 | struct xfs_extent *extp; |
| 366 | uint next_extent; |
| 367 | xfs_agnumber_t agno = XFS_FSB_TO_AGNO(mp, start_block); |
| 368 | xfs_agblock_t agbno = XFS_FSB_TO_AGBNO(mp, |
| 369 | start_block); |
| 370 | int error; |
| 371 | |
| 372 | trace_xfs_bmap_free_deferred(tp->t_mountp, agno, 0, agbno, ext_len); |
| 373 | |
| 374 | error = __xfs_free_extent(tp, start_block, ext_len, |
| 375 | oinfo, XFS_AG_RESV_NONE, skip_discard); |
| 376 | /* |
| 377 | * Mark the transaction dirty, even on error. This ensures the |
| 378 | * transaction is aborted, which: |
| 379 | * |
| 380 | * 1.) releases the EFI and frees the EFD |
| 381 | * 2.) shuts down the filesystem |
| 382 | */ |
| 383 | tp->t_flags |= XFS_TRANS_DIRTY; |
| 384 | set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags); |
| 385 | |
| 386 | next_extent = efdp->efd_next_extent; |
| 387 | ASSERT(next_extent < efdp->efd_format.efd_nextents); |
| 388 | extp = &(efdp->efd_format.efd_extents[next_extent]); |
| 389 | extp->ext_start = start_block; |
| 390 | extp->ext_len = ext_len; |
| 391 | efdp->efd_next_extent++; |
| 392 | |
| 393 | return error; |
| 394 | } |
| 395 | |
| 396 | /* Sort bmap items by AG. */ |
| 397 | static int |
| 398 | xfs_extent_free_diff_items( |
| 399 | void *priv, |
Sami Tolvanen | 4f0f586 | 2021-04-08 11:28:34 -0700 | [diff] [blame] | 400 | const struct list_head *a, |
| 401 | const struct list_head *b) |
Christoph Hellwig | 81f4004 | 2019-06-28 19:28:17 -0700 | [diff] [blame] | 402 | { |
| 403 | struct xfs_mount *mp = priv; |
| 404 | struct xfs_extent_free_item *ra; |
| 405 | struct xfs_extent_free_item *rb; |
| 406 | |
| 407 | ra = container_of(a, struct xfs_extent_free_item, xefi_list); |
| 408 | rb = container_of(b, struct xfs_extent_free_item, xefi_list); |
| 409 | return XFS_FSB_TO_AGNO(mp, ra->xefi_startblock) - |
| 410 | XFS_FSB_TO_AGNO(mp, rb->xefi_startblock); |
| 411 | } |
| 412 | |
Christoph Hellwig | 81f4004 | 2019-06-28 19:28:17 -0700 | [diff] [blame] | 413 | /* Log a free extent to the intent item. */ |
| 414 | STATIC void |
| 415 | xfs_extent_free_log_item( |
| 416 | struct xfs_trans *tp, |
Christoph Hellwig | c1f0918 | 2020-04-30 12:52:20 -0700 | [diff] [blame] | 417 | struct xfs_efi_log_item *efip, |
| 418 | struct xfs_extent_free_item *free) |
Christoph Hellwig | 81f4004 | 2019-06-28 19:28:17 -0700 | [diff] [blame] | 419 | { |
Christoph Hellwig | 81f4004 | 2019-06-28 19:28:17 -0700 | [diff] [blame] | 420 | uint next_extent; |
| 421 | struct xfs_extent *extp; |
| 422 | |
Christoph Hellwig | 81f4004 | 2019-06-28 19:28:17 -0700 | [diff] [blame] | 423 | tp->t_flags |= XFS_TRANS_DIRTY; |
| 424 | set_bit(XFS_LI_DIRTY, &efip->efi_item.li_flags); |
| 425 | |
| 426 | /* |
| 427 | * atomic_inc_return gives us the value after the increment; |
| 428 | * we want to use it as an array index so we need to subtract 1 from |
| 429 | * it. |
| 430 | */ |
| 431 | next_extent = atomic_inc_return(&efip->efi_next_extent) - 1; |
| 432 | ASSERT(next_extent < efip->efi_format.efi_nextents); |
| 433 | extp = &efip->efi_format.efi_extents[next_extent]; |
| 434 | extp->ext_start = free->xefi_startblock; |
| 435 | extp->ext_len = free->xefi_blockcount; |
| 436 | } |
| 437 | |
Christoph Hellwig | 13a8333 | 2020-04-30 12:52:21 -0700 | [diff] [blame] | 438 | static struct xfs_log_item * |
Christoph Hellwig | c1f0918 | 2020-04-30 12:52:20 -0700 | [diff] [blame] | 439 | xfs_extent_free_create_intent( |
| 440 | struct xfs_trans *tp, |
| 441 | struct list_head *items, |
Christoph Hellwig | d367a86 | 2020-04-30 12:52:20 -0700 | [diff] [blame] | 442 | unsigned int count, |
| 443 | bool sort) |
Christoph Hellwig | c1f0918 | 2020-04-30 12:52:20 -0700 | [diff] [blame] | 444 | { |
| 445 | struct xfs_mount *mp = tp->t_mountp; |
| 446 | struct xfs_efi_log_item *efip = xfs_efi_init(mp, count); |
| 447 | struct xfs_extent_free_item *free; |
| 448 | |
| 449 | ASSERT(count > 0); |
| 450 | |
| 451 | xfs_trans_add_item(tp, &efip->efi_item); |
Christoph Hellwig | d367a86 | 2020-04-30 12:52:20 -0700 | [diff] [blame] | 452 | if (sort) |
| 453 | list_sort(mp, items, xfs_extent_free_diff_items); |
Christoph Hellwig | c1f0918 | 2020-04-30 12:52:20 -0700 | [diff] [blame] | 454 | list_for_each_entry(free, items, xefi_list) |
| 455 | xfs_extent_free_log_item(tp, efip, free); |
Christoph Hellwig | 13a8333 | 2020-04-30 12:52:21 -0700 | [diff] [blame] | 456 | return &efip->efi_item; |
Christoph Hellwig | c1f0918 | 2020-04-30 12:52:20 -0700 | [diff] [blame] | 457 | } |
| 458 | |
Christoph Hellwig | 81f4004 | 2019-06-28 19:28:17 -0700 | [diff] [blame] | 459 | /* Get an EFD so we can process all the free extents. */ |
Christoph Hellwig | f09d167 | 2020-04-30 12:52:22 -0700 | [diff] [blame] | 460 | static struct xfs_log_item * |
Christoph Hellwig | 81f4004 | 2019-06-28 19:28:17 -0700 | [diff] [blame] | 461 | xfs_extent_free_create_done( |
| 462 | struct xfs_trans *tp, |
Christoph Hellwig | 13a8333 | 2020-04-30 12:52:21 -0700 | [diff] [blame] | 463 | struct xfs_log_item *intent, |
Christoph Hellwig | 81f4004 | 2019-06-28 19:28:17 -0700 | [diff] [blame] | 464 | unsigned int count) |
| 465 | { |
Christoph Hellwig | f09d167 | 2020-04-30 12:52:22 -0700 | [diff] [blame] | 466 | return &xfs_trans_get_efd(tp, EFI_ITEM(intent), count)->efd_item; |
Christoph Hellwig | 81f4004 | 2019-06-28 19:28:17 -0700 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | /* Process a free extent. */ |
| 470 | STATIC int |
| 471 | xfs_extent_free_finish_item( |
| 472 | struct xfs_trans *tp, |
Christoph Hellwig | f09d167 | 2020-04-30 12:52:22 -0700 | [diff] [blame] | 473 | struct xfs_log_item *done, |
Christoph Hellwig | 81f4004 | 2019-06-28 19:28:17 -0700 | [diff] [blame] | 474 | struct list_head *item, |
Christoph Hellwig | 3ec1b26 | 2020-04-30 12:52:22 -0700 | [diff] [blame] | 475 | struct xfs_btree_cur **state) |
Christoph Hellwig | 81f4004 | 2019-06-28 19:28:17 -0700 | [diff] [blame] | 476 | { |
Darrick J. Wong | b3b5ff4 | 2021-10-12 15:55:54 -0700 | [diff] [blame] | 477 | struct xfs_owner_info oinfo = { }; |
Christoph Hellwig | 81f4004 | 2019-06-28 19:28:17 -0700 | [diff] [blame] | 478 | struct xfs_extent_free_item *free; |
| 479 | int error; |
| 480 | |
| 481 | free = container_of(item, struct xfs_extent_free_item, xefi_list); |
Darrick J. Wong | b3b5ff4 | 2021-10-12 15:55:54 -0700 | [diff] [blame] | 482 | oinfo.oi_owner = free->xefi_owner; |
| 483 | if (free->xefi_flags & XFS_EFI_ATTR_FORK) |
| 484 | oinfo.oi_flags |= XFS_OWNER_INFO_ATTR_FORK; |
| 485 | if (free->xefi_flags & XFS_EFI_BMBT_BLOCK) |
| 486 | oinfo.oi_flags |= XFS_OWNER_INFO_BMBT_BLOCK; |
Christoph Hellwig | f09d167 | 2020-04-30 12:52:22 -0700 | [diff] [blame] | 487 | error = xfs_trans_free_extent(tp, EFD_ITEM(done), |
Christoph Hellwig | 81f4004 | 2019-06-28 19:28:17 -0700 | [diff] [blame] | 488 | free->xefi_startblock, |
| 489 | free->xefi_blockcount, |
Darrick J. Wong | b3b5ff4 | 2021-10-12 15:55:54 -0700 | [diff] [blame] | 490 | &oinfo, free->xefi_flags & XFS_EFI_SKIP_DISCARD); |
Darrick J. Wong | c201d9c | 2021-10-12 14:17:01 -0700 | [diff] [blame] | 491 | kmem_cache_free(xfs_extfree_item_cache, free); |
Christoph Hellwig | 81f4004 | 2019-06-28 19:28:17 -0700 | [diff] [blame] | 492 | return error; |
| 493 | } |
| 494 | |
| 495 | /* Abort all pending EFIs. */ |
| 496 | STATIC void |
| 497 | xfs_extent_free_abort_intent( |
Christoph Hellwig | 13a8333 | 2020-04-30 12:52:21 -0700 | [diff] [blame] | 498 | struct xfs_log_item *intent) |
Christoph Hellwig | 81f4004 | 2019-06-28 19:28:17 -0700 | [diff] [blame] | 499 | { |
Christoph Hellwig | 13a8333 | 2020-04-30 12:52:21 -0700 | [diff] [blame] | 500 | xfs_efi_release(EFI_ITEM(intent)); |
Christoph Hellwig | 81f4004 | 2019-06-28 19:28:17 -0700 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | /* Cancel a free extent. */ |
| 504 | STATIC void |
| 505 | xfs_extent_free_cancel_item( |
| 506 | struct list_head *item) |
| 507 | { |
| 508 | struct xfs_extent_free_item *free; |
| 509 | |
| 510 | free = container_of(item, struct xfs_extent_free_item, xefi_list); |
Darrick J. Wong | c201d9c | 2021-10-12 14:17:01 -0700 | [diff] [blame] | 511 | kmem_cache_free(xfs_extfree_item_cache, free); |
Christoph Hellwig | 81f4004 | 2019-06-28 19:28:17 -0700 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | const struct xfs_defer_op_type xfs_extent_free_defer_type = { |
| 515 | .max_items = XFS_EFI_MAX_FAST_EXTENTS, |
Christoph Hellwig | 81f4004 | 2019-06-28 19:28:17 -0700 | [diff] [blame] | 516 | .create_intent = xfs_extent_free_create_intent, |
| 517 | .abort_intent = xfs_extent_free_abort_intent, |
Christoph Hellwig | 81f4004 | 2019-06-28 19:28:17 -0700 | [diff] [blame] | 518 | .create_done = xfs_extent_free_create_done, |
| 519 | .finish_item = xfs_extent_free_finish_item, |
| 520 | .cancel_item = xfs_extent_free_cancel_item, |
| 521 | }; |
| 522 | |
| 523 | /* |
| 524 | * AGFL blocks are accounted differently in the reserve pools and are not |
| 525 | * inserted into the busy extent list. |
| 526 | */ |
| 527 | STATIC int |
| 528 | xfs_agfl_free_finish_item( |
| 529 | struct xfs_trans *tp, |
Christoph Hellwig | f09d167 | 2020-04-30 12:52:22 -0700 | [diff] [blame] | 530 | struct xfs_log_item *done, |
Christoph Hellwig | 81f4004 | 2019-06-28 19:28:17 -0700 | [diff] [blame] | 531 | struct list_head *item, |
Christoph Hellwig | 3ec1b26 | 2020-04-30 12:52:22 -0700 | [diff] [blame] | 532 | struct xfs_btree_cur **state) |
Christoph Hellwig | 81f4004 | 2019-06-28 19:28:17 -0700 | [diff] [blame] | 533 | { |
Darrick J. Wong | b3b5ff4 | 2021-10-12 15:55:54 -0700 | [diff] [blame] | 534 | struct xfs_owner_info oinfo = { }; |
Christoph Hellwig | 81f4004 | 2019-06-28 19:28:17 -0700 | [diff] [blame] | 535 | struct xfs_mount *mp = tp->t_mountp; |
Christoph Hellwig | f09d167 | 2020-04-30 12:52:22 -0700 | [diff] [blame] | 536 | struct xfs_efd_log_item *efdp = EFD_ITEM(done); |
Christoph Hellwig | 81f4004 | 2019-06-28 19:28:17 -0700 | [diff] [blame] | 537 | struct xfs_extent_free_item *free; |
| 538 | struct xfs_extent *extp; |
| 539 | struct xfs_buf *agbp; |
| 540 | int error; |
| 541 | xfs_agnumber_t agno; |
| 542 | xfs_agblock_t agbno; |
| 543 | uint next_extent; |
| 544 | |
| 545 | free = container_of(item, struct xfs_extent_free_item, xefi_list); |
| 546 | ASSERT(free->xefi_blockcount == 1); |
| 547 | agno = XFS_FSB_TO_AGNO(mp, free->xefi_startblock); |
| 548 | agbno = XFS_FSB_TO_AGBNO(mp, free->xefi_startblock); |
Darrick J. Wong | b3b5ff4 | 2021-10-12 15:55:54 -0700 | [diff] [blame] | 549 | oinfo.oi_owner = free->xefi_owner; |
Christoph Hellwig | 81f4004 | 2019-06-28 19:28:17 -0700 | [diff] [blame] | 550 | |
| 551 | trace_xfs_agfl_free_deferred(mp, agno, 0, agbno, free->xefi_blockcount); |
| 552 | |
| 553 | error = xfs_alloc_read_agf(mp, tp, agno, 0, &agbp); |
| 554 | if (!error) |
Darrick J. Wong | b3b5ff4 | 2021-10-12 15:55:54 -0700 | [diff] [blame] | 555 | error = xfs_free_agfl_block(tp, agno, agbno, agbp, &oinfo); |
Christoph Hellwig | 81f4004 | 2019-06-28 19:28:17 -0700 | [diff] [blame] | 556 | |
| 557 | /* |
| 558 | * Mark the transaction dirty, even on error. This ensures the |
| 559 | * transaction is aborted, which: |
| 560 | * |
| 561 | * 1.) releases the EFI and frees the EFD |
| 562 | * 2.) shuts down the filesystem |
| 563 | */ |
| 564 | tp->t_flags |= XFS_TRANS_DIRTY; |
| 565 | set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags); |
| 566 | |
| 567 | next_extent = efdp->efd_next_extent; |
| 568 | ASSERT(next_extent < efdp->efd_format.efd_nextents); |
| 569 | extp = &(efdp->efd_format.efd_extents[next_extent]); |
| 570 | extp->ext_start = free->xefi_startblock; |
| 571 | extp->ext_len = free->xefi_blockcount; |
| 572 | efdp->efd_next_extent++; |
| 573 | |
Darrick J. Wong | c201d9c | 2021-10-12 14:17:01 -0700 | [diff] [blame] | 574 | kmem_cache_free(xfs_extfree_item_cache, free); |
Christoph Hellwig | 81f4004 | 2019-06-28 19:28:17 -0700 | [diff] [blame] | 575 | return error; |
| 576 | } |
| 577 | |
| 578 | /* sub-type with special handling for AGFL deferred frees */ |
| 579 | const struct xfs_defer_op_type xfs_agfl_free_defer_type = { |
| 580 | .max_items = XFS_EFI_MAX_FAST_EXTENTS, |
Christoph Hellwig | 81f4004 | 2019-06-28 19:28:17 -0700 | [diff] [blame] | 581 | .create_intent = xfs_extent_free_create_intent, |
| 582 | .abort_intent = xfs_extent_free_abort_intent, |
Christoph Hellwig | 81f4004 | 2019-06-28 19:28:17 -0700 | [diff] [blame] | 583 | .create_done = xfs_extent_free_create_done, |
| 584 | .finish_item = xfs_agfl_free_finish_item, |
| 585 | .cancel_item = xfs_extent_free_cancel_item, |
| 586 | }; |
| 587 | |
Darrick J. Wong | 3c15df3 | 2020-11-29 16:33:38 -0800 | [diff] [blame] | 588 | /* Is this recovered EFI ok? */ |
| 589 | static inline bool |
| 590 | xfs_efi_validate_ext( |
| 591 | struct xfs_mount *mp, |
| 592 | struct xfs_extent *extp) |
| 593 | { |
Darrick J. Wong | 67457eb | 2020-12-04 13:20:00 -0800 | [diff] [blame] | 594 | return xfs_verify_fsbext(mp, extp->ext_start, extp->ext_len); |
Darrick J. Wong | 3c15df3 | 2020-11-29 16:33:38 -0800 | [diff] [blame] | 595 | } |
| 596 | |
Christoph Hellwig | 81f4004 | 2019-06-28 19:28:17 -0700 | [diff] [blame] | 597 | /* |
Darrick J. Wong | dc42375d | 2016-08-03 11:23:49 +1000 | [diff] [blame] | 598 | * Process an extent free intent item that was recovered from |
| 599 | * the log. We need to free the extents that it describes. |
| 600 | */ |
Darrick J. Wong | 10d0c6e | 2020-05-01 16:00:50 -0700 | [diff] [blame] | 601 | STATIC int |
Darrick J. Wong | 96b60f8 | 2020-05-01 16:00:55 -0700 | [diff] [blame] | 602 | xfs_efi_item_recover( |
| 603 | struct xfs_log_item *lip, |
Darrick J. Wong | e6fff81 | 2020-09-25 17:39:37 -0700 | [diff] [blame] | 604 | struct list_head *capture_list) |
Darrick J. Wong | dc42375d | 2016-08-03 11:23:49 +1000 | [diff] [blame] | 605 | { |
Darrick J. Wong | 96b60f8 | 2020-05-01 16:00:55 -0700 | [diff] [blame] | 606 | struct xfs_efi_log_item *efip = EFI_ITEM(lip); |
Dave Chinner | d86142d | 2022-03-17 09:09:12 -0700 | [diff] [blame] | 607 | struct xfs_mount *mp = lip->li_log->l_mp; |
Darrick J. Wong | 96b60f8 | 2020-05-01 16:00:55 -0700 | [diff] [blame] | 608 | struct xfs_efd_log_item *efdp; |
| 609 | struct xfs_trans *tp; |
| 610 | struct xfs_extent *extp; |
Darrick J. Wong | 96b60f8 | 2020-05-01 16:00:55 -0700 | [diff] [blame] | 611 | int i; |
| 612 | int error = 0; |
Darrick J. Wong | dc42375d | 2016-08-03 11:23:49 +1000 | [diff] [blame] | 613 | |
Darrick J. Wong | dc42375d | 2016-08-03 11:23:49 +1000 | [diff] [blame] | 614 | /* |
| 615 | * First check the validity of the extents described by the |
| 616 | * EFI. If any are bad, then assume that all are bad and |
| 617 | * just toss the EFI. |
| 618 | */ |
| 619 | for (i = 0; i < efip->efi_format.efi_nextents; i++) { |
Darrick J. Wong | 3c15df3 | 2020-11-29 16:33:38 -0800 | [diff] [blame] | 620 | if (!xfs_efi_validate_ext(mp, |
| 621 | &efip->efi_format.efi_extents[i])) { |
| 622 | XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, |
| 623 | &efip->efi_format, |
| 624 | sizeof(efip->efi_format)); |
Darrick J. Wong | 895e196 | 2019-11-06 09:17:43 -0800 | [diff] [blame] | 625 | return -EFSCORRUPTED; |
Darrick J. Wong | 3c15df3 | 2020-11-29 16:33:38 -0800 | [diff] [blame] | 626 | } |
Darrick J. Wong | dc42375d | 2016-08-03 11:23:49 +1000 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp); |
| 630 | if (error) |
| 631 | return error; |
| 632 | efdp = xfs_trans_get_efd(tp, efip, efip->efi_format.efi_nextents); |
| 633 | |
| 634 | for (i = 0; i < efip->efi_format.efi_nextents; i++) { |
Darrick J. Wong | e127faf | 2016-08-03 12:29:32 +1000 | [diff] [blame] | 635 | extp = &efip->efi_format.efi_extents[i]; |
Darrick J. Wong | dc42375d | 2016-08-03 11:23:49 +1000 | [diff] [blame] | 636 | error = xfs_trans_free_extent(tp, efdp, extp->ext_start, |
Darrick J. Wong | 7280fed | 2018-12-12 08:46:23 -0800 | [diff] [blame] | 637 | extp->ext_len, |
| 638 | &XFS_RMAP_OINFO_ANY_OWNER, false); |
Darrick J. Wong | 43059d5 | 2021-08-06 11:06:35 -0700 | [diff] [blame] | 639 | if (error == -EFSCORRUPTED) |
| 640 | XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, |
| 641 | extp, sizeof(*extp)); |
Darrick J. Wong | dc42375d | 2016-08-03 11:23:49 +1000 | [diff] [blame] | 642 | if (error) |
| 643 | goto abort_error; |
| 644 | |
| 645 | } |
| 646 | |
Darrick J. Wong | 512edfa | 2021-09-16 17:28:07 -0700 | [diff] [blame] | 647 | return xfs_defer_ops_capture_and_commit(tp, capture_list); |
Darrick J. Wong | dc42375d | 2016-08-03 11:23:49 +1000 | [diff] [blame] | 648 | |
| 649 | abort_error: |
| 650 | xfs_trans_cancel(tp); |
| 651 | return error; |
| 652 | } |
Darrick J. Wong | 86ffa47 | 2020-05-01 16:00:45 -0700 | [diff] [blame] | 653 | |
Darrick J. Wong | 154c733 | 2020-05-01 16:00:54 -0700 | [diff] [blame] | 654 | STATIC bool |
| 655 | xfs_efi_item_match( |
| 656 | struct xfs_log_item *lip, |
| 657 | uint64_t intent_id) |
| 658 | { |
| 659 | return EFI_ITEM(lip)->efi_format.efi_id == intent_id; |
| 660 | } |
| 661 | |
Darrick J. Wong | 4e919af | 2020-09-27 16:18:13 -0700 | [diff] [blame] | 662 | /* Relog an intent item to push the log tail forward. */ |
| 663 | static struct xfs_log_item * |
| 664 | xfs_efi_item_relog( |
| 665 | struct xfs_log_item *intent, |
| 666 | struct xfs_trans *tp) |
| 667 | { |
| 668 | struct xfs_efd_log_item *efdp; |
| 669 | struct xfs_efi_log_item *efip; |
| 670 | struct xfs_extent *extp; |
| 671 | unsigned int count; |
| 672 | |
| 673 | count = EFI_ITEM(intent)->efi_format.efi_nextents; |
| 674 | extp = EFI_ITEM(intent)->efi_format.efi_extents; |
| 675 | |
| 676 | tp->t_flags |= XFS_TRANS_DIRTY; |
| 677 | efdp = xfs_trans_get_efd(tp, EFI_ITEM(intent), count); |
| 678 | efdp->efd_next_extent = count; |
| 679 | memcpy(efdp->efd_format.efd_extents, extp, count * sizeof(*extp)); |
| 680 | set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags); |
| 681 | |
| 682 | efip = xfs_efi_init(tp->t_mountp, count); |
| 683 | memcpy(efip->efi_format.efi_extents, extp, count * sizeof(*extp)); |
| 684 | atomic_set(&efip->efi_next_extent, count); |
| 685 | xfs_trans_add_item(tp, &efip->efi_item); |
| 686 | set_bit(XFS_LI_DIRTY, &efip->efi_item.li_flags); |
| 687 | return &efip->efi_item; |
| 688 | } |
| 689 | |
Darrick J. Wong | 10d0c6e | 2020-05-01 16:00:50 -0700 | [diff] [blame] | 690 | static const struct xfs_item_ops xfs_efi_item_ops = { |
| 691 | .iop_size = xfs_efi_item_size, |
| 692 | .iop_format = xfs_efi_item_format, |
| 693 | .iop_unpin = xfs_efi_item_unpin, |
| 694 | .iop_release = xfs_efi_item_release, |
| 695 | .iop_recover = xfs_efi_item_recover, |
Darrick J. Wong | 154c733 | 2020-05-01 16:00:54 -0700 | [diff] [blame] | 696 | .iop_match = xfs_efi_item_match, |
Darrick J. Wong | 4e919af | 2020-09-27 16:18:13 -0700 | [diff] [blame] | 697 | .iop_relog = xfs_efi_item_relog, |
Darrick J. Wong | 10d0c6e | 2020-05-01 16:00:50 -0700 | [diff] [blame] | 698 | }; |
| 699 | |
Darrick J. Wong | 9817aa8 | 2020-05-01 16:00:48 -0700 | [diff] [blame] | 700 | /* |
| 701 | * This routine is called to create an in-core extent free intent |
| 702 | * item from the efi format structure which was logged on disk. |
| 703 | * It allocates an in-core efi, copies the extents from the format |
| 704 | * structure into it, and adds the efi to the AIL with the given |
| 705 | * LSN. |
| 706 | */ |
| 707 | STATIC int |
| 708 | xlog_recover_efi_commit_pass2( |
| 709 | struct xlog *log, |
| 710 | struct list_head *buffer_list, |
| 711 | struct xlog_recover_item *item, |
| 712 | xfs_lsn_t lsn) |
| 713 | { |
| 714 | struct xfs_mount *mp = log->l_mp; |
| 715 | struct xfs_efi_log_item *efip; |
| 716 | struct xfs_efi_log_format *efi_formatp; |
| 717 | int error; |
| 718 | |
| 719 | efi_formatp = item->ri_buf[0].i_addr; |
| 720 | |
| 721 | efip = xfs_efi_init(mp, efi_formatp->efi_nextents); |
| 722 | error = xfs_efi_copy_format(&item->ri_buf[0], &efip->efi_format); |
| 723 | if (error) { |
| 724 | xfs_efi_item_free(efip); |
| 725 | return error; |
| 726 | } |
| 727 | atomic_set(&efip->efi_next_extent, efi_formatp->efi_nextents); |
Darrick J. Wong | 9817aa8 | 2020-05-01 16:00:48 -0700 | [diff] [blame] | 728 | /* |
Darrick J. Wong | 86a3717 | 2020-05-01 16:00:54 -0700 | [diff] [blame] | 729 | * Insert the intent into the AIL directly and drop one reference so |
| 730 | * that finishing or canceling the work will drop the other. |
Darrick J. Wong | 9817aa8 | 2020-05-01 16:00:48 -0700 | [diff] [blame] | 731 | */ |
Darrick J. Wong | 86a3717 | 2020-05-01 16:00:54 -0700 | [diff] [blame] | 732 | xfs_trans_ail_insert(log->l_ailp, &efip->efi_item, lsn); |
Darrick J. Wong | 9817aa8 | 2020-05-01 16:00:48 -0700 | [diff] [blame] | 733 | xfs_efi_release(efip); |
| 734 | return 0; |
| 735 | } |
| 736 | |
Darrick J. Wong | 86ffa47 | 2020-05-01 16:00:45 -0700 | [diff] [blame] | 737 | const struct xlog_recover_item_ops xlog_efi_item_ops = { |
| 738 | .item_type = XFS_LI_EFI, |
Darrick J. Wong | 9817aa8 | 2020-05-01 16:00:48 -0700 | [diff] [blame] | 739 | .commit_pass2 = xlog_recover_efi_commit_pass2, |
Darrick J. Wong | 86ffa47 | 2020-05-01 16:00:45 -0700 | [diff] [blame] | 740 | }; |
| 741 | |
Darrick J. Wong | 9817aa8 | 2020-05-01 16:00:48 -0700 | [diff] [blame] | 742 | /* |
| 743 | * This routine is called when an EFD format structure is found in a committed |
| 744 | * transaction in the log. Its purpose is to cancel the corresponding EFI if it |
| 745 | * was still in the log. To do this it searches the AIL for the EFI with an id |
| 746 | * equal to that in the EFD format structure. If we find it we drop the EFD |
| 747 | * reference, which removes the EFI from the AIL and frees it. |
| 748 | */ |
| 749 | STATIC int |
| 750 | xlog_recover_efd_commit_pass2( |
| 751 | struct xlog *log, |
| 752 | struct list_head *buffer_list, |
| 753 | struct xlog_recover_item *item, |
| 754 | xfs_lsn_t lsn) |
| 755 | { |
Darrick J. Wong | 9817aa8 | 2020-05-01 16:00:48 -0700 | [diff] [blame] | 756 | struct xfs_efd_log_format *efd_formatp; |
Darrick J. Wong | 9817aa8 | 2020-05-01 16:00:48 -0700 | [diff] [blame] | 757 | |
| 758 | efd_formatp = item->ri_buf[0].i_addr; |
| 759 | ASSERT((item->ri_buf[0].i_len == (sizeof(xfs_efd_log_format_32_t) + |
| 760 | ((efd_formatp->efd_nextents - 1) * sizeof(xfs_extent_32_t)))) || |
| 761 | (item->ri_buf[0].i_len == (sizeof(xfs_efd_log_format_64_t) + |
| 762 | ((efd_formatp->efd_nextents - 1) * sizeof(xfs_extent_64_t))))); |
Darrick J. Wong | 9817aa8 | 2020-05-01 16:00:48 -0700 | [diff] [blame] | 763 | |
Darrick J. Wong | 154c733 | 2020-05-01 16:00:54 -0700 | [diff] [blame] | 764 | xlog_recover_release_intent(log, XFS_LI_EFI, efd_formatp->efd_efi_id); |
Darrick J. Wong | 9817aa8 | 2020-05-01 16:00:48 -0700 | [diff] [blame] | 765 | return 0; |
| 766 | } |
| 767 | |
Darrick J. Wong | 86ffa47 | 2020-05-01 16:00:45 -0700 | [diff] [blame] | 768 | const struct xlog_recover_item_ops xlog_efd_item_ops = { |
| 769 | .item_type = XFS_LI_EFD, |
Darrick J. Wong | 9817aa8 | 2020-05-01 16:00:48 -0700 | [diff] [blame] | 770 | .commit_pass2 = xlog_recover_efd_commit_pass2, |
Darrick J. Wong | 86ffa47 | 2020-05-01 16:00:45 -0700 | [diff] [blame] | 771 | }; |