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