1
by brian
clean slate |
1 |
/******************************************************
|
2 |
File space management
|
|
3 |
||
4 |
(c) 1995 Innobase Oy
|
|
5 |
||
6 |
Created 12/18/1995 Heikki Tuuri
|
|
7 |
*******************************************************/
|
|
8 |
||
9 |
#ifndef fsp0fsp_h
|
|
10 |
#define fsp0fsp_h
|
|
11 |
||
12 |
#include "univ.i" |
|
13 |
||
14 |
#include "mtr0mtr.h" |
|
15 |
#include "fut0lst.h" |
|
16 |
#include "ut0byte.h" |
|
17 |
#include "page0types.h" |
|
18 |
||
19 |
/* If records are inserted in order, there are the following
|
|
20 |
flags to tell this (their type is made byte for the compiler
|
|
21 |
to warn if direction and hint parameters are switched in
|
|
22 |
fseg_alloc_free_page): */
|
|
23 |
#define FSP_UP ((byte)111) /* alphabetically upwards */ |
|
24 |
#define FSP_DOWN ((byte)112) /* alphabetically downwards */ |
|
25 |
#define FSP_NO_DIR ((byte)113) /* no order */ |
|
26 |
||
27 |
/* File space extent size in pages */
|
|
28 |
#define FSP_EXTENT_SIZE 64
|
|
29 |
||
30 |
/* On a page of any file segment, data may be put starting from this offset: */
|
|
31 |
#define FSEG_PAGE_DATA FIL_PAGE_DATA
|
|
32 |
||
33 |
/* File segment header which points to the inode describing the file segment */
|
|
34 |
typedef byte fseg_header_t; |
|
35 |
||
36 |
#define FSEG_HDR_SPACE 0 /* space id of the inode */ |
|
37 |
#define FSEG_HDR_PAGE_NO 4 /* page number of the inode */ |
|
38 |
#define FSEG_HDR_OFFSET 8 /* byte offset of the inode */ |
|
39 |
||
40 |
#define FSEG_HEADER_SIZE 10
|
|
41 |
||
42 |
/**************************************************************************
|
|
43 |
Initializes the file space system. */
|
|
44 |
||
45 |
void
|
|
46 |
fsp_init(void); |
|
47 |
/*==========*/
|
|
48 |
/**************************************************************************
|
|
49 |
Gets the current free limit of a tablespace. The free limit means the
|
|
50 |
place of the first page which has never been put to the the free list
|
|
51 |
for allocation. The space above that address is initialized to zero.
|
|
52 |
Sets also the global variable log_fsp_current_free_limit. */
|
|
53 |
||
54 |
ulint
|
|
55 |
fsp_header_get_free_limit( |
|
56 |
/*======================*/
|
|
57 |
/* out: free limit in megabytes */
|
|
58 |
ulint space); /* in: space id, must be 0 */ |
|
59 |
/**************************************************************************
|
|
60 |
Gets the size of the tablespace from the tablespace header. If we do not
|
|
61 |
have an auto-extending data file, this should be equal to the size of the
|
|
62 |
data files. If there is an auto-extending data file, this can be smaller. */
|
|
63 |
||
64 |
ulint
|
|
65 |
fsp_header_get_tablespace_size( |
|
66 |
/*===========================*/
|
|
67 |
/* out: size in pages */
|
|
68 |
ulint space); /* in: space id, must be 0 */ |
|
69 |
/**************************************************************************
|
|
70 |
Reads the file space size stored in the header page. */
|
|
71 |
||
72 |
ulint
|
|
73 |
fsp_get_size_low( |
|
74 |
/*=============*/
|
|
75 |
/* out: tablespace size stored in the space header */
|
|
76 |
page_t* page); /* in: header page (page 0 in the tablespace) */ |
|
77 |
/**************************************************************************
|
|
78 |
Reads the space id from the first page of a tablespace. */
|
|
79 |
||
80 |
ulint
|
|
81 |
fsp_header_get_space_id( |
|
82 |
/*====================*/
|
|
83 |
/* out: space id, ULINT UNDEFINED if error */
|
|
84 |
page_t* page); /* in: first page of a tablespace */ |
|
85 |
/**************************************************************************
|
|
86 |
Writes the space id to a tablespace header. This function is used past the
|
|
87 |
buffer pool when we in fil0fil.c create a new single-table tablespace. */
|
|
88 |
||
89 |
void
|
|
90 |
fsp_header_write_space_id( |
|
91 |
/*======================*/
|
|
92 |
page_t* page, /* in: first page in the space */ |
|
93 |
ulint space_id); /* in: space id */ |
|
94 |
/**************************************************************************
|
|
95 |
Initializes the space header of a new created space and creates also the
|
|
96 |
insert buffer tree root if space == 0. */
|
|
97 |
||
98 |
void
|
|
99 |
fsp_header_init( |
|
100 |
/*============*/
|
|
101 |
ulint space, /* in: space id */ |
|
102 |
ulint size, /* in: current size in blocks */ |
|
103 |
mtr_t* mtr); /* in: mini-transaction handle */ |
|
104 |
/**************************************************************************
|
|
105 |
Increases the space size field of a space. */
|
|
106 |
||
107 |
void
|
|
108 |
fsp_header_inc_size( |
|
109 |
/*================*/
|
|
110 |
ulint space, /* in: space id */ |
|
111 |
ulint size_inc,/* in: size increment in pages */ |
|
112 |
mtr_t* mtr); /* in: mini-transaction handle */ |
|
113 |
/**************************************************************************
|
|
114 |
Creates a new segment. */
|
|
115 |
||
116 |
page_t* |
|
117 |
fseg_create( |
|
118 |
/*========*/
|
|
119 |
/* out: the page where the segment header is placed,
|
|
120 |
x-latched, NULL if could not create segment
|
|
121 |
because of lack of space */
|
|
122 |
ulint space, /* in: space id */ |
|
123 |
ulint page, /* in: page where the segment header is placed: if |
|
124 |
this is != 0, the page must belong to another segment,
|
|
125 |
if this is 0, a new page will be allocated and it
|
|
126 |
will belong to the created segment */
|
|
127 |
ulint byte_offset, /* in: byte offset of the created segment header |
|
128 |
on the page */
|
|
129 |
mtr_t* mtr); /* in: mtr */ |
|
130 |
/**************************************************************************
|
|
131 |
Creates a new segment. */
|
|
132 |
||
133 |
page_t* |
|
134 |
fseg_create_general( |
|
135 |
/*================*/
|
|
136 |
/* out: the page where the segment header is placed,
|
|
137 |
x-latched, NULL if could not create segment
|
|
138 |
because of lack of space */
|
|
139 |
ulint space, /* in: space id */ |
|
140 |
ulint page, /* in: page where the segment header is placed: if |
|
141 |
this is != 0, the page must belong to another segment,
|
|
142 |
if this is 0, a new page will be allocated and it
|
|
143 |
will belong to the created segment */
|
|
144 |
ulint byte_offset, /* in: byte offset of the created segment header |
|
145 |
on the page */
|
|
146 |
ibool has_done_reservation, /* in: TRUE if the caller has already |
|
147 |
done the reservation for the pages with
|
|
148 |
fsp_reserve_free_extents (at least 2 extents: one for
|
|
149 |
the inode and the other for the segment) then there is |
|
150 |
no need to do the check for this individual |
|
151 |
operation */ |
|
152 |
mtr_t* mtr); /* in: mtr */ |
|
153 |
/**************************************************************************
|
|
154 |
Calculates the number of pages reserved by a segment, and how many pages are
|
|
155 |
currently used. */
|
|
156 |
||
157 |
ulint
|
|
158 |
fseg_n_reserved_pages( |
|
159 |
/*==================*/
|
|
160 |
/* out: number of reserved pages */
|
|
161 |
fseg_header_t* header, /* in: segment header */ |
|
162 |
ulint* used, /* out: number of pages used (<= reserved) */ |
|
163 |
mtr_t* mtr); /* in: mtr handle */ |
|
164 |
/**************************************************************************
|
|
165 |
Allocates a single free page from a segment. This function implements
|
|
166 |
the intelligent allocation strategy which tries to minimize
|
|
167 |
file space fragmentation. */
|
|
168 |
||
169 |
ulint
|
|
170 |
fseg_alloc_free_page( |
|
171 |
/*=================*/
|
|
172 |
/* out: the allocated page offset
|
|
173 |
FIL_NULL if no page could be allocated */
|
|
174 |
fseg_header_t* seg_header, /* in: segment header */ |
|
175 |
ulint hint, /* in: hint of which page would be desirable */ |
|
176 |
byte direction, /* in: if the new page is needed because |
|
177 |
of an index page split, and records are
|
|
178 |
inserted there in order, into which
|
|
179 |
direction they go alphabetically: FSP_DOWN,
|
|
180 |
FSP_UP, FSP_NO_DIR */
|
|
181 |
mtr_t* mtr); /* in: mtr handle */ |
|
182 |
/**************************************************************************
|
|
183 |
Allocates a single free page from a segment. This function implements
|
|
184 |
the intelligent allocation strategy which tries to minimize file space
|
|
185 |
fragmentation. */
|
|
186 |
||
187 |
ulint
|
|
188 |
fseg_alloc_free_page_general( |
|
189 |
/*=========================*/
|
|
190 |
/* out: allocated page offset, FIL_NULL if no
|
|
191 |
page could be allocated */
|
|
192 |
fseg_header_t* seg_header,/* in: segment header */ |
|
193 |
ulint hint, /* in: hint of which page would be desirable */ |
|
194 |
byte direction,/* in: if the new page is needed because |
|
195 |
of an index page split, and records are
|
|
196 |
inserted there in order, into which
|
|
197 |
direction they go alphabetically: FSP_DOWN,
|
|
198 |
FSP_UP, FSP_NO_DIR */
|
|
199 |
ibool has_done_reservation, /* in: TRUE if the caller has |
|
200 |
already done the reservation for the page
|
|
201 |
with fsp_reserve_free_extents, then there
|
|
202 |
is no need to do the check for this individual
|
|
203 |
page */
|
|
204 |
mtr_t* mtr); /* in: mtr handle */ |
|
205 |
/**************************************************************************
|
|
206 |
Reserves free pages from a tablespace. All mini-transactions which may
|
|
207 |
use several pages from the tablespace should call this function beforehand
|
|
208 |
and reserve enough free extents so that they certainly will be able
|
|
209 |
to do their operation, like a B-tree page split, fully. Reservations
|
|
210 |
must be released with function fil_space_release_free_extents!
|
|
211 |
||
212 |
The alloc_type below has the following meaning: FSP_NORMAL means an
|
|
213 |
operation which will probably result in more space usage, like an
|
|
214 |
insert in a B-tree; FSP_UNDO means allocation to undo logs: if we are
|
|
215 |
deleting rows, then this allocation will in the long run result in
|
|
216 |
less space usage (after a purge); FSP_CLEANING means allocation done
|
|
217 |
in a physical record delete (like in a purge) or other cleaning operation
|
|
218 |
which will result in less space usage in the long run. We prefer the latter
|
|
219 |
two types of allocation: when space is scarce, FSP_NORMAL allocations
|
|
220 |
will not succeed, but the latter two allocations will succeed, if possible.
|
|
221 |
The purpose is to avoid dead end where the database is full but the
|
|
222 |
user cannot free any space because these freeing operations temporarily
|
|
223 |
reserve some space.
|
|
224 |
||
225 |
Single-table tablespaces whose size is < 32 pages are a special case. In this
|
|
226 |
function we would liberally reserve several 64 page extents for every page
|
|
227 |
split or merge in a B-tree. But we do not want to waste disk space if the table
|
|
228 |
only occupies < 32 pages. That is why we apply different rules in that special
|
|
229 |
case, just ensuring that there are 3 free pages available. */
|
|
230 |
||
231 |
ibool
|
|
232 |
fsp_reserve_free_extents( |
|
233 |
/*=====================*/
|
|
234 |
/* out: TRUE if we were able to make the reservation */
|
|
235 |
ulint* n_reserved,/* out: number of extents actually reserved; if we |
|
236 |
return TRUE and the tablespace size is < 64 pages,
|
|
237 |
then this can be 0, otherwise it is n_ext */
|
|
238 |
ulint space, /* in: space id */ |
|
239 |
ulint n_ext, /* in: number of extents to reserve */ |
|
240 |
ulint alloc_type,/* in: FSP_NORMAL, FSP_UNDO, or FSP_CLEANING */ |
|
241 |
mtr_t* mtr); /* in: mtr */ |
|
242 |
/**************************************************************************
|
|
243 |
This function should be used to get information on how much we still
|
|
244 |
will be able to insert new data to the database without running out the
|
|
245 |
tablespace. Only free extents are taken into account and we also subtract
|
|
246 |
the safety margin required by the above function fsp_reserve_free_extents. */
|
|
247 |
||
248 |
ullint
|
|
249 |
fsp_get_available_space_in_free_extents( |
|
250 |
/*====================================*/
|
|
251 |
/* out: available space in kB */
|
|
252 |
ulint space); /* in: space id */ |
|
253 |
/**************************************************************************
|
|
254 |
Frees a single page of a segment. */
|
|
255 |
||
256 |
void
|
|
257 |
fseg_free_page( |
|
258 |
/*===========*/
|
|
259 |
fseg_header_t* seg_header, /* in: segment header */ |
|
260 |
ulint space, /* in: space id */ |
|
261 |
ulint page, /* in: page offset */ |
|
262 |
mtr_t* mtr); /* in: mtr handle */ |
|
263 |
/***********************************************************************
|
|
264 |
Frees a segment. The freeing is performed in several mini-transactions,
|
|
265 |
so that there is no danger of bufferfixing too many buffer pages. */
|
|
266 |
||
267 |
void
|
|
268 |
fseg_free( |
|
269 |
/*======*/
|
|
270 |
ulint space, /* in: space id */ |
|
271 |
ulint page_no,/* in: page number where the segment header is |
|
272 |
placed */
|
|
273 |
ulint offset);/* in: byte offset of the segment header on that |
|
274 |
page */
|
|
275 |
/**************************************************************************
|
|
276 |
Frees part of a segment. This function can be used to free a segment
|
|
277 |
by repeatedly calling this function in different mini-transactions.
|
|
278 |
Doing the freeing in a single mini-transaction might result in
|
|
279 |
too big a mini-transaction. */
|
|
280 |
||
281 |
ibool
|
|
282 |
fseg_free_step( |
|
283 |
/*===========*/
|
|
284 |
/* out: TRUE if freeing completed */
|
|
285 |
fseg_header_t* header, /* in, own: segment header; NOTE: if the header |
|
286 |
resides on the first page of the frag list
|
|
287 |
of the segment, this pointer becomes obsolete
|
|
288 |
after the last freeing step */
|
|
289 |
mtr_t* mtr); /* in: mtr */ |
|
290 |
/**************************************************************************
|
|
291 |
Frees part of a segment. Differs from fseg_free_step because this function
|
|
292 |
leaves the header page unfreed. */
|
|
293 |
||
294 |
ibool
|
|
295 |
fseg_free_step_not_header( |
|
296 |
/*======================*/
|
|
297 |
/* out: TRUE if freeing completed, except the
|
|
298 |
header page */
|
|
299 |
fseg_header_t* header, /* in: segment header which must reside on |
|
300 |
the first fragment page of the segment */
|
|
301 |
mtr_t* mtr); /* in: mtr */ |
|
302 |
/***************************************************************************
|
|
303 |
Checks if a page address is an extent descriptor page address. */
|
|
304 |
UNIV_INLINE
|
|
305 |
ibool
|
|
306 |
fsp_descr_page( |
|
307 |
/*===========*/
|
|
308 |
/* out: TRUE if a descriptor page */
|
|
309 |
ulint page_no);/* in: page number */ |
|
310 |
/***************************************************************
|
|
311 |
Parses a redo log record of a file page init. */
|
|
312 |
||
313 |
byte* |
|
314 |
fsp_parse_init_file_page( |
|
315 |
/*=====================*/
|
|
316 |
/* out: end of log record or NULL */
|
|
317 |
byte* ptr, /* in: buffer */ |
|
318 |
byte* end_ptr,/* in: buffer end */ |
|
319 |
page_t* page); /* in: page or NULL */ |
|
320 |
/***********************************************************************
|
|
321 |
Validates the file space system and its segments. */
|
|
322 |
||
323 |
ibool
|
|
324 |
fsp_validate( |
|
325 |
/*=========*/
|
|
326 |
/* out: TRUE if ok */
|
|
327 |
ulint space); /* in: space id */ |
|
328 |
/***********************************************************************
|
|
329 |
Prints info of a file space. */
|
|
330 |
||
331 |
void
|
|
332 |
fsp_print( |
|
333 |
/*======*/
|
|
334 |
ulint space); /* in: space id */ |
|
335 |
/***********************************************************************
|
|
336 |
Validates a segment. */
|
|
337 |
||
338 |
ibool
|
|
339 |
fseg_validate( |
|
340 |
/*==========*/
|
|
341 |
/* out: TRUE if ok */
|
|
342 |
fseg_header_t* header, /* in: segment header */ |
|
343 |
mtr_t* mtr2); /* in: mtr */ |
|
344 |
/***********************************************************************
|
|
345 |
Writes info of a segment. */
|
|
346 |
||
347 |
void
|
|
348 |
fseg_print( |
|
349 |
/*=======*/
|
|
350 |
fseg_header_t* header, /* in: segment header */ |
|
351 |
mtr_t* mtr); /* in: mtr */ |
|
352 |
||
353 |
/* Flags for fsp_reserve_free_extents */
|
|
354 |
#define FSP_NORMAL 1000000
|
|
355 |
#define FSP_UNDO 2000000
|
|
356 |
#define FSP_CLEANING 3000000
|
|
357 |
||
358 |
/* Number of pages described in a single descriptor page: currently each page
|
|
359 |
description takes less than 1 byte; a descriptor page is repeated every
|
|
360 |
this many file pages */
|
|
361 |
#define XDES_DESCRIBED_PER_PAGE UNIV_PAGE_SIZE
|
|
362 |
||
363 |
/* The space low address page map */
|
|
364 |
/*--------------------------------------*/
|
|
365 |
/* The following two pages are repeated
|
|
366 |
every XDES_DESCRIBED_PER_PAGE pages in
|
|
367 |
every tablespace. */
|
|
368 |
#define FSP_XDES_OFFSET 0 /* extent descriptor */ |
|
369 |
#define FSP_IBUF_BITMAP_OFFSET 1 /* insert buffer bitmap */ |
|
370 |
/* The ibuf bitmap pages are the ones whose
|
|
371 |
page number is the number above plus a
|
|
372 |
multiple of XDES_DESCRIBED_PER_PAGE */
|
|
373 |
||
374 |
#define FSP_FIRST_INODE_PAGE_NO 2 /* in every tablespace */ |
|
375 |
/* The following pages exist
|
|
376 |
in the system tablespace (space 0). */
|
|
377 |
#define FSP_IBUF_HEADER_PAGE_NO 3 /* in tablespace 0 */ |
|
378 |
#define FSP_IBUF_TREE_ROOT_PAGE_NO 4 /* in tablespace 0 */ |
|
379 |
/* The ibuf tree root page number in
|
|
380 |
tablespace 0; its fseg inode is on the page
|
|
381 |
number FSP_FIRST_INODE_PAGE_NO */
|
|
382 |
#define FSP_TRX_SYS_PAGE_NO 5 /* in tablespace 0 */ |
|
383 |
#define FSP_FIRST_RSEG_PAGE_NO 6 /* in tablespace 0 */ |
|
384 |
#define FSP_DICT_HDR_PAGE_NO 7 /* in tablespace 0 */ |
|
385 |
/*--------------------------------------*/
|
|
386 |
||
387 |
#ifndef UNIV_NONINL
|
|
388 |
#include "fsp0fsp.ic" |
|
389 |
#endif
|
|
390 |
||
391 |
#endif
|