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