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