00001 /****************************************************** 00002 File space management 00003 00004 (c) 1995 Innobase Oy 00005 00006 Created 12/18/1995 Heikki Tuuri 00007 *******************************************************/ 00008 00009 #ifndef fsp0fsp_h 00010 #define fsp0fsp_h 00011 00012 #include "univ.i" 00013 00014 #include "mtr0mtr.h" 00015 #include "fut0lst.h" 00016 #include "ut0byte.h" 00017 #include "page0types.h" 00018 00019 /* If records are inserted in order, there are the following 00020 flags to tell this (their type is made byte for the compiler 00021 to warn if direction and hint parameters are switched in 00022 fseg_alloc_free_page): */ 00023 #define FSP_UP ((byte)111) /* alphabetically upwards */ 00024 #define FSP_DOWN ((byte)112) /* alphabetically downwards */ 00025 #define FSP_NO_DIR ((byte)113) /* no order */ 00026 00027 /* File space extent size in pages */ 00028 #define FSP_EXTENT_SIZE 64 00029 00030 /* On a page of any file segment, data may be put starting from this offset: */ 00031 #define FSEG_PAGE_DATA FIL_PAGE_DATA 00032 00033 /* File segment header which points to the inode describing the file segment */ 00034 typedef byte fseg_header_t; 00035 00036 #define FSEG_HDR_SPACE 0 /* space id of the inode */ 00037 #define FSEG_HDR_PAGE_NO 4 /* page number of the inode */ 00038 #define FSEG_HDR_OFFSET 8 /* byte offset of the inode */ 00039 00040 #define FSEG_HEADER_SIZE 10 00041 00042 /************************************************************************** 00043 Initializes the file space system. */ 00044 00045 void 00046 fsp_init(void); 00047 /*==========*/ 00048 /************************************************************************** 00049 Gets the current free limit of a tablespace. The free limit means the 00050 place of the first page which has never been put to the the free list 00051 for allocation. The space above that address is initialized to zero. 00052 Sets also the global variable log_fsp_current_free_limit. */ 00053 00054 ulint 00055 fsp_header_get_free_limit( 00056 /*======================*/ 00057 /* out: free limit in megabytes */ 00058 ulint space); /* in: space id, must be 0 */ 00059 /************************************************************************** 00060 Gets the size of the tablespace from the tablespace header. If we do not 00061 have an auto-extending data file, this should be equal to the size of the 00062 data files. If there is an auto-extending data file, this can be smaller. */ 00063 00064 ulint 00065 fsp_header_get_tablespace_size( 00066 /*===========================*/ 00067 /* out: size in pages */ 00068 ulint space); /* in: space id, must be 0 */ 00069 /************************************************************************** 00070 Reads the file space size stored in the header page. */ 00071 00072 ulint 00073 fsp_get_size_low( 00074 /*=============*/ 00075 /* out: tablespace size stored in the space header */ 00076 page_t* page); /* in: header page (page 0 in the tablespace) */ 00077 /************************************************************************** 00078 Reads the space id from the first page of a tablespace. */ 00079 00080 ulint 00081 fsp_header_get_space_id( 00082 /*====================*/ 00083 /* out: space id, ULINT UNDEFINED if error */ 00084 page_t* page); /* in: first page of a tablespace */ 00085 /************************************************************************** 00086 Writes the space id to a tablespace header. This function is used past the 00087 buffer pool when we in fil0fil.c create a new single-table tablespace. */ 00088 00089 void 00090 fsp_header_write_space_id( 00091 /*======================*/ 00092 page_t* page, /* in: first page in the space */ 00093 ulint space_id); /* in: space id */ 00094 /************************************************************************** 00095 Initializes the space header of a new created space and creates also the 00096 insert buffer tree root if space == 0. */ 00097 00098 void 00099 fsp_header_init( 00100 /*============*/ 00101 ulint space, /* in: space id */ 00102 ulint size, /* in: current size in blocks */ 00103 mtr_t* mtr); /* in: mini-transaction handle */ 00104 /************************************************************************** 00105 Increases the space size field of a space. */ 00106 00107 void 00108 fsp_header_inc_size( 00109 /*================*/ 00110 ulint space, /* in: space id */ 00111 ulint size_inc,/* in: size increment in pages */ 00112 mtr_t* mtr); /* in: mini-transaction handle */ 00113 /************************************************************************** 00114 Creates a new segment. */ 00115 00116 page_t* 00117 fseg_create( 00118 /*========*/ 00119 /* out: the page where the segment header is placed, 00120 x-latched, NULL if could not create segment 00121 because of lack of space */ 00122 ulint space, /* in: space id */ 00123 ulint page, /* in: page where the segment header is placed: if 00124 this is != 0, the page must belong to another segment, 00125 if this is 0, a new page will be allocated and it 00126 will belong to the created segment */ 00127 ulint byte_offset, /* in: byte offset of the created segment header 00128 on the page */ 00129 mtr_t* mtr); /* in: mtr */ 00130 /************************************************************************** 00131 Creates a new segment. */ 00132 00133 page_t* 00134 fseg_create_general( 00135 /*================*/ 00136 /* out: the page where the segment header is placed, 00137 x-latched, NULL if could not create segment 00138 because of lack of space */ 00139 ulint space, /* in: space id */ 00140 ulint page, /* in: page where the segment header is placed: if 00141 this is != 0, the page must belong to another segment, 00142 if this is 0, a new page will be allocated and it 00143 will belong to the created segment */ 00144 ulint byte_offset, /* in: byte offset of the created segment header 00145 on the page */ 00146 ibool has_done_reservation, /* in: TRUE if the caller has already 00147 done the reservation for the pages with 00148 fsp_reserve_free_extents (at least 2 extents: one for 00149 the inode and the other for the segment) then there is 00150 no need to do the check for this individual 00151 operation */ 00152 mtr_t* mtr); /* in: mtr */ 00153 /************************************************************************** 00154 Calculates the number of pages reserved by a segment, and how many pages are 00155 currently used. */ 00156 00157 ulint 00158 fseg_n_reserved_pages( 00159 /*==================*/ 00160 /* out: number of reserved pages */ 00161 fseg_header_t* header, /* in: segment header */ 00162 ulint* used, /* out: number of pages used (<= reserved) */ 00163 mtr_t* mtr); /* in: mtr handle */ 00164 /************************************************************************** 00165 Allocates a single free page from a segment. This function implements 00166 the intelligent allocation strategy which tries to minimize 00167 file space fragmentation. */ 00168 00169 ulint 00170 fseg_alloc_free_page( 00171 /*=================*/ 00172 /* out: the allocated page offset 00173 FIL_NULL if no page could be allocated */ 00174 fseg_header_t* seg_header, /* in: segment header */ 00175 ulint hint, /* in: hint of which page would be desirable */ 00176 byte direction, /* in: if the new page is needed because 00177 of an index page split, and records are 00178 inserted there in order, into which 00179 direction they go alphabetically: FSP_DOWN, 00180 FSP_UP, FSP_NO_DIR */ 00181 mtr_t* mtr); /* in: mtr handle */ 00182 /************************************************************************** 00183 Allocates a single free page from a segment. This function implements 00184 the intelligent allocation strategy which tries to minimize file space 00185 fragmentation. */ 00186 00187 ulint 00188 fseg_alloc_free_page_general( 00189 /*=========================*/ 00190 /* out: allocated page offset, FIL_NULL if no 00191 page could be allocated */ 00192 fseg_header_t* seg_header,/* in: segment header */ 00193 ulint hint, /* in: hint of which page would be desirable */ 00194 byte direction,/* in: if the new page is needed because 00195 of an index page split, and records are 00196 inserted there in order, into which 00197 direction they go alphabetically: FSP_DOWN, 00198 FSP_UP, FSP_NO_DIR */ 00199 ibool has_done_reservation, /* in: TRUE if the caller has 00200 already done the reservation for the page 00201 with fsp_reserve_free_extents, then there 00202 is no need to do the check for this individual 00203 page */ 00204 mtr_t* mtr); /* in: mtr handle */ 00205 /************************************************************************** 00206 Reserves free pages from a tablespace. All mini-transactions which may 00207 use several pages from the tablespace should call this function beforehand 00208 and reserve enough free extents so that they certainly will be able 00209 to do their operation, like a B-tree page split, fully. Reservations 00210 must be released with function fil_space_release_free_extents! 00211 00212 The alloc_type below has the following meaning: FSP_NORMAL means an 00213 operation which will probably result in more space usage, like an 00214 insert in a B-tree; FSP_UNDO means allocation to undo logs: if we are 00215 deleting rows, then this allocation will in the long run result in 00216 less space usage (after a purge); FSP_CLEANING means allocation done 00217 in a physical record delete (like in a purge) or other cleaning operation 00218 which will result in less space usage in the long run. We prefer the latter 00219 two types of allocation: when space is scarce, FSP_NORMAL allocations 00220 will not succeed, but the latter two allocations will succeed, if possible. 00221 The purpose is to avoid dead end where the database is full but the 00222 user cannot free any space because these freeing operations temporarily 00223 reserve some space. 00224 00225 Single-table tablespaces whose size is < 32 pages are a special case. In this 00226 function we would liberally reserve several 64 page extents for every page 00227 split or merge in a B-tree. But we do not want to waste disk space if the table 00228 only occupies < 32 pages. That is why we apply different rules in that special 00229 case, just ensuring that there are 3 free pages available. */ 00230 00231 ibool 00232 fsp_reserve_free_extents( 00233 /*=====================*/ 00234 /* out: TRUE if we were able to make the reservation */ 00235 ulint* n_reserved,/* out: number of extents actually reserved; if we 00236 return TRUE and the tablespace size is < 64 pages, 00237 then this can be 0, otherwise it is n_ext */ 00238 ulint space, /* in: space id */ 00239 ulint n_ext, /* in: number of extents to reserve */ 00240 ulint alloc_type,/* in: FSP_NORMAL, FSP_UNDO, or FSP_CLEANING */ 00241 mtr_t* mtr); /* in: mtr */ 00242 /************************************************************************** 00243 This function should be used to get information on how much we still 00244 will be able to insert new data to the database without running out the 00245 tablespace. Only free extents are taken into account and we also subtract 00246 the safety margin required by the above function fsp_reserve_free_extents. */ 00247 00248 ulint 00249 fsp_get_available_space_in_free_extents( 00250 /*====================================*/ 00251 /* out: available space in kB */ 00252 ulint space); /* in: space id */ 00253 /************************************************************************** 00254 Frees a single page of a segment. */ 00255 00256 void 00257 fseg_free_page( 00258 /*===========*/ 00259 fseg_header_t* seg_header, /* in: segment header */ 00260 ulint space, /* in: space id */ 00261 ulint page, /* in: page offset */ 00262 mtr_t* mtr); /* in: mtr handle */ 00263 /*********************************************************************** 00264 Frees a segment. The freeing is performed in several mini-transactions, 00265 so that there is no danger of bufferfixing too many buffer pages. */ 00266 00267 void 00268 fseg_free( 00269 /*======*/ 00270 ulint space, /* in: space id */ 00271 ulint page_no,/* in: page number where the segment header is 00272 placed */ 00273 ulint offset);/* in: byte offset of the segment header on that 00274 page */ 00275 /************************************************************************** 00276 Frees part of a segment. This function can be used to free a segment 00277 by repeatedly calling this function in different mini-transactions. 00278 Doing the freeing in a single mini-transaction might result in 00279 too big a mini-transaction. */ 00280 00281 ibool 00282 fseg_free_step( 00283 /*===========*/ 00284 /* out: TRUE if freeing completed */ 00285 fseg_header_t* header, /* in, own: segment header; NOTE: if the header 00286 resides on the first page of the frag list 00287 of the segment, this pointer becomes obsolete 00288 after the last freeing step */ 00289 mtr_t* mtr); /* in: mtr */ 00290 /************************************************************************** 00291 Frees part of a segment. Differs from fseg_free_step because this function 00292 leaves the header page unfreed. */ 00293 00294 ibool 00295 fseg_free_step_not_header( 00296 /*======================*/ 00297 /* out: TRUE if freeing completed, except the 00298 header page */ 00299 fseg_header_t* header, /* in: segment header which must reside on 00300 the first fragment page of the segment */ 00301 mtr_t* mtr); /* in: mtr */ 00302 /*************************************************************************** 00303 Checks if a page address is an extent descriptor page address. */ 00304 UNIV_INLINE 00305 ibool 00306 fsp_descr_page( 00307 /*===========*/ 00308 /* out: TRUE if a descriptor page */ 00309 ulint page_no);/* in: page number */ 00310 /*************************************************************** 00311 Parses a redo log record of a file page init. */ 00312 00313 byte* 00314 fsp_parse_init_file_page( 00315 /*=====================*/ 00316 /* out: end of log record or NULL */ 00317 byte* ptr, /* in: buffer */ 00318 byte* end_ptr,/* in: buffer end */ 00319 page_t* page); /* in: page or NULL */ 00320 /*********************************************************************** 00321 Validates the file space system and its segments. */ 00322 00323 ibool 00324 fsp_validate( 00325 /*=========*/ 00326 /* out: TRUE if ok */ 00327 ulint space); /* in: space id */ 00328 /*********************************************************************** 00329 Prints info of a file space. */ 00330 00331 void 00332 fsp_print( 00333 /*======*/ 00334 ulint space); /* in: space id */ 00335 /*********************************************************************** 00336 Validates a segment. */ 00337 00338 ibool 00339 fseg_validate( 00340 /*==========*/ 00341 /* out: TRUE if ok */ 00342 fseg_header_t* header, /* in: segment header */ 00343 mtr_t* mtr2); /* in: mtr */ 00344 /*********************************************************************** 00345 Writes info of a segment. */ 00346 00347 void 00348 fseg_print( 00349 /*=======*/ 00350 fseg_header_t* header, /* in: segment header */ 00351 mtr_t* mtr); /* in: mtr */ 00352 00353 /* Flags for fsp_reserve_free_extents */ 00354 #define FSP_NORMAL 1000000 00355 #define FSP_UNDO 2000000 00356 #define FSP_CLEANING 3000000 00357 00358 /* Number of pages described in a single descriptor page: currently each page 00359 description takes less than 1 byte; a descriptor page is repeated every 00360 this many file pages */ 00361 #define XDES_DESCRIBED_PER_PAGE UNIV_PAGE_SIZE 00362 00363 /* The space low address page map */ 00364 /*--------------------------------------*/ 00365 /* The following two pages are repeated 00366 every XDES_DESCRIBED_PER_PAGE pages in 00367 every tablespace. */ 00368 #define FSP_XDES_OFFSET 0 /* extent descriptor */ 00369 #define FSP_IBUF_BITMAP_OFFSET 1 /* insert buffer bitmap */ 00370 /* The ibuf bitmap pages are the ones whose 00371 page number is the number above plus a 00372 multiple of XDES_DESCRIBED_PER_PAGE */ 00373 00374 #define FSP_FIRST_INODE_PAGE_NO 2 /* in every tablespace */ 00375 /* The following pages exist 00376 in the system tablespace (space 0). */ 00377 #define FSP_IBUF_HEADER_PAGE_NO 3 /* in tablespace 0 */ 00378 #define FSP_IBUF_TREE_ROOT_PAGE_NO 4 /* in tablespace 0 */ 00379 /* The ibuf tree root page number in 00380 tablespace 0; its fseg inode is on the page 00381 number FSP_FIRST_INODE_PAGE_NO */ 00382 #define FSP_TRX_SYS_PAGE_NO 5 /* in tablespace 0 */ 00383 #define FSP_FIRST_RSEG_PAGE_NO 6 /* in tablespace 0 */ 00384 #define FSP_DICT_HDR_PAGE_NO 7 /* in tablespace 0 */ 00385 /*--------------------------------------*/ 00386 00387 #ifndef UNIV_NONINL 00388 #include "fsp0fsp.ic" 00389 #endif 00390 00391 #endif
1.4.7

