1 /*****************************************************************************
2 
3 Copyright (c) 1995, 2021, Oracle and/or its affiliates.
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License, version 2.0,
7 as published by the Free Software Foundation.
8 
9 This program is also distributed with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation.  The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have included with MySQL.
15 
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 GNU General Public License, version 2.0, for more details.
20 
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
24 
25 *****************************************************************************/
26 
27 /******************************************************
28 @file include/fsp0types.h
29 File space management types
30 
31 Created May 26, 2009 Vasil Dimov
32 *******************************************************/
33 
34 #ifndef fsp0types_h
35 #define fsp0types_h
36 
37 #ifndef UNIV_INNOCHECKSUM
38 
39 #include "univ.i"
40 
41 /** @name Flags for inserting records in order
42 If records are inserted in order, there are the following
43 flags to tell this (their type is made byte for the compiler
44 to warn if direction and hint parameters are switched in
45 fseg_alloc_free_page) */
46 /* @{ */
47 #define	FSP_UP		((byte)111)	/*!< alphabetically upwards */
48 #define	FSP_DOWN	((byte)112)	/*!< alphabetically downwards */
49 #define	FSP_NO_DIR	((byte)113)	/*!< no order */
50 /* @} */
51 
52 #endif /* !UNIV_INNOCHECKSUM */
53 /** File space extent size in pages
54 page size | file space extent size
55 ----------+-----------------------
56    4 KiB  | 256 pages = 1 MiB
57    8 KiB  | 128 pages = 1 MiB
58   16 KiB  |  64 pages = 1 MiB
59   32 KiB  |  64 pages = 2 MiB
60   64 KiB  |  64 pages = 4 MiB
61 */
62 #define FSP_EXTENT_SIZE         ((UNIV_PAGE_SIZE <= (16384) ?	\
63 				(1048576 / UNIV_PAGE_SIZE) :	\
64 				((UNIV_PAGE_SIZE <= (32768)) ?	\
65 				(2097152 / UNIV_PAGE_SIZE) :	\
66 				(4194304 / UNIV_PAGE_SIZE))))
67 
68 /** File space extent size (four megabyte) in pages for MAX page size */
69 #define	FSP_EXTENT_SIZE_MAX	(4194304 / UNIV_PAGE_SIZE_MAX)
70 
71 /** File space extent size (one megabyte) in pages for MIN page size */
72 #define	FSP_EXTENT_SIZE_MIN	(1048576 / UNIV_PAGE_SIZE_MIN)
73 
74 /** On a page of any file segment, data may be put starting from this
75 offset */
76 #define FSEG_PAGE_DATA		FIL_PAGE_DATA
77 
78 #ifndef UNIV_INNOCHECKSUM
79 /** @name File segment header
80 The file segment header points to the inode describing the file segment. */
81 /* @{ */
82 /** Data type for file segment header */
83 typedef	byte	fseg_header_t;
84 
85 #define FSEG_HDR_SPACE		0	/*!< space id of the inode */
86 #define FSEG_HDR_PAGE_NO	4	/*!< page number of the inode */
87 #define FSEG_HDR_OFFSET		8	/*!< byte offset of the inode */
88 
89 #define FSEG_HEADER_SIZE	10	/*!< Length of the file system
90 					header, in bytes */
91 /* @} */
92 
93 #ifdef UNIV_DEBUG
94 
95 struct mtr_t;
96 
97 /** A wrapper class to print the file segment header information. */
98 class fseg_header
99 {
100 public:
101 	/** Constructor of fseg_header.
102 	@param[in]	header	the underlying file segment header object
103 	@param[in]	mtr	the mini-transaction.  No redo logs are
104 				generated, only latches are checked within
105 				mini-transaction */
fseg_header(const fseg_header_t * header,mtr_t * mtr)106 	fseg_header(
107 		const fseg_header_t*	header,
108 		mtr_t*			mtr)
109 		:
110 		m_header(header),
111 		m_mtr(mtr)
112 	{}
113 
114 	/** Print the file segment header to the given output stream.
115 	@param[in,out]	out	the output stream into which the object
116 				is printed.
117 	@retval	the output stream into which the object was printed. */
118 	std::ostream&
119 	to_stream(std::ostream&	out) const;
120 private:
121 	/** The underlying file segment header */
122 	const fseg_header_t*	m_header;
123 
124 	/** The mini transaction, which is used mainly to check whether
125 	appropriate latches have been taken by the calling thread. */
126 	mtr_t*			m_mtr;
127 };
128 
129 /* Overloading the global output operator to print a file segment header
130 @param[in,out]	out	the output stream into which object will be printed
131 @param[in]	header	the file segment header to be printed
132 @retval the output stream */
133 inline
134 std::ostream&
135 operator<<(
136 	std::ostream&		out,
137 	const fseg_header&	header)
138 {
139 	return(header.to_stream(out));
140 }
141 #endif /* UNIV_DEBUG */
142 
143 /** Flags for fsp_reserve_free_extents */
144 enum fsp_reserve_t {
145 	FSP_NORMAL,	/* reservation during normal B-tree operations */
146 	FSP_UNDO,	/* reservation done for undo logging */
147 	FSP_CLEANING,	/* reservation done during purge operations */
148 	FSP_BLOB	/* reservation being done for BLOB insertion */
149 };
150 
151 /* Number of pages described in a single descriptor page: currently each page
152 description takes less than 1 byte; a descriptor page is repeated every
153 this many file pages */
154 /* #define XDES_DESCRIBED_PER_PAGE		UNIV_PAGE_SIZE */
155 /* This has been replaced with either UNIV_PAGE_SIZE or page_zip->size. */
156 
157 /** @name The space low address page map
158 The pages at FSP_XDES_OFFSET and FSP_IBUF_BITMAP_OFFSET are repeated
159 every XDES_DESCRIBED_PER_PAGE pages in every tablespace. */
160 /* @{ */
161 /*--------------------------------------*/
162 #define FSP_XDES_OFFSET			0	/* !< extent descriptor */
163 #define FSP_IBUF_BITMAP_OFFSET		1	/* !< insert buffer bitmap */
164 				/* The ibuf bitmap pages are the ones whose
165 				page number is the number above plus a
166 				multiple of XDES_DESCRIBED_PER_PAGE */
167 
168 #define FSP_FIRST_INODE_PAGE_NO		2	/*!< in every tablespace */
169 				/* The following pages exist
170 				in the system tablespace (space 0). */
171 #define FSP_IBUF_HEADER_PAGE_NO		3	/*!< insert buffer
172 						header page, in
173 						tablespace 0 */
174 #define FSP_IBUF_TREE_ROOT_PAGE_NO	4	/*!< insert buffer
175 						B-tree root page in
176 						tablespace 0 */
177 				/* The ibuf tree root page number in
178 				tablespace 0; its fseg inode is on the page
179 				number FSP_FIRST_INODE_PAGE_NO */
180 #define FSP_TRX_SYS_PAGE_NO		5	/*!< transaction
181 						system header, in
182 						tablespace 0 */
183 #define	FSP_FIRST_RSEG_PAGE_NO		6	/*!< first rollback segment
184 						page, in tablespace 0 */
185 #define FSP_DICT_HDR_PAGE_NO		7	/*!< data dictionary header
186 						page, in tablespace 0 */
187 /*--------------------------------------*/
188 /* @} */
189 
190 /** Validate the tablespace flags.
191 These flags are stored in the tablespace header at offset FSP_SPACE_FLAGS.
192 They should be 0 for ROW_FORMAT=COMPACT and ROW_FORMAT=REDUNDANT.
193 The newer row formats, COMPRESSED and DYNAMIC, use a file format > Antelope
194 so they should have a file format number plus the DICT_TF_COMPACT bit set.
195 @param[in]	flags	Tablespace flags
196 @return true if valid, false if not */
197 bool
198 fsp_flags_is_valid(
199 	ulint	flags)
200 	MY_ATTRIBUTE((warn_unused_result, const));
201 
202 /** Check if tablespace is system temporary.
203 @param[in]      space_id        verify is checksum is enabled for given space.
204 @return true if tablespace is system temporary. */
205 bool
206 fsp_is_system_temporary(
207 	ulint	space_id);
208 
209 /** Check if checksum is disabled for the given space.
210 @param[in]	space_id	verify is checksum is enabled for given space.
211 @return true if checksum is disabled for given space. */
212 bool
213 fsp_is_checksum_disabled(
214 	ulint	space_id);
215 
216 /** Check if tablespace is file-per-table.
217 @param[in]	space_id	Tablespace ID
218 @param[in]	fsp_flags	Tablespace Flags
219 @return true if tablespace is file-per-table. */
220 bool
221 fsp_is_file_per_table(
222 	ulint	space_id,
223 	ulint	fsp_flags);
224 
225 #ifdef UNIV_DEBUG
226 /** Skip some of the sanity checks that are time consuming even in debug mode
227 and can affect frequent verification runs that are done to ensure stability of
228 the product.
229 @return true if check should be skipped for given space. */
230 bool
231 fsp_skip_sanity_check(
232 	ulint	space_id);
233 #endif /* UNIV_DEBUG */
234 
235 #endif /* !UNIV_INNOCHECKSUM */
236 
237 /* @defgroup fsp_flags InnoDB Tablespace Flag Constants @{ */
238 
239 /** Width of the POST_ANTELOPE flag */
240 #define FSP_FLAGS_WIDTH_POST_ANTELOPE	1
241 /** Number of flag bits used to indicate the tablespace zip page size */
242 #define FSP_FLAGS_WIDTH_ZIP_SSIZE	4
243 /** Width of the ATOMIC_BLOBS flag.  The ability to break up a long
244 column into an in-record prefix and an externally stored part is available
245 to the two Barracuda row formats COMPRESSED and DYNAMIC. */
246 #define FSP_FLAGS_WIDTH_ATOMIC_BLOBS	1
247 /** Number of flag bits used to indicate the tablespace page size */
248 #define FSP_FLAGS_WIDTH_PAGE_SSIZE	4
249 /** Width of the DATA_DIR flag.  This flag indicates that the tablespace
250 is found in a remote location, not the default data directory. */
251 #define FSP_FLAGS_WIDTH_DATA_DIR	1
252 /** Width of the SHARED flag.  This flag indicates that the tablespace
253 was created with CREATE TABLESPACE and can be shared by multiple tables. */
254 #define FSP_FLAGS_WIDTH_SHARED		1
255 /** Width of the TEMPORARY flag.  This flag indicates that the tablespace
256 is a temporary tablespace and everything in it is temporary, meaning that
257 it is for a single client and should be deleted upon startup if it exists. */
258 #define FSP_FLAGS_WIDTH_TEMPORARY	1
259 /** Width of the encryption flag.  This flag indicates that the tablespace
260 is a tablespace with encryption. */
261 #define FSP_FLAGS_WIDTH_ENCRYPTION	1
262 /** Width of all the currently known tablespace flags */
263 #define FSP_FLAGS_WIDTH		(FSP_FLAGS_WIDTH_POST_ANTELOPE	\
264 				+ FSP_FLAGS_WIDTH_ZIP_SSIZE	\
265 				+ FSP_FLAGS_WIDTH_ATOMIC_BLOBS	\
266 				+ FSP_FLAGS_WIDTH_PAGE_SSIZE	\
267 				+ FSP_FLAGS_WIDTH_DATA_DIR	\
268 				+ FSP_FLAGS_WIDTH_SHARED	\
269 				+ FSP_FLAGS_WIDTH_TEMPORARY	\
270 				+ FSP_FLAGS_WIDTH_ENCRYPTION)
271 
272 /** A mask of all the known/used bits in tablespace flags */
273 #define FSP_FLAGS_MASK		(~(~0 << FSP_FLAGS_WIDTH))
274 
275 /** Zero relative shift position of the POST_ANTELOPE field */
276 #define FSP_FLAGS_POS_POST_ANTELOPE	0
277 /** Zero relative shift position of the ZIP_SSIZE field */
278 #define FSP_FLAGS_POS_ZIP_SSIZE		(FSP_FLAGS_POS_POST_ANTELOPE	\
279 					+ FSP_FLAGS_WIDTH_POST_ANTELOPE)
280 /** Zero relative shift position of the ATOMIC_BLOBS field */
281 #define FSP_FLAGS_POS_ATOMIC_BLOBS	(FSP_FLAGS_POS_ZIP_SSIZE	\
282 					+ FSP_FLAGS_WIDTH_ZIP_SSIZE)
283 /** Zero relative shift position of the PAGE_SSIZE field */
284 #define FSP_FLAGS_POS_PAGE_SSIZE	(FSP_FLAGS_POS_ATOMIC_BLOBS	\
285 					+ FSP_FLAGS_WIDTH_ATOMIC_BLOBS)
286 /** Zero relative shift position of the start of the DATA_DIR bit */
287 #define FSP_FLAGS_POS_DATA_DIR		(FSP_FLAGS_POS_PAGE_SSIZE	\
288 					+ FSP_FLAGS_WIDTH_PAGE_SSIZE)
289 /** Zero relative shift position of the start of the SHARED bit */
290 #define FSP_FLAGS_POS_SHARED		(FSP_FLAGS_POS_DATA_DIR		\
291 					+ FSP_FLAGS_WIDTH_DATA_DIR)
292 /** Zero relative shift position of the start of the TEMPORARY bit */
293 #define FSP_FLAGS_POS_TEMPORARY		(FSP_FLAGS_POS_SHARED		\
294 					+ FSP_FLAGS_WIDTH_SHARED)
295 /** Zero relative shift position of the start of the ENCRYPTION bit */
296 #define FSP_FLAGS_POS_ENCRYPTION	(FSP_FLAGS_POS_TEMPORARY	\
297 					+ FSP_FLAGS_WIDTH_TEMPORARY)
298 /** Zero relative shift position of the start of the UNUSED bits */
299 #define FSP_FLAGS_POS_UNUSED		(FSP_FLAGS_POS_ENCRYPTION	\
300 					+ FSP_FLAGS_WIDTH_ENCRYPTION)
301 
302 /** Bit mask of the POST_ANTELOPE field */
303 #define FSP_FLAGS_MASK_POST_ANTELOPE				\
304 		((~(~0U << FSP_FLAGS_WIDTH_POST_ANTELOPE))	\
305 		<< FSP_FLAGS_POS_POST_ANTELOPE)
306 /** Bit mask of the ZIP_SSIZE field */
307 #define FSP_FLAGS_MASK_ZIP_SSIZE				\
308 		((~(~0U << FSP_FLAGS_WIDTH_ZIP_SSIZE))		\
309 		<< FSP_FLAGS_POS_ZIP_SSIZE)
310 /** Bit mask of the ATOMIC_BLOBS field */
311 #define FSP_FLAGS_MASK_ATOMIC_BLOBS				\
312 		((~(~0U << FSP_FLAGS_WIDTH_ATOMIC_BLOBS))	\
313 		<< FSP_FLAGS_POS_ATOMIC_BLOBS)
314 /** Bit mask of the PAGE_SSIZE field */
315 #define FSP_FLAGS_MASK_PAGE_SSIZE				\
316 		((~(~0U << FSP_FLAGS_WIDTH_PAGE_SSIZE))		\
317 		<< FSP_FLAGS_POS_PAGE_SSIZE)
318 /** Bit mask of the DATA_DIR field */
319 #define FSP_FLAGS_MASK_DATA_DIR					\
320 		((~(~0U << FSP_FLAGS_WIDTH_DATA_DIR))		\
321 		<< FSP_FLAGS_POS_DATA_DIR)
322 /** Bit mask of the SHARED field */
323 #define FSP_FLAGS_MASK_SHARED					\
324 		((~(~0U << FSP_FLAGS_WIDTH_SHARED))		\
325 		<< FSP_FLAGS_POS_SHARED)
326 /** Bit mask of the TEMPORARY field */
327 #define FSP_FLAGS_MASK_TEMPORARY				\
328 		((~(~0U << FSP_FLAGS_WIDTH_TEMPORARY))		\
329 		<< FSP_FLAGS_POS_TEMPORARY)
330 /** Bit mask of the ENCRYPTION field */
331 #define FSP_FLAGS_MASK_ENCRYPTION				\
332 		((~(~0U << FSP_FLAGS_WIDTH_ENCRYPTION))		\
333 		<< FSP_FLAGS_POS_ENCRYPTION)
334 
335 /** Return the value of the POST_ANTELOPE field */
336 #define FSP_FLAGS_GET_POST_ANTELOPE(flags)			\
337 		((flags & FSP_FLAGS_MASK_POST_ANTELOPE)		\
338 		>> FSP_FLAGS_POS_POST_ANTELOPE)
339 /** Return the value of the ZIP_SSIZE field */
340 #define FSP_FLAGS_GET_ZIP_SSIZE(flags)				\
341 		((flags & FSP_FLAGS_MASK_ZIP_SSIZE)		\
342 		>> FSP_FLAGS_POS_ZIP_SSIZE)
343 /** Return the value of the ATOMIC_BLOBS field */
344 #define FSP_FLAGS_HAS_ATOMIC_BLOBS(flags)			\
345 		((flags & FSP_FLAGS_MASK_ATOMIC_BLOBS)		\
346 		>> FSP_FLAGS_POS_ATOMIC_BLOBS)
347 /** Return the value of the PAGE_SSIZE field */
348 #define FSP_FLAGS_GET_PAGE_SSIZE(flags)				\
349 		((flags & FSP_FLAGS_MASK_PAGE_SSIZE)		\
350 		>> FSP_FLAGS_POS_PAGE_SSIZE)
351 /** Return the value of the DATA_DIR field */
352 #define FSP_FLAGS_HAS_DATA_DIR(flags)				\
353 		((flags & FSP_FLAGS_MASK_DATA_DIR)		\
354 		>> FSP_FLAGS_POS_DATA_DIR)
355 /** Return the contents of the SHARED field */
356 #define FSP_FLAGS_GET_SHARED(flags)				\
357 		((flags & FSP_FLAGS_MASK_SHARED)		\
358 		>> FSP_FLAGS_POS_SHARED)
359 /** Return the contents of the TEMPORARY field */
360 #define FSP_FLAGS_GET_TEMPORARY(flags)				\
361 		((flags & FSP_FLAGS_MASK_TEMPORARY)		\
362 		>> FSP_FLAGS_POS_TEMPORARY)
363 /** Return the contents of the ENCRYPTION field */
364 #define FSP_FLAGS_GET_ENCRYPTION(flags)				\
365 		((flags & FSP_FLAGS_MASK_ENCRYPTION)		\
366 		>> FSP_FLAGS_POS_ENCRYPTION)
367 /** Return the contents of the UNUSED bits */
368 #define FSP_FLAGS_GET_UNUSED(flags)				\
369 		(flags >> FSP_FLAGS_POS_UNUSED)
370 
371 /** Use an alias in the code for FSP_FLAGS_GET_SHARED() */
372 #define fsp_is_shared_tablespace FSP_FLAGS_GET_SHARED
373 /* @} */
374 
375 #endif /* fsp0types_h */
376