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 | dc42375 | 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" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | |
| 26 | kmem_zone_t *xfs_efi_zone; |
| 27 | kmem_zone_t *xfs_efd_zone; |
| 28 | |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 29 | static inline struct xfs_efi_log_item *EFI_ITEM(struct xfs_log_item *lip) |
| 30 | { |
| 31 | return container_of(lip, struct xfs_efi_log_item, efi_item); |
| 32 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
Christoph Hellwig | 7d795ca | 2005-06-21 15:41:19 +1000 | [diff] [blame] | 34 | void |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 35 | xfs_efi_item_free( |
| 36 | struct xfs_efi_log_item *efip) |
Christoph Hellwig | 7d795ca | 2005-06-21 15:41:19 +1000 | [diff] [blame] | 37 | { |
Dave Chinner | b1c5ebb | 2016-07-22 09:52:35 +1000 | [diff] [blame] | 38 | kmem_free(efip->efi_item.li_lv_shadow); |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 39 | if (efip->efi_format.efi_nextents > XFS_EFI_MAX_FAST_EXTENTS) |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 40 | kmem_free(efip); |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 41 | else |
Christoph Hellwig | 7d795ca | 2005-06-21 15:41:19 +1000 | [diff] [blame] | 42 | kmem_zone_free(xfs_efi_zone, efip); |
Christoph Hellwig | 7d795ca | 2005-06-21 15:41:19 +1000 | [diff] [blame] | 43 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
| 45 | /* |
Dave Chinner | 0612d11 | 2018-04-02 20:08:27 -0700 | [diff] [blame] | 46 | * Freeing the efi requires that we remove it from the AIL if it has already |
| 47 | * been placed there. However, the EFI may not yet have been placed in the AIL |
| 48 | * when called by xfs_efi_release() from EFD processing due to the ordering of |
| 49 | * committed vs unpin operations in bulk insert operations. Hence the reference |
| 50 | * count to ensure only the last caller frees the EFI. |
| 51 | */ |
| 52 | void |
| 53 | xfs_efi_release( |
| 54 | struct xfs_efi_log_item *efip) |
| 55 | { |
| 56 | ASSERT(atomic_read(&efip->efi_refcount) > 0); |
| 57 | if (atomic_dec_and_test(&efip->efi_refcount)) { |
| 58 | xfs_trans_ail_remove(&efip->efi_item, SHUTDOWN_LOG_IO_ERROR); |
| 59 | xfs_efi_item_free(efip); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | * This returns the number of iovecs needed to log the given efi item. |
| 65 | * We only need 1 iovec for an efi item. It just logs the efi_log_format |
| 66 | * structure. |
| 67 | */ |
Dave Chinner | 166d136 | 2013-08-12 20:50:04 +1000 | [diff] [blame] | 68 | static inline int |
| 69 | xfs_efi_item_sizeof( |
| 70 | struct xfs_efi_log_item *efip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | { |
Dave Chinner | 166d136 | 2013-08-12 20:50:04 +1000 | [diff] [blame] | 72 | return sizeof(struct xfs_efi_log_format) + |
| 73 | (efip->efi_format.efi_nextents - 1) * sizeof(xfs_extent_t); |
| 74 | } |
| 75 | |
| 76 | STATIC void |
| 77 | xfs_efi_item_size( |
| 78 | struct xfs_log_item *lip, |
| 79 | int *nvecs, |
| 80 | int *nbytes) |
| 81 | { |
| 82 | *nvecs += 1; |
| 83 | *nbytes += xfs_efi_item_sizeof(EFI_ITEM(lip)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /* |
| 87 | * This is called to fill in the vector of log iovecs for the |
| 88 | * given efi log item. We use only 1 iovec, and we point that |
| 89 | * at the efi_log_format structure embedded in the efi item. |
| 90 | * It is at this point that we assert that all of the extent |
| 91 | * slots in the efi item have been filled. |
| 92 | */ |
| 93 | STATIC void |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 94 | xfs_efi_item_format( |
| 95 | struct xfs_log_item *lip, |
Christoph Hellwig | bde7cff | 2013-12-13 11:34:02 +1100 | [diff] [blame] | 96 | struct xfs_log_vec *lv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | { |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 98 | struct xfs_efi_log_item *efip = EFI_ITEM(lip); |
Christoph Hellwig | bde7cff | 2013-12-13 11:34:02 +1100 | [diff] [blame] | 99 | struct xfs_log_iovec *vecp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
Dave Chinner | b199c8a | 2010-12-20 11:59:49 +1100 | [diff] [blame] | 101 | ASSERT(atomic_read(&efip->efi_next_extent) == |
| 102 | efip->efi_format.efi_nextents); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
| 104 | efip->efi_format.efi_type = XFS_LI_EFI; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | efip->efi_format.efi_size = 1; |
| 106 | |
Christoph Hellwig | bde7cff | 2013-12-13 11:34:02 +1100 | [diff] [blame] | 107 | xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFI_FORMAT, |
Christoph Hellwig | 1234351 | 2013-12-13 11:00:43 +1100 | [diff] [blame] | 108 | &efip->efi_format, |
| 109 | xfs_efi_item_sizeof(efip)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | |
| 113 | /* |
Brian Foster | 8d99fe9 | 2015-08-19 09:51:16 +1000 | [diff] [blame] | 114 | * The unpin operation is the last place an EFI is manipulated in the log. It is |
| 115 | * either inserted in the AIL or aborted in the event of a log I/O error. In |
| 116 | * either case, the EFI transaction has been successfully committed to make it |
| 117 | * this far. Therefore, we expect whoever committed the EFI to either construct |
| 118 | * and commit the EFD or drop the EFD's reference in the event of error. Simply |
| 119 | * 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] | 120 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | STATIC void |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 122 | xfs_efi_item_unpin( |
| 123 | struct xfs_log_item *lip, |
| 124 | int remove) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 126 | struct xfs_efi_log_item *efip = EFI_ITEM(lip); |
Brian Foster | 5e4b538 | 2015-08-19 09:50:12 +1000 | [diff] [blame] | 127 | xfs_efi_release(efip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /* |
Brian Foster | 8d99fe9 | 2015-08-19 09:51:16 +1000 | [diff] [blame] | 131 | * The EFI has been either committed or aborted if the transaction has been |
| 132 | * cancelled. If the transaction was cancelled, an EFD isn't going to be |
| 133 | * constructed and thus we free the EFI here directly. |
| 134 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | STATIC void |
Christoph Hellwig | ddf9205 | 2019-06-28 19:27:32 -0700 | [diff] [blame] | 136 | xfs_efi_item_release( |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 137 | struct xfs_log_item *lip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | { |
Christoph Hellwig | ddf9205 | 2019-06-28 19:27:32 -0700 | [diff] [blame] | 139 | xfs_efi_release(EFI_ITEM(lip)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Christoph Hellwig | 272e42b | 2011-10-28 09:54:24 +0000 | [diff] [blame] | 142 | static const struct xfs_item_ops xfs_efi_item_ops = { |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 143 | .iop_size = xfs_efi_item_size, |
| 144 | .iop_format = xfs_efi_item_format, |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 145 | .iop_unpin = xfs_efi_item_unpin, |
Christoph Hellwig | ddf9205 | 2019-06-28 19:27:32 -0700 | [diff] [blame] | 146 | .iop_release = xfs_efi_item_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | |
| 150 | /* |
| 151 | * Allocate and initialize an efi item with the given number of extents. |
| 152 | */ |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 153 | struct xfs_efi_log_item * |
| 154 | xfs_efi_init( |
| 155 | struct xfs_mount *mp, |
| 156 | uint nextents) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
| 158 | { |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 159 | struct xfs_efi_log_item *efip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | uint size; |
| 161 | |
| 162 | ASSERT(nextents > 0); |
| 163 | if (nextents > XFS_EFI_MAX_FAST_EXTENTS) { |
| 164 | size = (uint)(sizeof(xfs_efi_log_item_t) + |
| 165 | ((nextents - 1) * sizeof(xfs_extent_t))); |
Tetsuo Handa | 707e0dd | 2019-08-26 12:06:22 -0700 | [diff] [blame^] | 166 | efip = kmem_zalloc(size, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | } else { |
Tetsuo Handa | 707e0dd | 2019-08-26 12:06:22 -0700 | [diff] [blame^] | 168 | efip = kmem_zone_zalloc(xfs_efi_zone, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Dave Chinner | 43f5efc | 2010-03-23 10:10:00 +1100 | [diff] [blame] | 171 | 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] | 172 | efip->efi_format.efi_nextents = nextents; |
Christoph Hellwig | db9d67d | 2015-06-22 09:43:32 +1000 | [diff] [blame] | 173 | efip->efi_format.efi_id = (uintptr_t)(void *)efip; |
Dave Chinner | b199c8a | 2010-12-20 11:59:49 +1100 | [diff] [blame] | 174 | atomic_set(&efip->efi_next_extent, 0); |
Dave Chinner | 666d644 | 2013-04-03 14:09:21 +1100 | [diff] [blame] | 175 | atomic_set(&efip->efi_refcount, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 177 | return efip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /* |
Tim Shimmin | 6d192a9 | 2006-06-09 14:55:38 +1000 | [diff] [blame] | 181 | * Copy an EFI format buffer from the given buf, and into the destination |
| 182 | * EFI format structure. |
| 183 | * The given buffer can be in 32 bit or 64 bit form (which has different padding), |
| 184 | * one of which will be the native format for this kernel. |
| 185 | * It will handle the conversion of formats if necessary. |
| 186 | */ |
| 187 | int |
| 188 | xfs_efi_copy_format(xfs_log_iovec_t *buf, xfs_efi_log_format_t *dst_efi_fmt) |
| 189 | { |
Christoph Hellwig | 4e0d5f9 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 190 | xfs_efi_log_format_t *src_efi_fmt = buf->i_addr; |
Tim Shimmin | 6d192a9 | 2006-06-09 14:55:38 +1000 | [diff] [blame] | 191 | uint i; |
| 192 | uint len = sizeof(xfs_efi_log_format_t) + |
| 193 | (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_t); |
| 194 | uint len32 = sizeof(xfs_efi_log_format_32_t) + |
| 195 | (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_32_t); |
| 196 | uint len64 = sizeof(xfs_efi_log_format_64_t) + |
| 197 | (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_64_t); |
| 198 | |
| 199 | if (buf->i_len == len) { |
| 200 | memcpy((char *)dst_efi_fmt, (char*)src_efi_fmt, len); |
| 201 | return 0; |
| 202 | } else if (buf->i_len == len32) { |
Christoph Hellwig | 4e0d5f9 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 203 | 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] | 204 | |
| 205 | dst_efi_fmt->efi_type = src_efi_fmt_32->efi_type; |
| 206 | dst_efi_fmt->efi_size = src_efi_fmt_32->efi_size; |
| 207 | dst_efi_fmt->efi_nextents = src_efi_fmt_32->efi_nextents; |
| 208 | dst_efi_fmt->efi_id = src_efi_fmt_32->efi_id; |
| 209 | for (i = 0; i < dst_efi_fmt->efi_nextents; i++) { |
| 210 | dst_efi_fmt->efi_extents[i].ext_start = |
| 211 | src_efi_fmt_32->efi_extents[i].ext_start; |
| 212 | dst_efi_fmt->efi_extents[i].ext_len = |
| 213 | src_efi_fmt_32->efi_extents[i].ext_len; |
| 214 | } |
| 215 | return 0; |
| 216 | } else if (buf->i_len == len64) { |
Christoph Hellwig | 4e0d5f9 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 217 | 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] | 218 | |
| 219 | dst_efi_fmt->efi_type = src_efi_fmt_64->efi_type; |
| 220 | dst_efi_fmt->efi_size = src_efi_fmt_64->efi_size; |
| 221 | dst_efi_fmt->efi_nextents = src_efi_fmt_64->efi_nextents; |
| 222 | dst_efi_fmt->efi_id = src_efi_fmt_64->efi_id; |
| 223 | for (i = 0; i < dst_efi_fmt->efi_nextents; i++) { |
| 224 | dst_efi_fmt->efi_extents[i].ext_start = |
| 225 | src_efi_fmt_64->efi_extents[i].ext_start; |
| 226 | dst_efi_fmt->efi_extents[i].ext_len = |
| 227 | src_efi_fmt_64->efi_extents[i].ext_len; |
| 228 | } |
| 229 | return 0; |
| 230 | } |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 231 | return -EFSCORRUPTED; |
Tim Shimmin | 6d192a9 | 2006-06-09 14:55:38 +1000 | [diff] [blame] | 232 | } |
| 233 | |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 234 | 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] | 235 | { |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 236 | return container_of(lip, struct xfs_efd_log_item, efd_item); |
| 237 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 239 | STATIC void |
| 240 | xfs_efd_item_free(struct xfs_efd_log_item *efdp) |
| 241 | { |
Dave Chinner | b1c5ebb | 2016-07-22 09:52:35 +1000 | [diff] [blame] | 242 | kmem_free(efdp->efd_item.li_lv_shadow); |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 243 | if (efdp->efd_format.efd_nextents > XFS_EFD_MAX_FAST_EXTENTS) |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 244 | kmem_free(efdp); |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 245 | else |
Christoph Hellwig | 7d795ca | 2005-06-21 15:41:19 +1000 | [diff] [blame] | 246 | kmem_zone_free(xfs_efd_zone, efdp); |
Christoph Hellwig | 7d795ca | 2005-06-21 15:41:19 +1000 | [diff] [blame] | 247 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
| 249 | /* |
| 250 | * This returns the number of iovecs needed to log the given efd item. |
| 251 | * We only need 1 iovec for an efd item. It just logs the efd_log_format |
| 252 | * structure. |
| 253 | */ |
Dave Chinner | 166d136 | 2013-08-12 20:50:04 +1000 | [diff] [blame] | 254 | static inline int |
| 255 | xfs_efd_item_sizeof( |
| 256 | struct xfs_efd_log_item *efdp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | { |
Dave Chinner | 166d136 | 2013-08-12 20:50:04 +1000 | [diff] [blame] | 258 | return sizeof(xfs_efd_log_format_t) + |
| 259 | (efdp->efd_format.efd_nextents - 1) * sizeof(xfs_extent_t); |
| 260 | } |
| 261 | |
| 262 | STATIC void |
| 263 | xfs_efd_item_size( |
| 264 | struct xfs_log_item *lip, |
| 265 | int *nvecs, |
| 266 | int *nbytes) |
| 267 | { |
| 268 | *nvecs += 1; |
| 269 | *nbytes += xfs_efd_item_sizeof(EFD_ITEM(lip)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | /* |
| 273 | * This is called to fill in the vector of log iovecs for the |
| 274 | * given efd log item. We use only 1 iovec, and we point that |
| 275 | * at the efd_log_format structure embedded in the efd item. |
| 276 | * It is at this point that we assert that all of the extent |
| 277 | * slots in the efd item have been filled. |
| 278 | */ |
| 279 | STATIC void |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 280 | xfs_efd_item_format( |
| 281 | struct xfs_log_item *lip, |
Christoph Hellwig | bde7cff | 2013-12-13 11:34:02 +1100 | [diff] [blame] | 282 | struct xfs_log_vec *lv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | { |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 284 | struct xfs_efd_log_item *efdp = EFD_ITEM(lip); |
Christoph Hellwig | bde7cff | 2013-12-13 11:34:02 +1100 | [diff] [blame] | 285 | struct xfs_log_iovec *vecp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | |
| 287 | ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents); |
| 288 | |
| 289 | efdp->efd_format.efd_type = XFS_LI_EFD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | efdp->efd_format.efd_size = 1; |
| 291 | |
Christoph Hellwig | bde7cff | 2013-12-13 11:34:02 +1100 | [diff] [blame] | 292 | xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFD_FORMAT, |
Christoph Hellwig | 1234351 | 2013-12-13 11:00:43 +1100 | [diff] [blame] | 293 | &efdp->efd_format, |
| 294 | xfs_efd_item_sizeof(efdp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | /* |
Brian Foster | 8d99fe9 | 2015-08-19 09:51:16 +1000 | [diff] [blame] | 298 | * The EFD is either committed or aborted if the transaction is cancelled. If |
| 299 | * the transaction is cancelled, drop our reference to the EFI and free the EFD. |
| 300 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | STATIC void |
Christoph Hellwig | ddf9205 | 2019-06-28 19:27:32 -0700 | [diff] [blame] | 302 | xfs_efd_item_release( |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 303 | struct xfs_log_item *lip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | { |
Brian Foster | 8d99fe9 | 2015-08-19 09:51:16 +1000 | [diff] [blame] | 305 | struct xfs_efd_log_item *efdp = EFD_ITEM(lip); |
| 306 | |
Christoph Hellwig | ddf9205 | 2019-06-28 19:27:32 -0700 | [diff] [blame] | 307 | xfs_efi_release(efdp->efd_efip); |
| 308 | xfs_efd_item_free(efdp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Christoph Hellwig | 272e42b | 2011-10-28 09:54:24 +0000 | [diff] [blame] | 311 | static const struct xfs_item_ops xfs_efd_item_ops = { |
Christoph Hellwig | 9ce632a | 2019-06-28 19:27:32 -0700 | [diff] [blame] | 312 | .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED, |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 313 | .iop_size = xfs_efd_item_size, |
| 314 | .iop_format = xfs_efd_item_format, |
Christoph Hellwig | ddf9205 | 2019-06-28 19:27:32 -0700 | [diff] [blame] | 315 | .iop_release = xfs_efd_item_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | }; |
| 317 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | /* |
Christoph Hellwig | 9c5e7c2 | 2019-06-28 19:27:35 -0700 | [diff] [blame] | 319 | * Allocate an "extent free done" log item that will hold nextents worth of |
| 320 | * extents. The caller must use all nextents extents, because we are not |
| 321 | * flexible about this at all. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | */ |
Christoph Hellwig | 81f4004 | 2019-06-28 19:28:17 -0700 | [diff] [blame] | 323 | static struct xfs_efd_log_item * |
Christoph Hellwig | 9c5e7c2 | 2019-06-28 19:27:35 -0700 | [diff] [blame] | 324 | xfs_trans_get_efd( |
| 325 | struct xfs_trans *tp, |
| 326 | struct xfs_efi_log_item *efip, |
| 327 | unsigned int nextents) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | { |
Christoph Hellwig | 9c5e7c2 | 2019-06-28 19:27:35 -0700 | [diff] [blame] | 329 | struct xfs_efd_log_item *efdp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | |
| 331 | ASSERT(nextents > 0); |
Christoph Hellwig | 9c5e7c2 | 2019-06-28 19:27:35 -0700 | [diff] [blame] | 332 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | if (nextents > XFS_EFD_MAX_FAST_EXTENTS) { |
Christoph Hellwig | 9c5e7c2 | 2019-06-28 19:27:35 -0700 | [diff] [blame] | 334 | efdp = kmem_zalloc(sizeof(struct xfs_efd_log_item) + |
| 335 | (nextents - 1) * sizeof(struct xfs_extent), |
Tetsuo Handa | 707e0dd | 2019-08-26 12:06:22 -0700 | [diff] [blame^] | 336 | 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | } else { |
Tetsuo Handa | 707e0dd | 2019-08-26 12:06:22 -0700 | [diff] [blame^] | 338 | efdp = kmem_zone_zalloc(xfs_efd_zone, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Christoph Hellwig | 9c5e7c2 | 2019-06-28 19:27:35 -0700 | [diff] [blame] | 341 | xfs_log_item_init(tp->t_mountp, &efdp->efd_item, XFS_LI_EFD, |
| 342 | &xfs_efd_item_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | efdp->efd_efip = efip; |
| 344 | efdp->efd_format.efd_nextents = nextents; |
| 345 | efdp->efd_format.efd_efi_id = efip->efi_format.efi_id; |
| 346 | |
Christoph Hellwig | 9c5e7c2 | 2019-06-28 19:27:35 -0700 | [diff] [blame] | 347 | xfs_trans_add_item(tp, &efdp->efd_item); |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 348 | return efdp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | } |
Darrick J. Wong | dc42375 | 2016-08-03 11:23:49 +1000 | [diff] [blame] | 350 | |
| 351 | /* |
Christoph Hellwig | 81f4004 | 2019-06-28 19:28:17 -0700 | [diff] [blame] | 352 | * Free an extent and log it to the EFD. Note that the transaction is marked |
| 353 | * dirty regardless of whether the extent free succeeds or fails to support the |
| 354 | * EFI/EFD lifecycle rules. |
| 355 | */ |
| 356 | static int |
| 357 | xfs_trans_free_extent( |
| 358 | struct xfs_trans *tp, |
| 359 | struct xfs_efd_log_item *efdp, |
| 360 | xfs_fsblock_t start_block, |
| 361 | xfs_extlen_t ext_len, |
| 362 | const struct xfs_owner_info *oinfo, |
| 363 | bool skip_discard) |
| 364 | { |
| 365 | struct xfs_mount *mp = tp->t_mountp; |
| 366 | struct xfs_extent *extp; |
| 367 | uint next_extent; |
| 368 | xfs_agnumber_t agno = XFS_FSB_TO_AGNO(mp, start_block); |
| 369 | xfs_agblock_t agbno = XFS_FSB_TO_AGBNO(mp, |
| 370 | start_block); |
| 371 | int error; |
| 372 | |
| 373 | trace_xfs_bmap_free_deferred(tp->t_mountp, agno, 0, agbno, ext_len); |
| 374 | |
| 375 | error = __xfs_free_extent(tp, start_block, ext_len, |
| 376 | oinfo, XFS_AG_RESV_NONE, skip_discard); |
| 377 | /* |
| 378 | * Mark the transaction dirty, even on error. This ensures the |
| 379 | * transaction is aborted, which: |
| 380 | * |
| 381 | * 1.) releases the EFI and frees the EFD |
| 382 | * 2.) shuts down the filesystem |
| 383 | */ |
| 384 | tp->t_flags |= XFS_TRANS_DIRTY; |
| 385 | set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags); |
| 386 | |
| 387 | next_extent = efdp->efd_next_extent; |
| 388 | ASSERT(next_extent < efdp->efd_format.efd_nextents); |
| 389 | extp = &(efdp->efd_format.efd_extents[next_extent]); |
| 390 | extp->ext_start = start_block; |
| 391 | extp->ext_len = ext_len; |
| 392 | efdp->efd_next_extent++; |
| 393 | |
| 394 | return error; |
| 395 | } |
| 396 | |
| 397 | /* Sort bmap items by AG. */ |
| 398 | static int |
| 399 | xfs_extent_free_diff_items( |
| 400 | void *priv, |
| 401 | struct list_head *a, |
| 402 | struct list_head *b) |
| 403 | { |
| 404 | struct xfs_mount *mp = priv; |
| 405 | struct xfs_extent_free_item *ra; |
| 406 | struct xfs_extent_free_item *rb; |
| 407 | |
| 408 | ra = container_of(a, struct xfs_extent_free_item, xefi_list); |
| 409 | rb = container_of(b, struct xfs_extent_free_item, xefi_list); |
| 410 | return XFS_FSB_TO_AGNO(mp, ra->xefi_startblock) - |
| 411 | XFS_FSB_TO_AGNO(mp, rb->xefi_startblock); |
| 412 | } |
| 413 | |
| 414 | /* Get an EFI. */ |
| 415 | STATIC void * |
| 416 | xfs_extent_free_create_intent( |
| 417 | struct xfs_trans *tp, |
| 418 | unsigned int count) |
| 419 | { |
| 420 | struct xfs_efi_log_item *efip; |
| 421 | |
| 422 | ASSERT(tp != NULL); |
| 423 | ASSERT(count > 0); |
| 424 | |
| 425 | efip = xfs_efi_init(tp->t_mountp, count); |
| 426 | ASSERT(efip != NULL); |
| 427 | |
| 428 | /* |
| 429 | * Get a log_item_desc to point at the new item. |
| 430 | */ |
| 431 | xfs_trans_add_item(tp, &efip->efi_item); |
| 432 | return efip; |
| 433 | } |
| 434 | |
| 435 | /* Log a free extent to the intent item. */ |
| 436 | STATIC void |
| 437 | xfs_extent_free_log_item( |
| 438 | struct xfs_trans *tp, |
| 439 | void *intent, |
| 440 | struct list_head *item) |
| 441 | { |
| 442 | struct xfs_efi_log_item *efip = intent; |
| 443 | struct xfs_extent_free_item *free; |
| 444 | uint next_extent; |
| 445 | struct xfs_extent *extp; |
| 446 | |
| 447 | free = container_of(item, struct xfs_extent_free_item, xefi_list); |
| 448 | |
| 449 | tp->t_flags |= XFS_TRANS_DIRTY; |
| 450 | set_bit(XFS_LI_DIRTY, &efip->efi_item.li_flags); |
| 451 | |
| 452 | /* |
| 453 | * atomic_inc_return gives us the value after the increment; |
| 454 | * we want to use it as an array index so we need to subtract 1 from |
| 455 | * it. |
| 456 | */ |
| 457 | next_extent = atomic_inc_return(&efip->efi_next_extent) - 1; |
| 458 | ASSERT(next_extent < efip->efi_format.efi_nextents); |
| 459 | extp = &efip->efi_format.efi_extents[next_extent]; |
| 460 | extp->ext_start = free->xefi_startblock; |
| 461 | extp->ext_len = free->xefi_blockcount; |
| 462 | } |
| 463 | |
| 464 | /* Get an EFD so we can process all the free extents. */ |
| 465 | STATIC void * |
| 466 | xfs_extent_free_create_done( |
| 467 | struct xfs_trans *tp, |
| 468 | void *intent, |
| 469 | unsigned int count) |
| 470 | { |
| 471 | return xfs_trans_get_efd(tp, intent, count); |
| 472 | } |
| 473 | |
| 474 | /* Process a free extent. */ |
| 475 | STATIC int |
| 476 | xfs_extent_free_finish_item( |
| 477 | struct xfs_trans *tp, |
| 478 | struct list_head *item, |
| 479 | void *done_item, |
| 480 | void **state) |
| 481 | { |
| 482 | struct xfs_extent_free_item *free; |
| 483 | int error; |
| 484 | |
| 485 | free = container_of(item, struct xfs_extent_free_item, xefi_list); |
| 486 | error = xfs_trans_free_extent(tp, done_item, |
| 487 | free->xefi_startblock, |
| 488 | free->xefi_blockcount, |
| 489 | &free->xefi_oinfo, free->xefi_skip_discard); |
| 490 | kmem_free(free); |
| 491 | return error; |
| 492 | } |
| 493 | |
| 494 | /* Abort all pending EFIs. */ |
| 495 | STATIC void |
| 496 | xfs_extent_free_abort_intent( |
| 497 | void *intent) |
| 498 | { |
| 499 | xfs_efi_release(intent); |
| 500 | } |
| 501 | |
| 502 | /* Cancel a free extent. */ |
| 503 | STATIC void |
| 504 | xfs_extent_free_cancel_item( |
| 505 | struct list_head *item) |
| 506 | { |
| 507 | struct xfs_extent_free_item *free; |
| 508 | |
| 509 | free = container_of(item, struct xfs_extent_free_item, xefi_list); |
| 510 | kmem_free(free); |
| 511 | } |
| 512 | |
| 513 | const struct xfs_defer_op_type xfs_extent_free_defer_type = { |
| 514 | .max_items = XFS_EFI_MAX_FAST_EXTENTS, |
| 515 | .diff_items = xfs_extent_free_diff_items, |
| 516 | .create_intent = xfs_extent_free_create_intent, |
| 517 | .abort_intent = xfs_extent_free_abort_intent, |
| 518 | .log_item = xfs_extent_free_log_item, |
| 519 | .create_done = xfs_extent_free_create_done, |
| 520 | .finish_item = xfs_extent_free_finish_item, |
| 521 | .cancel_item = xfs_extent_free_cancel_item, |
| 522 | }; |
| 523 | |
| 524 | /* |
| 525 | * AGFL blocks are accounted differently in the reserve pools and are not |
| 526 | * inserted into the busy extent list. |
| 527 | */ |
| 528 | STATIC int |
| 529 | xfs_agfl_free_finish_item( |
| 530 | struct xfs_trans *tp, |
| 531 | struct list_head *item, |
| 532 | void *done_item, |
| 533 | void **state) |
| 534 | { |
| 535 | struct xfs_mount *mp = tp->t_mountp; |
| 536 | struct xfs_efd_log_item *efdp = done_item; |
| 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); |
| 549 | |
| 550 | trace_xfs_agfl_free_deferred(mp, agno, 0, agbno, free->xefi_blockcount); |
| 551 | |
| 552 | error = xfs_alloc_read_agf(mp, tp, agno, 0, &agbp); |
| 553 | if (!error) |
| 554 | error = xfs_free_agfl_block(tp, agno, agbno, agbp, |
| 555 | &free->xefi_oinfo); |
| 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 | |
| 574 | kmem_free(free); |
| 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, |
| 581 | .diff_items = xfs_extent_free_diff_items, |
| 582 | .create_intent = xfs_extent_free_create_intent, |
| 583 | .abort_intent = xfs_extent_free_abort_intent, |
| 584 | .log_item = xfs_extent_free_log_item, |
| 585 | .create_done = xfs_extent_free_create_done, |
| 586 | .finish_item = xfs_agfl_free_finish_item, |
| 587 | .cancel_item = xfs_extent_free_cancel_item, |
| 588 | }; |
| 589 | |
| 590 | /* |
Darrick J. Wong | dc42375 | 2016-08-03 11:23:49 +1000 | [diff] [blame] | 591 | * Process an extent free intent item that was recovered from |
| 592 | * the log. We need to free the extents that it describes. |
| 593 | */ |
| 594 | int |
| 595 | xfs_efi_recover( |
| 596 | struct xfs_mount *mp, |
| 597 | struct xfs_efi_log_item *efip) |
| 598 | { |
| 599 | struct xfs_efd_log_item *efdp; |
| 600 | struct xfs_trans *tp; |
| 601 | int i; |
| 602 | int error = 0; |
| 603 | xfs_extent_t *extp; |
| 604 | xfs_fsblock_t startblock_fsb; |
| 605 | |
| 606 | ASSERT(!test_bit(XFS_EFI_RECOVERED, &efip->efi_flags)); |
| 607 | |
| 608 | /* |
| 609 | * First check the validity of the extents described by the |
| 610 | * EFI. If any are bad, then assume that all are bad and |
| 611 | * just toss the EFI. |
| 612 | */ |
| 613 | for (i = 0; i < efip->efi_format.efi_nextents; i++) { |
Darrick J. Wong | e127faf | 2016-08-03 12:29:32 +1000 | [diff] [blame] | 614 | extp = &efip->efi_format.efi_extents[i]; |
Darrick J. Wong | dc42375 | 2016-08-03 11:23:49 +1000 | [diff] [blame] | 615 | startblock_fsb = XFS_BB_TO_FSB(mp, |
| 616 | XFS_FSB_TO_DADDR(mp, extp->ext_start)); |
Darrick J. Wong | e127faf | 2016-08-03 12:29:32 +1000 | [diff] [blame] | 617 | if (startblock_fsb == 0 || |
| 618 | extp->ext_len == 0 || |
| 619 | startblock_fsb >= mp->m_sb.sb_dblocks || |
| 620 | extp->ext_len >= mp->m_sb.sb_agblocks) { |
Darrick J. Wong | dc42375 | 2016-08-03 11:23:49 +1000 | [diff] [blame] | 621 | /* |
| 622 | * This will pull the EFI from the AIL and |
| 623 | * free the memory associated with it. |
| 624 | */ |
| 625 | set_bit(XFS_EFI_RECOVERED, &efip->efi_flags); |
| 626 | xfs_efi_release(efip); |
| 627 | return -EIO; |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp); |
| 632 | if (error) |
| 633 | return error; |
| 634 | efdp = xfs_trans_get_efd(tp, efip, efip->efi_format.efi_nextents); |
| 635 | |
| 636 | for (i = 0; i < efip->efi_format.efi_nextents; i++) { |
Darrick J. Wong | e127faf | 2016-08-03 12:29:32 +1000 | [diff] [blame] | 637 | extp = &efip->efi_format.efi_extents[i]; |
Darrick J. Wong | dc42375 | 2016-08-03 11:23:49 +1000 | [diff] [blame] | 638 | error = xfs_trans_free_extent(tp, efdp, extp->ext_start, |
Darrick J. Wong | 7280fed | 2018-12-12 08:46:23 -0800 | [diff] [blame] | 639 | extp->ext_len, |
| 640 | &XFS_RMAP_OINFO_ANY_OWNER, false); |
Darrick J. Wong | dc42375 | 2016-08-03 11:23:49 +1000 | [diff] [blame] | 641 | if (error) |
| 642 | goto abort_error; |
| 643 | |
| 644 | } |
| 645 | |
| 646 | set_bit(XFS_EFI_RECOVERED, &efip->efi_flags); |
| 647 | error = xfs_trans_commit(tp); |
| 648 | return error; |
| 649 | |
| 650 | abort_error: |
| 651 | xfs_trans_cancel(tp); |
| 652 | return error; |
| 653 | } |