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" |
Darrick J. Wong | 3481b68 | 2016-08-03 12:31:07 +1000 | [diff] [blame] | 24 | #include "xfs_bit.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include "xfs_mount.h" |
Darrick J. Wong | 9749fee | 2016-08-03 11:14:35 +1000 | [diff] [blame] | 26 | #include "xfs_defer.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 27 | #include "xfs_trans.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include "xfs_trans_priv.h" |
| 29 | #include "xfs_extfree_item.h" |
Brian Foster | 6bc43af | 2015-08-19 09:51:43 +1000 | [diff] [blame] | 30 | #include "xfs_alloc.h" |
Darrick J. Wong | 9749fee | 2016-08-03 11:14:35 +1000 | [diff] [blame] | 31 | #include "xfs_bmap.h" |
Darrick J. Wong | 3481b68 | 2016-08-03 12:31:07 +1000 | [diff] [blame] | 32 | #include "xfs_trace.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | * This routine is called to allocate an "extent free done" |
| 36 | * log item that will hold nextents worth of extents. The |
| 37 | * caller must use all nextents extents, because we are not |
| 38 | * flexible about this at all. |
| 39 | */ |
Darrick J. Wong | bba61cb | 2016-08-03 11:13:47 +1000 | [diff] [blame] | 40 | struct xfs_efd_log_item * |
| 41 | xfs_trans_get_efd(struct xfs_trans *tp, |
| 42 | struct xfs_efi_log_item *efip, |
| 43 | uint nextents) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | { |
Darrick J. Wong | bba61cb | 2016-08-03 11:13:47 +1000 | [diff] [blame] | 45 | struct xfs_efd_log_item *efdp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
| 47 | ASSERT(tp != NULL); |
| 48 | ASSERT(nextents > 0); |
| 49 | |
| 50 | efdp = xfs_efd_init(tp->t_mountp, efip, nextents); |
| 51 | ASSERT(efdp != NULL); |
| 52 | |
| 53 | /* |
| 54 | * Get a log_item_desc to point at the new item. |
| 55 | */ |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 56 | xfs_trans_add_item(tp, &efdp->efd_item); |
| 57 | return efdp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /* |
Brian Foster | 6bc43af | 2015-08-19 09:51:43 +1000 | [diff] [blame] | 61 | * Free an extent and log it to the EFD. Note that the transaction is marked |
| 62 | * dirty regardless of whether the extent free succeeds or fails to support the |
| 63 | * EFI/EFD lifecycle rules. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | */ |
Brian Foster | 6bc43af | 2015-08-19 09:51:43 +1000 | [diff] [blame] | 65 | int |
| 66 | xfs_trans_free_extent( |
| 67 | struct xfs_trans *tp, |
| 68 | struct xfs_efd_log_item *efdp, |
| 69 | xfs_fsblock_t start_block, |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 70 | xfs_extlen_t ext_len, |
| 71 | struct xfs_owner_info *oinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | { |
Darrick J. Wong | 3481b68 | 2016-08-03 12:31:07 +1000 | [diff] [blame] | 73 | struct xfs_mount *mp = tp->t_mountp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | uint next_extent; |
Darrick J. Wong | 3481b68 | 2016-08-03 12:31:07 +1000 | [diff] [blame] | 75 | xfs_agnumber_t agno = XFS_FSB_TO_AGNO(mp, start_block); |
| 76 | xfs_agblock_t agbno = XFS_FSB_TO_AGBNO(mp, start_block); |
Brian Foster | 6bc43af | 2015-08-19 09:51:43 +1000 | [diff] [blame] | 77 | struct xfs_extent *extp; |
| 78 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
Darrick J. Wong | 3481b68 | 2016-08-03 12:31:07 +1000 | [diff] [blame] | 80 | trace_xfs_bmap_free_deferred(tp->t_mountp, agno, 0, agbno, ext_len); |
| 81 | |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 82 | error = xfs_free_extent(tp, start_block, ext_len, oinfo, |
| 83 | XFS_AG_RESV_NONE); |
Brian Foster | 6bc43af | 2015-08-19 09:51:43 +1000 | [diff] [blame] | 84 | |
| 85 | /* |
| 86 | * Mark the transaction dirty, even on error. This ensures the |
| 87 | * transaction is aborted, which: |
| 88 | * |
| 89 | * 1.) releases the EFI and frees the EFD |
| 90 | * 2.) shuts down the filesystem |
| 91 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | tp->t_flags |= XFS_TRANS_DIRTY; |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 93 | efdp->efd_item.li_desc->lid_flags |= XFS_LID_DIRTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
| 95 | next_extent = efdp->efd_next_extent; |
| 96 | ASSERT(next_extent < efdp->efd_format.efd_nextents); |
| 97 | extp = &(efdp->efd_format.efd_extents[next_extent]); |
| 98 | extp->ext_start = start_block; |
| 99 | extp->ext_len = ext_len; |
| 100 | efdp->efd_next_extent++; |
Brian Foster | 6bc43af | 2015-08-19 09:51:43 +1000 | [diff] [blame] | 101 | |
| 102 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | } |
Darrick J. Wong | 9749fee | 2016-08-03 11:14:35 +1000 | [diff] [blame] | 104 | |
| 105 | /* Sort bmap items by AG. */ |
| 106 | static int |
| 107 | xfs_extent_free_diff_items( |
| 108 | void *priv, |
| 109 | struct list_head *a, |
| 110 | struct list_head *b) |
| 111 | { |
| 112 | struct xfs_mount *mp = priv; |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 113 | struct xfs_extent_free_item *ra; |
| 114 | struct xfs_extent_free_item *rb; |
Darrick J. Wong | 9749fee | 2016-08-03 11:14:35 +1000 | [diff] [blame] | 115 | |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 116 | ra = container_of(a, struct xfs_extent_free_item, xefi_list); |
| 117 | rb = container_of(b, struct xfs_extent_free_item, xefi_list); |
| 118 | return XFS_FSB_TO_AGNO(mp, ra->xefi_startblock) - |
| 119 | XFS_FSB_TO_AGNO(mp, rb->xefi_startblock); |
Darrick J. Wong | 9749fee | 2016-08-03 11:14:35 +1000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /* Get an EFI. */ |
| 123 | STATIC void * |
| 124 | xfs_extent_free_create_intent( |
| 125 | struct xfs_trans *tp, |
| 126 | unsigned int count) |
| 127 | { |
Darrick J. Wong | 51ce9d0 | 2016-08-03 12:30:31 +1000 | [diff] [blame] | 128 | struct xfs_efi_log_item *efip; |
| 129 | |
| 130 | ASSERT(tp != NULL); |
| 131 | ASSERT(count > 0); |
| 132 | |
| 133 | efip = xfs_efi_init(tp->t_mountp, count); |
| 134 | ASSERT(efip != NULL); |
| 135 | |
| 136 | /* |
| 137 | * Get a log_item_desc to point at the new item. |
| 138 | */ |
| 139 | xfs_trans_add_item(tp, &efip->efi_item); |
| 140 | return efip; |
Darrick J. Wong | 9749fee | 2016-08-03 11:14:35 +1000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /* Log a free extent to the intent item. */ |
| 144 | STATIC void |
| 145 | xfs_extent_free_log_item( |
| 146 | struct xfs_trans *tp, |
| 147 | void *intent, |
| 148 | struct list_head *item) |
| 149 | { |
Darrick J. Wong | 51ce9d0 | 2016-08-03 12:30:31 +1000 | [diff] [blame] | 150 | struct xfs_efi_log_item *efip = intent; |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 151 | struct xfs_extent_free_item *free; |
Darrick J. Wong | 51ce9d0 | 2016-08-03 12:30:31 +1000 | [diff] [blame] | 152 | uint next_extent; |
| 153 | struct xfs_extent *extp; |
Darrick J. Wong | 9749fee | 2016-08-03 11:14:35 +1000 | [diff] [blame] | 154 | |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 155 | free = container_of(item, struct xfs_extent_free_item, xefi_list); |
Darrick J. Wong | 51ce9d0 | 2016-08-03 12:30:31 +1000 | [diff] [blame] | 156 | |
| 157 | tp->t_flags |= XFS_TRANS_DIRTY; |
| 158 | efip->efi_item.li_desc->lid_flags |= XFS_LID_DIRTY; |
| 159 | |
| 160 | /* |
| 161 | * atomic_inc_return gives us the value after the increment; |
| 162 | * we want to use it as an array index so we need to subtract 1 from |
| 163 | * it. |
| 164 | */ |
| 165 | next_extent = atomic_inc_return(&efip->efi_next_extent) - 1; |
| 166 | ASSERT(next_extent < efip->efi_format.efi_nextents); |
| 167 | extp = &efip->efi_format.efi_extents[next_extent]; |
| 168 | extp->ext_start = free->xefi_startblock; |
| 169 | extp->ext_len = free->xefi_blockcount; |
Darrick J. Wong | 9749fee | 2016-08-03 11:14:35 +1000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /* Get an EFD so we can process all the free extents. */ |
| 173 | STATIC void * |
| 174 | xfs_extent_free_create_done( |
| 175 | struct xfs_trans *tp, |
| 176 | void *intent, |
| 177 | unsigned int count) |
| 178 | { |
| 179 | return xfs_trans_get_efd(tp, intent, count); |
| 180 | } |
| 181 | |
| 182 | /* Process a free extent. */ |
| 183 | STATIC int |
| 184 | xfs_extent_free_finish_item( |
| 185 | struct xfs_trans *tp, |
| 186 | struct xfs_defer_ops *dop, |
| 187 | struct list_head *item, |
| 188 | void *done_item, |
| 189 | void **state) |
| 190 | { |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 191 | struct xfs_extent_free_item *free; |
Darrick J. Wong | 9749fee | 2016-08-03 11:14:35 +1000 | [diff] [blame] | 192 | int error; |
| 193 | |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 194 | free = container_of(item, struct xfs_extent_free_item, xefi_list); |
Darrick J. Wong | 9749fee | 2016-08-03 11:14:35 +1000 | [diff] [blame] | 195 | error = xfs_trans_free_extent(tp, done_item, |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 196 | free->xefi_startblock, |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 197 | free->xefi_blockcount, |
| 198 | &free->xefi_oinfo); |
Darrick J. Wong | 9749fee | 2016-08-03 11:14:35 +1000 | [diff] [blame] | 199 | kmem_free(free); |
| 200 | return error; |
| 201 | } |
| 202 | |
| 203 | /* Abort all pending EFIs. */ |
| 204 | STATIC void |
| 205 | xfs_extent_free_abort_intent( |
| 206 | void *intent) |
| 207 | { |
| 208 | xfs_efi_release(intent); |
| 209 | } |
| 210 | |
| 211 | /* Cancel a free extent. */ |
| 212 | STATIC void |
| 213 | xfs_extent_free_cancel_item( |
| 214 | struct list_head *item) |
| 215 | { |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 216 | struct xfs_extent_free_item *free; |
Darrick J. Wong | 9749fee | 2016-08-03 11:14:35 +1000 | [diff] [blame] | 217 | |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 218 | free = container_of(item, struct xfs_extent_free_item, xefi_list); |
Darrick J. Wong | 9749fee | 2016-08-03 11:14:35 +1000 | [diff] [blame] | 219 | kmem_free(free); |
| 220 | } |
| 221 | |
| 222 | static const struct xfs_defer_op_type xfs_extent_free_defer_type = { |
| 223 | .type = XFS_DEFER_OPS_TYPE_FREE, |
| 224 | .max_items = XFS_EFI_MAX_FAST_EXTENTS, |
| 225 | .diff_items = xfs_extent_free_diff_items, |
| 226 | .create_intent = xfs_extent_free_create_intent, |
| 227 | .abort_intent = xfs_extent_free_abort_intent, |
| 228 | .log_item = xfs_extent_free_log_item, |
| 229 | .create_done = xfs_extent_free_create_done, |
| 230 | .finish_item = xfs_extent_free_finish_item, |
| 231 | .cancel_item = xfs_extent_free_cancel_item, |
| 232 | }; |
| 233 | |
Brian Foster | f8f2835 | 2018-05-07 17:38:47 -0700 | [diff] [blame^] | 234 | /* |
| 235 | * AGFL blocks are accounted differently in the reserve pools and are not |
| 236 | * inserted into the busy extent list. |
| 237 | */ |
| 238 | STATIC int |
| 239 | xfs_agfl_free_finish_item( |
| 240 | struct xfs_trans *tp, |
| 241 | struct xfs_defer_ops *dop, |
| 242 | struct list_head *item, |
| 243 | void *done_item, |
| 244 | void **state) |
| 245 | { |
| 246 | struct xfs_mount *mp = tp->t_mountp; |
| 247 | struct xfs_efd_log_item *efdp = done_item; |
| 248 | struct xfs_extent_free_item *free; |
| 249 | struct xfs_extent *extp; |
| 250 | struct xfs_buf *agbp; |
| 251 | int error; |
| 252 | xfs_agnumber_t agno; |
| 253 | xfs_agblock_t agbno; |
| 254 | uint next_extent; |
| 255 | |
| 256 | free = container_of(item, struct xfs_extent_free_item, xefi_list); |
| 257 | ASSERT(free->xefi_blockcount == 1); |
| 258 | agno = XFS_FSB_TO_AGNO(mp, free->xefi_startblock); |
| 259 | agbno = XFS_FSB_TO_AGBNO(mp, free->xefi_startblock); |
| 260 | |
| 261 | trace_xfs_agfl_free_deferred(mp, agno, 0, agbno, free->xefi_blockcount); |
| 262 | |
| 263 | error = xfs_alloc_read_agf(mp, tp, agno, 0, &agbp); |
| 264 | if (!error) |
| 265 | error = xfs_free_agfl_block(tp, agno, agbno, agbp, |
| 266 | &free->xefi_oinfo); |
| 267 | |
| 268 | /* |
| 269 | * Mark the transaction dirty, even on error. This ensures the |
| 270 | * transaction is aborted, which: |
| 271 | * |
| 272 | * 1.) releases the EFI and frees the EFD |
| 273 | * 2.) shuts down the filesystem |
| 274 | */ |
| 275 | tp->t_flags |= XFS_TRANS_DIRTY; |
| 276 | efdp->efd_item.li_desc->lid_flags |= XFS_LID_DIRTY; |
| 277 | |
| 278 | next_extent = efdp->efd_next_extent; |
| 279 | ASSERT(next_extent < efdp->efd_format.efd_nextents); |
| 280 | extp = &(efdp->efd_format.efd_extents[next_extent]); |
| 281 | extp->ext_start = free->xefi_startblock; |
| 282 | extp->ext_len = free->xefi_blockcount; |
| 283 | efdp->efd_next_extent++; |
| 284 | |
| 285 | kmem_free(free); |
| 286 | return error; |
| 287 | } |
| 288 | |
| 289 | |
| 290 | /* sub-type with special handling for AGFL deferred frees */ |
| 291 | static const struct xfs_defer_op_type xfs_agfl_free_defer_type = { |
| 292 | .type = XFS_DEFER_OPS_TYPE_AGFL_FREE, |
| 293 | .max_items = XFS_EFI_MAX_FAST_EXTENTS, |
| 294 | .diff_items = xfs_extent_free_diff_items, |
| 295 | .create_intent = xfs_extent_free_create_intent, |
| 296 | .abort_intent = xfs_extent_free_abort_intent, |
| 297 | .log_item = xfs_extent_free_log_item, |
| 298 | .create_done = xfs_extent_free_create_done, |
| 299 | .finish_item = xfs_agfl_free_finish_item, |
| 300 | .cancel_item = xfs_extent_free_cancel_item, |
| 301 | }; |
| 302 | |
Darrick J. Wong | 9749fee | 2016-08-03 11:14:35 +1000 | [diff] [blame] | 303 | /* Register the deferred op type. */ |
| 304 | void |
| 305 | xfs_extent_free_init_defer_op(void) |
| 306 | { |
| 307 | xfs_defer_init_op_type(&xfs_extent_free_defer_type); |
Brian Foster | f8f2835 | 2018-05-07 17:38:47 -0700 | [diff] [blame^] | 308 | xfs_defer_init_op_type(&xfs_agfl_free_defer_type); |
Darrick J. Wong | 9749fee | 2016-08-03 11:14:35 +1000 | [diff] [blame] | 309 | } |