1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2000-2005 Silicon Graphics, Inc. 4 * All Rights Reserved. 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_errortag.h" 14 #include "xfs_error.h" 15 #include "xfs_trans.h" 16 #include "xfs_trans_priv.h" 17 #include "xfs_log.h" 18 #include "xfs_log_priv.h" 19 #include "xfs_trace.h" 20 #include "xfs_sysfs.h" 21 #include "xfs_sb.h" 22 #include "xfs_health.h" 23 #include "xfs_zone_alloc.h" 24 25 struct xlog_write_data { 26 struct xlog_ticket *ticket; 27 struct xlog_in_core *iclog; 28 uint32_t bytes_left; 29 uint32_t record_cnt; 30 uint32_t data_cnt; 31 int log_offset; 32 }; 33 34 struct kmem_cache *xfs_log_ticket_cache; 35 36 /* Local miscellaneous function prototypes */ 37 STATIC struct xlog * 38 xlog_alloc_log( 39 struct xfs_mount *mp, 40 struct xfs_buftarg *log_target, 41 xfs_daddr_t blk_offset, 42 int num_bblks); 43 STATIC void 44 xlog_dealloc_log( 45 struct xlog *log); 46 47 /* local state machine functions */ 48 STATIC void xlog_state_done_syncing( 49 struct xlog_in_core *iclog); 50 STATIC void xlog_state_do_callback( 51 struct xlog *log); 52 STATIC int 53 xlog_state_get_iclog_space( 54 struct xlog *log, 55 struct xlog_write_data *data); 56 STATIC void 57 xlog_sync( 58 struct xlog *log, 59 struct xlog_in_core *iclog, 60 struct xlog_ticket *ticket); 61 #if defined(DEBUG) 62 STATIC void 63 xlog_verify_iclog( 64 struct xlog *log, 65 struct xlog_in_core *iclog, 66 int count); 67 STATIC void 68 xlog_verify_tail_lsn( 69 struct xlog *log, 70 struct xlog_in_core *iclog); 71 #else 72 #define xlog_verify_iclog(a,b,c) 73 #define xlog_verify_tail_lsn(a,b) 74 #endif 75 76 STATIC int 77 xlog_iclogs_empty( 78 struct xlog *log); 79 80 static int 81 xfs_log_cover(struct xfs_mount *); 82 83 static inline void 84 xlog_grant_sub_space( 85 struct xlog_grant_head *head, 86 int64_t bytes) 87 { 88 atomic64_sub(bytes, &head->grant); 89 } 90 91 static inline void 92 xlog_grant_add_space( 93 struct xlog_grant_head *head, 94 int64_t bytes) 95 { 96 atomic64_add(bytes, &head->grant); 97 } 98 99 static void 100 xlog_grant_head_init( 101 struct xlog_grant_head *head) 102 { 103 atomic64_set(&head->grant, 0); 104 INIT_LIST_HEAD(&head->waiters); 105 spin_lock_init(&head->lock); 106 } 107 108 void 109 xlog_grant_return_space( 110 struct xlog *log, 111 xfs_lsn_t old_head, 112 xfs_lsn_t new_head) 113 { 114 int64_t diff = xlog_lsn_sub(log, new_head, old_head); 115 116 xlog_grant_sub_space(&log->l_reserve_head, diff); 117 xlog_grant_sub_space(&log->l_write_head, diff); 118 } 119 120 /* 121 * Return the space in the log between the tail and the head. In the case where 122 * we have overrun available reservation space, return 0. The memory barrier 123 * pairs with the smp_wmb() in xlog_cil_ail_insert() to ensure that grant head 124 * vs tail space updates are seen in the correct order and hence avoid 125 * transients as space is transferred from the grant heads to the AIL on commit 126 * completion. 127 */ 128 static uint64_t 129 xlog_grant_space_left( 130 struct xlog *log, 131 struct xlog_grant_head *head) 132 { 133 int64_t free_bytes; 134 135 smp_rmb(); /* paired with smp_wmb in xlog_cil_ail_insert() */ 136 free_bytes = log->l_logsize - READ_ONCE(log->l_tail_space) - 137 atomic64_read(&head->grant); 138 if (free_bytes > 0) 139 return free_bytes; 140 return 0; 141 } 142 143 STATIC void 144 xlog_grant_head_wake_all( 145 struct xlog_grant_head *head) 146 { 147 struct xlog_ticket *tic; 148 149 spin_lock(&head->lock); 150 list_for_each_entry(tic, &head->waiters, t_queue) 151 wake_up_process(tic->t_task); 152 spin_unlock(&head->lock); 153 } 154 155 static inline int 156 xlog_ticket_reservation( 157 struct xlog *log, 158 struct xlog_grant_head *head, 159 struct xlog_ticket *tic) 160 { 161 if (head == &log->l_write_head) { 162 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV); 163 return tic->t_unit_res; 164 } 165 166 if (tic->t_flags & XLOG_TIC_PERM_RESERV) 167 return tic->t_unit_res * tic->t_cnt; 168 169 return tic->t_unit_res; 170 } 171 172 STATIC bool 173 xlog_grant_head_wake( 174 struct xlog *log, 175 struct xlog_grant_head *head, 176 int *free_bytes) 177 { 178 struct xlog_ticket *tic; 179 int need_bytes; 180 181 list_for_each_entry(tic, &head->waiters, t_queue) { 182 need_bytes = xlog_ticket_reservation(log, head, tic); 183 if (*free_bytes < need_bytes) 184 return false; 185 186 *free_bytes -= need_bytes; 187 trace_xfs_log_grant_wake_up(log, tic); 188 wake_up_process(tic->t_task); 189 } 190 191 return true; 192 } 193 194 STATIC int 195 xlog_grant_head_wait( 196 struct xlog *log, 197 struct xlog_grant_head *head, 198 struct xlog_ticket *tic, 199 int need_bytes) __releases(&head->lock) 200 __acquires(&head->lock) 201 { 202 list_add_tail(&tic->t_queue, &head->waiters); 203 204 do { 205 if (xlog_is_shutdown(log)) 206 goto shutdown; 207 208 __set_current_state(TASK_UNINTERRUPTIBLE); 209 spin_unlock(&head->lock); 210 211 XFS_STATS_INC(log->l_mp, xs_sleep_logspace); 212 213 /* Push on the AIL to free up all the log space. */ 214 xfs_ail_push_all(log->l_ailp); 215 216 trace_xfs_log_grant_sleep(log, tic); 217 schedule(); 218 trace_xfs_log_grant_wake(log, tic); 219 220 spin_lock(&head->lock); 221 if (xlog_is_shutdown(log)) 222 goto shutdown; 223 } while (xlog_grant_space_left(log, head) < need_bytes); 224 225 list_del_init(&tic->t_queue); 226 return 0; 227 shutdown: 228 list_del_init(&tic->t_queue); 229 return -EIO; 230 } 231 232 /* 233 * Atomically get the log space required for a log ticket. 234 * 235 * Once a ticket gets put onto head->waiters, it will only return after the 236 * needed reservation is satisfied. 237 * 238 * This function is structured so that it has a lock free fast path. This is 239 * necessary because every new transaction reservation will come through this 240 * path. Hence any lock will be globally hot if we take it unconditionally on 241 * every pass. 242 * 243 * As tickets are only ever moved on and off head->waiters under head->lock, we 244 * only need to take that lock if we are going to add the ticket to the queue 245 * and sleep. We can avoid taking the lock if the ticket was never added to 246 * head->waiters because the t_queue list head will be empty and we hold the 247 * only reference to it so it can safely be checked unlocked. 248 */ 249 STATIC int 250 xlog_grant_head_check( 251 struct xlog *log, 252 struct xlog_grant_head *head, 253 struct xlog_ticket *tic, 254 int *need_bytes) 255 { 256 int free_bytes; 257 int error = 0; 258 259 ASSERT(!xlog_in_recovery(log)); 260 261 /* 262 * If there are other waiters on the queue then give them a chance at 263 * logspace before us. Wake up the first waiters, if we do not wake 264 * up all the waiters then go to sleep waiting for more free space, 265 * otherwise try to get some space for this transaction. 266 */ 267 *need_bytes = xlog_ticket_reservation(log, head, tic); 268 free_bytes = xlog_grant_space_left(log, head); 269 if (!list_empty_careful(&head->waiters)) { 270 spin_lock(&head->lock); 271 if (!xlog_grant_head_wake(log, head, &free_bytes) || 272 free_bytes < *need_bytes) { 273 error = xlog_grant_head_wait(log, head, tic, 274 *need_bytes); 275 } 276 spin_unlock(&head->lock); 277 } else if (free_bytes < *need_bytes) { 278 spin_lock(&head->lock); 279 error = xlog_grant_head_wait(log, head, tic, *need_bytes); 280 spin_unlock(&head->lock); 281 } 282 283 return error; 284 } 285 286 bool 287 xfs_log_writable( 288 struct xfs_mount *mp) 289 { 290 /* 291 * Do not write to the log on norecovery mounts, if the data or log 292 * devices are read-only, or if the filesystem is shutdown. Read-only 293 * mounts allow internal writes for log recovery and unmount purposes, 294 * so don't restrict that case. 295 */ 296 if (xfs_has_norecovery(mp)) 297 return false; 298 if (xfs_readonly_buftarg(mp->m_ddev_targp)) 299 return false; 300 if (xfs_readonly_buftarg(mp->m_log->l_targ)) 301 return false; 302 if (xlog_is_shutdown(mp->m_log)) 303 return false; 304 return true; 305 } 306 307 /* 308 * Replenish the byte reservation required by moving the grant write head. 309 */ 310 int 311 xfs_log_regrant( 312 struct xfs_mount *mp, 313 struct xlog_ticket *tic) 314 { 315 struct xlog *log = mp->m_log; 316 int need_bytes; 317 int error = 0; 318 319 if (xlog_is_shutdown(log)) 320 return -EIO; 321 322 XFS_STATS_INC(mp, xs_try_logspace); 323 324 /* 325 * This is a new transaction on the ticket, so we need to change the 326 * transaction ID so that the next transaction has a different TID in 327 * the log. Just add one to the existing tid so that we can see chains 328 * of rolling transactions in the log easily. 329 */ 330 tic->t_tid++; 331 tic->t_curr_res = tic->t_unit_res; 332 if (tic->t_cnt > 0) 333 return 0; 334 335 trace_xfs_log_regrant(log, tic); 336 337 error = xlog_grant_head_check(log, &log->l_write_head, tic, 338 &need_bytes); 339 if (error) 340 goto out_error; 341 342 xlog_grant_add_space(&log->l_write_head, need_bytes); 343 trace_xfs_log_regrant_exit(log, tic); 344 return 0; 345 346 out_error: 347 /* 348 * If we are failing, make sure the ticket doesn't have any current 349 * reservations. We don't want to add this back when the ticket/ 350 * transaction gets cancelled. 351 */ 352 tic->t_curr_res = 0; 353 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */ 354 return error; 355 } 356 357 /* 358 * Reserve log space and return a ticket corresponding to the reservation. 359 * 360 * Each reservation is going to reserve extra space for a log record header. 361 * When writes happen to the on-disk log, we don't subtract the length of the 362 * log record header from any reservation. By wasting space in each 363 * reservation, we prevent over allocation problems. 364 */ 365 int 366 xfs_log_reserve( 367 struct xfs_mount *mp, 368 int unit_bytes, 369 int cnt, 370 struct xlog_ticket **ticp, 371 bool permanent) 372 { 373 struct xlog *log = mp->m_log; 374 struct xlog_ticket *tic; 375 int need_bytes; 376 int error = 0; 377 378 if (xlog_is_shutdown(log)) 379 return -EIO; 380 381 XFS_STATS_INC(mp, xs_try_logspace); 382 383 ASSERT(*ticp == NULL); 384 tic = xlog_ticket_alloc(log, unit_bytes, cnt, permanent); 385 *ticp = tic; 386 trace_xfs_log_reserve(log, tic); 387 error = xlog_grant_head_check(log, &log->l_reserve_head, tic, 388 &need_bytes); 389 if (error) 390 goto out_error; 391 392 xlog_grant_add_space(&log->l_reserve_head, need_bytes); 393 xlog_grant_add_space(&log->l_write_head, need_bytes); 394 trace_xfs_log_reserve_exit(log, tic); 395 return 0; 396 397 out_error: 398 /* 399 * If we are failing, make sure the ticket doesn't have any current 400 * reservations. We don't want to add this back when the ticket/ 401 * transaction gets cancelled. 402 */ 403 tic->t_curr_res = 0; 404 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */ 405 return error; 406 } 407 408 /* 409 * Run all the pending iclog callbacks and wake log force waiters and iclog 410 * space waiters so they can process the newly set shutdown state. We really 411 * don't care what order we process callbacks here because the log is shut down 412 * and so state cannot change on disk anymore. However, we cannot wake waiters 413 * until the callbacks have been processed because we may be in unmount and 414 * we must ensure that all AIL operations the callbacks perform have completed 415 * before we tear down the AIL. 416 * 417 * We avoid processing actively referenced iclogs so that we don't run callbacks 418 * while the iclog owner might still be preparing the iclog for IO submssion. 419 * These will be caught by xlog_state_iclog_release() and call this function 420 * again to process any callbacks that may have been added to that iclog. 421 */ 422 static void 423 xlog_state_shutdown_callbacks( 424 struct xlog *log) 425 __releases(&log->l_icloglock) 426 __acquires(&log->l_icloglock) 427 { 428 struct xlog_in_core *iclog; 429 LIST_HEAD(cb_list); 430 431 iclog = log->l_iclog; 432 do { 433 if (atomic_read(&iclog->ic_refcnt)) { 434 /* Reference holder will re-run iclog callbacks. */ 435 continue; 436 } 437 list_splice_init(&iclog->ic_callbacks, &cb_list); 438 spin_unlock(&log->l_icloglock); 439 440 xlog_cil_process_committed(&cb_list); 441 442 spin_lock(&log->l_icloglock); 443 wake_up_all(&iclog->ic_write_wait); 444 wake_up_all(&iclog->ic_force_wait); 445 } while ((iclog = iclog->ic_next) != log->l_iclog); 446 447 wake_up_all(&log->l_flush_wait); 448 } 449 450 /* 451 * Flush iclog to disk if this is the last reference to the given iclog and the 452 * it is in the WANT_SYNC state. 453 * 454 * If XLOG_ICL_NEED_FUA is already set on the iclog, we need to ensure that the 455 * log tail is updated correctly. NEED_FUA indicates that the iclog will be 456 * written to stable storage, and implies that a commit record is contained 457 * within the iclog. We need to ensure that the log tail does not move beyond 458 * the tail that the first commit record in the iclog ordered against, otherwise 459 * correct recovery of that checkpoint becomes dependent on future operations 460 * performed on this iclog. 461 * 462 * Hence if NEED_FUA is set and the current iclog tail lsn is empty, write the 463 * current tail into iclog. Once the iclog tail is set, future operations must 464 * not modify it, otherwise they potentially violate ordering constraints for 465 * the checkpoint commit that wrote the initial tail lsn value. The tail lsn in 466 * the iclog will get zeroed on activation of the iclog after sync, so we 467 * always capture the tail lsn on the iclog on the first NEED_FUA release 468 * regardless of the number of active reference counts on this iclog. 469 */ 470 int 471 xlog_state_release_iclog( 472 struct xlog *log, 473 struct xlog_in_core *iclog, 474 struct xlog_ticket *ticket) 475 __releases(&log->l_icloglock) 476 __acquires(&log->l_icloglock) 477 { 478 bool last_ref; 479 480 lockdep_assert_held(&log->l_icloglock); 481 482 trace_xlog_iclog_release(iclog, _RET_IP_); 483 /* 484 * Grabbing the current log tail needs to be atomic w.r.t. the writing 485 * of the tail LSN into the iclog so we guarantee that the log tail does 486 * not move between the first time we know that the iclog needs to be 487 * made stable and when we eventually submit it. 488 */ 489 if ((iclog->ic_state == XLOG_STATE_WANT_SYNC || 490 (iclog->ic_flags & XLOG_ICL_NEED_FUA)) && 491 !iclog->ic_header->h_tail_lsn) { 492 iclog->ic_header->h_tail_lsn = 493 cpu_to_be64(atomic64_read(&log->l_tail_lsn)); 494 } 495 496 last_ref = atomic_dec_and_test(&iclog->ic_refcnt); 497 498 if (xlog_is_shutdown(log)) { 499 /* 500 * If there are no more references to this iclog, process the 501 * pending iclog callbacks that were waiting on the release of 502 * this iclog. 503 */ 504 if (last_ref) 505 xlog_state_shutdown_callbacks(log); 506 return -EIO; 507 } 508 509 if (!last_ref) 510 return 0; 511 512 if (iclog->ic_state != XLOG_STATE_WANT_SYNC) { 513 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE); 514 return 0; 515 } 516 517 iclog->ic_state = XLOG_STATE_SYNCING; 518 xlog_verify_tail_lsn(log, iclog); 519 trace_xlog_iclog_syncing(iclog, _RET_IP_); 520 521 spin_unlock(&log->l_icloglock); 522 xlog_sync(log, iclog, ticket); 523 spin_lock(&log->l_icloglock); 524 return 0; 525 } 526 527 /* 528 * Mount a log filesystem 529 * 530 * mp - ubiquitous xfs mount point structure 531 * log_target - buftarg of on-disk log device 532 * blk_offset - Start block # where block size is 512 bytes (BBSIZE) 533 * num_bblocks - Number of BBSIZE blocks in on-disk log 534 * 535 * Return error or zero. 536 */ 537 int 538 xfs_log_mount( 539 xfs_mount_t *mp, 540 struct xfs_buftarg *log_target, 541 xfs_daddr_t blk_offset, 542 int num_bblks) 543 { 544 struct xlog *log; 545 int error = 0; 546 int min_logfsbs; 547 548 if (!xfs_has_norecovery(mp)) { 549 xfs_notice(mp, "Mounting V%d Filesystem %pU", 550 XFS_SB_VERSION_NUM(&mp->m_sb), 551 &mp->m_sb.sb_uuid); 552 } else { 553 xfs_notice(mp, 554 "Mounting V%d filesystem %pU in no-recovery mode. Filesystem will be inconsistent.", 555 XFS_SB_VERSION_NUM(&mp->m_sb), 556 &mp->m_sb.sb_uuid); 557 ASSERT(xfs_is_readonly(mp)); 558 } 559 560 log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks); 561 if (IS_ERR(log)) { 562 error = PTR_ERR(log); 563 goto out; 564 } 565 mp->m_log = log; 566 567 /* 568 * Now that we have set up the log and it's internal geometry 569 * parameters, we can validate the given log space and drop a critical 570 * message via syslog if the log size is too small. A log that is too 571 * small can lead to unexpected situations in transaction log space 572 * reservation stage. The superblock verifier has already validated all 573 * the other log geometry constraints, so we don't have to check those 574 * here. 575 * 576 * Note: For v4 filesystems, we can't just reject the mount if the 577 * validation fails. This would mean that people would have to 578 * downgrade their kernel just to remedy the situation as there is no 579 * way to grow the log (short of black magic surgery with xfs_db). 580 * 581 * We can, however, reject mounts for V5 format filesystems, as the 582 * mkfs binary being used to make the filesystem should never create a 583 * filesystem with a log that is too small. 584 */ 585 min_logfsbs = xfs_log_calc_minimum_size(mp); 586 if (mp->m_sb.sb_logblocks < min_logfsbs) { 587 xfs_warn(mp, 588 "Log size %d blocks too small, minimum size is %d blocks", 589 mp->m_sb.sb_logblocks, min_logfsbs); 590 591 /* 592 * Log check errors are always fatal on v5; or whenever bad 593 * metadata leads to a crash. 594 */ 595 if (xfs_has_crc(mp)) { 596 xfs_crit(mp, "AAIEEE! Log failed size checks. Abort!"); 597 ASSERT(0); 598 error = -EINVAL; 599 goto out_free_log; 600 } 601 xfs_crit(mp, "Log size out of supported range."); 602 xfs_crit(mp, 603 "Continuing onwards, but if log hangs are experienced then please report this message in the bug report."); 604 } 605 606 /* 607 * Initialize the AIL now we have a log. 608 */ 609 error = xfs_trans_ail_init(mp); 610 if (error) { 611 xfs_warn(mp, "AIL initialisation failed: error %d", error); 612 goto out_free_log; 613 } 614 log->l_ailp = mp->m_ail; 615 616 /* 617 * skip log recovery on a norecovery mount. pretend it all 618 * just worked. 619 */ 620 if (!xfs_has_norecovery(mp)) { 621 error = xlog_recover(log); 622 if (error) { 623 xfs_warn(mp, "log mount/recovery failed: error %d", 624 error); 625 xlog_recover_cancel(log); 626 goto out_destroy_ail; 627 } 628 } 629 630 error = xfs_sysfs_init(&log->l_kobj, &xfs_log_ktype, &mp->m_kobj, 631 "log"); 632 if (error) 633 goto out_destroy_ail; 634 635 /* Normal transactions can now occur */ 636 clear_bit(XLOG_ACTIVE_RECOVERY, &log->l_opstate); 637 638 /* 639 * Now the log has been fully initialised and we know were our 640 * space grant counters are, we can initialise the permanent ticket 641 * needed for delayed logging to work. 642 */ 643 xlog_cil_init_post_recovery(log); 644 645 return 0; 646 647 out_destroy_ail: 648 xfs_trans_ail_destroy(mp); 649 out_free_log: 650 xlog_dealloc_log(log); 651 out: 652 return error; 653 } 654 655 /* 656 * Finish the recovery of the file system. This is separate from the 657 * xfs_log_mount() call, because it depends on the code in xfs_mountfs() to read 658 * in the root and real-time bitmap inodes between calling xfs_log_mount() and 659 * here. 660 * 661 * If we finish recovery successfully, start the background log work. If we are 662 * not doing recovery, then we have a RO filesystem and we don't need to start 663 * it. 664 */ 665 int 666 xfs_log_mount_finish( 667 struct xfs_mount *mp) 668 { 669 struct xlog *log = mp->m_log; 670 int error = 0; 671 672 if (xfs_has_norecovery(mp)) { 673 ASSERT(xfs_is_readonly(mp)); 674 return 0; 675 } 676 677 /* 678 * During the second phase of log recovery, we need iget and 679 * iput to behave like they do for an active filesystem. 680 * xfs_fs_drop_inode needs to be able to prevent the deletion 681 * of inodes before we're done replaying log items on those 682 * inodes. Turn it off immediately after recovery finishes 683 * so that we don't leak the quota inodes if subsequent mount 684 * activities fail. 685 * 686 * We let all inodes involved in redo item processing end up on 687 * the LRU instead of being evicted immediately so that if we do 688 * something to an unlinked inode, the irele won't cause 689 * premature truncation and freeing of the inode, which results 690 * in log recovery failure. We have to evict the unreferenced 691 * lru inodes after clearing SB_ACTIVE because we don't 692 * otherwise clean up the lru if there's a subsequent failure in 693 * xfs_mountfs, which leads to us leaking the inodes if nothing 694 * else (e.g. quotacheck) references the inodes before the 695 * mount failure occurs. 696 */ 697 mp->m_super->s_flags |= SB_ACTIVE; 698 xfs_log_work_queue(mp); 699 if (xlog_recovery_needed(log)) 700 error = xlog_recover_finish(log); 701 mp->m_super->s_flags &= ~SB_ACTIVE; 702 evict_inodes(mp->m_super); 703 704 /* 705 * Drain the buffer LRU after log recovery. This is required for v4 706 * filesystems to avoid leaving around buffers with NULL verifier ops, 707 * but we do it unconditionally to make sure we're always in a clean 708 * cache state after mount. 709 * 710 * Don't push in the error case because the AIL may have pending intents 711 * that aren't removed until recovery is cancelled. 712 */ 713 if (xlog_recovery_needed(log)) { 714 if (!error) { 715 xfs_log_force(mp, XFS_LOG_SYNC); 716 xfs_ail_push_all_sync(mp->m_ail); 717 } 718 xfs_notice(mp, "Ending recovery (logdev: %s)", 719 mp->m_logname ? mp->m_logname : "internal"); 720 } else { 721 xfs_info(mp, "Ending clean mount"); 722 } 723 xfs_buftarg_drain(mp->m_ddev_targp); 724 725 clear_bit(XLOG_RECOVERY_NEEDED, &log->l_opstate); 726 727 /* Make sure the log is dead if we're returning failure. */ 728 ASSERT(!error || xlog_is_shutdown(log)); 729 730 return error; 731 } 732 733 /* 734 * The mount has failed. Cancel the recovery if it hasn't completed and destroy 735 * the log. 736 */ 737 void 738 xfs_log_mount_cancel( 739 struct xfs_mount *mp) 740 { 741 xlog_recover_cancel(mp->m_log); 742 xfs_log_unmount(mp); 743 } 744 745 /* 746 * Flush out the iclog to disk ensuring that device caches are flushed and 747 * the iclog hits stable storage before any completion waiters are woken. 748 */ 749 static inline int 750 xlog_force_iclog( 751 struct xlog *log, 752 struct xlog_in_core *iclog) 753 __releases(&log->l_icloglock) 754 __acquires(&log->l_icloglock) 755 { 756 atomic_inc(&iclog->ic_refcnt); 757 iclog->ic_flags |= XLOG_ICL_NEED_FLUSH | XLOG_ICL_NEED_FUA; 758 if (iclog->ic_state == XLOG_STATE_ACTIVE) 759 xlog_state_switch_iclogs(log, iclog, 0); 760 return xlog_state_release_iclog(log, iclog, NULL); 761 } 762 763 /* 764 * Cycle all the iclogbuf locks to make sure all log IO completion 765 * is done before we tear down these buffers. 766 */ 767 static void 768 xlog_wait_iclog_completion(struct xlog *log) 769 { 770 int i; 771 struct xlog_in_core *iclog = log->l_iclog; 772 773 for (i = 0; i < log->l_iclog_bufs; i++) { 774 down(&iclog->ic_sema); 775 up(&iclog->ic_sema); 776 iclog = iclog->ic_next; 777 } 778 } 779 780 /* 781 * Wait for the iclog and all prior iclogs to be written disk as required by the 782 * log force state machine. Waiting on ic_force_wait ensures iclog completions 783 * have been ordered and callbacks run before we are woken here, hence 784 * guaranteeing that all the iclogs up to this one are on stable storage. 785 */ 786 int 787 xlog_wait_on_iclog( 788 struct xlog *log, 789 struct xlog_in_core *iclog) 790 __releases(log->l_icloglock) 791 { 792 trace_xlog_iclog_wait_on(iclog, _RET_IP_); 793 if (!xlog_is_shutdown(log) && 794 iclog->ic_state != XLOG_STATE_ACTIVE && 795 iclog->ic_state != XLOG_STATE_DIRTY) { 796 XFS_STATS_INC(log->l_mp, xs_log_force_sleep); 797 xlog_wait(&iclog->ic_force_wait, &log->l_icloglock); 798 } else { 799 spin_unlock(&log->l_icloglock); 800 } 801 802 if (xlog_is_shutdown(log)) 803 return -EIO; 804 return 0; 805 } 806 807 int 808 xlog_write_one_vec( 809 struct xlog *log, 810 struct xfs_cil_ctx *ctx, 811 struct xfs_log_iovec *reg, 812 struct xlog_ticket *ticket) 813 { 814 struct xfs_log_vec lv = { 815 .lv_niovecs = 1, 816 .lv_iovecp = reg, 817 .lv_bytes = reg->i_len, 818 }; 819 LIST_HEAD (lv_chain); 820 821 /* account for space used by record data */ 822 ticket->t_curr_res -= lv.lv_bytes; 823 824 list_add(&lv.lv_list, &lv_chain); 825 return xlog_write(log, ctx, &lv_chain, ticket, lv.lv_bytes); 826 } 827 828 /* 829 * Write out an unmount record using the ticket provided. We have to account for 830 * the data space used in the unmount ticket as this write is not done from a 831 * transaction context that has already done the accounting for us. 832 */ 833 static int 834 xlog_write_unmount_record( 835 struct xlog *log, 836 struct xlog_ticket *ticket) 837 { 838 struct { 839 struct xlog_op_header ophdr; 840 struct xfs_unmount_log_format ulf; 841 } unmount_rec = { 842 .ophdr = { 843 .oh_clientid = XFS_LOG, 844 .oh_tid = cpu_to_be32(ticket->t_tid), 845 .oh_flags = XLOG_UNMOUNT_TRANS, 846 }, 847 .ulf = { 848 .magic = XLOG_UNMOUNT_TYPE, 849 }, 850 }; 851 struct xfs_log_iovec reg = { 852 .i_addr = &unmount_rec, 853 .i_len = sizeof(unmount_rec), 854 .i_type = XLOG_REG_TYPE_UNMOUNT, 855 }; 856 857 return xlog_write_one_vec(log, NULL, ®, ticket); 858 } 859 860 /* 861 * Mark the filesystem clean by writing an unmount record to the head of the 862 * log. 863 */ 864 static void 865 xlog_unmount_write( 866 struct xlog *log) 867 { 868 struct xfs_mount *mp = log->l_mp; 869 struct xlog_in_core *iclog; 870 struct xlog_ticket *tic = NULL; 871 int error; 872 873 error = xfs_log_reserve(mp, 600, 1, &tic, 0); 874 if (error) 875 goto out_err; 876 877 error = xlog_write_unmount_record(log, tic); 878 /* 879 * At this point, we're umounting anyway, so there's no point in 880 * transitioning log state to shutdown. Just continue... 881 */ 882 out_err: 883 if (error) 884 xfs_alert(mp, "%s: unmount record failed", __func__); 885 886 spin_lock(&log->l_icloglock); 887 iclog = log->l_iclog; 888 error = xlog_force_iclog(log, iclog); 889 xlog_wait_on_iclog(log, iclog); 890 891 if (tic) { 892 trace_xfs_log_umount_write(log, tic); 893 xfs_log_ticket_ungrant(log, tic); 894 } 895 } 896 897 static void 898 xfs_log_unmount_verify_iclog( 899 struct xlog *log) 900 { 901 struct xlog_in_core *iclog = log->l_iclog; 902 903 do { 904 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE); 905 ASSERT(iclog->ic_offset == 0); 906 } while ((iclog = iclog->ic_next) != log->l_iclog); 907 } 908 909 /* 910 * Unmount record used to have a string "Unmount filesystem--" in the 911 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE). 912 * We just write the magic number now since that particular field isn't 913 * currently architecture converted and "Unmount" is a bit foo. 914 * As far as I know, there weren't any dependencies on the old behaviour. 915 */ 916 static void 917 xfs_log_unmount_write( 918 struct xfs_mount *mp) 919 { 920 struct xlog *log = mp->m_log; 921 922 if (!xfs_log_writable(mp)) 923 return; 924 925 xfs_log_force(mp, XFS_LOG_SYNC); 926 927 if (xlog_is_shutdown(log)) 928 return; 929 930 /* 931 * If we think the summary counters are bad, avoid writing the unmount 932 * record to force log recovery at next mount, after which the summary 933 * counters will be recalculated. Refer to xlog_check_unmount_rec for 934 * more details. 935 */ 936 if (xfs_fs_has_sickness(mp, XFS_SICK_FS_COUNTERS) || 937 XFS_TEST_ERROR(mp, XFS_ERRTAG_FORCE_SUMMARY_RECALC)) { 938 xfs_alert(mp, "%s: will fix summary counters at next mount", 939 __func__); 940 return; 941 } 942 943 xfs_log_unmount_verify_iclog(log); 944 xlog_unmount_write(log); 945 } 946 947 /* 948 * Empty the log for unmount/freeze. 949 * 950 * To do this, we first need to shut down the background log work so it is not 951 * trying to cover the log as we clean up. We then need to unpin all objects in 952 * the log so we can then flush them out. Once they have completed their IO and 953 * run the callbacks removing themselves from the AIL, we can cover the log. 954 */ 955 int 956 xfs_log_quiesce( 957 struct xfs_mount *mp) 958 { 959 /* 960 * Clear log incompat features since we're quiescing the log. Report 961 * failures, though it's not fatal to have a higher log feature 962 * protection level than the log contents actually require. 963 */ 964 if (xfs_clear_incompat_log_features(mp)) { 965 int error; 966 967 error = xfs_sync_sb(mp, false); 968 if (error) 969 xfs_warn(mp, 970 "Failed to clear log incompat features on quiesce"); 971 } 972 973 cancel_delayed_work_sync(&mp->m_log->l_work); 974 xfs_log_force(mp, XFS_LOG_SYNC); 975 976 /* 977 * The superblock buffer is uncached and while xfs_ail_push_all_sync() 978 * will push it, xfs_buftarg_wait() will not wait for it. Further, 979 * xfs_buf_iowait() cannot be used because it was pushed with the 980 * XBF_ASYNC flag set, so we need to use a lock/unlock pair to wait for 981 * the IO to complete. 982 */ 983 xfs_ail_push_all_sync(mp->m_ail); 984 xfs_buftarg_wait(mp->m_ddev_targp); 985 xfs_buf_lock(mp->m_sb_bp); 986 xfs_buf_unlock(mp->m_sb_bp); 987 988 return xfs_log_cover(mp); 989 } 990 991 void 992 xfs_log_clean( 993 struct xfs_mount *mp) 994 { 995 xfs_log_quiesce(mp); 996 xfs_log_unmount_write(mp); 997 } 998 999 /* 1000 * Shut down and release the AIL and Log. 1001 * 1002 * During unmount, we need to ensure we flush all the dirty metadata objects 1003 * from the AIL so that the log is empty before we write the unmount record to 1004 * the log. Once this is done, we can tear down the AIL and the log. 1005 */ 1006 void 1007 xfs_log_unmount( 1008 struct xfs_mount *mp) 1009 { 1010 xfs_log_clean(mp); 1011 1012 /* 1013 * If shutdown has come from iclog IO context, the log 1014 * cleaning will have been skipped and so we need to wait 1015 * for the iclog to complete shutdown processing before we 1016 * tear anything down. 1017 */ 1018 xlog_wait_iclog_completion(mp->m_log); 1019 1020 xfs_buftarg_drain(mp->m_ddev_targp); 1021 1022 xfs_trans_ail_destroy(mp); 1023 1024 xfs_sysfs_del(&mp->m_log->l_kobj); 1025 1026 xlog_dealloc_log(mp->m_log); 1027 } 1028 1029 void 1030 xfs_log_item_init( 1031 struct xfs_mount *mp, 1032 struct xfs_log_item *item, 1033 int type, 1034 const struct xfs_item_ops *ops) 1035 { 1036 item->li_log = mp->m_log; 1037 item->li_ailp = mp->m_ail; 1038 item->li_type = type; 1039 item->li_ops = ops; 1040 item->li_lv = NULL; 1041 1042 INIT_LIST_HEAD(&item->li_ail); 1043 INIT_LIST_HEAD(&item->li_cil); 1044 INIT_LIST_HEAD(&item->li_bio_list); 1045 INIT_LIST_HEAD(&item->li_trans); 1046 } 1047 1048 /* 1049 * Wake up processes waiting for log space after we have moved the log tail. 1050 */ 1051 void 1052 xfs_log_space_wake( 1053 struct xfs_mount *mp) 1054 { 1055 struct xlog *log = mp->m_log; 1056 int free_bytes; 1057 1058 if (xlog_is_shutdown(log)) 1059 return; 1060 1061 if (!list_empty_careful(&log->l_write_head.waiters)) { 1062 ASSERT(!xlog_in_recovery(log)); 1063 1064 spin_lock(&log->l_write_head.lock); 1065 free_bytes = xlog_grant_space_left(log, &log->l_write_head); 1066 xlog_grant_head_wake(log, &log->l_write_head, &free_bytes); 1067 spin_unlock(&log->l_write_head.lock); 1068 } 1069 1070 if (!list_empty_careful(&log->l_reserve_head.waiters)) { 1071 ASSERT(!xlog_in_recovery(log)); 1072 1073 spin_lock(&log->l_reserve_head.lock); 1074 free_bytes = xlog_grant_space_left(log, &log->l_reserve_head); 1075 xlog_grant_head_wake(log, &log->l_reserve_head, &free_bytes); 1076 spin_unlock(&log->l_reserve_head.lock); 1077 } 1078 } 1079 1080 /* 1081 * Determine if we have a transaction that has gone to disk that needs to be 1082 * covered. To begin the transition to the idle state firstly the log needs to 1083 * be idle. That means the CIL, the AIL and the iclogs needs to be empty before 1084 * we start attempting to cover the log. 1085 * 1086 * Only if we are then in a state where covering is needed, the caller is 1087 * informed that dummy transactions are required to move the log into the idle 1088 * state. 1089 * 1090 * If there are any items in the AIl or CIL, then we do not want to attempt to 1091 * cover the log as we may be in a situation where there isn't log space 1092 * available to run a dummy transaction and this can lead to deadlocks when the 1093 * tail of the log is pinned by an item that is modified in the CIL. Hence 1094 * there's no point in running a dummy transaction at this point because we 1095 * can't start trying to idle the log until both the CIL and AIL are empty. 1096 */ 1097 static bool 1098 xfs_log_need_covered( 1099 struct xfs_mount *mp) 1100 { 1101 struct xlog *log = mp->m_log; 1102 bool needed = false; 1103 1104 if (!xlog_cil_empty(log)) 1105 return false; 1106 1107 spin_lock(&log->l_icloglock); 1108 switch (log->l_covered_state) { 1109 case XLOG_STATE_COVER_DONE: 1110 case XLOG_STATE_COVER_DONE2: 1111 case XLOG_STATE_COVER_IDLE: 1112 break; 1113 case XLOG_STATE_COVER_NEED: 1114 case XLOG_STATE_COVER_NEED2: 1115 if (xfs_ail_min_lsn(log->l_ailp)) 1116 break; 1117 if (!xlog_iclogs_empty(log)) 1118 break; 1119 1120 needed = true; 1121 if (log->l_covered_state == XLOG_STATE_COVER_NEED) 1122 log->l_covered_state = XLOG_STATE_COVER_DONE; 1123 else 1124 log->l_covered_state = XLOG_STATE_COVER_DONE2; 1125 break; 1126 default: 1127 needed = true; 1128 break; 1129 } 1130 spin_unlock(&log->l_icloglock); 1131 return needed; 1132 } 1133 1134 /* 1135 * Explicitly cover the log. This is similar to background log covering but 1136 * intended for usage in quiesce codepaths. The caller is responsible to ensure 1137 * the log is idle and suitable for covering. The CIL, iclog buffers and AIL 1138 * must all be empty. 1139 */ 1140 static int 1141 xfs_log_cover( 1142 struct xfs_mount *mp) 1143 { 1144 int error = 0; 1145 bool need_covered; 1146 1147 if (!xlog_is_shutdown(mp->m_log)) { 1148 ASSERT(xlog_cil_empty(mp->m_log)); 1149 ASSERT(xlog_iclogs_empty(mp->m_log)); 1150 ASSERT(!xfs_ail_min_lsn(mp->m_log->l_ailp)); 1151 } 1152 1153 if (!xfs_log_writable(mp)) 1154 return 0; 1155 1156 /* 1157 * xfs_log_need_covered() is not idempotent because it progresses the 1158 * state machine if the log requires covering. Therefore, we must call 1159 * this function once and use the result until we've issued an sb sync. 1160 * Do so first to make that abundantly clear. 1161 * 1162 * Fall into the covering sequence if the log needs covering or the 1163 * mount has lazy superblock accounting to sync to disk. The sb sync 1164 * used for covering accumulates the in-core counters, so covering 1165 * handles this for us. 1166 */ 1167 need_covered = xfs_log_need_covered(mp); 1168 if (!need_covered && !xfs_has_lazysbcount(mp)) 1169 return 0; 1170 1171 /* 1172 * To cover the log, commit the superblock twice (at most) in 1173 * independent checkpoints. The first serves as a reference for the 1174 * tail pointer. The sync transaction and AIL push empties the AIL and 1175 * updates the in-core tail to the LSN of the first checkpoint. The 1176 * second commit updates the on-disk tail with the in-core LSN, 1177 * covering the log. Push the AIL one more time to leave it empty, as 1178 * we found it. 1179 */ 1180 do { 1181 error = xfs_sync_sb(mp, true); 1182 if (error) 1183 break; 1184 xfs_ail_push_all_sync(mp->m_ail); 1185 } while (xfs_log_need_covered(mp)); 1186 1187 return error; 1188 } 1189 1190 static void 1191 xlog_ioend_work( 1192 struct work_struct *work) 1193 { 1194 struct xlog_in_core *iclog = 1195 container_of(work, struct xlog_in_core, ic_end_io_work); 1196 struct xlog *log = iclog->ic_log; 1197 int error; 1198 1199 error = blk_status_to_errno(iclog->ic_bio.bi_status); 1200 #ifdef DEBUG 1201 /* treat writes with injected CRC errors as failed */ 1202 if (iclog->ic_fail_crc) 1203 error = -EIO; 1204 #endif 1205 1206 /* 1207 * Race to shutdown the filesystem if we see an error. 1208 */ 1209 if (error || XFS_TEST_ERROR(log->l_mp, XFS_ERRTAG_IODONE_IOERR)) { 1210 xfs_alert(log->l_mp, "log I/O error %d", error); 1211 xlog_force_shutdown(log, SHUTDOWN_LOG_IO_ERROR); 1212 } 1213 1214 xlog_state_done_syncing(iclog); 1215 bio_uninit(&iclog->ic_bio); 1216 1217 /* 1218 * Drop the lock to signal that we are done. Nothing references the 1219 * iclog after this, so an unmount waiting on this lock can now tear it 1220 * down safely. As such, it is unsafe to reference the iclog after the 1221 * unlock as we could race with it being freed. 1222 */ 1223 up(&iclog->ic_sema); 1224 } 1225 1226 /* 1227 * Return size of each in-core log record buffer. 1228 * 1229 * All machines get 8 x 32kB buffers by default, unless tuned otherwise. 1230 * 1231 * If the filesystem blocksize is too large, we may need to choose a 1232 * larger size since the directory code currently logs entire blocks. 1233 */ 1234 STATIC void 1235 xlog_get_iclog_buffer_size( 1236 struct xfs_mount *mp, 1237 struct xlog *log) 1238 { 1239 if (mp->m_logbufs <= 0) 1240 mp->m_logbufs = XLOG_MAX_ICLOGS; 1241 if (mp->m_logbsize <= 0) 1242 mp->m_logbsize = XLOG_BIG_RECORD_BSIZE; 1243 1244 log->l_iclog_bufs = mp->m_logbufs; 1245 log->l_iclog_size = mp->m_logbsize; 1246 1247 /* 1248 * Combined size of the log record headers. The first 32k cycles 1249 * are stored directly in the xlog_rec_header, the rest in the 1250 * variable number of xlog_rec_ext_headers at its end. 1251 */ 1252 log->l_iclog_hsize = struct_size(log->l_iclog->ic_header, h_ext, 1253 DIV_ROUND_UP(mp->m_logbsize, XLOG_HEADER_CYCLE_SIZE) - 1); 1254 } 1255 1256 void 1257 xfs_log_work_queue( 1258 struct xfs_mount *mp) 1259 { 1260 queue_delayed_work(mp->m_sync_workqueue, &mp->m_log->l_work, 1261 msecs_to_jiffies(xfs_syncd_centisecs * 10)); 1262 } 1263 1264 /* 1265 * Clear the log incompat flags if we have the opportunity. 1266 * 1267 * This only happens if we're about to log the second dummy transaction as part 1268 * of covering the log. 1269 */ 1270 static inline void 1271 xlog_clear_incompat( 1272 struct xlog *log) 1273 { 1274 struct xfs_mount *mp = log->l_mp; 1275 1276 if (!xfs_sb_has_incompat_log_feature(&mp->m_sb, 1277 XFS_SB_FEAT_INCOMPAT_LOG_ALL)) 1278 return; 1279 1280 if (log->l_covered_state != XLOG_STATE_COVER_DONE2) 1281 return; 1282 1283 xfs_clear_incompat_log_features(mp); 1284 } 1285 1286 /* 1287 * Every sync period we need to unpin all items in the AIL and push them to 1288 * disk. If there is nothing dirty, then we might need to cover the log to 1289 * indicate that the filesystem is idle. 1290 */ 1291 static void 1292 xfs_log_worker( 1293 struct work_struct *work) 1294 { 1295 struct xlog *log = container_of(to_delayed_work(work), 1296 struct xlog, l_work); 1297 struct xfs_mount *mp = log->l_mp; 1298 1299 /* dgc: errors ignored - not fatal and nowhere to report them */ 1300 if (xfs_fs_writable(mp, SB_FREEZE_WRITE) && xfs_log_need_covered(mp)) { 1301 /* 1302 * Dump a transaction into the log that contains no real change. 1303 * This is needed to stamp the current tail LSN into the log 1304 * during the covering operation. 1305 * 1306 * We cannot use an inode here for this - that will push dirty 1307 * state back up into the VFS and then periodic inode flushing 1308 * will prevent log covering from making progress. Hence we 1309 * synchronously log the superblock instead to ensure the 1310 * superblock is immediately unpinned and can be written back. 1311 */ 1312 xlog_clear_incompat(log); 1313 xfs_sync_sb(mp, true); 1314 } else 1315 xfs_log_force(mp, 0); 1316 1317 /* start pushing all the metadata that is currently dirty */ 1318 xfs_ail_push_all(mp->m_ail); 1319 1320 /* queue us up again */ 1321 xfs_log_work_queue(mp); 1322 } 1323 1324 /* 1325 * This routine initializes some of the log structure for a given mount point. 1326 * Its primary purpose is to fill in enough, so recovery can occur. However, 1327 * some other stuff may be filled in too. 1328 */ 1329 STATIC struct xlog * 1330 xlog_alloc_log( 1331 struct xfs_mount *mp, 1332 struct xfs_buftarg *log_target, 1333 xfs_daddr_t blk_offset, 1334 int num_bblks) 1335 { 1336 struct xlog *log; 1337 struct xlog_in_core **iclogp; 1338 struct xlog_in_core *iclog, *prev_iclog = NULL; 1339 int i; 1340 int error = -ENOMEM; 1341 uint log2_size = 0; 1342 1343 log = kzalloc_obj(struct xlog, GFP_KERNEL | __GFP_RETRY_MAYFAIL); 1344 if (!log) { 1345 xfs_warn(mp, "Log allocation failed: No memory!"); 1346 goto out; 1347 } 1348 1349 log->l_mp = mp; 1350 log->l_targ = log_target; 1351 log->l_logsize = BBTOB(num_bblks); 1352 log->l_logBBstart = blk_offset; 1353 log->l_logBBsize = num_bblks; 1354 log->l_covered_state = XLOG_STATE_COVER_IDLE; 1355 set_bit(XLOG_ACTIVE_RECOVERY, &log->l_opstate); 1356 INIT_DELAYED_WORK(&log->l_work, xfs_log_worker); 1357 INIT_LIST_HEAD(&log->r_dfops); 1358 1359 log->l_prev_block = -1; 1360 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */ 1361 xlog_assign_atomic_lsn(&log->l_tail_lsn, 1, 0); 1362 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */ 1363 1364 if (xfs_has_logv2(mp) && mp->m_sb.sb_logsunit > 1) 1365 log->l_iclog_roundoff = mp->m_sb.sb_logsunit; 1366 else if (mp->m_sb.sb_logsectsize > 0) 1367 log->l_iclog_roundoff = mp->m_sb.sb_logsectsize; 1368 else 1369 log->l_iclog_roundoff = BBSIZE; 1370 1371 xlog_grant_head_init(&log->l_reserve_head); 1372 xlog_grant_head_init(&log->l_write_head); 1373 1374 error = -EFSCORRUPTED; 1375 if (xfs_has_sector(mp)) { 1376 log2_size = mp->m_sb.sb_logsectlog; 1377 if (log2_size < BBSHIFT) { 1378 xfs_warn(mp, "Log sector size too small (0x%x < 0x%x)", 1379 log2_size, BBSHIFT); 1380 goto out_free_log; 1381 } 1382 1383 log2_size -= BBSHIFT; 1384 if (log2_size > mp->m_sectbb_log) { 1385 xfs_warn(mp, "Log sector size too large (0x%x > 0x%x)", 1386 log2_size, mp->m_sectbb_log); 1387 goto out_free_log; 1388 } 1389 1390 /* for larger sector sizes, must have v2 or external log */ 1391 if (log2_size && log->l_logBBstart > 0 && 1392 !xfs_has_logv2(mp)) { 1393 xfs_warn(mp, 1394 "log sector size (0x%x) invalid for configuration.", 1395 log2_size); 1396 goto out_free_log; 1397 } 1398 } 1399 log->l_sectBBsize = 1 << log2_size; 1400 1401 xlog_get_iclog_buffer_size(mp, log); 1402 1403 spin_lock_init(&log->l_icloglock); 1404 init_waitqueue_head(&log->l_flush_wait); 1405 1406 iclogp = &log->l_iclog; 1407 ASSERT(log->l_iclog_size >= 4096); 1408 for (i = 0; i < log->l_iclog_bufs; i++) { 1409 size_t bvec_size = howmany(log->l_iclog_size, PAGE_SIZE) * 1410 sizeof(struct bio_vec); 1411 1412 iclog = kzalloc(sizeof(*iclog) + bvec_size, 1413 GFP_KERNEL | __GFP_RETRY_MAYFAIL); 1414 if (!iclog) 1415 goto out_free_iclog; 1416 1417 *iclogp = iclog; 1418 iclog->ic_prev = prev_iclog; 1419 prev_iclog = iclog; 1420 1421 iclog->ic_header = kvzalloc(log->l_iclog_size, 1422 GFP_KERNEL | __GFP_RETRY_MAYFAIL); 1423 if (!iclog->ic_header) 1424 goto out_free_iclog; 1425 iclog->ic_header->h_magicno = 1426 cpu_to_be32(XLOG_HEADER_MAGIC_NUM); 1427 iclog->ic_header->h_version = cpu_to_be32( 1428 xfs_has_logv2(log->l_mp) ? 2 : 1); 1429 iclog->ic_header->h_size = cpu_to_be32(log->l_iclog_size); 1430 iclog->ic_header->h_fmt = cpu_to_be32(XLOG_FMT); 1431 memcpy(&iclog->ic_header->h_fs_uuid, &mp->m_sb.sb_uuid, 1432 sizeof(iclog->ic_header->h_fs_uuid)); 1433 1434 iclog->ic_datap = (void *)iclog->ic_header + log->l_iclog_hsize; 1435 iclog->ic_size = log->l_iclog_size - log->l_iclog_hsize; 1436 iclog->ic_state = XLOG_STATE_ACTIVE; 1437 iclog->ic_log = log; 1438 atomic_set(&iclog->ic_refcnt, 0); 1439 INIT_LIST_HEAD(&iclog->ic_callbacks); 1440 1441 init_waitqueue_head(&iclog->ic_force_wait); 1442 init_waitqueue_head(&iclog->ic_write_wait); 1443 INIT_WORK(&iclog->ic_end_io_work, xlog_ioend_work); 1444 sema_init(&iclog->ic_sema, 1); 1445 1446 iclogp = &iclog->ic_next; 1447 } 1448 *iclogp = log->l_iclog; /* complete ring */ 1449 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */ 1450 1451 log->l_ioend_workqueue = alloc_workqueue("xfs-log/%s", 1452 XFS_WQFLAGS(WQ_FREEZABLE | WQ_MEM_RECLAIM | WQ_HIGHPRI | WQ_PERCPU), 1453 0, mp->m_super->s_id); 1454 if (!log->l_ioend_workqueue) 1455 goto out_free_iclog; 1456 1457 error = xlog_cil_init(log); 1458 if (error) 1459 goto out_destroy_workqueue; 1460 return log; 1461 1462 out_destroy_workqueue: 1463 destroy_workqueue(log->l_ioend_workqueue); 1464 out_free_iclog: 1465 for (iclog = log->l_iclog; iclog; iclog = prev_iclog) { 1466 prev_iclog = iclog->ic_next; 1467 kvfree(iclog->ic_header); 1468 kfree(iclog); 1469 if (prev_iclog == log->l_iclog) 1470 break; 1471 } 1472 out_free_log: 1473 kfree(log); 1474 out: 1475 return ERR_PTR(error); 1476 } /* xlog_alloc_log */ 1477 1478 /* 1479 * Stamp cycle number in every block 1480 */ 1481 STATIC void 1482 xlog_pack_data( 1483 struct xlog *log, 1484 struct xlog_in_core *iclog, 1485 int roundoff) 1486 { 1487 struct xlog_rec_header *rhead = iclog->ic_header; 1488 __be32 cycle_lsn = CYCLE_LSN_DISK(rhead->h_lsn); 1489 char *dp = iclog->ic_datap; 1490 int i; 1491 1492 for (i = 0; i < BTOBB(iclog->ic_offset + roundoff); i++) { 1493 *xlog_cycle_data(rhead, i) = *(__be32 *)dp; 1494 *(__be32 *)dp = cycle_lsn; 1495 dp += BBSIZE; 1496 } 1497 1498 for (i = 0; i < (log->l_iclog_hsize >> BBSHIFT) - 1; i++) 1499 rhead->h_ext[i].xh_cycle = cycle_lsn; 1500 } 1501 1502 /* 1503 * Calculate the checksum for a log buffer. 1504 * 1505 * This is a little more complicated than it should be because the various 1506 * headers and the actual data are non-contiguous. 1507 */ 1508 __le32 1509 xlog_cksum( 1510 struct xlog *log, 1511 struct xlog_rec_header *rhead, 1512 char *dp, 1513 unsigned int hdrsize, 1514 unsigned int size) 1515 { 1516 uint32_t crc; 1517 1518 /* first generate the crc for the record header ... */ 1519 crc = xfs_start_cksum_update((char *)rhead, hdrsize, 1520 offsetof(struct xlog_rec_header, h_crc)); 1521 1522 /* ... then for additional cycle data for v2 logs ... */ 1523 if (xfs_has_logv2(log->l_mp)) { 1524 int xheads, i; 1525 1526 xheads = DIV_ROUND_UP(size, XLOG_HEADER_CYCLE_SIZE) - 1; 1527 for (i = 0; i < xheads; i++) 1528 crc = crc32c(crc, &rhead->h_ext[i], XLOG_REC_EXT_SIZE); 1529 } 1530 1531 /* ... and finally for the payload */ 1532 crc = crc32c(crc, dp, size); 1533 1534 return xfs_end_cksum(crc); 1535 } 1536 1537 static void 1538 xlog_bio_end_io( 1539 struct bio *bio) 1540 { 1541 struct xlog_in_core *iclog = bio->bi_private; 1542 1543 queue_work(iclog->ic_log->l_ioend_workqueue, 1544 &iclog->ic_end_io_work); 1545 } 1546 1547 /* 1548 * When using multiple devices, we also need to flush the data and RT device 1549 * caches first to ensure that all metadata writeback covered by the LSN in 1550 * this iclog is on stable storage. This is slow, but it *must* complete 1551 * before we issue the external log IO. 1552 * 1553 * If the flush fails, we cannot conclude that past metadata writeback from 1554 * the log succeeded. Repeating the flush is not possible, hence we must 1555 * shut down with log IO error to avoid shutdown re-entering this path and 1556 * erroring out again. 1557 */ 1558 static int 1559 xlog_flush_data_caches( 1560 struct xlog *log) 1561 { 1562 struct xfs_mount *mp = log->l_mp; 1563 1564 if (log->l_targ != mp->m_ddev_targp) { 1565 if (blkdev_issue_flush(mp->m_ddev_targp->bt_bdev)) 1566 return -EIO; 1567 } 1568 if (mp->m_rtdev_targp && mp->m_rtdev_targp != mp->m_ddev_targp) { 1569 if (blkdev_issue_flush(mp->m_rtdev_targp->bt_bdev)) 1570 return -EIO; 1571 } 1572 1573 return 0; 1574 } 1575 1576 STATIC void 1577 xlog_write_iclog( 1578 struct xlog *log, 1579 struct xlog_in_core *iclog, 1580 uint64_t bno, 1581 unsigned int count) 1582 { 1583 ASSERT(bno < log->l_logBBsize); 1584 trace_xlog_iclog_write(iclog, _RET_IP_); 1585 1586 /* 1587 * We lock the iclogbufs here so that we can serialise against I/O 1588 * completion during unmount. We might be processing a shutdown 1589 * triggered during unmount, and that can occur asynchronously to the 1590 * unmount thread, and hence we need to ensure that completes before 1591 * tearing down the iclogbufs. Hence we need to hold the buffer lock 1592 * across the log IO to archieve that. 1593 */ 1594 down(&iclog->ic_sema); 1595 if (xlog_is_shutdown(log)) { 1596 /* 1597 * It would seem logical to return EIO here, but we rely on 1598 * the log state machine to propagate I/O errors instead of 1599 * doing it here. We kick of the state machine and unlock 1600 * the buffer manually, the code needs to be kept in sync 1601 * with the I/O completion path. 1602 */ 1603 goto sync; 1604 } 1605 1606 /* 1607 * We use REQ_SYNC | REQ_IDLE here to tell the block layer the are more 1608 * IOs coming immediately after this one. This prevents the block layer 1609 * writeback throttle from throttling log writes behind background 1610 * metadata writeback and causing priority inversions. 1611 */ 1612 bio_init(&iclog->ic_bio, log->l_targ->bt_bdev, iclog->ic_bvec, 1613 howmany(count, PAGE_SIZE), 1614 REQ_OP_WRITE | REQ_META | REQ_SYNC | REQ_IDLE); 1615 iclog->ic_bio.bi_iter.bi_sector = log->l_logBBstart + bno; 1616 iclog->ic_bio.bi_end_io = xlog_bio_end_io; 1617 iclog->ic_bio.bi_private = iclog; 1618 1619 if (iclog->ic_flags & XLOG_ICL_NEED_FLUSH) { 1620 if (xlog_flush_data_caches(log)) 1621 goto shutdown; 1622 iclog->ic_bio.bi_opf |= REQ_PREFLUSH; 1623 } 1624 if (iclog->ic_flags & XLOG_ICL_NEED_FUA) 1625 iclog->ic_bio.bi_opf |= REQ_FUA; 1626 1627 iclog->ic_flags &= ~(XLOG_ICL_NEED_FLUSH | XLOG_ICL_NEED_FUA); 1628 1629 if (is_vmalloc_addr(iclog->ic_header)) { 1630 if (!bio_add_vmalloc(&iclog->ic_bio, iclog->ic_header, count)) 1631 goto shutdown; 1632 } else { 1633 bio_add_virt_nofail(&iclog->ic_bio, iclog->ic_header, count); 1634 } 1635 1636 /* 1637 * If this log buffer would straddle the end of the log we will have 1638 * to split it up into two bios, so that we can continue at the start. 1639 */ 1640 if (bno + BTOBB(count) > log->l_logBBsize) { 1641 struct bio *split; 1642 1643 split = bio_split(&iclog->ic_bio, log->l_logBBsize - bno, 1644 GFP_NOIO, &fs_bio_set); 1645 bio_chain(split, &iclog->ic_bio); 1646 submit_bio(split); 1647 1648 /* restart at logical offset zero for the remainder */ 1649 iclog->ic_bio.bi_iter.bi_sector = log->l_logBBstart; 1650 } 1651 1652 submit_bio(&iclog->ic_bio); 1653 return; 1654 shutdown: 1655 xlog_force_shutdown(log, SHUTDOWN_LOG_IO_ERROR); 1656 sync: 1657 xlog_state_done_syncing(iclog); 1658 up(&iclog->ic_sema); 1659 } 1660 1661 /* 1662 * We need to bump cycle number for the part of the iclog that is 1663 * written to the start of the log. Watch out for the header magic 1664 * number case, though. 1665 */ 1666 static void 1667 xlog_split_iclog( 1668 struct xlog *log, 1669 void *data, 1670 uint64_t bno, 1671 unsigned int count) 1672 { 1673 unsigned int split_offset = BBTOB(log->l_logBBsize - bno); 1674 unsigned int i; 1675 1676 for (i = split_offset; i < count; i += BBSIZE) { 1677 uint32_t cycle = get_unaligned_be32(data + i); 1678 1679 if (++cycle == XLOG_HEADER_MAGIC_NUM) 1680 cycle++; 1681 put_unaligned_be32(cycle, data + i); 1682 } 1683 } 1684 1685 static int 1686 xlog_calc_iclog_size( 1687 struct xlog *log, 1688 struct xlog_in_core *iclog, 1689 uint32_t *roundoff) 1690 { 1691 uint32_t count_init, count; 1692 1693 /* Add for LR header */ 1694 count_init = log->l_iclog_hsize + iclog->ic_offset; 1695 count = roundup(count_init, log->l_iclog_roundoff); 1696 1697 *roundoff = count - count_init; 1698 1699 ASSERT(count >= count_init); 1700 ASSERT(*roundoff < log->l_iclog_roundoff); 1701 return count; 1702 } 1703 1704 /* 1705 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous 1706 * fashion. Previously, we should have moved the current iclog 1707 * ptr in the log to point to the next available iclog. This allows further 1708 * write to continue while this code syncs out an iclog ready to go. 1709 * Before an in-core log can be written out, the data section must be scanned 1710 * to save away the 1st word of each BBSIZE block into the header. We replace 1711 * it with the current cycle count. Each BBSIZE block is tagged with the 1712 * cycle count because there in an implicit assumption that drives will 1713 * guarantee that entire 512 byte blocks get written at once. In other words, 1714 * we can't have part of a 512 byte block written and part not written. By 1715 * tagging each block, we will know which blocks are valid when recovering 1716 * after an unclean shutdown. 1717 * 1718 * This routine is single threaded on the iclog. No other thread can be in 1719 * this routine with the same iclog. Changing contents of iclog can there- 1720 * fore be done without grabbing the state machine lock. Updating the global 1721 * log will require grabbing the lock though. 1722 * 1723 * The entire log manager uses a logical block numbering scheme. Only 1724 * xlog_write_iclog knows about the fact that the log may not start with 1725 * block zero on a given device. 1726 */ 1727 STATIC void 1728 xlog_sync( 1729 struct xlog *log, 1730 struct xlog_in_core *iclog, 1731 struct xlog_ticket *ticket) 1732 { 1733 unsigned int count; /* byte count of bwrite */ 1734 unsigned int roundoff; /* roundoff to BB or stripe */ 1735 uint64_t bno; 1736 unsigned int size; 1737 1738 ASSERT(atomic_read(&iclog->ic_refcnt) == 0); 1739 trace_xlog_iclog_sync(iclog, _RET_IP_); 1740 1741 count = xlog_calc_iclog_size(log, iclog, &roundoff); 1742 1743 /* 1744 * If we have a ticket, account for the roundoff via the ticket 1745 * reservation to avoid touching the hot grant heads needlessly. 1746 * Otherwise, we have to move grant heads directly. 1747 */ 1748 if (ticket) { 1749 ticket->t_curr_res -= roundoff; 1750 } else { 1751 xlog_grant_add_space(&log->l_reserve_head, roundoff); 1752 xlog_grant_add_space(&log->l_write_head, roundoff); 1753 } 1754 1755 /* put cycle number in every block */ 1756 xlog_pack_data(log, iclog, roundoff); 1757 1758 /* real byte length */ 1759 size = iclog->ic_offset; 1760 if (xfs_has_logv2(log->l_mp)) 1761 size += roundoff; 1762 iclog->ic_header->h_len = cpu_to_be32(size); 1763 1764 XFS_STATS_INC(log->l_mp, xs_log_writes); 1765 XFS_STATS_ADD(log->l_mp, xs_log_blocks, BTOBB(count)); 1766 1767 bno = BLOCK_LSN(be64_to_cpu(iclog->ic_header->h_lsn)); 1768 1769 /* Do we need to split this write into 2 parts? */ 1770 if (bno + BTOBB(count) > log->l_logBBsize) 1771 xlog_split_iclog(log, iclog->ic_header, bno, count); 1772 1773 /* calculcate the checksum */ 1774 iclog->ic_header->h_crc = xlog_cksum(log, iclog->ic_header, 1775 iclog->ic_datap, XLOG_REC_SIZE, size); 1776 /* 1777 * Intentionally corrupt the log record CRC based on the error injection 1778 * frequency, if defined. This facilitates testing log recovery in the 1779 * event of torn writes. Hence, set the IOABORT state to abort the log 1780 * write on I/O completion and shutdown the fs. The subsequent mount 1781 * detects the bad CRC and attempts to recover. 1782 */ 1783 #ifdef DEBUG 1784 if (XFS_TEST_ERROR(log->l_mp, XFS_ERRTAG_LOG_BAD_CRC)) { 1785 iclog->ic_header->h_crc &= cpu_to_le32(0xAAAAAAAA); 1786 iclog->ic_fail_crc = true; 1787 xfs_warn(log->l_mp, 1788 "Intentionally corrupted log record at LSN 0x%llx. Shutdown imminent.", 1789 be64_to_cpu(iclog->ic_header->h_lsn)); 1790 } 1791 #endif 1792 xlog_verify_iclog(log, iclog, count); 1793 xlog_write_iclog(log, iclog, bno, count); 1794 } 1795 1796 /* 1797 * Deallocate a log structure 1798 */ 1799 STATIC void 1800 xlog_dealloc_log( 1801 struct xlog *log) 1802 { 1803 struct xlog_in_core *iclog, *next_iclog; 1804 int i; 1805 1806 /* 1807 * Destroy the CIL after waiting for iclog IO completion because an 1808 * iclog EIO error will try to shut down the log, which accesses the 1809 * CIL to wake up the waiters. 1810 */ 1811 xlog_cil_destroy(log); 1812 1813 iclog = log->l_iclog; 1814 for (i = 0; i < log->l_iclog_bufs; i++) { 1815 next_iclog = iclog->ic_next; 1816 kvfree(iclog->ic_header); 1817 kfree(iclog); 1818 iclog = next_iclog; 1819 } 1820 1821 log->l_mp->m_log = NULL; 1822 destroy_workqueue(log->l_ioend_workqueue); 1823 kfree(log); 1824 } 1825 1826 /* 1827 * Update counters atomically now that memcpy is done. 1828 */ 1829 static inline void 1830 xlog_state_finish_copy( 1831 struct xlog *log, 1832 struct xlog_in_core *iclog, 1833 int record_cnt, 1834 int copy_bytes) 1835 { 1836 lockdep_assert_held(&log->l_icloglock); 1837 1838 be32_add_cpu(&iclog->ic_header->h_num_logops, record_cnt); 1839 iclog->ic_offset += copy_bytes; 1840 } 1841 1842 /* 1843 * print out info relating to regions written which consume 1844 * the reservation 1845 */ 1846 void 1847 xlog_print_tic_res( 1848 struct xfs_mount *mp, 1849 struct xlog_ticket *ticket) 1850 { 1851 xfs_warn(mp, "ticket reservation summary:"); 1852 xfs_warn(mp, " unit res = %d bytes", ticket->t_unit_res); 1853 xfs_warn(mp, " current res = %d bytes", ticket->t_curr_res); 1854 xfs_warn(mp, " original count = %d", ticket->t_ocnt); 1855 xfs_warn(mp, " remaining count = %d", ticket->t_cnt); 1856 } 1857 1858 /* 1859 * Print a summary of the transaction. 1860 */ 1861 void 1862 xlog_print_trans( 1863 struct xfs_trans *tp) 1864 { 1865 struct xfs_mount *mp = tp->t_mountp; 1866 struct xfs_log_item *lip; 1867 1868 /* dump core transaction and ticket info */ 1869 xfs_warn(mp, "transaction summary:"); 1870 xfs_warn(mp, " log res = %d", tp->t_log_res); 1871 xfs_warn(mp, " log count = %d", tp->t_log_count); 1872 xfs_warn(mp, " flags = 0x%x", tp->t_flags); 1873 1874 xlog_print_tic_res(mp, tp->t_ticket); 1875 1876 /* dump each log item */ 1877 list_for_each_entry(lip, &tp->t_items, li_trans) { 1878 struct xfs_log_vec *lv = lip->li_lv; 1879 struct xfs_log_iovec *vec; 1880 int i; 1881 1882 xfs_warn(mp, "log item: "); 1883 xfs_warn(mp, " type = 0x%x", lip->li_type); 1884 xfs_warn(mp, " flags = 0x%lx", lip->li_flags); 1885 if (!lv) 1886 continue; 1887 xfs_warn(mp, " niovecs = %d", lv->lv_niovecs); 1888 xfs_warn(mp, " alloc_size = %d", lv->lv_alloc_size); 1889 xfs_warn(mp, " bytes = %d", lv->lv_bytes); 1890 xfs_warn(mp, " buf used= %d", lv->lv_buf_used); 1891 1892 /* dump each iovec for the log item */ 1893 vec = lv->lv_iovecp; 1894 for (i = 0; i < lv->lv_niovecs; i++) { 1895 int dumplen = min(vec->i_len, 32); 1896 1897 xfs_warn(mp, " iovec[%d]", i); 1898 xfs_warn(mp, " type = 0x%x", vec->i_type); 1899 xfs_warn(mp, " len = %d", vec->i_len); 1900 xfs_warn(mp, " first %d bytes of iovec[%d]:", dumplen, i); 1901 xfs_hex_dump(vec->i_addr, dumplen); 1902 1903 vec++; 1904 } 1905 } 1906 } 1907 1908 static inline uint32_t xlog_write_space_left(struct xlog_write_data *data) 1909 { 1910 return data->iclog->ic_size - data->log_offset; 1911 } 1912 1913 static void * 1914 xlog_write_space_advance( 1915 struct xlog_write_data *data, 1916 unsigned int len) 1917 { 1918 void *p = data->iclog->ic_datap + data->log_offset; 1919 1920 ASSERT(xlog_write_space_left(data) >= len); 1921 ASSERT(data->log_offset % sizeof(int32_t) == 0); 1922 ASSERT(len % sizeof(int32_t) == 0); 1923 1924 data->data_cnt += len; 1925 data->log_offset += len; 1926 data->bytes_left -= len; 1927 return p; 1928 } 1929 1930 static inline void 1931 xlog_write_iovec( 1932 struct xlog_write_data *data, 1933 void *buf, 1934 uint32_t buf_len) 1935 { 1936 memcpy(xlog_write_space_advance(data, buf_len), buf, buf_len); 1937 data->record_cnt++; 1938 } 1939 1940 /* 1941 * Write log vectors into a single iclog which is guaranteed by the caller 1942 * to have enough space to write the entire log vector into. 1943 */ 1944 static void 1945 xlog_write_full( 1946 struct xfs_log_vec *lv, 1947 struct xlog_write_data *data) 1948 { 1949 int index; 1950 1951 ASSERT(data->bytes_left <= xlog_write_space_left(data) || 1952 data->iclog->ic_state == XLOG_STATE_WANT_SYNC); 1953 1954 /* 1955 * Ordered log vectors have no regions to write so this 1956 * loop will naturally skip them. 1957 */ 1958 for (index = 0; index < lv->lv_niovecs; index++) { 1959 struct xfs_log_iovec *reg = &lv->lv_iovecp[index]; 1960 struct xlog_op_header *ophdr = reg->i_addr; 1961 1962 ophdr->oh_tid = cpu_to_be32(data->ticket->t_tid); 1963 xlog_write_iovec(data, reg->i_addr, reg->i_len); 1964 } 1965 } 1966 1967 static int 1968 xlog_write_get_more_iclog_space( 1969 struct xlog_write_data *data) 1970 { 1971 struct xlog *log = data->iclog->ic_log; 1972 int error; 1973 1974 spin_lock(&log->l_icloglock); 1975 ASSERT(data->iclog->ic_state == XLOG_STATE_WANT_SYNC); 1976 xlog_state_finish_copy(log, data->iclog, data->record_cnt, 1977 data->data_cnt); 1978 error = xlog_state_release_iclog(log, data->iclog, data->ticket); 1979 spin_unlock(&log->l_icloglock); 1980 if (error) 1981 return error; 1982 1983 error = xlog_state_get_iclog_space(log, data); 1984 if (error) 1985 return error; 1986 data->record_cnt = 0; 1987 data->data_cnt = 0; 1988 return 0; 1989 } 1990 1991 /* 1992 * Write log vectors into a single iclog which is smaller than the current chain 1993 * length. We write until we cannot fit a full record into the remaining space 1994 * and then stop. We return the log vector that is to be written that cannot 1995 * wholly fit in the iclog. 1996 */ 1997 static int 1998 xlog_write_partial( 1999 struct xfs_log_vec *lv, 2000 struct xlog_write_data *data) 2001 { 2002 struct xlog_op_header *ophdr; 2003 int index = 0; 2004 uint32_t rlen; 2005 int error; 2006 2007 /* walk the logvec, copying until we run out of space in the iclog */ 2008 for (index = 0; index < lv->lv_niovecs; index++) { 2009 struct xfs_log_iovec *reg = &lv->lv_iovecp[index]; 2010 uint32_t reg_offset = 0; 2011 2012 /* 2013 * The first region of a continuation must have a non-zero 2014 * length otherwise log recovery will just skip over it and 2015 * start recovering from the next opheader it finds. Because we 2016 * mark the next opheader as a continuation, recovery will then 2017 * incorrectly add the continuation to the previous region and 2018 * that breaks stuff. 2019 * 2020 * Hence if there isn't space for region data after the 2021 * opheader, then we need to start afresh with a new iclog. 2022 */ 2023 if (xlog_write_space_left(data) <= 2024 sizeof(struct xlog_op_header)) { 2025 error = xlog_write_get_more_iclog_space(data); 2026 if (error) 2027 return error; 2028 } 2029 2030 ophdr = reg->i_addr; 2031 rlen = min_t(uint32_t, reg->i_len, xlog_write_space_left(data)); 2032 2033 ophdr->oh_tid = cpu_to_be32(data->ticket->t_tid); 2034 ophdr->oh_len = cpu_to_be32(rlen - sizeof(struct xlog_op_header)); 2035 if (rlen != reg->i_len) 2036 ophdr->oh_flags |= XLOG_CONTINUE_TRANS; 2037 2038 xlog_write_iovec(data, reg->i_addr, rlen); 2039 2040 /* If we wrote the whole region, move to the next. */ 2041 if (rlen == reg->i_len) 2042 continue; 2043 2044 /* 2045 * We now have a partially written iovec, but it can span 2046 * multiple iclogs so we loop here. First we release the iclog 2047 * we currently have, then we get a new iclog and add a new 2048 * opheader. Then we continue copying from where we were until 2049 * we either complete the iovec or fill the iclog. If we 2050 * complete the iovec, then we increment the index and go right 2051 * back to the top of the outer loop. if we fill the iclog, we 2052 * run the inner loop again. 2053 * 2054 * This is complicated by the tail of a region using all the 2055 * space in an iclog and hence requiring us to release the iclog 2056 * and get a new one before returning to the outer loop. We must 2057 * always guarantee that we exit this inner loop with at least 2058 * space for log transaction opheaders left in the current 2059 * iclog, hence we cannot just terminate the loop at the end 2060 * of the of the continuation. So we loop while there is no 2061 * space left in the current iclog, and check for the end of the 2062 * continuation after getting a new iclog. 2063 */ 2064 do { 2065 /* 2066 * Ensure we include the continuation opheader in the 2067 * space we need in the new iclog by adding that size 2068 * to the length we require. This continuation opheader 2069 * needs to be accounted to the ticket as the space it 2070 * consumes hasn't been accounted to the lv we are 2071 * writing. 2072 */ 2073 data->bytes_left += sizeof(struct xlog_op_header); 2074 error = xlog_write_get_more_iclog_space(data); 2075 if (error) 2076 return error; 2077 2078 ophdr = xlog_write_space_advance(data, 2079 sizeof(struct xlog_op_header)); 2080 ophdr->oh_tid = cpu_to_be32(data->ticket->t_tid); 2081 ophdr->oh_clientid = XFS_TRANSACTION; 2082 ophdr->oh_res2 = 0; 2083 ophdr->oh_flags = XLOG_WAS_CONT_TRANS; 2084 2085 data->ticket->t_curr_res -= 2086 sizeof(struct xlog_op_header); 2087 2088 /* 2089 * If rlen fits in the iclog, then end the region 2090 * continuation. Otherwise we're going around again. 2091 */ 2092 reg_offset += rlen; 2093 rlen = reg->i_len - reg_offset; 2094 if (rlen <= xlog_write_space_left(data)) 2095 ophdr->oh_flags |= XLOG_END_TRANS; 2096 else 2097 ophdr->oh_flags |= XLOG_CONTINUE_TRANS; 2098 2099 rlen = min_t(uint32_t, rlen, 2100 xlog_write_space_left(data)); 2101 ophdr->oh_len = cpu_to_be32(rlen); 2102 2103 xlog_write_iovec(data, reg->i_addr + reg_offset, rlen); 2104 } while (ophdr->oh_flags & XLOG_CONTINUE_TRANS); 2105 } 2106 2107 return 0; 2108 } 2109 2110 /* 2111 * Write some region out to in-core log 2112 * 2113 * This will be called when writing externally provided regions or when 2114 * writing out a commit record for a given transaction. 2115 * 2116 * General algorithm: 2117 * 1. Find total length of this write. This may include adding to the 2118 * lengths passed in. 2119 * 2. Check whether we violate the tickets reservation. 2120 * 3. While writing to this iclog 2121 * A. Reserve as much space in this iclog as can get 2122 * B. If this is first write, save away start lsn 2123 * C. While writing this region: 2124 * 1. If first write of transaction, write start record 2125 * 2. Write log operation header (header per region) 2126 * 3. Find out if we can fit entire region into this iclog 2127 * 4. Potentially, verify destination memcpy ptr 2128 * 5. Memcpy (partial) region 2129 * 6. If partial copy, release iclog; otherwise, continue 2130 * copying more regions into current iclog 2131 * 4. Mark want sync bit (in simulation mode) 2132 * 5. Release iclog for potential flush to on-disk log. 2133 * 2134 * ERRORS: 2135 * 1. Panic if reservation is overrun. This should never happen since 2136 * reservation amounts are generated internal to the filesystem. 2137 * NOTES: 2138 * 1. Tickets are single threaded data structures. 2139 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the 2140 * syncing routine. When a single log_write region needs to span 2141 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set 2142 * on all log operation writes which don't contain the end of the 2143 * region. The XLOG_END_TRANS bit is used for the in-core log 2144 * operation which contains the end of the continued log_write region. 2145 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog, 2146 * we don't really know exactly how much space will be used. As a result, 2147 * we don't update ic_offset until the end when we know exactly how many 2148 * bytes have been written out. 2149 */ 2150 int 2151 xlog_write( 2152 struct xlog *log, 2153 struct xfs_cil_ctx *ctx, 2154 struct list_head *lv_chain, 2155 struct xlog_ticket *ticket, 2156 uint32_t len) 2157 2158 { 2159 struct xfs_log_vec *lv; 2160 struct xlog_write_data data = { 2161 .ticket = ticket, 2162 .bytes_left = len, 2163 }; 2164 int error; 2165 2166 if (ticket->t_curr_res < 0) { 2167 xfs_alert_tag(log->l_mp, XFS_PTAG_LOGRES, 2168 "ctx ticket reservation ran out. Need to up reservation"); 2169 xlog_print_tic_res(log->l_mp, ticket); 2170 xlog_force_shutdown(log, SHUTDOWN_LOG_IO_ERROR); 2171 } 2172 2173 error = xlog_state_get_iclog_space(log, &data); 2174 if (error) 2175 return error; 2176 2177 ASSERT(xlog_write_space_left(&data) > 0); 2178 2179 /* 2180 * If we have a context pointer, pass it the first iclog we are 2181 * writing to so it can record state needed for iclog write 2182 * ordering. 2183 */ 2184 if (ctx) 2185 xlog_cil_set_ctx_write_state(ctx, data.iclog); 2186 2187 list_for_each_entry(lv, lv_chain, lv_list) { 2188 /* 2189 * If the entire log vec does not fit in the iclog, punt it to 2190 * the partial copy loop which can handle this case. 2191 */ 2192 if (lv->lv_niovecs && 2193 lv->lv_bytes > xlog_write_space_left(&data)) { 2194 error = xlog_write_partial(lv, &data); 2195 if (error) { 2196 /* 2197 * We have no iclog to release, so just return 2198 * the error immediately. 2199 */ 2200 return error; 2201 } 2202 } else { 2203 xlog_write_full(lv, &data); 2204 } 2205 } 2206 ASSERT(data.bytes_left == 0); 2207 2208 /* 2209 * We've already been guaranteed that the last writes will fit inside 2210 * the current iclog, and hence it will already have the space used by 2211 * those writes accounted to it. Hence we do not need to update the 2212 * iclog with the number of bytes written here. 2213 */ 2214 spin_lock(&log->l_icloglock); 2215 xlog_state_finish_copy(log, data.iclog, data.record_cnt, 0); 2216 error = xlog_state_release_iclog(log, data.iclog, ticket); 2217 spin_unlock(&log->l_icloglock); 2218 2219 return error; 2220 } 2221 2222 static void 2223 xlog_state_activate_iclog( 2224 struct xlog_in_core *iclog, 2225 int *iclogs_changed) 2226 { 2227 ASSERT(list_empty_careful(&iclog->ic_callbacks)); 2228 trace_xlog_iclog_activate(iclog, _RET_IP_); 2229 2230 /* 2231 * If the number of ops in this iclog indicate it just contains the 2232 * dummy transaction, we can change state into IDLE (the second time 2233 * around). Otherwise we should change the state into NEED a dummy. 2234 * We don't need to cover the dummy. 2235 */ 2236 if (*iclogs_changed == 0 && 2237 iclog->ic_header->h_num_logops == cpu_to_be32(XLOG_COVER_OPS)) { 2238 *iclogs_changed = 1; 2239 } else { 2240 /* 2241 * We have two dirty iclogs so start over. This could also be 2242 * num of ops indicating this is not the dummy going out. 2243 */ 2244 *iclogs_changed = 2; 2245 } 2246 2247 iclog->ic_state = XLOG_STATE_ACTIVE; 2248 iclog->ic_offset = 0; 2249 iclog->ic_header->h_num_logops = 0; 2250 memset(iclog->ic_header->h_cycle_data, 0, 2251 sizeof(iclog->ic_header->h_cycle_data)); 2252 iclog->ic_header->h_lsn = 0; 2253 iclog->ic_header->h_tail_lsn = 0; 2254 } 2255 2256 /* 2257 * Loop through all iclogs and mark all iclogs currently marked DIRTY as 2258 * ACTIVE after iclog I/O has completed. 2259 */ 2260 static void 2261 xlog_state_activate_iclogs( 2262 struct xlog *log, 2263 int *iclogs_changed) 2264 { 2265 struct xlog_in_core *iclog = log->l_iclog; 2266 2267 do { 2268 if (iclog->ic_state == XLOG_STATE_DIRTY) 2269 xlog_state_activate_iclog(iclog, iclogs_changed); 2270 /* 2271 * The ordering of marking iclogs ACTIVE must be maintained, so 2272 * an iclog doesn't become ACTIVE beyond one that is SYNCING. 2273 */ 2274 else if (iclog->ic_state != XLOG_STATE_ACTIVE) 2275 break; 2276 } while ((iclog = iclog->ic_next) != log->l_iclog); 2277 } 2278 2279 static int 2280 xlog_covered_state( 2281 int prev_state, 2282 int iclogs_changed) 2283 { 2284 /* 2285 * We go to NEED for any non-covering writes. We go to NEED2 if we just 2286 * wrote the first covering record (DONE). We go to IDLE if we just 2287 * wrote the second covering record (DONE2) and remain in IDLE until a 2288 * non-covering write occurs. 2289 */ 2290 switch (prev_state) { 2291 case XLOG_STATE_COVER_IDLE: 2292 if (iclogs_changed == 1) 2293 return XLOG_STATE_COVER_IDLE; 2294 fallthrough; 2295 case XLOG_STATE_COVER_NEED: 2296 case XLOG_STATE_COVER_NEED2: 2297 break; 2298 case XLOG_STATE_COVER_DONE: 2299 if (iclogs_changed == 1) 2300 return XLOG_STATE_COVER_NEED2; 2301 break; 2302 case XLOG_STATE_COVER_DONE2: 2303 if (iclogs_changed == 1) 2304 return XLOG_STATE_COVER_IDLE; 2305 break; 2306 default: 2307 ASSERT(0); 2308 } 2309 2310 return XLOG_STATE_COVER_NEED; 2311 } 2312 2313 STATIC void 2314 xlog_state_clean_iclog( 2315 struct xlog *log, 2316 struct xlog_in_core *dirty_iclog) 2317 { 2318 int iclogs_changed = 0; 2319 2320 trace_xlog_iclog_clean(dirty_iclog, _RET_IP_); 2321 2322 dirty_iclog->ic_state = XLOG_STATE_DIRTY; 2323 2324 xlog_state_activate_iclogs(log, &iclogs_changed); 2325 wake_up_all(&dirty_iclog->ic_force_wait); 2326 2327 if (iclogs_changed) { 2328 log->l_covered_state = xlog_covered_state(log->l_covered_state, 2329 iclogs_changed); 2330 } 2331 } 2332 2333 STATIC xfs_lsn_t 2334 xlog_get_lowest_lsn( 2335 struct xlog *log) 2336 { 2337 struct xlog_in_core *iclog = log->l_iclog; 2338 xfs_lsn_t lowest_lsn = 0, lsn; 2339 2340 do { 2341 if (iclog->ic_state == XLOG_STATE_ACTIVE || 2342 iclog->ic_state == XLOG_STATE_DIRTY) 2343 continue; 2344 2345 lsn = be64_to_cpu(iclog->ic_header->h_lsn); 2346 if ((lsn && !lowest_lsn) || XFS_LSN_CMP(lsn, lowest_lsn) < 0) 2347 lowest_lsn = lsn; 2348 } while ((iclog = iclog->ic_next) != log->l_iclog); 2349 2350 return lowest_lsn; 2351 } 2352 2353 /* 2354 * Return true if we need to stop processing, false to continue to the next 2355 * iclog. The caller will need to run callbacks if the iclog is returned in the 2356 * XLOG_STATE_CALLBACK state. 2357 */ 2358 static bool 2359 xlog_state_iodone_process_iclog( 2360 struct xlog *log, 2361 struct xlog_in_core *iclog) 2362 { 2363 xfs_lsn_t lowest_lsn; 2364 xfs_lsn_t header_lsn; 2365 2366 switch (iclog->ic_state) { 2367 case XLOG_STATE_ACTIVE: 2368 case XLOG_STATE_DIRTY: 2369 /* 2370 * Skip all iclogs in the ACTIVE & DIRTY states: 2371 */ 2372 return false; 2373 case XLOG_STATE_DONE_SYNC: 2374 /* 2375 * Now that we have an iclog that is in the DONE_SYNC state, do 2376 * one more check here to see if we have chased our tail around. 2377 * If this is not the lowest lsn iclog, then we will leave it 2378 * for another completion to process. 2379 */ 2380 header_lsn = be64_to_cpu(iclog->ic_header->h_lsn); 2381 lowest_lsn = xlog_get_lowest_lsn(log); 2382 if (lowest_lsn && XFS_LSN_CMP(lowest_lsn, header_lsn) < 0) 2383 return false; 2384 /* 2385 * If there are no callbacks on this iclog, we can mark it clean 2386 * immediately and return. Otherwise we need to run the 2387 * callbacks. 2388 */ 2389 if (list_empty(&iclog->ic_callbacks)) { 2390 xlog_state_clean_iclog(log, iclog); 2391 return false; 2392 } 2393 trace_xlog_iclog_callback(iclog, _RET_IP_); 2394 iclog->ic_state = XLOG_STATE_CALLBACK; 2395 return false; 2396 default: 2397 /* 2398 * Can only perform callbacks in order. Since this iclog is not 2399 * in the DONE_SYNC state, we skip the rest and just try to 2400 * clean up. 2401 */ 2402 return true; 2403 } 2404 } 2405 2406 /* 2407 * Loop over all the iclogs, running attached callbacks on them. Return true if 2408 * we ran any callbacks, indicating that we dropped the icloglock. We don't need 2409 * to handle transient shutdown state here at all because 2410 * xlog_state_shutdown_callbacks() will be run to do the necessary shutdown 2411 * cleanup of the callbacks. 2412 */ 2413 static bool 2414 xlog_state_do_iclog_callbacks( 2415 struct xlog *log) 2416 __releases(&log->l_icloglock) 2417 __acquires(&log->l_icloglock) 2418 { 2419 struct xlog_in_core *first_iclog = log->l_iclog; 2420 struct xlog_in_core *iclog = first_iclog; 2421 bool ran_callback = false; 2422 2423 do { 2424 LIST_HEAD(cb_list); 2425 2426 if (xlog_state_iodone_process_iclog(log, iclog)) 2427 break; 2428 if (iclog->ic_state != XLOG_STATE_CALLBACK) { 2429 iclog = iclog->ic_next; 2430 continue; 2431 } 2432 list_splice_init(&iclog->ic_callbacks, &cb_list); 2433 spin_unlock(&log->l_icloglock); 2434 2435 trace_xlog_iclog_callbacks_start(iclog, _RET_IP_); 2436 xlog_cil_process_committed(&cb_list); 2437 trace_xlog_iclog_callbacks_done(iclog, _RET_IP_); 2438 ran_callback = true; 2439 2440 spin_lock(&log->l_icloglock); 2441 xlog_state_clean_iclog(log, iclog); 2442 iclog = iclog->ic_next; 2443 } while (iclog != first_iclog); 2444 2445 return ran_callback; 2446 } 2447 2448 2449 /* 2450 * Loop running iclog completion callbacks until there are no more iclogs in a 2451 * state that can run callbacks. 2452 */ 2453 STATIC void 2454 xlog_state_do_callback( 2455 struct xlog *log) 2456 { 2457 int flushcnt = 0; 2458 int repeats = 0; 2459 2460 spin_lock(&log->l_icloglock); 2461 while (xlog_state_do_iclog_callbacks(log)) { 2462 if (xlog_is_shutdown(log)) 2463 break; 2464 2465 if (++repeats > 5000) { 2466 flushcnt += repeats; 2467 repeats = 0; 2468 xfs_warn(log->l_mp, 2469 "%s: possible infinite loop (%d iterations)", 2470 __func__, flushcnt); 2471 } 2472 } 2473 2474 if (log->l_iclog->ic_state == XLOG_STATE_ACTIVE) 2475 wake_up_all(&log->l_flush_wait); 2476 2477 spin_unlock(&log->l_icloglock); 2478 } 2479 2480 2481 /* 2482 * Finish transitioning this iclog to the dirty state. 2483 * 2484 * Callbacks could take time, so they are done outside the scope of the 2485 * global state machine log lock. 2486 */ 2487 STATIC void 2488 xlog_state_done_syncing( 2489 struct xlog_in_core *iclog) 2490 { 2491 struct xlog *log = iclog->ic_log; 2492 2493 spin_lock(&log->l_icloglock); 2494 ASSERT(atomic_read(&iclog->ic_refcnt) == 0); 2495 trace_xlog_iclog_sync_done(iclog, _RET_IP_); 2496 2497 /* 2498 * If we got an error, either on the first buffer, or in the case of 2499 * split log writes, on the second, we shut down the file system and 2500 * no iclogs should ever be attempted to be written to disk again. 2501 */ 2502 if (!xlog_is_shutdown(log)) { 2503 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING); 2504 iclog->ic_state = XLOG_STATE_DONE_SYNC; 2505 } 2506 2507 /* 2508 * Someone could be sleeping prior to writing out the next 2509 * iclog buffer, we wake them all, one will get to do the 2510 * I/O, the others get to wait for the result. 2511 */ 2512 wake_up_all(&iclog->ic_write_wait); 2513 spin_unlock(&log->l_icloglock); 2514 xlog_state_do_callback(log); 2515 } 2516 2517 /* 2518 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must 2519 * sleep. We wait on the flush queue on the head iclog as that should be 2520 * the first iclog to complete flushing. Hence if all iclogs are syncing, 2521 * we will wait here and all new writes will sleep until a sync completes. 2522 * 2523 * The in-core logs are used in a circular fashion. They are not used 2524 * out-of-order even when an iclog past the head is free. 2525 * 2526 * return: 2527 * * log_offset where xlog_write() can start writing into the in-core 2528 * log's data space. 2529 * * in-core log pointer to which xlog_write() should write. 2530 * * boolean indicating this is a continued write to an in-core log. 2531 * If this is the last write, then the in-core log's offset field 2532 * needs to be incremented, depending on the amount of data which 2533 * is copied. 2534 */ 2535 STATIC int 2536 xlog_state_get_iclog_space( 2537 struct xlog *log, 2538 struct xlog_write_data *data) 2539 { 2540 int log_offset; 2541 struct xlog_rec_header *head; 2542 struct xlog_in_core *iclog; 2543 2544 restart: 2545 spin_lock(&log->l_icloglock); 2546 if (xlog_is_shutdown(log)) { 2547 spin_unlock(&log->l_icloglock); 2548 return -EIO; 2549 } 2550 2551 iclog = log->l_iclog; 2552 if (iclog->ic_state != XLOG_STATE_ACTIVE) { 2553 XFS_STATS_INC(log->l_mp, xs_log_noiclogs); 2554 2555 /* Wait for log writes to have flushed */ 2556 xlog_wait(&log->l_flush_wait, &log->l_icloglock); 2557 goto restart; 2558 } 2559 2560 head = iclog->ic_header; 2561 2562 atomic_inc(&iclog->ic_refcnt); /* prevents sync */ 2563 log_offset = iclog->ic_offset; 2564 2565 trace_xlog_iclog_get_space(iclog, _RET_IP_); 2566 2567 /* On the 1st write to an iclog, figure out lsn. This works 2568 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are 2569 * committing to. If the offset is set, that's how many blocks 2570 * must be written. 2571 */ 2572 if (log_offset == 0) { 2573 data->ticket->t_curr_res -= log->l_iclog_hsize; 2574 head->h_cycle = cpu_to_be32(log->l_curr_cycle); 2575 head->h_lsn = cpu_to_be64( 2576 xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block)); 2577 ASSERT(log->l_curr_block >= 0); 2578 } 2579 2580 /* If there is enough room to write everything, then do it. Otherwise, 2581 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC 2582 * bit is on, so this will get flushed out. Don't update ic_offset 2583 * until you know exactly how many bytes get copied. Therefore, wait 2584 * until later to update ic_offset. 2585 * 2586 * xlog_write() algorithm assumes that at least 2 xlog_op_header's 2587 * can fit into remaining data section. 2588 */ 2589 if (iclog->ic_size - iclog->ic_offset < 2590 2 * sizeof(struct xlog_op_header)) { 2591 int error = 0; 2592 2593 xlog_state_switch_iclogs(log, iclog, iclog->ic_size); 2594 2595 /* 2596 * If we are the only one writing to this iclog, sync it to 2597 * disk. We need to do an atomic compare and decrement here to 2598 * avoid racing with concurrent atomic_dec_and_lock() calls in 2599 * xlog_state_release_iclog() when there is more than one 2600 * reference to the iclog. 2601 */ 2602 if (!atomic_add_unless(&iclog->ic_refcnt, -1, 1)) 2603 error = xlog_state_release_iclog(log, iclog, 2604 data->ticket); 2605 spin_unlock(&log->l_icloglock); 2606 if (error) 2607 return error; 2608 goto restart; 2609 } 2610 2611 /* Do we have enough room to write the full amount in the remainder 2612 * of this iclog? Or must we continue a write on the next iclog and 2613 * mark this iclog as completely taken? In the case where we switch 2614 * iclogs (to mark it taken), this particular iclog will release/sync 2615 * to disk in xlog_write(). 2616 */ 2617 if (data->bytes_left <= iclog->ic_size - iclog->ic_offset) 2618 iclog->ic_offset += data->bytes_left; 2619 else 2620 xlog_state_switch_iclogs(log, iclog, iclog->ic_size); 2621 data->iclog = iclog; 2622 2623 ASSERT(iclog->ic_offset <= iclog->ic_size); 2624 spin_unlock(&log->l_icloglock); 2625 2626 data->log_offset = log_offset; 2627 return 0; 2628 } 2629 2630 /* 2631 * The first cnt-1 times a ticket goes through here we don't need to move the 2632 * grant write head because the permanent reservation has reserved cnt times the 2633 * unit amount. Release part of current permanent unit reservation and reset 2634 * current reservation to be one units worth. Also move grant reservation head 2635 * forward. 2636 */ 2637 void 2638 xfs_log_ticket_regrant( 2639 struct xlog *log, 2640 struct xlog_ticket *ticket) 2641 { 2642 trace_xfs_log_ticket_regrant(log, ticket); 2643 2644 if (ticket->t_cnt > 0) 2645 ticket->t_cnt--; 2646 2647 xlog_grant_sub_space(&log->l_reserve_head, ticket->t_curr_res); 2648 xlog_grant_sub_space(&log->l_write_head, ticket->t_curr_res); 2649 ticket->t_curr_res = ticket->t_unit_res; 2650 2651 trace_xfs_log_ticket_regrant_sub(log, ticket); 2652 2653 /* just return if we still have some of the pre-reserved space */ 2654 if (!ticket->t_cnt) { 2655 xlog_grant_add_space(&log->l_reserve_head, ticket->t_unit_res); 2656 trace_xfs_log_ticket_regrant_exit(log, ticket); 2657 } 2658 2659 xfs_log_ticket_put(ticket); 2660 } 2661 2662 /* 2663 * Give back the space left from a reservation. 2664 * 2665 * All the information we need to make a correct determination of space left 2666 * is present. For non-permanent reservations, things are quite easy. The 2667 * count should have been decremented to zero. We only need to deal with the 2668 * space remaining in the current reservation part of the ticket. If the 2669 * ticket contains a permanent reservation, there may be left over space which 2670 * needs to be released. A count of N means that N-1 refills of the current 2671 * reservation can be done before we need to ask for more space. The first 2672 * one goes to fill up the first current reservation. Once we run out of 2673 * space, the count will stay at zero and the only space remaining will be 2674 * in the current reservation field. 2675 */ 2676 void 2677 xfs_log_ticket_ungrant( 2678 struct xlog *log, 2679 struct xlog_ticket *ticket) 2680 { 2681 int bytes; 2682 2683 trace_xfs_log_ticket_ungrant(log, ticket); 2684 2685 if (ticket->t_cnt > 0) 2686 ticket->t_cnt--; 2687 2688 trace_xfs_log_ticket_ungrant_sub(log, ticket); 2689 2690 /* 2691 * If this is a permanent reservation ticket, we may be able to free 2692 * up more space based on the remaining count. 2693 */ 2694 bytes = ticket->t_curr_res; 2695 if (ticket->t_cnt > 0) { 2696 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV); 2697 bytes += ticket->t_unit_res*ticket->t_cnt; 2698 } 2699 2700 xlog_grant_sub_space(&log->l_reserve_head, bytes); 2701 xlog_grant_sub_space(&log->l_write_head, bytes); 2702 2703 trace_xfs_log_ticket_ungrant_exit(log, ticket); 2704 2705 xfs_log_space_wake(log->l_mp); 2706 xfs_log_ticket_put(ticket); 2707 } 2708 2709 /* 2710 * This routine will mark the current iclog in the ring as WANT_SYNC and move 2711 * the current iclog pointer to the next iclog in the ring. 2712 */ 2713 void 2714 xlog_state_switch_iclogs( 2715 struct xlog *log, 2716 struct xlog_in_core *iclog, 2717 int eventual_size) 2718 { 2719 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE); 2720 assert_spin_locked(&log->l_icloglock); 2721 trace_xlog_iclog_switch(iclog, _RET_IP_); 2722 2723 if (!eventual_size) 2724 eventual_size = iclog->ic_offset; 2725 iclog->ic_state = XLOG_STATE_WANT_SYNC; 2726 iclog->ic_header->h_prev_block = cpu_to_be32(log->l_prev_block); 2727 log->l_prev_block = log->l_curr_block; 2728 log->l_prev_cycle = log->l_curr_cycle; 2729 2730 /* roll log?: ic_offset changed later */ 2731 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize); 2732 2733 /* Round up to next log-sunit */ 2734 if (log->l_iclog_roundoff > BBSIZE) { 2735 uint32_t sunit_bb = BTOBB(log->l_iclog_roundoff); 2736 log->l_curr_block = roundup(log->l_curr_block, sunit_bb); 2737 } 2738 2739 if (log->l_curr_block >= log->l_logBBsize) { 2740 /* 2741 * Rewind the current block before the cycle is bumped to make 2742 * sure that the combined LSN never transiently moves forward 2743 * when the log wraps to the next cycle. This is to support the 2744 * unlocked sample of these fields from xlog_valid_lsn(). Most 2745 * other cases should acquire l_icloglock. 2746 */ 2747 log->l_curr_block -= log->l_logBBsize; 2748 ASSERT(log->l_curr_block >= 0); 2749 smp_wmb(); 2750 log->l_curr_cycle++; 2751 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM) 2752 log->l_curr_cycle++; 2753 } 2754 ASSERT(iclog == log->l_iclog); 2755 log->l_iclog = iclog->ic_next; 2756 } 2757 2758 /* 2759 * Force the iclog to disk and check if the iclog has been completed before 2760 * xlog_force_iclog() returns. This can happen on synchronous (e.g. 2761 * pmem) or fast async storage because we drop the icloglock to issue the IO. 2762 * If completion has already occurred, tell the caller so that it can avoid an 2763 * unnecessary wait on the iclog. 2764 */ 2765 static int 2766 xlog_force_and_check_iclog( 2767 struct xlog *log, 2768 struct xlog_in_core *iclog, 2769 bool *completed) 2770 __releases(&log->l_icloglock) 2771 __acquires(&log->l_icloglock) 2772 { 2773 xfs_lsn_t lsn = be64_to_cpu(iclog->ic_header->h_lsn); 2774 int error; 2775 2776 *completed = false; 2777 error = xlog_force_iclog(log, iclog); 2778 if (error) 2779 return error; 2780 2781 /* 2782 * If the iclog has already been completed and reused the header LSN 2783 * will have been rewritten by completion 2784 */ 2785 if (be64_to_cpu(iclog->ic_header->h_lsn) != lsn) 2786 *completed = true; 2787 return 0; 2788 } 2789 2790 /* 2791 * Write out all data in the in-core log as of this exact moment in time. 2792 * 2793 * Data may be written to the in-core log during this call. However, 2794 * we don't guarantee this data will be written out. A change from past 2795 * implementation means this routine will *not* write out zero length LRs. 2796 * 2797 * Basically, we try and perform an intelligent scan of the in-core logs. 2798 * If we determine there is no flushable data, we just return. There is no 2799 * flushable data if: 2800 * 2801 * 1. the current iclog is active and has no data; the previous iclog 2802 * is in the active or dirty state. 2803 * 2. the current iclog is dirty, and the previous iclog is in the 2804 * active or dirty state. 2805 * 2806 * We may sleep if: 2807 * 2808 * 1. the current iclog is not in the active nor dirty state. 2809 * 2. the current iclog dirty, and the previous iclog is not in the 2810 * active nor dirty state. 2811 * 3. the current iclog is active, and there is another thread writing 2812 * to this particular iclog. 2813 * 4. a) the current iclog is active and has no other writers 2814 * b) when we return from flushing out this iclog, it is still 2815 * not in the active nor dirty state. 2816 */ 2817 int 2818 xfs_log_force( 2819 struct xfs_mount *mp, 2820 uint flags) 2821 { 2822 struct xlog *log = mp->m_log; 2823 struct xlog_in_core *iclog; 2824 2825 XFS_STATS_INC(mp, xs_log_force); 2826 trace_xfs_log_force(mp, 0, _RET_IP_); 2827 2828 xlog_cil_force(log); 2829 2830 spin_lock(&log->l_icloglock); 2831 if (xlog_is_shutdown(log)) 2832 goto out_error; 2833 2834 iclog = log->l_iclog; 2835 trace_xlog_iclog_force(iclog, _RET_IP_); 2836 2837 if (iclog->ic_state == XLOG_STATE_DIRTY || 2838 (iclog->ic_state == XLOG_STATE_ACTIVE && 2839 atomic_read(&iclog->ic_refcnt) == 0 && iclog->ic_offset == 0)) { 2840 /* 2841 * If the head is dirty or (active and empty), then we need to 2842 * look at the previous iclog. 2843 * 2844 * If the previous iclog is active or dirty we are done. There 2845 * is nothing to sync out. Otherwise, we attach ourselves to the 2846 * previous iclog and go to sleep. 2847 */ 2848 iclog = iclog->ic_prev; 2849 } else if (iclog->ic_state == XLOG_STATE_ACTIVE) { 2850 if (atomic_read(&iclog->ic_refcnt) == 0) { 2851 /* We have exclusive access to this iclog. */ 2852 bool completed; 2853 2854 if (xlog_force_and_check_iclog(log, iclog, &completed)) 2855 goto out_error; 2856 2857 if (completed) 2858 goto out_unlock; 2859 } else { 2860 /* 2861 * Someone else is still writing to this iclog, so we 2862 * need to ensure that when they release the iclog it 2863 * gets synced immediately as we may be waiting on it. 2864 */ 2865 xlog_state_switch_iclogs(log, iclog, 0); 2866 } 2867 } 2868 2869 /* 2870 * The iclog we are about to wait on may contain the checkpoint pushed 2871 * by the above xlog_cil_force() call, but it may not have been pushed 2872 * to disk yet. Like the ACTIVE case above, we need to make sure caches 2873 * are flushed when this iclog is written. 2874 */ 2875 if (iclog->ic_state == XLOG_STATE_WANT_SYNC) 2876 iclog->ic_flags |= XLOG_ICL_NEED_FLUSH | XLOG_ICL_NEED_FUA; 2877 2878 if (flags & XFS_LOG_SYNC) 2879 return xlog_wait_on_iclog(log, iclog); 2880 out_unlock: 2881 spin_unlock(&log->l_icloglock); 2882 return 0; 2883 out_error: 2884 spin_unlock(&log->l_icloglock); 2885 return -EIO; 2886 } 2887 2888 /* 2889 * Force the log to a specific LSN. 2890 * 2891 * If an iclog with that lsn can be found: 2892 * If it is in the DIRTY state, just return. 2893 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC 2894 * state and go to sleep or return. 2895 * If it is in any other state, go to sleep or return. 2896 * 2897 * Synchronous forces are implemented with a wait queue. All callers trying 2898 * to force a given lsn to disk must wait on the queue attached to the 2899 * specific in-core log. When given in-core log finally completes its write 2900 * to disk, that thread will wake up all threads waiting on the queue. 2901 */ 2902 static int 2903 xlog_force_lsn( 2904 struct xlog *log, 2905 xfs_lsn_t lsn, 2906 uint flags, 2907 int *log_flushed, 2908 bool already_slept) 2909 { 2910 struct xlog_in_core *iclog; 2911 bool completed; 2912 2913 spin_lock(&log->l_icloglock); 2914 if (xlog_is_shutdown(log)) 2915 goto out_error; 2916 2917 iclog = log->l_iclog; 2918 while (be64_to_cpu(iclog->ic_header->h_lsn) != lsn) { 2919 trace_xlog_iclog_force_lsn(iclog, _RET_IP_); 2920 iclog = iclog->ic_next; 2921 if (iclog == log->l_iclog) 2922 goto out_unlock; 2923 } 2924 2925 switch (iclog->ic_state) { 2926 case XLOG_STATE_ACTIVE: 2927 /* 2928 * We sleep here if we haven't already slept (e.g. this is the 2929 * first time we've looked at the correct iclog buf) and the 2930 * buffer before us is going to be sync'ed. The reason for this 2931 * is that if we are doing sync transactions here, by waiting 2932 * for the previous I/O to complete, we can allow a few more 2933 * transactions into this iclog before we close it down. 2934 * 2935 * Otherwise, we mark the buffer WANT_SYNC, and bump up the 2936 * refcnt so we can release the log (which drops the ref count). 2937 * The state switch keeps new transaction commits from using 2938 * this buffer. When the current commits finish writing into 2939 * the buffer, the refcount will drop to zero and the buffer 2940 * will go out then. 2941 */ 2942 if (!already_slept && 2943 (iclog->ic_prev->ic_state == XLOG_STATE_WANT_SYNC || 2944 iclog->ic_prev->ic_state == XLOG_STATE_SYNCING)) { 2945 xlog_wait(&iclog->ic_prev->ic_write_wait, 2946 &log->l_icloglock); 2947 return -EAGAIN; 2948 } 2949 if (xlog_force_and_check_iclog(log, iclog, &completed)) 2950 goto out_error; 2951 if (log_flushed) 2952 *log_flushed = 1; 2953 if (completed) 2954 goto out_unlock; 2955 break; 2956 case XLOG_STATE_WANT_SYNC: 2957 /* 2958 * This iclog may contain the checkpoint pushed by the 2959 * xlog_cil_force_seq() call, but there are other writers still 2960 * accessing it so it hasn't been pushed to disk yet. Like the 2961 * ACTIVE case above, we need to make sure caches are flushed 2962 * when this iclog is written. 2963 */ 2964 iclog->ic_flags |= XLOG_ICL_NEED_FLUSH | XLOG_ICL_NEED_FUA; 2965 break; 2966 default: 2967 /* 2968 * The entire checkpoint was written by the CIL force and is on 2969 * its way to disk already. It will be stable when it 2970 * completes, so we don't need to manipulate caches here at all. 2971 * We just need to wait for completion if necessary. 2972 */ 2973 break; 2974 } 2975 2976 if (flags & XFS_LOG_SYNC) 2977 return xlog_wait_on_iclog(log, iclog); 2978 out_unlock: 2979 spin_unlock(&log->l_icloglock); 2980 return 0; 2981 out_error: 2982 spin_unlock(&log->l_icloglock); 2983 return -EIO; 2984 } 2985 2986 /* 2987 * Force the log to a specific checkpoint sequence. 2988 * 2989 * First force the CIL so that all the required changes have been flushed to the 2990 * iclogs. If the CIL force completed it will return a commit LSN that indicates 2991 * the iclog that needs to be flushed to stable storage. If the caller needs 2992 * a synchronous log force, we will wait on the iclog with the LSN returned by 2993 * xlog_cil_force_seq() to be completed. 2994 */ 2995 int 2996 xfs_log_force_seq( 2997 struct xfs_mount *mp, 2998 xfs_csn_t seq, 2999 uint flags, 3000 int *log_flushed) 3001 { 3002 struct xlog *log = mp->m_log; 3003 xfs_lsn_t lsn; 3004 int ret; 3005 ASSERT(seq != 0); 3006 3007 XFS_STATS_INC(mp, xs_log_force); 3008 trace_xfs_log_force(mp, seq, _RET_IP_); 3009 3010 lsn = xlog_cil_force_seq(log, seq); 3011 if (lsn == NULLCOMMITLSN) 3012 return 0; 3013 3014 ret = xlog_force_lsn(log, lsn, flags, log_flushed, false); 3015 if (ret == -EAGAIN) { 3016 XFS_STATS_INC(mp, xs_log_force_sleep); 3017 ret = xlog_force_lsn(log, lsn, flags, log_flushed, true); 3018 } 3019 return ret; 3020 } 3021 3022 /* 3023 * Free a used ticket when its refcount falls to zero. 3024 */ 3025 void 3026 xfs_log_ticket_put( 3027 struct xlog_ticket *ticket) 3028 { 3029 ASSERT(atomic_read(&ticket->t_ref) > 0); 3030 if (atomic_dec_and_test(&ticket->t_ref)) 3031 kmem_cache_free(xfs_log_ticket_cache, ticket); 3032 } 3033 3034 struct xlog_ticket * 3035 xfs_log_ticket_get( 3036 struct xlog_ticket *ticket) 3037 { 3038 ASSERT(atomic_read(&ticket->t_ref) > 0); 3039 atomic_inc(&ticket->t_ref); 3040 return ticket; 3041 } 3042 3043 /* 3044 * Figure out the total log space unit (in bytes) that would be 3045 * required for a log ticket. 3046 */ 3047 static int 3048 xlog_calc_unit_res( 3049 struct xlog *log, 3050 int unit_bytes, 3051 int *niclogs) 3052 { 3053 int iclog_space; 3054 uint num_headers; 3055 3056 /* 3057 * Permanent reservations have up to 'cnt'-1 active log operations 3058 * in the log. A unit in this case is the amount of space for one 3059 * of these log operations. Normal reservations have a cnt of 1 3060 * and their unit amount is the total amount of space required. 3061 * 3062 * The following lines of code account for non-transaction data 3063 * which occupy space in the on-disk log. 3064 * 3065 * Normal form of a transaction is: 3066 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph> 3067 * and then there are LR hdrs, split-recs and roundoff at end of syncs. 3068 * 3069 * We need to account for all the leadup data and trailer data 3070 * around the transaction data. 3071 * And then we need to account for the worst case in terms of using 3072 * more space. 3073 * The worst case will happen if: 3074 * - the placement of the transaction happens to be such that the 3075 * roundoff is at its maximum 3076 * - the transaction data is synced before the commit record is synced 3077 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff> 3078 * Therefore the commit record is in its own Log Record. 3079 * This can happen as the commit record is called with its 3080 * own region to xlog_write(). 3081 * This then means that in the worst case, roundoff can happen for 3082 * the commit-rec as well. 3083 * The commit-rec is smaller than padding in this scenario and so it is 3084 * not added separately. 3085 */ 3086 3087 /* for trans header */ 3088 unit_bytes += sizeof(struct xlog_op_header); 3089 unit_bytes += sizeof(struct xfs_trans_header); 3090 3091 /* for start-rec */ 3092 unit_bytes += sizeof(struct xlog_op_header); 3093 3094 /* 3095 * for LR headers - the space for data in an iclog is the size minus 3096 * the space used for the headers. If we use the iclog size, then we 3097 * undercalculate the number of headers required. 3098 * 3099 * Furthermore - the addition of op headers for split-recs might 3100 * increase the space required enough to require more log and op 3101 * headers, so take that into account too. 3102 * 3103 * IMPORTANT: This reservation makes the assumption that if this 3104 * transaction is the first in an iclog and hence has the LR headers 3105 * accounted to it, then the remaining space in the iclog is 3106 * exclusively for this transaction. i.e. if the transaction is larger 3107 * than the iclog, it will be the only thing in that iclog. 3108 * Fundamentally, this means we must pass the entire log vector to 3109 * xlog_write to guarantee this. 3110 */ 3111 iclog_space = log->l_iclog_size - log->l_iclog_hsize; 3112 num_headers = howmany(unit_bytes, iclog_space); 3113 3114 /* for split-recs - ophdrs added when data split over LRs */ 3115 unit_bytes += sizeof(struct xlog_op_header) * num_headers; 3116 3117 /* add extra header reservations if we overrun */ 3118 while (!num_headers || 3119 howmany(unit_bytes, iclog_space) > num_headers) { 3120 unit_bytes += sizeof(struct xlog_op_header); 3121 num_headers++; 3122 } 3123 unit_bytes += log->l_iclog_hsize * num_headers; 3124 3125 /* for commit-rec LR header - note: padding will subsume the ophdr */ 3126 unit_bytes += log->l_iclog_hsize; 3127 3128 /* roundoff padding for transaction data and one for commit record */ 3129 unit_bytes += 2 * log->l_iclog_roundoff; 3130 3131 if (niclogs) 3132 *niclogs = num_headers; 3133 return unit_bytes; 3134 } 3135 3136 int 3137 xfs_log_calc_unit_res( 3138 struct xfs_mount *mp, 3139 int unit_bytes) 3140 { 3141 return xlog_calc_unit_res(mp->m_log, unit_bytes, NULL); 3142 } 3143 3144 /* 3145 * Allocate and initialise a new log ticket. 3146 */ 3147 struct xlog_ticket * 3148 xlog_ticket_alloc( 3149 struct xlog *log, 3150 int unit_bytes, 3151 int cnt, 3152 bool permanent) 3153 { 3154 struct xlog_ticket *tic; 3155 int unit_res; 3156 3157 tic = kmem_cache_zalloc(xfs_log_ticket_cache, 3158 GFP_KERNEL | __GFP_NOFAIL); 3159 3160 unit_res = xlog_calc_unit_res(log, unit_bytes, &tic->t_iclog_hdrs); 3161 3162 atomic_set(&tic->t_ref, 1); 3163 tic->t_task = current; 3164 INIT_LIST_HEAD(&tic->t_queue); 3165 tic->t_unit_res = unit_res; 3166 tic->t_curr_res = unit_res; 3167 tic->t_cnt = cnt; 3168 tic->t_ocnt = cnt; 3169 tic->t_tid = get_random_u32(); 3170 if (permanent) 3171 tic->t_flags |= XLOG_TIC_PERM_RESERV; 3172 3173 return tic; 3174 } 3175 3176 #if defined(DEBUG) 3177 static void 3178 xlog_verify_dump_tail( 3179 struct xlog *log, 3180 struct xlog_in_core *iclog) 3181 { 3182 xfs_alert(log->l_mp, 3183 "ran out of log space tail 0x%llx/0x%llx, head lsn 0x%llx, head 0x%x/0x%x, prev head 0x%x/0x%x", 3184 iclog ? be64_to_cpu(iclog->ic_header->h_tail_lsn) : -1, 3185 atomic64_read(&log->l_tail_lsn), 3186 log->l_ailp->ail_head_lsn, 3187 log->l_curr_cycle, log->l_curr_block, 3188 log->l_prev_cycle, log->l_prev_block); 3189 xfs_alert(log->l_mp, 3190 "write grant 0x%llx, reserve grant 0x%llx, tail_space 0x%llx, size 0x%x, iclog flags 0x%x", 3191 atomic64_read(&log->l_write_head.grant), 3192 atomic64_read(&log->l_reserve_head.grant), 3193 log->l_tail_space, log->l_logsize, 3194 iclog ? iclog->ic_flags : -1); 3195 } 3196 3197 /* Check if the new iclog will fit in the log. */ 3198 STATIC void 3199 xlog_verify_tail_lsn( 3200 struct xlog *log, 3201 struct xlog_in_core *iclog) 3202 { 3203 xfs_lsn_t tail_lsn = be64_to_cpu(iclog->ic_header->h_tail_lsn); 3204 int blocks; 3205 3206 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) { 3207 blocks = log->l_logBBsize - 3208 (log->l_prev_block - BLOCK_LSN(tail_lsn)); 3209 if (blocks < BTOBB(iclog->ic_offset) + 3210 BTOBB(log->l_iclog_hsize)) { 3211 xfs_emerg(log->l_mp, 3212 "%s: ran out of log space", __func__); 3213 xlog_verify_dump_tail(log, iclog); 3214 } 3215 return; 3216 } 3217 3218 if (CYCLE_LSN(tail_lsn) + 1 != log->l_prev_cycle) { 3219 xfs_emerg(log->l_mp, "%s: head has wrapped tail.", __func__); 3220 xlog_verify_dump_tail(log, iclog); 3221 return; 3222 } 3223 if (BLOCK_LSN(tail_lsn) == log->l_prev_block) { 3224 xfs_emerg(log->l_mp, "%s: tail wrapped", __func__); 3225 xlog_verify_dump_tail(log, iclog); 3226 return; 3227 } 3228 3229 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block; 3230 if (blocks < BTOBB(iclog->ic_offset) + 1) { 3231 xfs_emerg(log->l_mp, "%s: ran out of iclog space", __func__); 3232 xlog_verify_dump_tail(log, iclog); 3233 } 3234 } 3235 3236 /* 3237 * Perform a number of checks on the iclog before writing to disk. 3238 * 3239 * 1. Make sure the iclogs are still circular 3240 * 2. Make sure we have a good magic number 3241 * 3. Make sure we don't have magic numbers in the data 3242 * 4. Check fields of each log operation header for: 3243 * A. Valid client identifier 3244 * B. tid ptr value falls in valid ptr space (user space code) 3245 * C. Length in log record header is correct according to the 3246 * individual operation headers within record. 3247 * 5. When a bwrite will occur within 5 blocks of the front of the physical 3248 * log, check the preceding blocks of the physical log to make sure all 3249 * the cycle numbers agree with the current cycle number. 3250 */ 3251 STATIC void 3252 xlog_verify_iclog( 3253 struct xlog *log, 3254 struct xlog_in_core *iclog, 3255 int count) 3256 { 3257 struct xlog_rec_header *rhead = iclog->ic_header; 3258 struct xlog_in_core *icptr; 3259 void *base_ptr, *ptr; 3260 ptrdiff_t field_offset; 3261 uint8_t clientid; 3262 int len, i, op_len; 3263 int idx; 3264 3265 /* check validity of iclog pointers */ 3266 spin_lock(&log->l_icloglock); 3267 icptr = log->l_iclog; 3268 for (i = 0; i < log->l_iclog_bufs; i++, icptr = icptr->ic_next) 3269 ASSERT(icptr); 3270 3271 if (icptr != log->l_iclog) 3272 xfs_emerg(log->l_mp, "%s: corrupt iclog ring", __func__); 3273 spin_unlock(&log->l_icloglock); 3274 3275 /* check log magic numbers */ 3276 if (rhead->h_magicno != cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) 3277 xfs_emerg(log->l_mp, "%s: invalid magic num", __func__); 3278 3279 base_ptr = ptr = rhead; 3280 for (ptr += BBSIZE; ptr < base_ptr + count; ptr += BBSIZE) { 3281 if (*(__be32 *)ptr == cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) 3282 xfs_emerg(log->l_mp, "%s: unexpected magic num", 3283 __func__); 3284 } 3285 3286 /* check fields */ 3287 len = be32_to_cpu(rhead->h_num_logops); 3288 base_ptr = ptr = iclog->ic_datap; 3289 for (i = 0; i < len; i++) { 3290 struct xlog_op_header *ophead = ptr; 3291 void *p = &ophead->oh_clientid; 3292 3293 /* clientid is only 1 byte */ 3294 field_offset = p - base_ptr; 3295 if (field_offset & 0x1ff) { 3296 clientid = ophead->oh_clientid; 3297 } else { 3298 idx = BTOBBT((void *)&ophead->oh_clientid - iclog->ic_datap); 3299 clientid = xlog_get_client_id(*xlog_cycle_data(rhead, idx)); 3300 } 3301 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG) { 3302 xfs_warn(log->l_mp, 3303 "%s: op %d invalid clientid %d op "PTR_FMT" offset 0x%lx", 3304 __func__, i, clientid, ophead, 3305 (unsigned long)field_offset); 3306 } 3307 3308 /* check length */ 3309 p = &ophead->oh_len; 3310 field_offset = p - base_ptr; 3311 if (field_offset & 0x1ff) { 3312 op_len = be32_to_cpu(ophead->oh_len); 3313 } else { 3314 idx = BTOBBT((void *)&ophead->oh_len - iclog->ic_datap); 3315 op_len = be32_to_cpu(*xlog_cycle_data(rhead, idx)); 3316 } 3317 ptr += sizeof(struct xlog_op_header) + op_len; 3318 } 3319 } 3320 #endif 3321 3322 /* 3323 * Perform a forced shutdown on the log. 3324 * 3325 * This can be called from low level log code to trigger a shutdown, or from the 3326 * high level mount shutdown code when the mount shuts down. 3327 * 3328 * Our main objectives here are to make sure that: 3329 * a. if the shutdown was not due to a log IO error, flush the logs to 3330 * disk. Anything modified after this is ignored. 3331 * b. the log gets atomically marked 'XLOG_IO_ERROR' for all interested 3332 * parties to find out. Nothing new gets queued after this is done. 3333 * c. Tasks sleeping on log reservations, pinned objects and 3334 * other resources get woken up. 3335 * d. The mount is also marked as shut down so that log triggered shutdowns 3336 * still behave the same as if they called xfs_forced_shutdown(). 3337 * 3338 * Return true if the shutdown cause was a log IO error and we actually shut the 3339 * log down. 3340 */ 3341 bool 3342 xlog_force_shutdown( 3343 struct xlog *log, 3344 uint32_t shutdown_flags) 3345 { 3346 bool log_error = (shutdown_flags & SHUTDOWN_LOG_IO_ERROR); 3347 3348 if (!log) 3349 return false; 3350 3351 /* 3352 * Ensure that there is only ever one log shutdown being processed. 3353 * If we allow the log force below on a second pass after shutting 3354 * down the log, we risk deadlocking the CIL push as it may require 3355 * locks on objects the current shutdown context holds (e.g. taking 3356 * buffer locks to abort buffers on last unpin of buf log items). 3357 */ 3358 if (test_and_set_bit(XLOG_SHUTDOWN_STARTED, &log->l_opstate)) 3359 return false; 3360 3361 /* 3362 * Flush all the completed transactions to disk before marking the log 3363 * being shut down. We need to do this first as shutting down the log 3364 * before the force will prevent the log force from flushing the iclogs 3365 * to disk. 3366 * 3367 * When we are in recovery, there are no transactions to flush, and 3368 * we don't want to touch the log because we don't want to perturb the 3369 * current head/tail for future recovery attempts. Hence we need to 3370 * avoid a log force in this case. 3371 * 3372 * If we are shutting down due to a log IO error, then we must avoid 3373 * trying to write the log as that may just result in more IO errors and 3374 * an endless shutdown/force loop. 3375 */ 3376 if (!log_error && !xlog_in_recovery(log)) 3377 xfs_log_force(log->l_mp, XFS_LOG_SYNC); 3378 3379 /* 3380 * Atomically set the shutdown state. If the shutdown state is already 3381 * set, there someone else is performing the shutdown and so we are done 3382 * here. This should never happen because we should only ever get called 3383 * once by the first shutdown caller. 3384 * 3385 * Much of the log state machine transitions assume that shutdown state 3386 * cannot change once they hold the log->l_icloglock. Hence we need to 3387 * hold that lock here, even though we use the atomic test_and_set_bit() 3388 * operation to set the shutdown state. 3389 */ 3390 spin_lock(&log->l_icloglock); 3391 if (test_and_set_bit(XLOG_IO_ERROR, &log->l_opstate)) { 3392 spin_unlock(&log->l_icloglock); 3393 ASSERT(0); 3394 return false; 3395 } 3396 spin_unlock(&log->l_icloglock); 3397 3398 /* 3399 * If this log shutdown also sets the mount shutdown state, issue a 3400 * shutdown warning message. 3401 */ 3402 if (!xfs_set_shutdown(log->l_mp)) { 3403 xfs_alert_tag(log->l_mp, XFS_PTAG_SHUTDOWN_LOGERROR, 3404 "Filesystem has been shut down due to log error (0x%x).", 3405 shutdown_flags); 3406 xfs_alert(log->l_mp, 3407 "Please unmount the filesystem and rectify the problem(s)."); 3408 if (xfs_error_level >= XFS_ERRLEVEL_HIGH) 3409 xfs_stack_trace(); 3410 } 3411 3412 /* 3413 * We don't want anybody waiting for log reservations after this. That 3414 * means we have to wake up everybody queued up on reserveq as well as 3415 * writeq. In addition, we make sure in xlog_{re}grant_log_space that 3416 * we don't enqueue anything once the SHUTDOWN flag is set, and this 3417 * action is protected by the grant locks. 3418 */ 3419 xlog_grant_head_wake_all(&log->l_reserve_head); 3420 xlog_grant_head_wake_all(&log->l_write_head); 3421 3422 /* 3423 * Wake up everybody waiting on xfs_log_force. Wake the CIL push first 3424 * as if the log writes were completed. The abort handling in the log 3425 * item committed callback functions will do this again under lock to 3426 * avoid races. 3427 */ 3428 spin_lock(&log->l_cilp->xc_push_lock); 3429 wake_up_all(&log->l_cilp->xc_start_wait); 3430 wake_up_all(&log->l_cilp->xc_commit_wait); 3431 spin_unlock(&log->l_cilp->xc_push_lock); 3432 3433 spin_lock(&log->l_icloglock); 3434 xlog_state_shutdown_callbacks(log); 3435 spin_unlock(&log->l_icloglock); 3436 3437 wake_up_var(&log->l_opstate); 3438 if (IS_ENABLED(CONFIG_XFS_RT) && xfs_has_zoned(log->l_mp)) 3439 xfs_zoned_wake_all(log->l_mp); 3440 3441 return log_error; 3442 } 3443 3444 STATIC int 3445 xlog_iclogs_empty( 3446 struct xlog *log) 3447 { 3448 struct xlog_in_core *iclog = log->l_iclog; 3449 3450 do { 3451 /* endianness does not matter here, zero is zero in 3452 * any language. 3453 */ 3454 if (iclog->ic_header->h_num_logops) 3455 return 0; 3456 iclog = iclog->ic_next; 3457 } while (iclog != log->l_iclog); 3458 3459 return 1; 3460 } 3461 3462 /* 3463 * Verify that an LSN stamped into a piece of metadata is valid. This is 3464 * intended for use in read verifiers on v5 superblocks. 3465 */ 3466 bool 3467 xfs_log_check_lsn( 3468 struct xfs_mount *mp, 3469 xfs_lsn_t lsn) 3470 { 3471 struct xlog *log = mp->m_log; 3472 bool valid; 3473 3474 /* 3475 * norecovery mode skips mount-time log processing and unconditionally 3476 * resets the in-core LSN. We can't validate in this mode, but 3477 * modifications are not allowed anyways so just return true. 3478 */ 3479 if (xfs_has_norecovery(mp)) 3480 return true; 3481 3482 /* 3483 * Some metadata LSNs are initialized to NULL (e.g., the agfl). This is 3484 * handled by recovery and thus safe to ignore here. 3485 */ 3486 if (lsn == NULLCOMMITLSN) 3487 return true; 3488 3489 valid = xlog_valid_lsn(mp->m_log, lsn); 3490 3491 /* warn the user about what's gone wrong before verifier failure */ 3492 if (!valid) { 3493 spin_lock(&log->l_icloglock); 3494 xfs_warn(mp, 3495 "Corruption warning: Metadata has LSN (%d:%d) ahead of current LSN (%d:%d). " 3496 "Please unmount and run xfs_repair (>= v4.3) to resolve.", 3497 CYCLE_LSN(lsn), BLOCK_LSN(lsn), 3498 log->l_curr_cycle, log->l_curr_block); 3499 spin_unlock(&log->l_icloglock); 3500 } 3501 3502 return valid; 3503 } 3504