1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Copyright (C) 2016 Oracle. All Rights Reserved. 4 * Author: Darrick J. Wong <darrick.wong@oracle.com> 5 */ 6 #include "xfs_platform.h" 7 #include "xfs_fs.h" 8 #include "xfs_shared.h" 9 #include "xfs_format.h" 10 #include "xfs_log_format.h" 11 #include "xfs_trans_resv.h" 12 #include "xfs_mount.h" 13 #include "xfs_defer.h" 14 #include "xfs_trans.h" 15 #include "xfs_trans_priv.h" 16 #include "xfs_buf_item.h" 17 #include "xfs_inode.h" 18 #include "xfs_inode_item.h" 19 #include "xfs_trace.h" 20 #include "xfs_icache.h" 21 #include "xfs_log.h" 22 #include "xfs_log_priv.h" 23 #include "xfs_rmap.h" 24 #include "xfs_refcount.h" 25 #include "xfs_bmap.h" 26 #include "xfs_alloc.h" 27 #include "xfs_buf.h" 28 #include "xfs_da_format.h" 29 #include "xfs_da_btree.h" 30 #include "xfs_attr.h" 31 #include "xfs_exchmaps.h" 32 33 static struct kmem_cache *xfs_defer_pending_cache; 34 35 /* 36 * Deferred Operations in XFS 37 * 38 * Due to the way locking rules work in XFS, certain transactions (block 39 * mapping and unmapping, typically) have permanent reservations so that 40 * we can roll the transaction to adhere to AG locking order rules and 41 * to unlock buffers between metadata updates. Prior to rmap/reflink, 42 * the mapping code had a mechanism to perform these deferrals for 43 * extents that were going to be freed; this code makes that facility 44 * more generic. 45 * 46 * When adding the reverse mapping and reflink features, it became 47 * necessary to perform complex remapping multi-transactions to comply 48 * with AG locking order rules, and to be able to spread a single 49 * refcount update operation (an operation on an n-block extent can 50 * update as many as n records!) among multiple transactions. XFS can 51 * roll a transaction to facilitate this, but using this facility 52 * requires us to log "intent" items in case log recovery needs to 53 * redo the operation, and to log "done" items to indicate that redo 54 * is not necessary. 55 * 56 * Deferred work is tracked in xfs_defer_pending items. Each pending 57 * item tracks one type of deferred work. Incoming work items (which 58 * have not yet had an intent logged) are attached to a pending item 59 * on the dop_intake list, where they wait for the caller to finish 60 * the deferred operations. 61 * 62 * Finishing a set of deferred operations is an involved process. To 63 * start, we define "rolling a deferred-op transaction" as follows: 64 * 65 * > For each xfs_defer_pending item on the dop_intake list, 66 * - Sort the work items in AG order. XFS locking 67 * order rules require us to lock buffers in AG order. 68 * - Create a log intent item for that type. 69 * - Attach it to the pending item. 70 * - Move the pending item from the dop_intake list to the 71 * dop_pending list. 72 * > Roll the transaction. 73 * 74 * NOTE: To avoid exceeding the transaction reservation, we limit the 75 * number of items that we attach to a given xfs_defer_pending. 76 * 77 * The actual finishing process looks like this: 78 * 79 * > For each xfs_defer_pending in the dop_pending list, 80 * - Roll the deferred-op transaction as above. 81 * - Create a log done item for that type, and attach it to the 82 * log intent item. 83 * - For each work item attached to the log intent item, 84 * * Perform the described action. 85 * * Attach the work item to the log done item. 86 * * If the result of doing the work was -EAGAIN, ->finish work 87 * wants a new transaction. See the "Requesting a Fresh 88 * Transaction while Finishing Deferred Work" section below for 89 * details. 90 * 91 * The key here is that we must log an intent item for all pending 92 * work items every time we roll the transaction, and that we must log 93 * a done item as soon as the work is completed. With this mechanism 94 * we can perform complex remapping operations, chaining intent items 95 * as needed. 96 * 97 * Requesting a Fresh Transaction while Finishing Deferred Work 98 * 99 * If ->finish_item decides that it needs a fresh transaction to 100 * finish the work, it must ask its caller (xfs_defer_finish) for a 101 * continuation. The most likely cause of this circumstance are the 102 * refcount adjust functions deciding that they've logged enough items 103 * to be at risk of exceeding the transaction reservation. 104 * 105 * To get a fresh transaction, we want to log the existing log done 106 * item to prevent the log intent item from replaying, immediately log 107 * a new log intent item with the unfinished work items, roll the 108 * transaction, and re-call ->finish_item wherever it left off. The 109 * log done item and the new log intent item must be in the same 110 * transaction or atomicity cannot be guaranteed; defer_finish ensures 111 * that this happens. 112 * 113 * This requires some coordination between ->finish_item and 114 * defer_finish. Upon deciding to request a new transaction, 115 * ->finish_item should update the current work item to reflect the 116 * unfinished work. Next, it should reset the log done item's list 117 * count to the number of items finished, and return -EAGAIN. 118 * defer_finish sees the -EAGAIN, logs the new log intent item 119 * with the remaining work items, and leaves the xfs_defer_pending 120 * item at the head of the dop_work queue. Then it rolls the 121 * transaction and picks up processing where it left off. It is 122 * required that ->finish_item must be careful to leave enough 123 * transaction reservation to fit the new log intent item. 124 * 125 * This is an example of remapping the extent (E, E+B) into file X at 126 * offset A and dealing with the extent (C, C+B) already being mapped 127 * there: 128 * +-------------------------------------------------+ 129 * | Unmap file X startblock C offset A length B | t0 130 * | Intent to reduce refcount for extent (C, B) | 131 * | Intent to remove rmap (X, C, A, B) | 132 * | Intent to free extent (D, 1) (bmbt block) | 133 * | Intent to map (X, A, B) at startblock E | 134 * +-------------------------------------------------+ 135 * | Map file X startblock E offset A length B | t1 136 * | Done mapping (X, E, A, B) | 137 * | Intent to increase refcount for extent (E, B) | 138 * | Intent to add rmap (X, E, A, B) | 139 * +-------------------------------------------------+ 140 * | Reduce refcount for extent (C, B) | t2 141 * | Done reducing refcount for extent (C, 9) | 142 * | Intent to reduce refcount for extent (C+9, B-9) | 143 * | (ran out of space after 9 refcount updates) | 144 * +-------------------------------------------------+ 145 * | Reduce refcount for extent (C+9, B+9) | t3 146 * | Done reducing refcount for extent (C+9, B-9) | 147 * | Increase refcount for extent (E, B) | 148 * | Done increasing refcount for extent (E, B) | 149 * | Intent to free extent (C, B) | 150 * | Intent to free extent (F, 1) (refcountbt block) | 151 * | Intent to remove rmap (F, 1, REFC) | 152 * +-------------------------------------------------+ 153 * | Remove rmap (X, C, A, B) | t4 154 * | Done removing rmap (X, C, A, B) | 155 * | Add rmap (X, E, A, B) | 156 * | Done adding rmap (X, E, A, B) | 157 * | Remove rmap (F, 1, REFC) | 158 * | Done removing rmap (F, 1, REFC) | 159 * +-------------------------------------------------+ 160 * | Free extent (C, B) | t5 161 * | Done freeing extent (C, B) | 162 * | Free extent (D, 1) | 163 * | Done freeing extent (D, 1) | 164 * | Free extent (F, 1) | 165 * | Done freeing extent (F, 1) | 166 * +-------------------------------------------------+ 167 * 168 * If we should crash before t2 commits, log recovery replays 169 * the following intent items: 170 * 171 * - Intent to reduce refcount for extent (C, B) 172 * - Intent to remove rmap (X, C, A, B) 173 * - Intent to free extent (D, 1) (bmbt block) 174 * - Intent to increase refcount for extent (E, B) 175 * - Intent to add rmap (X, E, A, B) 176 * 177 * In the process of recovering, it should also generate and take care 178 * of these intent items: 179 * 180 * - Intent to free extent (C, B) 181 * - Intent to free extent (F, 1) (refcountbt block) 182 * - Intent to remove rmap (F, 1, REFC) 183 * 184 * Note that the continuation requested between t2 and t3 is likely to 185 * reoccur. 186 */ 187 STATIC struct xfs_log_item * 188 xfs_defer_barrier_create_intent( 189 struct xfs_trans *tp, 190 struct list_head *items, 191 unsigned int count, 192 bool sort) 193 { 194 return NULL; 195 } 196 197 STATIC void 198 xfs_defer_barrier_abort_intent( 199 struct xfs_log_item *intent) 200 { 201 /* empty */ 202 } 203 204 STATIC struct xfs_log_item * 205 xfs_defer_barrier_create_done( 206 struct xfs_trans *tp, 207 struct xfs_log_item *intent, 208 unsigned int count) 209 { 210 return NULL; 211 } 212 213 STATIC int 214 xfs_defer_barrier_finish_item( 215 struct xfs_trans *tp, 216 struct xfs_log_item *done, 217 struct list_head *item, 218 struct xfs_btree_cur **state) 219 { 220 ASSERT(0); 221 return -EFSCORRUPTED; 222 } 223 224 STATIC void 225 xfs_defer_barrier_cancel_item( 226 struct list_head *item) 227 { 228 ASSERT(0); 229 } 230 231 static const struct xfs_defer_op_type xfs_barrier_defer_type = { 232 .name = "barrier", 233 .max_items = 1, 234 .create_intent = xfs_defer_barrier_create_intent, 235 .abort_intent = xfs_defer_barrier_abort_intent, 236 .create_done = xfs_defer_barrier_create_done, 237 .finish_item = xfs_defer_barrier_finish_item, 238 .cancel_item = xfs_defer_barrier_cancel_item, 239 }; 240 241 /* Create a log intent done item for a log intent item. */ 242 static inline void 243 xfs_defer_create_done( 244 struct xfs_trans *tp, 245 struct xfs_defer_pending *dfp) 246 { 247 struct xfs_log_item *lip; 248 249 /* If there is no log intent item, there can be no log done item. */ 250 if (!dfp->dfp_intent) 251 return; 252 253 /* 254 * Mark the transaction dirty, even on error. This ensures the 255 * transaction is aborted, which: 256 * 257 * 1.) releases the log intent item and frees the log done item 258 * 2.) shuts down the filesystem 259 */ 260 tp->t_flags |= XFS_TRANS_DIRTY; 261 lip = dfp->dfp_ops->create_done(tp, dfp->dfp_intent, dfp->dfp_count); 262 if (!lip) 263 return; 264 265 tp->t_flags |= XFS_TRANS_HAS_INTENT_DONE; 266 xfs_trans_add_item(tp, lip); 267 set_bit(XFS_LI_DIRTY, &lip->li_flags); 268 dfp->dfp_done = lip; 269 } 270 271 /* 272 * Ensure there's a log intent item associated with this deferred work item if 273 * the operation must be restarted on crash. Returns 1 if there's a log item; 274 * 0 if there isn't; or a negative errno. 275 */ 276 static int 277 xfs_defer_create_intent( 278 struct xfs_trans *tp, 279 struct xfs_defer_pending *dfp, 280 bool sort) 281 { 282 struct xfs_log_item *lip; 283 284 if (dfp->dfp_intent) 285 return 1; 286 287 lip = dfp->dfp_ops->create_intent(tp, &dfp->dfp_work, dfp->dfp_count, 288 sort); 289 if (!lip) 290 return 0; 291 if (IS_ERR(lip)) 292 return PTR_ERR(lip); 293 294 tp->t_flags |= XFS_TRANS_DIRTY; 295 xfs_trans_add_item(tp, lip); 296 set_bit(XFS_LI_DIRTY, &lip->li_flags); 297 dfp->dfp_intent = lip; 298 return 1; 299 } 300 301 /* 302 * For each pending item in the intake list, log its intent item and the 303 * associated extents, then add the entire intake list to the end of 304 * the pending list. 305 * 306 * Returns 1 if at least one log item was associated with the deferred work; 307 * 0 if there are no log items; or a negative errno. 308 */ 309 static int 310 xfs_defer_create_intents( 311 struct xfs_trans *tp) 312 { 313 struct xfs_defer_pending *dfp; 314 int ret = 0; 315 316 list_for_each_entry(dfp, &tp->t_dfops, dfp_list) { 317 int ret2; 318 319 trace_xfs_defer_create_intent(tp->t_mountp, dfp); 320 ret2 = xfs_defer_create_intent(tp, dfp, true); 321 if (ret2 < 0) 322 return ret2; 323 ret |= ret2; 324 } 325 return ret; 326 } 327 328 static inline void 329 xfs_defer_pending_abort( 330 struct xfs_mount *mp, 331 struct xfs_defer_pending *dfp) 332 { 333 trace_xfs_defer_pending_abort(mp, dfp); 334 335 if (dfp->dfp_intent && !dfp->dfp_done) { 336 dfp->dfp_ops->abort_intent(dfp->dfp_intent); 337 dfp->dfp_intent = NULL; 338 } 339 } 340 341 static inline void 342 xfs_defer_pending_cancel_work( 343 struct xfs_mount *mp, 344 struct xfs_defer_pending *dfp) 345 { 346 struct list_head *pwi; 347 struct list_head *n; 348 349 trace_xfs_defer_cancel_list(mp, dfp); 350 351 list_del(&dfp->dfp_list); 352 list_for_each_safe(pwi, n, &dfp->dfp_work) { 353 list_del(pwi); 354 dfp->dfp_count--; 355 trace_xfs_defer_cancel_item(mp, dfp, pwi); 356 dfp->dfp_ops->cancel_item(pwi); 357 } 358 ASSERT(dfp->dfp_count == 0); 359 kmem_cache_free(xfs_defer_pending_cache, dfp); 360 } 361 362 STATIC void 363 xfs_defer_pending_abort_list( 364 struct xfs_mount *mp, 365 struct list_head *dop_list) 366 { 367 struct xfs_defer_pending *dfp; 368 369 /* Abort intent items that don't have a done item. */ 370 list_for_each_entry(dfp, dop_list, dfp_list) 371 xfs_defer_pending_abort(mp, dfp); 372 } 373 374 /* Abort all the intents that were committed. */ 375 STATIC void 376 xfs_defer_trans_abort( 377 struct xfs_trans *tp, 378 struct list_head *dop_pending) 379 { 380 trace_xfs_defer_trans_abort(tp, _RET_IP_); 381 xfs_defer_pending_abort_list(tp->t_mountp, dop_pending); 382 } 383 384 /* 385 * Capture resources that the caller said not to release ("held") when the 386 * transaction commits. Caller is responsible for zero-initializing @dres. 387 */ 388 static int 389 xfs_defer_save_resources( 390 struct xfs_defer_resources *dres, 391 struct xfs_trans *tp) 392 { 393 struct xfs_buf_log_item *bli; 394 struct xfs_inode_log_item *ili; 395 struct xfs_log_item *lip; 396 397 BUILD_BUG_ON(NBBY * sizeof(dres->dr_ordered) < XFS_DEFER_OPS_NR_BUFS); 398 399 list_for_each_entry(lip, &tp->t_items, li_trans) { 400 switch (lip->li_type) { 401 case XFS_LI_BUF: 402 bli = container_of(lip, struct xfs_buf_log_item, 403 bli_item); 404 if (bli->bli_flags & XFS_BLI_HOLD) { 405 if (dres->dr_bufs >= XFS_DEFER_OPS_NR_BUFS) { 406 ASSERT(0); 407 return -EFSCORRUPTED; 408 } 409 if (bli->bli_flags & XFS_BLI_ORDERED) 410 dres->dr_ordered |= 411 (1U << dres->dr_bufs); 412 else 413 xfs_trans_dirty_buf(tp, bli->bli_buf); 414 dres->dr_bp[dres->dr_bufs++] = bli->bli_buf; 415 } 416 break; 417 case XFS_LI_INODE: 418 ili = container_of(lip, struct xfs_inode_log_item, 419 ili_item); 420 if (ili->ili_lock_flags == 0) { 421 if (dres->dr_inos >= XFS_DEFER_OPS_NR_INODES) { 422 ASSERT(0); 423 return -EFSCORRUPTED; 424 } 425 xfs_trans_log_inode(tp, ili->ili_inode, 426 XFS_ILOG_CORE); 427 dres->dr_ip[dres->dr_inos++] = ili->ili_inode; 428 } 429 break; 430 default: 431 break; 432 } 433 } 434 435 return 0; 436 } 437 438 /* Attach the held resources to the transaction. */ 439 static void 440 xfs_defer_restore_resources( 441 struct xfs_trans *tp, 442 struct xfs_defer_resources *dres) 443 { 444 unsigned short i; 445 446 /* Rejoin the joined inodes. */ 447 for (i = 0; i < dres->dr_inos; i++) 448 xfs_trans_ijoin(tp, dres->dr_ip[i], 0); 449 450 /* Rejoin the buffers and dirty them so the log moves forward. */ 451 for (i = 0; i < dres->dr_bufs; i++) { 452 xfs_trans_bjoin(tp, dres->dr_bp[i]); 453 if (dres->dr_ordered & (1U << i)) 454 xfs_trans_ordered_buf(tp, dres->dr_bp[i]); 455 xfs_trans_bhold(tp, dres->dr_bp[i]); 456 } 457 } 458 459 /* Roll a transaction so we can do some deferred op processing. */ 460 STATIC int 461 xfs_defer_trans_roll( 462 struct xfs_trans **tpp) 463 { 464 struct xfs_defer_resources dres = { }; 465 int error; 466 467 error = xfs_defer_save_resources(&dres, *tpp); 468 if (error) 469 return error; 470 471 trace_xfs_defer_trans_roll(*tpp, _RET_IP_); 472 473 /* 474 * Roll the transaction. Rolling always given a new transaction (even 475 * if committing the old one fails!) to hand back to the caller, so we 476 * join the held resources to the new transaction so that we always 477 * return with the held resources joined to @tpp, no matter what 478 * happened. 479 */ 480 error = xfs_trans_roll(tpp); 481 482 xfs_defer_restore_resources(*tpp, &dres); 483 484 if (error) 485 trace_xfs_defer_trans_roll_error(*tpp, error); 486 return error; 487 } 488 489 /* 490 * Free up any items left in the list. 491 */ 492 static void 493 xfs_defer_cancel_list( 494 struct xfs_mount *mp, 495 struct list_head *dop_list) 496 { 497 struct xfs_defer_pending *dfp; 498 struct xfs_defer_pending *pli; 499 500 /* 501 * Free the pending items. Caller should already have arranged 502 * for the intent items to be released. 503 */ 504 list_for_each_entry_safe(dfp, pli, dop_list, dfp_list) 505 xfs_defer_pending_cancel_work(mp, dfp); 506 } 507 508 static inline void 509 xfs_defer_relog_intent( 510 struct xfs_trans *tp, 511 struct xfs_defer_pending *dfp) 512 { 513 struct xfs_log_item *lip; 514 515 xfs_defer_create_done(tp, dfp); 516 517 lip = dfp->dfp_ops->relog_intent(tp, dfp->dfp_intent, dfp->dfp_done); 518 if (lip) { 519 xfs_trans_add_item(tp, lip); 520 set_bit(XFS_LI_DIRTY, &lip->li_flags); 521 } 522 dfp->dfp_done = NULL; 523 dfp->dfp_intent = lip; 524 } 525 526 /* 527 * Prevent a log intent item from pinning the tail of the log by logging a 528 * done item to release the intent item; and then log a new intent item. 529 * The caller should provide a fresh transaction and roll it after we're done. 530 */ 531 static void 532 xfs_defer_relog( 533 struct xfs_trans **tpp, 534 struct list_head *dfops) 535 { 536 struct xlog *log = (*tpp)->t_mountp->m_log; 537 struct xfs_defer_pending *dfp; 538 xfs_lsn_t threshold_lsn = NULLCOMMITLSN; 539 540 541 ASSERT((*tpp)->t_flags & XFS_TRANS_PERM_LOG_RES); 542 543 list_for_each_entry(dfp, dfops, dfp_list) { 544 /* 545 * If the log intent item for this deferred op is not a part of 546 * the current log checkpoint, relog the intent item to keep 547 * the log tail moving forward. We're ok with this being racy 548 * because an incorrect decision means we'll be a little slower 549 * at pushing the tail. 550 */ 551 if (dfp->dfp_intent == NULL || 552 xfs_log_item_in_current_chkpt(dfp->dfp_intent)) 553 continue; 554 555 /* 556 * Figure out where we need the tail to be in order to maintain 557 * the minimum required free space in the log. Only sample 558 * the log threshold once per call. 559 */ 560 if (threshold_lsn == NULLCOMMITLSN) { 561 threshold_lsn = xfs_ail_get_push_target(log->l_ailp); 562 if (threshold_lsn == NULLCOMMITLSN) 563 break; 564 } 565 if (XFS_LSN_CMP(dfp->dfp_intent->li_lsn, threshold_lsn) >= 0) 566 continue; 567 568 trace_xfs_defer_relog_intent((*tpp)->t_mountp, dfp); 569 XFS_STATS_INC((*tpp)->t_mountp, xs_defer_relog); 570 571 xfs_defer_relog_intent(*tpp, dfp); 572 } 573 } 574 575 /* 576 * Log an intent-done item for the first pending intent, and finish the work 577 * items. 578 */ 579 int 580 xfs_defer_finish_one( 581 struct xfs_trans *tp, 582 struct xfs_defer_pending *dfp) 583 { 584 const struct xfs_defer_op_type *ops = dfp->dfp_ops; 585 struct xfs_btree_cur *state = NULL; 586 struct list_head *li, *n; 587 int error = 0; 588 589 trace_xfs_defer_pending_finish(tp->t_mountp, dfp); 590 591 xfs_defer_create_done(tp, dfp); 592 list_for_each_safe(li, n, &dfp->dfp_work) { 593 list_del(li); 594 dfp->dfp_count--; 595 trace_xfs_defer_finish_item(tp->t_mountp, dfp, li); 596 error = ops->finish_item(tp, dfp->dfp_done, li, &state); 597 if (error == -EAGAIN) { 598 int ret; 599 600 /* 601 * Caller wants a fresh transaction; put the work item 602 * back on the list and log a new log intent item to 603 * replace the old one. See "Requesting a Fresh 604 * Transaction while Finishing Deferred Work" above. 605 */ 606 list_add(li, &dfp->dfp_work); 607 dfp->dfp_count++; 608 dfp->dfp_done = NULL; 609 dfp->dfp_intent = NULL; 610 ret = xfs_defer_create_intent(tp, dfp, false); 611 if (ret < 0) 612 error = ret; 613 } 614 615 if (error) 616 goto out; 617 } 618 619 /* Done with the dfp, free it. */ 620 list_del(&dfp->dfp_list); 621 kmem_cache_free(xfs_defer_pending_cache, dfp); 622 out: 623 if (ops->finish_cleanup) 624 ops->finish_cleanup(tp, state, error); 625 return error; 626 } 627 628 /* Move all paused deferred work from @tp to @paused_list. */ 629 static void 630 xfs_defer_isolate_paused( 631 struct xfs_trans *tp, 632 struct list_head *paused_list) 633 { 634 struct xfs_defer_pending *dfp; 635 struct xfs_defer_pending *pli; 636 637 list_for_each_entry_safe(dfp, pli, &tp->t_dfops, dfp_list) { 638 if (!(dfp->dfp_flags & XFS_DEFER_PAUSED)) 639 continue; 640 641 list_move_tail(&dfp->dfp_list, paused_list); 642 trace_xfs_defer_isolate_paused(tp->t_mountp, dfp); 643 } 644 } 645 646 /* 647 * Finish all the pending work. This involves logging intent items for 648 * any work items that wandered in since the last transaction roll (if 649 * one has even happened), rolling the transaction, and finishing the 650 * work items in the first item on the logged-and-pending list. 651 * 652 * If an inode is provided, relog it to the new transaction. 653 */ 654 int 655 xfs_defer_finish_noroll( 656 struct xfs_trans **tp) 657 { 658 struct xfs_defer_pending *dfp = NULL; 659 const char *what = "chain"; 660 int error = 0; 661 LIST_HEAD(dop_pending); 662 LIST_HEAD(dop_paused); 663 664 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES); 665 666 trace_xfs_defer_finish(*tp, _RET_IP_); 667 668 /* Until we run out of pending work to finish... */ 669 while (!list_empty(&dop_pending) || !list_empty(&(*tp)->t_dfops)) { 670 /* 671 * Deferred items that are created in the process of finishing 672 * other deferred work items should be queued at the head of 673 * the pending list, which puts them ahead of the deferred work 674 * that was created by the caller. This keeps the number of 675 * pending work items to a minimum, which decreases the amount 676 * of time that any one intent item can stick around in memory, 677 * pinning the log tail. 678 */ 679 int has_intents = xfs_defer_create_intents(*tp); 680 681 xfs_defer_isolate_paused(*tp, &dop_paused); 682 683 list_splice_init(&(*tp)->t_dfops, &dop_pending); 684 685 if (has_intents < 0) { 686 error = has_intents; 687 goto out_shutdown; 688 } 689 if (has_intents || dfp) { 690 error = xfs_defer_trans_roll(tp); 691 if (error) 692 goto out_shutdown; 693 694 /* Relog intent items to keep the log moving. */ 695 xfs_defer_relog(tp, &dop_pending); 696 xfs_defer_relog(tp, &dop_paused); 697 698 if ((*tp)->t_flags & XFS_TRANS_DIRTY) { 699 error = xfs_defer_trans_roll(tp); 700 if (error) 701 goto out_shutdown; 702 } 703 } 704 705 dfp = list_first_entry_or_null(&dop_pending, 706 struct xfs_defer_pending, dfp_list); 707 if (!dfp) 708 break; 709 what = dfp->dfp_ops->name; 710 error = xfs_defer_finish_one(*tp, dfp); 711 if (error && error != -EAGAIN) 712 goto out_shutdown; 713 /* 714 * A finished item is no longer a candidate for a later 715 * failure. An -EAGAIN one is not finished, so it keeps the 716 * attribution across the roll that completes it. 717 */ 718 if (!error) 719 what = "chain"; 720 } 721 722 /* Requeue the paused items in the outgoing transaction. */ 723 list_splice_tail_init(&dop_paused, &(*tp)->t_dfops); 724 725 trace_xfs_defer_finish_done(*tp, _RET_IP_); 726 return 0; 727 728 out_shutdown: 729 list_splice_tail_init(&dop_paused, &dop_pending); 730 xfs_defer_trans_abort(*tp, &dop_pending); 731 trace_xfs_defer_finish_error(*tp, error); 732 if (!xfs_is_shutdown((*tp)->t_mountp)) 733 xfs_alert((*tp)->t_mountp, 734 "deferred %s work failed, error %d, %u blocks reserved", 735 what, error, (*tp)->t_blk_res); 736 xfs_force_shutdown((*tp)->t_mountp, SHUTDOWN_CORRUPT_INCORE); 737 xfs_defer_cancel_list((*tp)->t_mountp, &dop_pending); 738 xfs_defer_cancel(*tp); 739 return error; 740 } 741 742 int 743 xfs_defer_finish( 744 struct xfs_trans **tp) 745 { 746 #ifdef DEBUG 747 struct xfs_defer_pending *dfp; 748 #endif 749 int error; 750 751 /* 752 * Finish and roll the transaction once more to avoid returning to the 753 * caller with a dirty transaction. 754 */ 755 error = xfs_defer_finish_noroll(tp); 756 if (error) 757 return error; 758 if ((*tp)->t_flags & XFS_TRANS_DIRTY) { 759 error = xfs_defer_trans_roll(tp); 760 if (error) { 761 xfs_force_shutdown((*tp)->t_mountp, 762 SHUTDOWN_CORRUPT_INCORE); 763 return error; 764 } 765 } 766 767 /* Reset LOWMODE now that we've finished all the dfops. */ 768 #ifdef DEBUG 769 list_for_each_entry(dfp, &(*tp)->t_dfops, dfp_list) 770 ASSERT(dfp->dfp_flags & XFS_DEFER_PAUSED); 771 #endif 772 (*tp)->t_flags &= ~XFS_TRANS_LOWMODE; 773 return 0; 774 } 775 776 void 777 xfs_defer_cancel( 778 struct xfs_trans *tp) 779 { 780 struct xfs_mount *mp = tp->t_mountp; 781 782 trace_xfs_defer_cancel(tp, _RET_IP_); 783 xfs_defer_trans_abort(tp, &tp->t_dfops); 784 xfs_defer_cancel_list(mp, &tp->t_dfops); 785 } 786 787 /* 788 * Return the last pending work item attached to this transaction if it matches 789 * the deferred op type. 790 */ 791 static inline struct xfs_defer_pending * 792 xfs_defer_find_last( 793 struct xfs_trans *tp, 794 const struct xfs_defer_op_type *ops) 795 { 796 struct xfs_defer_pending *dfp = NULL; 797 798 /* No dfops at all? */ 799 if (list_empty(&tp->t_dfops)) 800 return NULL; 801 802 dfp = list_last_entry(&tp->t_dfops, struct xfs_defer_pending, 803 dfp_list); 804 805 /* Wrong type? */ 806 if (dfp->dfp_ops != ops) 807 return NULL; 808 return dfp; 809 } 810 811 /* 812 * Decide if we can add a deferred work item to the last dfops item attached 813 * to the transaction. 814 */ 815 static inline bool 816 xfs_defer_can_append( 817 struct xfs_defer_pending *dfp, 818 const struct xfs_defer_op_type *ops) 819 { 820 /* Already logged? */ 821 if (dfp->dfp_intent) 822 return false; 823 824 /* Paused items cannot absorb more work */ 825 if (dfp->dfp_flags & XFS_DEFER_PAUSED) 826 return false; 827 828 /* Already full? */ 829 if (ops->max_items && dfp->dfp_count >= ops->max_items) 830 return false; 831 832 return true; 833 } 834 835 /* Create a new pending item at the end of the transaction list. */ 836 static inline struct xfs_defer_pending * 837 xfs_defer_alloc( 838 struct list_head *dfops, 839 const struct xfs_defer_op_type *ops) 840 { 841 struct xfs_defer_pending *dfp; 842 843 dfp = kmem_cache_zalloc(xfs_defer_pending_cache, 844 GFP_KERNEL | __GFP_NOFAIL); 845 dfp->dfp_ops = ops; 846 INIT_LIST_HEAD(&dfp->dfp_work); 847 list_add_tail(&dfp->dfp_list, dfops); 848 849 return dfp; 850 } 851 852 /* Add an item for later deferred processing. */ 853 struct xfs_defer_pending * 854 xfs_defer_add( 855 struct xfs_trans *tp, 856 struct list_head *li, 857 const struct xfs_defer_op_type *ops) 858 { 859 struct xfs_defer_pending *dfp = NULL; 860 861 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES); 862 863 if (!ops->finish_item) { 864 ASSERT(ops->finish_item != NULL); 865 xfs_force_shutdown(tp->t_mountp, SHUTDOWN_CORRUPT_INCORE); 866 return NULL; 867 } 868 869 dfp = xfs_defer_find_last(tp, ops); 870 if (!dfp || !xfs_defer_can_append(dfp, ops)) 871 dfp = xfs_defer_alloc(&tp->t_dfops, ops); 872 873 xfs_defer_add_item(dfp, li); 874 trace_xfs_defer_add_item(tp->t_mountp, dfp, li); 875 return dfp; 876 } 877 878 /* 879 * Add a defer ops barrier to force two otherwise adjacent deferred work items 880 * to be tracked separately and have separate log items. 881 */ 882 void 883 xfs_defer_add_barrier( 884 struct xfs_trans *tp) 885 { 886 struct xfs_defer_pending *dfp; 887 888 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES); 889 890 /* If the last defer op added was a barrier, we're done. */ 891 dfp = xfs_defer_find_last(tp, &xfs_barrier_defer_type); 892 if (dfp) 893 return; 894 895 dfp = xfs_defer_alloc(&tp->t_dfops, &xfs_barrier_defer_type); 896 897 trace_xfs_defer_add_item(tp->t_mountp, dfp, NULL); 898 } 899 900 /* 901 * Create a pending deferred work item to replay the recovered intent item 902 * and add it to the list. 903 */ 904 void 905 xfs_defer_start_recovery( 906 struct xfs_log_item *lip, 907 struct list_head *r_dfops, 908 const struct xfs_defer_op_type *ops) 909 { 910 struct xfs_defer_pending *dfp = xfs_defer_alloc(r_dfops, ops); 911 912 dfp->dfp_intent = lip; 913 } 914 915 /* 916 * Cancel a deferred work item created to recover a log intent item. @dfp 917 * will be freed after this function returns. 918 */ 919 void 920 xfs_defer_cancel_recovery( 921 struct xfs_mount *mp, 922 struct xfs_defer_pending *dfp) 923 { 924 xfs_defer_pending_abort(mp, dfp); 925 xfs_defer_pending_cancel_work(mp, dfp); 926 } 927 928 /* Replay the deferred work item created from a recovered log intent item. */ 929 int 930 xfs_defer_finish_recovery( 931 struct xfs_mount *mp, 932 struct xfs_defer_pending *dfp, 933 struct list_head *capture_list) 934 { 935 const struct xfs_defer_op_type *ops = dfp->dfp_ops; 936 int error; 937 938 /* dfp is freed by recover_work and must not be accessed afterwards */ 939 error = ops->recover_work(dfp, capture_list); 940 if (error) 941 trace_xlog_intent_recovery_failed(mp, ops, error); 942 return error; 943 } 944 945 /* 946 * Move deferred ops from one transaction to another and reset the source to 947 * initial state. This is primarily used to carry state forward across 948 * transaction rolls with pending dfops. 949 */ 950 void 951 xfs_defer_move( 952 struct xfs_trans *dtp, 953 struct xfs_trans *stp) 954 { 955 list_splice_init(&stp->t_dfops, &dtp->t_dfops); 956 957 /* 958 * Low free space mode was historically controlled by a dfops field. 959 * This meant that low mode state potentially carried across multiple 960 * transaction rolls. Transfer low mode on a dfops move to preserve 961 * that behavior. 962 */ 963 dtp->t_flags |= (stp->t_flags & XFS_TRANS_LOWMODE); 964 stp->t_flags &= ~XFS_TRANS_LOWMODE; 965 } 966 967 /* 968 * Prepare a chain of fresh deferred ops work items to be completed later. Log 969 * recovery requires the ability to put off until later the actual finishing 970 * work so that it can process unfinished items recovered from the log in 971 * correct order. 972 * 973 * Create and log intent items for all the work that we're capturing so that we 974 * can be assured that the items will get replayed if the system goes down 975 * before log recovery gets a chance to finish the work it put off. The entire 976 * deferred ops state is transferred to the capture structure and the 977 * transaction is then ready for the caller to commit it. If there are no 978 * intent items to capture, this function returns NULL. 979 * 980 * If capture_ip is not NULL, the capture structure will obtain an extra 981 * reference to the inode. 982 */ 983 static struct xfs_defer_capture * 984 xfs_defer_ops_capture( 985 struct xfs_trans *tp) 986 { 987 struct xfs_defer_capture *dfc; 988 unsigned short i; 989 int error; 990 991 if (list_empty(&tp->t_dfops)) 992 return NULL; 993 994 error = xfs_defer_create_intents(tp); 995 if (error < 0) 996 return ERR_PTR(error); 997 998 /* Create an object to capture the defer ops. */ 999 dfc = kzalloc_obj(*dfc, GFP_KERNEL | __GFP_NOFAIL); 1000 INIT_LIST_HEAD(&dfc->dfc_list); 1001 INIT_LIST_HEAD(&dfc->dfc_dfops); 1002 1003 /* Move the dfops chain and transaction state to the capture struct. */ 1004 list_splice_init(&tp->t_dfops, &dfc->dfc_dfops); 1005 dfc->dfc_tpflags = tp->t_flags & XFS_TRANS_LOWMODE; 1006 tp->t_flags &= ~XFS_TRANS_LOWMODE; 1007 1008 /* Capture the remaining block reservations along with the dfops. */ 1009 dfc->dfc_blkres = tp->t_blk_res - tp->t_blk_res_used; 1010 dfc->dfc_rtxres = tp->t_rtx_res - tp->t_rtx_res_used; 1011 1012 /* Preserve the log reservation size. */ 1013 dfc->dfc_logres = tp->t_log_res; 1014 1015 error = xfs_defer_save_resources(&dfc->dfc_held, tp); 1016 if (error) { 1017 /* 1018 * Resource capture should never fail, but if it does, we 1019 * still have to shut down the log and release things 1020 * properly. 1021 */ 1022 xfs_force_shutdown(tp->t_mountp, SHUTDOWN_CORRUPT_INCORE); 1023 } 1024 1025 /* 1026 * Grab extra references to the inodes and buffers because callers are 1027 * expected to release their held references after we commit the 1028 * transaction. 1029 */ 1030 for (i = 0; i < dfc->dfc_held.dr_inos; i++) { 1031 xfs_assert_ilocked(dfc->dfc_held.dr_ip[i], XFS_ILOCK_EXCL); 1032 ihold(VFS_I(dfc->dfc_held.dr_ip[i])); 1033 } 1034 1035 for (i = 0; i < dfc->dfc_held.dr_bufs; i++) 1036 xfs_buf_hold(dfc->dfc_held.dr_bp[i]); 1037 1038 return dfc; 1039 } 1040 1041 /* Release all resources that we used to capture deferred ops. */ 1042 void 1043 xfs_defer_ops_capture_abort( 1044 struct xfs_mount *mp, 1045 struct xfs_defer_capture *dfc) 1046 { 1047 unsigned short i; 1048 1049 xfs_defer_pending_abort_list(mp, &dfc->dfc_dfops); 1050 xfs_defer_cancel_list(mp, &dfc->dfc_dfops); 1051 1052 for (i = 0; i < dfc->dfc_held.dr_bufs; i++) 1053 xfs_buf_relse(dfc->dfc_held.dr_bp[i]); 1054 1055 for (i = 0; i < dfc->dfc_held.dr_inos; i++) 1056 xfs_irele(dfc->dfc_held.dr_ip[i]); 1057 1058 kfree(dfc); 1059 } 1060 1061 /* 1062 * Capture any deferred ops and commit the transaction. This is the last step 1063 * needed to finish a log intent item that we recovered from the log. If any 1064 * of the deferred ops operate on an inode, the caller must pass in that inode 1065 * so that the reference can be transferred to the capture structure. The 1066 * caller must hold ILOCK_EXCL on the inode, and must unlock it before calling 1067 * xfs_defer_ops_continue. 1068 */ 1069 int 1070 xfs_defer_ops_capture_and_commit( 1071 struct xfs_trans *tp, 1072 struct list_head *capture_list) 1073 { 1074 struct xfs_mount *mp = tp->t_mountp; 1075 struct xfs_defer_capture *dfc; 1076 int error; 1077 1078 /* If we don't capture anything, commit transaction and exit. */ 1079 dfc = xfs_defer_ops_capture(tp); 1080 if (IS_ERR(dfc)) { 1081 xfs_trans_cancel(tp); 1082 return PTR_ERR(dfc); 1083 } 1084 if (!dfc) 1085 return xfs_trans_commit(tp); 1086 1087 /* Commit the transaction and add the capture structure to the list. */ 1088 error = xfs_trans_commit(tp); 1089 if (error) { 1090 xfs_defer_ops_capture_abort(mp, dfc); 1091 return error; 1092 } 1093 1094 list_add_tail(&dfc->dfc_list, capture_list); 1095 return 0; 1096 } 1097 1098 /* 1099 * Attach a chain of captured deferred ops to a new transaction and free the 1100 * capture structure. If an inode was captured, it will be passed back to the 1101 * caller with ILOCK_EXCL held and joined to the transaction with lockflags==0. 1102 * The caller now owns the inode reference. 1103 */ 1104 void 1105 xfs_defer_ops_continue( 1106 struct xfs_defer_capture *dfc, 1107 struct xfs_trans *tp, 1108 struct xfs_defer_resources *dres) 1109 { 1110 unsigned int i; 1111 1112 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES); 1113 ASSERT(!(tp->t_flags & XFS_TRANS_DIRTY)); 1114 1115 /* Lock the captured resources to the new transaction. */ 1116 if (dfc->dfc_held.dr_inos > 2) { 1117 xfs_sort_inodes(dfc->dfc_held.dr_ip, dfc->dfc_held.dr_inos); 1118 xfs_lock_inodes(dfc->dfc_held.dr_ip, dfc->dfc_held.dr_inos, 1119 XFS_ILOCK_EXCL); 1120 } else if (dfc->dfc_held.dr_inos == 2) 1121 xfs_lock_two_inodes(dfc->dfc_held.dr_ip[0], XFS_ILOCK_EXCL, 1122 dfc->dfc_held.dr_ip[1], XFS_ILOCK_EXCL); 1123 else if (dfc->dfc_held.dr_inos == 1) 1124 xfs_ilock(dfc->dfc_held.dr_ip[0], XFS_ILOCK_EXCL); 1125 1126 for (i = 0; i < dfc->dfc_held.dr_bufs; i++) 1127 xfs_buf_lock(dfc->dfc_held.dr_bp[i]); 1128 1129 /* Join the captured resources to the new transaction. */ 1130 xfs_defer_restore_resources(tp, &dfc->dfc_held); 1131 memcpy(dres, &dfc->dfc_held, sizeof(struct xfs_defer_resources)); 1132 dres->dr_bufs = 0; 1133 1134 /* Move captured dfops chain and state to the transaction. */ 1135 list_splice_init(&dfc->dfc_dfops, &tp->t_dfops); 1136 tp->t_flags |= dfc->dfc_tpflags; 1137 1138 kfree(dfc); 1139 } 1140 1141 /* Release the resources captured and continued during recovery. */ 1142 void 1143 xfs_defer_resources_rele( 1144 struct xfs_defer_resources *dres) 1145 { 1146 unsigned short i; 1147 1148 for (i = 0; i < dres->dr_inos; i++) { 1149 xfs_iunlock(dres->dr_ip[i], XFS_ILOCK_EXCL); 1150 xfs_irele(dres->dr_ip[i]); 1151 dres->dr_ip[i] = NULL; 1152 } 1153 1154 for (i = 0; i < dres->dr_bufs; i++) { 1155 xfs_buf_relse(dres->dr_bp[i]); 1156 dres->dr_bp[i] = NULL; 1157 } 1158 1159 dres->dr_inos = 0; 1160 dres->dr_bufs = 0; 1161 dres->dr_ordered = 0; 1162 } 1163 1164 static inline int __init 1165 xfs_defer_init_cache(void) 1166 { 1167 xfs_defer_pending_cache = kmem_cache_create("xfs_defer_pending", 1168 sizeof(struct xfs_defer_pending), 1169 0, 0, NULL); 1170 1171 return xfs_defer_pending_cache != NULL ? 0 : -ENOMEM; 1172 } 1173 1174 static inline void 1175 xfs_defer_destroy_cache(void) 1176 { 1177 kmem_cache_destroy(xfs_defer_pending_cache); 1178 xfs_defer_pending_cache = NULL; 1179 } 1180 1181 /* Set up caches for deferred work items. */ 1182 int __init 1183 xfs_defer_init_item_caches(void) 1184 { 1185 int error; 1186 1187 error = xfs_defer_init_cache(); 1188 if (error) 1189 return error; 1190 error = xfs_rmap_intent_init_cache(); 1191 if (error) 1192 goto err; 1193 error = xfs_refcount_intent_init_cache(); 1194 if (error) 1195 goto err; 1196 error = xfs_bmap_intent_init_cache(); 1197 if (error) 1198 goto err; 1199 error = xfs_extfree_intent_init_cache(); 1200 if (error) 1201 goto err; 1202 error = xfs_attr_intent_init_cache(); 1203 if (error) 1204 goto err; 1205 error = xfs_exchmaps_intent_init_cache(); 1206 if (error) 1207 goto err; 1208 1209 return 0; 1210 err: 1211 xfs_defer_destroy_item_caches(); 1212 return error; 1213 } 1214 1215 /* Destroy all the deferred work item caches, if they've been allocated. */ 1216 void 1217 xfs_defer_destroy_item_caches(void) 1218 { 1219 xfs_exchmaps_intent_destroy_cache(); 1220 xfs_attr_intent_destroy_cache(); 1221 xfs_extfree_intent_destroy_cache(); 1222 xfs_bmap_intent_destroy_cache(); 1223 xfs_refcount_intent_destroy_cache(); 1224 xfs_rmap_intent_destroy_cache(); 1225 xfs_defer_destroy_cache(); 1226 } 1227 1228 /* 1229 * Mark a deferred work item so that it will be requeued indefinitely without 1230 * being finished. Caller must ensure there are no data dependencies on this 1231 * work item in the meantime. 1232 */ 1233 void 1234 xfs_defer_item_pause( 1235 struct xfs_trans *tp, 1236 struct xfs_defer_pending *dfp) 1237 { 1238 ASSERT(!(dfp->dfp_flags & XFS_DEFER_PAUSED)); 1239 1240 dfp->dfp_flags |= XFS_DEFER_PAUSED; 1241 1242 trace_xfs_defer_item_pause(tp->t_mountp, dfp); 1243 } 1244 1245 /* 1246 * Release a paused deferred work item so that it will be finished during the 1247 * next transaction roll. 1248 */ 1249 void 1250 xfs_defer_item_unpause( 1251 struct xfs_trans *tp, 1252 struct xfs_defer_pending *dfp) 1253 { 1254 ASSERT(dfp->dfp_flags & XFS_DEFER_PAUSED); 1255 1256 dfp->dfp_flags &= ~XFS_DEFER_PAUSED; 1257 1258 trace_xfs_defer_item_unpause(tp->t_mountp, dfp); 1259 } 1260