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