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