Debian Codegraph @

storage/innobase/include/fsp0fsp.h

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.h
29 File space management
30
31 Created 12/18/1995 Heikki Tuuri
32 *******************************************************/
33
34 #ifndef fsp0fsp_h
35 #define fsp0fsp_h
36
37 #include "univ.i"
38
39 #include "fsp0space.h"
40 #include "fut0lst.h"
41 #include "mtr0mtr.h"
42 #include "mysql/components/services/bits/mysql_cond_bits.h"
43 #include "mysql/components/services/bits/mysql_mutex_bits.h"
44 #include "page0types.h"
45 #include "rem0types.h"
46 #include "ut0byte.h"
47
48 #include <random>
49 #include "fsp0types.h"
50
51 #ifdef UNIV_HOTBACKUP
52 #include "buf0buf.h"
53 #endif /* UNIV_HOTBACKUP */
54
55 class DDL_Record;
56 extern std::vector<DDL_Record *> ts_encrypt_ddl_records;
57 extern mysql_cond_t resume_encryption_cond;
58 extern mysql_mutex_t resume_encryption_cond_m;
59
60 /** @defgroup Tablespace Header Constants (moved from fsp0fsp.c)
61 @{ */
62 #ifdef UNIV_DEBUG
63 extern std::vector<space_id_t> flag_mismatch_spaces;
64 #endif
65
66 /** Offset of the space header within a file page */
67 constexpr uint32_t FSP_HEADER_OFFSET = FIL_PAGE_DATA;
68
69 /** The number of bytes required to store SDI root page number(4)
70 and SDI version(4) at Page 0 */
71 constexpr uint32_t FSP_SDI_HEADER_LEN = 8;
72
73 /* The data structures in files are defined just as byte strings in C */
74 typedef byte fsp_header_t;
75 typedef byte xdes_t;
76
77 #ifdef UNIV_DEBUG
78 /** Check if the state of extent descriptor is valid.
79 @param[in] state the extent descriptor state
80 @return true if state is valid, false otherwise */
81 bool xdes_state_is_valid(ulint state);
82 #endif /* UNIV_DEBUG */
83
84 #ifdef UNIV_DEBUG
85 struct xdes_mem_t {
86 xdes_mem_t(const xdes_t *xdes) : m_xdes(xdes) {}
87
88 const char *state_name() const;
89
90 bool is_valid() const;
91 const xdes_t *m_xdes;
92
93 std::ostream &print(std::ostream &out) const;
94 };
95
96 inline std::ostream &operator<<(std::ostream &out, const xdes_mem_t &obj) {
97 return (obj.print(out));
98 }
99
100 /** In-memory representation of the fsp_header_t file structure. */
101 struct fsp_header_mem_t {
102 fsp_header_mem_t(const fsp_header_t *header, mtr_t *mtr);
103
104 ulint m_space_id;
105 ulint m_notused;
106 ulint m_fsp_size;
107 ulint m_free_limit;
108 ulint m_flags;
109 ulint m_fsp_frag_n_used;
110 flst_bnode_t m_fsp_free;
111 flst_bnode_t m_free_frag;
112 flst_bnode_t m_full_frag;
113 ib_id_t m_segid;
114 flst_bnode_t m_inodes_full;
115 flst_bnode_t m_inodes_free;
116
117 std::ostream &print(std::ostream &out) const;
118 };
119
120 inline std::ostream &operator<<(std::ostream &out,
121 const fsp_header_mem_t &obj) {
122 return (obj.print(out));
123 }
124 #endif /* UNIV_DEBUG */
125
126 /* SPACE HEADER
127 ============
128
129 File space header data structure: this data structure is contained in the
130 first page of a space. The space for this header is reserved in every extent
131 descriptor page, but used only in the first. */
132
133 /*-------------------------------------*/
134 /** space id */
135 constexpr uint32_t FSP_SPACE_ID = 0;
136 /** this field contained a value up to which we know that the modifications in
137 the database have been flushed to the file space; not used now */
138 constexpr uint32_t FSP_NOT_USED = 4;
139 /** Current size of the space in pages */
140 constexpr uint32_t FSP_SIZE = 8;
141 /** Minimum page number for which the free list has not been initialized: the
142 pages >= this limit are, bydefinition, free; note that in a single-table
143 tablespace where size < 64 pages, this number is 64, i.e.,we have initialized
144 the space about the first extent, but have not physically allocated those
145 pages to thefile */
146 constexpr uint32_t FSP_FREE_LIMIT = 12;
147 /** fsp_space_t.flags, similar to dict_table_t::flags */
148 constexpr uint32_t FSP_SPACE_FLAGS = 16;
149 /** number of used pages in the FSP_FREE_FRAG list */
150 constexpr uint32_t FSP_FRAG_N_USED = 20;
151 /** list of free extents */
152 constexpr uint32_t FSP_FREE = 24;
153 /** list of partially free extents not belonging to any segment */
154 constexpr uint32_t FSP_FREE_FRAG = 24 + FLST_BASE_NODE_SIZE;
155
156 /** list of full extents not belonging to any segment */
157 constexpr uint32_t FSP_FULL_FRAG = 24 + 2 * FLST_BASE_NODE_SIZE;
158
159 /** 8 bytes which give the first unused segment id */
160 constexpr uint32_t FSP_SEG_ID = 24 + 3 * FLST_BASE_NODE_SIZE;
161
162 /** list of pages containing segment headers, where all the segment inode slots
163 are reserved */
164 constexpr uint32_t FSP_SEG_INODES_FULL = 32 + 3 * FLST_BASE_NODE_SIZE;
165
166 /** list of pages containing segment headers, where not all the segment header
167 slots are reserved */
168 constexpr uint32_t FSP_SEG_INODES_FREE = 32 + 4 * FLST_BASE_NODE_SIZE;
169
170 /*-------------------------------------*/
171 /* File space header size */
172 constexpr uint32_t FSP_HEADER_SIZE = 32 + 5 * FLST_BASE_NODE_SIZE;
173
174 /** This many free extents are added to the free list from above FSP_FREE_LIMIT
175 at a time */
176 constexpr uint32_t FSP_FREE_ADD = 4;
177
178 /** @} */
179
180 /* Maximum allowed value of AUTOEXTEND_SIZE attribute */
181 constexpr uint64_t FSP_MAX_AUTOEXTEND_SIZE = 4 * 1024 * 1024 * 1024ULL;
182
183 /** @defgroup File Segment Inode Constants (moved from fsp0fsp.c)
184 @{ */
185
186 /* FILE SEGMENT INODE
187 ==================
188
189 Segment inode which is created for each segment in a tablespace. NOTE: in
190 purge we assume that a segment having only one currently used page can be
191 freed in a few steps, so that the freeing cannot fill the file buffer with
192 bufferfixed file pages. */
193
194 typedef byte fseg_inode_t;
195
196 constexpr uint32_t FSEG_INODE_PAGE_NODE = FSEG_PAGE_DATA;
197 /* the list node for linking
198 segment inode pages */
199
200 constexpr uint32_t FSEG_ARR_OFFSET = FSEG_PAGE_DATA + FLST_NODE_SIZE;
201 /*-------------------------------------*/
202 /* 8 bytes of segment id: if this is 0, it means that the header is unused */
203 constexpr uint32_t FSEG_ID = 0;
204 /** number of used segment pages in the FSEG_NOT_FULL list */
205 constexpr uint32_t FSEG_NOT_FULL_N_USED = 8;
206 /** list of free extents of this segment */
207 constexpr uint32_t FSEG_FREE = 12;
208 /** list of partially free extents */
209 constexpr uint32_t FSEG_NOT_FULL = 12 + FLST_BASE_NODE_SIZE;
210 /** list of full extents */
211 constexpr uint32_t FSEG_FULL = 12 + 2 * FLST_BASE_NODE_SIZE;
212 /** magic number used in debugging */
213 constexpr uint32_t FSEG_MAGIC_N = 12 + 3 * FLST_BASE_NODE_SIZE;
214 /** array of individual pages belonging to this segment in fsp fragment extent
215 lists */
216 constexpr uint32_t FSEG_FRAG_ARR = 16 + 3 * FLST_BASE_NODE_SIZE;
217 /* number of slots in the array for the fragment pages */
218 #define FSEG_FRAG_ARR_N_SLOTS (FSP_EXTENT_SIZE / 2)
219 /** a fragment page slot contains its page number within space, FIL_NULL means
220 that the slot is not in use */
221 constexpr uint32_t FSEG_FRAG_SLOT_SIZE = 4;
222
223 /*-------------------------------------*/
224 #define FSEG_INODE_SIZE \
225 (16 + 3 * FLST_BASE_NODE_SIZE + FSEG_FRAG_ARR_N_SLOTS * FSEG_FRAG_SLOT_SIZE)
226
227 static inline uint32_t FSP_SEG_INODES_PER_PAGE(page_size_t page_size) {
228 return (page_size.physical() - FSEG_ARR_OFFSET - 10) / FSEG_INODE_SIZE;
229 }
230 /* Number of segment inodes which fit on a
231 single page */
232
233 constexpr uint32_t FSEG_MAGIC_N_VALUE = 97937874;
234
235 /** The segment_reserve_factor is the ratio x/y expressed in percentage,
236 where x is the number of free pages in the segment, and y is the total number
237 of pages in the segment. The number of used pages in the segment is given by
238 (y-x). The number of free pages in the segment (x) will be maintained such
239 that the actual segment_reserve_factor will be >= the requested
240 segment_reserve_factor, which is contained in this variable. */
241 extern double fseg_reserve_pct;
242
243 /* Various constants related to segment reserve factor */
244 constexpr double FSEG_RESERVE_PCT_DFLT = 12.50;
245 constexpr double FSEG_RESERVE_PCT_MIN = 0.03;
246 constexpr double FSEG_RESERVE_PCT_MAX = 40.00;
247
248 #define FSEG_FRAG_LIMIT FSEG_FRAG_ARR_N_SLOTS
249
250 /** If the reserved size of a segment is at least this many extents, we allow
251 extents to be put to the free list of the extent: at most
252 FSEG_FREE_LIST_MAX_LEN many */
253 constexpr uint32_t FSEG_FREE_LIST_LIMIT = 40;
254 constexpr uint32_t FSEG_FREE_LIST_MAX_LEN = 4;
255 /** @} */
256
257 /** @defgroup Extent Descriptor Constants (moved from fsp0fsp.c)
258 @{ */
259
260 /* EXTENT DESCRIPTOR
261 =================
262
263 File extent descriptor data structure: contains bits to tell which pages in
264 the extent are free and which contain old tuple version to clean. */
265
266 /*-------------------------------------*/
267 /** The identifier of the segment to which this extent belongs */
268 constexpr uint32_t XDES_ID = 0;
269 /** The list node data structure for the descriptors */
270 constexpr uint32_t XDES_FLST_NODE = 8;
271 /** contains state information of the extent */
272 constexpr uint32_t XDES_STATE = FLST_NODE_SIZE + 8;
273 /** Descriptor bitmap of the pages in the extent */
274 constexpr uint32_t XDES_BITMAP = FLST_NODE_SIZE + 12;
275
276 /*-------------------------------------*/
277
278 /** How many bits are there per page */
279 constexpr uint32_t XDES_BITS_PER_PAGE = 2;
280 /** Index of the bit which tells if the page is free */
281 constexpr uint32_t XDES_FREE_BIT = 0;
282 /** NOTE: currently not used! Index of the bit which tells if there are old
283 versions of tuples on the page */
284 constexpr uint32_t XDES_CLEAN_BIT = 1;
285
286 /** States of a descriptor */
287 enum xdes_state_t {
288
289 /** extent descriptor is not initialized */
290 XDES_NOT_INITED = 0,
291
292 /** extent is in free list of space */
293 XDES_FREE = 1,
294
295 /** extent is in free fragment list of space */
296 XDES_FREE_FRAG = 2,
297
298 /** extent is in full fragment list of space */
299 XDES_FULL_FRAG = 3,
300
301 /** extent belongs to a segment */
302 XDES_FSEG = 4,
303
304 /** fragment extent leased to segment */
305 XDES_FSEG_FRAG = 5
306 };
307
308 /** File extent data structure size in bytes. */
309 #define XDES_SIZE \
310 (XDES_BITMAP + UT_BITS_IN_BYTES(FSP_EXTENT_SIZE * XDES_BITS_PER_PAGE))
311
312 /** File extent data structure size in bytes for MAX page size. */
313 #define XDES_SIZE_MAX \
314 (XDES_BITMAP + UT_BITS_IN_BYTES(FSP_EXTENT_SIZE_MAX * XDES_BITS_PER_PAGE))
315
316 /** File extent data structure size in bytes for MIN page size. */
317 #define XDES_SIZE_MIN \
318 (XDES_BITMAP + UT_BITS_IN_BYTES(FSP_EXTENT_SIZE_MIN * XDES_BITS_PER_PAGE))
319
320 /** Offset of the descriptor array on a descriptor page */
321 constexpr uint32_t XDES_ARR_OFFSET = FSP_HEADER_OFFSET + FSP_HEADER_SIZE;
322
323 /** The number of reserved pages in a fragment extent. */
324 const ulint XDES_FRAG_N_USED = 2;
325
326 /** @} */
327
328 /** Initializes the file space system. */
329 void fsp_init(void);
330
331 /** Gets the size of the system tablespace from the tablespace header. If
332 we do not have an auto-extending data file, this should be equal to
333 the size of the data files. If there is an auto-extending data file,
334 this can be smaller.
335 @return size in pages */
336 page_no_t fsp_header_get_tablespace_size(void);
337
338 /** Calculate the number of pages to extend a datafile.
339 We extend single-table and general tablespaces first one extent at a time,
340 but 4 at a time for bigger tablespaces. It is not enough to extend always
341 by one extent, because we need to add at least one extent to FSP_FREE.
342 A single extent descriptor page will track many extents. And the extent
343 that uses its extent descriptor page is put onto the FSP_FREE_FRAG list.
344 Extents that do not use their extent descriptor page are added to FSP_FREE.
345 The physical page size is used to determine how many extents are tracked
346 on one extent descriptor page. See xdes_calc_descriptor_page().
347 @param[in] page_size page_size of the datafile
348 @param[in] size current number of pages in the datafile
349 @return number of pages to extend the file. */
350 page_no_t fsp_get_pages_to_extend_ibd(const page_size_t &page_size,
351 page_no_t size);
352
353 /** Calculate the number of physical pages in an extent for this file.
354 @param[in] page_size page_size of the datafile
355 @return number of pages in an extent for this file. */
356 static inline page_no_t fsp_get_extent_size_in_pages(
357 const page_size_t &page_size) {
358 return (static_cast<page_no_t>(FSP_EXTENT_SIZE * UNIV_PAGE_SIZE /
359 page_size.physical()));
360 }
361
362 /** Reads the space id from the first page of a tablespace.
363 @return space id, ULINT UNDEFINED if error */
364 space_id_t fsp_header_get_space_id(
365 const page_t *page); /*!< in: first page of a tablespace */
366
367 /** Read the server version number from the DD tablespace header.
368 @param[out] version server version from tablespace header
369 @return false if success. */
370 bool fsp_header_dict_get_server_version(uint *version);
371
372 /** Read a tablespace header field.
373 @param[in] page first page of a tablespace
374 @param[in] field the header field
375 @return the contents of the header field */
376 inline uint32_t fsp_header_get_field(const page_t *page, uint32_t field) {
377 return (mach_read_from_4(FSP_HEADER_OFFSET + field + page));
378 }
379
380 /** Update a tablespace header field.
381 @param[in] page first page of a tablespace
382 @param[in] field the header field
383 @param[in] val field value */
384 inline void fsp_header_set_field(page_t *page, uint32_t field, uint32_t val) {
385 mach_write_to_4(page + FSP_HEADER_OFFSET + field, val);
386 }
387
388 /** Read the flags from the tablespace header page.
389 @param[in] page first page of a tablespace
390 @return the contents of FSP_SPACE_FLAGS */
391 inline uint32_t fsp_header_get_flags(const page_t *page) {
392 return (fsp_header_get_field(page, FSP_SPACE_FLAGS));
393 }
394
395 /** Reads the page size from the first page of a tablespace.
396 @param[in] page first page of a tablespace
397 @return page size */
398 page_size_t fsp_header_get_page_size(const page_t *page);
399
400 /** Reads the encryption key from the first page of a tablespace.
401 @param[in] fsp_flags tablespace flags
402 @param[in,out] e_key tablespace key, iv
403 @param[in] page first page of a tablespace
404 @return true if success */
405 bool fsp_header_get_encryption_key(uint32_t fsp_flags, Encryption_key &e_key,
406 page_t *page);
407
408 /** Get encryption operation type in progress from the first
409 page of a tablespace.
410 @param[in] page first page of a tablespace
411 @param[in] page_size tablespace page size
412 @return operation type
413 */
414 Encryption::Progress fsp_header_encryption_op_type_in_progress(
415 const page_t *page, page_size_t page_size);
416
417 /** Check if the tablespace size information is valid.
418 @param[in] space_id the tablespace identifier
419 @return true if valid, false if invalid. */
420 bool fsp_check_tablespace_size(space_id_t space_id);
421
422 /** Writes the space id and flags to a tablespace header. The flags contain
423 row type, physical/compressed page size, and logical/uncompressed page
424 size of the tablespace. */
425 void fsp_header_init_fields(
426 page_t *page, /*!< in/out: first page in the space */
427 space_id_t space_id, /*!< in: space id */
428 uint32_t flags); /*!< in: tablespace flags
429 (FSP_SPACE_FLAGS): 0, or
430 table->flags if newer than COMPACT */
431
432 /** Get the offset of encryption information in page 0.
433 @param[in] page_size page size.
434 @return offset on success, otherwise 0. */
435 ulint fsp_header_get_encryption_offset(const page_size_t &page_size);
436
437 /** Write the encryption info into the space header.
438 @param[in] space_id Tablespace id
439 @param[in] space_flags Tablespace flags
440 @param[in] encrypt_info Buffer for re-encrypt key
441 @param[in] update_fsp_flags If it need to update the space flags
442 @param[in] rotate_encryption If it is called during key rotation
443 @param[in,out] mtr Mini-transaction
444 @return true if success. */
445 bool fsp_header_write_encryption(space_id_t space_id, uint32_t space_flags,
446 byte *encrypt_info, bool update_fsp_flags,
447 bool rotate_encryption, mtr_t *mtr);
448
449 /** Write the encryption progress info into the space header.
450 @param[in] space_id Tablespace id
451 @param[in] space_flags Tablespace flags
452 @param[in] progress_info Max pages (un)encrypted
453 @param[in] operation_type Type of operation
454 @param[in] update_operation_type Is operation to be updated
455 @param[in,out] mtr Mini-transaction
456 @return true if success. */
457 bool fsp_header_write_encryption_progress(
458 space_id_t space_id, ulint space_flags, ulint progress_info,
459 byte operation_type, bool update_operation_type, mtr_t *mtr);
460
461 /** Rotate the encryption info in the space header.
462 @param[in] space Tablespace
463 @param[in] encrypt_info Buffer for re-encrypt key.
464 @param[in,out] mtr Mini-transaction
465 @return true if success. */
466 bool fsp_header_rotate_encryption(fil_space_t *space, byte *encrypt_info,
467 mtr_t *mtr);
468
469 /** Initializes the space header of a new created space and creates also the
470 insert buffer tree root if space == 0.
471 @param[in] space_id Space id
472 @param[in] size Current size in blocks
473 @param[in,out] mtr Mini-transaction
474 @return true on success, otherwise false. */
475 bool fsp_header_init(space_id_t space_id, page_no_t size, mtr_t *mtr);
476
477 /** Increases the space size field of a space.
478 @param[in] space_id Space id
479 @param[in] size_inc Size increment in pages
480 @param[in,out] mtr Mini-transaction */
481 void fsp_header_inc_size(space_id_t space_id, page_no_t size_inc, mtr_t *mtr);
482
483 /** Creates a new segment.
484 @return the block where the segment header is placed, x-latched, NULL
485 if could not create segment because of lack of space */
486 buf_block_t *fseg_create(
487 space_id_t space, /*!< in: space id */
488 page_no_t page, /*!< in: page where the segment header is
489 placed: if this is != 0, the page must belong
490 to another segment, if this is 0, a new page
491 will be allocated and it will belong to the
492 created segment */
493 ulint byte_offset, /*!< in: byte offset of the created
494 segment header on the page */
495 mtr_t *mtr); /*!< in/out: mini-transaction */
496
497 /** Creates a new segment.
498 @param[in] space_id Space id
499 @param[in,out] page Page where the segment header is placed: if this is != 0,
500 the page must belong to another segment, if this is 0, a new page will be
501 allocated and it will belong to the created segment
502 @param[in] byte_offset Byte offset of the created segment header on the page
503 @param[in,out] has_done_reservation True if the caller has already done the
504 reservation for the pages with fsp_reserve_free_extents (at least 2 extents: one
505 for the inode and the other for the segment) then there is no need to do the
506 check for this individual operation
507 @param[in,out] mtr Mini-transaction
508 @return the block where the segment header is placed, x-latched, NULL
509 if could not create segment because of lack of space */
510 buf_block_t *fseg_create_general(space_id_t space_id, page_no_t page,
511 ulint byte_offset, bool has_done_reservation,
512 mtr_t *mtr);
513
514 /** Calculates the number of pages reserved by a segment, and how many pages are
515 currently used.
516 @return number of reserved pages */
517 ulint fseg_n_reserved_pages(
518 fseg_header_t *header, /*!< in: segment header */
519 ulint *used, /*!< out: number of pages used (<= reserved) */
520 mtr_t *mtr); /*!< in/out: mini-transaction */
521
522 /** Allocates a single free page from a segment. This function implements
523 the intelligent allocation strategy which tries to minimize file space
524 fragmentation.
525 @param[in,out] seg_header Segment header
526 @param[in] hint Hint of which page would be desirable
527 @param[in,out] direction If the new page is needed because of an index page
528 split, and records are inserted there in order, into which direction they go
529 alphabetically: fsp_down, fsp_up, fsp_no_dir
530 @param[in] has_done_reservation True if the caller has already done the
531 reservation for the page with fsp_reserve_free_extents, then there is no need to
532 do the check for this individual page
533 @param[in,out] mtr Mini-transaction
534 @param[in,out] init_mtr mtr or another mini-transaction in which
535 the page should be initialized. if init_mtr!=mtr, but the page is already
536 latched in mtr, do not initialize the page.
537 @retval NULL if no page could be allocated
538 @retval block, rw_lock_x_lock_count(&block->lock) == 1 if allocation succeeded
539 (init_mtr == mtr, or the page was not previously freed in mtr),
540 returned block is not allocated nor initialized otherwise */
541 [[nodiscard]] buf_block_t *fseg_alloc_free_page_general(
542 fseg_header_t *seg_header, page_no_t hint, byte direction,
543 bool has_done_reservation, mtr_t *mtr, mtr_t *init_mtr);
544
545 /** Allocates a single free page from a segment. This function implements
546 the intelligent allocation strategy which tries to minimize
547 file space fragmentation.
548 @param[in,out] seg_header Segment header
549 @param[in] hint Hint of which page would be desirable
550 @param[in] direction If the new page is needed because
551 of an index page split, and records are
552 inserted there in order, into which
553 direction they go alphabetically: FSP_DOWN,
554 FSP_UP, FSP_NO_DIR
555 @param[in,out] mtr Mini-transaction
556 @return X-latched block, or NULL if no page could be allocated */
557 static inline buf_block_t *fseg_alloc_free_page(fseg_header_t *seg_header,
558 page_no_t hint, byte direction,
559 mtr_t *mtr) {
560 return fseg_alloc_free_page_general(seg_header, hint, direction, false, mtr,
561 mtr);
562 }
563
564 /** Reserves free pages from a tablespace. All mini-transactions which may
565 use several pages from the tablespace should call this function beforehand
566 and reserve enough free extents so that they certainly will be able
567 to do their operation, like a B-tree page split, fully. Reservations
568 must be released with function fil_space_release_free_extents!
569
570 The alloc_type below has the following meaning: FSP_NORMAL means an
571 operation which will probably result in more space usage, like an
572 insert in a B-tree; FSP_UNDO means allocation to undo logs: if we are
573 deleting rows, then this allocation will in the long run result in
574 less space usage (after a purge); FSP_CLEANING means allocation done
575 in a physical record delete (like in a purge) or other cleaning operation
576 which will result in less space usage in the long run. We prefer the latter
577 two types of allocation: when space is scarce, FSP_NORMAL allocations
578 will not succeed, but the latter two allocations will succeed, if possible.
579 The purpose is to avoid dead end where the database is full but the
580 user cannot free any space because these freeing operations temporarily
581 reserve some space.
582
583 Single-table tablespaces whose size is < FSP_EXTENT_SIZE pages are a special
584 case. In this function we would liberally reserve several extents for
585 every page split or merge in a B-tree. But we do not want to waste disk space
586 if the table only occupies < FSP_EXTENT_SIZE pages. That is why we apply
587 different rules in that special case, just ensuring that there are n_pages
588 free pages available.
589
590 @param[out] n_reserved Number of extents actually reserved; if we
591 return true and the tablespace size is <
592 FSP_EXTENT_SIZE pages, then this can be 0,
593 otherwise it is n_ext
594 @param[in] space_id Tablespace identifier
595 @param[in] n_ext Number of extents to reserve
596 @param[in] alloc_type Page reservation type (FSP_BLOB, etc)
597 @param[in,out] mtr Mini-transaction
598 @param[in] n_pages For small tablespaces (tablespace size is
599 less than FSP_EXTENT_SIZE), number of free
600 pages to reserve.
601 @return true if we were able to make the reservation */
602 bool fsp_reserve_free_extents(ulint *n_reserved, space_id_t space_id,
603 ulint n_ext, fsp_reserve_t alloc_type, mtr_t *mtr,
604 page_no_t n_pages = 2);
605
606 /** Calculate how many KiB of new data we will be able to insert to the
607 tablespace without running out of space.
608 @param[in] space_id tablespace ID
609 @return available space in KiB
610 @retval UINTMAX_MAX if unknown */
611 uintmax_t fsp_get_available_space_in_free_extents(space_id_t space_id);
612
613 /** Calculate how many KiB of new data we will be able to insert to the
614 tablespace without running out of space. Start with a space object that has
615 been acquired by the caller who holds it for the calculation,
616 @param[in] space tablespace object from fil_space_acquire()
617 @return available space in KiB */
618 uintmax_t fsp_get_available_space_in_free_extents(const fil_space_t *space);
619
620 /** Frees a single page of a segment.
621 @param[in] seg_header Segment header
622 @param[in] space_id Space id
623 @param[in] page Page offset
624 @param[in] ahi Whether we may need to drop the adaptive hash index
625 @param[in,out] mtr Mini-transaction */
626 void fseg_free_page(fseg_header_t *seg_header, space_id_t space_id,
627 page_no_t page, bool ahi, mtr_t *mtr);
628
629 /** Checks if a single page of a segment is free.
630 @return true if free */
631 [[nodiscard]] bool fseg_page_is_free(
632 fseg_header_t *seg_header, /*!< in: segment header */
633 space_id_t space_id, /*!< in: space id */
634 page_no_t page); /*!< in: page offset */
635 /** Frees part of a segment. This function can be used to free a segment
636 by repeatedly calling this function in different mini-transactions.
637 Doing the freeing in a single mini-transaction might result in
638 too big a mini-transaction.
639 @return true if freeing completed */
640 [[nodiscard]] bool fseg_free_step(
641 fseg_header_t *header, /*!< in, own: segment header; NOTE: if the header
642 resides on the first page of the frag list
643 of the segment, this pointer becomes obsolete
644 after the last freeing step */
645 bool ahi, /*!< in: whether we may need to drop
646 the adaptive hash index */
647 mtr_t *mtr); /*!< in/out: mini-transaction */
648 /** Frees part of a segment. Differs from fseg_free_step because this function
649 leaves the header page unfreed.
650 @return true if freeing completed, except the header page */
651 [[nodiscard]] bool fseg_free_step_not_header(
652 fseg_header_t *header, /*!< in: segment header which must reside on
653 the first fragment page of the segment */
654 bool ahi, /*!< in: whether we may need to drop
655 the adaptive hash index */
656 mtr_t *mtr); /*!< in/out: mini-transaction */
657
658 /** Checks if a page address is an extent descriptor page address.
659 @param[in] page_id page id
660 @param[in] page_size page size
661 @return true if a descriptor page */
662 static inline bool fsp_descr_page(const page_id_t &page_id,
663 const page_size_t &page_size);
664
665 /** Parses a redo log record of a file page init.
666 @param[in] ptr Buffer.
667 @param[in] end_ptr Buffer end.
668 @param[in] block Block or nullptr.
669 @return end of log record or nullptr */
670 byte *fsp_parse_init_file_page(byte *ptr, byte *end_ptr, buf_block_t *block);
671
672 #ifdef UNIV_BTR_PRINT
673 /** Writes info of a segment. */
674 void fseg_print(fseg_header_t *header, /*!< in: segment header */
675 mtr_t *mtr); /*!< in/out: mini-transaction */
676 #endif /* UNIV_BTR_PRINT */
677
678 /** Check whether a space id is an undo tablespace ID
679 Undo tablespaces have space_id's starting 1 less than the redo logs.
680 They are numbered down from this. Since rseg_id=0 always refers to the
681 system tablespace, undo_space_num values start at 1. The current limit
682 is 127. The translation from an undo_space_num is:
683 undo space_id = log_first_space_id - undo_space_num
684 @param[in] space_id space id to check
685 @return true if it is undo tablespace else false. */
686 bool fsp_is_undo_tablespace(space_id_t space_id);
687
688 static inline bool fsp_is_system_tablespace(space_id_t space_id) {
689 return (space_id == TRX_SYS_SPACE);
690 }
691
692 /** Check if the space_id is for a system-tablespace (shared + temp).
693 @param[in] space_id tablespace ID
694 @return true if id is a system tablespace, false if not. */
695 static inline bool fsp_is_system_or_temp_tablespace(space_id_t space_id) {
696 return (fsp_is_system_tablespace(space_id) ||
697 fsp_is_system_temporary(space_id));
698 }
699
700 /** Determine if the space ID is an IBD tablespace, either file_per_table
701 or a general shared tablespace, where user tables exist.
702 @param[in] space_id tablespace ID
703 @return true if it is a user tablespace ID */
704 static inline bool fsp_is_ibd_tablespace(space_id_t space_id) {
705 return (space_id != TRX_SYS_SPACE && !fsp_is_undo_tablespace(space_id) &&
706 !fsp_is_system_temporary(space_id));
707 }
708
709 /** Check if tablespace is file-per-table.
710 @param[in] space_id tablespace ID
711 @param[in] fsp_flags tablespace flags
712 @return true if tablespace is file-per-table. */
713 static inline bool fsp_is_file_per_table(space_id_t space_id,
714 uint32_t fsp_flags) {
715 return (!fsp_is_shared_tablespace(fsp_flags) &&
716 fsp_is_ibd_tablespace(space_id));
717 }
718
719 /** Check if tablespace is dd tablespace.
720 @param[in] space_id tablespace ID
721 @return true if tablespace is dd tablespace. */
722 bool fsp_is_dd_tablespace(space_id_t space_id);
723
724 /** Determine if the tablespace is compressed from tablespace flags.
725 @param[in] flags Tablespace flags
726 @return true if compressed, false if not compressed */
727 static inline bool fsp_flags_is_compressed(uint32_t flags);
728
729 /** Determine if two tablespaces are equivalent or compatible.
730 @param[in] flags1 First tablespace flags
731 @param[in] flags2 Second tablespace flags
732 @return true the flags are compatible, false if not */
733 static inline bool fsp_flags_are_equal(uint32_t flags1, uint32_t flags2);
734
735 /** Initialize an FSP flags integer.
736 @param[in] page_size page sizes in bytes and compression flag.
737 @param[in] atomic_blobs Used by Dynamic and Compressed.
738 @param[in] has_data_dir This tablespace is in a remote location.
739 @param[in] is_shared This tablespace can be shared by many tables.
740 @param[in] is_temporary This tablespace is temporary.
741 @param[in] is_encrypted This tablespace is encrypted.
742 @return tablespace flags after initialization */
743 static inline uint32_t fsp_flags_init(const page_size_t &page_size,
744 bool atomic_blobs, bool has_data_dir,
745 bool is_shared, bool is_temporary,
746 bool is_encrypted = false);
747
748 /** Convert a 32 bit integer tablespace flags to the 32 bit table flags.
749 This can only be done for a tablespace that was built as a file-per-table
750 tablespace. Note that the fsp_flags cannot show the difference between a
751 Compact and Redundant table, so an extra Compact boolean must be supplied.
752 Low order bit
753 | REDUNDANT | COMPACT | COMPRESSED | DYNAMIC
754 fil_space_t::flags | 0 | 0 | 1 | 1
755 dict_table_t::flags | 0 | 1 | 1 | 1
756 @param[in] fsp_flags fil_space_t::flags
757 @param[in] compact true if not Redundant row format
758 @return tablespace flags (fil_space_t::flags) */
759 uint32_t fsp_flags_to_dict_tf(uint32_t fsp_flags, bool compact);
760
761 /** Calculates the descriptor index within a descriptor page.
762 @param[in] page_size page size
763 @param[in] offset page offset
764 @return descriptor index */
765 static inline ulint xdes_calc_descriptor_index(const page_size_t &page_size,
766 ulint offset);
767
768 /** Gets a descriptor bit of a page.
769 @param[in] descr descriptor
770 @param[in] bit XDES_FREE_BIT or XDES_CLEAN_BIT
771 @param[in] offset page offset within extent: 0 ... FSP_EXTENT_SIZE - 1
772 @return true if free */
773 static inline bool xdes_get_bit(const xdes_t *descr, ulint bit,
774 page_no_t offset);
775
776 /** Calculates the page where the descriptor of a page resides.
777 @param[in] page_size page size
778 @param[in] offset page offset
779 @return descriptor page offset */
780 static inline page_no_t xdes_calc_descriptor_page(const page_size_t &page_size,
781 page_no_t offset);
782
783 /** Gets a pointer to the space header and acquires a
784 SX lock on the page.
785 @param[in] id Space id
786 @param[in] page_size Page size
787 @param[in,out] mtr Mini-transaction
788 @param[out] block Block
789 @return pointer to the space header, page x-locked */
790 fsp_header_t *fsp_get_space_header_block(space_id_t id,
791 const page_size_t &page_size,
792 mtr_t *mtr, buf_block_t **block);
793
794 /** Gets a pointer to the space header and acquires a
795 SX lock on the page.
796 @param[in] id Space id
797 @param[in] page_size Page size
798 @param[in,out] mtr Mini-transaction
799 @return pointer to the space header, page x-locked */
800 fsp_header_t *fsp_get_space_header(space_id_t id, const page_size_t &page_size,
801 mtr_t *mtr);
802
803 /** Retrieve tablespace dictionary index root page number stored in the
804 page 0
805 @param[in] space Tablespace id
806 @param[in] page_size Page size
807 @param[in,out] mtr Mini-transaction
808 @return root page num of the tablespace dictionary index copy */
809 page_no_t fsp_sdi_get_root_page_num(space_id_t space,
810 const page_size_t &page_size, mtr_t *mtr);
811
812 /** Write SDI Index root page num to page 0 of tablespace.
813 @param[in,out] page Page 0 frame
814 @param[in] page_size Size of page
815 @param[in] root_page_num Root page number of SDI
816 @param[in,out] mtr Mini-transaction */
817 void fsp_sdi_write_root_to_page(page_t *page, const page_size_t &page_size,
818 page_no_t root_page_num, mtr_t *mtr);
819
820 #include "fsp0fsp.ic"
821
822 /** Reads the server version from the first page of a tablespace.
823 @param[in] page first page of a tablespace
824 @return space server version */
825 inline uint32_t fsp_header_get_server_version(const page_t *page);
826
827 /** Reads the server space version from the first page of a tablespace.
828 @param[in] page first page of a tablespace
829 @return space server version */
830 inline uint32_t fsp_header_get_space_version(const page_t *page);
831
832 /** Get the state of an xdes.
833 @param[in] descr extent descriptor
834 @param[in,out] mtr Mini-transaction.
835 @return state */
836 inline xdes_state_t xdes_get_state(const xdes_t *descr, mtr_t *mtr) {
837 ut_ad(descr && mtr);
838 ut_ad(mtr_memo_contains_page(mtr, descr, MTR_MEMO_PAGE_SX_FIX));
839
840 const ulint state = mach_read_from_4(descr + XDES_STATE);
841
842 ut_ad(xdes_state_is_valid(state));
843 return (static_cast<xdes_state_t>(state));
844 }
845
846 #ifdef UNIV_DEBUG
847 /** Print the extent descriptor page in user-friendly format.
848 @param[in] out The output file stream
849 @param[in] xdes The extent descriptor page
850 @param[in] page_no The page number of xdes page
851 @param[in] mtr Mini-transaction.
852 @return None. */
853 std::ostream &xdes_page_print(std::ostream &out, const page_t *xdes,
854 page_no_t page_no, mtr_t *mtr);
855
856 inline bool xdes_mem_t::is_valid() const {
857 const ulint state = mach_read_from_4(m_xdes + XDES_STATE);
858 return (xdes_state_is_valid(state));
859 }
860
861 inline const char *xdes_mem_t::state_name() const {
862 const ulint val = mach_read_from_4(m_xdes + XDES_STATE);
863
864 ut_ad(xdes_state_is_valid(val));
865
866 xdes_state_t state = static_cast<xdes_state_t>(val);
867
868 switch (state) {
869 case XDES_NOT_INITED:
870 return ("XDES_NOT_INITED");
871 case XDES_FREE:
872 return ("XDES_FREE");
873 case XDES_FREE_FRAG:
874 return ("XDES_FREE_FRAG");
875 case XDES_FULL_FRAG:
876 return ("XDES_FULL_FRAG");
877 case XDES_FSEG:
878 return ("XDES_FSEG");
879 case XDES_FSEG_FRAG:
880 return ("XDES_FSEG_FRAG");
881 }
882 return ("UNKNOWN");
883 }
884
885 #endif /* UNIV_DEBUG */
886
887 /** Update the tablespace size information and generate redo log for it.
888 @param[in] header Tablespace header.
889 @param[in] size New tablespace size in pages.
890 @param[in] mtr Mini-transaction context. */
891 inline void fsp_header_size_update(fsp_header_t *header, ulint size,
892 mtr_t *mtr) {
893 DBUG_TRACE;
894
895 DBUG_LOG("ib_log", "old_size=" << mach_read_from_4(header + FSP_SIZE)
896 << ", new_size=" << size);
897
898 mlog_write_ulint(header + FSP_SIZE, size, MLOG_4BYTES, mtr);
899 }
900
901 /** Check if a specified page is inode page or not. This is used for
902 index root pages of hard-coded DD tables, we can safely assume that the passed
903 in page number is in the range of pages which are only either index root page
904 or inode page
905 @param[in] page Page number to check
906 @return true if it's inode page, otherwise false */
907 inline bool fsp_is_inode_page(page_no_t page);
908
909 /** Get the offset of SDI root page number in page 0.
910 @param[in] page_size Page size.
911 @return offset on success, else 0 */
912 inline ulint fsp_header_get_sdi_offset(const page_size_t &page_size);
913
914 /** Get the offset of encryption progress information in page 0.
915 @param[in] page_size page size.
916 @return offset on success, otherwise 0. */
917 inline ulint fsp_header_get_encryption_progress_offset(
918 const page_size_t &page_size);
919
920 /** Determine if the tablespace has SDI.
921 @param[in] space_id Tablespace id
922 @return DB_SUCCESS if SDI is present else DB_ERROR
923 or DB_TABLESPACE_NOT_FOUND */
924 dberr_t fsp_has_sdi(space_id_t space_id);
925
926 /** Encrypt/Unencrypt a tablespace.
927 @param[in] thd current thread
928 @param[in] space_id Tablespace id
929 @param[in] to_encrypt true if to encrypt, false if to decrypt
930 @param[in,out] dd_space_in dd tablespace object
931 @return 0 for success, otherwise error code */
932 dberr_t fsp_alter_encrypt_tablespace(THD *thd, space_id_t space_id,
933 bool to_encrypt, void *dd_space_in);
934
935 /** Initiate roll-forward of alter encrypt in background thread */
936 void fsp_init_resume_alter_encrypt_tablespace();
937
938 /** A wrapper class to operate on a file segment inode pointer (fseg_inode_t*)
939 */
940 class File_segment_inode {
941 public:
942 /** Constructor
943 @param[in] space_id Table space identifier
944 @param[in] page_size Size of each page in the tablespace.
945 @param[in] inode File segment inode pointer
946 @param[in] mtr Mini-transaction context. */
947 File_segment_inode(space_id_t space_id, const page_size_t &page_size,
948 fseg_inode_t *inode, mtr_t *mtr)
949 : m_space_id(space_id),
950 m_page_size(page_size),
951 m_fseg_inode(inode),
952 m_mtr(mtr) IF_DEBUG(, m_random_engine(m_rd()), m_dist(1, 100)) {}
953
954 /** Update the value of FSEG_NOT_FULL_N_USED.
955 @param[in] n_used the new value of FSEG_NOT_FULL_N_USED. */
956 void write_not_full_n_used(uint32_t n_used);
957
958 /** Get the current value of FSEG_NOT_FULL_N_USED.
959 @return the current value of FSEG_NOT_FULL_N_USED. */
960 uint32_t read_not_full_n_used() const;
961
962 /** Get the segment identifier value.
963 @return the segment identifier value. */
964 uint64_t get_seg_id() const {
965 return (mach_read_from_8(m_fseg_inode + FSEG_ID));
966 }
967
968 /** Print the current object into the given output stream.
969 @return the output stream. */
970 std::ostream &print(std::ostream &out) const;
971
972 private:
973 /** Unique tablespace identifier */
974 space_id_t m_space_id;
975
976 /** The page size used in this tablespace. */
977 const page_size_t &m_page_size;
978
979 /** file segment inode pointer that is being wrapped by this object. */
980 fseg_inode_t *m_fseg_inode;
981
982 /** The mini-transaction operation context. */
983 mtr_t *m_mtr;
984
985 #ifdef UNIV_DEBUG
986 public:
987 /** Verify the stored FSEG_NOT_FULL_N_USED value.
988 @return true if correct value, false if incorrect. */
989 bool verify_not_full_n_used();
990
991 private:
992 /** Calculate the value of FSEG_NOT_FULL_N_USED by traversing
993 the FSEG_NOT_FULL list.
994 @return the calculated value of FSEG_NOT_FULL_N_USED. */
995 page_no_t calculate_not_full_n_used();
996
997 std::random_device m_rd;
998 std::default_random_engine m_random_engine;
999 std::uniform_int_distribution<int> m_dist;
1000
1001 /** To reduce the cost of verification of FSEG_NOT_FULL_N_USED, do it
1002 only when this function returns true.
1003 @return true for 10% of the time. */
1004 bool do_verify() { return (m_dist(m_random_engine) > 90); }
1005 #endif /* UNIV_DEBUG */
1006 };
1007
1008 /** The global output stream operator is overloaded to work with an object
1009 of type File_segment_inode.
1010 @param[in] out the output stream.
1011 @param[in] obj an object of type File_segment_inode.
1012 @return the output stream. */
1013 inline std::ostream &operator<<(std::ostream &out,
1014 const File_segment_inode &obj) {
1015 return (obj.print(out));
1016 }
1017
1018 /** Returns the file segment inode, page x-latched.
1019 @param[in] header Segment header
1020 @param[in] space Space id
1021 @param[in] page_size Page size
1022 @param[in,out] mtr Mini-transaction
1023 @param[out] block Inode block
1024 @return segment inode, page x-latched */
1025 fseg_inode_t *fseg_inode_get(const fseg_header_t *header, space_id_t space,
1026 const page_size_t &page_size, mtr_t *mtr,
1027 buf_block_t **block = nullptr);
1028 #endif