summaryrefslogtreecommitdiff
path: root/storage/innobase/include/mtr0log.ic
blob: bdfd98709d1939551311d60651a2353c9779856a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*****************************************************************************

Copyright (c) 1995, 2012, Oracle and/or its affiliates. All Rights Reserved.

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA

*****************************************************************************/

/**************************************************//**
@file include/mtr0log.ic
Mini-transaction logging routines

Created 12/7/1995 Heikki Tuuri
*******************************************************/

#include "mach0data.h"
#include "ut0lst.h"
#include "buf0buf.h"
#include "buf0dblwr.h"
#include "fsp0types.h"
#include "trx0sys.h"

/********************************************************//**
Opens a buffer to mlog. It must be closed with mlog_close.
@return	buffer, NULL if log mode MTR_LOG_NONE */
UNIV_INLINE
byte*
mlog_open(
/*======*/
	mtr_t*	mtr,	/*!< in: mtr */
	ulint	size)	/*!< in: buffer size in bytes; MUST be
			smaller than DYN_ARRAY_DATA_SIZE! */
{
	dyn_array_t*	mlog;

	mtr->modifications = TRUE;

	if (mtr_get_log_mode(mtr) == MTR_LOG_NONE) {

		return(NULL);
	}

	mlog = &(mtr->log);

	return(dyn_array_open(mlog, size));
}

/********************************************************//**
Closes a buffer opened to mlog. */
UNIV_INLINE
void
mlog_close(
/*=======*/
	mtr_t*	mtr,	/*!< in: mtr */
	byte*	ptr)	/*!< in: buffer space from ptr up was not used */
{
	dyn_array_t*	mlog;

	ut_ad(mtr_get_log_mode(mtr) != MTR_LOG_NONE);

	mlog = &(mtr->log);

	dyn_array_close(mlog, ptr);
}

#ifndef UNIV_HOTBACKUP
/********************************************************//**
Catenates 1 - 4 bytes to the mtr log. The value is not compressed. */
UNIV_INLINE
void
mlog_catenate_ulint(
/*================*/
	mtr_t*	mtr,	/*!< in: mtr */
	ulint	val,	/*!< in: value to write */
	ulint	type)	/*!< in: MLOG_1BYTE, MLOG_2BYTES, MLOG_4BYTES */
{
	dyn_array_t*	mlog;
	byte*		ptr;

	if (mtr_get_log_mode(mtr) == MTR_LOG_NONE) {

		return;
	}

	mlog = &(mtr->log);

#if MLOG_1BYTE != 1
# error "MLOG_1BYTE != 1"
#endif
#if MLOG_2BYTES != 2
# error "MLOG_2BYTES != 2"
#endif
#if MLOG_4BYTES != 4
# error "MLOG_4BYTES != 4"
#endif
#if MLOG_8BYTES != 8
# error "MLOG_8BYTES != 8"
#endif
	ptr = (byte*) dyn_array_push(mlog, type);

	if (type == MLOG_4BYTES) {
		mach_write_to_4(ptr, val);
	} else if (type == MLOG_2BYTES) {
		mach_write_to_2(ptr, val);
	} else {
		ut_ad(type == MLOG_1BYTE);
		mach_write_to_1(ptr, val);
	}
}

/********************************************************//**
Catenates a compressed ulint to mlog. */
UNIV_INLINE
void
mlog_catenate_ulint_compressed(
/*===========================*/
	mtr_t*	mtr,	/*!< in: mtr */
	ulint	val)	/*!< in: value to write */
{
	byte*	log_ptr;

	log_ptr = mlog_open(mtr, 10);

	/* If no logging is requested, we may return now */
	if (log_ptr == NULL) {

		return;
	}

	log_ptr += mach_write_compressed(log_ptr, val);

	mlog_close(mtr, log_ptr);
}

/********************************************************//**
Catenates a compressed 64-bit integer to mlog. */
UNIV_INLINE
void
mlog_catenate_ull_compressed(
/*=========================*/
	mtr_t*		mtr,	/*!< in: mtr */
	ib_uint64_t	val)	/*!< in: value to write */
{
	byte*	log_ptr;

	log_ptr = mlog_open(mtr, 15);

	/* If no logging is requested, we may return now */
	if (log_ptr == NULL) {

		return;
	}

	log_ptr += mach_ull_write_compressed(log_ptr, val);

	mlog_close(mtr, log_ptr);
}

