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