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,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" |
Dave Chinner | 70a9883 | 2013-10-23 10:36:05 +1100 | [diff] [blame] | 20 | #include "xfs_shared.h" |
Christoph Hellwig | 4fb6e8a | 2014-11-28 14:25:04 +1100 | [diff] [blame] | 21 | #include "xfs_format.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 22 | #include "xfs_log_format.h" |
| 23 | #include "xfs_trans_resv.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "xfs_mount.h" |
Darrick J. Wong | 9749fee | 2016-08-03 11:14:35 +1000 | [diff] [blame^] | 25 | #include "xfs_defer.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 26 | #include "xfs_trans.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include "xfs_trans_priv.h" |
| 28 | #include "xfs_extfree_item.h" |
Brian Foster | 6bc43af | 2015-08-19 09:51:43 +1000 | [diff] [blame] | 29 | #include "xfs_alloc.h" |
Darrick J. Wong | 9749fee | 2016-08-03 11:14:35 +1000 | [diff] [blame^] | 30 | #include "xfs_bmap.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 32 | /* |
| 33 | * This routine is called to allocate an "extent free intention" |
| 34 | * log item that will hold nextents worth of extents. The |
| 35 | * caller must use all nextents extents, because we are not |
| 36 | * flexible about this at all. |
| 37 | */ |
Darrick J. Wong | bba61cb | 2016-08-03 11:13:47 +1000 | [diff] [blame] | 38 | struct xfs_efi_log_item * |
| 39 | xfs_trans_get_efi(struct xfs_trans *tp, |
| 40 | uint nextents) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | { |
Darrick J. Wong | bba61cb | 2016-08-03 11:13:47 +1000 | [diff] [blame] | 42 | struct xfs_efi_log_item *efip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | ASSERT(tp != NULL); |
| 45 | ASSERT(nextents > 0); |
| 46 | |
| 47 | efip = xfs_efi_init(tp->t_mountp, nextents); |
| 48 | ASSERT(efip != NULL); |
| 49 | |
| 50 | /* |
| 51 | * Get a log_item_desc to point at the new item. |
| 52 | */ |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 53 | xfs_trans_add_item(tp, &efip->efi_item); |
| 54 | return efip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /* |
| 58 | * This routine is called to indicate that the described |
| 59 | * extent is to be logged as needing to be freed. It should |
| 60 | * be called once for each extent to be freed. |
| 61 | */ |
| 62 | void |
Darrick J. Wong | bba61cb | 2016-08-03 11:13:47 +1000 | [diff] [blame] | 63 | xfs_trans_log_efi_extent(struct xfs_trans *tp, |
| 64 | struct xfs_efi_log_item *efip, |
| 65 | xfs_fsblock_t start_block, |
| 66 | xfs_extlen_t ext_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | { |
Darrick J. Wong | bba61cb | 2016-08-03 11:13:47 +1000 | [diff] [blame] | 68 | uint next_extent; |
| 69 | struct xfs_extent *extp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | tp->t_flags |= XFS_TRANS_DIRTY; |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 72 | efip->efi_item.li_desc->lid_flags |= XFS_LID_DIRTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
Dave Chinner | b199c8a | 2010-12-20 11:59:49 +1100 | [diff] [blame] | 74 | /* |
| 75 | * atomic_inc_return gives us the value after the increment; |
| 76 | * we want to use it as an array index so we need to subtract 1 from |
| 77 | * it. |
| 78 | */ |
| 79 | next_extent = atomic_inc_return(&efip->efi_next_extent) - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | ASSERT(next_extent < efip->efi_format.efi_nextents); |
| 81 | extp = &(efip->efi_format.efi_extents[next_extent]); |
| 82 | extp->ext_start = start_block; |
| 83 | extp->ext_len = ext_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | |
| 87 | /* |
| 88 | * This routine is called to allocate an "extent free done" |
| 89 | * log item that will hold nextents worth of extents. The |
| 90 | * caller must use all nextents extents, because we are not |
| 91 | * flexible about this at all. |
| 92 | */ |
Darrick J. Wong | bba61cb | 2016-08-03 11:13:47 +1000 | [diff] [blame] | 93 | struct xfs_efd_log_item * |
| 94 | xfs_trans_get_efd(struct xfs_trans *tp, |
| 95 | struct xfs_efi_log_item *efip, |
| 96 | uint nextents) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | { |
Darrick J. Wong | bba61cb | 2016-08-03 11:13:47 +1000 | [diff] [blame] | 98 | struct xfs_efd_log_item *efdp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
| 100 | ASSERT(tp != NULL); |
| 101 | ASSERT(nextents > 0); |
| 102 | |
| 103 | efdp = xfs_efd_init(tp->t_mountp, efip, nextents); |
| 104 | ASSERT(efdp != NULL); |
| 105 | |
| 106 | /* |
| 107 | * Get a log_item_desc to point at the new item. |
| 108 | */ |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 109 | xfs_trans_add_item(tp, &efdp->efd_item); |
| 110 | return efdp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /* |
Brian Foster | 6bc43af | 2015-08-19 09:51:43 +1000 | [diff] [blame] | 114 | * Free an extent and log it to the EFD. Note that the transaction is marked |
| 115 | * dirty regardless of whether the extent free succeeds or fails to support the |
| 116 | * EFI/EFD lifecycle rules. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | */ |
Brian Foster | 6bc43af | 2015-08-19 09:51:43 +1000 | [diff] [blame] | 118 | int |
| 119 | xfs_trans_free_extent( |
| 120 | struct xfs_trans *tp, |
| 121 | struct xfs_efd_log_item *efdp, |
| 122 | xfs_fsblock_t start_block, |
| 123 | xfs_extlen_t ext_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | uint next_extent; |
Brian Foster | 6bc43af | 2015-08-19 09:51:43 +1000 | [diff] [blame] | 126 | struct xfs_extent *extp; |
| 127 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
Brian Foster | 6bc43af | 2015-08-19 09:51:43 +1000 | [diff] [blame] | 129 | error = xfs_free_extent(tp, start_block, ext_len); |
| 130 | |
| 131 | /* |
| 132 | * Mark the transaction dirty, even on error. This ensures the |
| 133 | * transaction is aborted, which: |
| 134 | * |
| 135 | * 1.) releases the EFI and frees the EFD |
| 136 | * 2.) shuts down the filesystem |
| 137 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | tp->t_flags |= XFS_TRANS_DIRTY; |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 139 | efdp->efd_item.li_desc->lid_flags |= XFS_LID_DIRTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
| 141 | next_extent = efdp->efd_next_extent; |
| 142 | ASSERT(next_extent < efdp->efd_format.efd_nextents); |
| 143 | extp = &(efdp->efd_format.efd_extents[next_extent]); |
| 144 | extp->ext_start = start_block; |
| 145 | extp->ext_len = ext_len; |
| 146 | efdp->efd_next_extent++; |
Brian Foster | 6bc43af | 2015-08-19 09:51:43 +1000 | [diff] [blame] | 147 | |
| 148 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | } |
Darrick J. Wong | 9749fee | 2016-08-03 11:14:35 +1000 | [diff] [blame^] | 150 | |
| 151 | /* Sort bmap items by AG. */ |
| 152 | static int |
| 153 | xfs_extent_free_diff_items( |
| 154 | void *priv, |
| 155 | struct list_head *a, |
| 156 | struct list_head *b) |
| 157 | { |
| 158 | struct xfs_mount *mp = priv; |
| 159 | struct xfs_bmap_free_item *ra; |
| 160 | struct xfs_bmap_free_item *rb; |
| 161 | |
| 162 | ra = container_of(a, struct xfs_bmap_free_item, xbfi_list); |
| 163 | rb = container_of(b, struct xfs_bmap_free_item, xbfi_list); |
| 164 | return XFS_FSB_TO_AGNO(mp, ra->xbfi_startblock) - |
| 165 | XFS_FSB_TO_AGNO(mp, rb->xbfi_startblock); |
| 166 | } |
| 167 | |
| 168 | /* Get an EFI. */ |
| 169 | STATIC void * |
| 170 | xfs_extent_free_create_intent( |
| 171 | struct xfs_trans *tp, |
| 172 | unsigned int count) |
| 173 | { |
| 174 | return xfs_trans_get_efi(tp, count); |
| 175 | } |
| 176 | |
| 177 | /* Log a free extent to the intent item. */ |
| 178 | STATIC void |
| 179 | xfs_extent_free_log_item( |
| 180 | struct xfs_trans *tp, |
| 181 | void *intent, |
| 182 | struct list_head *item) |
| 183 | { |
| 184 | struct xfs_bmap_free_item *free; |
| 185 | |
| 186 | free = container_of(item, struct xfs_bmap_free_item, xbfi_list); |
| 187 | xfs_trans_log_efi_extent(tp, intent, free->xbfi_startblock, |
| 188 | free->xbfi_blockcount); |
| 189 | } |
| 190 | |
| 191 | /* Get an EFD so we can process all the free extents. */ |
| 192 | STATIC void * |
| 193 | xfs_extent_free_create_done( |
| 194 | struct xfs_trans *tp, |
| 195 | void *intent, |
| 196 | unsigned int count) |
| 197 | { |
| 198 | return xfs_trans_get_efd(tp, intent, count); |
| 199 | } |
| 200 | |
| 201 | /* Process a free extent. */ |
| 202 | STATIC int |
| 203 | xfs_extent_free_finish_item( |
| 204 | struct xfs_trans *tp, |
| 205 | struct xfs_defer_ops *dop, |
| 206 | struct list_head *item, |
| 207 | void *done_item, |
| 208 | void **state) |
| 209 | { |
| 210 | struct xfs_bmap_free_item *free; |
| 211 | int error; |
| 212 | |
| 213 | free = container_of(item, struct xfs_bmap_free_item, xbfi_list); |
| 214 | error = xfs_trans_free_extent(tp, done_item, |
| 215 | free->xbfi_startblock, |
| 216 | free->xbfi_blockcount); |
| 217 | kmem_free(free); |
| 218 | return error; |
| 219 | } |
| 220 | |
| 221 | /* Abort all pending EFIs. */ |
| 222 | STATIC void |
| 223 | xfs_extent_free_abort_intent( |
| 224 | void *intent) |
| 225 | { |
| 226 | xfs_efi_release(intent); |
| 227 | } |
| 228 | |
| 229 | /* Cancel a free extent. */ |
| 230 | STATIC void |
| 231 | xfs_extent_free_cancel_item( |
| 232 | struct list_head *item) |
| 233 | { |
| 234 | struct xfs_bmap_free_item *free; |
| 235 | |
| 236 | free = container_of(item, struct xfs_bmap_free_item, xbfi_list); |
| 237 | kmem_free(free); |
| 238 | } |
| 239 | |
| 240 | static const struct xfs_defer_op_type xfs_extent_free_defer_type = { |
| 241 | .type = XFS_DEFER_OPS_TYPE_FREE, |
| 242 | .max_items = XFS_EFI_MAX_FAST_EXTENTS, |
| 243 | .diff_items = xfs_extent_free_diff_items, |
| 244 | .create_intent = xfs_extent_free_create_intent, |
| 245 | .abort_intent = xfs_extent_free_abort_intent, |
| 246 | .log_item = xfs_extent_free_log_item, |
| 247 | .create_done = xfs_extent_free_create_done, |
| 248 | .finish_item = xfs_extent_free_finish_item, |
| 249 | .cancel_item = xfs_extent_free_cancel_item, |
| 250 | }; |
| 251 | |
| 252 | /* Register the deferred op type. */ |
| 253 | void |
| 254 | xfs_extent_free_init_defer_op(void) |
| 255 | { |
| 256 | xfs_defer_init_op_type(&xfs_extent_free_defer_type); |
| 257 | } |