1 /*****************************************************************************
2 
3 Copyright (c) 1995, 2016, Oracle and/or its affiliates. All Rights Reserved.
4 Copyright (c) 2014, 2019, MariaDB Corporation.
5 
6 This program is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free Software
8 Foundation; version 2 of the License.
9 
10 This program is distributed in the hope that it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along with
15 this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA
17 
18 *****************************************************************************/
19 
20 /******************************************************
21 @file include/fsp0types.h
22 File space management types
23 
24 Created May 26, 2009 Vasil Dimov
25 *******************************************************/
26 
27 #ifndef fsp0types_h
28 #define fsp0types_h
29 
30 #include "univ.i"
31 
32 #ifndef UNIV_INNOCHECKSUM
33 
34 /** The fil_space_t::id of the redo log. All persistent tablespaces
35 have a smaller fil_space_t::id. */
36 #define SRV_LOG_SPACE_FIRST_ID		0xFFFFFFF0U
37 /** The fil_space_t::id of the innodb_temporary tablespace. */
38 #define SRV_TMP_SPACE_ID		0xFFFFFFFEU
39 
40 #include "ut0byte.h"
41 
42 /** @name Flags for inserting records in order
43 If records are inserted in order, there are the following
44 flags to tell this (their type is made byte for the compiler
45 to warn if direction and hint parameters are switched in
46 fseg_alloc_free_page) */
47 /* @{ */
id(&self) -> u3248 #define	FSP_UP		((byte)111)	/*!< alphabetically upwards */
49 #define	FSP_DOWN	((byte)112)	/*!< alphabetically downwards */
50 #define	FSP_NO_DIR	((byte)113)	/*!< no order */
51 /* @} */
52 
53 #endif /* !UNIV_INNOCHECKSUM */
54 /** File space extent size in pages
55 page size | file space extent size
56 ----------+-----------------------
57    4 KiB  | 256 pages = 1 MiB
58    8 KiB  | 128 pages = 1 MiB
59   16 KiB  |  64 pages = 1 MiB
60   32 KiB  |  64 pages = 2 MiB
61   64 KiB  |  64 pages = 4 MiB
62 */
63 #define FSP_EXTENT_SIZE         (srv_page_size_shift < 14 ?	\
64 				 (1048576U >> srv_page_size_shift) : 64U)
65 
66 /** File space extent size (four megabyte) in pages for MAX page size */
67 #define	FSP_EXTENT_SIZE_MAX	(4194304 / UNIV_PAGE_SIZE_MAX)
68 
69 /** File space extent size (one megabyte) in pages for MIN page size */
70 #define	FSP_EXTENT_SIZE_MIN	(1048576 / UNIV_PAGE_SIZE_MIN)
71 
72 /** On a page of any file segment, data may be put starting from this
73 offset */
74 #define FSEG_PAGE_DATA		FIL_PAGE_DATA
75 
76 #ifndef UNIV_INNOCHECKSUM
77 /** @name File segment header
78 The file segment header points to the inode describing the file segment. */
79 /* @{ */
80 /** Data type for file segment header */
81 typedef	byte	fseg_header_t;
82 
83 #define FSEG_HDR_SPACE		0	/*!< space id of the inode */
84 #define FSEG_HDR_PAGE_NO	4	/*!< page number of the inode */
85 #define FSEG_HDR_OFFSET		8	/*!< byte offset of the inode */
86 
87 #define FSEG_HEADER_SIZE	10	/*!< Length of the file system
88 					header, in bytes */
89 /* @} */
90 
fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result91 #ifdef UNIV_DEBUG
92 
93 struct mtr_t;
94 
95 /** A wrapper class to print the file segment header information. */
96 class fseg_header
97 {
spawn_child(cmd: &mut std::process::Command) -> io::Result<SpawnedChild>98 public:
99 	/** Constructor of fseg_header.
100 	@param[in]	header	the underlying file segment header object
101 	@param[in]	mtr	the mini-transaction.  No redo logs are
102 				generated, only latches are checked within
103 				mini-transaction */
104 	fseg_header(
105 		const fseg_header_t*	header,
106 		mtr_t*			mtr)
107 		:
108 		m_header(header),
109 		m_mtr(mtr)
110 	{}
111 
112 	/** Print the file segment header to the given output stream.
113 	@param[in,out]	out	the output stream into which the object
114 				is printed.
115 	@retval	the output stream into which the object was printed. */
116 	std::ostream&
id(&self) -> u32117 	to_stream(std::ostream&	out) const;
118 private:
119 	/** The underlying file segment header */
120 	const fseg_header_t*	m_header;
121 
122 	/** The mini transaction, which is used mainly to check whether
kill(&mut self) -> io::Result<()>123 	appropriate latches have been taken by the calling thread. */
124 	mtr_t*			m_mtr;
125 };
126 
127 /* Overloading the global output operator to print a file segment header
128 @param[in,out]	out	the output stream into which object will be printed
129 @param[in]	header	the file segment header to be printed
130 @retval the output stream */
poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>131 inline
132 std::ostream&
133 operator<<(
134 	std::ostream&		out,
135 	const fseg_header&	header)
136 {
137 	return(header.to_stream(out));
138 }
139 #endif /* UNIV_DEBUG */
140 
141 /** Flags for fsp_reserve_free_extents */
142 enum fsp_reserve_t {
143 	FSP_NORMAL,	/* reservation during normal B-tree operations */
144 	FSP_UNDO,	/* reservation done for undo logging */
read(&mut self, bytes: &mut [u8]) -> io::Result<usize>145 	FSP_CLEANING,	/* reservation done during purge operations */
146 	FSP_BLOB	/* reservation being done for BLOB insertion */
147 };
148 
149 /* Number of pages described in a single descriptor page: currently each page
150 description takes less than 1 byte; a descriptor page is repeated every
151 this many file pages */
152 /* #define XDES_DESCRIBED_PER_PAGE		srv_page_size */
153 /* This has been replaced with either srv_page_size or page_zip->size. */
write(&mut self, bytes: &[u8]) -> io::Result<usize>154 
155 /** @name The space low address page map
156 The pages at FSP_XDES_OFFSET and FSP_IBUF_BITMAP_OFFSET are repeated
157 every XDES_DESCRIBED_PER_PAGE pages in every tablespace. */
158 /* @{ */
159 /*--------------------------------------*/
160 #define FSP_XDES_OFFSET			0U	/* !< extent descriptor */
161 #define FSP_IBUF_BITMAP_OFFSET		1U	/* !< insert buffer bitmap */
162 				/* The ibuf bitmap pages are the ones whose
163 				page number is the number above plus a
164 				multiple of XDES_DESCRIBED_PER_PAGE */
165 
166 #define FSP_FIRST_INODE_PAGE_NO		2U	/*!< in every tablespace */
167 				/* The following pages exist
168 				in the system tablespace (space 0). */
169 #define FSP_IBUF_HEADER_PAGE_NO		3U	/*!< insert buffer
170 						header page, in
171 						tablespace 0 */
172 #define FSP_IBUF_TREE_ROOT_PAGE_NO	4U	/*!< insert buffer
173 						B-tree root page in
174 						tablespace 0 */
175 				/* The ibuf tree root page number in
176 				tablespace 0; its fseg inode is on the page
177 				number FSP_FIRST_INODE_PAGE_NO */
178 #define FSP_TRX_SYS_PAGE_NO		5U	/*!< transaction
179 						system header, in
180 						tablespace 0 */
181 #define	FSP_FIRST_RSEG_PAGE_NO		6U	/*!< first rollback segment
182 						page, in tablespace 0 */
183 #define FSP_DICT_HDR_PAGE_NO		7U	/*!< data dictionary header
184 						page, in tablespace 0 */
185 /*--------------------------------------*/
186 /* @} */
187 
188 /** Check if tablespace is system temporary.
189 @param[in]      space_id        verify is checksum is enabled for given space.
190 @return true if tablespace is system temporary. */
191 inline
192 bool
193 fsp_is_system_temporary(ulint	space_id)
194 {
195 	return(space_id == SRV_TMP_SPACE_ID);
196 }
197 #endif /* !UNIV_INNOCHECKSUM */
198 
199 /* @defgroup fsp_flags InnoDB Tablespace Flag Constants @{ */
200 
201 /** Width of the POST_ANTELOPE flag */
202 #define FSP_FLAGS_WIDTH_POST_ANTELOPE	1
203 /** Number of flag bits used to indicate the tablespace zip page size */
204 #define FSP_FLAGS_WIDTH_ZIP_SSIZE	4
stdio<T>(option: Option<T>) -> io::Result<Option<PollEvented<Fd<T>>>> where T: AsRawFd,205 /** Width of the ATOMIC_BLOBS flag.  The ability to break up a long
206 column into an in-record prefix and an externally stored part is available
207 to ROW_FORMAT=REDUNDANT and ROW_FORMAT=COMPACT. */
208 #define FSP_FLAGS_WIDTH_ATOMIC_BLOBS	1
209 /** Number of flag bits used to indicate the tablespace page size */
210 #define FSP_FLAGS_WIDTH_PAGE_SSIZE	4
211 /** Number of reserved bits */
212 #define FSP_FLAGS_WIDTH_RESERVED 6
213 /** Number of flag bits used to indicate the page compression */
214 #define FSP_FLAGS_WIDTH_PAGE_COMPRESSION 1
215 
216 /** Width of all the currently known persistent tablespace flags */
217 #define FSP_FLAGS_WIDTH		(FSP_FLAGS_WIDTH_POST_ANTELOPE	\
218 				+ FSP_FLAGS_WIDTH_ZIP_SSIZE	\
219 				+ FSP_FLAGS_WIDTH_ATOMIC_BLOBS	\
220 				+ FSP_FLAGS_WIDTH_PAGE_SSIZE	\
221 				+ FSP_FLAGS_WIDTH_RESERVED	\
222 				+ FSP_FLAGS_WIDTH_PAGE_COMPRESSION)
223 
224 /** A mask of all the known/used bits in FSP_SPACE_FLAGS */
225 #define FSP_FLAGS_MASK		(~(~0U << FSP_FLAGS_WIDTH))
226 
227 /* FSP_SPACE_FLAGS position and name in MySQL 5.6/MariaDB 10.0 or older
228 and MariaDB 10.1.20 or older MariaDB 10.1 and in MariaDB 10.1.21
229 or newer.
230 MySQL 5.6		MariaDB 10.1.x		MariaDB 10.1.21
231 ====================================================================
232 Below flags in same offset
233 ====================================================================
234 0: POST_ANTELOPE	0:POST_ANTELOPE		0: POST_ANTELOPE
235 1..4: ZIP_SSIZE(0..5)	1..4:ZIP_SSIZE(0..5)	1..4: ZIP_SSIZE(0..5)
236 (NOTE: bit 4 is always 0)
237 5: ATOMIC_BLOBS    	5:ATOMIC_BLOBS		5: ATOMIC_BLOBS
238 =====================================================================
239 Below note the order difference:
240 =====================================================================
241 6..9: PAGE_SSIZE(3..7)	6: COMPRESSION		6..9: PAGE_SSIZE(3..7)
242 10: DATA_DIR		7..10: COMP_LEVEL(0..9)	10: RESERVED (5.6 DATA_DIR)
243 =====================================================================
244 The flags below were in incorrect position in MariaDB 10.1,
245 or have been introduced in MySQL 5.7 or 8.0:
246 =====================================================================
247 11: UNUSED		11..12:ATOMIC_WRITES	11: RESERVED (5.7 SHARED)
248 						12: RESERVED (5.7 TEMPORARY)
249 			13..15:PAGE_SSIZE(3..7)	13: RESERVED (5.7 ENCRYPTION)
250 						14: RESERVED (8.0 SDI)
251 						15: RESERVED
252 			16: PAGE_SSIZE_msb(0)	16: COMPRESSION
253 			17: DATA_DIR		17: UNUSED
254 			18: UNUSED
255 =====================================================================
256 The flags below only exist in fil_space_t::flags, not in FSP_SPACE_FLAGS:
257 =====================================================================
258 						27: DATA_DIR
259 						28..31: COMPRESSION_LEVEL
260 */
261 
262 /** A mask of the memory-only flags in fil_space_t::flags */
263 #define FSP_FLAGS_MEM_MASK		(~0U << FSP_FLAGS_MEM_DATA_DIR)
264 
265 /** Zero relative shift position of the DATA_DIR flag */
266 #define FSP_FLAGS_MEM_DATA_DIR		27
267 /** Zero relative shift position of the COMPRESSION_LEVEL field */
268 #define FSP_FLAGS_MEM_COMPRESSION_LEVEL	28
269 
270 /** Zero relative shift position of the POST_ANTELOPE field */
271 #define FSP_FLAGS_POS_POST_ANTELOPE	0
272 /** Zero relative shift position of the ZIP_SSIZE field */
273 #define FSP_FLAGS_POS_ZIP_SSIZE		(FSP_FLAGS_POS_POST_ANTELOPE	\
274 					+ FSP_FLAGS_WIDTH_POST_ANTELOPE)
275 /** Zero relative shift position of the ATOMIC_BLOBS field */
276 #define FSP_FLAGS_POS_ATOMIC_BLOBS	(FSP_FLAGS_POS_ZIP_SSIZE	\
277 					+ FSP_FLAGS_WIDTH_ZIP_SSIZE)
278 /** Zero relative shift position of the start of the PAGE_SSIZE bits */
279 #define FSP_FLAGS_POS_PAGE_SSIZE	(FSP_FLAGS_POS_ATOMIC_BLOBS	\
280                                         + FSP_FLAGS_WIDTH_ATOMIC_BLOBS)
281 /** Zero relative shift position of the start of the RESERVED bits
282 these are only used in MySQL 5.7 and used for compatibility. */
283 #define FSP_FLAGS_POS_RESERVED		(FSP_FLAGS_POS_PAGE_SSIZE	\
284 					+ FSP_FLAGS_WIDTH_PAGE_SSIZE)
285 /** Zero relative shift position of the PAGE_COMPRESSION field */
286 #define FSP_FLAGS_POS_PAGE_COMPRESSION	(FSP_FLAGS_POS_RESERVED \
287 					+ FSP_FLAGS_WIDTH_RESERVED)
288 
289 /** Bit mask of the POST_ANTELOPE field */
290 #define FSP_FLAGS_MASK_POST_ANTELOPE				\
291 		((~(~0U << FSP_FLAGS_WIDTH_POST_ANTELOPE))	\
292 		<< FSP_FLAGS_POS_POST_ANTELOPE)
293 /** Bit mask of the ZIP_SSIZE field */
294 #define FSP_FLAGS_MASK_ZIP_SSIZE				\
295 		((~(~0U << FSP_FLAGS_WIDTH_ZIP_SSIZE))		\
296 		<< FSP_FLAGS_POS_ZIP_SSIZE)
297 /** Bit mask of the ATOMIC_BLOBS field */
298 #define FSP_FLAGS_MASK_ATOMIC_BLOBS				\
299 		((~(~0U << FSP_FLAGS_WIDTH_ATOMIC_BLOBS))	\
300 		<< FSP_FLAGS_POS_ATOMIC_BLOBS)
301 /** Bit mask of the PAGE_SSIZE field */
302 #define FSP_FLAGS_MASK_PAGE_SSIZE				\
303 		((~(~0U << FSP_FLAGS_WIDTH_PAGE_SSIZE))		\
304 		<< FSP_FLAGS_POS_PAGE_SSIZE)
305 /** Bit mask of the RESERVED1 field */
306 #define FSP_FLAGS_MASK_RESERVED					\
307 		((~(~0U << FSP_FLAGS_WIDTH_RESERVED))		\
308 		<< FSP_FLAGS_POS_RESERVED)
309 /** Bit mask of the PAGE_COMPRESSION field */
310 #define FSP_FLAGS_MASK_PAGE_COMPRESSION				\
311 		((~(~0U << FSP_FLAGS_WIDTH_PAGE_COMPRESSION))	\
312 		<< FSP_FLAGS_POS_PAGE_COMPRESSION)
313 
314 /** Bit mask of the in-memory COMPRESSION_LEVEL field */
315 #define FSP_FLAGS_MASK_MEM_COMPRESSION_LEVEL			\
316 		(15U << FSP_FLAGS_MEM_COMPRESSION_LEVEL)
317 
318 /** Return the value of the POST_ANTELOPE field */
319 #define FSP_FLAGS_GET_POST_ANTELOPE(flags)			\
320 		((flags & FSP_FLAGS_MASK_POST_ANTELOPE)		\
321 		>> FSP_FLAGS_POS_POST_ANTELOPE)
322 /** Return the value of the ZIP_SSIZE field */
323 #define FSP_FLAGS_GET_ZIP_SSIZE(flags)				\
324 		((flags & FSP_FLAGS_MASK_ZIP_SSIZE)		\
325 		>> FSP_FLAGS_POS_ZIP_SSIZE)
326 /** Return the value of the ATOMIC_BLOBS field */
327 #define FSP_FLAGS_HAS_ATOMIC_BLOBS(flags)			\
328 		((flags & FSP_FLAGS_MASK_ATOMIC_BLOBS)		\
329 		>> FSP_FLAGS_POS_ATOMIC_BLOBS)
330 /** Return the value of the PAGE_SSIZE field */
331 #define FSP_FLAGS_GET_PAGE_SSIZE(flags)				\
332 		((flags & FSP_FLAGS_MASK_PAGE_SSIZE)		\
333 		>> FSP_FLAGS_POS_PAGE_SSIZE)
334 /** @return the RESERVED flags */
335 #define FSP_FLAGS_GET_RESERVED(flags)				\
336 		((flags & FSP_FLAGS_MASK_RESERVED)		\
337 		>> FSP_FLAGS_POS_RESERVED)
338 /** @return the PAGE_COMPRESSION flag */
339 #define FSP_FLAGS_HAS_PAGE_COMPRESSION(flags)			\
340 		((flags & FSP_FLAGS_MASK_PAGE_COMPRESSION)	\
341 		>> FSP_FLAGS_POS_PAGE_COMPRESSION)
342 
343 /** Return the contents of the UNUSED bits */
344 #define FSP_FLAGS_GET_UNUSED(flags)				\
345 		(flags >> FSP_FLAGS_POS_UNUSED)
346 
347 /** @return the value of the DATA_DIR field */
348 #define FSP_FLAGS_HAS_DATA_DIR(flags)				\
349 	(flags & 1U << FSP_FLAGS_MEM_DATA_DIR)
350 /** @return the COMPRESSION_LEVEL field */
351 #define FSP_FLAGS_GET_PAGE_COMPRESSION_LEVEL(flags)		\
352 	((flags & FSP_FLAGS_MASK_MEM_COMPRESSION_LEVEL)		\
353 	 >> FSP_FLAGS_MEM_COMPRESSION_LEVEL)
354 
355 /* @} */
356 
357 /** Validate the tablespace flags, which are stored in the
358 tablespace header at offset FSP_SPACE_FLAGS.
359 @param[in]	flags	the contents of FSP_SPACE_FLAGS
360 @param[in]	is_ibd	whether this is an .ibd file (not system tablespace)
361 @return	whether the flags are correct (not in the buggy 10.1) format */
362 MY_ATTRIBUTE((warn_unused_result, const))
363 UNIV_INLINE
364 bool
365 fsp_flags_is_valid(ulint flags, bool is_ibd)
366 {
367 	DBUG_EXECUTE_IF("fsp_flags_is_valid_failure",
368 			return(false););
369 	if (flags == 0) {
370 		return(true);
371 	}
372 	if (flags & ~FSP_FLAGS_MASK) {
373 		return(false);
374 	}
375 	if ((flags & (FSP_FLAGS_MASK_POST_ANTELOPE | FSP_FLAGS_MASK_ATOMIC_BLOBS))
376 	    == FSP_FLAGS_MASK_ATOMIC_BLOBS) {
377 		/* If the "atomic blobs" flag (indicating
378 		ROW_FORMAT=DYNAMIC or ROW_FORMAT=COMPRESSED) flag
379 		is set, then the "post Antelope" (ROW_FORMAT!=REDUNDANT) flag
380 		must also be set. */
381 		return(false);
382 	}
383 	/* Bits 10..14 should be 0b0000d where d is the DATA_DIR flag
384 	of MySQL 5.6 and MariaDB 10.0, which we ignore.
385 	In the buggy FSP_SPACE_FLAGS written by MariaDB 10.1.0 to 10.1.20,
386 	bits 10..14 would be nonzero 0bsssaa where sss is
387 	nonzero PAGE_SSIZE (3, 4, 6, or 7)
388 	and aa is ATOMIC_WRITES (not 0b11). */
389 	if (FSP_FLAGS_GET_RESERVED(flags) & ~1U) {
390 		return(false);
391 	}
392 
393 	const ulint	ssize = FSP_FLAGS_GET_PAGE_SSIZE(flags);
394 	if (ssize == 1 || ssize == 2 || ssize == 5 || ssize & 8) {
395 		/* the page_size is not between 4k and 64k;
396 		16k should be encoded as 0, not 5 */
397 		return(false);
398 	}
399 	const ulint	zssize = FSP_FLAGS_GET_ZIP_SSIZE(flags);
400 	if (zssize == 0) {
401 		/* not ROW_FORMAT=COMPRESSED */
402 	} else if (zssize > (ssize ? ssize : 5)) {
403 		/* invalid KEY_BLOCK_SIZE */
404 		return(false);
405 	} else if (~flags & (FSP_FLAGS_MASK_POST_ANTELOPE
406 			     | FSP_FLAGS_MASK_ATOMIC_BLOBS)) {
407 		/* both these flags should be set for
408 		ROW_FORMAT=COMPRESSED */
409 		return(false);
410 	}
411 
412 	/* The flags do look valid. But, avoid misinterpreting
413 	buggy MariaDB 10.1 format flags for
414 	PAGE_COMPRESSED=1 PAGE_COMPRESSION_LEVEL={0,2,3}
415 	as valid-looking PAGE_SSIZE if this is known to be
416 	an .ibd file and we are using the default innodb_page_size=16k. */
417 	return(ssize == 0 || !is_ibd || srv_page_size != UNIV_PAGE_SIZE_ORIG);
418 }
419 
420 #endif /* fsp0types_h */
421