/********************************************************//**
Writes the initial part of a log record (3..11 bytes).
If the implementation of this function is changed, all
size parameters to mlog_open() should be adjusted accordingly!
@return	new value of log_ptr */
UNIV_INLINE
byte*
mlog_write_initial_log_record_fast(
/*===============================*/
	const byte*	ptr,	/*!< in: pointer to (inside) a buffer
				frame holding the file page where
				modification is made */
	byte		type,	/*!< in: log item type: MLOG_1BYTE, ... */
	byte*		log_ptr,/*!< in: pointer to mtr log which has
				been opened */
	mtr_t*		mtr)	/*!< in: mtr */
{
#ifdef UNIV_DEBUG
	buf_block_t*	block;
#endif
	const byte*	page;
	ulint		space;
	ulint		offset;

	ut_ad(mtr_memo_contains_page(mtr, ptr, MTR_MEMO_PAGE_X_FIX));
	ut_ad(type <= MLOG_BIGGEST_TYPE || EXTRA_CHECK_MLOG_NUMBER(type));
	ut_ad(ptr && log_ptr);

	page = (const byte*) ut_align_down(ptr, UNIV_PAGE_SIZE);
	space = mach_read_from_4(page + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID);
	offset = mach_read_from_4(page + FIL_PAGE_OFFSET);

	/* check whether the page is in the doublewrite buffer;
	the doublewrite buffer is located in pages
	FSP_EXTENT_SIZE, ..., 3 * FSP_EXTENT_SIZE - 1 in the
	system tablespace */
	if (space == TRX_SYS_SPACE
	    && offset >= FSP_EXTENT_SIZE && offset < 3 * FSP_EXTENT_SIZE) {
		if (buf_dblwr_being_created) {
			/* Do nothing: we only come to this branch in an
			InnoDB database creation. We do not redo log
			anything for the doublewrite buffer pages. */
			return(log_ptr);
		} else {
			fprintf(stderr,
				"Error: trying to redo log a record of type "
				"%d on page %lu of space %lu in the "
				"doublewrite buffer, continuing anyway.\n"
				"Please post a bug report to "
				"https://jira.mariadb.org/\n",
				type, offset, space);
			ut_ad(0);
		}
	}

	mach_write_to_1(log_ptr, type);
	log_ptr++;
	log_ptr += mach_write_compressed(log_ptr, space);
	log_ptr += mach_write_compressed(log_ptr, offset);

	mtr->n_log_recs++;

#ifdef UNIV_LOG_DEBUG
	fprintf(stderr,
		"Adding to mtr log record type %lu space %lu page no %lu\n",
		(ulong) type, space, offset);
#endif

#ifdef UNIV_DEBUG
	/* We now assume that all x-latched pages have been modified! */
	block = (buf_block_t*) buf_block_align(ptr);

	if (!mtr_memo_contains(mtr, block, MTR_MEMO_MODIFY)) {

		mtr_memo_push(mtr, block, MTR_MEMO_MODIFY);
	}
#endif
	return(log_ptr);
}

/********************************************************//**
Writes a log record about an .ibd file create/delete/rename.
@return	new value of log_ptr */
UNIV_INLINE
byte*
mlog_write_initial_log_record_for_file_op(
/*======================================*/
	ulint	type,	/*!< in: MLOG_FILE_CREATE, MLOG_FILE_DELETE, or
			MLOG_FILE_RENAME */
	ulint	space_id,/*!< in: space id, if applicable */
	ulint	page_no,/*!< in: page number (not relevant currently) */
	byte*	log_ptr,/*!< in: pointer to mtr log which has been opened */
	mtr_t*	mtr)	/*!< in: mtr */
{
	ut_ad(log_ptr);

	mach_write_to_1(log_ptr, type);
	log_ptr++;

	/* We write dummy space id and page number */
	log_ptr += mach_write_compressed(log_ptr, space_id);
	log_ptr += mach_write_compressed(log_ptr, page_no);

	mtr->n_log_recs++;

	return(log_ptr);
}
#endif /* !UNIV_HOTBACKUP */