Debian Codegraph @

storage/innobase/include/fsp0fsp.ic

raw
1 /*****************************************************************************
2
3 Copyright (c) 1995, 2026, Oracle and/or its affiliates.
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 designed to work 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 either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20 for more details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 *****************************************************************************/
27
28 /** @file include/fsp0fsp.ic
29 File space management
30
31 Created 12/18/1995 Heikki Tuuri
32 *******************************************************/
33
34 /** Checks if a page address is an extent descriptor page address.
35 @param[in] page_id page id
36 @param[in] page_size page size
37 @return true if a descriptor page */
38 static inline bool fsp_descr_page(const page_id_t &page_id,
39 const page_size_t &page_size) {
40 return ((page_id.page_no() & (page_size.physical() - 1)) == FSP_XDES_OFFSET);
41 }
42
43 /** Determine if the tablespace is compressed from tablespace flags.
44 @param[in] flags Tablespace flags
45 @return true if compressed, false if not compressed */
46 static inline bool fsp_flags_is_compressed(uint32_t flags) {
47 return (FSP_FLAGS_GET_ZIP_SSIZE(flags) != 0);
48 }
49
50 constexpr uint32_t ACTUAL_SSIZE(uint32_t ssize) {
51 if (ssize == 0) {
52 return UNIV_PAGE_SSIZE_ORIG;
53 }
54 return ssize;
55 }
56
57 /** Determine if two tablespaces are equivalent or compatible.
58 @param[in] flags1 First tablespace flags
59 @param[in] flags2 Second tablespace flags
60 @return true the flags are compatible, false if not */
61 static inline bool fsp_flags_are_equal(uint32_t flags1, uint32_t flags2) {
62 /* If either one of these flags is UINT32_UNDEFINED,
63 then they are not equal */
64 if (flags1 == UINT32_UNDEFINED || flags2 == UINT32_UNDEFINED) {
65 return (false);
66 }
67
68 if (!fsp_is_shared_tablespace(flags1) || !fsp_is_shared_tablespace(flags2)) {
69 /* At least one of these is a single-table tablespaces so all
70 flags must match. */
71 return (flags1 == flags2);
72 }
73
74 /* Both are shared tablespaces which can contain all formats.
75 But they must have the same logical and physical page size.
76 Once InnoDB can support multiple page sizes together,
77 the logical page size will not matter. */
78 ulint zip_ssize1 = ACTUAL_SSIZE(FSP_FLAGS_GET_ZIP_SSIZE(flags1));
79 ulint zip_ssize2 = ACTUAL_SSIZE(FSP_FLAGS_GET_ZIP_SSIZE(flags2));
80 ulint page_ssize1 = ACTUAL_SSIZE(FSP_FLAGS_GET_PAGE_SSIZE(flags1));
81 ulint page_ssize2 = ACTUAL_SSIZE(FSP_FLAGS_GET_PAGE_SSIZE(flags2));
82
83 return (zip_ssize1 == zip_ssize2 && page_ssize1 == page_ssize2);
84 }
85
86 /** Convert a page size, which is a power of 2, to an ssize, which is
87 the number of bit shifts from 512 to make that page size.
88 @param[in] page_size compressed page size in bytes
89 @return an ssize created from the page size provided. */
90 static inline uint32_t page_size_to_ssize(ulint page_size) {
91 uint32_t ssize;
92
93 for (ssize = UNIV_ZIP_SIZE_SHIFT_MIN;
94 static_cast<uint32_t>(1 << ssize) < page_size; ssize++) {
95 }
96
97 return (ssize - UNIV_ZIP_SIZE_SHIFT_MIN + 1);
98 }
99
100 /** Add the compressed page size to the tablespace flags.
101 @param[in] flags Tablespace flags
102 @param[in] page_size page sizes in bytes and compression flag.
103 @return tablespace flags after zip size is added */
104 static inline uint32_t fsp_flags_set_zip_size(uint32_t flags,
105 const page_size_t &page_size) {
106 if (!page_size.is_compressed()) {
107 return (flags);
108 }
109
110 /* Zip size should be a power of 2 between UNIV_ZIP_SIZE_MIN
111 and UNIV_ZIP_SIZE_MAX */
112 ut_ad(page_size.physical() >= UNIV_ZIP_SIZE_MIN);
113 ut_ad(page_size.physical() <= UNIV_ZIP_SIZE_MAX);
114 ut_ad(ut_is_2pow(page_size.physical()));
115
116 uint32_t ssize = page_size_to_ssize(page_size.physical());
117
118 ut_ad(ssize > 0);
119 ut_ad(ssize <= UNIV_PAGE_SSIZE_MAX);
120
121 flags |= (ssize << FSP_FLAGS_POS_ZIP_SSIZE);
122
123 ut_ad(fsp_flags_is_valid(flags));
124
125 return (flags);
126 }
127
128 /** Add the page size to the tablespace flags.
129 @param[in] flags Tablespace flags
130 @param[in] page_size page sizes in bytes and compression flag.
131 @return tablespace flags after page size is added */
132 static inline uint32_t fsp_flags_set_page_size(uint32_t flags,
133 const page_size_t &page_size) {
134 /* Page size should be a power of two between UNIV_PAGE_SIZE_MIN
135 and UNIV_PAGE_SIZE */
136 ut_ad(page_size.logical() >= UNIV_PAGE_SIZE_MIN);
137 ut_ad(page_size.logical() <= UNIV_PAGE_SIZE_MAX);
138 ut_ad(ut_is_2pow(page_size.logical()));
139
140 /* Remove this assert once we add support for different
141 page size per tablespace. Currently all tablespaces must
142 have a page size that is equal to innodb-page-size */
143 ut_ad(page_size.logical() == UNIV_PAGE_SIZE);
144
145 if (page_size.logical() == UNIV_PAGE_SIZE_ORIG) {
146 ut_ad(0 == FSP_FLAGS_GET_PAGE_SSIZE(flags));
147
148 } else {
149 uint32_t ssize = page_size_to_ssize(page_size.logical());
150
151 ut_ad(ssize);
152 ut_ad(ssize <= UNIV_PAGE_SSIZE_MAX);
153
154 flags |= (ssize << FSP_FLAGS_POS_PAGE_SSIZE);
155 }
156
157 ut_ad(fsp_flags_is_valid(flags));
158
159 return (flags);
160 }
161
162 /** Initialize an FSP flags integer.
163 @param[in] page_size page sizes in bytes and compression flag.
164 @param[in] atomic_blobs Used by Dynammic and Compressed.
165 @param[in] has_data_dir This tablespace is in a remote location.
166 @param[in] is_shared This tablespace can be shared by many tables.
167 @param[in] is_temporary This tablespace is temporary.
168 @param[in] is_encrypted This tablespace is encrypted.
169 @return tablespace flags after initialization */
170 static inline uint32_t fsp_flags_init(const page_size_t &page_size,
171 bool atomic_blobs, bool has_data_dir,
172 bool is_shared, bool is_temporary,
173 bool is_encrypted) {
174 ut_ad(page_size.physical() <= page_size.logical());
175 ut_ad(!page_size.is_compressed() || atomic_blobs);
176
177 /* Page size should be a power of two between UNIV_PAGE_SIZE_MIN
178 and UNIV_PAGE_SIZE, but zip_size may be 0 if not compressed. */
179 uint32_t flags = fsp_flags_set_page_size(0, page_size);
180
181 if (atomic_blobs) {
182 flags |= FSP_FLAGS_MASK_POST_ANTELOPE | FSP_FLAGS_MASK_ATOMIC_BLOBS;
183 }
184
185 /* If the zip_size is explicit and different from the default,
186 compressed row format is implied. */
187 flags = fsp_flags_set_zip_size(flags, page_size);
188
189 if (has_data_dir) {
190 flags |= FSP_FLAGS_MASK_DATA_DIR;
191 }
192
193 /* Shared tablespaces can hold all row formats, so we only mark the
194 POST_ANTELOPE and ATOMIC_BLOB bits if it is compressed. */
195 if (is_shared) {
196 ut_ad(!has_data_dir);
197 flags |= FSP_FLAGS_MASK_SHARED;
198 }
199
200 if (is_temporary) {
201 ut_ad(!has_data_dir);
202 flags |= FSP_FLAGS_MASK_TEMPORARY;
203 }
204
205 if (is_encrypted) {
206 flags |= FSP_FLAGS_MASK_ENCRYPTION;
207 }
208
209 return (flags);
210 }
211
212 /** Calculates the descriptor index within a descriptor page.
213 @param[in] page_size page size
214 @param[in] offset page offset
215 @return descriptor index */
216 static inline ulint xdes_calc_descriptor_index(const page_size_t &page_size,
217 ulint offset) {
218 return (ut_2pow_remainder(offset, page_size.physical()) / FSP_EXTENT_SIZE);
219 }
220
221 /** Gets a descriptor bit of a page.
222 @return true if free */
223 static inline bool xdes_get_bit(
224 const xdes_t *descr, /*!< in: descriptor */
225 ulint bit, /*!< in: XDES_FREE_BIT or XDES_CLEAN_BIT */
226 page_no_t offset) /*!< in: page offset within extent:
227 0 ... FSP_EXTENT_SIZE - 1 */
228 {
229 ut_ad(offset < FSP_EXTENT_SIZE);
230 ut_ad(bit == XDES_FREE_BIT || bit == XDES_CLEAN_BIT);
231
232 ulint index = bit + XDES_BITS_PER_PAGE * offset;
233
234 ulint bit_index = index % 8;
235 ulint byte_index = index / 8;
236
237 return (ut_bit_get_nth(
238 mach_read_ulint(descr + XDES_BITMAP + byte_index, MLOG_1BYTE),
239 bit_index));
240 }
241
242 /** Calculates the page where the descriptor of a page resides.
243 @param[in] page_size page size
244 @param[in] offset page offset
245 @return descriptor page offset */
246 static inline page_no_t xdes_calc_descriptor_page(const page_size_t &page_size,
247 page_no_t offset) {
248 static_assert(UNIV_PAGE_SIZE_MAX > XDES_ARR_OFFSET + (UNIV_PAGE_SIZE_MAX /
249 FSP_EXTENT_SIZE_MAX) *
250 XDES_SIZE_MAX,
251 "Extent descriptor won't fit on a page");
252
253 static_assert(UNIV_ZIP_SIZE_MIN > XDES_ARR_OFFSET + (UNIV_ZIP_SIZE_MIN /
254 FSP_EXTENT_SIZE_MIN) *
255 XDES_SIZE_MIN,
256 "Extent descriptor won't fit on a page");
257
258 ut_ad(UNIV_PAGE_SIZE >
259 XDES_ARR_OFFSET + (UNIV_PAGE_SIZE / FSP_EXTENT_SIZE) * XDES_SIZE);
260 ut_ad(UNIV_ZIP_SIZE_MIN >
261 XDES_ARR_OFFSET + (UNIV_ZIP_SIZE_MIN / FSP_EXTENT_SIZE) * XDES_SIZE);
262
263 #ifdef UNIV_DEBUG
264 if (page_size.is_compressed()) {
265 ut_a(page_size.physical() >
266 XDES_ARR_OFFSET +
267 (page_size.physical() / FSP_EXTENT_SIZE) * XDES_SIZE);
268 }
269 #endif /* UNIV_DEBUG */
270
271 return (ut_2pow_round(offset, page_size.physical()));
272 }
273
274 /** Calculates the descriptor array size.
275 @param[in] page_size page size
276 @return size of descriptor array */
277 static inline ulint xdes_arr_size(const page_size_t &page_size) {
278 return (page_size.physical() / FSP_EXTENT_SIZE);
279 }
280
281 inline bool fsp_is_inode_page(page_no_t page) {
282 static const uint inode_per_page = FSP_SEG_INODES_PER_PAGE(univ_page_size);
283
284 /* Every two inode would be allocated for one index, and all inodes in
285 two inode pages are needed exactly for FSP_SEG_INODES_PER_PAGE indexes.
286 One inode page is at the beginning of one cycle, the other is in the
287 middle. If FSP_SEG_INODES_PER_PAGE is even, the second inode page
288 is at cycle / 2, if odd, it should be (cycle + 1) / 2 in the cycle. */
289 static const uint cycle = inode_per_page + 2;
290
291 /* Number of all hard-coded DD table indexes. Please sync it with
292 innodb_dd_table array. */
293 static const uint indexes = 102;
294
295 /* Max page number for index root pages of hard-coded DD tables. */
296 static const uint max_page_no =
297 FSP_FIRST_INODE_PAGE_NO + 1 /* SDI Index page */
298 + (indexes / inode_per_page) * cycle + (indexes % inode_per_page) +
299 ((indexes % inode_per_page) / ((inode_per_page + 1) / 2));
300
301 /* The page range should be determinate for different page sizes. */
302 ut_a(page >= FSP_FIRST_INODE_PAGE_NO);
303 ut_a(page <= max_page_no);
304
305 uint step = (page - FSP_FIRST_INODE_PAGE_NO) % cycle;
306
307 return (step == 0 || step == (cycle + 1) / 2);
308 }
309
310 /** Validate the tablespace flags.
311 These flags are stored in the tablespace header at offset FSP_SPACE_FLAGS.
312 They should be 0 for ROW_FORMAT=COMPACT and ROW_FORMAT=REDUNDANT.
313 The newer row formats, COMPRESSED and DYNAMIC, will have at least
314 the DICT_TF_COMPACT bit set.
315 @param[in] flags Tablespace flags
316 @return true if valid, false if not */
317 inline bool fsp_flags_is_valid(uint32_t flags) {
318 bool post_antelope = FSP_FLAGS_GET_POST_ANTELOPE(flags);
319 ulint zip_ssize = FSP_FLAGS_GET_ZIP_SSIZE(flags);
320 bool atomic_blobs = FSP_FLAGS_HAS_ATOMIC_BLOBS(flags);
321 ulint page_ssize = FSP_FLAGS_GET_PAGE_SSIZE(flags);
322 bool has_data_dir = FSP_FLAGS_HAS_DATA_DIR(flags);
323 bool is_shared = FSP_FLAGS_GET_SHARED(flags);
324 bool is_temp = FSP_FLAGS_GET_TEMPORARY(flags);
325 bool is_encryption = FSP_FLAGS_GET_ENCRYPTION(flags);
326
327 ulint unused = FSP_FLAGS_GET_UNUSED(flags);
328
329 DBUG_EXECUTE_IF("fsp_flags_is_valid_failure", return (false););
330
331 /* The Antelope row formats REDUNDANT and COMPACT did
332 not use tablespace flags, so the entire 4-byte field
333 is zero for Antelope row formats. */
334 if (flags == 0) {
335 return (true);
336 }
337
338 /* Row_FORMAT=COMPRESSED and ROW_FORMAT=DYNAMIC use a feature called
339 ATOMIC_BLOBS which builds on the page structure introduced for the
340 COMPACT row format by allowing long fields to be broken into prefix
341 and externally stored parts. So if it is Post_antelope, it uses
342 Atomic BLOBs. */
343 if (post_antelope != atomic_blobs) {
344 return (false);
345 }
346
347 /* Make sure there are no bits that we do not know about. */
348 if (unused != 0) {
349 return (false);
350 }
351
352 /* The zip ssize can be zero if it is other than compressed row format,
353 or it could be from 1 to the max. */
354 if (zip_ssize > PAGE_ZIP_SSIZE_MAX) {
355 return (false);
356 }
357
358 /* The actual page size must be within 4k and 64K (3 =< ssize =< 7). */
359 if (page_ssize != 0 &&
360 (page_ssize < UNIV_PAGE_SSIZE_MIN || page_ssize > UNIV_PAGE_SSIZE_MAX)) {
361 return (false);
362 }
363
364 /* Only single-table tablespaces use the DATA DIRECTORY clause.
365 It is not compatible with the TABLESPACE clause. Nor is it
366 compatible with the TEMPORARY clause. */
367 if (has_data_dir && (is_shared || is_temp)) {
368 return (false);
369 }
370
371 /* Only single-table and general tablespaces and not temp tablespaces
372 use the encryption clause. */
373 if (is_encryption && (is_temp)) {
374 return (false);
375 }
376
377 static_assert(
378 FSP_FLAGS_POS_UNUSED == 15,
379 "You have added a new FSP_FLAG without adding a validation check.");
380
381 return (true);
382 }
383
384 inline ulint fsp_header_get_sdi_offset(const page_size_t &page_size) {
385 ulint offset = XDES_ARR_OFFSET + XDES_SIZE * xdes_arr_size(page_size) +
386 Encryption::INFO_MAX_SIZE;
387 #ifdef UNIV_DEBUG
388 ulint left_size =
389 page_size.physical() - FSP_HEADER_OFFSET - offset - FIL_PAGE_DATA_END;
390
391 ut_ad(left_size >= FSP_SDI_HEADER_LEN);
392 #endif
393 return (offset);
394 }
395
396 inline ulint fsp_header_get_encryption_progress_offset(
397 const page_size_t &page_size) {
398 ulint offset = fsp_header_get_sdi_offset(page_size) + FSP_SDI_HEADER_LEN;
399 #ifdef UNIV_DEBUG
400 ulint left_size =
401 page_size.physical() - FSP_HEADER_OFFSET - offset - FIL_PAGE_DATA_END;
402
403 ut_ad(left_size >=
404 Encryption::OPERATION_INFO_SIZE + Encryption::PROGRESS_INFO_SIZE);
405 #endif
406
407 return (offset);
408 }
409
410 /** Reads the server space version from the first page of a tablespace.
411 @param[in] page first page of a tablespace
412 @return space server version */
413 static inline uint32_t fsp_header_get_server_version(const page_t *page) {
414 uint32_t version;
415
416 version = mach_read_from_4(page + FIL_PAGE_SRV_VERSION);
417
418 return (version);
419 }
420
421 /** Reads the server space version from the first page of a tablespace.
422 @param[in] page first page of a tablespace
423 @return space server version */
424 static inline uint32_t fsp_header_get_space_version(const page_t *page) {
425 uint32_t version;
426
427 version = mach_read_from_4(page + FIL_PAGE_SPACE_VERSION);
428
429 return (version);
430 }