1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc. 4 * All Rights Reserved. 5 */ 6 #ifndef __XFS_LOG_PRIV_H__ 7 #define __XFS_LOG_PRIV_H__ 8 9 #include "xfs_extent_busy.h" /* for struct xfs_busy_extents */ 10 11 struct xfs_buf; 12 struct xlog; 13 struct xlog_ticket; 14 struct xfs_mount; 15 16 struct xfs_log_iovec { 17 void *i_addr;/* beginning address of region */ 18 int i_len; /* length in bytes of region */ 19 uint i_type; /* type of region */ 20 }; 21 22 struct xfs_log_vec { 23 struct list_head lv_list; /* CIL lv chain ptrs */ 24 uint32_t lv_order_id; /* chain ordering info */ 25 int lv_niovecs; /* number of iovecs in lv */ 26 struct xfs_log_iovec *lv_iovecp; /* iovec array */ 27 struct xfs_log_item *lv_item; /* owner */ 28 char *lv_buf; /* formatted buffer */ 29 int lv_bytes; /* accounted space in buffer */ 30 int lv_buf_used; /* buffer space used so far */ 31 int lv_alloc_size; /* size of allocated lv */ 32 }; 33 34 /* 35 * get client id from packed copy. 36 * 37 * this hack is here because the xlog_pack code copies four bytes 38 * of xlog_op_header containing the fields oh_clientid, oh_flags 39 * and oh_res2 into the packed copy. 40 * 41 * later on this four byte chunk is treated as an int and the 42 * client id is pulled out. 43 * 44 * this has endian issues, of course. 45 */ 46 static inline uint xlog_get_client_id(__be32 i) 47 { 48 return be32_to_cpu(i) >> 24; 49 } 50 51 /* 52 * In core log state 53 */ 54 enum xlog_iclog_state { 55 XLOG_STATE_ACTIVE, /* Current IC log being written to */ 56 XLOG_STATE_WANT_SYNC, /* Want to sync this iclog; no more writes */ 57 XLOG_STATE_SYNCING, /* This IC log is syncing */ 58 XLOG_STATE_DONE_SYNC, /* Done syncing to disk */ 59 XLOG_STATE_CALLBACK, /* Callback functions now */ 60 XLOG_STATE_DIRTY, /* Dirty IC log, not ready for ACTIVE status */ 61 }; 62 63 #define XLOG_STATE_STRINGS \ 64 { XLOG_STATE_ACTIVE, "XLOG_STATE_ACTIVE" }, \ 65 { XLOG_STATE_WANT_SYNC, "XLOG_STATE_WANT_SYNC" }, \ 66 { XLOG_STATE_SYNCING, "XLOG_STATE_SYNCING" }, \ 67 { XLOG_STATE_DONE_SYNC, "XLOG_STATE_DONE_SYNC" }, \ 68 { XLOG_STATE_CALLBACK, "XLOG_STATE_CALLBACK" }, \ 69 { XLOG_STATE_DIRTY, "XLOG_STATE_DIRTY" } 70 71 /* 72 * In core log flags 73 */ 74 #define XLOG_ICL_NEED_FLUSH (1u << 0) /* all devs need REQ_PREFLUSH */ 75 #define XLOG_ICL_NEED_FLUSH_LOG (1u << 1) /* logdev needs REQ_PREFLUSH */ 76 #define XLOG_ICL_NEED_FUA (1u << 2) /* iclog needs REQ_FUA */ 77 78 #define XLOG_ICL_STRINGS \ 79 { XLOG_ICL_NEED_FLUSH, "XLOG_ICL_NEED_FLUSH" }, \ 80 { XLOG_ICL_NEED_FLUSH_LOG, "XLOG_ICL_NEED_FLUSH_LOG" }, \ 81 { XLOG_ICL_NEED_FUA, "XLOG_ICL_NEED_FUA" } 82 83 84 /* 85 * Log ticket flags 86 */ 87 #define XLOG_TIC_PERM_RESERV (1u << 0) /* permanent reservation */ 88 89 #define XLOG_TIC_FLAGS \ 90 { XLOG_TIC_PERM_RESERV, "XLOG_TIC_PERM_RESERV" } 91 92 /* 93 * Below are states for covering allocation transactions. 94 * By covering, we mean changing the h_tail_lsn in the last on-disk 95 * log write such that no allocation transactions will be re-done during 96 * recovery after a system crash. Recovery starts at the last on-disk 97 * log write. 98 * 99 * These states are used to insert dummy log entries to cover 100 * space allocation transactions which can undo non-transactional changes 101 * after a crash. Writes to a file with space 102 * already allocated do not result in any transactions. Allocations 103 * might include space beyond the EOF. So if we just push the EOF a 104 * little, the last transaction for the file could contain the wrong 105 * size. If there is no file system activity, after an allocation 106 * transaction, and the system crashes, the allocation transaction 107 * will get replayed and the file will be truncated. This could 108 * be hours/days/... after the allocation occurred. 109 * 110 * The fix for this is to do two dummy transactions when the 111 * system is idle. We need two dummy transaction because the h_tail_lsn 112 * in the log record header needs to point beyond the last possible 113 * non-dummy transaction. The first dummy changes the h_tail_lsn to 114 * the first transaction before the dummy. The second dummy causes 115 * h_tail_lsn to point to the first dummy. Recovery starts at h_tail_lsn. 116 * 117 * These dummy transactions get committed when everything 118 * is idle (after there has been some activity). 119 * 120 * There are 5 states used to control this. 121 * 122 * IDLE -- no logging has been done on the file system or 123 * we are done covering previous transactions. 124 * NEED -- logging has occurred and we need a dummy transaction 125 * when the log becomes idle. 126 * DONE -- we were in the NEED state and have committed a dummy 127 * transaction. 128 * NEED2 -- we detected that a dummy transaction has gone to the 129 * on disk log with no other transactions. 130 * DONE2 -- we committed a dummy transaction when in the NEED2 state. 131 * 132 * There are two places where we switch states: 133 * 134 * 1.) In xfs_sync, when we detect an idle log and are in NEED or NEED2. 135 * We commit the dummy transaction and switch to DONE or DONE2, 136 * respectively. In all other states, we don't do anything. 137 * 138 * 2.) When we finish writing the on-disk log (xlog_state_clean_log). 139 * 140 * No matter what state we are in, if this isn't the dummy 141 * transaction going out, the next state is NEED. 142 * So, if we aren't in the DONE or DONE2 states, the next state 143 * is NEED. We can't be finishing a write of the dummy record 144 * unless it was committed and the state switched to DONE or DONE2. 145 * 146 * If we are in the DONE state and this was a write of the 147 * dummy transaction, we move to NEED2. 148 * 149 * If we are in the DONE2 state and this was a write of the 150 * dummy transaction, we move to IDLE. 151 * 152 * 153 * Writing only one dummy transaction can get appended to 154 * one file space allocation. When this happens, the log recovery 155 * code replays the space allocation and a file could be truncated. 156 * This is why we have the NEED2 and DONE2 states before going idle. 157 */ 158 159 #define XLOG_STATE_COVER_IDLE 0 160 #define XLOG_STATE_COVER_NEED 1 161 #define XLOG_STATE_COVER_DONE 2 162 #define XLOG_STATE_COVER_NEED2 3 163 #define XLOG_STATE_COVER_DONE2 4 164 165 #define XLOG_COVER_OPS 5 166 167 struct xlog_ticket { 168 struct list_head t_queue; /* reserve/write queue */ 169 struct task_struct *t_task; /* task that owns this ticket */ 170 xlog_tid_t t_tid; /* transaction identifier */ 171 atomic_t t_ref; /* ticket reference count */ 172 int t_curr_res; /* current reservation */ 173 int t_unit_res; /* unit reservation */ 174 char t_ocnt; /* original unit count */ 175 char t_cnt; /* current unit count */ 176 uint8_t t_flags; /* properties of reservation */ 177 int t_iclog_hdrs; /* iclog hdrs in t_curr_res */ 178 }; 179 180 /* 181 * In-core log structure. 182 * 183 * - ic_forcewait is used to implement synchronous forcing of the iclog to disk. 184 * - ic_next is the pointer to the next iclog in the ring. 185 * - ic_log is a pointer back to the global log structure. 186 * - ic_size is the full size of the log buffer, minus the cycle headers. 187 * - ic_offset is the current number of bytes written to in this iclog. 188 * - ic_refcnt is bumped when someone is writing to the log. 189 * - ic_state is the state of the iclog. 190 * 191 * Because of cacheline contention on large machines, we need to separate 192 * various resources onto different cachelines. To start with, make the 193 * structure cacheline aligned. The following fields can be contended on 194 * by independent processes: 195 * 196 * - ic_callbacks 197 * - ic_refcnt 198 * - fields protected by the global l_icloglock 199 * 200 * so we need to ensure that these fields are located in separate cachelines. 201 * We'll put all the read-only and l_icloglock fields in the first cacheline, 202 * and move everything else out to subsequent cachelines. 203 */ 204 struct xlog_in_core { 205 wait_queue_head_t ic_force_wait; 206 wait_queue_head_t ic_write_wait; 207 struct xlog_in_core *ic_next; 208 struct xlog_in_core *ic_prev; 209 struct xlog *ic_log; 210 u32 ic_size; 211 u32 ic_offset; 212 enum xlog_iclog_state ic_state; 213 unsigned int ic_flags; 214 void *ic_datap; /* pointer to iclog data */ 215 struct list_head ic_callbacks; 216 217 /* reference counts need their own cacheline */ 218 atomic_t ic_refcnt ____cacheline_aligned_in_smp; 219 struct xlog_rec_header *ic_header; 220 #ifdef DEBUG 221 bool ic_fail_crc : 1; 222 #endif 223 struct semaphore ic_sema; 224 struct work_struct ic_end_io_work; 225 struct bio ic_bio; 226 struct bio_vec ic_bvec[]; 227 }; 228 229 /* 230 * The CIL context is used to aggregate per-transaction details as well be 231 * passed to the iclog for checkpoint post-commit processing. After being 232 * passed to the iclog, another context needs to be allocated for tracking the 233 * next set of transactions to be aggregated into a checkpoint. 234 */ 235 struct xfs_cil; 236 237 struct xfs_cil_ctx { 238 struct xfs_cil *cil; 239 xfs_csn_t sequence; /* chkpt sequence # */ 240 xfs_lsn_t start_lsn; /* first LSN of chkpt commit */ 241 xfs_lsn_t commit_lsn; /* chkpt commit record lsn */ 242 struct xlog_in_core *commit_iclog; 243 struct xlog_ticket *ticket; /* chkpt ticket */ 244 atomic_t space_used; /* aggregate size of regions */ 245 struct xfs_busy_extents busy_extents; 246 struct list_head log_items; /* log items in chkpt */ 247 struct list_head lv_chain; /* logvecs being pushed */ 248 struct list_head iclog_entry; 249 struct list_head committing; /* ctx committing list */ 250 struct work_struct push_work; 251 atomic_t order_id; 252 253 /* 254 * CPUs that could have added items to the percpu CIL data. Access is 255 * coordinated with xc_ctx_lock. 256 */ 257 struct cpumask cil_pcpmask; 258 }; 259 260 /* 261 * Per-cpu CIL tracking items 262 */ 263 struct xlog_cil_pcp { 264 int32_t space_used; 265 uint32_t space_reserved; 266 struct list_head busy_extents; 267 struct list_head log_items; 268 }; 269 270 /* 271 * Committed Item List structure 272 * 273 * This structure is used to track log items that have been committed but not 274 * yet written into the log. It is used only when the delayed logging mount 275 * option is enabled. 276 * 277 * This structure tracks the list of committing checkpoint contexts so 278 * we can avoid the problem of having to hold out new transactions during a 279 * flush until we have a the commit record LSN of the checkpoint. We can 280 * traverse the list of committing contexts in xlog_cil_push_lsn() to find a 281 * sequence match and extract the commit LSN directly from there. If the 282 * checkpoint is still in the process of committing, we can block waiting for 283 * the commit LSN to be determined as well. This should make synchronous 284 * operations almost as efficient as the old logging methods. 285 */ 286 struct xfs_cil { 287 struct xlog *xc_log; 288 unsigned long xc_flags; 289 atomic_t xc_iclog_hdrs; 290 struct workqueue_struct *xc_push_wq; 291 292 struct rw_semaphore xc_ctx_lock ____cacheline_aligned_in_smp; 293 struct xfs_cil_ctx *xc_ctx; 294 295 spinlock_t xc_push_lock ____cacheline_aligned_in_smp; 296 xfs_csn_t xc_push_seq; 297 bool xc_push_commit_stable; 298 struct list_head xc_committing; 299 wait_queue_head_t xc_commit_wait; 300 wait_queue_head_t xc_start_wait; 301 xfs_csn_t xc_current_sequence; 302 wait_queue_head_t xc_push_wait; /* background push throttle */ 303 304 void __percpu *xc_pcp; /* percpu CIL structures */ 305 } ____cacheline_aligned_in_smp; 306 307 /* xc_flags bit values */ 308 #define XLOG_CIL_EMPTY 1 309 #define XLOG_CIL_PCP_SPACE 2 310 311 /* 312 * The amount of log space we allow the CIL to aggregate is difficult to size. 313 * Whatever we choose, we have to make sure we can get a reservation for the 314 * log space effectively, that it is large enough to capture sufficient 315 * relogging to reduce log buffer IO significantly, but it is not too large for 316 * the log or induces too much latency when writing out through the iclogs. We 317 * track both space consumed and the number of vectors in the checkpoint 318 * context, so we need to decide which to use for limiting. 319 * 320 * Every log buffer we write out during a push needs a header reserved, which 321 * is at least one sector and more for v2 logs. Hence we need a reservation of 322 * at least 512 bytes per 32k of log space just for the LR headers. That means 323 * 16KB of reservation per megabyte of delayed logging space we will consume, 324 * plus various headers. The number of headers will vary based on the num of 325 * io vectors, so limiting on a specific number of vectors is going to result 326 * in transactions of varying size. IOWs, it is more consistent to track and 327 * limit space consumed in the log rather than by the number of objects being 328 * logged in order to prevent checkpoint ticket overruns. 329 * 330 * Further, use of static reservations through the log grant mechanism is 331 * problematic. It introduces a lot of complexity (e.g. reserve grant vs write 332 * grant) and a significant deadlock potential because regranting write space 333 * can block on log pushes. Hence if we have to regrant log space during a log 334 * push, we can deadlock. 335 * 336 * However, we can avoid this by use of a dynamic "reservation stealing" 337 * technique during transaction commit whereby unused reservation space in the 338 * transaction ticket is transferred to the CIL ctx commit ticket to cover the 339 * space needed by the checkpoint transaction. This means that we never need to 340 * specifically reserve space for the CIL checkpoint transaction, nor do we 341 * need to regrant space once the checkpoint completes. This also means the 342 * checkpoint transaction ticket is specific to the checkpoint context, rather 343 * than the CIL itself. 344 * 345 * With dynamic reservations, we can effectively make up arbitrary limits for 346 * the checkpoint size so long as they don't violate any other size rules. 347 * Recovery imposes a rule that no transaction exceed half the log, so we are 348 * limited by that. Furthermore, the log transaction reservation subsystem 349 * tries to keep 25% of the log free, so we need to keep below that limit or we 350 * risk running out of free log space to start any new transactions. 351 * 352 * In order to keep background CIL push efficient, we only need to ensure the 353 * CIL is large enough to maintain sufficient in-memory relogging to avoid 354 * repeated physical writes of frequently modified metadata. If we allow the CIL 355 * to grow to a substantial fraction of the log, then we may be pinning hundreds 356 * of megabytes of metadata in memory until the CIL flushes. This can cause 357 * issues when we are running low on memory - pinned memory cannot be reclaimed, 358 * and the CIL consumes a lot of memory. Hence we need to set an upper physical 359 * size limit for the CIL that limits the maximum amount of memory pinned by the 360 * CIL but does not limit performance by reducing relogging efficiency 361 * significantly. 362 * 363 * As such, the CIL push threshold ends up being the smaller of two thresholds: 364 * - a threshold large enough that it allows CIL to be pushed and progress to be 365 * made without excessive blocking of incoming transaction commits. This is 366 * defined to be 12.5% of the log space - half the 25% push threshold of the 367 * AIL. 368 * - small enough that it doesn't pin excessive amounts of memory but maintains 369 * close to peak relogging efficiency. This is defined to be 16x the iclog 370 * buffer window (32MB) as measurements have shown this to be roughly the 371 * point of diminishing performance increases under highly concurrent 372 * modification workloads. 373 * 374 * To prevent the CIL from overflowing upper commit size bounds, we introduce a 375 * new threshold at which we block committing transactions until the background 376 * CIL commit commences and switches to a new context. While this is not a hard 377 * limit, it forces the process committing a transaction to the CIL to block and 378 * yeild the CPU, giving the CIL push work a chance to be scheduled and start 379 * work. This prevents a process running lots of transactions from overfilling 380 * the CIL because it is not yielding the CPU. We set the blocking limit at 381 * twice the background push space threshold so we keep in line with the AIL 382 * push thresholds. 383 * 384 * Note: this is not a -hard- limit as blocking is applied after the transaction 385 * is inserted into the CIL and the push has been triggered. It is largely a 386 * throttling mechanism that allows the CIL push to be scheduled and run. A hard 387 * limit will be difficult to implement without introducing global serialisation 388 * in the CIL commit fast path, and it's not at all clear that we actually need 389 * such hard limits given the ~7 years we've run without a hard limit before 390 * finding the first situation where a checkpoint size overflow actually 391 * occurred. Hence the simple throttle, and an ASSERT check to tell us that 392 * we've overrun the max size. 393 */ 394 #define XLOG_CIL_SPACE_LIMIT(log) \ 395 min_t(int, (log)->l_logsize >> 3, BBTOB(XLOG_TOTAL_REC_SHIFT(log)) << 4) 396 397 #define XLOG_CIL_BLOCKING_SPACE_LIMIT(log) \ 398 (XLOG_CIL_SPACE_LIMIT(log) * 2) 399 400 /* 401 * ticket grant locks, queues and accounting have their own cachlines 402 * as these are quite hot and can be operated on concurrently. 403 */ 404 struct xlog_grant_head { 405 spinlock_t lock ____cacheline_aligned_in_smp; 406 struct list_head waiters; 407 atomic64_t grant; 408 }; 409 410 /* 411 * The reservation head lsn is not made up of a cycle number and block number. 412 * Instead, it uses a cycle number and byte number. Logs don't expect to 413 * overflow 31 bits worth of byte offset, so using a byte number will mean 414 * that round off problems won't occur when releasing partial reservations. 415 */ 416 struct xlog { 417 /* The following fields don't need locking */ 418 struct xfs_mount *l_mp; /* mount point */ 419 struct xfs_ail *l_ailp; /* AIL log is working with */ 420 struct xfs_cil *l_cilp; /* CIL log is working with */ 421 struct xfs_buftarg *l_targ; /* buftarg of log */ 422 struct workqueue_struct *l_ioend_workqueue; /* for I/O completions */ 423 struct delayed_work l_work; /* background flush work */ 424 long l_opstate; /* operational state */ 425 uint l_quotaoffs_flag; /* XFS_DQ_*, for QUOTAOFFs */ 426 struct list_head *l_buf_cancel_table; 427 struct list_head r_dfops; /* recovered log intent items */ 428 int l_iclog_hsize; /* size of iclog header */ 429 uint l_sectBBsize; /* sector size in BBs (2^n) */ 430 int l_iclog_size; /* size of log in bytes */ 431 int l_iclog_bufs; /* number of iclog buffers */ 432 xfs_daddr_t l_logBBstart; /* start block of log */ 433 int l_logsize; /* size of log in bytes */ 434 int l_logBBsize; /* size of log in BB chunks */ 435 436 /* The following block of fields are changed while holding icloglock */ 437 wait_queue_head_t l_flush_wait ____cacheline_aligned_in_smp; 438 /* waiting for iclog flush */ 439 int l_covered_state;/* state of "covering disk 440 * log entries" */ 441 struct xlog_in_core *l_iclog; /* head log queue */ 442 spinlock_t l_icloglock; /* grab to change iclog state */ 443 int l_curr_cycle; /* Cycle number of log writes */ 444 int l_prev_cycle; /* Cycle number before last 445 * block increment */ 446 int l_curr_block; /* current logical log block */ 447 int l_prev_block; /* previous logical log block */ 448 449 /* 450 * l_tail_lsn is atomic so it can be set and read without needing to 451 * hold specific locks. To avoid operations contending with other hot 452 * objects, it on a separate cacheline. 453 */ 454 /* lsn of 1st LR with unflushed * buffers */ 455 atomic64_t l_tail_lsn ____cacheline_aligned_in_smp; 456 457 struct xlog_grant_head l_reserve_head; 458 struct xlog_grant_head l_write_head; 459 uint64_t l_tail_space; 460 461 struct xfs_kobj l_kobj; 462 463 /* log recovery lsn tracking (for buffer submission */ 464 xfs_lsn_t l_recovery_lsn; 465 466 uint32_t l_iclog_roundoff;/* padding roundoff */ 467 }; 468 469 /* 470 * Bits for operational state 471 */ 472 #define XLOG_ACTIVE_RECOVERY 0 /* in the middle of recovery */ 473 #define XLOG_RECOVERY_NEEDED 1 /* log was recovered */ 474 #define XLOG_IO_ERROR 2 /* log hit an I/O error, and being 475 shutdown */ 476 #define XLOG_TAIL_WARN 3 /* log tail verify warning issued */ 477 #define XLOG_SHUTDOWN_STARTED 4 /* xlog_force_shutdown() exclusion */ 478 479 static inline bool 480 xlog_recovery_needed(struct xlog *log) 481 { 482 return test_bit(XLOG_RECOVERY_NEEDED, &log->l_opstate); 483 } 484 485 static inline bool 486 xlog_in_recovery(struct xlog *log) 487 { 488 return test_bit(XLOG_ACTIVE_RECOVERY, &log->l_opstate); 489 } 490 491 static inline bool 492 xlog_is_shutdown(struct xlog *log) 493 { 494 return test_bit(XLOG_IO_ERROR, &log->l_opstate); 495 } 496 497 /* 498 * Wait until the xlog_force_shutdown() has marked the log as shut down 499 * so xlog_is_shutdown() will always return true. 500 */ 501 static inline void 502 xlog_shutdown_wait( 503 struct xlog *log) 504 { 505 wait_var_event(&log->l_opstate, xlog_is_shutdown(log)); 506 } 507 508 /* common routines */ 509 extern int 510 xlog_recover( 511 struct xlog *log); 512 extern int 513 xlog_recover_finish( 514 struct xlog *log); 515 extern void 516 xlog_recover_cancel(struct xlog *); 517 518 __le32 xlog_cksum(struct xlog *log, struct xlog_rec_header *rhead, 519 char *dp, unsigned int hdrsize, unsigned int size); 520 521 extern struct kmem_cache *xfs_log_ticket_cache; 522 struct xlog_ticket *xlog_ticket_alloc(struct xlog *log, int unit_bytes, 523 int count, bool permanent); 524 525 void xlog_print_tic_res(struct xfs_mount *mp, struct xlog_ticket *ticket); 526 void xlog_print_trans(struct xfs_trans *); 527 int xlog_write(struct xlog *log, struct xfs_cil_ctx *ctx, 528 struct list_head *lv_chain, struct xlog_ticket *tic, 529 uint32_t len); 530 int xlog_write_one_vec(struct xlog *log, struct xfs_cil_ctx *ctx, 531 struct xfs_log_iovec *reg, struct xlog_ticket *ticket); 532 void xfs_log_ticket_ungrant(struct xlog *log, struct xlog_ticket *ticket); 533 void xfs_log_ticket_regrant(struct xlog *log, struct xlog_ticket *ticket); 534 535 void xlog_state_switch_iclogs(struct xlog *log, struct xlog_in_core *iclog, 536 int eventual_size); 537 int xlog_state_release_iclog(struct xlog *log, struct xlog_in_core *iclog, 538 struct xlog_ticket *ticket); 539 540 /* 541 * When we crack an atomic LSN, we sample it first so that the value will not 542 * change while we are cracking it into the component values. This means we 543 * will always get consistent component values to work from. This should always 544 * be used to sample and crack LSNs that are stored and updated in atomic 545 * variables. 546 */ 547 static inline void 548 xlog_crack_atomic_lsn(atomic64_t *lsn, uint *cycle, uint *block) 549 { 550 xfs_lsn_t val = atomic64_read(lsn); 551 552 *cycle = CYCLE_LSN(val); 553 *block = BLOCK_LSN(val); 554 } 555 556 /* 557 * Calculate and assign a value to an atomic LSN variable from component pieces. 558 */ 559 static inline void 560 xlog_assign_atomic_lsn(atomic64_t *lsn, uint cycle, uint block) 561 { 562 atomic64_set(lsn, xlog_assign_lsn(cycle, block)); 563 } 564 565 /* 566 * Committed Item List interfaces 567 */ 568 int xlog_cil_init(struct xlog *log); 569 void xlog_cil_init_post_recovery(struct xlog *log); 570 void xlog_cil_destroy(struct xlog *log); 571 bool xlog_cil_empty(struct xlog *log); 572 void xlog_cil_commit(struct xlog *log, struct xfs_trans *tp, 573 xfs_csn_t *commit_seq, bool regrant); 574 void xlog_cil_set_ctx_write_state(struct xfs_cil_ctx *ctx, 575 struct xlog_in_core *iclog); 576 577 578 /* 579 * CIL force routines 580 */ 581 void xlog_cil_flush(struct xlog *log); 582 xfs_lsn_t xlog_cil_force_seq(struct xlog *log, xfs_csn_t sequence); 583 584 static inline void 585 xlog_cil_force(struct xlog *log) 586 { 587 xlog_cil_force_seq(log, log->l_cilp->xc_current_sequence); 588 } 589 590 /* 591 * Wrapper function for waiting on a wait queue serialised against wakeups 592 * by a spinlock. This matches the semantics of all the wait queues used in the 593 * log code. 594 */ 595 static inline void 596 xlog_wait( 597 struct wait_queue_head *wq, 598 struct spinlock *lock) 599 __releases(lock) 600 { 601 DECLARE_WAITQUEUE(wait, current); 602 603 add_wait_queue_exclusive(wq, &wait); 604 __set_current_state(TASK_UNINTERRUPTIBLE); 605 spin_unlock(lock); 606 schedule(); 607 remove_wait_queue(wq, &wait); 608 } 609 610 int xlog_wait_on_iclog(struct xlog *log, struct xlog_in_core *iclog) 611 __releases(log->l_icloglock); 612 613 /* Calculate the distance between two LSNs in bytes */ 614 static inline uint64_t 615 xlog_lsn_sub( 616 struct xlog *log, 617 xfs_lsn_t high, 618 xfs_lsn_t low) 619 { 620 uint32_t hi_cycle = CYCLE_LSN(high); 621 uint32_t hi_block = BLOCK_LSN(high); 622 uint32_t lo_cycle = CYCLE_LSN(low); 623 uint32_t lo_block = BLOCK_LSN(low); 624 625 if (hi_cycle == lo_cycle) 626 return BBTOB(hi_block - lo_block); 627 ASSERT((hi_cycle == lo_cycle + 1) || xlog_is_shutdown(log)); 628 return (uint64_t)log->l_logsize - BBTOB(lo_block - hi_block); 629 } 630 631 void xlog_grant_return_space(struct xlog *log, xfs_lsn_t old_head, 632 xfs_lsn_t new_head); 633 634 /* 635 * The LSN is valid so long as it is behind the current LSN. If it isn't, this 636 * means that the next log record that includes this metadata could have a 637 * smaller LSN. In turn, this means that the modification in the log would not 638 * replay. 639 */ 640 static inline bool 641 xlog_valid_lsn( 642 struct xlog *log, 643 xfs_lsn_t lsn) 644 { 645 int cur_cycle; 646 int cur_block; 647 bool valid = true; 648 649 /* 650 * First, sample the current lsn without locking to avoid added 651 * contention from metadata I/O. The current cycle and block are updated 652 * (in xlog_state_switch_iclogs()) and read here in a particular order 653 * to avoid false negatives (e.g., thinking the metadata LSN is valid 654 * when it is not). 655 * 656 * The current block is always rewound before the cycle is bumped in 657 * xlog_state_switch_iclogs() to ensure the current LSN is never seen in 658 * a transiently forward state. Instead, we can see the LSN in a 659 * transiently behind state if we happen to race with a cycle wrap. 660 */ 661 cur_cycle = READ_ONCE(log->l_curr_cycle); 662 smp_rmb(); 663 cur_block = READ_ONCE(log->l_curr_block); 664 665 if ((CYCLE_LSN(lsn) > cur_cycle) || 666 (CYCLE_LSN(lsn) == cur_cycle && BLOCK_LSN(lsn) > cur_block)) { 667 /* 668 * If the metadata LSN appears invalid, it's possible the check 669 * above raced with a wrap to the next log cycle. Grab the lock 670 * to check for sure. 671 */ 672 spin_lock(&log->l_icloglock); 673 cur_cycle = log->l_curr_cycle; 674 cur_block = log->l_curr_block; 675 spin_unlock(&log->l_icloglock); 676 677 if ((CYCLE_LSN(lsn) > cur_cycle) || 678 (CYCLE_LSN(lsn) == cur_cycle && BLOCK_LSN(lsn) > cur_block)) 679 valid = false; 680 } 681 682 return valid; 683 } 684 685 /* 686 * Log vector and shadow buffers can be large, so we need to use kvmalloc() here 687 * to ensure success. Unfortunately, kvmalloc() only allows GFP_KERNEL contexts 688 * to fall back to vmalloc, so we can't actually do anything useful with gfp 689 * flags to control the kmalloc() behaviour within kvmalloc(). Hence kmalloc() 690 * will do direct reclaim and compaction in the slow path, both of which are 691 * horrendously expensive. We just want kmalloc to fail fast and fall back to 692 * vmalloc if it can't get something straight away from the free lists or 693 * buddy allocator. Hence we have to open code kvmalloc outselves here. 694 * 695 * This assumes that the caller uses memalloc_nofs_save task context here, so 696 * despite the use of GFP_KERNEL here, we are going to be doing GFP_NOFS 697 * allocations. This is actually the only way to make vmalloc() do GFP_NOFS 698 * allocations, so lets just all pretend this is a GFP_KERNEL context 699 * operation.... 700 */ 701 static inline void * 702 xlog_kvmalloc( 703 size_t buf_size) 704 { 705 gfp_t flags = GFP_KERNEL; 706 void *p; 707 708 flags &= ~__GFP_DIRECT_RECLAIM; 709 flags |= __GFP_NOWARN | __GFP_NORETRY; 710 do { 711 p = kmalloc(buf_size, flags); 712 if (!p) 713 p = vmalloc(buf_size); 714 } while (!p); 715 716 return p; 717 } 718 719 /* 720 * Given a count of iovecs and space for a log item, compute the space we need 721 * in the log to store that data plus the log headers. 722 */ 723 static inline unsigned int 724 xlog_item_space( 725 unsigned int niovecs, 726 unsigned int nbytes) 727 { 728 nbytes += niovecs * (sizeof(uint64_t) + sizeof(struct xlog_op_header)); 729 return round_up(nbytes, sizeof(uint64_t)); 730 } 731 732 /* 733 * Cycles over XLOG_CYCLE_DATA_SIZE overflow into the extended header that was 734 * added for v2 logs. Addressing for the cycles array there is off by one, 735 * because the first batch of cycles is in the original header. 736 */ 737 static inline __be32 *xlog_cycle_data(struct xlog_rec_header *rhead, unsigned i) 738 { 739 if (i >= XLOG_CYCLE_DATA_SIZE) { 740 unsigned j = i / XLOG_CYCLE_DATA_SIZE; 741 unsigned k = i % XLOG_CYCLE_DATA_SIZE; 742 743 return &rhead->h_ext[j - 1].xh_cycle_data[k]; 744 } 745 746 return &rhead->h_cycle_data[i]; 747 } 748 749 #endif /* __XFS_LOG_PRIV_H__ */ 750