1 /*****************************************************************************
2 
3 Copyright (c) 1995, 2018, Oracle and/or its affiliates. All rights reserved.
4 Copyright (c) 2009, Google Inc.
5 
6 Portions of this file contain modifications contributed and copyrighted by
7 Google, Inc. Those modifications are gratefully acknowledged and are described
8 briefly in the InnoDB documentation. The contributions by Google are
9 incorporated with their permission, and subject to the conditions contained in
10 the file COPYING.Google.
11 
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License, version 2.0,
14 as published by the Free Software Foundation.
15 
16 This program is also distributed with certain software (including
17 but not limited to OpenSSL) that is licensed under separate terms,
18 as designated in a particular file or component or in included license
19 documentation.  The authors of MySQL hereby grant you an additional
20 permission to link the program and your derivative works with the
21 separately licensed software that they have included with MySQL.
22 
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26 GNU General Public License, version 2.0, for more details.
27 
28 You should have received a copy of the GNU General Public License along with
29 this program; if not, write to the Free Software Foundation, Inc.,
30 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
31 
32 *****************************************************************************/
33 
34 /**************************************************//**
35 @file include/log0log.h
36 Database log
37 
38 Created 12/9/1995 Heikki Tuuri
39 *******************************************************/
40 
41 #ifndef log0log_h
42 #define log0log_h
43 
44 #include "univ.i"
45 #include "ut0byte.h"
46 #include "ut0lst.h"
47 #ifndef UNIV_HOTBACKUP
48 #include "sync0sync.h"
49 #include "sync0rw.h"
50 #endif /* !UNIV_HOTBACKUP */
51 
52 /* Type used for all log sequence number storage and arithmetics */
53 typedef	ib_uint64_t		lsn_t;
54 #define LSN_MAX			IB_UINT64_MAX
55 
56 #define LSN_PF			UINT64PF
57 
58 /** Redo log buffer */
59 struct log_t;
60 /** Redo log group */
61 struct log_group_t;
62 
63 #ifdef UNIV_DEBUG
64 /** Flag: write to log file? */
65 extern	ibool	log_do_write;
66 /** Flag: enable debug output when writing to the log? */
67 extern	ibool	log_debug_writes;
68 #else /* UNIV_DEBUG */
69 /** Write to log */
70 # define log_do_write TRUE
71 #endif /* UNIV_DEBUG */
72 
73 static const char ib_logfile_basename[] = "ib_logfile";
74 
75 /** Magic value to use instead of log checksums when they are disabled */
76 #define LOG_NO_CHECKSUM_MAGIC 0xDEADBEEFUL
77 
78 typedef ulint (*log_checksum_func_t)(const byte* log_block);
79 
80 /** Pointer to the log checksum calculation function. Protected with
81 log_sys->mutex. */
82 extern log_checksum_func_t log_checksum_algorithm_ptr;
83 
84 /** Wait modes for log_write_up_to @{ */
85 #define LOG_NO_WAIT		91
86 #define LOG_WAIT_ONE_GROUP	92
87 #define	LOG_WAIT_ALL_GROUPS	93
88 /* @} */
89 /** Maximum number of log groups in log_group_t::checkpoint_buf */
90 #define LOG_MAX_N_GROUPS	32
91 
92 #define IB_ARCHIVED_LOGS_PREFIX		"ib_log_archive_"
93 #define IB_ARCHIVED_LOGS_PREFIX_LEN	(sizeof(IB_ARCHIVED_LOGS_PREFIX) - 1)
94 #define IB_ARCHIVED_LOGS_SERIAL_LEN	20
95 
96 /*******************************************************************//**
97 Calculates where in log files we find a specified lsn.
98 @return	log file number */
99 UNIV_INTERN
100 ulint
101 log_calc_where_lsn_is(
102 /*==================*/
103 	ib_int64_t*	log_file_offset,	/*!< out: offset in that file
104 						(including the header) */
105 	ib_uint64_t	first_header_lsn,	/*!< in: first log file start
106 						lsn */
107 	ib_uint64_t	lsn,			/*!< in: lsn whose position to
108 						determine */
109 	ulint		n_log_files,		/*!< in: total number of log
110 						files */
111 	ib_int64_t	log_file_size);		/*!< in: log file size
112 						(including the header) */
113 #ifndef UNIV_HOTBACKUP
114 /************************************************************//**
115 Writes to the log the string given. The log must be released with
116 log_release.
117 @return	end lsn of the log record, zero if did not succeed */
118 UNIV_INLINE
119 lsn_t
120 log_reserve_and_write_fast(
121 /*=======================*/
122 	const void*	str,	/*!< in: string */
123 	ulint		len,	/*!< in: string length */
124 	lsn_t*		start_lsn);/*!< out: start lsn of the log record */
125 /***********************************************************************//**
126 Releases the log mutex. */
127 UNIV_INLINE
128 void
129 log_release(void);
130 /*=============*/
131 /***********************************************************************//**
132 Checks if there is need for a log buffer flush or a new checkpoint, and does
133 this if yes. Any database operation should call this when it has modified
134 more than about 4 pages. NOTE that this function may only be called when the
135 OS thread owns no synchronization objects except the dictionary mutex. */
136 UNIV_INLINE
137 void
138 log_free_check(void);
139 /*================*/
140 /**************************************************************************//**
141 Locks the log mutex and opens the log for log_write_low. The log must be closed
142 with log_close and released with log_release.
143 @return start lsn of the log record */
144 UNIV_INLINE
145 lsn_t
146 log_reserve_and_open(
147 /*=================*/
148 	ulint	len);	/*!< in: length of data to be catenated */
149 /************************************************************//**
150 Opens the log for log_write_low. The log must be closed with log_close.
151 @return	start lsn of the log record */
152 UNIV_INTERN
153 lsn_t
154 log_open(
155 /*=====*/
156 	ulint	len);	/*!< in: length of data to be catenated */
157 /************************************************************//**
158 Writes to the log the string given. It is assumed that the caller holds the
159 log mutex. */
160 UNIV_INTERN
161 void
162 log_write_low(
163 /*==========*/
164 	byte*	str,		/*!< in: string */
165 	ulint	str_len);	/*!< in: string length */
166 /************************************************************//**
167 Closes the log.
168 @return	lsn */
169 UNIV_INTERN
170 lsn_t
171 log_close(void);
172 /*===========*/
173 /************************************************************//**
174 Gets the current lsn.
175 @return	current lsn */
176 UNIV_INLINE
177 lsn_t
178 log_get_lsn(void);
179 /*=============*/
180 /****************************************************************
181 Gets the log group capacity. It is OK to read the value without
182 holding log_sys->mutex because it is constant.
183 @return	log group capacity */
184 UNIV_INLINE
185 lsn_t
186 log_get_capacity(void);
187 /*==================*/
188 /****************************************************************
189 Get log_sys::max_modified_age_async. It is OK to read the value without
190 holding log_sys::mutex because it is constant.
191 @return	max_modified_age_async */
192 UNIV_INLINE
193 lsn_t
194 log_get_max_modified_age_async(void);
195 /*================================*/
196 /******************************************************//**
197 Initializes the log. */
198 UNIV_INTERN
199 void
200 log_init(void);
201 /*==========*/
202 /******************************************************************//**
203 Inits a log group to the log system. */
204 UNIV_INTERN
205 void
206 log_group_init(
207 /*===========*/
208 	ulint	id,			/*!< in: group id */
209 	ulint	n_files,		/*!< in: number of log files */
210 	lsn_t	file_size,		/*!< in: log file size in bytes */
211 	ulint	space_id,		/*!< in: space id of the file space
212 					which contains the log files of this
213 					group */
214 	ulint	archive_space_id);	/*!< in: space id of the file space
215 					which contains some archived log
216 					files for this group; currently, only
217 					for the first log group this is
218 					used */
219 /******************************************************//**
220 Completes an i/o to a log file. */
221 UNIV_INTERN
222 void
223 log_io_complete(
224 /*============*/
225 	log_group_t*	group);	/*!< in: log group */
226 /******************************************************//**
227 This function is called, e.g., when a transaction wants to commit. It checks
228 that the log has been written to the log file up to the last log entry written
229 by the transaction. If there is a flush running, it waits and checks if the
230 flush flushed enough. If not, starts a new flush. */
231 UNIV_INTERN
232 void
233 log_write_up_to(
234 /*============*/
235 	lsn_t	lsn,	/*!< in: log sequence number up to which
236 			the log should be written, LSN_MAX if not specified */
237 	ulint	wait,	/*!< in: LOG_NO_WAIT, LOG_WAIT_ONE_GROUP,
238 			or LOG_WAIT_ALL_GROUPS */
239 	ibool	flush_to_disk);
240 			/*!< in: TRUE if we want the written log
241 			also to be flushed to disk */
242 /****************************************************************//**
243 Does a syncronous flush of the log buffer to disk. */
244 UNIV_INTERN
245 void
246 log_buffer_flush_to_disk(void);
247 /*==========================*/
248 /****************************************************************//**
249 This functions writes the log buffer to the log file and if 'flush'
250 is set it forces a flush of the log file as well. This is meant to be
251 called from background master thread only as it does not wait for
252 the write (+ possible flush) to finish. */
253 UNIV_INTERN
254 void
255 log_buffer_sync_in_background(
256 /*==========================*/
257 	ibool	flush);	/*<! in: flush the logs to disk */
258 /******************************************************//**
259 Makes a checkpoint. Note that this function does not flush dirty
260 blocks from the buffer pool: it only checks what is lsn of the oldest
261 modification in the pool, and writes information about the lsn in
262 log files. Use log_make_checkpoint_at to flush also the pool.
263 @return	TRUE if success, FALSE if a checkpoint write was already running */
264 UNIV_INTERN
265 ibool
266 log_checkpoint(
267 /*===========*/
268 	ibool	sync,		/*!< in: TRUE if synchronous operation is
269 				desired */
270 	ibool	write_always);	/*!< in: the function normally checks if the
271 				the new checkpoint would have a greater
272 				lsn than the previous one: if not, then no
273 				physical write is done; by setting this
274 				parameter TRUE, a physical write will always be
275 				made to log files */
276 /****************************************************************//**
277 Makes a checkpoint at a given lsn or later. */
278 UNIV_INTERN
279 void
280 log_make_checkpoint_at(
281 /*===================*/
282 	lsn_t	lsn,		/*!< in: make a checkpoint at this or a
283 				later lsn, if LSN_MAX, makes
284 				a checkpoint at the latest lsn */
285 	ibool	write_always);	/*!< in: the function normally checks if
286 				the new checkpoint would have a
287 				greater lsn than the previous one: if
288 				not, then no physical write is done;
289 				by setting this parameter TRUE, a
290 				physical write will always be made to
291 				log files */
292 /****************************************************************//**
293 Makes a checkpoint at the latest lsn and writes it to first page of each
294 data file in the database, so that we know that the file spaces contain
295 all modifications up to that lsn. This can only be called at database
296 shutdown. This function also writes all log in log files to the log archive. */
297 UNIV_INTERN
298 void
299 logs_empty_and_mark_files_at_shutdown(void);
300 /*=======================================*/
301 /******************************************************//**
302 Reads a checkpoint info from a log group header to log_sys->checkpoint_buf. */
303 UNIV_INTERN
304 void
305 log_group_read_checkpoint_info(
306 /*===========================*/
307 	log_group_t*	group,	/*!< in: log group */
308 	ulint		field);	/*!< in: LOG_CHECKPOINT_1 or LOG_CHECKPOINT_2 */
309 /*******************************************************************//**
310 Gets info from a checkpoint about a log group. */
311 UNIV_INTERN
312 void
313 log_checkpoint_get_nth_group_info(
314 /*==============================*/
315 	const byte*	buf,	/*!< in: buffer containing checkpoint info */
316 	ulint		n,	/*!< in: nth slot */
317 	lsn_t*		file_no);/*!< out: archived file number */
318 /******************************************************//**
319 Writes checkpoint info to groups. */
320 UNIV_INTERN
321 void
322 log_groups_write_checkpoint_info(void);
323 /*==================================*/
324 /********************************************************************//**
325 Starts an archiving operation.
326 @return	TRUE if succeed, FALSE if an archiving operation was already running */
327 UNIV_INTERN
328 ibool
329 log_archive_do(
330 /*===========*/
331 	ibool	sync,	/*!< in: TRUE if synchronous operation is desired */
332 	ulint*	n_bytes);/*!< out: archive log buffer size, 0 if nothing to
333 			archive */
334 /****************************************************************//**
335 Starts again archiving which has been stopped.
336 @return	DB_SUCCESS or DB_ERROR */
337 UNIV_INTERN
338 ulint
339 log_archive_start(void);
340 /*===================*/
341 /****************************************************************//**
342 Stop archiving the log so that a gap may occur in the archived log files.
343 @return	DB_SUCCESS or DB_ERROR */
344 UNIV_INTERN
345 ulint
346 log_archive_noarchivelog(void);
347 /*==========================*/
348 /****************************************************************//**
349 Start archiving the log so that a gap may occur in the archived log files.
350 @return	DB_SUCCESS or DB_ERROR */
351 UNIV_INTERN
352 ulint
353 log_archive_archivelog(void);
354 /*========================*/
355 /******************************************************//**
356 Generates an archived log file name. */
357 UNIV_INTERN
358 void
359 log_archived_file_name_gen(
360 /*=======================*/
361 	char*	buf,	/*!< in: buffer where to write */
362 	ulint	buf_len,/*!< in: buffer length */
363 	ulint	id,	/*!< in: group id */
364 	lsn_t	file_no);/*!< in: file number */
365 
366 UNIV_INTERN
367 void
368 log_archived_get_offset(
369 /*====================*/
370 	log_group_t*	group,		/*!< in: log group */
371 	lsn_t		file_no,	/*!< in: archive log file number */
372 	lsn_t		archived_lsn,	/*!< in: last archived LSN */
373 	lsn_t*		offset);	/*!< out: offset within archived file */
374 #else /* !UNIV_HOTBACKUP */
375 /******************************************************//**
376 Writes info to a buffer of a log group when log files are created in
377 backup restoration. */
378 UNIV_INTERN
379 void
380 log_reset_first_header_and_checkpoint(
381 /*==================================*/
382 	byte*		hdr_buf,/*!< in: buffer which will be written to the
383 				start of the first log file */
384 	ib_uint64_t	start);	/*!< in: lsn of the start of the first log file;
385 				we pretend that there is a checkpoint at
386 				start + LOG_BLOCK_HDR_SIZE */
387 #endif /* !UNIV_HOTBACKUP */
388 /********************************************************************//**
389 Checks that there is enough free space in the log to start a new query step.
390 Flushes the log buffer or makes a new checkpoint if necessary. NOTE: this
391 function may only be called if the calling thread owns no synchronization
392 objects! */
393 UNIV_INTERN
394 void
395 log_check_margins(void);
396 /*===================*/
397 #ifndef UNIV_HOTBACKUP
398 /******************************************************//**
399 Reads a specified log segment to a buffer. */
400 UNIV_INTERN
401 void
402 log_group_read_log_seg(
403 /*===================*/
404 	ulint		type,		/*!< in: LOG_ARCHIVE or LOG_RECOVER */
405 	byte*		buf,		/*!< in: buffer where to read */
406 	log_group_t*	group,		/*!< in: log group */
407 	lsn_t		start_lsn,	/*!< in: read area start */
408 	lsn_t		end_lsn,	/*!< in: read area end */
409 	ibool		release_mutex);	/*!< in: whether the log_sys->mutex
410 				        should be released before the read */
411 /******************************************************//**
412 Writes a buffer to a log file group. */
413 UNIV_INTERN
414 void
415 log_group_write_buf(
416 /*================*/
417 	log_group_t*	group,		/*!< in: log group */
418 	byte*		buf,		/*!< in: buffer */
419 	ulint		len,		/*!< in: buffer len; must be divisible
420 					by OS_FILE_LOG_BLOCK_SIZE */
421 	lsn_t		start_lsn,	/*!< in: start lsn of the buffer; must
422 					be divisible by
423 					OS_FILE_LOG_BLOCK_SIZE */
424 	ulint		new_data_offset);/*!< in: start offset of new data in
425 					buf: this parameter is used to decide
426 					if we have to write a new log file
427 					header */
428 /********************************************************//**
429 Sets the field values in group to correspond to a given lsn. For this function
430 to work, the values must already be correctly initialized to correspond to
431 some lsn, for instance, a checkpoint lsn. */
432 UNIV_INTERN
433 void
434 log_group_set_fields(
435 /*=================*/
436 	log_group_t*	group,	/*!< in/out: group */
437 	lsn_t		lsn);	/*!< in: lsn for which the values should be
438 				set */
439 /******************************************************//**
440 Calculates the data capacity of a log group, when the log file headers are not
441 included.
442 @return	capacity in bytes */
443 UNIV_INTERN
444 lsn_t
445 log_group_get_capacity(
446 /*===================*/
447 	const log_group_t*	group);	/*!< in: log group */
448 #endif /* !UNIV_HOTBACKUP */
449 /************************************************************//**
450 Gets a log block flush bit.
451 @return	TRUE if this block was the first to be written in a log flush */
452 UNIV_INLINE
453 ibool
454 log_block_get_flush_bit(
455 /*====================*/
456 	const byte*	log_block);	/*!< in: log block */
457 /************************************************************//**
458 Gets a log block number stored in the header.
459 @return	log block number stored in the block header */
460 UNIV_INLINE
461 ulint
462 log_block_get_hdr_no(
463 /*=================*/
464 	const byte*	log_block);	/*!< in: log block */
465 /************************************************************//**
466 Gets a log block data length.
467 @return	log block data length measured as a byte offset from the block start */
468 UNIV_INLINE
469 ulint
470 log_block_get_data_len(
471 /*===================*/
472 	const byte*	log_block);	/*!< in: log block */
473 /************************************************************//**
474 Sets the log block data length. */
475 UNIV_INLINE
476 void
477 log_block_set_data_len(
478 /*===================*/
479 	byte*	log_block,	/*!< in/out: log block */
480 	ulint	len);		/*!< in: data length */
481 /************************************************************//**
482 Calculates the checksum for a log block.
483 @return	checksum */
484 UNIV_INLINE
485 ulint
486 log_block_calc_checksum(
487 /*====================*/
488 	const byte*	block);	/*!< in: log block */
489 /************************************************************//**
490 Gets a log block checksum field value.
491 @return	checksum */
492 UNIV_INLINE
493 ulint
494 log_block_get_checksum(
495 /*===================*/
496 	const byte*	log_block);	/*!< in: log block */
497 /************************************************************//**
498 Sets a log block checksum field value. */
499 UNIV_INLINE
500 void
501 log_block_set_checksum(
502 /*===================*/
503 	byte*	log_block,	/*!< in/out: log block */
504 	ulint	checksum);	/*!< in: checksum */
505 /************************************************************//**
506 Gets a log block first mtr log record group offset.
507 @return first mtr log record group byte offset from the block start, 0
508 if none */
509 UNIV_INLINE
510 ulint
511 log_block_get_first_rec_group(
512 /*==========================*/
513 	const byte*	log_block);	/*!< in: log block */
514 /************************************************************//**
515 Sets the log block first mtr log record group offset. */
516 UNIV_INLINE
517 void
518 log_block_set_first_rec_group(
519 /*==========================*/
520 	byte*	log_block,	/*!< in/out: log block */
521 	ulint	offset);	/*!< in: offset, 0 if none */
522 /************************************************************//**
523 Gets a log block checkpoint number field (4 lowest bytes).
524 @return	checkpoint no (4 lowest bytes) */
525 UNIV_INLINE
526 ulint
527 log_block_get_checkpoint_no(
528 /*========================*/
529 	const byte*	log_block);	/*!< in: log block */
530 /************************************************************//**
531 Initializes a log block in the log buffer. */
532 UNIV_INLINE
533 void
534 log_block_init(
535 /*===========*/
536 	byte*	log_block,	/*!< in: pointer to the log buffer */
537 	lsn_t	lsn);		/*!< in: lsn within the log block */
538 /************************************************************//**
539 Initializes a log block in the log buffer in the old, < 3.23.52 format, where
540 there was no checksum yet. */
541 UNIV_INLINE
542 void
543 log_block_init_in_old_format(
544 /*=========================*/
545 	byte*	log_block,	/*!< in: pointer to the log buffer */
546 	lsn_t	lsn);		/*!< in: lsn within the log block */
547 /************************************************************//**
548 Converts a lsn to a log block number.
549 @return	log block number, it is > 0 and <= 1G */
550 UNIV_INLINE
551 ulint
552 log_block_convert_lsn_to_no(
553 /*========================*/
554 	lsn_t	lsn);	/*!< in: lsn of a byte within the block */
555 /******************************************************//**
556 Prints info of the log. */
557 UNIV_INTERN
558 void
559 log_print(
560 /*======*/
561 	FILE*	file);	/*!< in: file where to print */
562 /******************************************************//**
563 Peeks the current lsn.
564 @return	TRUE if success, FALSE if could not get the log system mutex */
565 UNIV_INTERN
566 ibool
567 log_peek_lsn(
568 /*=========*/
569 	lsn_t*	lsn);	/*!< out: if returns TRUE, current lsn is here */
570 /**********************************************************************//**
571 Refreshes the statistics used to print per-second averages. */
572 UNIV_INTERN
573 void
574 log_refresh_stats(void);
575 /*===================*/
576 /********************************************************//**
577 Closes all log groups. */
578 UNIV_INTERN
579 void
580 log_group_close_all(void);
581 /*=====================*/
582 /********************************************************//**
583 Shutdown the log system but do not release all the memory. */
584 UNIV_INTERN
585 void
586 log_shutdown(void);
587 /*==============*/
588 /********************************************************//**
589 Free the log system data structures. */
590 UNIV_INTERN
591 void
592 log_mem_free(void);
593 /*==============*/
594 
595 /****************************************************************//**
596 Safely reads the log_sys->tracked_lsn value.  The writer counterpart function
597 is log_set_tracked_lsn() in log0online.c.
598 
599 @return log_sys->tracked_lsn value. */
600 UNIV_INLINE
601 lsn_t
602 log_get_tracked_lsn(void);
603 /*=====================*/
604 
605 extern log_t*	log_sys;
606 
607 /* Values used as flags */
608 #define LOG_FLUSH	7652559
609 #define LOG_CHECKPOINT	78656949
610 #ifdef UNIV_LOG_ARCHIVE
611 # define LOG_ARCHIVE	11122331
612 #endif /* UNIV_LOG_ARCHIVE */
613 #define LOG_RECOVER	98887331
614 
615 /* The counting of lsn's starts from this value: this must be non-zero */
616 #define LOG_START_LSN		((lsn_t) (16 * OS_FILE_LOG_BLOCK_SIZE))
617 
618 #define LOG_BUFFER_SIZE		(srv_log_buffer_size * UNIV_PAGE_SIZE)
619 #define LOG_ARCHIVE_BUF_SIZE	(srv_log_buffer_size * UNIV_PAGE_SIZE / 4)
620 
621 /* Offsets of a log block header */
622 #define	LOG_BLOCK_HDR_NO	0	/* block number which must be > 0 and
623 					is allowed to wrap around at 2G; the
624 					highest bit is set to 1 if this is the
625 					first log block in a log flush write
626 					segment */
627 #define LOG_BLOCK_FLUSH_BIT_MASK 0x80000000UL
628 					/* mask used to get the highest bit in
629 					the preceding field */
630 #define	LOG_BLOCK_HDR_DATA_LEN	4	/* number of bytes of log written to
631 					this block */
632 #define	LOG_BLOCK_FIRST_REC_GROUP 6	/* offset of the first start of an
633 					mtr log record group in this log block,
634 					0 if none; if the value is the same
635 					as LOG_BLOCK_HDR_DATA_LEN, it means
636 					that the first rec group has not yet
637 					been catenated to this log block, but
638 					if it will, it will start at this
639 					offset; an archive recovery can
640 					start parsing the log records starting
641 					from this offset in this log block,
642 					if value not 0 */
643 #define LOG_BLOCK_CHECKPOINT_NO	8	/* 4 lower bytes of the value of
644 					log_sys->next_checkpoint_no when the
645 					log block was last written to: if the
646 					block has not yet been written full,
647 					this value is only updated before a
648 					log buffer flush */
649 #define LOG_BLOCK_HDR_SIZE	12	/* size of the log block header in
650 					bytes */
651 
652 /* Offsets of a log block trailer from the end of the block */
653 #define	LOG_BLOCK_CHECKSUM	4	/* 4 byte checksum of the log block
654 					contents; in InnoDB versions
655 					< 3.23.52 this did not contain the
656 					checksum but the same value as
657 					.._HDR_NO */
658 #define	LOG_BLOCK_TRL_SIZE	4	/* trailer size in bytes */
659 
660 /* Offsets for a checkpoint field */
661 #define LOG_CHECKPOINT_NO		0
662 #define LOG_CHECKPOINT_LSN		8
663 #define LOG_CHECKPOINT_OFFSET_LOW32	16
664 #define LOG_CHECKPOINT_LOG_BUF_SIZE	20
665 #define	LOG_CHECKPOINT_ARCHIVED_LSN	24
666 #define	LOG_CHECKPOINT_GROUP_ARRAY	32
667 
668 /* For each value smaller than LOG_MAX_N_GROUPS the following 8 bytes: */
669 
670 #define LOG_CHECKPOINT_ARCHIVED_FILE_NO	0
671 #define LOG_CHECKPOINT_ARCHIVED_OFFSET	4
672 
673 #define	LOG_CHECKPOINT_ARRAY_END	(LOG_CHECKPOINT_GROUP_ARRAY\
674 							+ LOG_MAX_N_GROUPS * 8)
675 #define LOG_CHECKPOINT_CHECKSUM_1	LOG_CHECKPOINT_ARRAY_END
676 #define LOG_CHECKPOINT_CHECKSUM_2	(4 + LOG_CHECKPOINT_ARRAY_END)
677 #if 0
678 #define LOG_CHECKPOINT_FSP_FREE_LIMIT	(8 + LOG_CHECKPOINT_ARRAY_END)
679 					/*!< Not used (0);
680 					This used to contain the
681 					current fsp free limit in
682 					tablespace 0, in units of one
683 					megabyte.
684 
685 					This information might have been used
686 					since mysqlbackup version 0.35 but
687 					before 1.41 to decide if unused ends of
688 					non-auto-extending data files
689 					in space 0 can be truncated.
690 
691 					This information was made obsolete
692 					by mysqlbackup --compress. */
693 #define LOG_CHECKPOINT_FSP_MAGIC_N	(12 + LOG_CHECKPOINT_ARRAY_END)
694 					/*!< Not used (0);
695 					This magic number tells if the
696 					checkpoint contains the above field:
697 					the field was added to
698 					InnoDB-3.23.50 and
699 					removed from MySQL 5.6 */
700 #define LOG_CHECKPOINT_FSP_MAGIC_N_VAL	1441231243
701 					/*!< if LOG_CHECKPOINT_FSP_MAGIC_N
702 					contains this value, then
703 					LOG_CHECKPOINT_FSP_FREE_LIMIT
704 					is valid */
705 #endif
706 #define LOG_CHECKPOINT_OFFSET_HIGH32	(16 + LOG_CHECKPOINT_ARRAY_END)
707 #define LOG_CHECKPOINT_SIZE		(20 + LOG_CHECKPOINT_ARRAY_END)
708 
709 
710 /* Offsets of a log file header */
711 #define LOG_GROUP_ID		0	/* log group number */
712 #define LOG_FILE_START_LSN	4	/* lsn of the start of data in this
713 					log file */
714 #define LOG_FILE_NO		12	/* 4-byte archived log file number;
715 					this field is only defined in an
716 					archived log file */
717 #define LOG_FILE_WAS_CREATED_BY_HOT_BACKUP 16
718 					/* a 32-byte field which contains
719 					the string 'ibbackup' and the
720 					creation time if the log file was
721 					created by mysqlbackup --restore;
722 					when mysqld is first time started
723 					on the restored database, it can
724 					print helpful info for the user */
725 #define LOG_FILE_OS_FILE_LOG_BLOCK_SIZE 64
726 					/* extend to record log_block_size
727 					of XtraDB. 0 means default 512 */
728 #define	LOG_FILE_ARCH_COMPLETED	OS_FILE_LOG_BLOCK_SIZE
729 					/* this 4-byte field is TRUE when
730 					the writing of an archived log file
731 					has been completed; this field is
732 					only defined in an archived log file */
733 #define LOG_FILE_END_LSN	(OS_FILE_LOG_BLOCK_SIZE + 4)
734 					/* lsn where the archived log file
735 					at least extends: actually the
736 					archived log file may extend to a
737 					later lsn, as long as it is within the
738 					same log block as this lsn; this field
739 					is defined only when an archived log
740 					file has been completely written */
741 #define LOG_CHECKPOINT_1	OS_FILE_LOG_BLOCK_SIZE
742 					/* first checkpoint field in the log
743 					header; we write alternately to the
744 					checkpoint fields when we make new
745 					checkpoints; this field is only defined
746 					in the first log file of a log group */
747 #define LOG_CHECKPOINT_2	(3 * OS_FILE_LOG_BLOCK_SIZE)
748 					/* second checkpoint field in the log
749 					header */
750 #define LOG_FILE_HDR_SIZE	(4 * OS_FILE_LOG_BLOCK_SIZE)
751 
752 #define LOG_GROUP_OK		301
753 #define LOG_GROUP_CORRUPTED	302
754 
755 /** Log group consists of a number of log files, each of the same size; a log
756 group is implemented as a space in the sense of the module fil0fil. */
757 struct log_group_t{
758 	/* The following fields are protected by log_sys->mutex */
759 	ulint		id;		/*!< log group id */
760 	ulint		n_files;	/*!< number of files in the group */
761 	lsn_t		file_size;	/*!< individual log file size in bytes,
762 					including the log file header */
763 	ulint		space_id;	/*!< file space which implements the log
764 					group */
765 	ulint		state;		/*!< LOG_GROUP_OK or
766 					LOG_GROUP_CORRUPTED */
767 	lsn_t		lsn;		/*!< lsn used to fix coordinates within
768 					the log group */
769 	lsn_t		lsn_offset;	/*!< the offset of the above lsn */
770 	ulint		n_pending_writes;/*!< number of currently pending flush
771 					writes for this log group */
772 	byte**		file_header_bufs_ptr;/*!< unaligned buffers */
773 	byte**		file_header_bufs;/*!< buffers for each file
774 					header in the group */
775 #ifdef UNIV_LOG_ARCHIVE
776 	/*-----------------------------*/
777 	byte**		archive_file_header_bufs_ptr;/*!< unaligned buffers */
778 	byte**		archive_file_header_bufs;/*!< buffers for each file
779 					header in the group */
780 	ulint		archive_space_id;/*!< file space which
781 					implements the log group
782 					archive */
783 	lsn_t		archived_file_no;/*!< file number corresponding to
784 					log_sys->archived_lsn */
785 	lsn_t		archived_offset;/*!< file offset corresponding to
786 					log_sys->archived_lsn, 0 if we have
787 					not yet written to the archive file
788 					number archived_file_no */
789 	lsn_t		next_archived_file_no;/*!< during an archive write,
790 					until the write is completed, we
791 					store the next value for
792 					archived_file_no here: the write
793 					completion function then sets the new
794 					value to ..._file_no */
795 	lsn_t		next_archived_offset; /*!< like the preceding field */
796 #endif /* UNIV_LOG_ARCHIVE */
797 	/*-----------------------------*/
798 	lsn_t		scanned_lsn;	/*!< used only in recovery: recovery scan
799 					succeeded up to this lsn in this log
800 					group */
801 	byte*		checkpoint_buf_ptr;/*!< unaligned checkpoint header */
802 	byte*		checkpoint_buf;	/*!< checkpoint header is written from
803 					this buffer to the group */
804 	UT_LIST_NODE_T(log_group_t)
805 			log_groups;	/*!< list of log groups */
806 };
807 
808 /** Redo log buffer */
809 struct log_t{
810 	byte		pad[64];	/*!< padding to prevent other memory
811 					update hotspots from residing on the
812 					same memory cache line */
813 	lsn_t		lsn;		/*!< log sequence number */
814 	ulint		buf_free;	/*!< first free offset within the log
815 					buffer */
816 #ifndef UNIV_HOTBACKUP
817 	ib_prio_mutex_t		mutex;		/*!< mutex protecting the log */
818 
819 	ib_mutex_t		log_flush_order_mutex;/*!< mutex to serialize access to
820 					the flush list when we are putting
821 					dirty blocks in the list. The idea
822 					behind this mutex is to be able
823 					to release log_sys->mutex during
824 					mtr_commit and still ensure that
825 					insertions in the flush_list happen
826 					in the LSN order. */
827 #endif /* !UNIV_HOTBACKUP */
828 	byte*		buf_ptr;	/* unaligned log buffer */
829 	byte*		buf;		/*!< log buffer */
830 	ulint		buf_size;	/*!< log buffer size in bytes */
831 	ulint		max_buf_free;	/*!< recommended maximum value of
832 					buf_free, after which the buffer is
833 					flushed */
834  #ifdef UNIV_LOG_DEBUG
835 	ulint		old_buf_free;	/*!< value of buf free when log was
836 					last time opened; only in the debug
837 					version */
838 	ib_uint64_t	old_lsn;	/*!< value of lsn when log was
839 					last time opened; only in the
840 					debug version */
841 #endif /* UNIV_LOG_DEBUG */
842 	ibool		check_flush_or_checkpoint;
843 					/*!< this is set to TRUE when there may
844 					be need to flush the log buffer, or
845 					preflush buffer pool pages, or make
846 					a checkpoint; this MUST be TRUE when
847 					lsn - last_checkpoint_lsn >
848 					max_checkpoint_age; this flag is
849 					peeked at by log_free_check(), which
850 					does not reserve the log mutex */
851 	UT_LIST_BASE_NODE_T(log_group_t)
852 			log_groups;	/*!< log groups */
853 
854 #ifndef UNIV_HOTBACKUP
855 	/** The fields involved in the log buffer flush @{ */
856 
857 	ulint		buf_next_to_write;/*!< first offset in the log buffer
858 					where the byte content may not exist
859 					written to file, e.g., the start
860 					offset of a log record catenated
861 					later; this is advanced when a flush
862 					operation is completed to all the log
863 					groups */
864 	volatile bool	is_extending;	/*!< this is set to true during extend
865 					the log buffer size */
866 	lsn_t		written_to_some_lsn;
867 					/*!< first log sequence number not yet
868 					written to any log group; for this to
869 					be advanced, it is enough that the
870 					write i/o has been completed for any
871 					one log group */
872 	lsn_t		written_to_all_lsn;
873 					/*!< first log sequence number not yet
874 					written to some log group; for this to
875 					be advanced, it is enough that the
876 					write i/o has been completed for all
877 					log groups.
878 					Note that since InnoDB currently
879 					has only one log group therefore
880 					this value is redundant. Also it
881 					is possible that this value
882 					falls behind the
883 					flushed_to_disk_lsn transiently.
884 					It is appropriate to use either
885 					flushed_to_disk_lsn or
886 					write_lsn which are always
887 					up-to-date and accurate. */
888 	lsn_t		write_lsn;	/*!< end lsn for the current running
889 					write */
890 	ulint		write_end_offset;/*!< the data in buffer has
891 					been written up to this offset
892 					when the current write ends:
893 					this field will then be copied
894 					to buf_next_to_write */
895 	lsn_t		current_flush_lsn;/*!< end lsn for the current running
896 					write + flush operation */
897 	lsn_t		flushed_to_disk_lsn;
898 					/*!< how far we have written the log
899 					AND flushed to disk */
900 	ulint		n_pending_writes;/*!< number of currently
901 					pending flushes or writes */
902 	/* NOTE on the 'flush' in names of the fields below: starting from
903 	4.0.14, we separate the write of the log file and the actual fsync()
904 	or other method to flush it to disk. The names below shhould really
905 	be 'flush_or_write'! */
906 	os_event_t	no_flush_event;	/*!< this event is in the reset state
907 					when a flush or a write is running;
908 					a thread should wait for this without
909 					owning the log mutex, but NOTE that
910 					to set or reset this event, the
911 					thread MUST own the log mutex! */
912 	ibool		one_flushed;	/*!< during a flush, this is
913 					first FALSE and becomes TRUE
914 					when one log group has been
915 					written or flushed */
916 	os_event_t	one_flushed_event;/*!< this event is reset when the
917 					flush or write has not yet completed
918 					for any log group; e.g., this means
919 					that a transaction has been committed
920 					when this is set; a thread should wait
921 					for this without owning the log mutex,
922 					but NOTE that to set or reset this
923 					event, the thread MUST own the log
924 					mutex! */
925 	ulint		n_log_ios;	/*!< number of log i/os initiated thus
926 					far */
927 	ulint		n_log_ios_old;	/*!< number of log i/o's at the
928 					previous printout */
929 	time_t		last_printout_time;/*!< when log_print was last time
930 					called */
931 	/* @} */
932 
933 	/** Fields involved in checkpoints @{ */
934 	lsn_t		log_group_capacity; /*!< capacity of the log group; if
935 					the checkpoint age exceeds this, it is
936 					a serious error because it is possible
937 					we will then overwrite log and spoil
938 					crash recovery */
939 	lsn_t		max_modified_age_async;
940 					/*!< when this recommended
941 					value for lsn -
942 					buf_pool_get_oldest_modification()
943 					is exceeded, we start an
944 					asynchronous preflush of pool pages */
945 	lsn_t		max_modified_age_sync;
946 					/*!< when this recommended
947 					value for lsn -
948 					buf_pool_get_oldest_modification()
949 					is exceeded, we start a
950 					synchronous preflush of pool pages */
951 	lsn_t		max_checkpoint_age_async;
952 					/*!< when this checkpoint age
953 					is exceeded we start an
954 					asynchronous writing of a new
955 					checkpoint */
956 	lsn_t		max_checkpoint_age;
957 					/*!< this is the maximum allowed value
958 					for lsn - last_checkpoint_lsn when a
959 					new query step is started */
960 	ib_uint64_t	next_checkpoint_no;
961 					/*!< next checkpoint number */
962 	lsn_t		last_checkpoint_lsn;
963 					/*!< latest checkpoint lsn */
964 	lsn_t		next_checkpoint_lsn;
965 					/*!< next checkpoint lsn */
966 	ulint		n_pending_checkpoint_writes;
967 					/*!< number of currently pending
968 					checkpoint writes */
969 	rw_lock_t	checkpoint_lock;/*!< this latch is x-locked when a
970 					checkpoint write is running; a thread
971 					should wait for this without owning
972 					the log mutex */
973 #endif /* !UNIV_HOTBACKUP */
974 	byte*		checkpoint_buf_ptr;/* unaligned checkpoint header */
975 	byte*		checkpoint_buf;	/*!< checkpoint header is read to this
976 					buffer */
977 	/* @} */
978 #ifdef UNIV_LOG_ARCHIVE
979 	/** Fields involved in archiving @{ */
980 	ulint		archiving_state;/*!< LOG_ARCH_ON, LOG_ARCH_STOPPING
981 					LOG_ARCH_STOPPED, LOG_ARCH_OFF */
982 	lsn_t		archived_lsn;	/*!< archiving has advanced to this
983 					lsn */
984 	lsn_t		max_archived_lsn_age_async;
985 					/*!< recommended maximum age of
986 					archived_lsn, before we start
987 					asynchronous copying to the archive */
988 	lsn_t		max_archived_lsn_age;
989 					/*!< maximum allowed age for
990 					archived_lsn */
991 	lsn_t		next_archived_lsn;/*!< during an archive write,
992 					until the write is completed, we
993 					store the next value for
994 					archived_lsn here: the write
995 					completion function then sets the new
996 					value to archived_lsn */
997 	ulint		archiving_phase;/*!< LOG_ARCHIVE_READ or
998 					LOG_ARCHIVE_WRITE */
999 	ulint		n_pending_archive_ios;
1000 					/*!< number of currently pending reads
1001 					or writes in archiving */
1002 	rw_lock_t	archive_lock;	/*!< this latch is x-locked when an
1003 					archive write is running; a thread
1004 					should wait for this without owning
1005 					the log mutex */
1006 	ulint		archive_buf_size;/*!< size of archive_buf */
1007 	byte*		archive_buf_ptr;/*!< unaligned archived_buf */
1008 	byte*		archive_buf;	/*!< log segment is written to the
1009 					archive from this buffer */
1010 	os_event_t	archiving_on;	/*!< if archiving has been stopped,
1011 					a thread can wait for this event to
1012 					become signaled */
1013 	/* @} */
1014 #endif /* UNIV_LOG_ARCHIVE */
1015 	lsn_t		tracked_lsn;	/*!< log tracking has advanced to this
1016 					lsn.  Field accessed atomically where
1017 					64-bit atomic ops are supported,
1018 					protected by the log sys mutex
1019 					otherwise. */
1020 };
1021 
1022 /** Test if flush order mutex is owned. */
1023 #define log_flush_order_mutex_own()	\
1024 	mutex_own(&log_sys->log_flush_order_mutex)
1025 
1026 /** Acquire the flush order mutex. */
1027 #define log_flush_order_mutex_enter() do {		\
1028 	mutex_enter(&log_sys->log_flush_order_mutex);	\
1029 } while (0)
1030 /** Release the flush order mutex. */
1031 # define log_flush_order_mutex_exit() do {		\
1032 	mutex_exit(&log_sys->log_flush_order_mutex);	\
1033 } while (0)
1034 
1035 #ifdef UNIV_LOG_ARCHIVE
1036 /** Archiving state @{ */
1037 #define LOG_ARCH_ON		71
1038 #define LOG_ARCH_STOPPING	72
1039 #define LOG_ARCH_STOPPING2	73
1040 #define LOG_ARCH_STOPPED	74
1041 #define LOG_ARCH_OFF		75
1042 /* @} */
1043 #endif /* UNIV_LOG_ARCHIVE */
1044 
1045 #ifndef UNIV_NONINL
1046 #include "log0log.ic"
1047 #endif
1048 
1049 #endif
1050