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" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include "xfs_mount.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 13 | #include "xfs_trans.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include "xfs_trans_priv.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 15 | #include "xfs_buf_item.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include "xfs_extfree_item.h" |
Christoph Hellwig | 1234351 | 2013-12-13 11:00:43 +1100 | [diff] [blame] | 17 | #include "xfs_log.h" |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 18 | #include "xfs_btree.h" |
| 19 | #include "xfs_rmap.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
| 21 | |
| 22 | kmem_zone_t *xfs_efi_zone; |
| 23 | kmem_zone_t *xfs_efd_zone; |
| 24 | |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 25 | static inline struct xfs_efi_log_item *EFI_ITEM(struct xfs_log_item *lip) |
| 26 | { |
| 27 | return container_of(lip, struct xfs_efi_log_item, efi_item); |
| 28 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
Christoph Hellwig | 7d795ca | 2005-06-21 15:41:19 +1000 | [diff] [blame] | 30 | void |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 31 | xfs_efi_item_free( |
| 32 | struct xfs_efi_log_item *efip) |
Christoph Hellwig | 7d795ca | 2005-06-21 15:41:19 +1000 | [diff] [blame] | 33 | { |
Dave Chinner | b1c5ebb | 2016-07-22 09:52:35 +1000 | [diff] [blame] | 34 | kmem_free(efip->efi_item.li_lv_shadow); |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 35 | if (efip->efi_format.efi_nextents > XFS_EFI_MAX_FAST_EXTENTS) |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 36 | kmem_free(efip); |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 37 | else |
Christoph Hellwig | 7d795ca | 2005-06-21 15:41:19 +1000 | [diff] [blame] | 38 | kmem_zone_free(xfs_efi_zone, efip); |
Christoph Hellwig | 7d795ca | 2005-06-21 15:41:19 +1000 | [diff] [blame] | 39 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
| 41 | /* |
Dave Chinner | 0612d11 | 2018-04-02 20:08:27 -0700 | [diff] [blame] | 42 | * Freeing the efi requires that we remove it from the AIL if it has already |
| 43 | * been placed there. However, the EFI may not yet have been placed in the AIL |
| 44 | * when called by xfs_efi_release() from EFD processing due to the ordering of |
| 45 | * committed vs unpin operations in bulk insert operations. Hence the reference |
| 46 | * count to ensure only the last caller frees the EFI. |
| 47 | */ |
| 48 | void |
| 49 | xfs_efi_release( |
| 50 | struct xfs_efi_log_item *efip) |
| 51 | { |
| 52 | ASSERT(atomic_read(&efip->efi_refcount) > 0); |
| 53 | if (atomic_dec_and_test(&efip->efi_refcount)) { |
| 54 | xfs_trans_ail_remove(&efip->efi_item, SHUTDOWN_LOG_IO_ERROR); |
| 55 | xfs_efi_item_free(efip); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | * This returns the number of iovecs needed to log the given efi item. |
| 61 | * We only need 1 iovec for an efi item. It just logs the efi_log_format |
| 62 | * structure. |
| 63 | */ |
Dave Chinner | 166d136 | 2013-08-12 20:50:04 +1000 | [diff] [blame] | 64 | static inline int |
| 65 | xfs_efi_item_sizeof( |
| 66 | struct xfs_efi_log_item *efip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | { |
Dave Chinner | 166d136 | 2013-08-12 20:50:04 +1000 | [diff] [blame] | 68 | return sizeof(struct xfs_efi_log_format) + |
| 69 | (efip->efi_format.efi_nextents - 1) * sizeof(xfs_extent_t); |
| 70 | } |
| 71 | |
| 72 | STATIC void |
| 73 | xfs_efi_item_size( |
| 74 | struct xfs_log_item *lip, |
| 75 | int *nvecs, |
| 76 | int *nbytes) |
| 77 | { |
| 78 | *nvecs += 1; |
| 79 | *nbytes += xfs_efi_item_sizeof(EFI_ITEM(lip)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /* |
| 83 | * This is called to fill in the vector of log iovecs for the |
| 84 | * given efi log item. We use only 1 iovec, and we point that |
| 85 | * at the efi_log_format structure embedded in the efi item. |
| 86 | * It is at this point that we assert that all of the extent |
| 87 | * slots in the efi item have been filled. |
| 88 | */ |
| 89 | STATIC void |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 90 | xfs_efi_item_format( |
| 91 | struct xfs_log_item *lip, |
Christoph Hellwig | bde7cff | 2013-12-13 11:34:02 +1100 | [diff] [blame] | 92 | struct xfs_log_vec *lv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | { |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 94 | struct xfs_efi_log_item *efip = EFI_ITEM(lip); |
Christoph Hellwig | bde7cff | 2013-12-13 11:34:02 +1100 | [diff] [blame] | 95 | struct xfs_log_iovec *vecp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
Dave Chinner | b199c8a | 2010-12-20 11:59:49 +1100 | [diff] [blame] | 97 | ASSERT(atomic_read(&efip->efi_next_extent) == |
| 98 | efip->efi_format.efi_nextents); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
| 100 | efip->efi_format.efi_type = XFS_LI_EFI; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | efip->efi_format.efi_size = 1; |
| 102 | |
Christoph Hellwig | bde7cff | 2013-12-13 11:34:02 +1100 | [diff] [blame] | 103 | xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFI_FORMAT, |
Christoph Hellwig | 1234351 | 2013-12-13 11:00:43 +1100 | [diff] [blame] | 104 | &efip->efi_format, |
| 105 | xfs_efi_item_sizeof(efip)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | |
| 109 | /* |
| 110 | * Pinning has no meaning for an efi item, so just return. |
| 111 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | STATIC void |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 113 | xfs_efi_item_pin( |
| 114 | struct xfs_log_item *lip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | /* |
Brian Foster | 8d99fe9 | 2015-08-19 09:51:16 +1000 | [diff] [blame] | 119 | * The unpin operation is the last place an EFI is manipulated in the log. It is |
| 120 | * either inserted in the AIL or aborted in the event of a log I/O error. In |
| 121 | * either case, the EFI transaction has been successfully committed to make it |
| 122 | * this far. Therefore, we expect whoever committed the EFI to either construct |
| 123 | * and commit the EFD or drop the EFD's reference in the event of error. Simply |
| 124 | * 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] | 125 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | STATIC void |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 127 | xfs_efi_item_unpin( |
| 128 | struct xfs_log_item *lip, |
| 129 | int remove) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | { |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 131 | struct xfs_efi_log_item *efip = EFI_ITEM(lip); |
Brian Foster | 5e4b538 | 2015-08-19 09:50:12 +1000 | [diff] [blame] | 132 | xfs_efi_release(efip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /* |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 136 | * Efi items have no locking or pushing. However, since EFIs are pulled from |
| 137 | * the AIL when their corresponding EFDs are committed to disk, their situation |
| 138 | * is very similar to being pinned. Return XFS_ITEM_PINNED so that the caller |
| 139 | * will eventually flush the log. This should help in getting the EFI out of |
| 140 | * the AIL. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | STATIC uint |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 143 | xfs_efi_item_push( |
| 144 | struct xfs_log_item *lip, |
| 145 | struct list_head *buffer_list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | { |
| 147 | return XFS_ITEM_PINNED; |
| 148 | } |
| 149 | |
Brian Foster | 8d99fe9 | 2015-08-19 09:51:16 +1000 | [diff] [blame] | 150 | /* |
| 151 | * The EFI has been either committed or aborted if the transaction has been |
| 152 | * cancelled. If the transaction was cancelled, an EFD isn't going to be |
| 153 | * constructed and thus we free the EFI here directly. |
| 154 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | STATIC void |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 156 | xfs_efi_item_unlock( |
| 157 | struct xfs_log_item *lip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | { |
Dave Chinner | 22525c1 | 2018-05-09 07:47:34 -0700 | [diff] [blame] | 159 | if (test_bit(XFS_LI_ABORTED, &lip->li_flags)) |
Dave Chinner | 0612d11 | 2018-04-02 20:08:27 -0700 | [diff] [blame] | 160 | xfs_efi_release(EFI_ITEM(lip)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | /* |
Dave Chinner | b199c8a | 2010-12-20 11:59:49 +1100 | [diff] [blame] | 164 | * The EFI is logged only once and cannot be moved in the log, so simply return |
Dave Chinner | 666d644 | 2013-04-03 14:09:21 +1100 | [diff] [blame] | 165 | * the lsn at which it's been logged. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | STATIC xfs_lsn_t |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 168 | xfs_efi_item_committed( |
| 169 | struct xfs_log_item *lip, |
| 170 | xfs_lsn_t lsn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | { |
| 172 | return lsn; |
| 173 | } |
| 174 | |
| 175 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | * The EFI dependency tracking op doesn't do squat. It can't because |
| 177 | * it doesn't know where the free extent is coming from. The dependency |
| 178 | * tracking has to be handled by the "enclosing" metadata object. For |
| 179 | * example, for inodes, the inode is locked throughout the extent freeing |
| 180 | * so the dependency should be recorded there. |
| 181 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | STATIC void |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 183 | xfs_efi_item_committing( |
| 184 | struct xfs_log_item *lip, |
| 185 | xfs_lsn_t lsn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /* |
| 190 | * This is the ops vector shared by all efi log items. |
| 191 | */ |
Christoph Hellwig | 272e42b | 2011-10-28 09:54:24 +0000 | [diff] [blame] | 192 | static const struct xfs_item_ops xfs_efi_item_ops = { |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 193 | .iop_size = xfs_efi_item_size, |
| 194 | .iop_format = xfs_efi_item_format, |
| 195 | .iop_pin = xfs_efi_item_pin, |
| 196 | .iop_unpin = xfs_efi_item_unpin, |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 197 | .iop_unlock = xfs_efi_item_unlock, |
| 198 | .iop_committed = xfs_efi_item_committed, |
| 199 | .iop_push = xfs_efi_item_push, |
| 200 | .iop_committing = xfs_efi_item_committing |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | }; |
| 202 | |
| 203 | |
| 204 | /* |
| 205 | * Allocate and initialize an efi item with the given number of extents. |
| 206 | */ |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 207 | struct xfs_efi_log_item * |
| 208 | xfs_efi_init( |
| 209 | struct xfs_mount *mp, |
| 210 | uint nextents) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
| 212 | { |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 213 | struct xfs_efi_log_item *efip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | uint size; |
| 215 | |
| 216 | ASSERT(nextents > 0); |
| 217 | if (nextents > XFS_EFI_MAX_FAST_EXTENTS) { |
| 218 | size = (uint)(sizeof(xfs_efi_log_item_t) + |
| 219 | ((nextents - 1) * sizeof(xfs_extent_t))); |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 220 | efip = kmem_zalloc(size, KM_SLEEP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | } else { |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 222 | efip = kmem_zone_zalloc(xfs_efi_zone, KM_SLEEP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Dave Chinner | 43f5efc | 2010-03-23 10:10:00 +1100 | [diff] [blame] | 225 | 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] | 226 | efip->efi_format.efi_nextents = nextents; |
Christoph Hellwig | db9d67d | 2015-06-22 09:43:32 +1000 | [diff] [blame] | 227 | efip->efi_format.efi_id = (uintptr_t)(void *)efip; |
Dave Chinner | b199c8a | 2010-12-20 11:59:49 +1100 | [diff] [blame] | 228 | atomic_set(&efip->efi_next_extent, 0); |
Dave Chinner | 666d644 | 2013-04-03 14:09:21 +1100 | [diff] [blame] | 229 | atomic_set(&efip->efi_refcount, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 231 | return efip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /* |
Tim Shimmin | 6d192a9 | 2006-06-09 14:55:38 +1000 | [diff] [blame] | 235 | * Copy an EFI format buffer from the given buf, and into the destination |
| 236 | * EFI format structure. |
| 237 | * The given buffer can be in 32 bit or 64 bit form (which has different padding), |
| 238 | * one of which will be the native format for this kernel. |
| 239 | * It will handle the conversion of formats if necessary. |
| 240 | */ |
| 241 | int |
| 242 | xfs_efi_copy_format(xfs_log_iovec_t *buf, xfs_efi_log_format_t *dst_efi_fmt) |
| 243 | { |
Christoph Hellwig | 4e0d5f9 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 244 | xfs_efi_log_format_t *src_efi_fmt = buf->i_addr; |
Tim Shimmin | 6d192a9 | 2006-06-09 14:55:38 +1000 | [diff] [blame] | 245 | uint i; |
| 246 | uint len = sizeof(xfs_efi_log_format_t) + |
| 247 | (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_t); |
| 248 | uint len32 = sizeof(xfs_efi_log_format_32_t) + |
| 249 | (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_32_t); |
| 250 | uint len64 = sizeof(xfs_efi_log_format_64_t) + |
| 251 | (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_64_t); |
| 252 | |
| 253 | if (buf->i_len == len) { |
| 254 | memcpy((char *)dst_efi_fmt, (char*)src_efi_fmt, len); |
| 255 | return 0; |
| 256 | } else if (buf->i_len == len32) { |
Christoph Hellwig | 4e0d5f9 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 257 | 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] | 258 | |
| 259 | dst_efi_fmt->efi_type = src_efi_fmt_32->efi_type; |
| 260 | dst_efi_fmt->efi_size = src_efi_fmt_32->efi_size; |
| 261 | dst_efi_fmt->efi_nextents = src_efi_fmt_32->efi_nextents; |
| 262 | dst_efi_fmt->efi_id = src_efi_fmt_32->efi_id; |
| 263 | for (i = 0; i < dst_efi_fmt->efi_nextents; i++) { |
| 264 | dst_efi_fmt->efi_extents[i].ext_start = |
| 265 | src_efi_fmt_32->efi_extents[i].ext_start; |
| 266 | dst_efi_fmt->efi_extents[i].ext_len = |
| 267 | src_efi_fmt_32->efi_extents[i].ext_len; |
| 268 | } |
| 269 | return 0; |
| 270 | } else if (buf->i_len == len64) { |
Christoph Hellwig | 4e0d5f9 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 271 | 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] | 272 | |
| 273 | dst_efi_fmt->efi_type = src_efi_fmt_64->efi_type; |
| 274 | dst_efi_fmt->efi_size = src_efi_fmt_64->efi_size; |
| 275 | dst_efi_fmt->efi_nextents = src_efi_fmt_64->efi_nextents; |
| 276 | dst_efi_fmt->efi_id = src_efi_fmt_64->efi_id; |
| 277 | for (i = 0; i < dst_efi_fmt->efi_nextents; i++) { |
| 278 | dst_efi_fmt->efi_extents[i].ext_start = |
| 279 | src_efi_fmt_64->efi_extents[i].ext_start; |
| 280 | dst_efi_fmt->efi_extents[i].ext_len = |
| 281 | src_efi_fmt_64->efi_extents[i].ext_len; |
| 282 | } |
| 283 | return 0; |
| 284 | } |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 285 | return -EFSCORRUPTED; |
Tim Shimmin | 6d192a9 | 2006-06-09 14:55:38 +1000 | [diff] [blame] | 286 | } |
| 287 | |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 288 | 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] | 289 | { |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 290 | return container_of(lip, struct xfs_efd_log_item, efd_item); |
| 291 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 293 | STATIC void |
| 294 | xfs_efd_item_free(struct xfs_efd_log_item *efdp) |
| 295 | { |
Dave Chinner | b1c5ebb | 2016-07-22 09:52:35 +1000 | [diff] [blame] | 296 | kmem_free(efdp->efd_item.li_lv_shadow); |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 297 | if (efdp->efd_format.efd_nextents > XFS_EFD_MAX_FAST_EXTENTS) |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 298 | kmem_free(efdp); |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 299 | else |
Christoph Hellwig | 7d795ca | 2005-06-21 15:41:19 +1000 | [diff] [blame] | 300 | kmem_zone_free(xfs_efd_zone, efdp); |
Christoph Hellwig | 7d795ca | 2005-06-21 15:41:19 +1000 | [diff] [blame] | 301 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | |
| 303 | /* |
| 304 | * This returns the number of iovecs needed to log the given efd item. |
| 305 | * We only need 1 iovec for an efd item. It just logs the efd_log_format |
| 306 | * structure. |
| 307 | */ |
Dave Chinner | 166d136 | 2013-08-12 20:50:04 +1000 | [diff] [blame] | 308 | static inline int |
| 309 | xfs_efd_item_sizeof( |
| 310 | struct xfs_efd_log_item *efdp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | { |
Dave Chinner | 166d136 | 2013-08-12 20:50:04 +1000 | [diff] [blame] | 312 | return sizeof(xfs_efd_log_format_t) + |
| 313 | (efdp->efd_format.efd_nextents - 1) * sizeof(xfs_extent_t); |
| 314 | } |
| 315 | |
| 316 | STATIC void |
| 317 | xfs_efd_item_size( |
| 318 | struct xfs_log_item *lip, |
| 319 | int *nvecs, |
| 320 | int *nbytes) |
| 321 | { |
| 322 | *nvecs += 1; |
| 323 | *nbytes += xfs_efd_item_sizeof(EFD_ITEM(lip)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | /* |
| 327 | * This is called to fill in the vector of log iovecs for the |
| 328 | * given efd log item. We use only 1 iovec, and we point that |
| 329 | * at the efd_log_format structure embedded in the efd item. |
| 330 | * It is at this point that we assert that all of the extent |
| 331 | * slots in the efd item have been filled. |
| 332 | */ |
| 333 | STATIC void |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 334 | xfs_efd_item_format( |
| 335 | struct xfs_log_item *lip, |
Christoph Hellwig | bde7cff | 2013-12-13 11:34:02 +1100 | [diff] [blame] | 336 | struct xfs_log_vec *lv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | { |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 338 | struct xfs_efd_log_item *efdp = EFD_ITEM(lip); |
Christoph Hellwig | bde7cff | 2013-12-13 11:34:02 +1100 | [diff] [blame] | 339 | struct xfs_log_iovec *vecp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | |
| 341 | ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents); |
| 342 | |
| 343 | efdp->efd_format.efd_type = XFS_LI_EFD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | efdp->efd_format.efd_size = 1; |
| 345 | |
Christoph Hellwig | bde7cff | 2013-12-13 11:34:02 +1100 | [diff] [blame] | 346 | xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFD_FORMAT, |
Christoph Hellwig | 1234351 | 2013-12-13 11:00:43 +1100 | [diff] [blame] | 347 | &efdp->efd_format, |
| 348 | xfs_efd_item_sizeof(efdp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | /* |
| 352 | * Pinning has no meaning for an efd item, so just return. |
| 353 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | STATIC void |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 355 | xfs_efd_item_pin( |
| 356 | struct xfs_log_item *lip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | /* |
| 361 | * Since pinning has no meaning for an efd item, unpinning does |
| 362 | * not either. |
| 363 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | STATIC void |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 365 | xfs_efd_item_unpin( |
| 366 | struct xfs_log_item *lip, |
| 367 | int remove) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | /* |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 372 | * There isn't much you can do to push on an efd item. It is simply stuck |
| 373 | * waiting for the log to be flushed to disk. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | STATIC uint |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 376 | xfs_efd_item_push( |
| 377 | struct xfs_log_item *lip, |
| 378 | struct list_head *buffer_list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | { |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 380 | return XFS_ITEM_PINNED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Brian Foster | 8d99fe9 | 2015-08-19 09:51:16 +1000 | [diff] [blame] | 383 | /* |
| 384 | * The EFD is either committed or aborted if the transaction is cancelled. If |
| 385 | * the transaction is cancelled, drop our reference to the EFI and free the EFD. |
| 386 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | STATIC void |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 388 | xfs_efd_item_unlock( |
| 389 | struct xfs_log_item *lip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | { |
Brian Foster | 8d99fe9 | 2015-08-19 09:51:16 +1000 | [diff] [blame] | 391 | struct xfs_efd_log_item *efdp = EFD_ITEM(lip); |
| 392 | |
Dave Chinner | 22525c1 | 2018-05-09 07:47:34 -0700 | [diff] [blame] | 393 | if (test_bit(XFS_LI_ABORTED, &lip->li_flags)) { |
Brian Foster | 8d99fe9 | 2015-08-19 09:51:16 +1000 | [diff] [blame] | 394 | xfs_efi_release(efdp->efd_efip); |
| 395 | xfs_efd_item_free(efdp); |
| 396 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | /* |
Brian Foster | 8d99fe9 | 2015-08-19 09:51:16 +1000 | [diff] [blame] | 400 | * When the efd item is committed to disk, all we need to do is delete our |
| 401 | * reference to our partner efi item and then free ourselves. Since we're |
| 402 | * freeing ourselves we must return -1 to keep the transaction code from further |
| 403 | * referencing this item. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | STATIC xfs_lsn_t |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 406 | xfs_efd_item_committed( |
| 407 | struct xfs_log_item *lip, |
| 408 | xfs_lsn_t lsn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | { |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 410 | struct xfs_efd_log_item *efdp = EFD_ITEM(lip); |
| 411 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | /* |
Brian Foster | 8d99fe9 | 2015-08-19 09:51:16 +1000 | [diff] [blame] | 413 | * Drop the EFI reference regardless of whether the EFD has been |
| 414 | * aborted. Once the EFD transaction is constructed, it is the sole |
| 415 | * responsibility of the EFD to release the EFI (even if the EFI is |
| 416 | * aborted due to log I/O error). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | */ |
Brian Foster | 8d99fe9 | 2015-08-19 09:51:16 +1000 | [diff] [blame] | 418 | xfs_efi_release(efdp->efd_efip); |
Christoph Hellwig | 7d795ca | 2005-06-21 15:41:19 +1000 | [diff] [blame] | 419 | xfs_efd_item_free(efdp); |
Brian Foster | 8d99fe9 | 2015-08-19 09:51:16 +1000 | [diff] [blame] | 420 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | return (xfs_lsn_t)-1; |
| 422 | } |
| 423 | |
| 424 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | * The EFD dependency tracking op doesn't do squat. It can't because |
| 426 | * it doesn't know where the free extent is coming from. The dependency |
| 427 | * tracking has to be handled by the "enclosing" metadata object. For |
| 428 | * example, for inodes, the inode is locked throughout the extent freeing |
| 429 | * so the dependency should be recorded there. |
| 430 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | STATIC void |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 432 | xfs_efd_item_committing( |
| 433 | struct xfs_log_item *lip, |
| 434 | xfs_lsn_t lsn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | /* |
| 439 | * This is the ops vector shared by all efd log items. |
| 440 | */ |
Christoph Hellwig | 272e42b | 2011-10-28 09:54:24 +0000 | [diff] [blame] | 441 | static const struct xfs_item_ops xfs_efd_item_ops = { |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 442 | .iop_size = xfs_efd_item_size, |
| 443 | .iop_format = xfs_efd_item_format, |
| 444 | .iop_pin = xfs_efd_item_pin, |
| 445 | .iop_unpin = xfs_efd_item_unpin, |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 446 | .iop_unlock = xfs_efd_item_unlock, |
| 447 | .iop_committed = xfs_efd_item_committed, |
| 448 | .iop_push = xfs_efd_item_push, |
| 449 | .iop_committing = xfs_efd_item_committing |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | }; |
| 451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | /* |
| 453 | * Allocate and initialize an efd item with the given number of extents. |
| 454 | */ |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 455 | struct xfs_efd_log_item * |
| 456 | xfs_efd_init( |
| 457 | struct xfs_mount *mp, |
| 458 | struct xfs_efi_log_item *efip, |
| 459 | uint nextents) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | |
| 461 | { |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 462 | struct xfs_efd_log_item *efdp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | uint size; |
| 464 | |
| 465 | ASSERT(nextents > 0); |
| 466 | if (nextents > XFS_EFD_MAX_FAST_EXTENTS) { |
| 467 | size = (uint)(sizeof(xfs_efd_log_item_t) + |
| 468 | ((nextents - 1) * sizeof(xfs_extent_t))); |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 469 | efdp = kmem_zalloc(size, KM_SLEEP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | } else { |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 471 | efdp = kmem_zone_zalloc(xfs_efd_zone, KM_SLEEP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Dave Chinner | 43f5efc | 2010-03-23 10:10:00 +1100 | [diff] [blame] | 474 | xfs_log_item_init(mp, &efdp->efd_item, XFS_LI_EFD, &xfs_efd_item_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | efdp->efd_efip = efip; |
| 476 | efdp->efd_format.efd_nextents = nextents; |
| 477 | efdp->efd_format.efd_efi_id = efip->efi_format.efi_id; |
| 478 | |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 479 | return efdp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | } |
Darrick J. Wong | dc42375 | 2016-08-03 11:23:49 +1000 | [diff] [blame] | 481 | |
| 482 | /* |
| 483 | * Process an extent free intent item that was recovered from |
| 484 | * the log. We need to free the extents that it describes. |
| 485 | */ |
| 486 | int |
| 487 | xfs_efi_recover( |
| 488 | struct xfs_mount *mp, |
| 489 | struct xfs_efi_log_item *efip) |
| 490 | { |
| 491 | struct xfs_efd_log_item *efdp; |
| 492 | struct xfs_trans *tp; |
| 493 | int i; |
| 494 | int error = 0; |
| 495 | xfs_extent_t *extp; |
| 496 | xfs_fsblock_t startblock_fsb; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 497 | struct xfs_owner_info oinfo; |
Darrick J. Wong | dc42375 | 2016-08-03 11:23:49 +1000 | [diff] [blame] | 498 | |
| 499 | ASSERT(!test_bit(XFS_EFI_RECOVERED, &efip->efi_flags)); |
| 500 | |
| 501 | /* |
| 502 | * First check the validity of the extents described by the |
| 503 | * EFI. If any are bad, then assume that all are bad and |
| 504 | * just toss the EFI. |
| 505 | */ |
| 506 | for (i = 0; i < efip->efi_format.efi_nextents; i++) { |
Darrick J. Wong | e127faf | 2016-08-03 12:29:32 +1000 | [diff] [blame] | 507 | extp = &efip->efi_format.efi_extents[i]; |
Darrick J. Wong | dc42375 | 2016-08-03 11:23:49 +1000 | [diff] [blame] | 508 | startblock_fsb = XFS_BB_TO_FSB(mp, |
| 509 | XFS_FSB_TO_DADDR(mp, extp->ext_start)); |
Darrick J. Wong | e127faf | 2016-08-03 12:29:32 +1000 | [diff] [blame] | 510 | if (startblock_fsb == 0 || |
| 511 | extp->ext_len == 0 || |
| 512 | startblock_fsb >= mp->m_sb.sb_dblocks || |
| 513 | extp->ext_len >= mp->m_sb.sb_agblocks) { |
Darrick J. Wong | dc42375 | 2016-08-03 11:23:49 +1000 | [diff] [blame] | 514 | /* |
| 515 | * This will pull the EFI from the AIL and |
| 516 | * free the memory associated with it. |
| 517 | */ |
| 518 | set_bit(XFS_EFI_RECOVERED, &efip->efi_flags); |
| 519 | xfs_efi_release(efip); |
| 520 | return -EIO; |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp); |
| 525 | if (error) |
| 526 | return error; |
| 527 | efdp = xfs_trans_get_efd(tp, efip, efip->efi_format.efi_nextents); |
| 528 | |
Darrick J. Wong | 33df3a9 | 2017-12-07 19:07:27 -0800 | [diff] [blame] | 529 | xfs_rmap_any_owner_update(&oinfo); |
Darrick J. Wong | dc42375 | 2016-08-03 11:23:49 +1000 | [diff] [blame] | 530 | for (i = 0; i < efip->efi_format.efi_nextents; i++) { |
Darrick J. Wong | e127faf | 2016-08-03 12:29:32 +1000 | [diff] [blame] | 531 | extp = &efip->efi_format.efi_extents[i]; |
Darrick J. Wong | dc42375 | 2016-08-03 11:23:49 +1000 | [diff] [blame] | 532 | error = xfs_trans_free_extent(tp, efdp, extp->ext_start, |
Brian Foster | fcb762f | 2018-05-09 08:45:04 -0700 | [diff] [blame] | 533 | extp->ext_len, &oinfo, false); |
Darrick J. Wong | dc42375 | 2016-08-03 11:23:49 +1000 | [diff] [blame] | 534 | if (error) |
| 535 | goto abort_error; |
| 536 | |
| 537 | } |
| 538 | |
| 539 | set_bit(XFS_EFI_RECOVERED, &efip->efi_flags); |
| 540 | error = xfs_trans_commit(tp); |
| 541 | return error; |
| 542 | |
| 543 | abort_error: |
| 544 | xfs_trans_cancel(tp); |
| 545 | return error; |
| 546 | } |