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_ag.h" 15 #include "xfs_defer.h" 16 #include "xfs_trans.h" 17 #include "xfs_trans_priv.h" 18 #include "xfs_extfree_item.h" 19 #include "xfs_log.h" 20 #include "xfs_btree.h" 21 #include "xfs_rmap.h" 22 #include "xfs_alloc.h" 23 #include "xfs_bmap.h" 24 #include "xfs_trace.h" 25 #include "xfs_error.h" 26 #include "xfs_log_priv.h" 27 #include "xfs_log_recover.h" 28 #include "xfs_rtalloc.h" 29 #include "xfs_inode.h" 30 #include "xfs_rtbitmap.h" 31 #include "xfs_rtgroup.h" 32 #include "xfs_zone_alloc.h" 33 34 struct kmem_cache *xfs_efi_cache; 35 struct kmem_cache *xfs_efd_cache; 36 37 static const struct xfs_item_ops xfs_efi_item_ops; 38 39 static inline struct xfs_efi_log_item *EFI_ITEM(struct xfs_log_item *lip) 40 { 41 return container_of(lip, struct xfs_efi_log_item, efi_item); 42 } 43 44 STATIC void 45 xfs_efi_item_free( 46 struct xfs_efi_log_item *efip) 47 { 48 kvfree(efip->efi_item.li_lv_shadow); 49 if (efip->efi_format.efi_nextents > XFS_EFI_MAX_FAST_EXTENTS) 50 kfree(efip); 51 else 52 kmem_cache_free(xfs_efi_cache, efip); 53 } 54 55 /* 56 * Freeing the efi requires that we remove it from the AIL if it has already 57 * been placed there. However, the EFI may not yet have been placed in the AIL 58 * when called by xfs_efi_release() from EFD processing due to the ordering of 59 * committed vs unpin operations in bulk insert operations. Hence the reference 60 * count to ensure only the last caller frees the EFI. 61 */ 62 STATIC void 63 xfs_efi_release( 64 struct xfs_efi_log_item *efip) 65 { 66 ASSERT(atomic_read(&efip->efi_refcount) > 0); 67 if (!atomic_dec_and_test(&efip->efi_refcount)) 68 return; 69 70 xfs_trans_ail_delete(&efip->efi_item, 0); 71 xfs_efi_item_free(efip); 72 } 73 74 STATIC void 75 xfs_efi_item_size( 76 struct xfs_log_item *lip, 77 int *nvecs, 78 int *nbytes) 79 { 80 struct xfs_efi_log_item *efip = EFI_ITEM(lip); 81 82 *nvecs += 1; 83 *nbytes += xfs_efi_log_format_sizeof(efip->efi_format.efi_nextents); 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 ASSERT(lip->li_type == XFS_LI_EFI || lip->li_type == XFS_LI_EFI_RT); 104 105 efip->efi_format.efi_type = lip->li_type; 106 efip->efi_format.efi_size = 1; 107 108 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFI_FORMAT, &efip->efi_format, 109 xfs_efi_log_format_sizeof(efip->efi_format.efi_nextents)); 110 } 111 112 /* 113 * The unpin operation is the last place an EFI is manipulated in the log. It is 114 * either inserted in the AIL or aborted in the event of a log I/O error. In 115 * either case, the EFI transaction has been successfully committed to make it 116 * this far. Therefore, we expect whoever committed the EFI to either construct 117 * and commit the EFD or drop the EFD's reference in the event of error. Simply 118 * drop the log's EFI reference now that the log is done with it. 119 */ 120 STATIC void 121 xfs_efi_item_unpin( 122 struct xfs_log_item *lip, 123 int remove) 124 { 125 struct xfs_efi_log_item *efip = EFI_ITEM(lip); 126 xfs_efi_release(efip); 127 } 128 129 /* 130 * The EFI has been either committed or aborted if the transaction has been 131 * cancelled. If the transaction was cancelled, an EFD isn't going to be 132 * constructed and thus we free the EFI here directly. 133 */ 134 STATIC void 135 xfs_efi_item_release( 136 struct xfs_log_item *lip) 137 { 138 xfs_efi_release(EFI_ITEM(lip)); 139 } 140 141 /* 142 * Allocate and initialize an efi item with the given number of extents. 143 */ 144 STATIC struct xfs_efi_log_item * 145 xfs_efi_init( 146 struct xfs_mount *mp, 147 unsigned short item_type, 148 uint nextents) 149 { 150 struct xfs_efi_log_item *efip; 151 152 ASSERT(item_type == XFS_LI_EFI || item_type == XFS_LI_EFI_RT); 153 ASSERT(nextents > 0); 154 155 if (nextents > XFS_EFI_MAX_FAST_EXTENTS) { 156 efip = kzalloc(xfs_efi_log_item_sizeof(nextents), 157 GFP_KERNEL | __GFP_NOFAIL); 158 } else { 159 efip = kmem_cache_zalloc(xfs_efi_cache, 160 GFP_KERNEL | __GFP_NOFAIL); 161 } 162 163 xfs_log_item_init(mp, &efip->efi_item, item_type, &xfs_efi_item_ops); 164 efip->efi_format.efi_nextents = nextents; 165 efip->efi_format.efi_id = (uintptr_t)(void *)efip; 166 atomic_set(&efip->efi_next_extent, 0); 167 atomic_set(&efip->efi_refcount, 2); 168 169 return efip; 170 } 171 172 /* 173 * Copy an EFI format buffer from the given buf, and into the destination 174 * EFI format structure. 175 * The given buffer can be in 32 bit or 64 bit form (which has different padding), 176 * one of which will be the native format for this kernel. 177 * It will handle the conversion of formats if necessary. 178 */ 179 STATIC int 180 xfs_efi_copy_format(xfs_log_iovec_t *buf, xfs_efi_log_format_t *dst_efi_fmt) 181 { 182 xfs_efi_log_format_t *src_efi_fmt = buf->i_addr; 183 uint i; 184 uint len = xfs_efi_log_format_sizeof(src_efi_fmt->efi_nextents); 185 uint len32 = xfs_efi_log_format32_sizeof(src_efi_fmt->efi_nextents); 186 uint len64 = xfs_efi_log_format64_sizeof(src_efi_fmt->efi_nextents); 187 188 if (buf->i_len == len) { 189 memcpy(dst_efi_fmt, src_efi_fmt, 190 offsetof(struct xfs_efi_log_format, efi_extents)); 191 for (i = 0; i < src_efi_fmt->efi_nextents; i++) 192 memcpy(&dst_efi_fmt->efi_extents[i], 193 &src_efi_fmt->efi_extents[i], 194 sizeof(struct xfs_extent)); 195 return 0; 196 } else if (buf->i_len == len32) { 197 xfs_efi_log_format_32_t *src_efi_fmt_32 = buf->i_addr; 198 199 dst_efi_fmt->efi_type = src_efi_fmt_32->efi_type; 200 dst_efi_fmt->efi_size = src_efi_fmt_32->efi_size; 201 dst_efi_fmt->efi_nextents = src_efi_fmt_32->efi_nextents; 202 dst_efi_fmt->efi_id = src_efi_fmt_32->efi_id; 203 for (i = 0; i < dst_efi_fmt->efi_nextents; i++) { 204 dst_efi_fmt->efi_extents[i].ext_start = 205 src_efi_fmt_32->efi_extents[i].ext_start; 206 dst_efi_fmt->efi_extents[i].ext_len = 207 src_efi_fmt_32->efi_extents[i].ext_len; 208 } 209 return 0; 210 } else if (buf->i_len == len64) { 211 xfs_efi_log_format_64_t *src_efi_fmt_64 = buf->i_addr; 212 213 dst_efi_fmt->efi_type = src_efi_fmt_64->efi_type; 214 dst_efi_fmt->efi_size = src_efi_fmt_64->efi_size; 215 dst_efi_fmt->efi_nextents = src_efi_fmt_64->efi_nextents; 216 dst_efi_fmt->efi_id = src_efi_fmt_64->efi_id; 217 for (i = 0; i < dst_efi_fmt->efi_nextents; i++) { 218 dst_efi_fmt->efi_extents[i].ext_start = 219 src_efi_fmt_64->efi_extents[i].ext_start; 220 dst_efi_fmt->efi_extents[i].ext_len = 221 src_efi_fmt_64->efi_extents[i].ext_len; 222 } 223 return 0; 224 } 225 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, NULL, buf->i_addr, 226 buf->i_len); 227 return -EFSCORRUPTED; 228 } 229 230 static inline struct xfs_efd_log_item *EFD_ITEM(struct xfs_log_item *lip) 231 { 232 return container_of(lip, struct xfs_efd_log_item, efd_item); 233 } 234 235 STATIC void 236 xfs_efd_item_free(struct xfs_efd_log_item *efdp) 237 { 238 kvfree(efdp->efd_item.li_lv_shadow); 239 if (efdp->efd_format.efd_nextents > XFS_EFD_MAX_FAST_EXTENTS) 240 kfree(efdp); 241 else 242 kmem_cache_free(xfs_efd_cache, efdp); 243 } 244 245 STATIC void 246 xfs_efd_item_size( 247 struct xfs_log_item *lip, 248 int *nvecs, 249 int *nbytes) 250 { 251 struct xfs_efd_log_item *efdp = EFD_ITEM(lip); 252 253 *nvecs += 1; 254 *nbytes += xfs_efd_log_format_sizeof(efdp->efd_format.efd_nextents); 255 } 256 257 /* 258 * This is called to fill in the vector of log iovecs for the 259 * given efd log item. We use only 1 iovec, and we point that 260 * at the efd_log_format structure embedded in the efd item. 261 * It is at this point that we assert that all of the extent 262 * slots in the efd item have been filled. 263 */ 264 STATIC void 265 xfs_efd_item_format( 266 struct xfs_log_item *lip, 267 struct xfs_log_vec *lv) 268 { 269 struct xfs_efd_log_item *efdp = EFD_ITEM(lip); 270 struct xfs_log_iovec *vecp = NULL; 271 272 ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents); 273 ASSERT(lip->li_type == XFS_LI_EFD || lip->li_type == XFS_LI_EFD_RT); 274 275 efdp->efd_format.efd_type = lip->li_type; 276 efdp->efd_format.efd_size = 1; 277 278 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFD_FORMAT, &efdp->efd_format, 279 xfs_efd_log_format_sizeof(efdp->efd_format.efd_nextents)); 280 } 281 282 /* 283 * The EFD is either committed or aborted if the transaction is cancelled. If 284 * the transaction is cancelled, drop our reference to the EFI and free the EFD. 285 */ 286 STATIC void 287 xfs_efd_item_release( 288 struct xfs_log_item *lip) 289 { 290 struct xfs_efd_log_item *efdp = EFD_ITEM(lip); 291 292 xfs_efi_release(efdp->efd_efip); 293 xfs_efd_item_free(efdp); 294 } 295 296 static struct xfs_log_item * 297 xfs_efd_item_intent( 298 struct xfs_log_item *lip) 299 { 300 return &EFD_ITEM(lip)->efd_efip->efi_item; 301 } 302 303 static const struct xfs_item_ops xfs_efd_item_ops = { 304 .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED | 305 XFS_ITEM_INTENT_DONE, 306 .iop_size = xfs_efd_item_size, 307 .iop_format = xfs_efd_item_format, 308 .iop_release = xfs_efd_item_release, 309 .iop_intent = xfs_efd_item_intent, 310 }; 311 312 static inline struct xfs_extent_free_item *xefi_entry(const struct list_head *e) 313 { 314 return list_entry(e, struct xfs_extent_free_item, xefi_list); 315 } 316 317 static inline bool 318 xfs_efi_item_isrt(const struct xfs_log_item *lip) 319 { 320 ASSERT(lip->li_type == XFS_LI_EFI || lip->li_type == XFS_LI_EFI_RT); 321 322 return lip->li_type == XFS_LI_EFI_RT; 323 } 324 325 /* 326 * Fill the EFD with all extents from the EFI when we need to roll the 327 * transaction and continue with a new EFI. 328 * 329 * This simply copies all the extents in the EFI to the EFD rather than make 330 * assumptions about which extents in the EFI have already been processed. We 331 * currently keep the xefi list in the same order as the EFI extent list, but 332 * that may not always be the case. Copying everything avoids leaving a landmine 333 * were we fail to cancel all the extents in an EFI if the xefi list is 334 * processed in a different order to the extents in the EFI. 335 */ 336 static void 337 xfs_efd_from_efi( 338 struct xfs_efd_log_item *efdp) 339 { 340 struct xfs_efi_log_item *efip = efdp->efd_efip; 341 uint i; 342 343 ASSERT(efip->efi_format.efi_nextents > 0); 344 ASSERT(efdp->efd_next_extent < efip->efi_format.efi_nextents); 345 346 for (i = 0; i < efip->efi_format.efi_nextents; i++) { 347 efdp->efd_format.efd_extents[i] = 348 efip->efi_format.efi_extents[i]; 349 } 350 efdp->efd_next_extent = efip->efi_format.efi_nextents; 351 } 352 353 static void 354 xfs_efd_add_extent( 355 struct xfs_efd_log_item *efdp, 356 struct xfs_extent_free_item *xefi) 357 { 358 struct xfs_extent *extp; 359 360 ASSERT(efdp->efd_next_extent < efdp->efd_format.efd_nextents); 361 362 extp = &efdp->efd_format.efd_extents[efdp->efd_next_extent]; 363 extp->ext_start = xefi->xefi_startblock; 364 extp->ext_len = xefi->xefi_blockcount; 365 366 efdp->efd_next_extent++; 367 } 368 369 /* Sort bmap items by AG. */ 370 static int 371 xfs_extent_free_diff_items( 372 void *priv, 373 const struct list_head *a, 374 const struct list_head *b) 375 { 376 struct xfs_extent_free_item *ra = xefi_entry(a); 377 struct xfs_extent_free_item *rb = xefi_entry(b); 378 379 return ra->xefi_group->xg_gno - rb->xefi_group->xg_gno; 380 } 381 382 /* Log a free extent to the intent item. */ 383 STATIC void 384 xfs_extent_free_log_item( 385 struct xfs_trans *tp, 386 struct xfs_efi_log_item *efip, 387 struct xfs_extent_free_item *xefi) 388 { 389 uint next_extent; 390 struct xfs_extent *extp; 391 392 /* 393 * atomic_inc_return gives us the value after the increment; 394 * we want to use it as an array index so we need to subtract 1 from 395 * it. 396 */ 397 next_extent = atomic_inc_return(&efip->efi_next_extent) - 1; 398 ASSERT(next_extent < efip->efi_format.efi_nextents); 399 extp = &efip->efi_format.efi_extents[next_extent]; 400 extp->ext_start = xefi->xefi_startblock; 401 extp->ext_len = xefi->xefi_blockcount; 402 } 403 404 static struct xfs_log_item * 405 __xfs_extent_free_create_intent( 406 struct xfs_trans *tp, 407 struct list_head *items, 408 unsigned int count, 409 bool sort, 410 unsigned short item_type) 411 { 412 struct xfs_mount *mp = tp->t_mountp; 413 struct xfs_efi_log_item *efip; 414 struct xfs_extent_free_item *xefi; 415 416 ASSERT(count > 0); 417 418 efip = xfs_efi_init(mp, item_type, count); 419 if (sort) 420 list_sort(mp, items, xfs_extent_free_diff_items); 421 list_for_each_entry(xefi, items, xefi_list) 422 xfs_extent_free_log_item(tp, efip, xefi); 423 return &efip->efi_item; 424 } 425 426 static struct xfs_log_item * 427 xfs_extent_free_create_intent( 428 struct xfs_trans *tp, 429 struct list_head *items, 430 unsigned int count, 431 bool sort) 432 { 433 return __xfs_extent_free_create_intent(tp, items, count, sort, 434 XFS_LI_EFI); 435 } 436 437 static inline unsigned short 438 xfs_efd_type_from_efi(const struct xfs_efi_log_item *efip) 439 { 440 return xfs_efi_item_isrt(&efip->efi_item) ? XFS_LI_EFD_RT : XFS_LI_EFD; 441 } 442 443 /* Get an EFD so we can process all the free extents. */ 444 static struct xfs_log_item * 445 xfs_extent_free_create_done( 446 struct xfs_trans *tp, 447 struct xfs_log_item *intent, 448 unsigned int count) 449 { 450 struct xfs_efi_log_item *efip = EFI_ITEM(intent); 451 struct xfs_efd_log_item *efdp; 452 453 ASSERT(count > 0); 454 455 if (count > XFS_EFD_MAX_FAST_EXTENTS) { 456 efdp = kzalloc(xfs_efd_log_item_sizeof(count), 457 GFP_KERNEL | __GFP_NOFAIL); 458 } else { 459 efdp = kmem_cache_zalloc(xfs_efd_cache, 460 GFP_KERNEL | __GFP_NOFAIL); 461 } 462 463 xfs_log_item_init(tp->t_mountp, &efdp->efd_item, 464 xfs_efd_type_from_efi(efip), &xfs_efd_item_ops); 465 efdp->efd_efip = efip; 466 efdp->efd_format.efd_nextents = count; 467 efdp->efd_format.efd_efi_id = efip->efi_format.efi_id; 468 469 return &efdp->efd_item; 470 } 471 472 static inline const struct xfs_defer_op_type * 473 xefi_ops( 474 struct xfs_extent_free_item *xefi) 475 { 476 if (xfs_efi_is_realtime(xefi)) 477 return &xfs_rtextent_free_defer_type; 478 if (xefi->xefi_agresv == XFS_AG_RESV_AGFL) 479 return &xfs_agfl_free_defer_type; 480 return &xfs_extent_free_defer_type; 481 } 482 483 /* Add this deferred EFI to the transaction. */ 484 void 485 xfs_extent_free_defer_add( 486 struct xfs_trans *tp, 487 struct xfs_extent_free_item *xefi, 488 struct xfs_defer_pending **dfpp) 489 { 490 struct xfs_mount *mp = tp->t_mountp; 491 492 xefi->xefi_group = xfs_group_intent_get(mp, xefi->xefi_startblock, 493 xfs_efi_is_realtime(xefi) ? XG_TYPE_RTG : XG_TYPE_AG); 494 495 trace_xfs_extent_free_defer(mp, xefi); 496 *dfpp = xfs_defer_add(tp, &xefi->xefi_list, xefi_ops(xefi)); 497 } 498 499 /* Cancel a free extent. */ 500 STATIC void 501 xfs_extent_free_cancel_item( 502 struct list_head *item) 503 { 504 struct xfs_extent_free_item *xefi = xefi_entry(item); 505 506 xfs_group_intent_put(xefi->xefi_group); 507 kmem_cache_free(xfs_extfree_item_cache, xefi); 508 } 509 510 /* Process a free extent. */ 511 STATIC int 512 xfs_extent_free_finish_item( 513 struct xfs_trans *tp, 514 struct xfs_log_item *done, 515 struct list_head *item, 516 struct xfs_btree_cur **state) 517 { 518 struct xfs_owner_info oinfo = { }; 519 struct xfs_extent_free_item *xefi = xefi_entry(item); 520 struct xfs_efd_log_item *efdp = EFD_ITEM(done); 521 struct xfs_mount *mp = tp->t_mountp; 522 xfs_agblock_t agbno; 523 int error = 0; 524 525 agbno = XFS_FSB_TO_AGBNO(mp, xefi->xefi_startblock); 526 527 oinfo.oi_owner = xefi->xefi_owner; 528 if (xefi->xefi_flags & XFS_EFI_ATTR_FORK) 529 oinfo.oi_flags |= XFS_OWNER_INFO_ATTR_FORK; 530 if (xefi->xefi_flags & XFS_EFI_BMBT_BLOCK) 531 oinfo.oi_flags |= XFS_OWNER_INFO_BMBT_BLOCK; 532 533 trace_xfs_extent_free_deferred(mp, xefi); 534 535 /* 536 * If we need a new transaction to make progress, the caller will log a 537 * new EFI with the current contents. It will also log an EFD to cancel 538 * the existing EFI, and so we need to copy all the unprocessed extents 539 * in this EFI to the EFD so this works correctly. 540 */ 541 if (!(xefi->xefi_flags & XFS_EFI_CANCELLED)) 542 error = __xfs_free_extent(tp, to_perag(xefi->xefi_group), agbno, 543 xefi->xefi_blockcount, &oinfo, xefi->xefi_agresv, 544 xefi->xefi_flags & XFS_EFI_SKIP_DISCARD); 545 if (error == -EAGAIN) { 546 xfs_efd_from_efi(efdp); 547 return error; 548 } 549 550 xfs_efd_add_extent(efdp, xefi); 551 xfs_extent_free_cancel_item(item); 552 return error; 553 } 554 555 /* Abort all pending EFIs. */ 556 STATIC void 557 xfs_extent_free_abort_intent( 558 struct xfs_log_item *intent) 559 { 560 xfs_efi_release(EFI_ITEM(intent)); 561 } 562 563 /* 564 * AGFL blocks are accounted differently in the reserve pools and are not 565 * inserted into the busy extent list. 566 */ 567 STATIC int 568 xfs_agfl_free_finish_item( 569 struct xfs_trans *tp, 570 struct xfs_log_item *done, 571 struct list_head *item, 572 struct xfs_btree_cur **state) 573 { 574 struct xfs_owner_info oinfo = { }; 575 struct xfs_mount *mp = tp->t_mountp; 576 struct xfs_efd_log_item *efdp = EFD_ITEM(done); 577 struct xfs_extent_free_item *xefi = xefi_entry(item); 578 struct xfs_buf *agbp; 579 int error; 580 xfs_agblock_t agbno; 581 582 ASSERT(xefi->xefi_blockcount == 1); 583 agbno = XFS_FSB_TO_AGBNO(mp, xefi->xefi_startblock); 584 oinfo.oi_owner = xefi->xefi_owner; 585 586 trace_xfs_agfl_free_deferred(mp, xefi); 587 588 error = xfs_alloc_read_agf(to_perag(xefi->xefi_group), tp, 0, &agbp); 589 if (!error) 590 error = xfs_free_ag_extent(tp, agbp, agbno, 1, &oinfo, 591 XFS_AG_RESV_AGFL); 592 593 xfs_efd_add_extent(efdp, xefi); 594 xfs_extent_free_cancel_item(&xefi->xefi_list); 595 return error; 596 } 597 598 /* Is this recovered EFI ok? */ 599 static inline bool 600 xfs_efi_validate_ext( 601 struct xfs_mount *mp, 602 bool isrt, 603 struct xfs_extent *extp) 604 { 605 if (isrt) 606 return xfs_verify_rtbext(mp, extp->ext_start, extp->ext_len); 607 608 return xfs_verify_fsbext(mp, extp->ext_start, extp->ext_len); 609 } 610 611 static inline void 612 xfs_efi_recover_work( 613 struct xfs_mount *mp, 614 struct xfs_defer_pending *dfp, 615 bool isrt, 616 struct xfs_extent *extp) 617 { 618 struct xfs_extent_free_item *xefi; 619 620 xefi = kmem_cache_zalloc(xfs_extfree_item_cache, 621 GFP_KERNEL | __GFP_NOFAIL); 622 xefi->xefi_startblock = extp->ext_start; 623 xefi->xefi_blockcount = extp->ext_len; 624 xefi->xefi_agresv = XFS_AG_RESV_NONE; 625 xefi->xefi_owner = XFS_RMAP_OWN_UNKNOWN; 626 xefi->xefi_group = xfs_group_intent_get(mp, extp->ext_start, 627 isrt ? XG_TYPE_RTG : XG_TYPE_AG); 628 if (isrt) 629 xefi->xefi_flags |= XFS_EFI_REALTIME; 630 631 xfs_defer_add_item(dfp, &xefi->xefi_list); 632 } 633 634 /* 635 * Process an extent free intent item that was recovered from 636 * the log. We need to free the extents that it describes. 637 */ 638 STATIC int 639 xfs_extent_free_recover_work( 640 struct xfs_defer_pending *dfp, 641 struct list_head *capture_list) 642 { 643 struct xfs_trans_res resv; 644 struct xfs_log_item *lip = dfp->dfp_intent; 645 struct xfs_efi_log_item *efip = EFI_ITEM(lip); 646 struct xfs_mount *mp = lip->li_log->l_mp; 647 struct xfs_trans *tp; 648 int i; 649 int error = 0; 650 bool isrt = xfs_efi_item_isrt(lip); 651 652 /* 653 * First check the validity of the extents described by the EFI. If 654 * any are bad, then assume that all are bad and just toss the EFI. 655 * Mixing RT and non-RT extents in the same EFI item is not allowed. 656 */ 657 for (i = 0; i < efip->efi_format.efi_nextents; i++) { 658 if (!xfs_efi_validate_ext(mp, isrt, 659 &efip->efi_format.efi_extents[i])) { 660 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, 661 &efip->efi_format, 662 sizeof(efip->efi_format)); 663 return -EFSCORRUPTED; 664 } 665 666 xfs_efi_recover_work(mp, dfp, isrt, 667 &efip->efi_format.efi_extents[i]); 668 } 669 670 resv = xlog_recover_resv(&M_RES(mp)->tr_itruncate); 671 error = xfs_trans_alloc(mp, &resv, 0, 0, 0, &tp); 672 if (error) 673 return error; 674 675 error = xlog_recover_finish_intent(tp, dfp); 676 if (error == -EFSCORRUPTED) 677 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, 678 &efip->efi_format, 679 sizeof(efip->efi_format)); 680 if (error) 681 goto abort_error; 682 683 return xfs_defer_ops_capture_and_commit(tp, capture_list); 684 685 abort_error: 686 xfs_trans_cancel(tp); 687 return error; 688 } 689 690 /* Relog an intent item to push the log tail forward. */ 691 static struct xfs_log_item * 692 xfs_extent_free_relog_intent( 693 struct xfs_trans *tp, 694 struct xfs_log_item *intent, 695 struct xfs_log_item *done_item) 696 { 697 struct xfs_efd_log_item *efdp = EFD_ITEM(done_item); 698 struct xfs_efi_log_item *efip; 699 struct xfs_extent *extp; 700 unsigned int count; 701 702 count = EFI_ITEM(intent)->efi_format.efi_nextents; 703 extp = EFI_ITEM(intent)->efi_format.efi_extents; 704 705 ASSERT(intent->li_type == XFS_LI_EFI || intent->li_type == XFS_LI_EFI_RT); 706 707 efdp->efd_next_extent = count; 708 memcpy(efdp->efd_format.efd_extents, extp, count * sizeof(*extp)); 709 710 efip = xfs_efi_init(tp->t_mountp, intent->li_type, count); 711 memcpy(efip->efi_format.efi_extents, extp, count * sizeof(*extp)); 712 atomic_set(&efip->efi_next_extent, count); 713 714 return &efip->efi_item; 715 } 716 717 const struct xfs_defer_op_type xfs_extent_free_defer_type = { 718 .name = "extent_free", 719 .max_items = XFS_EFI_MAX_FAST_EXTENTS, 720 .create_intent = xfs_extent_free_create_intent, 721 .abort_intent = xfs_extent_free_abort_intent, 722 .create_done = xfs_extent_free_create_done, 723 .finish_item = xfs_extent_free_finish_item, 724 .cancel_item = xfs_extent_free_cancel_item, 725 .recover_work = xfs_extent_free_recover_work, 726 .relog_intent = xfs_extent_free_relog_intent, 727 }; 728 729 /* sub-type with special handling for AGFL deferred frees */ 730 const struct xfs_defer_op_type xfs_agfl_free_defer_type = { 731 .name = "agfl_free", 732 .max_items = XFS_EFI_MAX_FAST_EXTENTS, 733 .create_intent = xfs_extent_free_create_intent, 734 .abort_intent = xfs_extent_free_abort_intent, 735 .create_done = xfs_extent_free_create_done, 736 .finish_item = xfs_agfl_free_finish_item, 737 .cancel_item = xfs_extent_free_cancel_item, 738 .recover_work = xfs_extent_free_recover_work, 739 .relog_intent = xfs_extent_free_relog_intent, 740 }; 741 742 #ifdef CONFIG_XFS_RT 743 /* Create a realtime extent freeing */ 744 static struct xfs_log_item * 745 xfs_rtextent_free_create_intent( 746 struct xfs_trans *tp, 747 struct list_head *items, 748 unsigned int count, 749 bool sort) 750 { 751 return __xfs_extent_free_create_intent(tp, items, count, sort, 752 XFS_LI_EFI_RT); 753 } 754 755 /* Process a free realtime extent. */ 756 STATIC int 757 xfs_rtextent_free_finish_item( 758 struct xfs_trans *tp, 759 struct xfs_log_item *done, 760 struct list_head *item, 761 struct xfs_btree_cur **state) 762 { 763 struct xfs_mount *mp = tp->t_mountp; 764 struct xfs_extent_free_item *xefi = xefi_entry(item); 765 struct xfs_efd_log_item *efdp = EFD_ITEM(done); 766 struct xfs_rtgroup **rtgp = (struct xfs_rtgroup **)state; 767 int error = 0; 768 769 trace_xfs_extent_free_deferred(mp, xefi); 770 771 if (xefi->xefi_flags & XFS_EFI_CANCELLED) 772 goto done; 773 774 if (*rtgp != to_rtg(xefi->xefi_group)) { 775 unsigned int lock_flags; 776 777 if (xfs_has_zoned(mp)) 778 lock_flags = XFS_RTGLOCK_RMAP; 779 else 780 lock_flags = XFS_RTGLOCK_BITMAP; 781 782 *rtgp = to_rtg(xefi->xefi_group); 783 xfs_rtgroup_lock(*rtgp, lock_flags); 784 xfs_rtgroup_trans_join(tp, *rtgp, lock_flags); 785 } 786 787 if (xfs_has_zoned(mp)) { 788 error = xfs_zone_free_blocks(tp, *rtgp, xefi->xefi_startblock, 789 xefi->xefi_blockcount); 790 } else { 791 error = xfs_rtfree_blocks(tp, *rtgp, xefi->xefi_startblock, 792 xefi->xefi_blockcount); 793 } 794 795 if (error == -EAGAIN) { 796 xfs_efd_from_efi(efdp); 797 return error; 798 } 799 done: 800 xfs_efd_add_extent(efdp, xefi); 801 xfs_extent_free_cancel_item(item); 802 return error; 803 } 804 805 const struct xfs_defer_op_type xfs_rtextent_free_defer_type = { 806 .name = "rtextent_free", 807 .max_items = XFS_EFI_MAX_FAST_EXTENTS, 808 .create_intent = xfs_rtextent_free_create_intent, 809 .abort_intent = xfs_extent_free_abort_intent, 810 .create_done = xfs_extent_free_create_done, 811 .finish_item = xfs_rtextent_free_finish_item, 812 .cancel_item = xfs_extent_free_cancel_item, 813 .recover_work = xfs_extent_free_recover_work, 814 .relog_intent = xfs_extent_free_relog_intent, 815 }; 816 #else 817 const struct xfs_defer_op_type xfs_rtextent_free_defer_type = { 818 .name = "rtextent_free", 819 }; 820 #endif /* CONFIG_XFS_RT */ 821 822 STATIC bool 823 xfs_efi_item_match( 824 struct xfs_log_item *lip, 825 uint64_t intent_id) 826 { 827 return EFI_ITEM(lip)->efi_format.efi_id == intent_id; 828 } 829 830 static const struct xfs_item_ops xfs_efi_item_ops = { 831 .flags = XFS_ITEM_INTENT, 832 .iop_size = xfs_efi_item_size, 833 .iop_format = xfs_efi_item_format, 834 .iop_unpin = xfs_efi_item_unpin, 835 .iop_release = xfs_efi_item_release, 836 .iop_match = xfs_efi_item_match, 837 }; 838 839 /* 840 * This routine is called to create an in-core extent free intent 841 * item from the efi format structure which was logged on disk. 842 * It allocates an in-core efi, copies the extents from the format 843 * structure into it, and adds the efi to the AIL with the given 844 * LSN. 845 */ 846 STATIC int 847 xlog_recover_efi_commit_pass2( 848 struct xlog *log, 849 struct list_head *buffer_list, 850 struct xlog_recover_item *item, 851 xfs_lsn_t lsn) 852 { 853 struct xfs_mount *mp = log->l_mp; 854 struct xfs_efi_log_item *efip; 855 struct xfs_efi_log_format *efi_formatp; 856 int error; 857 858 efi_formatp = item->ri_buf[0].i_addr; 859 860 if (item->ri_buf[0].i_len < xfs_efi_log_format_sizeof(0)) { 861 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, 862 item->ri_buf[0].i_addr, item->ri_buf[0].i_len); 863 return -EFSCORRUPTED; 864 } 865 866 efip = xfs_efi_init(mp, ITEM_TYPE(item), efi_formatp->efi_nextents); 867 error = xfs_efi_copy_format(&item->ri_buf[0], &efip->efi_format); 868 if (error) { 869 xfs_efi_item_free(efip); 870 return error; 871 } 872 atomic_set(&efip->efi_next_extent, efi_formatp->efi_nextents); 873 874 xlog_recover_intent_item(log, &efip->efi_item, lsn, 875 &xfs_extent_free_defer_type); 876 return 0; 877 } 878 879 const struct xlog_recover_item_ops xlog_efi_item_ops = { 880 .item_type = XFS_LI_EFI, 881 .commit_pass2 = xlog_recover_efi_commit_pass2, 882 }; 883 884 #ifdef CONFIG_XFS_RT 885 STATIC int 886 xlog_recover_rtefi_commit_pass2( 887 struct xlog *log, 888 struct list_head *buffer_list, 889 struct xlog_recover_item *item, 890 xfs_lsn_t lsn) 891 { 892 struct xfs_mount *mp = log->l_mp; 893 struct xfs_efi_log_item *efip; 894 struct xfs_efi_log_format *efi_formatp; 895 int error; 896 897 efi_formatp = item->ri_buf[0].i_addr; 898 899 if (item->ri_buf[0].i_len < xfs_efi_log_format_sizeof(0)) { 900 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, 901 item->ri_buf[0].i_addr, item->ri_buf[0].i_len); 902 return -EFSCORRUPTED; 903 } 904 905 efip = xfs_efi_init(mp, ITEM_TYPE(item), efi_formatp->efi_nextents); 906 error = xfs_efi_copy_format(&item->ri_buf[0], &efip->efi_format); 907 if (error) { 908 xfs_efi_item_free(efip); 909 return error; 910 } 911 atomic_set(&efip->efi_next_extent, efi_formatp->efi_nextents); 912 913 xlog_recover_intent_item(log, &efip->efi_item, lsn, 914 &xfs_rtextent_free_defer_type); 915 return 0; 916 } 917 #else 918 STATIC int 919 xlog_recover_rtefi_commit_pass2( 920 struct xlog *log, 921 struct list_head *buffer_list, 922 struct xlog_recover_item *item, 923 xfs_lsn_t lsn) 924 { 925 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp, 926 item->ri_buf[0].i_addr, item->ri_buf[0].i_len); 927 return -EFSCORRUPTED; 928 } 929 #endif 930 931 const struct xlog_recover_item_ops xlog_rtefi_item_ops = { 932 .item_type = XFS_LI_EFI_RT, 933 .commit_pass2 = xlog_recover_rtefi_commit_pass2, 934 }; 935 936 /* 937 * This routine is called when an EFD format structure is found in a committed 938 * transaction in the log. Its purpose is to cancel the corresponding EFI if it 939 * was still in the log. To do this it searches the AIL for the EFI with an id 940 * equal to that in the EFD format structure. If we find it we drop the EFD 941 * reference, which removes the EFI from the AIL and frees it. 942 */ 943 STATIC int 944 xlog_recover_efd_commit_pass2( 945 struct xlog *log, 946 struct list_head *buffer_list, 947 struct xlog_recover_item *item, 948 xfs_lsn_t lsn) 949 { 950 struct xfs_efd_log_format *efd_formatp; 951 int buflen = item->ri_buf[0].i_len; 952 953 efd_formatp = item->ri_buf[0].i_addr; 954 955 if (buflen < sizeof(struct xfs_efd_log_format)) { 956 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp, 957 efd_formatp, buflen); 958 return -EFSCORRUPTED; 959 } 960 961 if (item->ri_buf[0].i_len != xfs_efd_log_format32_sizeof( 962 efd_formatp->efd_nextents) && 963 item->ri_buf[0].i_len != xfs_efd_log_format64_sizeof( 964 efd_formatp->efd_nextents)) { 965 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp, 966 efd_formatp, buflen); 967 return -EFSCORRUPTED; 968 } 969 970 xlog_recover_release_intent(log, XFS_LI_EFI, efd_formatp->efd_efi_id); 971 return 0; 972 } 973 974 const struct xlog_recover_item_ops xlog_efd_item_ops = { 975 .item_type = XFS_LI_EFD, 976 .commit_pass2 = xlog_recover_efd_commit_pass2, 977 }; 978 979 #ifdef CONFIG_XFS_RT 980 STATIC int 981 xlog_recover_rtefd_commit_pass2( 982 struct xlog *log, 983 struct list_head *buffer_list, 984 struct xlog_recover_item *item, 985 xfs_lsn_t lsn) 986 { 987 struct xfs_efd_log_format *efd_formatp; 988 int buflen = item->ri_buf[0].i_len; 989 990 efd_formatp = item->ri_buf[0].i_addr; 991 992 if (buflen < sizeof(struct xfs_efd_log_format)) { 993 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp, 994 efd_formatp, buflen); 995 return -EFSCORRUPTED; 996 } 997 998 if (item->ri_buf[0].i_len != xfs_efd_log_format32_sizeof( 999 efd_formatp->efd_nextents) && 1000 item->ri_buf[0].i_len != xfs_efd_log_format64_sizeof( 1001 efd_formatp->efd_nextents)) { 1002 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp, 1003 efd_formatp, buflen); 1004 return -EFSCORRUPTED; 1005 } 1006 1007 xlog_recover_release_intent(log, XFS_LI_EFI_RT, 1008 efd_formatp->efd_efi_id); 1009 return 0; 1010 } 1011 #else 1012 # define xlog_recover_rtefd_commit_pass2 xlog_recover_rtefi_commit_pass2 1013 #endif 1014 1015 const struct xlog_recover_item_ops xlog_rtefd_item_ops = { 1016 .item_type = XFS_LI_EFD_RT, 1017 .commit_pass2 = xlog_recover_rtefd_commit_pass2, 1018 }; 1019