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