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 25 struct kmem_cache *xfs_log_ticket_cache; 26 27 /* Local miscellaneous function prototypes */ 28 STATIC struct xlog * 29 xlog_alloc_log( 30 struct xfs_mount *mp, 31 struct xfs_buftarg *log_target, 32 xfs_daddr_t blk_offset, 33 int num_bblks); 34 STATIC void 35 xlog_dealloc_log( 36 struct xlog *log); 37 38 /* local state machine functions */ 39 STATIC void xlog_state_done_syncing( 40 struct xlog_in_core *iclog); 41 STATIC void xlog_state_do_callback( 42 struct xlog *log); 43 STATIC int 44 xlog_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); 50 STATIC void 51 xlog_sync( 52 struct xlog *log, 53 struct xlog_in_core *iclog, 54 struct xlog_ticket *ticket); 55 #if defined(DEBUG) 56 STATIC void 57 xlog_verify_iclog( 58 struct xlog *log, 59 struct xlog_in_core *iclog, 60 int count); 61 STATIC void 62 xlog_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 70 STATIC int 71 xlog_iclogs_empty( 72 struct xlog *log); 73 74 static int 75 xfs_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 */ 94 void * 95 xlog_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_len + sizeof(struct xlog_op_header); 113 if (!IS_ALIGNED(len, sizeof(uint64_t))) { 114 lv->lv_buf_len = 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_len; 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 133 static inline void 134 xlog_grant_sub_space( 135 struct xlog_grant_head *head, 136 int64_t bytes) 137 { 138 atomic64_sub(bytes, &head->grant); 139 } 140 141 static inline void 142 xlog_grant_add_space( 143 struct xlog_grant_head *head, 144 int64_t bytes) 145 { 146 atomic64_add(bytes, &head->grant); 147 } 148 149 static void 150 xlog_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 158 void 159 xlog_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 */ 178 static uint64_t 179 xlog_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 193 STATIC void 194 xlog_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 205 static inline int 206 xlog_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 222 STATIC bool 223 xlog_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 244 STATIC int 245 xlog_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; 277 shutdown: 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 */ 299 STATIC int 300 xlog_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 336 bool 337 xfs_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 */ 360 int 361 xfs_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 396 out_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 */ 415 int 416 xfs_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 447 out_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 */ 472 static void 473 xlog_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 */ 518 int 519 xlog_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 */ 583 int 584 xfs_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 693 out_destroy_ail: 694 xfs_trans_ail_destroy(mp); 695 out_free_log: 696 xlog_dealloc_log(log); 697 out: 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 */ 711 int 712 xfs_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 */ 783 void 784 xfs_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 */ 795 static inline int 796 xlog_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 */ 810 static void 811 xlog_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 */ 829 int 830 xlog_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 */ 856 static int 857 xlog_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 */ 900 static void 901 xlog_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 */ 918 out_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 933 static void 934 xfs_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 */ 952 static void 953 xfs_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_TEST_ERROR(xfs_fs_has_sickness(mp, XFS_SICK_FS_COUNTERS), mp, 973 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 */ 991 int 992 xfs_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 1027 void 1028 xfs_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 */ 1042 void 1043 xfs_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 1065 void 1066 xfs_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 */ 1087 void 1088 xfs_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 */ 1133 static bool 1134 xfs_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 */ 1176 static int 1177 xfs_log_cover( 1178 struct xfs_mount *mp) 1179 { 1180 int error = 0; 1181 bool need_covered; 1182 1183 ASSERT((xlog_cil_empty(mp->m_log) && xlog_iclogs_empty(mp->m_log) && 1184 !xfs_ail_min_lsn(mp->m_log->l_ailp)) || 1185 xlog_is_shutdown(mp->m_log)); 1186 1187 if (!xfs_log_writable(mp)) 1188 return 0; 1189 1190 /* 1191 * xfs_log_need_covered() is not idempotent because it progresses the 1192 * state machine if the log requires covering. Therefore, we must call 1193 * this function once and use the result until we've issued an sb sync. 1194 * Do so first to make that abundantly clear. 1195 * 1196 * Fall into the covering sequence if the log needs covering or the 1197 * mount has lazy superblock accounting to sync to disk. The sb sync 1198 * used for covering accumulates the in-core counters, so covering 1199 * handles this for us. 1200 */ 1201 need_covered = xfs_log_need_covered(mp); 1202 if (!need_covered && !xfs_has_lazysbcount(mp)) 1203 return 0; 1204 1205 /* 1206 * To cover the log, commit the superblock twice (at most) in 1207 * independent checkpoints. The first serves as a reference for the 1208 * tail pointer. The sync transaction and AIL push empties the AIL and 1209 * updates the in-core tail to the LSN of the first checkpoint. The 1210 * second commit updates the on-disk tail with the in-core LSN, 1211 * covering the log. Push the AIL one more time to leave it empty, as 1212 * we found it. 1213 */ 1214 do { 1215 error = xfs_sync_sb(mp, true); 1216 if (error) 1217 break; 1218 xfs_ail_push_all_sync(mp->m_ail); 1219 } while (xfs_log_need_covered(mp)); 1220 1221 return error; 1222 } 1223 1224 static void 1225 xlog_ioend_work( 1226 struct work_struct *work) 1227 { 1228 struct xlog_in_core *iclog = 1229 container_of(work, struct xlog_in_core, ic_end_io_work); 1230 struct xlog *log = iclog->ic_log; 1231 int error; 1232 1233 error = blk_status_to_errno(iclog->ic_bio.bi_status); 1234 #ifdef DEBUG 1235 /* treat writes with injected CRC errors as failed */ 1236 if (iclog->ic_fail_crc) 1237 error = -EIO; 1238 #endif 1239 1240 /* 1241 * Race to shutdown the filesystem if we see an error. 1242 */ 1243 if (XFS_TEST_ERROR(error, log->l_mp, XFS_ERRTAG_IODONE_IOERR)) { 1244 xfs_alert(log->l_mp, "log I/O error %d", error); 1245 xlog_force_shutdown(log, SHUTDOWN_LOG_IO_ERROR); 1246 } 1247 1248 xlog_state_done_syncing(iclog); 1249 bio_uninit(&iclog->ic_bio); 1250 1251 /* 1252 * Drop the lock to signal that we are done. Nothing references the 1253 * iclog after this, so an unmount waiting on this lock can now tear it 1254 * down safely. As such, it is unsafe to reference the iclog after the 1255 * unlock as we could race with it being freed. 1256 */ 1257 up(&iclog->ic_sema); 1258 } 1259 1260 /* 1261 * Return size of each in-core log record buffer. 1262 * 1263 * All machines get 8 x 32kB buffers by default, unless tuned otherwise. 1264 * 1265 * If the filesystem blocksize is too large, we may need to choose a 1266 * larger size since the directory code currently logs entire blocks. 1267 */ 1268 STATIC void 1269 xlog_get_iclog_buffer_size( 1270 struct xfs_mount *mp, 1271 struct xlog *log) 1272 { 1273 if (mp->m_logbufs <= 0) 1274 mp->m_logbufs = XLOG_MAX_ICLOGS; 1275 if (mp->m_logbsize <= 0) 1276 mp->m_logbsize = XLOG_BIG_RECORD_BSIZE; 1277 1278 log->l_iclog_bufs = mp->m_logbufs; 1279 log->l_iclog_size = mp->m_logbsize; 1280 1281 /* 1282 * # headers = size / 32k - one header holds cycles from 32k of data. 1283 */ 1284 log->l_iclog_heads = 1285 DIV_ROUND_UP(mp->m_logbsize, XLOG_HEADER_CYCLE_SIZE); 1286 log->l_iclog_hsize = log->l_iclog_heads << BBSHIFT; 1287 } 1288 1289 void 1290 xfs_log_work_queue( 1291 struct xfs_mount *mp) 1292 { 1293 queue_delayed_work(mp->m_sync_workqueue, &mp->m_log->l_work, 1294 msecs_to_jiffies(xfs_syncd_centisecs * 10)); 1295 } 1296 1297 /* 1298 * Clear the log incompat flags if we have the opportunity. 1299 * 1300 * This only happens if we're about to log the second dummy transaction as part 1301 * of covering the log. 1302 */ 1303 static inline void 1304 xlog_clear_incompat( 1305 struct xlog *log) 1306 { 1307 struct xfs_mount *mp = log->l_mp; 1308 1309 if (!xfs_sb_has_incompat_log_feature(&mp->m_sb, 1310 XFS_SB_FEAT_INCOMPAT_LOG_ALL)) 1311 return; 1312 1313 if (log->l_covered_state != XLOG_STATE_COVER_DONE2) 1314 return; 1315 1316 xfs_clear_incompat_log_features(mp); 1317 } 1318 1319 /* 1320 * Every sync period we need to unpin all items in the AIL and push them to 1321 * disk. If there is nothing dirty, then we might need to cover the log to 1322 * indicate that the filesystem is idle. 1323 */ 1324 static void 1325 xfs_log_worker( 1326 struct work_struct *work) 1327 { 1328 struct xlog *log = container_of(to_delayed_work(work), 1329 struct xlog, l_work); 1330 struct xfs_mount *mp = log->l_mp; 1331 1332 /* dgc: errors ignored - not fatal and nowhere to report them */ 1333 if (xfs_fs_writable(mp, SB_FREEZE_WRITE) && xfs_log_need_covered(mp)) { 1334 /* 1335 * Dump a transaction into the log that contains no real change. 1336 * This is needed to stamp the current tail LSN into the log 1337 * during the covering operation. 1338 * 1339 * We cannot use an inode here for this - that will push dirty 1340 * state back up into the VFS and then periodic inode flushing 1341 * will prevent log covering from making progress. Hence we 1342 * synchronously log the superblock instead to ensure the 1343 * superblock is immediately unpinned and can be written back. 1344 */ 1345 xlog_clear_incompat(log); 1346 xfs_sync_sb(mp, true); 1347 } else 1348 xfs_log_force(mp, 0); 1349 1350 /* start pushing all the metadata that is currently dirty */ 1351 xfs_ail_push_all(mp->m_ail); 1352 1353 /* queue us up again */ 1354 xfs_log_work_queue(mp); 1355 } 1356 1357 /* 1358 * This routine initializes some of the log structure for a given mount point. 1359 * Its primary purpose is to fill in enough, so recovery can occur. However, 1360 * some other stuff may be filled in too. 1361 */ 1362 STATIC struct xlog * 1363 xlog_alloc_log( 1364 struct xfs_mount *mp, 1365 struct xfs_buftarg *log_target, 1366 xfs_daddr_t blk_offset, 1367 int num_bblks) 1368 { 1369 struct xlog *log; 1370 xlog_rec_header_t *head; 1371 xlog_in_core_t **iclogp; 1372 xlog_in_core_t *iclog, *prev_iclog=NULL; 1373 int i; 1374 int error = -ENOMEM; 1375 uint log2_size = 0; 1376 1377 log = kzalloc(sizeof(struct xlog), GFP_KERNEL | __GFP_RETRY_MAYFAIL); 1378 if (!log) { 1379 xfs_warn(mp, "Log allocation failed: No memory!"); 1380 goto out; 1381 } 1382 1383 log->l_mp = mp; 1384 log->l_targ = log_target; 1385 log->l_logsize = BBTOB(num_bblks); 1386 log->l_logBBstart = blk_offset; 1387 log->l_logBBsize = num_bblks; 1388 log->l_covered_state = XLOG_STATE_COVER_IDLE; 1389 set_bit(XLOG_ACTIVE_RECOVERY, &log->l_opstate); 1390 INIT_DELAYED_WORK(&log->l_work, xfs_log_worker); 1391 INIT_LIST_HEAD(&log->r_dfops); 1392 1393 log->l_prev_block = -1; 1394 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */ 1395 xlog_assign_atomic_lsn(&log->l_tail_lsn, 1, 0); 1396 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */ 1397 1398 if (xfs_has_logv2(mp) && mp->m_sb.sb_logsunit > 1) 1399 log->l_iclog_roundoff = mp->m_sb.sb_logsunit; 1400 else 1401 log->l_iclog_roundoff = BBSIZE; 1402 1403 xlog_grant_head_init(&log->l_reserve_head); 1404 xlog_grant_head_init(&log->l_write_head); 1405 1406 error = -EFSCORRUPTED; 1407 if (xfs_has_sector(mp)) { 1408 log2_size = mp->m_sb.sb_logsectlog; 1409 if (log2_size < BBSHIFT) { 1410 xfs_warn(mp, "Log sector size too small (0x%x < 0x%x)", 1411 log2_size, BBSHIFT); 1412 goto out_free_log; 1413 } 1414 1415 log2_size -= BBSHIFT; 1416 if (log2_size > mp->m_sectbb_log) { 1417 xfs_warn(mp, "Log sector size too large (0x%x > 0x%x)", 1418 log2_size, mp->m_sectbb_log); 1419 goto out_free_log; 1420 } 1421 1422 /* for larger sector sizes, must have v2 or external log */ 1423 if (log2_size && log->l_logBBstart > 0 && 1424 !xfs_has_logv2(mp)) { 1425 xfs_warn(mp, 1426 "log sector size (0x%x) invalid for configuration.", 1427 log2_size); 1428 goto out_free_log; 1429 } 1430 } 1431 log->l_sectBBsize = 1 << log2_size; 1432 1433 xlog_get_iclog_buffer_size(mp, log); 1434 1435 spin_lock_init(&log->l_icloglock); 1436 init_waitqueue_head(&log->l_flush_wait); 1437 1438 iclogp = &log->l_iclog; 1439 /* 1440 * The amount of memory to allocate for the iclog structure is 1441 * rather funky due to the way the structure is defined. It is 1442 * done this way so that we can use different sizes for machines 1443 * with different amounts of memory. See the definition of 1444 * xlog_in_core_t in xfs_log_priv.h for details. 1445 */ 1446 ASSERT(log->l_iclog_size >= 4096); 1447 for (i = 0; i < log->l_iclog_bufs; i++) { 1448 size_t bvec_size = howmany(log->l_iclog_size, PAGE_SIZE) * 1449 sizeof(struct bio_vec); 1450 1451 iclog = kzalloc(sizeof(*iclog) + bvec_size, 1452 GFP_KERNEL | __GFP_RETRY_MAYFAIL); 1453 if (!iclog) 1454 goto out_free_iclog; 1455 1456 *iclogp = iclog; 1457 iclog->ic_prev = prev_iclog; 1458 prev_iclog = iclog; 1459 1460 iclog->ic_data = kvzalloc(log->l_iclog_size, 1461 GFP_KERNEL | __GFP_RETRY_MAYFAIL); 1462 if (!iclog->ic_data) 1463 goto out_free_iclog; 1464 head = &iclog->ic_header; 1465 memset(head, 0, sizeof(xlog_rec_header_t)); 1466 head->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM); 1467 head->h_version = cpu_to_be32( 1468 xfs_has_logv2(log->l_mp) ? 2 : 1); 1469 head->h_size = cpu_to_be32(log->l_iclog_size); 1470 /* new fields */ 1471 head->h_fmt = cpu_to_be32(XLOG_FMT); 1472 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t)); 1473 1474 iclog->ic_size = log->l_iclog_size - log->l_iclog_hsize; 1475 iclog->ic_state = XLOG_STATE_ACTIVE; 1476 iclog->ic_log = log; 1477 atomic_set(&iclog->ic_refcnt, 0); 1478 INIT_LIST_HEAD(&iclog->ic_callbacks); 1479 iclog->ic_datap = (void *)iclog->ic_data + log->l_iclog_hsize; 1480 1481 init_waitqueue_head(&iclog->ic_force_wait); 1482 init_waitqueue_head(&iclog->ic_write_wait); 1483 INIT_WORK(&iclog->ic_end_io_work, xlog_ioend_work); 1484 sema_init(&iclog->ic_sema, 1); 1485 1486 iclogp = &iclog->ic_next; 1487 } 1488 *iclogp = log->l_iclog; /* complete ring */ 1489 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */ 1490 1491 log->l_ioend_workqueue = alloc_workqueue("xfs-log/%s", 1492 XFS_WQFLAGS(WQ_FREEZABLE | WQ_MEM_RECLAIM | 1493 WQ_HIGHPRI), 1494 0, mp->m_super->s_id); 1495 if (!log->l_ioend_workqueue) 1496 goto out_free_iclog; 1497 1498 error = xlog_cil_init(log); 1499 if (error) 1500 goto out_destroy_workqueue; 1501 return log; 1502 1503 out_destroy_workqueue: 1504 destroy_workqueue(log->l_ioend_workqueue); 1505 out_free_iclog: 1506 for (iclog = log->l_iclog; iclog; iclog = prev_iclog) { 1507 prev_iclog = iclog->ic_next; 1508 kvfree(iclog->ic_data); 1509 kfree(iclog); 1510 if (prev_iclog == log->l_iclog) 1511 break; 1512 } 1513 out_free_log: 1514 kfree(log); 1515 out: 1516 return ERR_PTR(error); 1517 } /* xlog_alloc_log */ 1518 1519 /* 1520 * Stamp cycle number in every block 1521 */ 1522 STATIC void 1523 xlog_pack_data( 1524 struct xlog *log, 1525 struct xlog_in_core *iclog, 1526 int roundoff) 1527 { 1528 int i, j, k; 1529 int size = iclog->ic_offset + roundoff; 1530 __be32 cycle_lsn; 1531 char *dp; 1532 1533 cycle_lsn = CYCLE_LSN_DISK(iclog->ic_header.h_lsn); 1534 1535 dp = iclog->ic_datap; 1536 for (i = 0; i < BTOBB(size); i++) { 1537 if (i >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) 1538 break; 1539 iclog->ic_header.h_cycle_data[i] = *(__be32 *)dp; 1540 *(__be32 *)dp = cycle_lsn; 1541 dp += BBSIZE; 1542 } 1543 1544 if (xfs_has_logv2(log->l_mp)) { 1545 xlog_in_core_2_t *xhdr = iclog->ic_data; 1546 1547 for ( ; i < BTOBB(size); i++) { 1548 j = i / (XLOG_HEADER_CYCLE_SIZE / BBSIZE); 1549 k = i % (XLOG_HEADER_CYCLE_SIZE / BBSIZE); 1550 xhdr[j].hic_xheader.xh_cycle_data[k] = *(__be32 *)dp; 1551 *(__be32 *)dp = cycle_lsn; 1552 dp += BBSIZE; 1553 } 1554 1555 for (i = 1; i < log->l_iclog_heads; i++) 1556 xhdr[i].hic_xheader.xh_cycle = cycle_lsn; 1557 } 1558 } 1559 1560 /* 1561 * Calculate the checksum for a log buffer. 1562 * 1563 * This is a little more complicated than it should be because the various 1564 * headers and the actual data are non-contiguous. 1565 */ 1566 __le32 1567 xlog_cksum( 1568 struct xlog *log, 1569 struct xlog_rec_header *rhead, 1570 char *dp, 1571 int size) 1572 { 1573 uint32_t crc; 1574 1575 /* first generate the crc for the record header ... */ 1576 crc = xfs_start_cksum_update((char *)rhead, 1577 sizeof(struct xlog_rec_header), 1578 offsetof(struct xlog_rec_header, h_crc)); 1579 1580 /* ... then for additional cycle data for v2 logs ... */ 1581 if (xfs_has_logv2(log->l_mp)) { 1582 union xlog_in_core2 *xhdr = (union xlog_in_core2 *)rhead; 1583 int i; 1584 int xheads; 1585 1586 xheads = DIV_ROUND_UP(size, XLOG_HEADER_CYCLE_SIZE); 1587 1588 for (i = 1; i < xheads; i++) { 1589 crc = crc32c(crc, &xhdr[i].hic_xheader, 1590 sizeof(struct xlog_rec_ext_header)); 1591 } 1592 } 1593 1594 /* ... and finally for the payload */ 1595 crc = crc32c(crc, dp, size); 1596 1597 return xfs_end_cksum(crc); 1598 } 1599 1600 static void 1601 xlog_bio_end_io( 1602 struct bio *bio) 1603 { 1604 struct xlog_in_core *iclog = bio->bi_private; 1605 1606 queue_work(iclog->ic_log->l_ioend_workqueue, 1607 &iclog->ic_end_io_work); 1608 } 1609 1610 static int 1611 xlog_map_iclog_data( 1612 struct bio *bio, 1613 void *data, 1614 size_t count) 1615 { 1616 do { 1617 struct page *page = kmem_to_page(data); 1618 unsigned int off = offset_in_page(data); 1619 size_t len = min_t(size_t, count, PAGE_SIZE - off); 1620 1621 if (bio_add_page(bio, page, len, off) != len) 1622 return -EIO; 1623 1624 data += len; 1625 count -= len; 1626 } while (count); 1627 1628 return 0; 1629 } 1630 1631 STATIC void 1632 xlog_write_iclog( 1633 struct xlog *log, 1634 struct xlog_in_core *iclog, 1635 uint64_t bno, 1636 unsigned int count) 1637 { 1638 ASSERT(bno < log->l_logBBsize); 1639 trace_xlog_iclog_write(iclog, _RET_IP_); 1640 1641 /* 1642 * We lock the iclogbufs here so that we can serialise against I/O 1643 * completion during unmount. We might be processing a shutdown 1644 * triggered during unmount, and that can occur asynchronously to the 1645 * unmount thread, and hence we need to ensure that completes before 1646 * tearing down the iclogbufs. Hence we need to hold the buffer lock 1647 * across the log IO to archieve that. 1648 */ 1649 down(&iclog->ic_sema); 1650 if (xlog_is_shutdown(log)) { 1651 /* 1652 * It would seem logical to return EIO here, but we rely on 1653 * the log state machine to propagate I/O errors instead of 1654 * doing it here. We kick of the state machine and unlock 1655 * the buffer manually, the code needs to be kept in sync 1656 * with the I/O completion path. 1657 */ 1658 goto sync; 1659 } 1660 1661 /* 1662 * We use REQ_SYNC | REQ_IDLE here to tell the block layer the are more 1663 * IOs coming immediately after this one. This prevents the block layer 1664 * writeback throttle from throttling log writes behind background 1665 * metadata writeback and causing priority inversions. 1666 */ 1667 bio_init(&iclog->ic_bio, log->l_targ->bt_bdev, iclog->ic_bvec, 1668 howmany(count, PAGE_SIZE), 1669 REQ_OP_WRITE | REQ_META | REQ_SYNC | REQ_IDLE); 1670 iclog->ic_bio.bi_iter.bi_sector = log->l_logBBstart + bno; 1671 iclog->ic_bio.bi_end_io = xlog_bio_end_io; 1672 iclog->ic_bio.bi_private = iclog; 1673 1674 if (iclog->ic_flags & XLOG_ICL_NEED_FLUSH) { 1675 iclog->ic_bio.bi_opf |= REQ_PREFLUSH; 1676 /* 1677 * For external log devices, we also need to flush the data 1678 * device cache first to ensure all metadata writeback covered 1679 * by the LSN in this iclog is on stable storage. This is slow, 1680 * but it *must* complete before we issue the external log IO. 1681 * 1682 * If the flush fails, we cannot conclude that past metadata 1683 * writeback from the log succeeded. Repeating the flush is 1684 * not possible, hence we must shut down with log IO error to 1685 * avoid shutdown re-entering this path and erroring out again. 1686 */ 1687 if (log->l_targ != log->l_mp->m_ddev_targp && 1688 blkdev_issue_flush(log->l_mp->m_ddev_targp->bt_bdev)) 1689 goto shutdown; 1690 } 1691 if (iclog->ic_flags & XLOG_ICL_NEED_FUA) 1692 iclog->ic_bio.bi_opf |= REQ_FUA; 1693 1694 iclog->ic_flags &= ~(XLOG_ICL_NEED_FLUSH | XLOG_ICL_NEED_FUA); 1695 1696 if (xlog_map_iclog_data(&iclog->ic_bio, iclog->ic_data, count)) 1697 goto shutdown; 1698 1699 if (is_vmalloc_addr(iclog->ic_data)) 1700 flush_kernel_vmap_range(iclog->ic_data, count); 1701 1702 /* 1703 * If this log buffer would straddle the end of the log we will have 1704 * to split it up into two bios, so that we can continue at the start. 1705 */ 1706 if (bno + BTOBB(count) > log->l_logBBsize) { 1707 struct bio *split; 1708 1709 split = bio_split(&iclog->ic_bio, log->l_logBBsize - bno, 1710 GFP_NOIO, &fs_bio_set); 1711 bio_chain(split, &iclog->ic_bio); 1712 submit_bio(split); 1713 1714 /* restart at logical offset zero for the remainder */ 1715 iclog->ic_bio.bi_iter.bi_sector = log->l_logBBstart; 1716 } 1717 1718 submit_bio(&iclog->ic_bio); 1719 return; 1720 shutdown: 1721 xlog_force_shutdown(log, SHUTDOWN_LOG_IO_ERROR); 1722 sync: 1723 xlog_state_done_syncing(iclog); 1724 up(&iclog->ic_sema); 1725 } 1726 1727 /* 1728 * We need to bump cycle number for the part of the iclog that is 1729 * written to the start of the log. Watch out for the header magic 1730 * number case, though. 1731 */ 1732 static void 1733 xlog_split_iclog( 1734 struct xlog *log, 1735 void *data, 1736 uint64_t bno, 1737 unsigned int count) 1738 { 1739 unsigned int split_offset = BBTOB(log->l_logBBsize - bno); 1740 unsigned int i; 1741 1742 for (i = split_offset; i < count; i += BBSIZE) { 1743 uint32_t cycle = get_unaligned_be32(data + i); 1744 1745 if (++cycle == XLOG_HEADER_MAGIC_NUM) 1746 cycle++; 1747 put_unaligned_be32(cycle, data + i); 1748 } 1749 } 1750 1751 static int 1752 xlog_calc_iclog_size( 1753 struct xlog *log, 1754 struct xlog_in_core *iclog, 1755 uint32_t *roundoff) 1756 { 1757 uint32_t count_init, count; 1758 1759 /* Add for LR header */ 1760 count_init = log->l_iclog_hsize + iclog->ic_offset; 1761 count = roundup(count_init, log->l_iclog_roundoff); 1762 1763 *roundoff = count - count_init; 1764 1765 ASSERT(count >= count_init); 1766 ASSERT(*roundoff < log->l_iclog_roundoff); 1767 return count; 1768 } 1769 1770 /* 1771 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous 1772 * fashion. Previously, we should have moved the current iclog 1773 * ptr in the log to point to the next available iclog. This allows further 1774 * write to continue while this code syncs out an iclog ready to go. 1775 * Before an in-core log can be written out, the data section must be scanned 1776 * to save away the 1st word of each BBSIZE block into the header. We replace 1777 * it with the current cycle count. Each BBSIZE block is tagged with the 1778 * cycle count because there in an implicit assumption that drives will 1779 * guarantee that entire 512 byte blocks get written at once. In other words, 1780 * we can't have part of a 512 byte block written and part not written. By 1781 * tagging each block, we will know which blocks are valid when recovering 1782 * after an unclean shutdown. 1783 * 1784 * This routine is single threaded on the iclog. No other thread can be in 1785 * this routine with the same iclog. Changing contents of iclog can there- 1786 * fore be done without grabbing the state machine lock. Updating the global 1787 * log will require grabbing the lock though. 1788 * 1789 * The entire log manager uses a logical block numbering scheme. Only 1790 * xlog_write_iclog knows about the fact that the log may not start with 1791 * block zero on a given device. 1792 */ 1793 STATIC void 1794 xlog_sync( 1795 struct xlog *log, 1796 struct xlog_in_core *iclog, 1797 struct xlog_ticket *ticket) 1798 { 1799 unsigned int count; /* byte count of bwrite */ 1800 unsigned int roundoff; /* roundoff to BB or stripe */ 1801 uint64_t bno; 1802 unsigned int size; 1803 1804 ASSERT(atomic_read(&iclog->ic_refcnt) == 0); 1805 trace_xlog_iclog_sync(iclog, _RET_IP_); 1806 1807 count = xlog_calc_iclog_size(log, iclog, &roundoff); 1808 1809 /* 1810 * If we have a ticket, account for the roundoff via the ticket 1811 * reservation to avoid touching the hot grant heads needlessly. 1812 * Otherwise, we have to move grant heads directly. 1813 */ 1814 if (ticket) { 1815 ticket->t_curr_res -= roundoff; 1816 } else { 1817 xlog_grant_add_space(&log->l_reserve_head, roundoff); 1818 xlog_grant_add_space(&log->l_write_head, roundoff); 1819 } 1820 1821 /* put cycle number in every block */ 1822 xlog_pack_data(log, iclog, roundoff); 1823 1824 /* real byte length */ 1825 size = iclog->ic_offset; 1826 if (xfs_has_logv2(log->l_mp)) 1827 size += roundoff; 1828 iclog->ic_header.h_len = cpu_to_be32(size); 1829 1830 XFS_STATS_INC(log->l_mp, xs_log_writes); 1831 XFS_STATS_ADD(log->l_mp, xs_log_blocks, BTOBB(count)); 1832 1833 bno = BLOCK_LSN(be64_to_cpu(iclog->ic_header.h_lsn)); 1834 1835 /* Do we need to split this write into 2 parts? */ 1836 if (bno + BTOBB(count) > log->l_logBBsize) 1837 xlog_split_iclog(log, &iclog->ic_header, bno, count); 1838 1839 /* calculcate the checksum */ 1840 iclog->ic_header.h_crc = xlog_cksum(log, &iclog->ic_header, 1841 iclog->ic_datap, size); 1842 /* 1843 * Intentionally corrupt the log record CRC based on the error injection 1844 * frequency, if defined. This facilitates testing log recovery in the 1845 * event of torn writes. Hence, set the IOABORT state to abort the log 1846 * write on I/O completion and shutdown the fs. The subsequent mount 1847 * detects the bad CRC and attempts to recover. 1848 */ 1849 #ifdef DEBUG 1850 if (XFS_TEST_ERROR(false, log->l_mp, XFS_ERRTAG_LOG_BAD_CRC)) { 1851 iclog->ic_header.h_crc &= cpu_to_le32(0xAAAAAAAA); 1852 iclog->ic_fail_crc = true; 1853 xfs_warn(log->l_mp, 1854 "Intentionally corrupted log record at LSN 0x%llx. Shutdown imminent.", 1855 be64_to_cpu(iclog->ic_header.h_lsn)); 1856 } 1857 #endif 1858 xlog_verify_iclog(log, iclog, count); 1859 xlog_write_iclog(log, iclog, bno, count); 1860 } 1861 1862 /* 1863 * Deallocate a log structure 1864 */ 1865 STATIC void 1866 xlog_dealloc_log( 1867 struct xlog *log) 1868 { 1869 xlog_in_core_t *iclog, *next_iclog; 1870 int i; 1871 1872 /* 1873 * Destroy the CIL after waiting for iclog IO completion because an 1874 * iclog EIO error will try to shut down the log, which accesses the 1875 * CIL to wake up the waiters. 1876 */ 1877 xlog_cil_destroy(log); 1878 1879 iclog = log->l_iclog; 1880 for (i = 0; i < log->l_iclog_bufs; i++) { 1881 next_iclog = iclog->ic_next; 1882 kvfree(iclog->ic_data); 1883 kfree(iclog); 1884 iclog = next_iclog; 1885 } 1886 1887 log->l_mp->m_log = NULL; 1888 destroy_workqueue(log->l_ioend_workqueue); 1889 kfree(log); 1890 } 1891 1892 /* 1893 * Update counters atomically now that memcpy is done. 1894 */ 1895 static inline void 1896 xlog_state_finish_copy( 1897 struct xlog *log, 1898 struct xlog_in_core *iclog, 1899 int record_cnt, 1900 int copy_bytes) 1901 { 1902 lockdep_assert_held(&log->l_icloglock); 1903 1904 be32_add_cpu(&iclog->ic_header.h_num_logops, record_cnt); 1905 iclog->ic_offset += copy_bytes; 1906 } 1907 1908 /* 1909 * print out info relating to regions written which consume 1910 * the reservation 1911 */ 1912 void 1913 xlog_print_tic_res( 1914 struct xfs_mount *mp, 1915 struct xlog_ticket *ticket) 1916 { 1917 xfs_warn(mp, "ticket reservation summary:"); 1918 xfs_warn(mp, " unit res = %d bytes", ticket->t_unit_res); 1919 xfs_warn(mp, " current res = %d bytes", ticket->t_curr_res); 1920 xfs_warn(mp, " original count = %d", ticket->t_ocnt); 1921 xfs_warn(mp, " remaining count = %d", ticket->t_cnt); 1922 } 1923 1924 /* 1925 * Print a summary of the transaction. 1926 */ 1927 void 1928 xlog_print_trans( 1929 struct xfs_trans *tp) 1930 { 1931 struct xfs_mount *mp = tp->t_mountp; 1932 struct xfs_log_item *lip; 1933 1934 /* dump core transaction and ticket info */ 1935 xfs_warn(mp, "transaction summary:"); 1936 xfs_warn(mp, " log res = %d", tp->t_log_res); 1937 xfs_warn(mp, " log count = %d", tp->t_log_count); 1938 xfs_warn(mp, " flags = 0x%x", tp->t_flags); 1939 1940 xlog_print_tic_res(mp, tp->t_ticket); 1941 1942 /* dump each log item */ 1943 list_for_each_entry(lip, &tp->t_items, li_trans) { 1944 struct xfs_log_vec *lv = lip->li_lv; 1945 struct xfs_log_iovec *vec; 1946 int i; 1947 1948 xfs_warn(mp, "log item: "); 1949 xfs_warn(mp, " type = 0x%x", lip->li_type); 1950 xfs_warn(mp, " flags = 0x%lx", lip->li_flags); 1951 if (!lv) 1952 continue; 1953 xfs_warn(mp, " niovecs = %d", lv->lv_niovecs); 1954 xfs_warn(mp, " size = %d", lv->lv_size); 1955 xfs_warn(mp, " bytes = %d", lv->lv_bytes); 1956 xfs_warn(mp, " buf len = %d", lv->lv_buf_len); 1957 1958 /* dump each iovec for the log item */ 1959 vec = lv->lv_iovecp; 1960 for (i = 0; i < lv->lv_niovecs; i++) { 1961 int dumplen = min(vec->i_len, 32); 1962 1963 xfs_warn(mp, " iovec[%d]", i); 1964 xfs_warn(mp, " type = 0x%x", vec->i_type); 1965 xfs_warn(mp, " len = %d", vec->i_len); 1966 xfs_warn(mp, " first %d bytes of iovec[%d]:", dumplen, i); 1967 xfs_hex_dump(vec->i_addr, dumplen); 1968 1969 vec++; 1970 } 1971 } 1972 } 1973 1974 static inline void 1975 xlog_write_iovec( 1976 struct xlog_in_core *iclog, 1977 uint32_t *log_offset, 1978 void *data, 1979 uint32_t write_len, 1980 int *bytes_left, 1981 uint32_t *record_cnt, 1982 uint32_t *data_cnt) 1983 { 1984 ASSERT(*log_offset < iclog->ic_log->l_iclog_size); 1985 ASSERT(*log_offset % sizeof(int32_t) == 0); 1986 ASSERT(write_len % sizeof(int32_t) == 0); 1987 1988 memcpy(iclog->ic_datap + *log_offset, data, write_len); 1989 *log_offset += write_len; 1990 *bytes_left -= write_len; 1991 (*record_cnt)++; 1992 *data_cnt += write_len; 1993 } 1994 1995 /* 1996 * Write log vectors into a single iclog which is guaranteed by the caller 1997 * to have enough space to write the entire log vector into. 1998 */ 1999 static void 2000 xlog_write_full( 2001 struct xfs_log_vec *lv, 2002 struct xlog_ticket *ticket, 2003 struct xlog_in_core *iclog, 2004 uint32_t *log_offset, 2005 uint32_t *len, 2006 uint32_t *record_cnt, 2007 uint32_t *data_cnt) 2008 { 2009 int index; 2010 2011 ASSERT(*log_offset + *len <= iclog->ic_size || 2012 iclog->ic_state == XLOG_STATE_WANT_SYNC); 2013 2014 /* 2015 * Ordered log vectors have no regions to write so this 2016 * loop will naturally skip them. 2017 */ 2018 for (index = 0; index < lv->lv_niovecs; index++) { 2019 struct xfs_log_iovec *reg = &lv->lv_iovecp[index]; 2020 struct xlog_op_header *ophdr = reg->i_addr; 2021 2022 ophdr->oh_tid = cpu_to_be32(ticket->t_tid); 2023 xlog_write_iovec(iclog, log_offset, reg->i_addr, 2024 reg->i_len, len, record_cnt, data_cnt); 2025 } 2026 } 2027 2028 static int 2029 xlog_write_get_more_iclog_space( 2030 struct xlog_ticket *ticket, 2031 struct xlog_in_core **iclogp, 2032 uint32_t *log_offset, 2033 uint32_t len, 2034 uint32_t *record_cnt, 2035 uint32_t *data_cnt) 2036 { 2037 struct xlog_in_core *iclog = *iclogp; 2038 struct xlog *log = iclog->ic_log; 2039 int error; 2040 2041 spin_lock(&log->l_icloglock); 2042 ASSERT(iclog->ic_state == XLOG_STATE_WANT_SYNC); 2043 xlog_state_finish_copy(log, iclog, *record_cnt, *data_cnt); 2044 error = xlog_state_release_iclog(log, iclog, ticket); 2045 spin_unlock(&log->l_icloglock); 2046 if (error) 2047 return error; 2048 2049 error = xlog_state_get_iclog_space(log, len, &iclog, ticket, 2050 log_offset); 2051 if (error) 2052 return error; 2053 *record_cnt = 0; 2054 *data_cnt = 0; 2055 *iclogp = iclog; 2056 return 0; 2057 } 2058 2059 /* 2060 * Write log vectors into a single iclog which is smaller than the current chain 2061 * length. We write until we cannot fit a full record into the remaining space 2062 * and then stop. We return the log vector that is to be written that cannot 2063 * wholly fit in the iclog. 2064 */ 2065 static int 2066 xlog_write_partial( 2067 struct xfs_log_vec *lv, 2068 struct xlog_ticket *ticket, 2069 struct xlog_in_core **iclogp, 2070 uint32_t *log_offset, 2071 uint32_t *len, 2072 uint32_t *record_cnt, 2073 uint32_t *data_cnt) 2074 { 2075 struct xlog_in_core *iclog = *iclogp; 2076 struct xlog_op_header *ophdr; 2077 int index = 0; 2078 uint32_t rlen; 2079 int error; 2080 2081 /* walk the logvec, copying until we run out of space in the iclog */ 2082 for (index = 0; index < lv->lv_niovecs; index++) { 2083 struct xfs_log_iovec *reg = &lv->lv_iovecp[index]; 2084 uint32_t reg_offset = 0; 2085 2086 /* 2087 * The first region of a continuation must have a non-zero 2088 * length otherwise log recovery will just skip over it and 2089 * start recovering from the next opheader it finds. Because we 2090 * mark the next opheader as a continuation, recovery will then 2091 * incorrectly add the continuation to the previous region and 2092 * that breaks stuff. 2093 * 2094 * Hence if there isn't space for region data after the 2095 * opheader, then we need to start afresh with a new iclog. 2096 */ 2097 if (iclog->ic_size - *log_offset <= 2098 sizeof(struct xlog_op_header)) { 2099 error = xlog_write_get_more_iclog_space(ticket, 2100 &iclog, log_offset, *len, record_cnt, 2101 data_cnt); 2102 if (error) 2103 return error; 2104 } 2105 2106 ophdr = reg->i_addr; 2107 rlen = min_t(uint32_t, reg->i_len, iclog->ic_size - *log_offset); 2108 2109 ophdr->oh_tid = cpu_to_be32(ticket->t_tid); 2110 ophdr->oh_len = cpu_to_be32(rlen - sizeof(struct xlog_op_header)); 2111 if (rlen != reg->i_len) 2112 ophdr->oh_flags |= XLOG_CONTINUE_TRANS; 2113 2114 xlog_write_iovec(iclog, log_offset, reg->i_addr, 2115 rlen, len, record_cnt, data_cnt); 2116 2117 /* If we wrote the whole region, move to the next. */ 2118 if (rlen == reg->i_len) 2119 continue; 2120 2121 /* 2122 * We now have a partially written iovec, but it can span 2123 * multiple iclogs so we loop here. First we release the iclog 2124 * we currently have, then we get a new iclog and add a new 2125 * opheader. Then we continue copying from where we were until 2126 * we either complete the iovec or fill the iclog. If we 2127 * complete the iovec, then we increment the index and go right 2128 * back to the top of the outer loop. if we fill the iclog, we 2129 * run the inner loop again. 2130 * 2131 * This is complicated by the tail of a region using all the 2132 * space in an iclog and hence requiring us to release the iclog 2133 * and get a new one before returning to the outer loop. We must 2134 * always guarantee that we exit this inner loop with at least 2135 * space for log transaction opheaders left in the current 2136 * iclog, hence we cannot just terminate the loop at the end 2137 * of the of the continuation. So we loop while there is no 2138 * space left in the current iclog, and check for the end of the 2139 * continuation after getting a new iclog. 2140 */ 2141 do { 2142 /* 2143 * Ensure we include the continuation opheader in the 2144 * space we need in the new iclog by adding that size 2145 * to the length we require. This continuation opheader 2146 * needs to be accounted to the ticket as the space it 2147 * consumes hasn't been accounted to the lv we are 2148 * writing. 2149 */ 2150 error = xlog_write_get_more_iclog_space(ticket, 2151 &iclog, log_offset, 2152 *len + sizeof(struct xlog_op_header), 2153 record_cnt, data_cnt); 2154 if (error) 2155 return error; 2156 2157 ophdr = iclog->ic_datap + *log_offset; 2158 ophdr->oh_tid = cpu_to_be32(ticket->t_tid); 2159 ophdr->oh_clientid = XFS_TRANSACTION; 2160 ophdr->oh_res2 = 0; 2161 ophdr->oh_flags = XLOG_WAS_CONT_TRANS; 2162 2163 ticket->t_curr_res -= sizeof(struct xlog_op_header); 2164 *log_offset += sizeof(struct xlog_op_header); 2165 *data_cnt += sizeof(struct xlog_op_header); 2166 2167 /* 2168 * If rlen fits in the iclog, then end the region 2169 * continuation. Otherwise we're going around again. 2170 */ 2171 reg_offset += rlen; 2172 rlen = reg->i_len - reg_offset; 2173 if (rlen <= iclog->ic_size - *log_offset) 2174 ophdr->oh_flags |= XLOG_END_TRANS; 2175 else 2176 ophdr->oh_flags |= XLOG_CONTINUE_TRANS; 2177 2178 rlen = min_t(uint32_t, rlen, iclog->ic_size - *log_offset); 2179 ophdr->oh_len = cpu_to_be32(rlen); 2180 2181 xlog_write_iovec(iclog, log_offset, 2182 reg->i_addr + reg_offset, 2183 rlen, len, record_cnt, data_cnt); 2184 2185 } while (ophdr->oh_flags & XLOG_CONTINUE_TRANS); 2186 } 2187 2188 /* 2189 * No more iovecs remain in this logvec so return the next log vec to 2190 * the caller so it can go back to fast path copying. 2191 */ 2192 *iclogp = iclog; 2193 return 0; 2194 } 2195 2196 /* 2197 * Write some region out to in-core log 2198 * 2199 * This will be called when writing externally provided regions or when 2200 * writing out a commit record for a given transaction. 2201 * 2202 * General algorithm: 2203 * 1. Find total length of this write. This may include adding to the 2204 * lengths passed in. 2205 * 2. Check whether we violate the tickets reservation. 2206 * 3. While writing to this iclog 2207 * A. Reserve as much space in this iclog as can get 2208 * B. If this is first write, save away start lsn 2209 * C. While writing this region: 2210 * 1. If first write of transaction, write start record 2211 * 2. Write log operation header (header per region) 2212 * 3. Find out if we can fit entire region into this iclog 2213 * 4. Potentially, verify destination memcpy ptr 2214 * 5. Memcpy (partial) region 2215 * 6. If partial copy, release iclog; otherwise, continue 2216 * copying more regions into current iclog 2217 * 4. Mark want sync bit (in simulation mode) 2218 * 5. Release iclog for potential flush to on-disk log. 2219 * 2220 * ERRORS: 2221 * 1. Panic if reservation is overrun. This should never happen since 2222 * reservation amounts are generated internal to the filesystem. 2223 * NOTES: 2224 * 1. Tickets are single threaded data structures. 2225 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the 2226 * syncing routine. When a single log_write region needs to span 2227 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set 2228 * on all log operation writes which don't contain the end of the 2229 * region. The XLOG_END_TRANS bit is used for the in-core log 2230 * operation which contains the end of the continued log_write region. 2231 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog, 2232 * we don't really know exactly how much space will be used. As a result, 2233 * we don't update ic_offset until the end when we know exactly how many 2234 * bytes have been written out. 2235 */ 2236 int 2237 xlog_write( 2238 struct xlog *log, 2239 struct xfs_cil_ctx *ctx, 2240 struct list_head *lv_chain, 2241 struct xlog_ticket *ticket, 2242 uint32_t len) 2243 2244 { 2245 struct xlog_in_core *iclog = NULL; 2246 struct xfs_log_vec *lv; 2247 uint32_t record_cnt = 0; 2248 uint32_t data_cnt = 0; 2249 int error = 0; 2250 int log_offset; 2251 2252 if (ticket->t_curr_res < 0) { 2253 xfs_alert_tag(log->l_mp, XFS_PTAG_LOGRES, 2254 "ctx ticket reservation ran out. Need to up reservation"); 2255 xlog_print_tic_res(log->l_mp, ticket); 2256 xlog_force_shutdown(log, SHUTDOWN_LOG_IO_ERROR); 2257 } 2258 2259 error = xlog_state_get_iclog_space(log, len, &iclog, ticket, 2260 &log_offset); 2261 if (error) 2262 return error; 2263 2264 ASSERT(log_offset <= iclog->ic_size - 1); 2265 2266 /* 2267 * If we have a context pointer, pass it the first iclog we are 2268 * writing to so it can record state needed for iclog write 2269 * ordering. 2270 */ 2271 if (ctx) 2272 xlog_cil_set_ctx_write_state(ctx, iclog); 2273 2274 list_for_each_entry(lv, lv_chain, lv_list) { 2275 /* 2276 * If the entire log vec does not fit in the iclog, punt it to 2277 * the partial copy loop which can handle this case. 2278 */ 2279 if (lv->lv_niovecs && 2280 lv->lv_bytes > iclog->ic_size - log_offset) { 2281 error = xlog_write_partial(lv, ticket, &iclog, 2282 &log_offset, &len, &record_cnt, 2283 &data_cnt); 2284 if (error) { 2285 /* 2286 * We have no iclog to release, so just return 2287 * the error immediately. 2288 */ 2289 return error; 2290 } 2291 } else { 2292 xlog_write_full(lv, ticket, iclog, &log_offset, 2293 &len, &record_cnt, &data_cnt); 2294 } 2295 } 2296 ASSERT(len == 0); 2297 2298 /* 2299 * We've already been guaranteed that the last writes will fit inside 2300 * the current iclog, and hence it will already have the space used by 2301 * those writes accounted to it. Hence we do not need to update the 2302 * iclog with the number of bytes written here. 2303 */ 2304 spin_lock(&log->l_icloglock); 2305 xlog_state_finish_copy(log, iclog, record_cnt, 0); 2306 error = xlog_state_release_iclog(log, iclog, ticket); 2307 spin_unlock(&log->l_icloglock); 2308 2309 return error; 2310 } 2311 2312 static void 2313 xlog_state_activate_iclog( 2314 struct xlog_in_core *iclog, 2315 int *iclogs_changed) 2316 { 2317 ASSERT(list_empty_careful(&iclog->ic_callbacks)); 2318 trace_xlog_iclog_activate(iclog, _RET_IP_); 2319 2320 /* 2321 * If the number of ops in this iclog indicate it just contains the 2322 * dummy transaction, we can change state into IDLE (the second time 2323 * around). Otherwise we should change the state into NEED a dummy. 2324 * We don't need to cover the dummy. 2325 */ 2326 if (*iclogs_changed == 0 && 2327 iclog->ic_header.h_num_logops == cpu_to_be32(XLOG_COVER_OPS)) { 2328 *iclogs_changed = 1; 2329 } else { 2330 /* 2331 * We have two dirty iclogs so start over. This could also be 2332 * num of ops indicating this is not the dummy going out. 2333 */ 2334 *iclogs_changed = 2; 2335 } 2336 2337 iclog->ic_state = XLOG_STATE_ACTIVE; 2338 iclog->ic_offset = 0; 2339 iclog->ic_header.h_num_logops = 0; 2340 memset(iclog->ic_header.h_cycle_data, 0, 2341 sizeof(iclog->ic_header.h_cycle_data)); 2342 iclog->ic_header.h_lsn = 0; 2343 iclog->ic_header.h_tail_lsn = 0; 2344 } 2345 2346 /* 2347 * Loop through all iclogs and mark all iclogs currently marked DIRTY as 2348 * ACTIVE after iclog I/O has completed. 2349 */ 2350 static void 2351 xlog_state_activate_iclogs( 2352 struct xlog *log, 2353 int *iclogs_changed) 2354 { 2355 struct xlog_in_core *iclog = log->l_iclog; 2356 2357 do { 2358 if (iclog->ic_state == XLOG_STATE_DIRTY) 2359 xlog_state_activate_iclog(iclog, iclogs_changed); 2360 /* 2361 * The ordering of marking iclogs ACTIVE must be maintained, so 2362 * an iclog doesn't become ACTIVE beyond one that is SYNCING. 2363 */ 2364 else if (iclog->ic_state != XLOG_STATE_ACTIVE) 2365 break; 2366 } while ((iclog = iclog->ic_next) != log->l_iclog); 2367 } 2368 2369 static int 2370 xlog_covered_state( 2371 int prev_state, 2372 int iclogs_changed) 2373 { 2374 /* 2375 * We go to NEED for any non-covering writes. We go to NEED2 if we just 2376 * wrote the first covering record (DONE). We go to IDLE if we just 2377 * wrote the second covering record (DONE2) and remain in IDLE until a 2378 * non-covering write occurs. 2379 */ 2380 switch (prev_state) { 2381 case XLOG_STATE_COVER_IDLE: 2382 if (iclogs_changed == 1) 2383 return XLOG_STATE_COVER_IDLE; 2384 fallthrough; 2385 case XLOG_STATE_COVER_NEED: 2386 case XLOG_STATE_COVER_NEED2: 2387 break; 2388 case XLOG_STATE_COVER_DONE: 2389 if (iclogs_changed == 1) 2390 return XLOG_STATE_COVER_NEED2; 2391 break; 2392 case XLOG_STATE_COVER_DONE2: 2393 if (iclogs_changed == 1) 2394 return XLOG_STATE_COVER_IDLE; 2395 break; 2396 default: 2397 ASSERT(0); 2398 } 2399 2400 return XLOG_STATE_COVER_NEED; 2401 } 2402 2403 STATIC void 2404 xlog_state_clean_iclog( 2405 struct xlog *log, 2406 struct xlog_in_core *dirty_iclog) 2407 { 2408 int iclogs_changed = 0; 2409 2410 trace_xlog_iclog_clean(dirty_iclog, _RET_IP_); 2411 2412 dirty_iclog->ic_state = XLOG_STATE_DIRTY; 2413 2414 xlog_state_activate_iclogs(log, &iclogs_changed); 2415 wake_up_all(&dirty_iclog->ic_force_wait); 2416 2417 if (iclogs_changed) { 2418 log->l_covered_state = xlog_covered_state(log->l_covered_state, 2419 iclogs_changed); 2420 } 2421 } 2422 2423 STATIC xfs_lsn_t 2424 xlog_get_lowest_lsn( 2425 struct xlog *log) 2426 { 2427 struct xlog_in_core *iclog = log->l_iclog; 2428 xfs_lsn_t lowest_lsn = 0, lsn; 2429 2430 do { 2431 if (iclog->ic_state == XLOG_STATE_ACTIVE || 2432 iclog->ic_state == XLOG_STATE_DIRTY) 2433 continue; 2434 2435 lsn = be64_to_cpu(iclog->ic_header.h_lsn); 2436 if ((lsn && !lowest_lsn) || XFS_LSN_CMP(lsn, lowest_lsn) < 0) 2437 lowest_lsn = lsn; 2438 } while ((iclog = iclog->ic_next) != log->l_iclog); 2439 2440 return lowest_lsn; 2441 } 2442 2443 /* 2444 * Return true if we need to stop processing, false to continue to the next 2445 * iclog. The caller will need to run callbacks if the iclog is returned in the 2446 * XLOG_STATE_CALLBACK state. 2447 */ 2448 static bool 2449 xlog_state_iodone_process_iclog( 2450 struct xlog *log, 2451 struct xlog_in_core *iclog) 2452 { 2453 xfs_lsn_t lowest_lsn; 2454 xfs_lsn_t header_lsn; 2455 2456 switch (iclog->ic_state) { 2457 case XLOG_STATE_ACTIVE: 2458 case XLOG_STATE_DIRTY: 2459 /* 2460 * Skip all iclogs in the ACTIVE & DIRTY states: 2461 */ 2462 return false; 2463 case XLOG_STATE_DONE_SYNC: 2464 /* 2465 * Now that we have an iclog that is in the DONE_SYNC state, do 2466 * one more check here to see if we have chased our tail around. 2467 * If this is not the lowest lsn iclog, then we will leave it 2468 * for another completion to process. 2469 */ 2470 header_lsn = be64_to_cpu(iclog->ic_header.h_lsn); 2471 lowest_lsn = xlog_get_lowest_lsn(log); 2472 if (lowest_lsn && XFS_LSN_CMP(lowest_lsn, header_lsn) < 0) 2473 return false; 2474 /* 2475 * If there are no callbacks on this iclog, we can mark it clean 2476 * immediately and return. Otherwise we need to run the 2477 * callbacks. 2478 */ 2479 if (list_empty(&iclog->ic_callbacks)) { 2480 xlog_state_clean_iclog(log, iclog); 2481 return false; 2482 } 2483 trace_xlog_iclog_callback(iclog, _RET_IP_); 2484 iclog->ic_state = XLOG_STATE_CALLBACK; 2485 return false; 2486 default: 2487 /* 2488 * Can only perform callbacks in order. Since this iclog is not 2489 * in the DONE_SYNC state, we skip the rest and just try to 2490 * clean up. 2491 */ 2492 return true; 2493 } 2494 } 2495 2496 /* 2497 * Loop over all the iclogs, running attached callbacks on them. Return true if 2498 * we ran any callbacks, indicating that we dropped the icloglock. We don't need 2499 * to handle transient shutdown state here at all because 2500 * xlog_state_shutdown_callbacks() will be run to do the necessary shutdown 2501 * cleanup of the callbacks. 2502 */ 2503 static bool 2504 xlog_state_do_iclog_callbacks( 2505 struct xlog *log) 2506 __releases(&log->l_icloglock) 2507 __acquires(&log->l_icloglock) 2508 { 2509 struct xlog_in_core *first_iclog = log->l_iclog; 2510 struct xlog_in_core *iclog = first_iclog; 2511 bool ran_callback = false; 2512 2513 do { 2514 LIST_HEAD(cb_list); 2515 2516 if (xlog_state_iodone_process_iclog(log, iclog)) 2517 break; 2518 if (iclog->ic_state != XLOG_STATE_CALLBACK) { 2519 iclog = iclog->ic_next; 2520 continue; 2521 } 2522 list_splice_init(&iclog->ic_callbacks, &cb_list); 2523 spin_unlock(&log->l_icloglock); 2524 2525 trace_xlog_iclog_callbacks_start(iclog, _RET_IP_); 2526 xlog_cil_process_committed(&cb_list); 2527 trace_xlog_iclog_callbacks_done(iclog, _RET_IP_); 2528 ran_callback = true; 2529 2530 spin_lock(&log->l_icloglock); 2531 xlog_state_clean_iclog(log, iclog); 2532 iclog = iclog->ic_next; 2533 } while (iclog != first_iclog); 2534 2535 return ran_callback; 2536 } 2537 2538 2539 /* 2540 * Loop running iclog completion callbacks until there are no more iclogs in a 2541 * state that can run callbacks. 2542 */ 2543 STATIC void 2544 xlog_state_do_callback( 2545 struct xlog *log) 2546 { 2547 int flushcnt = 0; 2548 int repeats = 0; 2549 2550 spin_lock(&log->l_icloglock); 2551 while (xlog_state_do_iclog_callbacks(log)) { 2552 if (xlog_is_shutdown(log)) 2553 break; 2554 2555 if (++repeats > 5000) { 2556 flushcnt += repeats; 2557 repeats = 0; 2558 xfs_warn(log->l_mp, 2559 "%s: possible infinite loop (%d iterations)", 2560 __func__, flushcnt); 2561 } 2562 } 2563 2564 if (log->l_iclog->ic_state == XLOG_STATE_ACTIVE) 2565 wake_up_all(&log->l_flush_wait); 2566 2567 spin_unlock(&log->l_icloglock); 2568 } 2569 2570 2571 /* 2572 * Finish transitioning this iclog to the dirty state. 2573 * 2574 * Callbacks could take time, so they are done outside the scope of the 2575 * global state machine log lock. 2576 */ 2577 STATIC void 2578 xlog_state_done_syncing( 2579 struct xlog_in_core *iclog) 2580 { 2581 struct xlog *log = iclog->ic_log; 2582 2583 spin_lock(&log->l_icloglock); 2584 ASSERT(atomic_read(&iclog->ic_refcnt) == 0); 2585 trace_xlog_iclog_sync_done(iclog, _RET_IP_); 2586 2587 /* 2588 * If we got an error, either on the first buffer, or in the case of 2589 * split log writes, on the second, we shut down the file system and 2590 * no iclogs should ever be attempted to be written to disk again. 2591 */ 2592 if (!xlog_is_shutdown(log)) { 2593 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING); 2594 iclog->ic_state = XLOG_STATE_DONE_SYNC; 2595 } 2596 2597 /* 2598 * Someone could be sleeping prior to writing out the next 2599 * iclog buffer, we wake them all, one will get to do the 2600 * I/O, the others get to wait for the result. 2601 */ 2602 wake_up_all(&iclog->ic_write_wait); 2603 spin_unlock(&log->l_icloglock); 2604 xlog_state_do_callback(log); 2605 } 2606 2607 /* 2608 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must 2609 * sleep. We wait on the flush queue on the head iclog as that should be 2610 * the first iclog to complete flushing. Hence if all iclogs are syncing, 2611 * we will wait here and all new writes will sleep until a sync completes. 2612 * 2613 * The in-core logs are used in a circular fashion. They are not used 2614 * out-of-order even when an iclog past the head is free. 2615 * 2616 * return: 2617 * * log_offset where xlog_write() can start writing into the in-core 2618 * log's data space. 2619 * * in-core log pointer to which xlog_write() should write. 2620 * * boolean indicating this is a continued write to an in-core log. 2621 * If this is the last write, then the in-core log's offset field 2622 * needs to be incremented, depending on the amount of data which 2623 * is copied. 2624 */ 2625 STATIC int 2626 xlog_state_get_iclog_space( 2627 struct xlog *log, 2628 int len, 2629 struct xlog_in_core **iclogp, 2630 struct xlog_ticket *ticket, 2631 int *logoffsetp) 2632 { 2633 int log_offset; 2634 xlog_rec_header_t *head; 2635 xlog_in_core_t *iclog; 2636 2637 restart: 2638 spin_lock(&log->l_icloglock); 2639 if (xlog_is_shutdown(log)) { 2640 spin_unlock(&log->l_icloglock); 2641 return -EIO; 2642 } 2643 2644 iclog = log->l_iclog; 2645 if (iclog->ic_state != XLOG_STATE_ACTIVE) { 2646 XFS_STATS_INC(log->l_mp, xs_log_noiclogs); 2647 2648 /* Wait for log writes to have flushed */ 2649 xlog_wait(&log->l_flush_wait, &log->l_icloglock); 2650 goto restart; 2651 } 2652 2653 head = &iclog->ic_header; 2654 2655 atomic_inc(&iclog->ic_refcnt); /* prevents sync */ 2656 log_offset = iclog->ic_offset; 2657 2658 trace_xlog_iclog_get_space(iclog, _RET_IP_); 2659 2660 /* On the 1st write to an iclog, figure out lsn. This works 2661 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are 2662 * committing to. If the offset is set, that's how many blocks 2663 * must be written. 2664 */ 2665 if (log_offset == 0) { 2666 ticket->t_curr_res -= log->l_iclog_hsize; 2667 head->h_cycle = cpu_to_be32(log->l_curr_cycle); 2668 head->h_lsn = cpu_to_be64( 2669 xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block)); 2670 ASSERT(log->l_curr_block >= 0); 2671 } 2672 2673 /* If there is enough room to write everything, then do it. Otherwise, 2674 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC 2675 * bit is on, so this will get flushed out. Don't update ic_offset 2676 * until you know exactly how many bytes get copied. Therefore, wait 2677 * until later to update ic_offset. 2678 * 2679 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's 2680 * can fit into remaining data section. 2681 */ 2682 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) { 2683 int error = 0; 2684 2685 xlog_state_switch_iclogs(log, iclog, iclog->ic_size); 2686 2687 /* 2688 * If we are the only one writing to this iclog, sync it to 2689 * disk. We need to do an atomic compare and decrement here to 2690 * avoid racing with concurrent atomic_dec_and_lock() calls in 2691 * xlog_state_release_iclog() when there is more than one 2692 * reference to the iclog. 2693 */ 2694 if (!atomic_add_unless(&iclog->ic_refcnt, -1, 1)) 2695 error = xlog_state_release_iclog(log, iclog, ticket); 2696 spin_unlock(&log->l_icloglock); 2697 if (error) 2698 return error; 2699 goto restart; 2700 } 2701 2702 /* Do we have enough room to write the full amount in the remainder 2703 * of this iclog? Or must we continue a write on the next iclog and 2704 * mark this iclog as completely taken? In the case where we switch 2705 * iclogs (to mark it taken), this particular iclog will release/sync 2706 * to disk in xlog_write(). 2707 */ 2708 if (len <= iclog->ic_size - iclog->ic_offset) 2709 iclog->ic_offset += len; 2710 else 2711 xlog_state_switch_iclogs(log, iclog, iclog->ic_size); 2712 *iclogp = iclog; 2713 2714 ASSERT(iclog->ic_offset <= iclog->ic_size); 2715 spin_unlock(&log->l_icloglock); 2716 2717 *logoffsetp = log_offset; 2718 return 0; 2719 } 2720 2721 /* 2722 * The first cnt-1 times a ticket goes through here we don't need to move the 2723 * grant write head because the permanent reservation has reserved cnt times the 2724 * unit amount. Release part of current permanent unit reservation and reset 2725 * current reservation to be one units worth. Also move grant reservation head 2726 * forward. 2727 */ 2728 void 2729 xfs_log_ticket_regrant( 2730 struct xlog *log, 2731 struct xlog_ticket *ticket) 2732 { 2733 trace_xfs_log_ticket_regrant(log, ticket); 2734 2735 if (ticket->t_cnt > 0) 2736 ticket->t_cnt--; 2737 2738 xlog_grant_sub_space(&log->l_reserve_head, ticket->t_curr_res); 2739 xlog_grant_sub_space(&log->l_write_head, ticket->t_curr_res); 2740 ticket->t_curr_res = ticket->t_unit_res; 2741 2742 trace_xfs_log_ticket_regrant_sub(log, ticket); 2743 2744 /* just return if we still have some of the pre-reserved space */ 2745 if (!ticket->t_cnt) { 2746 xlog_grant_add_space(&log->l_reserve_head, ticket->t_unit_res); 2747 trace_xfs_log_ticket_regrant_exit(log, ticket); 2748 } 2749 2750 xfs_log_ticket_put(ticket); 2751 } 2752 2753 /* 2754 * Give back the space left from a reservation. 2755 * 2756 * All the information we need to make a correct determination of space left 2757 * is present. For non-permanent reservations, things are quite easy. The 2758 * count should have been decremented to zero. We only need to deal with the 2759 * space remaining in the current reservation part of the ticket. If the 2760 * ticket contains a permanent reservation, there may be left over space which 2761 * needs to be released. A count of N means that N-1 refills of the current 2762 * reservation can be done before we need to ask for more space. The first 2763 * one goes to fill up the first current reservation. Once we run out of 2764 * space, the count will stay at zero and the only space remaining will be 2765 * in the current reservation field. 2766 */ 2767 void 2768 xfs_log_ticket_ungrant( 2769 struct xlog *log, 2770 struct xlog_ticket *ticket) 2771 { 2772 int bytes; 2773 2774 trace_xfs_log_ticket_ungrant(log, ticket); 2775 2776 if (ticket->t_cnt > 0) 2777 ticket->t_cnt--; 2778 2779 trace_xfs_log_ticket_ungrant_sub(log, ticket); 2780 2781 /* 2782 * If this is a permanent reservation ticket, we may be able to free 2783 * up more space based on the remaining count. 2784 */ 2785 bytes = ticket->t_curr_res; 2786 if (ticket->t_cnt > 0) { 2787 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV); 2788 bytes += ticket->t_unit_res*ticket->t_cnt; 2789 } 2790 2791 xlog_grant_sub_space(&log->l_reserve_head, bytes); 2792 xlog_grant_sub_space(&log->l_write_head, bytes); 2793 2794 trace_xfs_log_ticket_ungrant_exit(log, ticket); 2795 2796 xfs_log_space_wake(log->l_mp); 2797 xfs_log_ticket_put(ticket); 2798 } 2799 2800 /* 2801 * This routine will mark the current iclog in the ring as WANT_SYNC and move 2802 * the current iclog pointer to the next iclog in the ring. 2803 */ 2804 void 2805 xlog_state_switch_iclogs( 2806 struct xlog *log, 2807 struct xlog_in_core *iclog, 2808 int eventual_size) 2809 { 2810 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE); 2811 assert_spin_locked(&log->l_icloglock); 2812 trace_xlog_iclog_switch(iclog, _RET_IP_); 2813 2814 if (!eventual_size) 2815 eventual_size = iclog->ic_offset; 2816 iclog->ic_state = XLOG_STATE_WANT_SYNC; 2817 iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block); 2818 log->l_prev_block = log->l_curr_block; 2819 log->l_prev_cycle = log->l_curr_cycle; 2820 2821 /* roll log?: ic_offset changed later */ 2822 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize); 2823 2824 /* Round up to next log-sunit */ 2825 if (log->l_iclog_roundoff > BBSIZE) { 2826 uint32_t sunit_bb = BTOBB(log->l_iclog_roundoff); 2827 log->l_curr_block = roundup(log->l_curr_block, sunit_bb); 2828 } 2829 2830 if (log->l_curr_block >= log->l_logBBsize) { 2831 /* 2832 * Rewind the current block before the cycle is bumped to make 2833 * sure that the combined LSN never transiently moves forward 2834 * when the log wraps to the next cycle. This is to support the 2835 * unlocked sample of these fields from xlog_valid_lsn(). Most 2836 * other cases should acquire l_icloglock. 2837 */ 2838 log->l_curr_block -= log->l_logBBsize; 2839 ASSERT(log->l_curr_block >= 0); 2840 smp_wmb(); 2841 log->l_curr_cycle++; 2842 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM) 2843 log->l_curr_cycle++; 2844 } 2845 ASSERT(iclog == log->l_iclog); 2846 log->l_iclog = iclog->ic_next; 2847 } 2848 2849 /* 2850 * Force the iclog to disk and check if the iclog has been completed before 2851 * xlog_force_iclog() returns. This can happen on synchronous (e.g. 2852 * pmem) or fast async storage because we drop the icloglock to issue the IO. 2853 * If completion has already occurred, tell the caller so that it can avoid an 2854 * unnecessary wait on the iclog. 2855 */ 2856 static int 2857 xlog_force_and_check_iclog( 2858 struct xlog_in_core *iclog, 2859 bool *completed) 2860 { 2861 xfs_lsn_t lsn = be64_to_cpu(iclog->ic_header.h_lsn); 2862 int error; 2863 2864 *completed = false; 2865 error = xlog_force_iclog(iclog); 2866 if (error) 2867 return error; 2868 2869 /* 2870 * If the iclog has already been completed and reused the header LSN 2871 * will have been rewritten by completion 2872 */ 2873 if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) 2874 *completed = true; 2875 return 0; 2876 } 2877 2878 /* 2879 * Write out all data in the in-core log as of this exact moment in time. 2880 * 2881 * Data may be written to the in-core log during this call. However, 2882 * we don't guarantee this data will be written out. A change from past 2883 * implementation means this routine will *not* write out zero length LRs. 2884 * 2885 * Basically, we try and perform an intelligent scan of the in-core logs. 2886 * If we determine there is no flushable data, we just return. There is no 2887 * flushable data if: 2888 * 2889 * 1. the current iclog is active and has no data; the previous iclog 2890 * is in the active or dirty state. 2891 * 2. the current iclog is drity, and the previous iclog is in the 2892 * active or dirty state. 2893 * 2894 * We may sleep if: 2895 * 2896 * 1. the current iclog is not in the active nor dirty state. 2897 * 2. the current iclog dirty, and the previous iclog is not in the 2898 * active nor dirty state. 2899 * 3. the current iclog is active, and there is another thread writing 2900 * to this particular iclog. 2901 * 4. a) the current iclog is active and has no other writers 2902 * b) when we return from flushing out this iclog, it is still 2903 * not in the active nor dirty state. 2904 */ 2905 int 2906 xfs_log_force( 2907 struct xfs_mount *mp, 2908 uint flags) 2909 { 2910 struct xlog *log = mp->m_log; 2911 struct xlog_in_core *iclog; 2912 2913 XFS_STATS_INC(mp, xs_log_force); 2914 trace_xfs_log_force(mp, 0, _RET_IP_); 2915 2916 xlog_cil_force(log); 2917 2918 spin_lock(&log->l_icloglock); 2919 if (xlog_is_shutdown(log)) 2920 goto out_error; 2921 2922 iclog = log->l_iclog; 2923 trace_xlog_iclog_force(iclog, _RET_IP_); 2924 2925 if (iclog->ic_state == XLOG_STATE_DIRTY || 2926 (iclog->ic_state == XLOG_STATE_ACTIVE && 2927 atomic_read(&iclog->ic_refcnt) == 0 && iclog->ic_offset == 0)) { 2928 /* 2929 * If the head is dirty or (active and empty), then we need to 2930 * look at the previous iclog. 2931 * 2932 * If the previous iclog is active or dirty we are done. There 2933 * is nothing to sync out. Otherwise, we attach ourselves to the 2934 * previous iclog and go to sleep. 2935 */ 2936 iclog = iclog->ic_prev; 2937 } else if (iclog->ic_state == XLOG_STATE_ACTIVE) { 2938 if (atomic_read(&iclog->ic_refcnt) == 0) { 2939 /* We have exclusive access to this iclog. */ 2940 bool completed; 2941 2942 if (xlog_force_and_check_iclog(iclog, &completed)) 2943 goto out_error; 2944 2945 if (completed) 2946 goto out_unlock; 2947 } else { 2948 /* 2949 * Someone else is still writing to this iclog, so we 2950 * need to ensure that when they release the iclog it 2951 * gets synced immediately as we may be waiting on it. 2952 */ 2953 xlog_state_switch_iclogs(log, iclog, 0); 2954 } 2955 } 2956 2957 /* 2958 * The iclog we are about to wait on may contain the checkpoint pushed 2959 * by the above xlog_cil_force() call, but it may not have been pushed 2960 * to disk yet. Like the ACTIVE case above, we need to make sure caches 2961 * are flushed when this iclog is written. 2962 */ 2963 if (iclog->ic_state == XLOG_STATE_WANT_SYNC) 2964 iclog->ic_flags |= XLOG_ICL_NEED_FLUSH | XLOG_ICL_NEED_FUA; 2965 2966 if (flags & XFS_LOG_SYNC) 2967 return xlog_wait_on_iclog(iclog); 2968 out_unlock: 2969 spin_unlock(&log->l_icloglock); 2970 return 0; 2971 out_error: 2972 spin_unlock(&log->l_icloglock); 2973 return -EIO; 2974 } 2975 2976 /* 2977 * Force the log to a specific LSN. 2978 * 2979 * If an iclog with that lsn can be found: 2980 * If it is in the DIRTY state, just return. 2981 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC 2982 * state and go to sleep or return. 2983 * If it is in any other state, go to sleep or return. 2984 * 2985 * Synchronous forces are implemented with a wait queue. All callers trying 2986 * to force a given lsn to disk must wait on the queue attached to the 2987 * specific in-core log. When given in-core log finally completes its write 2988 * to disk, that thread will wake up all threads waiting on the queue. 2989 */ 2990 static int 2991 xlog_force_lsn( 2992 struct xlog *log, 2993 xfs_lsn_t lsn, 2994 uint flags, 2995 int *log_flushed, 2996 bool already_slept) 2997 { 2998 struct xlog_in_core *iclog; 2999 bool completed; 3000 3001 spin_lock(&log->l_icloglock); 3002 if (xlog_is_shutdown(log)) 3003 goto out_error; 3004 3005 iclog = log->l_iclog; 3006 while (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) { 3007 trace_xlog_iclog_force_lsn(iclog, _RET_IP_); 3008 iclog = iclog->ic_next; 3009 if (iclog == log->l_iclog) 3010 goto out_unlock; 3011 } 3012 3013 switch (iclog->ic_state) { 3014 case XLOG_STATE_ACTIVE: 3015 /* 3016 * We sleep here if we haven't already slept (e.g. this is the 3017 * first time we've looked at the correct iclog buf) and the 3018 * buffer before us is going to be sync'ed. The reason for this 3019 * is that if we are doing sync transactions here, by waiting 3020 * for the previous I/O to complete, we can allow a few more 3021 * transactions into this iclog before we close it down. 3022 * 3023 * Otherwise, we mark the buffer WANT_SYNC, and bump up the 3024 * refcnt so we can release the log (which drops the ref count). 3025 * The state switch keeps new transaction commits from using 3026 * this buffer. When the current commits finish writing into 3027 * the buffer, the refcount will drop to zero and the buffer 3028 * will go out then. 3029 */ 3030 if (!already_slept && 3031 (iclog->ic_prev->ic_state == XLOG_STATE_WANT_SYNC || 3032 iclog->ic_prev->ic_state == XLOG_STATE_SYNCING)) { 3033 xlog_wait(&iclog->ic_prev->ic_write_wait, 3034 &log->l_icloglock); 3035 return -EAGAIN; 3036 } 3037 if (xlog_force_and_check_iclog(iclog, &completed)) 3038 goto out_error; 3039 if (log_flushed) 3040 *log_flushed = 1; 3041 if (completed) 3042 goto out_unlock; 3043 break; 3044 case XLOG_STATE_WANT_SYNC: 3045 /* 3046 * This iclog may contain the checkpoint pushed by the 3047 * xlog_cil_force_seq() call, but there are other writers still 3048 * accessing it so it hasn't been pushed to disk yet. Like the 3049 * ACTIVE case above, we need to make sure caches are flushed 3050 * when this iclog is written. 3051 */ 3052 iclog->ic_flags |= XLOG_ICL_NEED_FLUSH | XLOG_ICL_NEED_FUA; 3053 break; 3054 default: 3055 /* 3056 * The entire checkpoint was written by the CIL force and is on 3057 * its way to disk already. It will be stable when it 3058 * completes, so we don't need to manipulate caches here at all. 3059 * We just need to wait for completion if necessary. 3060 */ 3061 break; 3062 } 3063 3064 if (flags & XFS_LOG_SYNC) 3065 return xlog_wait_on_iclog(iclog); 3066 out_unlock: 3067 spin_unlock(&log->l_icloglock); 3068 return 0; 3069 out_error: 3070 spin_unlock(&log->l_icloglock); 3071 return -EIO; 3072 } 3073 3074 /* 3075 * Force the log to a specific checkpoint sequence. 3076 * 3077 * First force the CIL so that all the required changes have been flushed to the 3078 * iclogs. If the CIL force completed it will return a commit LSN that indicates 3079 * the iclog that needs to be flushed to stable storage. If the caller needs 3080 * a synchronous log force, we will wait on the iclog with the LSN returned by 3081 * xlog_cil_force_seq() to be completed. 3082 */ 3083 int 3084 xfs_log_force_seq( 3085 struct xfs_mount *mp, 3086 xfs_csn_t seq, 3087 uint flags, 3088 int *log_flushed) 3089 { 3090 struct xlog *log = mp->m_log; 3091 xfs_lsn_t lsn; 3092 int ret; 3093 ASSERT(seq != 0); 3094 3095 XFS_STATS_INC(mp, xs_log_force); 3096 trace_xfs_log_force(mp, seq, _RET_IP_); 3097 3098 lsn = xlog_cil_force_seq(log, seq); 3099 if (lsn == NULLCOMMITLSN) 3100 return 0; 3101 3102 ret = xlog_force_lsn(log, lsn, flags, log_flushed, false); 3103 if (ret == -EAGAIN) { 3104 XFS_STATS_INC(mp, xs_log_force_sleep); 3105 ret = xlog_force_lsn(log, lsn, flags, log_flushed, true); 3106 } 3107 return ret; 3108 } 3109 3110 /* 3111 * Free a used ticket when its refcount falls to zero. 3112 */ 3113 void 3114 xfs_log_ticket_put( 3115 xlog_ticket_t *ticket) 3116 { 3117 ASSERT(atomic_read(&ticket->t_ref) > 0); 3118 if (atomic_dec_and_test(&ticket->t_ref)) 3119 kmem_cache_free(xfs_log_ticket_cache, ticket); 3120 } 3121 3122 xlog_ticket_t * 3123 xfs_log_ticket_get( 3124 xlog_ticket_t *ticket) 3125 { 3126 ASSERT(atomic_read(&ticket->t_ref) > 0); 3127 atomic_inc(&ticket->t_ref); 3128 return ticket; 3129 } 3130 3131 /* 3132 * Figure out the total log space unit (in bytes) that would be 3133 * required for a log ticket. 3134 */ 3135 static int 3136 xlog_calc_unit_res( 3137 struct xlog *log, 3138 int unit_bytes, 3139 int *niclogs) 3140 { 3141 int iclog_space; 3142 uint num_headers; 3143 3144 /* 3145 * Permanent reservations have up to 'cnt'-1 active log operations 3146 * in the log. A unit in this case is the amount of space for one 3147 * of these log operations. Normal reservations have a cnt of 1 3148 * and their unit amount is the total amount of space required. 3149 * 3150 * The following lines of code account for non-transaction data 3151 * which occupy space in the on-disk log. 3152 * 3153 * Normal form of a transaction is: 3154 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph> 3155 * and then there are LR hdrs, split-recs and roundoff at end of syncs. 3156 * 3157 * We need to account for all the leadup data and trailer data 3158 * around the transaction data. 3159 * And then we need to account for the worst case in terms of using 3160 * more space. 3161 * The worst case will happen if: 3162 * - the placement of the transaction happens to be such that the 3163 * roundoff is at its maximum 3164 * - the transaction data is synced before the commit record is synced 3165 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff> 3166 * Therefore the commit record is in its own Log Record. 3167 * This can happen as the commit record is called with its 3168 * own region to xlog_write(). 3169 * This then means that in the worst case, roundoff can happen for 3170 * the commit-rec as well. 3171 * The commit-rec is smaller than padding in this scenario and so it is 3172 * not added separately. 3173 */ 3174 3175 /* for trans header */ 3176 unit_bytes += sizeof(xlog_op_header_t); 3177 unit_bytes += sizeof(xfs_trans_header_t); 3178 3179 /* for start-rec */ 3180 unit_bytes += sizeof(xlog_op_header_t); 3181 3182 /* 3183 * for LR headers - the space for data in an iclog is the size minus 3184 * the space used for the headers. If we use the iclog size, then we 3185 * undercalculate the number of headers required. 3186 * 3187 * Furthermore - the addition of op headers for split-recs might 3188 * increase the space required enough to require more log and op 3189 * headers, so take that into account too. 3190 * 3191 * IMPORTANT: This reservation makes the assumption that if this 3192 * transaction is the first in an iclog and hence has the LR headers 3193 * accounted to it, then the remaining space in the iclog is 3194 * exclusively for this transaction. i.e. if the transaction is larger 3195 * than the iclog, it will be the only thing in that iclog. 3196 * Fundamentally, this means we must pass the entire log vector to 3197 * xlog_write to guarantee this. 3198 */ 3199 iclog_space = log->l_iclog_size - log->l_iclog_hsize; 3200 num_headers = howmany(unit_bytes, iclog_space); 3201 3202 /* for split-recs - ophdrs added when data split over LRs */ 3203 unit_bytes += sizeof(xlog_op_header_t) * num_headers; 3204 3205 /* add extra header reservations if we overrun */ 3206 while (!num_headers || 3207 howmany(unit_bytes, iclog_space) > num_headers) { 3208 unit_bytes += sizeof(xlog_op_header_t); 3209 num_headers++; 3210 } 3211 unit_bytes += log->l_iclog_hsize * num_headers; 3212 3213 /* for commit-rec LR header - note: padding will subsume the ophdr */ 3214 unit_bytes += log->l_iclog_hsize; 3215 3216 /* roundoff padding for transaction data and one for commit record */ 3217 unit_bytes += 2 * log->l_iclog_roundoff; 3218 3219 if (niclogs) 3220 *niclogs = num_headers; 3221 return unit_bytes; 3222 } 3223 3224 int 3225 xfs_log_calc_unit_res( 3226 struct xfs_mount *mp, 3227 int unit_bytes) 3228 { 3229 return xlog_calc_unit_res(mp->m_log, unit_bytes, NULL); 3230 } 3231 3232 /* 3233 * Allocate and initialise a new log ticket. 3234 */ 3235 struct xlog_ticket * 3236 xlog_ticket_alloc( 3237 struct xlog *log, 3238 int unit_bytes, 3239 int cnt, 3240 bool permanent) 3241 { 3242 struct xlog_ticket *tic; 3243 int unit_res; 3244 3245 tic = kmem_cache_zalloc(xfs_log_ticket_cache, 3246 GFP_KERNEL | __GFP_NOFAIL); 3247 3248 unit_res = xlog_calc_unit_res(log, unit_bytes, &tic->t_iclog_hdrs); 3249 3250 atomic_set(&tic->t_ref, 1); 3251 tic->t_task = current; 3252 INIT_LIST_HEAD(&tic->t_queue); 3253 tic->t_unit_res = unit_res; 3254 tic->t_curr_res = unit_res; 3255 tic->t_cnt = cnt; 3256 tic->t_ocnt = cnt; 3257 tic->t_tid = get_random_u32(); 3258 if (permanent) 3259 tic->t_flags |= XLOG_TIC_PERM_RESERV; 3260 3261 return tic; 3262 } 3263 3264 #if defined(DEBUG) 3265 static void 3266 xlog_verify_dump_tail( 3267 struct xlog *log, 3268 struct xlog_in_core *iclog) 3269 { 3270 xfs_alert(log->l_mp, 3271 "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", 3272 iclog ? be64_to_cpu(iclog->ic_header.h_tail_lsn) : -1, 3273 atomic64_read(&log->l_tail_lsn), 3274 log->l_ailp->ail_head_lsn, 3275 log->l_curr_cycle, log->l_curr_block, 3276 log->l_prev_cycle, log->l_prev_block); 3277 xfs_alert(log->l_mp, 3278 "write grant 0x%llx, reserve grant 0x%llx, tail_space 0x%llx, size 0x%x, iclog flags 0x%x", 3279 atomic64_read(&log->l_write_head.grant), 3280 atomic64_read(&log->l_reserve_head.grant), 3281 log->l_tail_space, log->l_logsize, 3282 iclog ? iclog->ic_flags : -1); 3283 } 3284 3285 /* Check if the new iclog will fit in the log. */ 3286 STATIC void 3287 xlog_verify_tail_lsn( 3288 struct xlog *log, 3289 struct xlog_in_core *iclog) 3290 { 3291 xfs_lsn_t tail_lsn = be64_to_cpu(iclog->ic_header.h_tail_lsn); 3292 int blocks; 3293 3294 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) { 3295 blocks = log->l_logBBsize - 3296 (log->l_prev_block - BLOCK_LSN(tail_lsn)); 3297 if (blocks < BTOBB(iclog->ic_offset) + 3298 BTOBB(log->l_iclog_hsize)) { 3299 xfs_emerg(log->l_mp, 3300 "%s: ran out of log space", __func__); 3301 xlog_verify_dump_tail(log, iclog); 3302 } 3303 return; 3304 } 3305 3306 if (CYCLE_LSN(tail_lsn) + 1 != log->l_prev_cycle) { 3307 xfs_emerg(log->l_mp, "%s: head has wrapped tail.", __func__); 3308 xlog_verify_dump_tail(log, iclog); 3309 return; 3310 } 3311 if (BLOCK_LSN(tail_lsn) == log->l_prev_block) { 3312 xfs_emerg(log->l_mp, "%s: tail wrapped", __func__); 3313 xlog_verify_dump_tail(log, iclog); 3314 return; 3315 } 3316 3317 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block; 3318 if (blocks < BTOBB(iclog->ic_offset) + 1) { 3319 xfs_emerg(log->l_mp, "%s: ran out of iclog space", __func__); 3320 xlog_verify_dump_tail(log, iclog); 3321 } 3322 } 3323 3324 /* 3325 * Perform a number of checks on the iclog before writing to disk. 3326 * 3327 * 1. Make sure the iclogs are still circular 3328 * 2. Make sure we have a good magic number 3329 * 3. Make sure we don't have magic numbers in the data 3330 * 4. Check fields of each log operation header for: 3331 * A. Valid client identifier 3332 * B. tid ptr value falls in valid ptr space (user space code) 3333 * C. Length in log record header is correct according to the 3334 * individual operation headers within record. 3335 * 5. When a bwrite will occur within 5 blocks of the front of the physical 3336 * log, check the preceding blocks of the physical log to make sure all 3337 * the cycle numbers agree with the current cycle number. 3338 */ 3339 STATIC void 3340 xlog_verify_iclog( 3341 struct xlog *log, 3342 struct xlog_in_core *iclog, 3343 int count) 3344 { 3345 xlog_op_header_t *ophead; 3346 xlog_in_core_t *icptr; 3347 xlog_in_core_2_t *xhdr; 3348 void *base_ptr, *ptr, *p; 3349 ptrdiff_t field_offset; 3350 uint8_t clientid; 3351 int len, i, j, k, op_len; 3352 int idx; 3353 3354 /* check validity of iclog pointers */ 3355 spin_lock(&log->l_icloglock); 3356 icptr = log->l_iclog; 3357 for (i = 0; i < log->l_iclog_bufs; i++, icptr = icptr->ic_next) 3358 ASSERT(icptr); 3359 3360 if (icptr != log->l_iclog) 3361 xfs_emerg(log->l_mp, "%s: corrupt iclog ring", __func__); 3362 spin_unlock(&log->l_icloglock); 3363 3364 /* check log magic numbers */ 3365 if (iclog->ic_header.h_magicno != cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) 3366 xfs_emerg(log->l_mp, "%s: invalid magic num", __func__); 3367 3368 base_ptr = ptr = &iclog->ic_header; 3369 p = &iclog->ic_header; 3370 for (ptr += BBSIZE; ptr < base_ptr + count; ptr += BBSIZE) { 3371 if (*(__be32 *)ptr == cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) 3372 xfs_emerg(log->l_mp, "%s: unexpected magic num", 3373 __func__); 3374 } 3375 3376 /* check fields */ 3377 len = be32_to_cpu(iclog->ic_header.h_num_logops); 3378 base_ptr = ptr = iclog->ic_datap; 3379 ophead = ptr; 3380 xhdr = iclog->ic_data; 3381 for (i = 0; i < len; i++) { 3382 ophead = ptr; 3383 3384 /* clientid is only 1 byte */ 3385 p = &ophead->oh_clientid; 3386 field_offset = p - base_ptr; 3387 if (field_offset & 0x1ff) { 3388 clientid = ophead->oh_clientid; 3389 } else { 3390 idx = BTOBBT((void *)&ophead->oh_clientid - iclog->ic_datap); 3391 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) { 3392 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE); 3393 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE); 3394 clientid = xlog_get_client_id( 3395 xhdr[j].hic_xheader.xh_cycle_data[k]); 3396 } else { 3397 clientid = xlog_get_client_id( 3398 iclog->ic_header.h_cycle_data[idx]); 3399 } 3400 } 3401 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG) { 3402 xfs_warn(log->l_mp, 3403 "%s: op %d invalid clientid %d op "PTR_FMT" offset 0x%lx", 3404 __func__, i, clientid, ophead, 3405 (unsigned long)field_offset); 3406 } 3407 3408 /* check length */ 3409 p = &ophead->oh_len; 3410 field_offset = p - base_ptr; 3411 if (field_offset & 0x1ff) { 3412 op_len = be32_to_cpu(ophead->oh_len); 3413 } else { 3414 idx = BTOBBT((void *)&ophead->oh_len - iclog->ic_datap); 3415 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) { 3416 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE); 3417 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE); 3418 op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]); 3419 } else { 3420 op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]); 3421 } 3422 } 3423 ptr += sizeof(xlog_op_header_t) + op_len; 3424 } 3425 } 3426 #endif 3427 3428 /* 3429 * Perform a forced shutdown on the log. 3430 * 3431 * This can be called from low level log code to trigger a shutdown, or from the 3432 * high level mount shutdown code when the mount shuts down. 3433 * 3434 * Our main objectives here are to make sure that: 3435 * a. if the shutdown was not due to a log IO error, flush the logs to 3436 * disk. Anything modified after this is ignored. 3437 * b. the log gets atomically marked 'XLOG_IO_ERROR' for all interested 3438 * parties to find out. Nothing new gets queued after this is done. 3439 * c. Tasks sleeping on log reservations, pinned objects and 3440 * other resources get woken up. 3441 * d. The mount is also marked as shut down so that log triggered shutdowns 3442 * still behave the same as if they called xfs_forced_shutdown(). 3443 * 3444 * Return true if the shutdown cause was a log IO error and we actually shut the 3445 * log down. 3446 */ 3447 bool 3448 xlog_force_shutdown( 3449 struct xlog *log, 3450 uint32_t shutdown_flags) 3451 { 3452 bool log_error = (shutdown_flags & SHUTDOWN_LOG_IO_ERROR); 3453 3454 if (!log) 3455 return false; 3456 3457 /* 3458 * Ensure that there is only ever one log shutdown being processed. 3459 * If we allow the log force below on a second pass after shutting 3460 * down the log, we risk deadlocking the CIL push as it may require 3461 * locks on objects the current shutdown context holds (e.g. taking 3462 * buffer locks to abort buffers on last unpin of buf log items). 3463 */ 3464 if (test_and_set_bit(XLOG_SHUTDOWN_STARTED, &log->l_opstate)) 3465 return false; 3466 3467 /* 3468 * Flush all the completed transactions to disk before marking the log 3469 * being shut down. We need to do this first as shutting down the log 3470 * before the force will prevent the log force from flushing the iclogs 3471 * to disk. 3472 * 3473 * When we are in recovery, there are no transactions to flush, and 3474 * we don't want to touch the log because we don't want to perturb the 3475 * current head/tail for future recovery attempts. Hence we need to 3476 * avoid a log force in this case. 3477 * 3478 * If we are shutting down due to a log IO error, then we must avoid 3479 * trying to write the log as that may just result in more IO errors and 3480 * an endless shutdown/force loop. 3481 */ 3482 if (!log_error && !xlog_in_recovery(log)) 3483 xfs_log_force(log->l_mp, XFS_LOG_SYNC); 3484 3485 /* 3486 * Atomically set the shutdown state. If the shutdown state is already 3487 * set, there someone else is performing the shutdown and so we are done 3488 * here. This should never happen because we should only ever get called 3489 * once by the first shutdown caller. 3490 * 3491 * Much of the log state machine transitions assume that shutdown state 3492 * cannot change once they hold the log->l_icloglock. Hence we need to 3493 * hold that lock here, even though we use the atomic test_and_set_bit() 3494 * operation to set the shutdown state. 3495 */ 3496 spin_lock(&log->l_icloglock); 3497 if (test_and_set_bit(XLOG_IO_ERROR, &log->l_opstate)) { 3498 spin_unlock(&log->l_icloglock); 3499 ASSERT(0); 3500 return false; 3501 } 3502 spin_unlock(&log->l_icloglock); 3503 3504 /* 3505 * If this log shutdown also sets the mount shutdown state, issue a 3506 * shutdown warning message. 3507 */ 3508 if (!xfs_set_shutdown(log->l_mp)) { 3509 xfs_alert_tag(log->l_mp, XFS_PTAG_SHUTDOWN_LOGERROR, 3510 "Filesystem has been shut down due to log error (0x%x).", 3511 shutdown_flags); 3512 xfs_alert(log->l_mp, 3513 "Please unmount the filesystem and rectify the problem(s)."); 3514 if (xfs_error_level >= XFS_ERRLEVEL_HIGH) 3515 xfs_stack_trace(); 3516 } 3517 3518 /* 3519 * We don't want anybody waiting for log reservations after this. That 3520 * means we have to wake up everybody queued up on reserveq as well as 3521 * writeq. In addition, we make sure in xlog_{re}grant_log_space that 3522 * we don't enqueue anything once the SHUTDOWN flag is set, and this 3523 * action is protected by the grant locks. 3524 */ 3525 xlog_grant_head_wake_all(&log->l_reserve_head); 3526 xlog_grant_head_wake_all(&log->l_write_head); 3527 3528 /* 3529 * Wake up everybody waiting on xfs_log_force. Wake the CIL push first 3530 * as if the log writes were completed. The abort handling in the log 3531 * item committed callback functions will do this again under lock to 3532 * avoid races. 3533 */ 3534 spin_lock(&log->l_cilp->xc_push_lock); 3535 wake_up_all(&log->l_cilp->xc_start_wait); 3536 wake_up_all(&log->l_cilp->xc_commit_wait); 3537 spin_unlock(&log->l_cilp->xc_push_lock); 3538 3539 spin_lock(&log->l_icloglock); 3540 xlog_state_shutdown_callbacks(log); 3541 spin_unlock(&log->l_icloglock); 3542 3543 wake_up_var(&log->l_opstate); 3544 if (IS_ENABLED(CONFIG_XFS_RT) && xfs_has_zoned(log->l_mp)) 3545 xfs_zoned_wake_all(log->l_mp); 3546 3547 return log_error; 3548 } 3549 3550 STATIC int 3551 xlog_iclogs_empty( 3552 struct xlog *log) 3553 { 3554 xlog_in_core_t *iclog; 3555 3556 iclog = log->l_iclog; 3557 do { 3558 /* endianness does not matter here, zero is zero in 3559 * any language. 3560 */ 3561 if (iclog->ic_header.h_num_logops) 3562 return 0; 3563 iclog = iclog->ic_next; 3564 } while (iclog != log->l_iclog); 3565 return 1; 3566 } 3567 3568 /* 3569 * Verify that an LSN stamped into a piece of metadata is valid. This is 3570 * intended for use in read verifiers on v5 superblocks. 3571 */ 3572 bool 3573 xfs_log_check_lsn( 3574 struct xfs_mount *mp, 3575 xfs_lsn_t lsn) 3576 { 3577 struct xlog *log = mp->m_log; 3578 bool valid; 3579 3580 /* 3581 * norecovery mode skips mount-time log processing and unconditionally 3582 * resets the in-core LSN. We can't validate in this mode, but 3583 * modifications are not allowed anyways so just return true. 3584 */ 3585 if (xfs_has_norecovery(mp)) 3586 return true; 3587 3588 /* 3589 * Some metadata LSNs are initialized to NULL (e.g., the agfl). This is 3590 * handled by recovery and thus safe to ignore here. 3591 */ 3592 if (lsn == NULLCOMMITLSN) 3593 return true; 3594 3595 valid = xlog_valid_lsn(mp->m_log, lsn); 3596 3597 /* warn the user about what's gone wrong before verifier failure */ 3598 if (!valid) { 3599 spin_lock(&log->l_icloglock); 3600 xfs_warn(mp, 3601 "Corruption warning: Metadata has LSN (%d:%d) ahead of current LSN (%d:%d). " 3602 "Please unmount and run xfs_repair (>= v4.3) to resolve.", 3603 CYCLE_LSN(lsn), BLOCK_LSN(lsn), 3604 log->l_curr_cycle, log->l_curr_block); 3605 spin_unlock(&log->l_icloglock); 3606 } 3607 3608 return valid; 3609 } 3610