You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4130 lines
115 KiB

17 years ago
9 years ago
17 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
9 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
9 years ago
16 years ago
16 years ago
16 years ago
16 years ago
9 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
9 years ago
16 years ago
16 years ago
9 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
9 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
9 years ago
16 years ago
16 years ago
9 years ago
16 years ago
16 years ago
10 years ago
16 years ago
10 years ago
10 years ago
10 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
9 years ago
9 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
9 years ago
16 years ago
9 years ago
16 years ago
16 years ago
15 years ago
16 years ago
16 years ago
15 years ago
16 years ago
16 years ago
15 years ago
16 years ago
16 years ago
15 years ago
16 years ago
16 years ago
16 years ago
15 years ago
16 years ago
16 years ago
15 years ago
16 years ago
16 years ago
9 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 years ago
17 years ago
15 years ago
15 years ago
15 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 years ago
15 years ago
15 years ago
16 years ago
16 years ago
16 years ago
15 years ago
15 years ago
15 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 years ago
16 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
16 years ago
16 years ago
16 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
9 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 years ago
9 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
9 years ago
15 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 years ago
15 years ago
16 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
  1. /*****************************************************************************
  2. Copyright (c) 1995, 2016, Oracle and/or its affiliates. All Rights Reserved.
  3. This program is free software; you can redistribute it and/or modify it under
  4. the terms of the GNU General Public License as published by the Free Software
  5. Foundation; version 2 of the License.
  6. This program is distributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along with
  10. this program; if not, write to the Free Software Foundation, Inc.,
  11. 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
  12. *****************************************************************************/
  13. /******************************************************************//**
  14. @file fsp/fsp0fsp.cc
  15. File space management
  16. Created 11/29/1995 Heikki Tuuri
  17. ***********************************************************************/
  18. #include "fsp0fsp.h"
  19. #ifdef UNIV_NONINL
  20. #include "fsp0fsp.ic"
  21. #endif
  22. #include "buf0buf.h"
  23. #include "fil0fil.h"
  24. #include "mtr0log.h"
  25. #include "ut0byte.h"
  26. #include "page0page.h"
  27. #include "page0zip.h"
  28. #ifdef UNIV_HOTBACKUP
  29. # include "fut0lst.h"
  30. #else /* UNIV_HOTBACKUP */
  31. # include "sync0sync.h"
  32. # include "fut0fut.h"
  33. # include "srv0srv.h"
  34. # include "ibuf0ibuf.h"
  35. # include "btr0btr.h"
  36. # include "btr0sea.h"
  37. # include "dict0boot.h"
  38. # include "log0log.h"
  39. #endif /* UNIV_HOTBACKUP */
  40. #include "dict0mem.h"
  41. #include "srv0start.h"
  42. #ifndef UNIV_HOTBACKUP
  43. /** Flag to indicate if we have printed the tablespace full error. */
  44. static ibool fsp_tbs_full_error_printed = FALSE;
  45. /**********************************************************************//**
  46. Returns an extent to the free list of a space. */
  47. static
  48. void
  49. fsp_free_extent(
  50. /*============*/
  51. ulint space, /*!< in: space id */
  52. ulint zip_size,/*!< in: compressed page size in bytes
  53. or 0 for uncompressed pages */
  54. ulint page, /*!< in: page offset in the extent */
  55. mtr_t* mtr); /*!< in/out: mini-transaction */
  56. /**********************************************************************//**
  57. Frees an extent of a segment to the space free list. */
  58. static
  59. void
  60. fseg_free_extent(
  61. /*=============*/
  62. fseg_inode_t* seg_inode, /*!< in: segment inode */
  63. ulint space, /*!< in: space id */
  64. ulint zip_size,/*!< in: compressed page size in bytes
  65. or 0 for uncompressed pages */
  66. ulint page, /*!< in: page offset in the extent */
  67. mtr_t* mtr); /*!< in/out: mini-transaction */
  68. /**********************************************************************//**
  69. Calculates the number of pages reserved by a segment, and how
  70. many pages are currently used.
  71. @return number of reserved pages */
  72. static
  73. ulint
  74. fseg_n_reserved_pages_low(
  75. /*======================*/
  76. fseg_inode_t* header, /*!< in: segment inode */
  77. ulint* used, /*!< out: number of pages used (not
  78. more than reserved) */
  79. mtr_t* mtr); /*!< in/out: mini-transaction */
  80. /********************************************************************//**
  81. Marks a page used. The page must reside within the extents of the given
  82. segment. */
  83. static MY_ATTRIBUTE((nonnull))
  84. void
  85. fseg_mark_page_used(
  86. /*================*/
  87. fseg_inode_t* seg_inode,/*!< in: segment inode */
  88. ulint page, /*!< in: page offset */
  89. xdes_t* descr, /*!< in: extent descriptor */
  90. mtr_t* mtr); /*!< in/out: mini-transaction */
  91. /**********************************************************************//**
  92. Returns the first extent descriptor for a segment. We think of the extent
  93. lists of the segment catenated in the order FSEG_FULL -> FSEG_NOT_FULL
  94. -> FSEG_FREE.
  95. @return the first extent descriptor, or NULL if none */
  96. static
  97. xdes_t*
  98. fseg_get_first_extent(
  99. /*==================*/
  100. fseg_inode_t* inode, /*!< in: segment inode */
  101. ulint space, /*!< in: space id */
  102. ulint zip_size,/*!< in: compressed page size in bytes
  103. or 0 for uncompressed pages */
  104. mtr_t* mtr); /*!< in/out: mini-transaction */
  105. /**********************************************************************//**
  106. Puts new extents to the free list if
  107. there are free extents above the free limit. If an extent happens
  108. to contain an extent descriptor page, the extent is put to
  109. the FSP_FREE_FRAG list with the page marked as used. */
  110. static
  111. void
  112. fsp_fill_free_list(
  113. /*===============*/
  114. ibool init_space, /*!< in: TRUE if this is a single-table
  115. tablespace and we are only initing
  116. the tablespace's first extent
  117. descriptor page and ibuf bitmap page;
  118. then we do not allocate more extents */
  119. ulint space, /*!< in: space */
  120. fsp_header_t* header, /*!< in/out: space header */
  121. mtr_t* mtr) /*!< in/out: mini-transaction */
  122. UNIV_COLD;
  123. /**********************************************************************//**
  124. Allocates a single free page from a segment. This function implements
  125. the intelligent allocation strategy which tries to minimize file space
  126. fragmentation.
  127. @retval NULL if no page could be allocated
  128. @retval block, rw_lock_x_lock_count(&block->lock) == 1 if allocation succeeded
  129. (init_mtr == mtr, or the page was not previously freed in mtr)
  130. @retval block (not allocated or initialized) otherwise */
  131. static
  132. buf_block_t*
  133. fseg_alloc_free_page_low(
  134. /*=====================*/
  135. ulint space, /*!< in: space */
  136. ulint zip_size,/*!< in: compressed page size in bytes
  137. or 0 for uncompressed pages */
  138. fseg_inode_t* seg_inode, /*!< in/out: segment inode */
  139. ulint hint, /*!< in: hint of which page would be
  140. desirable */
  141. byte direction, /*!< in: if the new page is needed because
  142. of an index page split, and records are
  143. inserted there in order, into which
  144. direction they go alphabetically: FSP_DOWN,
  145. FSP_UP, FSP_NO_DIR */
  146. mtr_t* mtr, /*!< in/out: mini-transaction */
  147. mtr_t* init_mtr)/*!< in/out: mtr or another mini-transaction
  148. in which the page should be initialized.
  149. If init_mtr!=mtr, but the page is already
  150. latched in mtr, do not initialize the page. */
  151. MY_ATTRIBUTE((warn_unused_result));
  152. #endif /* !UNIV_HOTBACKUP */
  153. /**********************************************************************//**
  154. Reads the file space size stored in the header page.
  155. @return tablespace size stored in the space header */
  156. UNIV_INTERN
  157. ulint
  158. fsp_get_size_low(
  159. /*=============*/
  160. page_t* page) /*!< in: header page (page 0 in the tablespace) */
  161. {
  162. return(mach_read_from_4(page + FSP_HEADER_OFFSET + FSP_SIZE));
  163. }
  164. #ifndef UNIV_HOTBACKUP
  165. /**********************************************************************//**
  166. Gets a pointer to the space header and x-locks its page.
  167. @return pointer to the space header, page x-locked */
  168. UNIV_INLINE
  169. fsp_header_t*
  170. fsp_get_space_header(
  171. /*=================*/
  172. ulint id, /*!< in: space id */
  173. ulint zip_size,/*!< in: compressed page size in bytes
  174. or 0 for uncompressed pages */
  175. mtr_t* mtr) /*!< in/out: mini-transaction */
  176. {
  177. buf_block_t* block;
  178. fsp_header_t* header;
  179. ut_ad(ut_is_2pow(zip_size));
  180. ut_ad(zip_size <= UNIV_ZIP_SIZE_MAX);
  181. ut_ad(!zip_size || zip_size >= UNIV_ZIP_SIZE_MIN);
  182. ut_ad(id || !zip_size);
  183. block = buf_page_get(id, zip_size, 0, RW_X_LATCH, mtr);
  184. SRV_CORRUPT_TABLE_CHECK(block, return(0););
  185. header = FSP_HEADER_OFFSET + buf_block_get_frame(block);
  186. buf_block_dbg_add_level(block, SYNC_FSP_PAGE);
  187. ut_ad(id == mach_read_from_4(FSP_SPACE_ID + header));
  188. ut_ad(zip_size == fsp_flags_get_zip_size(
  189. mach_read_from_4(FSP_SPACE_FLAGS + header)));
  190. return(header);
  191. }
  192. /**********************************************************************//**
  193. Gets a descriptor bit of a page.
  194. @return TRUE if free */
  195. UNIV_INLINE
  196. ibool
  197. xdes_mtr_get_bit(
  198. /*=============*/
  199. const xdes_t* descr, /*!< in: descriptor */
  200. ulint bit, /*!< in: XDES_FREE_BIT or XDES_CLEAN_BIT */
  201. ulint offset, /*!< in: page offset within extent:
  202. 0 ... FSP_EXTENT_SIZE - 1 */
  203. mtr_t* mtr) /*!< in: mini-transaction */
  204. {
  205. ut_ad(mtr->state == MTR_ACTIVE);
  206. ut_ad(mtr_memo_contains_page(mtr, descr, MTR_MEMO_PAGE_X_FIX));
  207. return(xdes_get_bit(descr, bit, offset));
  208. }
  209. /**********************************************************************//**
  210. Sets a descriptor bit of a page. */
  211. UNIV_INLINE
  212. void
  213. xdes_set_bit(
  214. /*=========*/
  215. xdes_t* descr, /*!< in: descriptor */
  216. ulint bit, /*!< in: XDES_FREE_BIT or XDES_CLEAN_BIT */
  217. ulint offset, /*!< in: page offset within extent:
  218. 0 ... FSP_EXTENT_SIZE - 1 */
  219. ibool val, /*!< in: bit value */
  220. mtr_t* mtr) /*!< in/out: mini-transaction */
  221. {
  222. ulint index;
  223. ulint byte_index;
  224. ulint bit_index;
  225. ulint descr_byte;
  226. ut_ad(mtr_memo_contains_page(mtr, descr, MTR_MEMO_PAGE_X_FIX));
  227. ut_ad((bit == XDES_FREE_BIT) || (bit == XDES_CLEAN_BIT));
  228. ut_ad(offset < FSP_EXTENT_SIZE);
  229. index = bit + XDES_BITS_PER_PAGE * offset;
  230. byte_index = index / 8;
  231. bit_index = index % 8;
  232. descr_byte = mtr_read_ulint(descr + XDES_BITMAP + byte_index,
  233. MLOG_1BYTE, mtr);
  234. descr_byte = ut_bit_set_nth(descr_byte, bit_index, val);
  235. mlog_write_ulint(descr + XDES_BITMAP + byte_index, descr_byte,
  236. MLOG_1BYTE, mtr);
  237. }
  238. /**********************************************************************//**
  239. Looks for a descriptor bit having the desired value. Starts from hint
  240. and scans upward; at the end of the extent the search is wrapped to
  241. the start of the extent.
  242. @return bit index of the bit, ULINT_UNDEFINED if not found */
  243. UNIV_INLINE
  244. ulint
  245. xdes_find_bit(
  246. /*==========*/
  247. xdes_t* descr, /*!< in: descriptor */
  248. ulint bit, /*!< in: XDES_FREE_BIT or XDES_CLEAN_BIT */
  249. ibool val, /*!< in: desired bit value */
  250. ulint hint, /*!< in: hint of which bit position would
  251. be desirable */
  252. mtr_t* mtr) /*!< in/out: mini-transaction */
  253. {
  254. ulint i;
  255. ut_ad(descr && mtr);
  256. ut_ad(val <= TRUE);
  257. ut_ad(hint < FSP_EXTENT_SIZE);
  258. ut_ad(mtr_memo_contains_page(mtr, descr, MTR_MEMO_PAGE_X_FIX));
  259. for (i = hint; i < FSP_EXTENT_SIZE; i++) {
  260. if (val == xdes_mtr_get_bit(descr, bit, i, mtr)) {
  261. return(i);
  262. }
  263. }
  264. for (i = 0; i < hint; i++) {
  265. if (val == xdes_mtr_get_bit(descr, bit, i, mtr)) {
  266. return(i);
  267. }
  268. }
  269. return(ULINT_UNDEFINED);
  270. }
  271. /**********************************************************************//**
  272. Returns the number of used pages in a descriptor.
  273. @return number of pages used */
  274. UNIV_INLINE
  275. ulint
  276. xdes_get_n_used(
  277. /*============*/
  278. const xdes_t* descr, /*!< in: descriptor */
  279. mtr_t* mtr) /*!< in/out: mini-transaction */
  280. {
  281. ulint count = 0;
  282. ut_ad(descr && mtr);
  283. ut_ad(mtr_memo_contains_page(mtr, descr, MTR_MEMO_PAGE_X_FIX));
  284. for (ulint i = 0; i < FSP_EXTENT_SIZE; ++i) {
  285. if (FALSE == xdes_mtr_get_bit(descr, XDES_FREE_BIT, i, mtr)) {
  286. count++;
  287. }
  288. }
  289. return(count);
  290. }
  291. /**********************************************************************//**
  292. Returns true if extent contains no used pages.
  293. @return TRUE if totally free */
  294. UNIV_INLINE
  295. ibool
  296. xdes_is_free(
  297. /*=========*/
  298. const xdes_t* descr, /*!< in: descriptor */
  299. mtr_t* mtr) /*!< in/out: mini-transaction */
  300. {
  301. if (0 == xdes_get_n_used(descr, mtr)) {
  302. return(TRUE);
  303. }
  304. return(FALSE);
  305. }
  306. /**********************************************************************//**
  307. Returns true if extent contains no free pages.
  308. @return TRUE if full */
  309. UNIV_INLINE
  310. ibool
  311. xdes_is_full(
  312. /*=========*/
  313. const xdes_t* descr, /*!< in: descriptor */
  314. mtr_t* mtr) /*!< in/out: mini-transaction */
  315. {
  316. if (FSP_EXTENT_SIZE == xdes_get_n_used(descr, mtr)) {
  317. return(TRUE);
  318. }
  319. return(FALSE);
  320. }
  321. /**********************************************************************//**
  322. Sets the state of an xdes. */
  323. UNIV_INLINE
  324. void
  325. xdes_set_state(
  326. /*===========*/
  327. xdes_t* descr, /*!< in/out: descriptor */
  328. ulint state, /*!< in: state to set */
  329. mtr_t* mtr) /*!< in/out: mini-transaction */
  330. {
  331. ut_ad(descr && mtr);
  332. ut_ad(state >= XDES_FREE);
  333. ut_ad(state <= XDES_FSEG);
  334. ut_ad(mtr_memo_contains_page(mtr, descr, MTR_MEMO_PAGE_X_FIX));
  335. mlog_write_ulint(descr + XDES_STATE, state, MLOG_4BYTES, mtr);
  336. }
  337. /**********************************************************************//**
  338. Gets the state of an xdes.
  339. @return state */
  340. UNIV_INLINE
  341. ulint
  342. xdes_get_state(
  343. /*===========*/
  344. const xdes_t* descr, /*!< in: descriptor */
  345. mtr_t* mtr) /*!< in/out: mini-transaction */
  346. {
  347. ulint state;
  348. ut_ad(descr && mtr);
  349. ut_ad(mtr_memo_contains_page(mtr, descr, MTR_MEMO_PAGE_X_FIX));
  350. state = mtr_read_ulint(descr + XDES_STATE, MLOG_4BYTES, mtr);
  351. ut_ad(state - 1 < XDES_FSEG);
  352. return(state);
  353. }
  354. /**********************************************************************//**
  355. Inits an extent descriptor to the free and clean state. */
  356. UNIV_INLINE
  357. void
  358. xdes_init(
  359. /*======*/
  360. xdes_t* descr, /*!< in: descriptor */
  361. mtr_t* mtr) /*!< in/out: mini-transaction */
  362. {
  363. ulint i;
  364. ut_ad(descr && mtr);
  365. ut_ad(mtr_memo_contains_page(mtr, descr, MTR_MEMO_PAGE_X_FIX));
  366. ut_ad((XDES_SIZE - XDES_BITMAP) % 4 == 0);
  367. for (i = XDES_BITMAP; i < XDES_SIZE; i += 4) {
  368. mlog_write_ulint(descr + i, 0xFFFFFFFFUL, MLOG_4BYTES, mtr);
  369. }
  370. xdes_set_state(descr, XDES_FREE, mtr);
  371. }
  372. /********************************************************************//**
  373. Gets pointer to a the extent descriptor of a page. The page where the extent
  374. descriptor resides is x-locked. This function no longer extends the data
  375. file.
  376. @return pointer to the extent descriptor, NULL if the page does not
  377. exist in the space or if the offset is >= the free limit */
  378. UNIV_INLINE MY_ATTRIBUTE((nonnull, warn_unused_result))
  379. xdes_t*
  380. xdes_get_descriptor_with_space_hdr(
  381. /*===============================*/
  382. fsp_header_t* sp_header, /*!< in/out: space header, x-latched
  383. in mtr */
  384. ulint space, /*!< in: space id */
  385. ulint offset, /*!< in: page offset; if equal
  386. to the free limit, we try to
  387. add new extents to the space
  388. free list */
  389. mtr_t* mtr) /*!< in/out: mini-transaction */
  390. {
  391. ulint limit;
  392. ulint size;
  393. ulint zip_size;
  394. ulint descr_page_no;
  395. page_t* descr_page;
  396. ut_ad(mtr_memo_contains(mtr, fil_space_get_latch(space, NULL),
  397. MTR_MEMO_X_LOCK));
  398. ut_ad(mtr_memo_contains_page(mtr, sp_header, MTR_MEMO_PAGE_X_FIX));
  399. ut_ad(page_offset(sp_header) == FSP_HEADER_OFFSET);
  400. /* Read free limit and space size */
  401. limit = mach_read_from_4(sp_header + FSP_FREE_LIMIT);
  402. size = mach_read_from_4(sp_header + FSP_SIZE);
  403. zip_size = fsp_flags_get_zip_size(
  404. mach_read_from_4(sp_header + FSP_SPACE_FLAGS));
  405. if ((offset >= size) || (offset >= limit)) {
  406. return(NULL);
  407. }
  408. descr_page_no = xdes_calc_descriptor_page(zip_size, offset);
  409. if (descr_page_no == 0) {
  410. /* It is on the space header page */
  411. descr_page = page_align(sp_header);
  412. } else {
  413. buf_block_t* block;
  414. block = buf_page_get(space, zip_size, descr_page_no,
  415. RW_X_LATCH, mtr);
  416. buf_block_dbg_add_level(block, SYNC_FSP_PAGE);
  417. descr_page = buf_block_get_frame(block);
  418. }
  419. return(descr_page + XDES_ARR_OFFSET
  420. + XDES_SIZE * xdes_calc_descriptor_index(zip_size, offset));
  421. }
  422. /********************************************************************//**
  423. Gets pointer to a the extent descriptor of a page. The page where the extent
  424. descriptor resides is x-locked. This function no longer extends the data
  425. file.
  426. @return pointer to the extent descriptor, NULL if the page does not
  427. exist in the space or if the offset exceeds the free limit */
  428. static MY_ATTRIBUTE((nonnull, warn_unused_result))
  429. xdes_t*
  430. xdes_get_descriptor(
  431. /*================*/
  432. ulint space, /*!< in: space id */
  433. ulint zip_size,/*!< in: compressed page size in bytes
  434. or 0 for uncompressed pages */
  435. ulint offset, /*!< in: page offset; if equal to the free limit,
  436. we try to add new extents to the space free list */
  437. mtr_t* mtr) /*!< in/out: mini-transaction */
  438. {
  439. buf_block_t* block;
  440. fsp_header_t* sp_header;
  441. block = buf_page_get(space, zip_size, 0, RW_X_LATCH, mtr);
  442. SRV_CORRUPT_TABLE_CHECK(block, return(0););
  443. buf_block_dbg_add_level(block, SYNC_FSP_PAGE);
  444. sp_header = FSP_HEADER_OFFSET + buf_block_get_frame(block);
  445. return(xdes_get_descriptor_with_space_hdr(sp_header, space, offset,
  446. mtr));
  447. }
  448. /********************************************************************//**
  449. Gets pointer to a the extent descriptor if the file address
  450. of the descriptor list node is known. The page where the
  451. extent descriptor resides is x-locked.
  452. @return pointer to the extent descriptor */
  453. UNIV_INLINE
  454. xdes_t*
  455. xdes_lst_get_descriptor(
  456. /*====================*/
  457. ulint space, /*!< in: space id */
  458. ulint zip_size,/*!< in: compressed page size in bytes
  459. or 0 for uncompressed pages */
  460. fil_addr_t lst_node,/*!< in: file address of the list node
  461. contained in the descriptor */
  462. mtr_t* mtr) /*!< in/out: mini-transaction */
  463. {
  464. xdes_t* descr;
  465. ut_ad(mtr);
  466. ut_ad(mtr_memo_contains(mtr, fil_space_get_latch(space, NULL),
  467. MTR_MEMO_X_LOCK));
  468. descr = fut_get_ptr(space, zip_size, lst_node, RW_X_LATCH, mtr)
  469. - XDES_FLST_NODE;
  470. return(descr);
  471. }
  472. /********************************************************************//**
  473. Returns page offset of the first page in extent described by a descriptor.
  474. @return offset of the first page in extent */
  475. UNIV_INLINE
  476. ulint
  477. xdes_get_offset(
  478. /*============*/
  479. const xdes_t* descr) /*!< in: extent descriptor */
  480. {
  481. ut_ad(descr);
  482. return(page_get_page_no(page_align(descr))
  483. + ((page_offset(descr) - XDES_ARR_OFFSET) / XDES_SIZE)
  484. * FSP_EXTENT_SIZE);
  485. }
  486. #endif /* !UNIV_HOTBACKUP */
  487. /***********************************************************//**
  488. Inits a file page whose prior contents should be ignored. */
  489. static
  490. void
  491. fsp_init_file_page_low(
  492. /*===================*/
  493. buf_block_t* block) /*!< in: pointer to a page */
  494. {
  495. page_t* page = buf_block_get_frame(block);
  496. page_zip_des_t* page_zip= buf_block_get_page_zip(block);
  497. #ifndef UNIV_HOTBACKUP
  498. block->check_index_page_at_flush = FALSE;
  499. #endif /* !UNIV_HOTBACKUP */
  500. if (page_zip) {
  501. memset(page, 0, UNIV_PAGE_SIZE);
  502. memset(page_zip->data, 0, page_zip_get_size(page_zip));
  503. mach_write_to_4(page + FIL_PAGE_OFFSET,
  504. buf_block_get_page_no(block));
  505. mach_write_to_4(page
  506. + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID,
  507. buf_block_get_space(block));
  508. memcpy(page_zip->data + FIL_PAGE_OFFSET,
  509. page + FIL_PAGE_OFFSET, 4);
  510. memcpy(page_zip->data + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID,
  511. page + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID, 4);
  512. return;
  513. }
  514. memset(page, 0, UNIV_PAGE_SIZE);
  515. mach_write_to_4(page + FIL_PAGE_OFFSET, buf_block_get_page_no(block));
  516. mach_write_to_4(page + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID,
  517. buf_block_get_space(block));
  518. }
  519. #ifndef UNIV_HOTBACKUP
  520. /***********************************************************//**
  521. Inits a file page whose prior contents should be ignored. */
  522. static
  523. void
  524. fsp_init_file_page(
  525. /*===============*/
  526. buf_block_t* block, /*!< in: pointer to a page */
  527. mtr_t* mtr) /*!< in/out: mini-transaction */
  528. {
  529. fsp_init_file_page_low(block);
  530. mlog_write_initial_log_record(buf_block_get_frame(block),
  531. MLOG_INIT_FILE_PAGE, mtr);
  532. }
  533. #endif /* !UNIV_HOTBACKUP */
  534. /***********************************************************//**
  535. Parses a redo log record of a file page init.
  536. @return end of log record or NULL */
  537. UNIV_INTERN
  538. byte*
  539. fsp_parse_init_file_page(
  540. /*=====================*/
  541. byte* ptr, /*!< in: buffer */
  542. byte* end_ptr MY_ATTRIBUTE((unused)), /*!< in: buffer end */
  543. buf_block_t* block) /*!< in: block or NULL */
  544. {
  545. ut_ad(ptr && end_ptr);
  546. if (block) {
  547. fsp_init_file_page_low(block);
  548. }
  549. return(ptr);
  550. }
  551. /**********************************************************************//**
  552. Initializes the fsp system. */
  553. UNIV_INTERN
  554. void
  555. fsp_init(void)
  556. /*==========*/
  557. {
  558. /* FSP_EXTENT_SIZE must be a multiple of page & zip size */
  559. ut_a(0 == (UNIV_PAGE_SIZE % FSP_EXTENT_SIZE));
  560. ut_a(UNIV_PAGE_SIZE);
  561. #if UNIV_PAGE_SIZE_MAX % FSP_EXTENT_SIZE_MAX
  562. # error "UNIV_PAGE_SIZE_MAX % FSP_EXTENT_SIZE_MAX != 0"
  563. #endif
  564. #if UNIV_ZIP_SIZE_MIN % FSP_EXTENT_SIZE_MIN
  565. # error "UNIV_ZIP_SIZE_MIN % FSP_EXTENT_SIZE_MIN != 0"
  566. #endif
  567. /* Does nothing at the moment */
  568. }
  569. /**********************************************************************//**
  570. Writes the space id and flags to a tablespace header. The flags contain
  571. row type, physical/compressed page size, and logical/uncompressed page
  572. size of the tablespace. */
  573. UNIV_INTERN
  574. void
  575. fsp_header_init_fields(
  576. /*===================*/
  577. page_t* page, /*!< in/out: first page in the space */
  578. ulint space_id, /*!< in: space id */
  579. ulint flags) /*!< in: tablespace flags (FSP_SPACE_FLAGS) */
  580. {
  581. ut_a(fsp_flags_is_valid(flags));
  582. mach_write_to_4(FSP_HEADER_OFFSET + FSP_SPACE_ID + page,
  583. space_id);
  584. mach_write_to_4(FSP_HEADER_OFFSET + FSP_SPACE_FLAGS + page,
  585. flags);
  586. }
  587. #ifndef UNIV_HOTBACKUP
  588. /**********************************************************************//**
  589. Initializes the space header of a new created space and creates also the
  590. insert buffer tree root if space == 0. */
  591. UNIV_INTERN
  592. void
  593. fsp_header_init(
  594. /*============*/
  595. ulint space, /*!< in: space id */
  596. ulint size, /*!< in: current size in blocks */
  597. mtr_t* mtr) /*!< in/out: mini-transaction */
  598. {
  599. fsp_header_t* header;
  600. buf_block_t* block;
  601. page_t* page;
  602. ulint flags;
  603. ulint zip_size;
  604. ut_ad(mtr);
  605. mtr_x_lock(fil_space_get_latch(space, &flags), mtr);
  606. zip_size = fsp_flags_get_zip_size(flags);
  607. block = buf_page_create(space, 0, zip_size, mtr);
  608. buf_page_get(space, zip_size, 0, RW_X_LATCH, mtr);
  609. buf_block_dbg_add_level(block, SYNC_FSP_PAGE);
  610. /* The prior contents of the file page should be ignored */
  611. fsp_init_file_page(block, mtr);
  612. page = buf_block_get_frame(block);
  613. mlog_write_ulint(page + FIL_PAGE_TYPE, FIL_PAGE_TYPE_FSP_HDR,
  614. MLOG_2BYTES, mtr);
  615. header = FSP_HEADER_OFFSET + page;
  616. mlog_write_ulint(header + FSP_SPACE_ID, space, MLOG_4BYTES, mtr);
  617. mlog_write_ulint(header + FSP_NOT_USED, 0, MLOG_4BYTES, mtr);
  618. mlog_write_ulint(header + FSP_SIZE, size, MLOG_4BYTES, mtr);
  619. mlog_write_ulint(header + FSP_FREE_LIMIT, 0, MLOG_4BYTES, mtr);
  620. mlog_write_ulint(header + FSP_SPACE_FLAGS, flags,
  621. MLOG_4BYTES, mtr);
  622. mlog_write_ulint(header + FSP_FRAG_N_USED, 0, MLOG_4BYTES, mtr);
  623. flst_init(header + FSP_FREE, mtr);
  624. flst_init(header + FSP_FREE_FRAG, mtr);
  625. flst_init(header + FSP_FULL_FRAG, mtr);
  626. flst_init(header + FSP_SEG_INODES_FULL, mtr);
  627. flst_init(header + FSP_SEG_INODES_FREE, mtr);
  628. mlog_write_ull(header + FSP_SEG_ID, 1, mtr);
  629. if (space == 0) {
  630. fsp_fill_free_list(FALSE, space, header, mtr);
  631. btr_create(DICT_CLUSTERED | DICT_UNIVERSAL | DICT_IBUF,
  632. 0, 0, DICT_IBUF_ID_MIN + space,
  633. dict_ind_redundant, mtr);
  634. } else {
  635. fsp_fill_free_list(TRUE, space, header, mtr);
  636. }
  637. }
  638. #endif /* !UNIV_HOTBACKUP */
  639. /**********************************************************************//**
  640. Reads the space id from the first page of a tablespace.
  641. @return space id, ULINT UNDEFINED if error */
  642. UNIV_INTERN
  643. ulint
  644. fsp_header_get_space_id(
  645. /*====================*/
  646. const page_t* page) /*!< in: first page of a tablespace */
  647. {
  648. ulint fsp_id;
  649. ulint id;
  650. fsp_id = mach_read_from_4(FSP_HEADER_OFFSET + page + FSP_SPACE_ID);
  651. id = mach_read_from_4(page + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID);
  652. DBUG_EXECUTE_IF("fsp_header_get_space_id_failure",
  653. id = ULINT_UNDEFINED;);
  654. if (id != fsp_id) {
  655. ib_logf(IB_LOG_LEVEL_ERROR,
  656. "Space id in fsp header %lu,but in the page header "
  657. "%lu", fsp_id, id);
  658. return(ULINT_UNDEFINED);
  659. }
  660. return(id);
  661. }
  662. /**********************************************************************//**
  663. Reads the space flags from the first page of a tablespace.
  664. @return flags */
  665. UNIV_INTERN
  666. ulint
  667. fsp_header_get_flags(
  668. /*=================*/
  669. const page_t* page) /*!< in: first page of a tablespace */
  670. {
  671. ut_ad(!page_offset(page));
  672. return(mach_read_from_4(FSP_HEADER_OFFSET + FSP_SPACE_FLAGS + page));
  673. }
  674. /**********************************************************************//**
  675. Reads the compressed page size from the first page of a tablespace.
  676. @return compressed page size in bytes, or 0 if uncompressed */
  677. UNIV_INTERN
  678. ulint
  679. fsp_header_get_zip_size(
  680. /*====================*/
  681. const page_t* page) /*!< in: first page of a tablespace */
  682. {
  683. ulint flags = fsp_header_get_flags(page);
  684. return(fsp_flags_get_zip_size(flags));
  685. }
  686. #ifndef UNIV_HOTBACKUP
  687. /**********************************************************************//**
  688. Increases the space size field of a space. */
  689. UNIV_INTERN
  690. void
  691. fsp_header_inc_size(
  692. /*================*/
  693. ulint space, /*!< in: space id */
  694. ulint size_inc, /*!< in: size increment in pages */
  695. mtr_t* mtr) /*!< in/out: mini-transaction */
  696. {
  697. fsp_header_t* header;
  698. ulint size;
  699. ulint flags;
  700. ut_ad(mtr);
  701. mtr_x_lock(fil_space_get_latch(space, &flags), mtr);
  702. header = fsp_get_space_header(space,
  703. fsp_flags_get_zip_size(flags),
  704. mtr);
  705. size = mtr_read_ulint(header + FSP_SIZE, MLOG_4BYTES, mtr);
  706. mlog_write_ulint(header + FSP_SIZE, size + size_inc, MLOG_4BYTES,
  707. mtr);
  708. }
  709. /**********************************************************************//**
  710. Gets the size of the system tablespace from the tablespace header. If
  711. we do not have an auto-extending data file, this should be equal to
  712. the size of the data files. If there is an auto-extending data file,
  713. this can be smaller.
  714. @return size in pages */
  715. UNIV_INTERN
  716. ulint
  717. fsp_header_get_tablespace_size(void)
  718. /*================================*/
  719. {
  720. fsp_header_t* header;
  721. ulint size;
  722. mtr_t mtr;
  723. mtr_start(&mtr);
  724. mtr_x_lock(fil_space_get_latch(0, NULL), &mtr);
  725. header = fsp_get_space_header(0, 0, &mtr);
  726. size = mtr_read_ulint(header + FSP_SIZE, MLOG_4BYTES, &mtr);
  727. mtr_commit(&mtr);
  728. return(size);
  729. }
  730. /***********************************************************************//**
  731. Tries to extend a single-table tablespace so that a page would fit in the
  732. data file.
  733. @return TRUE if success */
  734. static UNIV_COLD MY_ATTRIBUTE((nonnull, warn_unused_result))
  735. ibool
  736. fsp_try_extend_data_file_with_pages(
  737. /*================================*/
  738. ulint space, /*!< in: space */
  739. ulint page_no, /*!< in: page number */
  740. fsp_header_t* header, /*!< in/out: space header */
  741. mtr_t* mtr) /*!< in/out: mini-transaction */
  742. {
  743. ibool success;
  744. ulint actual_size;
  745. ulint size;
  746. ut_a(space != 0);
  747. size = mtr_read_ulint(header + FSP_SIZE, MLOG_4BYTES, mtr);
  748. ut_a(page_no >= size);
  749. success = fil_extend_space_to_desired_size(&actual_size, space,
  750. page_no + 1);
  751. /* actual_size now has the space size in pages; it may be less than
  752. we wanted if we ran out of disk space */
  753. mlog_write_ulint(header + FSP_SIZE, actual_size, MLOG_4BYTES, mtr);
  754. return(success);
  755. }
  756. /***********************************************************************//**
  757. Tries to extend the last data file of a tablespace if it is auto-extending.
  758. @return FALSE if not auto-extending */
  759. static UNIV_COLD MY_ATTRIBUTE((nonnull))
  760. ibool
  761. fsp_try_extend_data_file(
  762. /*=====================*/
  763. ulint* actual_increase,/*!< out: actual increase in pages, where
  764. we measure the tablespace size from
  765. what the header field says; it may be
  766. the actual file size rounded down to
  767. megabyte */
  768. ulint space, /*!< in: space */
  769. fsp_header_t* header, /*!< in/out: space header */
  770. mtr_t* mtr) /*!< in/out: mini-transaction */
  771. {
  772. ulint size;
  773. ulint zip_size;
  774. ulint new_size;
  775. ulint old_size;
  776. ulint size_increase;
  777. ulint actual_size;
  778. ibool success;
  779. *actual_increase = 0;
  780. if (space == 0 && !srv_auto_extend_last_data_file) {
  781. /* We print the error message only once to avoid
  782. spamming the error log. Note that we don't need
  783. to reset the flag to FALSE as dealing with this
  784. error requires server restart. */
  785. if (fsp_tbs_full_error_printed == FALSE) {
  786. fprintf(stderr,
  787. "InnoDB: Error: Data file(s) ran"
  788. " out of space.\n"
  789. "Please add another data file or"
  790. " use \'autoextend\' for the last"
  791. " data file.\n");
  792. fsp_tbs_full_error_printed = TRUE;
  793. }
  794. return(FALSE);
  795. }
  796. size = mtr_read_ulint(header + FSP_SIZE, MLOG_4BYTES, mtr);
  797. zip_size = fsp_flags_get_zip_size(
  798. mach_read_from_4(header + FSP_SPACE_FLAGS));
  799. old_size = size;
  800. if (space == 0) {
  801. if (!srv_last_file_size_max) {
  802. size_increase = SRV_AUTO_EXTEND_INCREMENT;
  803. } else {
  804. if (srv_last_file_size_max
  805. < srv_data_file_sizes[srv_n_data_files - 1]) {
  806. fprintf(stderr,
  807. "InnoDB: Error: Last data file size"
  808. " is %lu, max size allowed %lu\n",
  809. (ulong) srv_data_file_sizes[
  810. srv_n_data_files - 1],
  811. (ulong) srv_last_file_size_max);
  812. }
  813. size_increase = srv_last_file_size_max
  814. - srv_data_file_sizes[srv_n_data_files - 1];
  815. if (size_increase > SRV_AUTO_EXTEND_INCREMENT) {
  816. size_increase = SRV_AUTO_EXTEND_INCREMENT;
  817. }
  818. }
  819. } else {
  820. /* We extend single-table tablespaces first one extent
  821. at a time, but 4 at a time for bigger tablespaces. It is
  822. not enough to extend always by one extent, because we need
  823. to add at least one extent to FSP_FREE.
  824. A single extent descriptor page will track many extents.
  825. And the extent that uses its extent descriptor page is
  826. put onto the FSP_FREE_FRAG list. Extents that do not
  827. use their extent descriptor page are added to FSP_FREE.
  828. The physical page size is used to determine how many
  829. extents are tracked on one extent descriptor page. */
  830. ulint extent_size; /*!< one megabyte, in pages */
  831. ulint threshold; /*!< The size of the tablespace
  832. (in number of pages) where we
  833. start allocating more than one
  834. extent at a time. */
  835. if (!zip_size) {
  836. extent_size = FSP_EXTENT_SIZE;
  837. } else {
  838. extent_size = FSP_EXTENT_SIZE
  839. * UNIV_PAGE_SIZE / zip_size;
  840. }
  841. /* Threshold is set at 32mb except when the page
  842. size is small enough that it must be done sooner.
  843. For page size less than 4k, we may reach the
  844. extent contains extent descriptor page before
  845. 32 mb. */
  846. threshold = ut_min((32 * extent_size),
  847. (zip_size ? zip_size : UNIV_PAGE_SIZE));
  848. if (size < extent_size) {
  849. /* Let us first extend the file to extent_size */
  850. success = fsp_try_extend_data_file_with_pages(
  851. space, extent_size - 1, header, mtr);
  852. if (!success) {
  853. new_size = mtr_read_ulint(header + FSP_SIZE,
  854. MLOG_4BYTES, mtr);
  855. *actual_increase = new_size - old_size;
  856. return(FALSE);
  857. }
  858. size = extent_size;
  859. }
  860. if (size < threshold) {
  861. size_increase = extent_size;
  862. } else {
  863. /* Below in fsp_fill_free_list() we assume
  864. that we add at most FSP_FREE_ADD extents at
  865. a time */
  866. size_increase = FSP_FREE_ADD * extent_size;
  867. }
  868. }
  869. if (size_increase == 0) {
  870. return(TRUE);
  871. }
  872. success = fil_extend_space_to_desired_size(&actual_size, space,
  873. size + size_increase);
  874. if (!success) {
  875. return(false);
  876. }
  877. /* We ignore any fragments of a full megabyte when storing the size
  878. to the space header */
  879. if (!zip_size) {
  880. new_size = ut_calc_align_down(actual_size,
  881. (1024 * 1024) / UNIV_PAGE_SIZE);
  882. } else {
  883. new_size = ut_calc_align_down(actual_size,
  884. (1024 * 1024) / zip_size);
  885. }
  886. mlog_write_ulint(header + FSP_SIZE, new_size, MLOG_4BYTES, mtr);
  887. *actual_increase = new_size - old_size;
  888. return(TRUE);
  889. }
  890. /**********************************************************************//**
  891. Puts new extents to the free list if there are free extents above the free
  892. limit. If an extent happens to contain an extent descriptor page, the extent
  893. is put to the FSP_FREE_FRAG list with the page marked as used. */
  894. static
  895. void
  896. fsp_fill_free_list(
  897. /*===============*/
  898. ibool init_space, /*!< in: TRUE if this is a single-table
  899. tablespace and we are only initing
  900. the tablespace's first extent
  901. descriptor page and ibuf bitmap page;
  902. then we do not allocate more extents */
  903. ulint space, /*!< in: space */
  904. fsp_header_t* header, /*!< in/out: space header */
  905. mtr_t* mtr) /*!< in/out: mini-transaction */
  906. {
  907. ulint limit;
  908. ulint size;
  909. ulint zip_size;
  910. xdes_t* descr;
  911. ulint count = 0;
  912. ulint frag_n_used;
  913. ulint actual_increase;
  914. ulint i;
  915. mtr_t ibuf_mtr;
  916. ut_ad(page_offset(header) == FSP_HEADER_OFFSET);
  917. /* Check if we can fill free list from above the free list limit */
  918. size = mtr_read_ulint(header + FSP_SIZE, MLOG_4BYTES, mtr);
  919. limit = mtr_read_ulint(header + FSP_FREE_LIMIT, MLOG_4BYTES, mtr);
  920. zip_size = fsp_flags_get_zip_size(
  921. mach_read_from_4(FSP_SPACE_FLAGS + header));
  922. ut_a(ut_is_2pow(zip_size));
  923. ut_a(zip_size <= UNIV_ZIP_SIZE_MAX);
  924. ut_a(!zip_size || zip_size >= UNIV_ZIP_SIZE_MIN);
  925. if (space == 0 && srv_auto_extend_last_data_file
  926. && size < limit + FSP_EXTENT_SIZE * FSP_FREE_ADD) {
  927. /* Try to increase the last data file size */
  928. fsp_try_extend_data_file(&actual_increase, space, header, mtr);
  929. size = mtr_read_ulint(header + FSP_SIZE, MLOG_4BYTES, mtr);
  930. }
  931. if (space != 0 && !init_space
  932. && size < limit + FSP_EXTENT_SIZE * FSP_FREE_ADD) {
  933. /* Try to increase the .ibd file size */
  934. fsp_try_extend_data_file(&actual_increase, space, header, mtr);
  935. size = mtr_read_ulint(header + FSP_SIZE, MLOG_4BYTES, mtr);
  936. }
  937. i = limit;
  938. while ((init_space && i < 1)
  939. || ((i + FSP_EXTENT_SIZE <= size) && (count < FSP_FREE_ADD))) {
  940. ibool init_xdes;
  941. if (zip_size) {
  942. init_xdes = ut_2pow_remainder(i, zip_size) == 0;
  943. } else {
  944. init_xdes = ut_2pow_remainder(i, UNIV_PAGE_SIZE) == 0;
  945. }
  946. mlog_write_ulint(header + FSP_FREE_LIMIT, i + FSP_EXTENT_SIZE,
  947. MLOG_4BYTES, mtr);
  948. if (UNIV_UNLIKELY(init_xdes)) {
  949. buf_block_t* block;
  950. /* We are going to initialize a new descriptor page
  951. and a new ibuf bitmap page: the prior contents of the
  952. pages should be ignored. */
  953. if (i > 0) {
  954. block = buf_page_create(
  955. space, i, zip_size, mtr);
  956. buf_page_get(space, zip_size, i,
  957. RW_X_LATCH, mtr);
  958. buf_block_dbg_add_level(block,
  959. SYNC_FSP_PAGE);
  960. fsp_init_file_page(block, mtr);
  961. mlog_write_ulint(buf_block_get_frame(block)
  962. + FIL_PAGE_TYPE,
  963. FIL_PAGE_TYPE_XDES,
  964. MLOG_2BYTES, mtr);
  965. }
  966. /* Initialize the ibuf bitmap page in a separate
  967. mini-transaction because it is low in the latching
  968. order, and we must be able to release its latch
  969. before returning from the fsp routine */
  970. mtr_start(&ibuf_mtr);
  971. block = buf_page_create(space,
  972. i + FSP_IBUF_BITMAP_OFFSET,
  973. zip_size, &ibuf_mtr);
  974. buf_page_get(space, zip_size,
  975. i + FSP_IBUF_BITMAP_OFFSET,
  976. RW_X_LATCH, &ibuf_mtr);
  977. buf_block_dbg_add_level(block, SYNC_FSP_PAGE);
  978. fsp_init_file_page(block, &ibuf_mtr);
  979. ibuf_bitmap_page_init(block, &ibuf_mtr);
  980. mtr_commit(&ibuf_mtr);
  981. }
  982. descr = xdes_get_descriptor_with_space_hdr(header, space, i,
  983. mtr);
  984. xdes_init(descr, mtr);
  985. if (UNIV_UNLIKELY(init_xdes)) {
  986. /* The first page in the extent is a descriptor page
  987. and the second is an ibuf bitmap page: mark them
  988. used */
  989. xdes_set_bit(descr, XDES_FREE_BIT, 0, FALSE, mtr);
  990. xdes_set_bit(descr, XDES_FREE_BIT,
  991. FSP_IBUF_BITMAP_OFFSET, FALSE, mtr);
  992. xdes_set_state(descr, XDES_FREE_FRAG, mtr);
  993. flst_add_last(header + FSP_FREE_FRAG,
  994. descr + XDES_FLST_NODE, mtr);
  995. frag_n_used = mtr_read_ulint(header + FSP_FRAG_N_USED,
  996. MLOG_4BYTES, mtr);
  997. mlog_write_ulint(header + FSP_FRAG_N_USED,
  998. frag_n_used + 2, MLOG_4BYTES, mtr);
  999. } else {
  1000. flst_add_last(header + FSP_FREE,
  1001. descr + XDES_FLST_NODE, mtr);
  1002. count++;
  1003. }
  1004. i += FSP_EXTENT_SIZE;
  1005. }
  1006. }
  1007. /**********************************************************************//**
  1008. Allocates a new free extent.
  1009. @return extent descriptor, NULL if cannot be allocated */
  1010. static
  1011. xdes_t*
  1012. fsp_alloc_free_extent(
  1013. /*==================*/
  1014. ulint space, /*!< in: space id */
  1015. ulint zip_size,/*!< in: compressed page size in bytes
  1016. or 0 for uncompressed pages */
  1017. ulint hint, /*!< in: hint of which extent would be desirable: any
  1018. page offset in the extent goes; the hint must not
  1019. be > FSP_FREE_LIMIT */
  1020. mtr_t* mtr) /*!< in/out: mini-transaction */
  1021. {
  1022. fsp_header_t* header;
  1023. fil_addr_t first;
  1024. xdes_t* descr;
  1025. ut_ad(mtr);
  1026. header = fsp_get_space_header(space, zip_size, mtr);
  1027. descr = xdes_get_descriptor_with_space_hdr(header, space, hint, mtr);
  1028. if (descr && (xdes_get_state(descr, mtr) == XDES_FREE)) {
  1029. /* Ok, we can take this extent */
  1030. } else {
  1031. /* Take the first extent in the free list */
  1032. first = flst_get_first(header + FSP_FREE, mtr);
  1033. if (fil_addr_is_null(first)) {
  1034. fsp_fill_free_list(FALSE, space, header, mtr);
  1035. first = flst_get_first(header + FSP_FREE, mtr);
  1036. }
  1037. if (fil_addr_is_null(first)) {
  1038. return(NULL); /* No free extents left */
  1039. }
  1040. descr = xdes_lst_get_descriptor(space, zip_size, first, mtr);
  1041. }
  1042. flst_remove(header + FSP_FREE, descr + XDES_FLST_NODE, mtr);
  1043. return(descr);
  1044. }
  1045. /**********************************************************************//**
  1046. Allocates a single free page from a space. */
  1047. static MY_ATTRIBUTE((nonnull))
  1048. void
  1049. fsp_alloc_from_free_frag(
  1050. /*=====================*/
  1051. fsp_header_t* header, /*!< in/out: tablespace header */
  1052. xdes_t* descr, /*!< in/out: extent descriptor */
  1053. ulint bit, /*!< in: slot to allocate in the extent */
  1054. mtr_t* mtr) /*!< in/out: mini-transaction */
  1055. {
  1056. ulint frag_n_used;
  1057. ut_ad(xdes_get_state(descr, mtr) == XDES_FREE_FRAG);
  1058. ut_a(xdes_mtr_get_bit(descr, XDES_FREE_BIT, bit, mtr));
  1059. xdes_set_bit(descr, XDES_FREE_BIT, bit, FALSE, mtr);
  1060. /* Update the FRAG_N_USED field */
  1061. frag_n_used = mtr_read_ulint(header + FSP_FRAG_N_USED, MLOG_4BYTES,
  1062. mtr);
  1063. frag_n_used++;
  1064. mlog_write_ulint(header + FSP_FRAG_N_USED, frag_n_used, MLOG_4BYTES,
  1065. mtr);
  1066. if (xdes_is_full(descr, mtr)) {
  1067. /* The fragment is full: move it to another list */
  1068. flst_remove(header + FSP_FREE_FRAG, descr + XDES_FLST_NODE,
  1069. mtr);
  1070. xdes_set_state(descr, XDES_FULL_FRAG, mtr);
  1071. flst_add_last(header + FSP_FULL_FRAG, descr + XDES_FLST_NODE,
  1072. mtr);
  1073. mlog_write_ulint(header + FSP_FRAG_N_USED,
  1074. frag_n_used - FSP_EXTENT_SIZE, MLOG_4BYTES,
  1075. mtr);
  1076. }
  1077. }
  1078. /**********************************************************************//**
  1079. Gets a buffer block for an allocated page.
  1080. NOTE: If init_mtr != mtr, the block will only be initialized if it was
  1081. not previously x-latched. It is assumed that the block has been
  1082. x-latched only by mtr, and freed in mtr in that case.
  1083. @return block, initialized if init_mtr==mtr
  1084. or rw_lock_x_lock_count(&block->lock) == 1 */
  1085. static
  1086. buf_block_t*
  1087. fsp_page_create(
  1088. /*============*/
  1089. ulint space, /*!< in: space id of the allocated page */
  1090. ulint zip_size, /*!< in: compressed page size in bytes
  1091. or 0 for uncompressed pages */
  1092. ulint page_no, /*!< in: page number of the allocated page */
  1093. mtr_t* mtr, /*!< in: mini-transaction of the allocation */
  1094. mtr_t* init_mtr) /*!< in: mini-transaction for initializing
  1095. the page */
  1096. {
  1097. buf_block_t* block
  1098. = buf_page_create(space, page_no, zip_size, init_mtr);
  1099. #ifdef UNIV_SYNC_DEBUG
  1100. ut_ad(mtr_memo_contains(mtr, block, MTR_MEMO_PAGE_X_FIX)
  1101. == rw_lock_own(&block->lock, RW_LOCK_EX));
  1102. #endif /* UNIV_SYNC_DEBUG */
  1103. /* Mimic buf_page_get(), but avoid the buf_pool->page_hash lookup. */
  1104. rw_lock_x_lock(&block->lock);
  1105. mutex_enter(&block->mutex);
  1106. buf_block_buf_fix_inc(block, __FILE__, __LINE__);
  1107. mutex_exit(&block->mutex);
  1108. mtr_memo_push(init_mtr, block, MTR_MEMO_PAGE_X_FIX);
  1109. if (init_mtr == mtr
  1110. || rw_lock_get_x_lock_count(&block->lock) == 1) {
  1111. /* Initialize the page, unless it was already
  1112. X-latched in mtr. (In this case, we would want to
  1113. allocate another page that has not been freed in mtr.) */
  1114. ut_ad(init_mtr == mtr
  1115. || !mtr_memo_contains(mtr, block, MTR_MEMO_PAGE_X_FIX));
  1116. fsp_init_file_page(block, init_mtr);
  1117. }
  1118. return(block);
  1119. }
  1120. /**********************************************************************//**
  1121. Allocates a single free page from a space. The page is marked as used.
  1122. @retval NULL if no page could be allocated
  1123. @retval block, rw_lock_x_lock_count(&block->lock) == 1 if allocation succeeded
  1124. (init_mtr == mtr, or the page was not previously freed in mtr)
  1125. @retval block (not allocated or initialized) otherwise */
  1126. static MY_ATTRIBUTE((warn_unused_result))
  1127. buf_block_t*
  1128. fsp_alloc_free_page(
  1129. /*================*/
  1130. ulint space, /*!< in: space id */
  1131. ulint zip_size,/*!< in: compressed page size in bytes
  1132. or 0 for uncompressed pages */
  1133. ulint hint, /*!< in: hint of which page would be desirable */
  1134. mtr_t* mtr, /*!< in/out: mini-transaction */
  1135. mtr_t* init_mtr)/*!< in/out: mini-transaction in which the
  1136. page should be initialized
  1137. (may be the same as mtr) */
  1138. {
  1139. fsp_header_t* header;
  1140. fil_addr_t first;
  1141. xdes_t* descr;
  1142. ulint free;
  1143. ulint page_no;
  1144. ulint space_size;
  1145. header = fsp_get_space_header(space, zip_size, mtr);
  1146. /* Get the hinted descriptor */
  1147. descr = xdes_get_descriptor_with_space_hdr(header, space, hint, mtr);
  1148. if (descr && (xdes_get_state(descr, mtr) == XDES_FREE_FRAG)) {
  1149. /* Ok, we can take this extent */
  1150. } else {
  1151. /* Else take the first extent in free_frag list */
  1152. first = flst_get_first(header + FSP_FREE_FRAG, mtr);
  1153. if (fil_addr_is_null(first)) {
  1154. /* There are no partially full fragments: allocate
  1155. a free extent and add it to the FREE_FRAG list. NOTE
  1156. that the allocation may have as a side-effect that an
  1157. extent containing a descriptor page is added to the
  1158. FREE_FRAG list. But we will allocate our page from the
  1159. the free extent anyway. */
  1160. descr = fsp_alloc_free_extent(space, zip_size,
  1161. hint, mtr);
  1162. if (descr == NULL) {
  1163. /* No free space left */
  1164. return(NULL);
  1165. }
  1166. xdes_set_state(descr, XDES_FREE_FRAG, mtr);
  1167. flst_add_last(header + FSP_FREE_FRAG,
  1168. descr + XDES_FLST_NODE, mtr);
  1169. } else {
  1170. descr = xdes_lst_get_descriptor(space, zip_size,
  1171. first, mtr);
  1172. }
  1173. /* Reset the hint */
  1174. hint = 0;
  1175. }
  1176. /* Now we have in descr an extent with at least one free page. Look
  1177. for a free page in the extent. */
  1178. free = xdes_find_bit(descr, XDES_FREE_BIT, TRUE,
  1179. hint % FSP_EXTENT_SIZE, mtr);
  1180. if (free == ULINT_UNDEFINED) {
  1181. ut_print_buf(stderr, ((byte*) descr) - 500, 1000);
  1182. putc('\n', stderr);
  1183. ut_error;
  1184. }
  1185. page_no = xdes_get_offset(descr) + free;
  1186. space_size = mtr_read_ulint(header + FSP_SIZE, MLOG_4BYTES, mtr);
  1187. if (space_size <= page_no) {
  1188. /* It must be that we are extending a single-table tablespace
  1189. whose size is still < 64 pages */
  1190. ut_a(space != 0);
  1191. if (page_no >= FSP_EXTENT_SIZE) {
  1192. fprintf(stderr,
  1193. "InnoDB: Error: trying to extend a"
  1194. " single-table tablespace %lu\n"
  1195. "InnoDB: by single page(s) though the"
  1196. " space size %lu. Page no %lu.\n",
  1197. (ulong) space, (ulong) space_size,
  1198. (ulong) page_no);
  1199. return(NULL);
  1200. }
  1201. if (!fsp_try_extend_data_file_with_pages(space, page_no,
  1202. header, mtr)) {
  1203. /* No disk space left */
  1204. return(NULL);
  1205. }
  1206. }
  1207. fsp_alloc_from_free_frag(header, descr, free, mtr);
  1208. return(fsp_page_create(space, zip_size, page_no, mtr, init_mtr));
  1209. }
  1210. /**********************************************************************//**
  1211. Frees a single page of a space. The page is marked as free and clean. */
  1212. static
  1213. void
  1214. fsp_free_page(
  1215. /*==========*/
  1216. ulint space, /*!< in: space id */
  1217. ulint zip_size,/*!< in: compressed page size in bytes
  1218. or 0 for uncompressed pages */
  1219. ulint page, /*!< in: page offset */
  1220. mtr_t* mtr) /*!< in/out: mini-transaction */
  1221. {
  1222. fsp_header_t* header;
  1223. xdes_t* descr;
  1224. ulint state;
  1225. ulint frag_n_used;
  1226. ut_ad(mtr);
  1227. /* fprintf(stderr, "Freeing page %lu in space %lu\n", page, space); */
  1228. header = fsp_get_space_header(space, zip_size, mtr);
  1229. descr = xdes_get_descriptor_with_space_hdr(header, space, page, mtr);
  1230. state = xdes_get_state(descr, mtr);
  1231. if (state != XDES_FREE_FRAG && state != XDES_FULL_FRAG) {
  1232. fprintf(stderr,
  1233. "InnoDB: Error: File space extent descriptor"
  1234. " of page %lu has state %lu\n",
  1235. (ulong) page,
  1236. (ulong) state);
  1237. fputs("InnoDB: Dump of descriptor: ", stderr);
  1238. ut_print_buf(stderr, ((byte*) descr) - 50, 200);
  1239. putc('\n', stderr);
  1240. /* Crash in debug version, so that we get a core dump
  1241. of this corruption. */
  1242. ut_ad(0);
  1243. if (state == XDES_FREE) {
  1244. /* We put here some fault tolerance: if the page
  1245. is already free, return without doing anything! */
  1246. return;
  1247. }
  1248. ut_error;
  1249. }
  1250. if (xdes_mtr_get_bit(descr, XDES_FREE_BIT,
  1251. page % FSP_EXTENT_SIZE, mtr)) {
  1252. fprintf(stderr,
  1253. "InnoDB: Error: File space extent descriptor"
  1254. " of page %lu says it is free\n"
  1255. "InnoDB: Dump of descriptor: ", (ulong) page);
  1256. ut_print_buf(stderr, ((byte*) descr) - 50, 200);
  1257. putc('\n', stderr);
  1258. /* Crash in debug version, so that we get a core dump
  1259. of this corruption. */
  1260. ut_ad(0);
  1261. /* We put here some fault tolerance: if the page
  1262. is already free, return without doing anything! */
  1263. return;
  1264. }
  1265. xdes_set_bit(descr, XDES_FREE_BIT, page % FSP_EXTENT_SIZE, TRUE, mtr);
  1266. xdes_set_bit(descr, XDES_CLEAN_BIT, page % FSP_EXTENT_SIZE, TRUE, mtr);
  1267. frag_n_used = mtr_read_ulint(header + FSP_FRAG_N_USED, MLOG_4BYTES,
  1268. mtr);
  1269. if (state == XDES_FULL_FRAG) {
  1270. /* The fragment was full: move it to another list */
  1271. flst_remove(header + FSP_FULL_FRAG, descr + XDES_FLST_NODE,
  1272. mtr);
  1273. xdes_set_state(descr, XDES_FREE_FRAG, mtr);
  1274. flst_add_last(header + FSP_FREE_FRAG, descr + XDES_FLST_NODE,
  1275. mtr);
  1276. mlog_write_ulint(header + FSP_FRAG_N_USED,
  1277. frag_n_used + FSP_EXTENT_SIZE - 1,
  1278. MLOG_4BYTES, mtr);
  1279. } else {
  1280. ut_a(frag_n_used > 0);
  1281. mlog_write_ulint(header + FSP_FRAG_N_USED, frag_n_used - 1,
  1282. MLOG_4BYTES, mtr);
  1283. }
  1284. if (xdes_is_free(descr, mtr)) {
  1285. /* The extent has become free: move it to another list */
  1286. flst_remove(header + FSP_FREE_FRAG, descr + XDES_FLST_NODE,
  1287. mtr);
  1288. fsp_free_extent(space, zip_size, page, mtr);
  1289. }
  1290. mtr->n_freed_pages++;
  1291. }
  1292. /**********************************************************************//**
  1293. Returns an extent to the free list of a space. */
  1294. static
  1295. void
  1296. fsp_free_extent(
  1297. /*============*/
  1298. ulint space, /*!< in: space id */
  1299. ulint zip_size,/*!< in: compressed page size in bytes
  1300. or 0 for uncompressed pages */
  1301. ulint page, /*!< in: page offset in the extent */
  1302. mtr_t* mtr) /*!< in/out: mini-transaction */
  1303. {
  1304. fsp_header_t* header;
  1305. xdes_t* descr;
  1306. ut_ad(mtr);
  1307. header = fsp_get_space_header(space, zip_size, mtr);
  1308. descr = xdes_get_descriptor_with_space_hdr(header, space, page, mtr);
  1309. if (xdes_get_state(descr, mtr) == XDES_FREE) {
  1310. ut_print_buf(stderr, (byte*) descr - 500, 1000);
  1311. putc('\n', stderr);
  1312. ut_error;
  1313. }
  1314. xdes_init(descr, mtr);
  1315. flst_add_last(header + FSP_FREE, descr + XDES_FLST_NODE, mtr);
  1316. }
  1317. /**********************************************************************//**
  1318. Returns the nth inode slot on an inode page.
  1319. @return segment inode */
  1320. UNIV_INLINE
  1321. fseg_inode_t*
  1322. fsp_seg_inode_page_get_nth_inode(
  1323. /*=============================*/
  1324. page_t* page, /*!< in: segment inode page */
  1325. ulint i, /*!< in: inode index on page */
  1326. ulint zip_size MY_ATTRIBUTE((unused)),
  1327. /*!< in: compressed page size, or 0 */
  1328. mtr_t* mtr MY_ATTRIBUTE((unused)))
  1329. /*!< in/out: mini-transaction */
  1330. {
  1331. ut_ad(i < FSP_SEG_INODES_PER_PAGE(zip_size));
  1332. ut_ad(mtr_memo_contains_page(mtr, page, MTR_MEMO_PAGE_X_FIX));
  1333. return(page + FSEG_ARR_OFFSET + FSEG_INODE_SIZE * i);
  1334. }
  1335. /**********************************************************************//**
  1336. Looks for a used segment inode on a segment inode page.
  1337. @return segment inode index, or ULINT_UNDEFINED if not found */
  1338. static
  1339. ulint
  1340. fsp_seg_inode_page_find_used(
  1341. /*=========================*/
  1342. page_t* page, /*!< in: segment inode page */
  1343. ulint zip_size,/*!< in: compressed page size, or 0 */
  1344. mtr_t* mtr) /*!< in/out: mini-transaction */
  1345. {
  1346. ulint i;
  1347. fseg_inode_t* inode;
  1348. for (i = 0; i < FSP_SEG_INODES_PER_PAGE(zip_size); i++) {
  1349. inode = fsp_seg_inode_page_get_nth_inode(
  1350. page, i, zip_size, mtr);
  1351. if (mach_read_from_8(inode + FSEG_ID)) {
  1352. /* This is used */
  1353. ut_ad(mach_read_from_4(inode + FSEG_MAGIC_N)
  1354. == FSEG_MAGIC_N_VALUE);
  1355. return(i);
  1356. }
  1357. }
  1358. return(ULINT_UNDEFINED);
  1359. }
  1360. /**********************************************************************//**
  1361. Looks for an unused segment inode on a segment inode page.
  1362. @return segment inode index, or ULINT_UNDEFINED if not found */
  1363. static
  1364. ulint
  1365. fsp_seg_inode_page_find_free(
  1366. /*=========================*/
  1367. page_t* page, /*!< in: segment inode page */
  1368. ulint i, /*!< in: search forward starting from this index */
  1369. ulint zip_size,/*!< in: compressed page size, or 0 */
  1370. mtr_t* mtr) /*!< in/out: mini-transaction */
  1371. {
  1372. SRV_CORRUPT_TABLE_CHECK(page, return(ULINT_UNDEFINED););
  1373. for (; i < FSP_SEG_INODES_PER_PAGE(zip_size); i++) {
  1374. fseg_inode_t* inode;
  1375. inode = fsp_seg_inode_page_get_nth_inode(
  1376. page, i, zip_size, mtr);
  1377. if (!mach_read_from_8(inode + FSEG_ID)) {
  1378. /* This is unused */
  1379. return(i);
  1380. }
  1381. ut_ad(mach_read_from_4(inode + FSEG_MAGIC_N)
  1382. == FSEG_MAGIC_N_VALUE);
  1383. }
  1384. return(ULINT_UNDEFINED);
  1385. }
  1386. /**********************************************************************//**
  1387. Allocates a new file segment inode page.
  1388. @return TRUE if could be allocated */
  1389. static
  1390. ibool
  1391. fsp_alloc_seg_inode_page(
  1392. /*=====================*/
  1393. fsp_header_t* space_header, /*!< in: space header */
  1394. mtr_t* mtr) /*!< in/out: mini-transaction */
  1395. {
  1396. fseg_inode_t* inode;
  1397. buf_block_t* block;
  1398. page_t* page;
  1399. ulint space;
  1400. ulint zip_size;
  1401. ut_ad(page_offset(space_header) == FSP_HEADER_OFFSET);
  1402. space = page_get_space_id(page_align(space_header));
  1403. zip_size = fsp_flags_get_zip_size(
  1404. mach_read_from_4(FSP_SPACE_FLAGS + space_header));
  1405. block = fsp_alloc_free_page(space, zip_size, 0, mtr, mtr);
  1406. if (block == NULL) {
  1407. return(FALSE);
  1408. }
  1409. buf_block_dbg_add_level(block, SYNC_FSP_PAGE);
  1410. ut_ad(rw_lock_get_x_lock_count(&block->lock) == 1);
  1411. block->check_index_page_at_flush = FALSE;
  1412. page = buf_block_get_frame(block);
  1413. mlog_write_ulint(page + FIL_PAGE_TYPE, FIL_PAGE_INODE,
  1414. MLOG_2BYTES, mtr);
  1415. for (ulint i = 0; i < FSP_SEG_INODES_PER_PAGE(zip_size); i++) {
  1416. inode = fsp_seg_inode_page_get_nth_inode(
  1417. page, i, zip_size, mtr);
  1418. mlog_write_ull(inode + FSEG_ID, 0, mtr);
  1419. }
  1420. flst_add_last(
  1421. space_header + FSP_SEG_INODES_FREE,
  1422. page + FSEG_INODE_PAGE_NODE, mtr);
  1423. return(TRUE);
  1424. }
  1425. /**********************************************************************//**
  1426. Allocates a new file segment inode.
  1427. @return segment inode, or NULL if not enough space */
  1428. static
  1429. fseg_inode_t*
  1430. fsp_alloc_seg_inode(
  1431. /*================*/
  1432. fsp_header_t* space_header, /*!< in: space header */
  1433. mtr_t* mtr) /*!< in/out: mini-transaction */
  1434. {
  1435. ulint page_no;
  1436. buf_block_t* block;
  1437. page_t* page;
  1438. fseg_inode_t* inode;
  1439. ibool success;
  1440. ulint zip_size;
  1441. ulint n;
  1442. ut_ad(page_offset(space_header) == FSP_HEADER_OFFSET);
  1443. if (flst_get_len(space_header + FSP_SEG_INODES_FREE, mtr) == 0) {
  1444. /* Allocate a new segment inode page */
  1445. success = fsp_alloc_seg_inode_page(space_header, mtr);
  1446. if (!success) {
  1447. return(NULL);
  1448. }
  1449. }
  1450. page_no = flst_get_first(space_header + FSP_SEG_INODES_FREE, mtr).page;
  1451. zip_size = fsp_flags_get_zip_size(
  1452. mach_read_from_4(FSP_SPACE_FLAGS + space_header));
  1453. block = buf_page_get(page_get_space_id(page_align(space_header)),
  1454. zip_size, page_no, RW_X_LATCH, mtr);
  1455. buf_block_dbg_add_level(block, SYNC_FSP_PAGE);
  1456. page = buf_block_get_frame(block);
  1457. SRV_CORRUPT_TABLE_CHECK(page, return(0););
  1458. n = fsp_seg_inode_page_find_free(page, 0, zip_size, mtr);
  1459. ut_a(n != ULINT_UNDEFINED);
  1460. inode = fsp_seg_inode_page_get_nth_inode(page, n, zip_size, mtr);
  1461. if (ULINT_UNDEFINED == fsp_seg_inode_page_find_free(page, n + 1,
  1462. zip_size, mtr)) {
  1463. /* There are no other unused headers left on the page: move it
  1464. to another list */
  1465. flst_remove(space_header + FSP_SEG_INODES_FREE,
  1466. page + FSEG_INODE_PAGE_NODE, mtr);
  1467. flst_add_last(space_header + FSP_SEG_INODES_FULL,
  1468. page + FSEG_INODE_PAGE_NODE, mtr);
  1469. }
  1470. ut_ad(!mach_read_from_8(inode + FSEG_ID)
  1471. || mach_read_from_4(inode + FSEG_MAGIC_N) == FSEG_MAGIC_N_VALUE);
  1472. return(inode);
  1473. }
  1474. /**********************************************************************//**
  1475. Frees a file segment inode. */
  1476. static
  1477. void
  1478. fsp_free_seg_inode(
  1479. /*===============*/
  1480. ulint space, /*!< in: space id */
  1481. ulint zip_size,/*!< in: compressed page size in bytes
  1482. or 0 for uncompressed pages */
  1483. fseg_inode_t* inode, /*!< in: segment inode */
  1484. mtr_t* mtr) /*!< in/out: mini-transaction */
  1485. {
  1486. page_t* page;
  1487. fsp_header_t* space_header;
  1488. page = page_align(inode);
  1489. space_header = fsp_get_space_header(space, zip_size, mtr);
  1490. ut_ad(mach_read_from_4(inode + FSEG_MAGIC_N) == FSEG_MAGIC_N_VALUE);
  1491. if (ULINT_UNDEFINED
  1492. == fsp_seg_inode_page_find_free(page, 0, zip_size, mtr)) {
  1493. /* Move the page to another list */
  1494. flst_remove(space_header + FSP_SEG_INODES_FULL,
  1495. page + FSEG_INODE_PAGE_NODE, mtr);
  1496. flst_add_last(space_header + FSP_SEG_INODES_FREE,
  1497. page + FSEG_INODE_PAGE_NODE, mtr);
  1498. }
  1499. mlog_write_ull(inode + FSEG_ID, 0, mtr);
  1500. mlog_write_ulint(inode + FSEG_MAGIC_N, 0xfa051ce3, MLOG_4BYTES, mtr);
  1501. if (ULINT_UNDEFINED
  1502. == fsp_seg_inode_page_find_used(page, zip_size, mtr)) {
  1503. /* There are no other used headers left on the page: free it */
  1504. flst_remove(space_header + FSP_SEG_INODES_FREE,
  1505. page + FSEG_INODE_PAGE_NODE, mtr);
  1506. fsp_free_page(space, zip_size, page_get_page_no(page), mtr);
  1507. }
  1508. }
  1509. /**********************************************************************//**
  1510. Returns the file segment inode, page x-latched.
  1511. @return segment inode, page x-latched; NULL if the inode is free */
  1512. static
  1513. fseg_inode_t*
  1514. fseg_inode_try_get(
  1515. /*===============*/
  1516. fseg_header_t* header, /*!< in: segment header */
  1517. ulint space, /*!< in: space id */
  1518. ulint zip_size,/*!< in: compressed page size in bytes
  1519. or 0 for uncompressed pages */
  1520. mtr_t* mtr) /*!< in/out: mini-transaction */
  1521. {
  1522. fil_addr_t inode_addr;
  1523. fseg_inode_t* inode;
  1524. inode_addr.page = mach_read_from_4(header + FSEG_HDR_PAGE_NO);
  1525. inode_addr.boffset = mach_read_from_2(header + FSEG_HDR_OFFSET);
  1526. ut_ad(space == mach_read_from_4(header + FSEG_HDR_SPACE));
  1527. inode = fut_get_ptr(space, zip_size, inode_addr, RW_X_LATCH, mtr);
  1528. SRV_CORRUPT_TABLE_CHECK(inode, return(0););
  1529. if (UNIV_UNLIKELY(!mach_read_from_8(inode + FSEG_ID))) {
  1530. inode = NULL;
  1531. } else {
  1532. ut_ad(mach_read_from_4(inode + FSEG_MAGIC_N)
  1533. == FSEG_MAGIC_N_VALUE);
  1534. }
  1535. return(inode);
  1536. }
  1537. /**********************************************************************//**
  1538. Returns the file segment inode, page x-latched.
  1539. @return segment inode, page x-latched */
  1540. static
  1541. fseg_inode_t*
  1542. fseg_inode_get(
  1543. /*===========*/
  1544. fseg_header_t* header, /*!< in: segment header */
  1545. ulint space, /*!< in: space id */
  1546. ulint zip_size,/*!< in: compressed page size in bytes
  1547. or 0 for uncompressed pages */
  1548. mtr_t* mtr) /*!< in/out: mini-transaction */
  1549. {
  1550. fseg_inode_t* inode
  1551. = fseg_inode_try_get(header, space, zip_size, mtr);
  1552. SRV_CORRUPT_TABLE_CHECK(inode, ; /* do nothing */);
  1553. return(inode);
  1554. }
  1555. /**********************************************************************//**
  1556. Gets the page number from the nth fragment page slot.
  1557. @return page number, FIL_NULL if not in use */
  1558. UNIV_INLINE
  1559. ulint
  1560. fseg_get_nth_frag_page_no(
  1561. /*======================*/
  1562. fseg_inode_t* inode, /*!< in: segment inode */
  1563. ulint n, /*!< in: slot index */
  1564. mtr_t* mtr MY_ATTRIBUTE((unused)))
  1565. /*!< in/out: mini-transaction */
  1566. {
  1567. ut_ad(inode && mtr);
  1568. ut_ad(n < FSEG_FRAG_ARR_N_SLOTS);
  1569. ut_ad(mtr_memo_contains_page(mtr, inode, MTR_MEMO_PAGE_X_FIX));
  1570. ut_ad(mach_read_from_4(inode + FSEG_MAGIC_N) == FSEG_MAGIC_N_VALUE);
  1571. return(mach_read_from_4(inode + FSEG_FRAG_ARR
  1572. + n * FSEG_FRAG_SLOT_SIZE));
  1573. }
  1574. /**********************************************************************//**
  1575. Sets the page number in the nth fragment page slot. */
  1576. UNIV_INLINE
  1577. void
  1578. fseg_set_nth_frag_page_no(
  1579. /*======================*/
  1580. fseg_inode_t* inode, /*!< in: segment inode */
  1581. ulint n, /*!< in: slot index */
  1582. ulint page_no,/*!< in: page number to set */
  1583. mtr_t* mtr) /*!< in/out: mini-transaction */
  1584. {
  1585. ut_ad(inode && mtr);
  1586. ut_ad(n < FSEG_FRAG_ARR_N_SLOTS);
  1587. ut_ad(mtr_memo_contains_page(mtr, inode, MTR_MEMO_PAGE_X_FIX));
  1588. ut_ad(mach_read_from_4(inode + FSEG_MAGIC_N) == FSEG_MAGIC_N_VALUE);
  1589. mlog_write_ulint(inode + FSEG_FRAG_ARR + n * FSEG_FRAG_SLOT_SIZE,
  1590. page_no, MLOG_4BYTES, mtr);
  1591. }
  1592. /**********************************************************************//**
  1593. Finds a fragment page slot which is free.
  1594. @return slot index; ULINT_UNDEFINED if none found */
  1595. static
  1596. ulint
  1597. fseg_find_free_frag_page_slot(
  1598. /*==========================*/
  1599. fseg_inode_t* inode, /*!< in: segment inode */
  1600. mtr_t* mtr) /*!< in/out: mini-transaction */
  1601. {
  1602. ulint i;
  1603. ulint page_no;
  1604. ut_ad(inode && mtr);
  1605. for (i = 0; i < FSEG_FRAG_ARR_N_SLOTS; i++) {
  1606. page_no = fseg_get_nth_frag_page_no(inode, i, mtr);
  1607. if (page_no == FIL_NULL) {
  1608. return(i);
  1609. }
  1610. }
  1611. return(ULINT_UNDEFINED);
  1612. }
  1613. /**********************************************************************//**
  1614. Finds a fragment page slot which is used and last in the array.
  1615. @return slot index; ULINT_UNDEFINED if none found */
  1616. static
  1617. ulint
  1618. fseg_find_last_used_frag_page_slot(
  1619. /*===============================*/
  1620. fseg_inode_t* inode, /*!< in: segment inode */
  1621. mtr_t* mtr) /*!< in/out: mini-transaction */
  1622. {
  1623. ulint i;
  1624. ulint page_no;
  1625. ut_ad(inode && mtr);
  1626. for (i = 0; i < FSEG_FRAG_ARR_N_SLOTS; i++) {
  1627. page_no = fseg_get_nth_frag_page_no(
  1628. inode, FSEG_FRAG_ARR_N_SLOTS - i - 1, mtr);
  1629. if (page_no != FIL_NULL) {
  1630. return(FSEG_FRAG_ARR_N_SLOTS - i - 1);
  1631. }
  1632. }
  1633. return(ULINT_UNDEFINED);
  1634. }
  1635. /**********************************************************************//**
  1636. Calculates reserved fragment page slots.
  1637. @return number of fragment pages */
  1638. static
  1639. ulint
  1640. fseg_get_n_frag_pages(
  1641. /*==================*/
  1642. fseg_inode_t* inode, /*!< in: segment inode */
  1643. mtr_t* mtr) /*!< in/out: mini-transaction */
  1644. {
  1645. ulint i;
  1646. ulint count = 0;
  1647. ut_ad(inode && mtr);
  1648. for (i = 0; i < FSEG_FRAG_ARR_N_SLOTS; i++) {
  1649. if (FIL_NULL != fseg_get_nth_frag_page_no(inode, i, mtr)) {
  1650. count++;
  1651. }
  1652. }
  1653. return(count);
  1654. }
  1655. /**********************************************************************//**
  1656. Creates a new segment.
  1657. @return the block where the segment header is placed, x-latched, NULL
  1658. if could not create segment because of lack of space */
  1659. UNIV_INTERN
  1660. buf_block_t*
  1661. fseg_create_general(
  1662. /*================*/
  1663. ulint space, /*!< in: space id */
  1664. ulint page, /*!< in: page where the segment header is placed: if
  1665. this is != 0, the page must belong to another segment,
  1666. if this is 0, a new page will be allocated and it
  1667. will belong to the created segment */
  1668. ulint byte_offset, /*!< in: byte offset of the created segment header
  1669. on the page */
  1670. ibool has_done_reservation, /*!< in: TRUE if the caller has already
  1671. done the reservation for the pages with
  1672. fsp_reserve_free_extents (at least 2 extents: one for
  1673. the inode and the other for the segment) then there is
  1674. no need to do the check for this individual
  1675. operation */
  1676. mtr_t* mtr) /*!< in/out: mini-transaction */
  1677. {
  1678. ulint flags;
  1679. ulint zip_size;
  1680. fsp_header_t* space_header;
  1681. fseg_inode_t* inode;
  1682. ib_id_t seg_id;
  1683. buf_block_t* block = 0; /* remove warning */
  1684. fseg_header_t* header = 0; /* remove warning */
  1685. prio_rw_lock_t* latch;
  1686. ibool success;
  1687. ulint n_reserved;
  1688. ulint i;
  1689. ut_ad(mtr);
  1690. ut_ad(byte_offset + FSEG_HEADER_SIZE
  1691. <= UNIV_PAGE_SIZE - FIL_PAGE_DATA_END);
  1692. latch = fil_space_get_latch(space, &flags);
  1693. zip_size = fsp_flags_get_zip_size(flags);
  1694. if (page != 0) {
  1695. block = buf_page_get(space, zip_size, page, RW_X_LATCH, mtr);
  1696. header = byte_offset + buf_block_get_frame(block);
  1697. }
  1698. mtr_x_lock(latch, mtr);
  1699. if (rw_lock_get_x_lock_count(latch) == 1) {
  1700. /* This thread did not own the latch before this call: free
  1701. excess pages from the insert buffer free list */
  1702. if (space == IBUF_SPACE_ID) {
  1703. ibuf_free_excess_pages();
  1704. }
  1705. }
  1706. if (!has_done_reservation) {
  1707. success = fsp_reserve_free_extents(&n_reserved, space, 2,
  1708. FSP_NORMAL, mtr);
  1709. if (!success) {
  1710. return(NULL);
  1711. }
  1712. }
  1713. space_header = fsp_get_space_header(space, zip_size, mtr);
  1714. inode = fsp_alloc_seg_inode(space_header, mtr);
  1715. if (inode == NULL) {
  1716. goto funct_exit;
  1717. }
  1718. /* Read the next segment id from space header and increment the
  1719. value in space header */
  1720. seg_id = mach_read_from_8(space_header + FSP_SEG_ID);
  1721. mlog_write_ull(space_header + FSP_SEG_ID, seg_id + 1, mtr);
  1722. mlog_write_ull(inode + FSEG_ID, seg_id, mtr);
  1723. mlog_write_ulint(inode + FSEG_NOT_FULL_N_USED, 0, MLOG_4BYTES, mtr);
  1724. flst_init(inode + FSEG_FREE, mtr);
  1725. flst_init(inode + FSEG_NOT_FULL, mtr);
  1726. flst_init(inode + FSEG_FULL, mtr);
  1727. mlog_write_ulint(inode + FSEG_MAGIC_N, FSEG_MAGIC_N_VALUE,
  1728. MLOG_4BYTES, mtr);
  1729. for (i = 0; i < FSEG_FRAG_ARR_N_SLOTS; i++) {
  1730. fseg_set_nth_frag_page_no(inode, i, FIL_NULL, mtr);
  1731. }
  1732. if (page == 0) {
  1733. block = fseg_alloc_free_page_low(space, zip_size,
  1734. inode, 0, FSP_UP, mtr, mtr);
  1735. if (block == NULL) {
  1736. fsp_free_seg_inode(space, zip_size, inode, mtr);
  1737. goto funct_exit;
  1738. }
  1739. ut_ad(rw_lock_get_x_lock_count(&block->lock) == 1);
  1740. header = byte_offset + buf_block_get_frame(block);
  1741. mlog_write_ulint(buf_block_get_frame(block) + FIL_PAGE_TYPE,
  1742. FIL_PAGE_TYPE_SYS, MLOG_2BYTES, mtr);
  1743. }
  1744. mlog_write_ulint(header + FSEG_HDR_OFFSET,
  1745. page_offset(inode), MLOG_2BYTES, mtr);
  1746. mlog_write_ulint(header + FSEG_HDR_PAGE_NO,
  1747. page_get_page_no(page_align(inode)),
  1748. MLOG_4BYTES, mtr);
  1749. mlog_write_ulint(header + FSEG_HDR_SPACE, space, MLOG_4BYTES, mtr);
  1750. funct_exit:
  1751. if (!has_done_reservation) {
  1752. fil_space_release_free_extents(space, n_reserved);
  1753. }
  1754. return(block);
  1755. }
  1756. /**********************************************************************//**
  1757. Creates a new segment.
  1758. @return the block where the segment header is placed, x-latched, NULL
  1759. if could not create segment because of lack of space */
  1760. UNIV_INTERN
  1761. buf_block_t*
  1762. fseg_create(
  1763. /*========*/
  1764. ulint space, /*!< in: space id */
  1765. ulint page, /*!< in: page where the segment header is placed: if
  1766. this is != 0, the page must belong to another segment,
  1767. if this is 0, a new page will be allocated and it
  1768. will belong to the created segment */
  1769. ulint byte_offset, /*!< in: byte offset of the created segment header
  1770. on the page */
  1771. mtr_t* mtr) /*!< in/out: mini-transaction */
  1772. {
  1773. return(fseg_create_general(space, page, byte_offset, FALSE, mtr));
  1774. }
  1775. /**********************************************************************//**
  1776. Calculates the number of pages reserved by a segment, and how many pages are
  1777. currently used.
  1778. @return number of reserved pages */
  1779. static
  1780. ulint
  1781. fseg_n_reserved_pages_low(
  1782. /*======================*/
  1783. fseg_inode_t* inode, /*!< in: segment inode */
  1784. ulint* used, /*!< out: number of pages used (not
  1785. more than reserved) */
  1786. mtr_t* mtr) /*!< in/out: mini-transaction */
  1787. {
  1788. ulint ret;
  1789. ut_ad(inode && used && mtr);
  1790. ut_ad(mtr_memo_contains_page(mtr, inode, MTR_MEMO_PAGE_X_FIX));
  1791. *used = mtr_read_ulint(inode + FSEG_NOT_FULL_N_USED, MLOG_4BYTES, mtr)
  1792. + FSP_EXTENT_SIZE * flst_get_len(inode + FSEG_FULL, mtr)
  1793. + fseg_get_n_frag_pages(inode, mtr);
  1794. ret = fseg_get_n_frag_pages(inode, mtr)
  1795. + FSP_EXTENT_SIZE * flst_get_len(inode + FSEG_FREE, mtr)
  1796. + FSP_EXTENT_SIZE * flst_get_len(inode + FSEG_NOT_FULL, mtr)
  1797. + FSP_EXTENT_SIZE * flst_get_len(inode + FSEG_FULL, mtr);
  1798. return(ret);
  1799. }
  1800. /**********************************************************************//**
  1801. Calculates the number of pages reserved by a segment, and how many pages are
  1802. currently used.
  1803. @return number of reserved pages */
  1804. UNIV_INTERN
  1805. ulint
  1806. fseg_n_reserved_pages(
  1807. /*==================*/
  1808. fseg_header_t* header, /*!< in: segment header */
  1809. ulint* used, /*!< out: number of pages used (<= reserved) */
  1810. mtr_t* mtr) /*!< in/out: mini-transaction */
  1811. {
  1812. ulint ret;
  1813. fseg_inode_t* inode;
  1814. ulint space;
  1815. ulint flags;
  1816. ulint zip_size;
  1817. prio_rw_lock_t* latch;
  1818. space = page_get_space_id(page_align(header));
  1819. latch = fil_space_get_latch(space, &flags);
  1820. zip_size = fsp_flags_get_zip_size(flags);
  1821. mtr_x_lock(latch, mtr);
  1822. inode = fseg_inode_get(header, space, zip_size, mtr);
  1823. ret = fseg_n_reserved_pages_low(inode, used, mtr);
  1824. return(ret);
  1825. }
  1826. /*********************************************************************//**
  1827. Tries to fill the free list of a segment with consecutive free extents.
  1828. This happens if the segment is big enough to allow extents in the free list,
  1829. the free list is empty, and the extents can be allocated consecutively from
  1830. the hint onward. */
  1831. static
  1832. void
  1833. fseg_fill_free_list(
  1834. /*================*/
  1835. fseg_inode_t* inode, /*!< in: segment inode */
  1836. ulint space, /*!< in: space id */
  1837. ulint zip_size,/*!< in: compressed page size in bytes
  1838. or 0 for uncompressed pages */
  1839. ulint hint, /*!< in: hint which extent would be good as
  1840. the first extent */
  1841. mtr_t* mtr) /*!< in/out: mini-transaction */
  1842. {
  1843. xdes_t* descr;
  1844. ulint i;
  1845. ib_id_t seg_id;
  1846. ulint reserved;
  1847. ulint used;
  1848. ut_ad(inode && mtr);
  1849. ut_ad(!((page_offset(inode) - FSEG_ARR_OFFSET) % FSEG_INODE_SIZE));
  1850. reserved = fseg_n_reserved_pages_low(inode, &used, mtr);
  1851. if (reserved < FSEG_FREE_LIST_LIMIT * FSP_EXTENT_SIZE) {
  1852. /* The segment is too small to allow extents in free list */
  1853. return;
  1854. }
  1855. if (flst_get_len(inode + FSEG_FREE, mtr) > 0) {
  1856. /* Free list is not empty */
  1857. return;
  1858. }
  1859. for (i = 0; i < FSEG_FREE_LIST_MAX_LEN; i++) {
  1860. descr = xdes_get_descriptor(space, zip_size, hint, mtr);
  1861. if ((descr == NULL)
  1862. || (XDES_FREE != xdes_get_state(descr, mtr))) {
  1863. /* We cannot allocate the desired extent: stop */
  1864. return;
  1865. }
  1866. descr = fsp_alloc_free_extent(space, zip_size, hint, mtr);
  1867. xdes_set_state(descr, XDES_FSEG, mtr);
  1868. seg_id = mach_read_from_8(inode + FSEG_ID);
  1869. ut_ad(mach_read_from_4(inode + FSEG_MAGIC_N)
  1870. == FSEG_MAGIC_N_VALUE);
  1871. mlog_write_ull(descr + XDES_ID, seg_id, mtr);
  1872. flst_add_last(inode + FSEG_FREE, descr + XDES_FLST_NODE, mtr);
  1873. hint += FSP_EXTENT_SIZE;
  1874. }
  1875. }
  1876. /*********************************************************************//**
  1877. Allocates a free extent for the segment: looks first in the free list of the
  1878. segment, then tries to allocate from the space free list. NOTE that the extent
  1879. returned still resides in the segment free list, it is not yet taken off it!
  1880. @retval NULL if no page could be allocated
  1881. @retval block, rw_lock_x_lock_count(&block->lock) == 1 if allocation succeeded
  1882. (init_mtr == mtr, or the page was not previously freed in mtr)
  1883. @retval block (not allocated or initialized) otherwise */
  1884. static
  1885. xdes_t*
  1886. fseg_alloc_free_extent(
  1887. /*===================*/
  1888. fseg_inode_t* inode, /*!< in: segment inode */
  1889. ulint space, /*!< in: space id */
  1890. ulint zip_size,/*!< in: compressed page size in bytes
  1891. or 0 for uncompressed pages */
  1892. mtr_t* mtr) /*!< in/out: mini-transaction */
  1893. {
  1894. xdes_t* descr;
  1895. ib_id_t seg_id;
  1896. fil_addr_t first;
  1897. ut_ad(!((page_offset(inode) - FSEG_ARR_OFFSET) % FSEG_INODE_SIZE));
  1898. ut_ad(mach_read_from_4(inode + FSEG_MAGIC_N) == FSEG_MAGIC_N_VALUE);
  1899. if (flst_get_len(inode + FSEG_FREE, mtr) > 0) {
  1900. /* Segment free list is not empty, allocate from it */
  1901. first = flst_get_first(inode + FSEG_FREE, mtr);
  1902. descr = xdes_lst_get_descriptor(space, zip_size, first, mtr);
  1903. } else {
  1904. /* Segment free list was empty, allocate from space */
  1905. descr = fsp_alloc_free_extent(space, zip_size, 0, mtr);
  1906. if (descr == NULL) {
  1907. return(NULL);
  1908. }
  1909. seg_id = mach_read_from_8(inode + FSEG_ID);
  1910. xdes_set_state(descr, XDES_FSEG, mtr);
  1911. mlog_write_ull(descr + XDES_ID, seg_id, mtr);
  1912. flst_add_last(inode + FSEG_FREE, descr + XDES_FLST_NODE, mtr);
  1913. /* Try to fill the segment free list */
  1914. fseg_fill_free_list(inode, space, zip_size,
  1915. xdes_get_offset(descr) + FSP_EXTENT_SIZE,
  1916. mtr);
  1917. }
  1918. return(descr);
  1919. }
  1920. /**********************************************************************//**
  1921. Allocates a single free page from a segment. This function implements
  1922. the intelligent allocation strategy which tries to minimize file space
  1923. fragmentation.
  1924. @retval NULL if no page could be allocated
  1925. @retval block, rw_lock_x_lock_count(&block->lock) == 1 if allocation succeeded
  1926. (init_mtr == mtr, or the page was not previously freed in mtr)
  1927. @retval block (not allocated or initialized) otherwise */
  1928. static
  1929. buf_block_t*
  1930. fseg_alloc_free_page_low(
  1931. /*=====================*/
  1932. ulint space, /*!< in: space */
  1933. ulint zip_size,/*!< in: compressed page size in bytes
  1934. or 0 for uncompressed pages */
  1935. fseg_inode_t* seg_inode, /*!< in/out: segment inode */
  1936. ulint hint, /*!< in: hint of which page would be
  1937. desirable */
  1938. byte direction, /*!< in: if the new page is needed because
  1939. of an index page split, and records are
  1940. inserted there in order, into which
  1941. direction they go alphabetically: FSP_DOWN,
  1942. FSP_UP, FSP_NO_DIR */
  1943. mtr_t* mtr, /*!< in/out: mini-transaction */
  1944. mtr_t* init_mtr)/*!< in/out: mtr or another mini-transaction
  1945. in which the page should be initialized.
  1946. If init_mtr!=mtr, but the page is already
  1947. latched in mtr, do not initialize the page. */
  1948. {
  1949. fsp_header_t* space_header;
  1950. ulint space_size;
  1951. ib_id_t seg_id;
  1952. ulint used;
  1953. ulint reserved;
  1954. xdes_t* descr; /*!< extent of the hinted page */
  1955. ulint ret_page; /*!< the allocated page offset, FIL_NULL
  1956. if could not be allocated */
  1957. xdes_t* ret_descr; /*!< the extent of the allocated page */
  1958. ibool success;
  1959. ulint n;
  1960. ut_ad((direction >= FSP_UP) && (direction <= FSP_NO_DIR));
  1961. ut_ad(mach_read_from_4(seg_inode + FSEG_MAGIC_N)
  1962. == FSEG_MAGIC_N_VALUE);
  1963. ut_ad(!((page_offset(seg_inode) - FSEG_ARR_OFFSET) % FSEG_INODE_SIZE));
  1964. seg_id = mach_read_from_8(seg_inode + FSEG_ID);
  1965. ut_ad(seg_id);
  1966. reserved = fseg_n_reserved_pages_low(seg_inode, &used, mtr);
  1967. space_header = fsp_get_space_header(space, zip_size, mtr);
  1968. descr = xdes_get_descriptor_with_space_hdr(space_header, space,
  1969. hint, mtr);
  1970. if (descr == NULL) {
  1971. /* Hint outside space or too high above free limit: reset
  1972. hint */
  1973. /* The file space header page is always allocated. */
  1974. hint = 0;
  1975. descr = xdes_get_descriptor(space, zip_size, hint, mtr);
  1976. }
  1977. /* In the big if-else below we look for ret_page and ret_descr */
  1978. /*-------------------------------------------------------------*/
  1979. if ((xdes_get_state(descr, mtr) == XDES_FSEG)
  1980. && mach_read_from_8(descr + XDES_ID) == seg_id
  1981. && (xdes_mtr_get_bit(descr, XDES_FREE_BIT,
  1982. hint % FSP_EXTENT_SIZE, mtr) == TRUE)) {
  1983. take_hinted_page:
  1984. /* 1. We can take the hinted page
  1985. =================================*/
  1986. ret_descr = descr;
  1987. ret_page = hint;
  1988. /* Skip the check for extending the tablespace. If the
  1989. page hint were not within the size of the tablespace,
  1990. we would have got (descr == NULL) above and reset the hint. */
  1991. goto got_hinted_page;
  1992. /*-----------------------------------------------------------*/
  1993. } else if (xdes_get_state(descr, mtr) == XDES_FREE
  1994. && reserved - used < reserved / FSEG_FILLFACTOR
  1995. && used >= FSEG_FRAG_LIMIT) {
  1996. /* 2. We allocate the free extent from space and can take
  1997. =========================================================
  1998. the hinted page
  1999. ===============*/
  2000. ret_descr = fsp_alloc_free_extent(space, zip_size, hint, mtr);
  2001. ut_a(ret_descr == descr);
  2002. xdes_set_state(ret_descr, XDES_FSEG, mtr);
  2003. mlog_write_ull(ret_descr + XDES_ID, seg_id, mtr);
  2004. flst_add_last(seg_inode + FSEG_FREE,
  2005. ret_descr + XDES_FLST_NODE, mtr);
  2006. /* Try to fill the segment free list */
  2007. fseg_fill_free_list(seg_inode, space, zip_size,
  2008. hint + FSP_EXTENT_SIZE, mtr);
  2009. goto take_hinted_page;
  2010. /*-----------------------------------------------------------*/
  2011. } else if ((direction != FSP_NO_DIR)
  2012. && ((reserved - used) < reserved / FSEG_FILLFACTOR)
  2013. && (used >= FSEG_FRAG_LIMIT)
  2014. && (!!(ret_descr
  2015. = fseg_alloc_free_extent(seg_inode,
  2016. space, zip_size, mtr)))) {
  2017. /* 3. We take any free extent (which was already assigned above
  2018. ===============================================================
  2019. in the if-condition to ret_descr) and take the lowest or
  2020. ========================================================
  2021. highest page in it, depending on the direction
  2022. ==============================================*/
  2023. ret_page = xdes_get_offset(ret_descr);
  2024. if (direction == FSP_DOWN) {
  2025. ret_page += FSP_EXTENT_SIZE - 1;
  2026. }
  2027. /*-----------------------------------------------------------*/
  2028. } else if ((xdes_get_state(descr, mtr) == XDES_FSEG)
  2029. && mach_read_from_8(descr + XDES_ID) == seg_id
  2030. && (!xdes_is_full(descr, mtr))) {
  2031. /* 4. We can take the page from the same extent as the
  2032. ======================================================
  2033. hinted page (and the extent already belongs to the
  2034. ==================================================
  2035. segment)
  2036. ========*/
  2037. ret_descr = descr;
  2038. ret_page = xdes_get_offset(ret_descr)
  2039. + xdes_find_bit(ret_descr, XDES_FREE_BIT, TRUE,
  2040. hint % FSP_EXTENT_SIZE, mtr);
  2041. /*-----------------------------------------------------------*/
  2042. } else if (reserved - used > 0) {
  2043. /* 5. We take any unused page from the segment
  2044. ==============================================*/
  2045. fil_addr_t first;
  2046. if (flst_get_len(seg_inode + FSEG_NOT_FULL, mtr) > 0) {
  2047. first = flst_get_first(seg_inode + FSEG_NOT_FULL,
  2048. mtr);
  2049. } else if (flst_get_len(seg_inode + FSEG_FREE, mtr) > 0) {
  2050. first = flst_get_first(seg_inode + FSEG_FREE, mtr);
  2051. } else {
  2052. ut_error;
  2053. return(NULL);
  2054. }
  2055. ret_descr = xdes_lst_get_descriptor(space, zip_size,
  2056. first, mtr);
  2057. ret_page = xdes_get_offset(ret_descr)
  2058. + xdes_find_bit(ret_descr, XDES_FREE_BIT, TRUE,
  2059. 0, mtr);
  2060. /*-----------------------------------------------------------*/
  2061. } else if (used < FSEG_FRAG_LIMIT) {
  2062. /* 6. We allocate an individual page from the space
  2063. ===================================================*/
  2064. buf_block_t* block = fsp_alloc_free_page(
  2065. space, zip_size, hint, mtr, init_mtr);
  2066. if (block != NULL) {
  2067. /* Put the page in the fragment page array of the
  2068. segment */
  2069. n = fseg_find_free_frag_page_slot(seg_inode, mtr);
  2070. ut_a(n != ULINT_UNDEFINED);
  2071. fseg_set_nth_frag_page_no(
  2072. seg_inode, n, buf_block_get_page_no(block),
  2073. mtr);
  2074. }
  2075. /* fsp_alloc_free_page() invoked fsp_init_file_page()
  2076. already. */
  2077. return(block);
  2078. /*-----------------------------------------------------------*/
  2079. } else {
  2080. /* 7. We allocate a new extent and take its first page
  2081. ======================================================*/
  2082. ret_descr = fseg_alloc_free_extent(seg_inode,
  2083. space, zip_size, mtr);
  2084. if (ret_descr == NULL) {
  2085. ret_page = FIL_NULL;
  2086. } else {
  2087. ret_page = xdes_get_offset(ret_descr);
  2088. }
  2089. }
  2090. if (ret_page == FIL_NULL) {
  2091. /* Page could not be allocated */
  2092. return(NULL);
  2093. }
  2094. if (space != 0) {
  2095. space_size = fil_space_get_size(space);
  2096. if (space_size <= ret_page) {
  2097. /* It must be that we are extending a single-table
  2098. tablespace whose size is still < 64 pages */
  2099. if (ret_page >= FSP_EXTENT_SIZE) {
  2100. fprintf(stderr,
  2101. "InnoDB: Error (2): trying to extend"
  2102. " a single-table tablespace %lu\n"
  2103. "InnoDB: by single page(s) though"
  2104. " the space size %lu. Page no %lu.\n",
  2105. (ulong) space, (ulong) space_size,
  2106. (ulong) ret_page);
  2107. return(NULL);
  2108. }
  2109. success = fsp_try_extend_data_file_with_pages(
  2110. space, ret_page, space_header, mtr);
  2111. if (!success) {
  2112. /* No disk space left */
  2113. return(NULL);
  2114. }
  2115. }
  2116. }
  2117. got_hinted_page:
  2118. /* ret_descr == NULL if the block was allocated from free_frag
  2119. (XDES_FREE_FRAG) */
  2120. if (ret_descr != NULL) {
  2121. /* At this point we know the extent and the page offset.
  2122. The extent is still in the appropriate list (FSEG_NOT_FULL
  2123. or FSEG_FREE), and the page is not yet marked as used. */
  2124. ut_ad(xdes_get_descriptor(space, zip_size, ret_page, mtr)
  2125. == ret_descr);
  2126. ut_ad(xdes_mtr_get_bit(
  2127. ret_descr, XDES_FREE_BIT,
  2128. ret_page % FSP_EXTENT_SIZE, mtr));
  2129. fseg_mark_page_used(seg_inode, ret_page, ret_descr, mtr);
  2130. }
  2131. return(fsp_page_create(
  2132. space, fsp_flags_get_zip_size(
  2133. mach_read_from_4(FSP_SPACE_FLAGS
  2134. + space_header)),
  2135. ret_page, mtr, init_mtr));
  2136. }
  2137. /**********************************************************************//**
  2138. Allocates a single free page from a segment. This function implements
  2139. the intelligent allocation strategy which tries to minimize file space
  2140. fragmentation.
  2141. @retval NULL if no page could be allocated
  2142. @retval block, rw_lock_x_lock_count(&block->lock) == 1 if allocation succeeded
  2143. (init_mtr == mtr, or the page was not previously freed in mtr)
  2144. @retval block (not allocated or initialized) otherwise */
  2145. UNIV_INTERN
  2146. buf_block_t*
  2147. fseg_alloc_free_page_general(
  2148. /*=========================*/
  2149. fseg_header_t* seg_header,/*!< in/out: segment header */
  2150. ulint hint, /*!< in: hint of which page would be
  2151. desirable */
  2152. byte direction,/*!< in: if the new page is needed because
  2153. of an index page split, and records are
  2154. inserted there in order, into which
  2155. direction they go alphabetically: FSP_DOWN,
  2156. FSP_UP, FSP_NO_DIR */
  2157. ibool has_done_reservation, /*!< in: TRUE if the caller has
  2158. already done the reservation for the page
  2159. with fsp_reserve_free_extents, then there
  2160. is no need to do the check for this individual
  2161. page */
  2162. mtr_t* mtr, /*!< in/out: mini-transaction */
  2163. mtr_t* init_mtr)/*!< in/out: mtr or another mini-transaction
  2164. in which the page should be initialized.
  2165. If init_mtr!=mtr, but the page is already
  2166. latched in mtr, do not initialize the page. */
  2167. {
  2168. fseg_inode_t* inode;
  2169. ulint space;
  2170. ulint flags;
  2171. ulint zip_size;
  2172. prio_rw_lock_t* latch;
  2173. buf_block_t* block;
  2174. ulint n_reserved;
  2175. space = page_get_space_id(page_align(seg_header));
  2176. latch = fil_space_get_latch(space, &flags);
  2177. zip_size = fsp_flags_get_zip_size(flags);
  2178. mtr_x_lock(latch, mtr);
  2179. if (rw_lock_get_x_lock_count(latch) == 1) {
  2180. /* This thread did not own the latch before this call: free
  2181. excess pages from the insert buffer free list */
  2182. if (space == IBUF_SPACE_ID) {
  2183. ibuf_free_excess_pages();
  2184. }
  2185. }
  2186. inode = fseg_inode_get(seg_header, space, zip_size, mtr);
  2187. if (!has_done_reservation
  2188. && !fsp_reserve_free_extents(&n_reserved, space, 2,
  2189. FSP_NORMAL, mtr)) {
  2190. return(NULL);
  2191. }
  2192. block = fseg_alloc_free_page_low(space, zip_size,
  2193. inode, hint, direction,
  2194. mtr, init_mtr);
  2195. if (!has_done_reservation) {
  2196. fil_space_release_free_extents(space, n_reserved);
  2197. }
  2198. return(block);
  2199. }
  2200. /**********************************************************************//**
  2201. Checks that we have at least 2 frag pages free in the first extent of a
  2202. single-table tablespace, and they are also physically initialized to the data
  2203. file. That is we have already extended the data file so that those pages are
  2204. inside the data file. If not, this function extends the tablespace with
  2205. pages.
  2206. @return TRUE if there were >= 3 free pages, or we were able to extend */
  2207. static
  2208. ibool
  2209. fsp_reserve_free_pages(
  2210. /*===================*/
  2211. ulint space, /*!< in: space id, must be != 0 */
  2212. fsp_header_t* space_header, /*!< in: header of that space,
  2213. x-latched */
  2214. ulint size, /*!< in: size of the tablespace in
  2215. pages, must be < FSP_EXTENT_SIZE */
  2216. mtr_t* mtr) /*!< in/out: mini-transaction */
  2217. {
  2218. xdes_t* descr;
  2219. ulint n_used;
  2220. ut_a(space != 0);
  2221. ut_a(size < FSP_EXTENT_SIZE);
  2222. descr = xdes_get_descriptor_with_space_hdr(space_header, space, 0,
  2223. mtr);
  2224. n_used = xdes_get_n_used(descr, mtr);
  2225. ut_a(n_used <= size);
  2226. if (size >= n_used + 2) {
  2227. return(TRUE);
  2228. }
  2229. return(fsp_try_extend_data_file_with_pages(space, n_used + 1,
  2230. space_header, mtr));
  2231. }
  2232. /**********************************************************************//**
  2233. Reserves free pages from a tablespace. All mini-transactions which may
  2234. use several pages from the tablespace should call this function beforehand
  2235. and reserve enough free extents so that they certainly will be able
  2236. to do their operation, like a B-tree page split, fully. Reservations
  2237. must be released with function fil_space_release_free_extents!
  2238. The alloc_type below has the following meaning: FSP_NORMAL means an
  2239. operation which will probably result in more space usage, like an
  2240. insert in a B-tree; FSP_UNDO means allocation to undo logs: if we are
  2241. deleting rows, then this allocation will in the long run result in
  2242. less space usage (after a purge); FSP_CLEANING means allocation done
  2243. in a physical record delete (like in a purge) or other cleaning operation
  2244. which will result in less space usage in the long run. We prefer the latter
  2245. two types of allocation: when space is scarce, FSP_NORMAL allocations
  2246. will not succeed, but the latter two allocations will succeed, if possible.
  2247. The purpose is to avoid dead end where the database is full but the
  2248. user cannot free any space because these freeing operations temporarily
  2249. reserve some space.
  2250. Single-table tablespaces whose size is < 32 pages are a special case. In this
  2251. function we would liberally reserve several 64 page extents for every page
  2252. split or merge in a B-tree. But we do not want to waste disk space if the table
  2253. only occupies < 32 pages. That is why we apply different rules in that special
  2254. case, just ensuring that there are 3 free pages available.
  2255. @return TRUE if we were able to make the reservation */
  2256. UNIV_INTERN
  2257. ibool
  2258. fsp_reserve_free_extents(
  2259. /*=====================*/
  2260. ulint* n_reserved,/*!< out: number of extents actually reserved; if we
  2261. return TRUE and the tablespace size is < 64 pages,
  2262. then this can be 0, otherwise it is n_ext */
  2263. ulint space, /*!< in: space id */
  2264. ulint n_ext, /*!< in: number of extents to reserve */
  2265. ulint alloc_type,/*!< in: FSP_NORMAL, FSP_UNDO, or FSP_CLEANING */
  2266. mtr_t* mtr) /*!< in/out: mini-transaction */
  2267. {
  2268. fsp_header_t* space_header;
  2269. prio_rw_lock_t* latch;
  2270. ulint n_free_list_ext;
  2271. ulint free_limit;
  2272. ulint size;
  2273. ulint flags;
  2274. ulint zip_size;
  2275. ulint n_free;
  2276. ulint n_free_up;
  2277. ulint reserve;
  2278. ibool success;
  2279. ulint n_pages_added;
  2280. size_t total_reserved = 0;
  2281. ut_ad(mtr);
  2282. *n_reserved = n_ext;
  2283. latch = fil_space_get_latch(space, &flags);
  2284. zip_size = fsp_flags_get_zip_size(flags);
  2285. mtr_x_lock(latch, mtr);
  2286. space_header = fsp_get_space_header(space, zip_size, mtr);
  2287. try_again:
  2288. size = mtr_read_ulint(space_header + FSP_SIZE, MLOG_4BYTES, mtr);
  2289. if (size < FSP_EXTENT_SIZE / 2) {
  2290. /* Use different rules for small single-table tablespaces */
  2291. *n_reserved = 0;
  2292. return(fsp_reserve_free_pages(space, space_header, size, mtr));
  2293. }
  2294. n_free_list_ext = flst_get_len(space_header + FSP_FREE, mtr);
  2295. free_limit = mtr_read_ulint(space_header + FSP_FREE_LIMIT,
  2296. MLOG_4BYTES, mtr);
  2297. /* Below we play safe when counting free extents above the free limit:
  2298. some of them will contain extent descriptor pages, and therefore
  2299. will not be free extents */
  2300. n_free_up = (size - free_limit) / FSP_EXTENT_SIZE;
  2301. if (n_free_up > 0) {
  2302. n_free_up--;
  2303. if (!zip_size) {
  2304. n_free_up -= n_free_up
  2305. / (UNIV_PAGE_SIZE / FSP_EXTENT_SIZE);
  2306. } else {
  2307. n_free_up -= n_free_up
  2308. / (zip_size / FSP_EXTENT_SIZE);
  2309. }
  2310. }
  2311. n_free = n_free_list_ext + n_free_up;
  2312. if (alloc_type == FSP_NORMAL) {
  2313. /* We reserve 1 extent + 0.5 % of the space size to undo logs
  2314. and 1 extent + 0.5 % to cleaning operations; NOTE: this source
  2315. code is duplicated in the function below! */
  2316. reserve = 2 + ((size / FSP_EXTENT_SIZE) * 2) / 200;
  2317. if (n_free <= reserve + n_ext) {
  2318. goto try_to_extend;
  2319. }
  2320. } else if (alloc_type == FSP_UNDO) {
  2321. /* We reserve 0.5 % of the space size to cleaning operations */
  2322. reserve = 1 + ((size / FSP_EXTENT_SIZE) * 1) / 200;
  2323. if (n_free <= reserve + n_ext) {
  2324. goto try_to_extend;
  2325. }
  2326. } else {
  2327. ut_a(alloc_type == FSP_CLEANING);
  2328. reserve = 0;
  2329. }
  2330. success = fil_space_reserve_free_extents(space, n_free, n_ext);
  2331. *n_reserved = n_ext;
  2332. if (success) {
  2333. return(TRUE);
  2334. }
  2335. try_to_extend:
  2336. success = fsp_try_extend_data_file(&n_pages_added, space,
  2337. space_header, mtr);
  2338. if (success && n_pages_added > 0) {
  2339. total_reserved += n_pages_added;
  2340. goto try_again;
  2341. }
  2342. return(FALSE);
  2343. }
  2344. /**********************************************************************//**
  2345. This function should be used to get information on how much we still
  2346. will be able to insert new data to the database without running out the
  2347. tablespace. Only free extents are taken into account and we also subtract
  2348. the safety margin required by the above function fsp_reserve_free_extents.
  2349. @return available space in kB */
  2350. UNIV_INTERN
  2351. ullint
  2352. fsp_get_available_space_in_free_extents(
  2353. /*====================================*/
  2354. ulint space) /*!< in: space id */
  2355. {
  2356. fsp_header_t* space_header;
  2357. ulint n_free_list_ext;
  2358. ulint free_limit;
  2359. ulint size;
  2360. ulint flags;
  2361. ulint zip_size;
  2362. ulint n_free;
  2363. ulint n_free_up;
  2364. ulint reserve;
  2365. prio_rw_lock_t* latch;
  2366. mtr_t mtr;
  2367. /* The convoluted mutex acquire is to overcome latching order
  2368. issues: The problem is that the fil_mutex is at a lower level
  2369. than the tablespace latch and the buffer pool mutexes. We have to
  2370. first prevent any operations on the file system by acquiring the
  2371. dictionary mutex. Then acquire the tablespace latch to obey the
  2372. latching order and then release the dictionary mutex. That way we
  2373. ensure that the tablespace instance can't be freed while we are
  2374. examining its contents (see fil_space_free()).
  2375. However, there is one further complication, we release the fil_mutex
  2376. when we need to invalidate the the pages in the buffer pool and we
  2377. reacquire the fil_mutex when deleting and freeing the tablespace
  2378. instance in fil0fil.cc. Here we need to account for that situation
  2379. too. */
  2380. mutex_enter(&dict_sys->mutex);
  2381. /* At this stage there is no guarantee that the tablespace even
  2382. exists in the cache. */
  2383. if (fil_tablespace_deleted_or_being_deleted_in_mem(space, -1)) {
  2384. mutex_exit(&dict_sys->mutex);
  2385. return(ULLINT_UNDEFINED);
  2386. }
  2387. mtr_start(&mtr);
  2388. latch = fil_space_get_latch(space, &flags);
  2389. /* This should ensure that the tablespace instance can't be freed
  2390. by another thread. However, the tablespace pages can still be freed
  2391. from the buffer pool. We need to check for that again. */
  2392. zip_size = fsp_flags_get_zip_size(flags);
  2393. mtr_x_lock(latch, &mtr);
  2394. mutex_exit(&dict_sys->mutex);
  2395. /* At this point it is possible for the tablespace to be deleted and
  2396. its pages removed from the buffer pool. We need to check for that
  2397. situation. However, the tablespace instance can't be deleted because
  2398. our latching above should ensure that. */
  2399. if (fil_tablespace_is_being_deleted(space)) {
  2400. mtr_commit(&mtr);
  2401. return(ULLINT_UNDEFINED);
  2402. }
  2403. /* From here on even if the user has dropped the tablespace, the
  2404. pages _must_ still exist in the buffer pool and the tablespace
  2405. instance _must_ be in the file system hash table. */
  2406. space_header = fsp_get_space_header(space, zip_size, &mtr);
  2407. size = mtr_read_ulint(space_header + FSP_SIZE, MLOG_4BYTES, &mtr);
  2408. n_free_list_ext = flst_get_len(space_header + FSP_FREE, &mtr);
  2409. free_limit = mtr_read_ulint(space_header + FSP_FREE_LIMIT,
  2410. MLOG_4BYTES, &mtr);
  2411. mtr_commit(&mtr);
  2412. if (size < FSP_EXTENT_SIZE) {
  2413. ut_a(space != 0); /* This must be a single-table
  2414. tablespace */
  2415. return(0); /* TODO: count free frag pages and
  2416. return a value based on that */
  2417. }
  2418. /* Below we play safe when counting free extents above the free limit:
  2419. some of them will contain extent descriptor pages, and therefore
  2420. will not be free extents */
  2421. n_free_up = (size - free_limit) / FSP_EXTENT_SIZE;
  2422. if (n_free_up > 0) {
  2423. n_free_up--;
  2424. if (!zip_size) {
  2425. n_free_up -= n_free_up
  2426. / (UNIV_PAGE_SIZE / FSP_EXTENT_SIZE);
  2427. } else {
  2428. n_free_up -= n_free_up
  2429. / (zip_size / FSP_EXTENT_SIZE);
  2430. }
  2431. }
  2432. n_free = n_free_list_ext + n_free_up;
  2433. /* We reserve 1 extent + 0.5 % of the space size to undo logs
  2434. and 1 extent + 0.5 % to cleaning operations; NOTE: this source
  2435. code is duplicated in the function above! */
  2436. reserve = 2 + ((size / FSP_EXTENT_SIZE) * 2) / 200;
  2437. if (reserve > n_free) {
  2438. return(0);
  2439. }
  2440. if (!zip_size) {
  2441. return((ullint) (n_free - reserve)
  2442. * FSP_EXTENT_SIZE
  2443. * (UNIV_PAGE_SIZE / 1024));
  2444. } else {
  2445. return((ullint) (n_free - reserve)
  2446. * FSP_EXTENT_SIZE
  2447. * (zip_size / 1024));
  2448. }
  2449. }
  2450. /********************************************************************//**
  2451. Marks a page used. The page must reside within the extents of the given
  2452. segment. */
  2453. static MY_ATTRIBUTE((nonnull))
  2454. void
  2455. fseg_mark_page_used(
  2456. /*================*/
  2457. fseg_inode_t* seg_inode,/*!< in: segment inode */
  2458. ulint page, /*!< in: page offset */
  2459. xdes_t* descr, /*!< in: extent descriptor */
  2460. mtr_t* mtr) /*!< in/out: mini-transaction */
  2461. {
  2462. ulint not_full_n_used;
  2463. ut_ad(!((page_offset(seg_inode) - FSEG_ARR_OFFSET) % FSEG_INODE_SIZE));
  2464. ut_ad(mach_read_from_4(seg_inode + FSEG_MAGIC_N)
  2465. == FSEG_MAGIC_N_VALUE);
  2466. ut_ad(mtr_read_ulint(seg_inode + FSEG_ID, MLOG_4BYTES, mtr)
  2467. == mtr_read_ulint(descr + XDES_ID, MLOG_4BYTES, mtr));
  2468. if (xdes_is_free(descr, mtr)) {
  2469. /* We move the extent from the free list to the
  2470. NOT_FULL list */
  2471. flst_remove(seg_inode + FSEG_FREE, descr + XDES_FLST_NODE,
  2472. mtr);
  2473. flst_add_last(seg_inode + FSEG_NOT_FULL,
  2474. descr + XDES_FLST_NODE, mtr);
  2475. }
  2476. ut_ad(xdes_mtr_get_bit(
  2477. descr, XDES_FREE_BIT, page % FSP_EXTENT_SIZE, mtr));
  2478. /* We mark the page as used */
  2479. xdes_set_bit(descr, XDES_FREE_BIT, page % FSP_EXTENT_SIZE, FALSE, mtr);
  2480. not_full_n_used = mtr_read_ulint(seg_inode + FSEG_NOT_FULL_N_USED,
  2481. MLOG_4BYTES, mtr);
  2482. not_full_n_used++;
  2483. mlog_write_ulint(seg_inode + FSEG_NOT_FULL_N_USED, not_full_n_used,
  2484. MLOG_4BYTES, mtr);
  2485. if (xdes_is_full(descr, mtr)) {
  2486. /* We move the extent from the NOT_FULL list to the
  2487. FULL list */
  2488. flst_remove(seg_inode + FSEG_NOT_FULL,
  2489. descr + XDES_FLST_NODE, mtr);
  2490. flst_add_last(seg_inode + FSEG_FULL,
  2491. descr + XDES_FLST_NODE, mtr);
  2492. mlog_write_ulint(seg_inode + FSEG_NOT_FULL_N_USED,
  2493. not_full_n_used - FSP_EXTENT_SIZE,
  2494. MLOG_4BYTES, mtr);
  2495. }
  2496. }
  2497. /**********************************************************************//**
  2498. Frees a single page of a segment. */
  2499. static
  2500. void
  2501. fseg_free_page_low(
  2502. /*===============*/
  2503. fseg_inode_t* seg_inode, /*!< in: segment inode */
  2504. ulint space, /*!< in: space id */
  2505. ulint zip_size,/*!< in: compressed page size in bytes
  2506. or 0 for uncompressed pages */
  2507. ulint page, /*!< in: page offset */
  2508. mtr_t* mtr) /*!< in/out: mini-transaction */
  2509. {
  2510. xdes_t* descr;
  2511. ulint not_full_n_used;
  2512. ulint state;
  2513. ib_id_t descr_id;
  2514. ib_id_t seg_id;
  2515. ulint i;
  2516. ut_ad(seg_inode != NULL);
  2517. ut_ad(mtr != NULL);
  2518. ut_ad(mach_read_from_4(seg_inode + FSEG_MAGIC_N)
  2519. == FSEG_MAGIC_N_VALUE);
  2520. ut_ad(!((page_offset(seg_inode) - FSEG_ARR_OFFSET) % FSEG_INODE_SIZE));
  2521. /* Drop search system page hash index if the page is found in
  2522. the pool and is hashed */
  2523. btr_search_drop_page_hash_when_freed(space, zip_size, page);
  2524. descr = xdes_get_descriptor(space, zip_size, page, mtr);
  2525. SRV_CORRUPT_TABLE_CHECK(descr,
  2526. {
  2527. /* The page may be corrupt. pass it. */
  2528. return;
  2529. });
  2530. if (xdes_mtr_get_bit(descr, XDES_FREE_BIT,
  2531. page % FSP_EXTENT_SIZE, mtr)) {
  2532. fputs("InnoDB: Dump of the tablespace extent descriptor: ",
  2533. stderr);
  2534. ut_print_buf(stderr, descr, 40);
  2535. fprintf(stderr, "\n"
  2536. "InnoDB: Serious error! InnoDB is trying to"
  2537. " free page %lu\n"
  2538. "InnoDB: though it is already marked as free"
  2539. " in the tablespace!\n"
  2540. "InnoDB: The tablespace free space info is corrupt.\n"
  2541. "InnoDB: You may need to dump your"
  2542. " InnoDB tables and recreate the whole\n"
  2543. "InnoDB: database!\n", (ulong) page);
  2544. crash:
  2545. fputs("InnoDB: Please refer to\n"
  2546. "InnoDB: " REFMAN "forcing-innodb-recovery.html\n"
  2547. "InnoDB: about forcing recovery.\n", stderr);
  2548. ut_error;
  2549. }
  2550. state = xdes_get_state(descr, mtr);
  2551. if (state != XDES_FSEG) {
  2552. /* The page is in the fragment pages of the segment */
  2553. for (i = 0;; i++) {
  2554. if (fseg_get_nth_frag_page_no(seg_inode, i, mtr)
  2555. == page) {
  2556. fseg_set_nth_frag_page_no(seg_inode, i,
  2557. FIL_NULL, mtr);
  2558. break;
  2559. }
  2560. }
  2561. fsp_free_page(space, zip_size, page, mtr);
  2562. return;
  2563. }
  2564. /* If we get here, the page is in some extent of the segment */
  2565. descr_id = mach_read_from_8(descr + XDES_ID);
  2566. seg_id = mach_read_from_8(seg_inode + FSEG_ID);
  2567. #if 0
  2568. fprintf(stderr,
  2569. "InnoDB: InnoDB is freeing space %lu page %lu,\n"
  2570. "InnoDB: which belongs to descr seg %llu\n"
  2571. "InnoDB: segment %llu.\n",
  2572. (ulong) space, (ulong) page,
  2573. (ullint) descr_id,
  2574. (ullint) seg_id);
  2575. #endif /* 0 */
  2576. if (UNIV_UNLIKELY(descr_id != seg_id)) {
  2577. fputs("InnoDB: Dump of the tablespace extent descriptor: ",
  2578. stderr);
  2579. ut_print_buf(stderr, descr, 40);
  2580. fputs("\nInnoDB: Dump of the segment inode: ", stderr);
  2581. ut_print_buf(stderr, seg_inode, 40);
  2582. putc('\n', stderr);
  2583. fprintf(stderr,
  2584. "InnoDB: Serious error: InnoDB is trying to"
  2585. " free space %lu page %lu,\n"
  2586. "InnoDB: which does not belong to"
  2587. " segment %llu but belongs\n"
  2588. "InnoDB: to segment %llu.\n",
  2589. (ulong) space, (ulong) page,
  2590. (ullint) descr_id,
  2591. (ullint) seg_id);
  2592. goto crash;
  2593. }
  2594. not_full_n_used = mtr_read_ulint(seg_inode + FSEG_NOT_FULL_N_USED,
  2595. MLOG_4BYTES, mtr);
  2596. if (xdes_is_full(descr, mtr)) {
  2597. /* The fragment is full: move it to another list */
  2598. flst_remove(seg_inode + FSEG_FULL,
  2599. descr + XDES_FLST_NODE, mtr);
  2600. flst_add_last(seg_inode + FSEG_NOT_FULL,
  2601. descr + XDES_FLST_NODE, mtr);
  2602. mlog_write_ulint(seg_inode + FSEG_NOT_FULL_N_USED,
  2603. not_full_n_used + FSP_EXTENT_SIZE - 1,
  2604. MLOG_4BYTES, mtr);
  2605. } else {
  2606. ut_a(not_full_n_used > 0);
  2607. mlog_write_ulint(seg_inode + FSEG_NOT_FULL_N_USED,
  2608. not_full_n_used - 1, MLOG_4BYTES, mtr);
  2609. }
  2610. xdes_set_bit(descr, XDES_FREE_BIT, page % FSP_EXTENT_SIZE, TRUE, mtr);
  2611. xdes_set_bit(descr, XDES_CLEAN_BIT, page % FSP_EXTENT_SIZE, TRUE, mtr);
  2612. if (xdes_is_free(descr, mtr)) {
  2613. /* The extent has become free: free it to space */
  2614. flst_remove(seg_inode + FSEG_NOT_FULL,
  2615. descr + XDES_FLST_NODE, mtr);
  2616. fsp_free_extent(space, zip_size, page, mtr);
  2617. }
  2618. mtr->n_freed_pages++;
  2619. }
  2620. /**********************************************************************//**
  2621. Frees a single page of a segment. */
  2622. UNIV_INTERN
  2623. void
  2624. fseg_free_page(
  2625. /*===========*/
  2626. fseg_header_t* seg_header, /*!< in: segment header */
  2627. ulint space, /*!< in: space id */
  2628. ulint page, /*!< in: page offset */
  2629. mtr_t* mtr) /*!< in/out: mini-transaction */
  2630. {
  2631. ulint flags;
  2632. ulint zip_size;
  2633. fseg_inode_t* seg_inode;
  2634. prio_rw_lock_t* latch;
  2635. latch = fil_space_get_latch(space, &flags);
  2636. zip_size = fsp_flags_get_zip_size(flags);
  2637. mtr_x_lock(latch, mtr);
  2638. seg_inode = fseg_inode_get(seg_header, space, zip_size, mtr);
  2639. fseg_free_page_low(seg_inode, space, zip_size, page, mtr);
  2640. #if defined UNIV_DEBUG_FILE_ACCESSES || defined UNIV_DEBUG
  2641. buf_page_set_file_page_was_freed(space, page);
  2642. #endif /* UNIV_DEBUG_FILE_ACCESSES || UNIV_DEBUG */
  2643. }
  2644. /**********************************************************************//**
  2645. Checks if a single page of a segment is free.
  2646. @return true if free */
  2647. UNIV_INTERN
  2648. bool
  2649. fseg_page_is_free(
  2650. /*==============*/
  2651. fseg_header_t* seg_header, /*!< in: segment header */
  2652. ulint space, /*!< in: space id */
  2653. ulint page) /*!< in: page offset */
  2654. {
  2655. mtr_t mtr;
  2656. ibool is_free;
  2657. ulint flags;
  2658. prio_rw_lock_t* latch;
  2659. xdes_t* descr;
  2660. ulint zip_size;
  2661. fseg_inode_t* seg_inode;
  2662. latch = fil_space_get_latch(space, &flags);
  2663. zip_size = dict_tf_get_zip_size(flags);
  2664. mtr_start(&mtr);
  2665. mtr_x_lock(latch, &mtr);
  2666. seg_inode = fseg_inode_get(seg_header, space, zip_size, &mtr);
  2667. ut_a(seg_inode);
  2668. ut_ad(mach_read_from_4(seg_inode + FSEG_MAGIC_N)
  2669. == FSEG_MAGIC_N_VALUE);
  2670. ut_ad(!((page_offset(seg_inode) - FSEG_ARR_OFFSET) % FSEG_INODE_SIZE));
  2671. descr = xdes_get_descriptor(space, zip_size, page, &mtr);
  2672. ut_a(descr);
  2673. is_free = xdes_mtr_get_bit(
  2674. descr, XDES_FREE_BIT, page % FSP_EXTENT_SIZE, &mtr);
  2675. mtr_commit(&mtr);
  2676. return(is_free);
  2677. }
  2678. /**********************************************************************//**
  2679. Frees an extent of a segment to the space free list. */
  2680. static
  2681. void
  2682. fseg_free_extent(
  2683. /*=============*/
  2684. fseg_inode_t* seg_inode, /*!< in: segment inode */
  2685. ulint space, /*!< in: space id */
  2686. ulint zip_size,/*!< in: compressed page size in bytes
  2687. or 0 for uncompressed pages */
  2688. ulint page, /*!< in: a page in the extent */
  2689. mtr_t* mtr) /*!< in/out: mini-transaction */
  2690. {
  2691. ulint first_page_in_extent;
  2692. xdes_t* descr;
  2693. ulint not_full_n_used;
  2694. ulint descr_n_used;
  2695. ulint i;
  2696. ut_ad(seg_inode != NULL);
  2697. ut_ad(mtr != NULL);
  2698. descr = xdes_get_descriptor(space, zip_size, page, mtr);
  2699. ut_a(xdes_get_state(descr, mtr) == XDES_FSEG);
  2700. ut_a(!memcmp(descr + XDES_ID, seg_inode + FSEG_ID, 8));
  2701. ut_ad(mach_read_from_4(seg_inode + FSEG_MAGIC_N)
  2702. == FSEG_MAGIC_N_VALUE);
  2703. first_page_in_extent = page - (page % FSP_EXTENT_SIZE);
  2704. for (i = 0; i < FSP_EXTENT_SIZE; i++) {
  2705. if (!xdes_mtr_get_bit(descr, XDES_FREE_BIT, i, mtr)) {
  2706. /* Drop search system page hash index if the page is
  2707. found in the pool and is hashed */
  2708. btr_search_drop_page_hash_when_freed(
  2709. space, zip_size, first_page_in_extent + i);
  2710. }
  2711. }
  2712. if (xdes_is_full(descr, mtr)) {
  2713. flst_remove(seg_inode + FSEG_FULL,
  2714. descr + XDES_FLST_NODE, mtr);
  2715. } else if (xdes_is_free(descr, mtr)) {
  2716. flst_remove(seg_inode + FSEG_FREE,
  2717. descr + XDES_FLST_NODE, mtr);
  2718. } else {
  2719. flst_remove(seg_inode + FSEG_NOT_FULL,
  2720. descr + XDES_FLST_NODE, mtr);
  2721. not_full_n_used = mtr_read_ulint(
  2722. seg_inode + FSEG_NOT_FULL_N_USED, MLOG_4BYTES, mtr);
  2723. descr_n_used = xdes_get_n_used(descr, mtr);
  2724. ut_a(not_full_n_used >= descr_n_used);
  2725. mlog_write_ulint(seg_inode + FSEG_NOT_FULL_N_USED,
  2726. not_full_n_used - descr_n_used,
  2727. MLOG_4BYTES, mtr);
  2728. }
  2729. fsp_free_extent(space, zip_size, page, mtr);
  2730. #if defined UNIV_DEBUG_FILE_ACCESSES || defined UNIV_DEBUG
  2731. for (i = 0; i < FSP_EXTENT_SIZE; i++) {
  2732. buf_page_set_file_page_was_freed(space,
  2733. first_page_in_extent + i);
  2734. }
  2735. #endif /* UNIV_DEBUG_FILE_ACCESSES || UNIV_DEBUG */
  2736. }
  2737. /**********************************************************************//**
  2738. Frees part of a segment. This function can be used to free a segment by
  2739. repeatedly calling this function in different mini-transactions. Doing
  2740. the freeing in a single mini-transaction might result in too big a
  2741. mini-transaction.
  2742. @return TRUE if freeing completed */
  2743. UNIV_INTERN
  2744. ibool
  2745. fseg_free_step(
  2746. /*===========*/
  2747. fseg_header_t* header, /*!< in, own: segment header; NOTE: if the header
  2748. resides on the first page of the frag list
  2749. of the segment, this pointer becomes obsolete
  2750. after the last freeing step */
  2751. mtr_t* mtr) /*!< in/out: mini-transaction */
  2752. {
  2753. ulint n;
  2754. ulint page;
  2755. xdes_t* descr;
  2756. fseg_inode_t* inode;
  2757. ulint space;
  2758. ulint flags;
  2759. ulint zip_size;
  2760. ulint header_page;
  2761. prio_rw_lock_t* latch;
  2762. space = page_get_space_id(page_align(header));
  2763. header_page = page_get_page_no(page_align(header));
  2764. latch = fil_space_get_latch(space, &flags);
  2765. zip_size = fsp_flags_get_zip_size(flags);
  2766. mtr_x_lock(latch, mtr);
  2767. descr = xdes_get_descriptor(space, zip_size, header_page, mtr);
  2768. SRV_CORRUPT_TABLE_CHECK(descr,
  2769. {
  2770. /* The page may be corrupt. pass it. */
  2771. return(TRUE);
  2772. });
  2773. /* Check that the header resides on a page which has not been
  2774. freed yet */
  2775. ut_a(xdes_mtr_get_bit(descr, XDES_FREE_BIT,
  2776. header_page % FSP_EXTENT_SIZE, mtr) == FALSE);
  2777. inode = fseg_inode_try_get(header, space, zip_size, mtr);
  2778. if (UNIV_UNLIKELY(inode == NULL)) {
  2779. fprintf(stderr, "double free of inode from %u:%u\n",
  2780. (unsigned) space, (unsigned) header_page);
  2781. return(TRUE);
  2782. }
  2783. descr = fseg_get_first_extent(inode, space, zip_size, mtr);
  2784. if (descr != NULL) {
  2785. /* Free the extent held by the segment */
  2786. page = xdes_get_offset(descr);
  2787. fseg_free_extent(inode, space, zip_size, page, mtr);
  2788. return(FALSE);
  2789. }
  2790. /* Free a frag page */
  2791. n = fseg_find_last_used_frag_page_slot(inode, mtr);
  2792. if (n == ULINT_UNDEFINED) {
  2793. /* Freeing completed: free the segment inode */
  2794. fsp_free_seg_inode(space, zip_size, inode, mtr);
  2795. return(TRUE);
  2796. }
  2797. fseg_free_page_low(inode, space, zip_size,
  2798. fseg_get_nth_frag_page_no(inode, n, mtr), mtr);
  2799. n = fseg_find_last_used_frag_page_slot(inode, mtr);
  2800. if (n == ULINT_UNDEFINED) {
  2801. /* Freeing completed: free the segment inode */
  2802. fsp_free_seg_inode(space, zip_size, inode, mtr);
  2803. return(TRUE);
  2804. }
  2805. return(FALSE);
  2806. }
  2807. /**********************************************************************//**
  2808. Frees part of a segment. Differs from fseg_free_step because this function
  2809. leaves the header page unfreed.
  2810. @return TRUE if freeing completed, except the header page */
  2811. UNIV_INTERN
  2812. ibool
  2813. fseg_free_step_not_header(
  2814. /*======================*/
  2815. fseg_header_t* header, /*!< in: segment header which must reside on
  2816. the first fragment page of the segment */
  2817. mtr_t* mtr) /*!< in/out: mini-transaction */
  2818. {
  2819. ulint n;
  2820. ulint page;
  2821. xdes_t* descr;
  2822. fseg_inode_t* inode;
  2823. ulint space;
  2824. ulint flags;
  2825. ulint zip_size;
  2826. ulint page_no;
  2827. prio_rw_lock_t* latch;
  2828. space = page_get_space_id(page_align(header));
  2829. latch = fil_space_get_latch(space, &flags);
  2830. zip_size = fsp_flags_get_zip_size(flags);
  2831. mtr_x_lock(latch, mtr);
  2832. inode = fseg_inode_get(header, space, zip_size, mtr);
  2833. SRV_CORRUPT_TABLE_CHECK(inode,
  2834. {
  2835. /* ignore the corruption */
  2836. return(TRUE);
  2837. });
  2838. descr = fseg_get_first_extent(inode, space, zip_size, mtr);
  2839. if (descr != NULL) {
  2840. /* Free the extent held by the segment */
  2841. page = xdes_get_offset(descr);
  2842. fseg_free_extent(inode, space, zip_size, page, mtr);
  2843. return(FALSE);
  2844. }
  2845. /* Free a frag page */
  2846. n = fseg_find_last_used_frag_page_slot(inode, mtr);
  2847. if (n == ULINT_UNDEFINED) {
  2848. ut_error;
  2849. }
  2850. page_no = fseg_get_nth_frag_page_no(inode, n, mtr);
  2851. if (page_no == page_get_page_no(page_align(header))) {
  2852. return(TRUE);
  2853. }
  2854. fseg_free_page_low(inode, space, zip_size, page_no, mtr);
  2855. return(FALSE);
  2856. }
  2857. /**********************************************************************//**
  2858. Returns the first extent descriptor for a segment. We think of the extent
  2859. lists of the segment catenated in the order FSEG_FULL -> FSEG_NOT_FULL
  2860. -> FSEG_FREE.
  2861. @return the first extent descriptor, or NULL if none */
  2862. static
  2863. xdes_t*
  2864. fseg_get_first_extent(
  2865. /*==================*/
  2866. fseg_inode_t* inode, /*!< in: segment inode */
  2867. ulint space, /*!< in: space id */
  2868. ulint zip_size,/*!< in: compressed page size in bytes
  2869. or 0 for uncompressed pages */
  2870. mtr_t* mtr) /*!< in/out: mini-transaction */
  2871. {
  2872. fil_addr_t first;
  2873. xdes_t* descr;
  2874. ut_ad(inode && mtr);
  2875. ut_ad(space == page_get_space_id(page_align(inode)));
  2876. ut_ad(mach_read_from_4(inode + FSEG_MAGIC_N) == FSEG_MAGIC_N_VALUE);
  2877. first = fil_addr_null;
  2878. if (flst_get_len(inode + FSEG_FULL, mtr) > 0) {
  2879. first = flst_get_first(inode + FSEG_FULL, mtr);
  2880. } else if (flst_get_len(inode + FSEG_NOT_FULL, mtr) > 0) {
  2881. first = flst_get_first(inode + FSEG_NOT_FULL, mtr);
  2882. } else if (flst_get_len(inode + FSEG_FREE, mtr) > 0) {
  2883. first = flst_get_first(inode + FSEG_FREE, mtr);
  2884. }
  2885. if (first.page == FIL_NULL) {
  2886. return(NULL);
  2887. }
  2888. descr = xdes_lst_get_descriptor(space, zip_size, first, mtr);
  2889. return(descr);
  2890. }
  2891. /*******************************************************************//**
  2892. Validates a segment.
  2893. @return TRUE if ok */
  2894. static
  2895. ibool
  2896. fseg_validate_low(
  2897. /*==============*/
  2898. fseg_inode_t* inode, /*!< in: segment inode */
  2899. mtr_t* mtr2) /*!< in/out: mini-transaction */
  2900. {
  2901. ulint space;
  2902. ib_id_t seg_id;
  2903. mtr_t mtr;
  2904. xdes_t* descr;
  2905. fil_addr_t node_addr;
  2906. ulint n_used = 0;
  2907. ulint n_used2 = 0;
  2908. ut_ad(mtr_memo_contains_page(mtr2, inode, MTR_MEMO_PAGE_X_FIX));
  2909. ut_ad(mach_read_from_4(inode + FSEG_MAGIC_N) == FSEG_MAGIC_N_VALUE);
  2910. space = page_get_space_id(page_align(inode));
  2911. seg_id = mach_read_from_8(inode + FSEG_ID);
  2912. n_used = mtr_read_ulint(inode + FSEG_NOT_FULL_N_USED,
  2913. MLOG_4BYTES, mtr2);
  2914. flst_validate(inode + FSEG_FREE, mtr2);
  2915. flst_validate(inode + FSEG_NOT_FULL, mtr2);
  2916. flst_validate(inode + FSEG_FULL, mtr2);
  2917. /* Validate FSEG_FREE list */
  2918. node_addr = flst_get_first(inode + FSEG_FREE, mtr2);
  2919. while (!fil_addr_is_null(node_addr)) {
  2920. ulint flags;
  2921. ulint zip_size;
  2922. mtr_start(&mtr);
  2923. mtr_x_lock(fil_space_get_latch(space, &flags), &mtr);
  2924. zip_size = fsp_flags_get_zip_size(flags);
  2925. descr = xdes_lst_get_descriptor(space, zip_size,
  2926. node_addr, &mtr);
  2927. ut_a(xdes_get_n_used(descr, &mtr) == 0);
  2928. ut_a(xdes_get_state(descr, &mtr) == XDES_FSEG);
  2929. ut_a(mach_read_from_8(descr + XDES_ID) == seg_id);
  2930. node_addr = flst_get_next_addr(descr + XDES_FLST_NODE, &mtr);
  2931. mtr_commit(&mtr);
  2932. }
  2933. /* Validate FSEG_NOT_FULL list */
  2934. node_addr = flst_get_first(inode + FSEG_NOT_FULL, mtr2);
  2935. while (!fil_addr_is_null(node_addr)) {
  2936. ulint flags;
  2937. ulint zip_size;
  2938. mtr_start(&mtr);
  2939. mtr_x_lock(fil_space_get_latch(space, &flags), &mtr);
  2940. zip_size = fsp_flags_get_zip_size(flags);
  2941. descr = xdes_lst_get_descriptor(space, zip_size,
  2942. node_addr, &mtr);
  2943. ut_a(xdes_get_n_used(descr, &mtr) > 0);
  2944. ut_a(xdes_get_n_used(descr, &mtr) < FSP_EXTENT_SIZE);
  2945. ut_a(xdes_get_state(descr, &mtr) == XDES_FSEG);
  2946. ut_a(mach_read_from_8(descr + XDES_ID) == seg_id);
  2947. n_used2 += xdes_get_n_used(descr, &mtr);
  2948. node_addr = flst_get_next_addr(descr + XDES_FLST_NODE, &mtr);
  2949. mtr_commit(&mtr);
  2950. }
  2951. /* Validate FSEG_FULL list */
  2952. node_addr = flst_get_first(inode + FSEG_FULL, mtr2);
  2953. while (!fil_addr_is_null(node_addr)) {
  2954. ulint flags;
  2955. ulint zip_size;
  2956. mtr_start(&mtr);
  2957. mtr_x_lock(fil_space_get_latch(space, &flags), &mtr);
  2958. zip_size = fsp_flags_get_zip_size(flags);
  2959. descr = xdes_lst_get_descriptor(space, zip_size,
  2960. node_addr, &mtr);
  2961. ut_a(xdes_get_n_used(descr, &mtr) == FSP_EXTENT_SIZE);
  2962. ut_a(xdes_get_state(descr, &mtr) == XDES_FSEG);
  2963. ut_a(mach_read_from_8(descr + XDES_ID) == seg_id);
  2964. node_addr = flst_get_next_addr(descr + XDES_FLST_NODE, &mtr);
  2965. mtr_commit(&mtr);
  2966. }
  2967. ut_a(n_used == n_used2);
  2968. return(TRUE);
  2969. }
  2970. #ifdef UNIV_DEBUG
  2971. /*******************************************************************//**
  2972. Validates a segment.
  2973. @return TRUE if ok */
  2974. UNIV_INTERN
  2975. ibool
  2976. fseg_validate(
  2977. /*==========*/
  2978. fseg_header_t* header, /*!< in: segment header */
  2979. mtr_t* mtr) /*!< in/out: mini-transaction */
  2980. {
  2981. fseg_inode_t* inode;
  2982. ibool ret;
  2983. ulint space;
  2984. ulint flags;
  2985. ulint zip_size;
  2986. space = page_get_space_id(page_align(header));
  2987. mtr_x_lock(fil_space_get_latch(space, &flags), mtr);
  2988. zip_size = fsp_flags_get_zip_size(flags);
  2989. inode = fseg_inode_get(header, space, zip_size, mtr);
  2990. ret = fseg_validate_low(inode, mtr);
  2991. return(ret);
  2992. }
  2993. #endif /* UNIV_DEBUG */
  2994. /*******************************************************************//**
  2995. Writes info of a segment. */
  2996. static
  2997. void
  2998. fseg_print_low(
  2999. /*===========*/
  3000. fseg_inode_t* inode, /*!< in: segment inode */
  3001. mtr_t* mtr) /*!< in/out: mini-transaction */
  3002. {
  3003. ulint space;
  3004. ulint n_used;
  3005. ulint n_frag;
  3006. ulint n_free;
  3007. ulint n_not_full;
  3008. ulint n_full;
  3009. ulint reserved;
  3010. ulint used;
  3011. ulint page_no;
  3012. ib_id_t seg_id;
  3013. ut_ad(mtr_memo_contains_page(mtr, inode, MTR_MEMO_PAGE_X_FIX));
  3014. space = page_get_space_id(page_align(inode));
  3015. page_no = page_get_page_no(page_align(inode));
  3016. reserved = fseg_n_reserved_pages_low(inode, &used, mtr);
  3017. seg_id = mach_read_from_8(inode + FSEG_ID);
  3018. n_used = mtr_read_ulint(inode + FSEG_NOT_FULL_N_USED,
  3019. MLOG_4BYTES, mtr);
  3020. n_frag = fseg_get_n_frag_pages(inode, mtr);
  3021. n_free = flst_get_len(inode + FSEG_FREE, mtr);
  3022. n_not_full = flst_get_len(inode + FSEG_NOT_FULL, mtr);
  3023. n_full = flst_get_len(inode + FSEG_FULL, mtr);
  3024. fprintf(stderr,
  3025. "SEGMENT id %llu space %lu; page %lu;"
  3026. " res %lu used %lu; full ext %lu\n"
  3027. "fragm pages %lu; free extents %lu;"
  3028. " not full extents %lu: pages %lu\n",
  3029. (ullint) seg_id,
  3030. (ulong) space, (ulong) page_no,
  3031. (ulong) reserved, (ulong) used, (ulong) n_full,
  3032. (ulong) n_frag, (ulong) n_free, (ulong) n_not_full,
  3033. (ulong) n_used);
  3034. ut_ad(mach_read_from_4(inode + FSEG_MAGIC_N) == FSEG_MAGIC_N_VALUE);
  3035. }
  3036. #ifdef UNIV_BTR_PRINT
  3037. /*******************************************************************//**
  3038. Writes info of a segment. */
  3039. UNIV_INTERN
  3040. void
  3041. fseg_print(
  3042. /*=======*/
  3043. fseg_header_t* header, /*!< in: segment header */
  3044. mtr_t* mtr) /*!< in/out: mini-transaction */
  3045. {
  3046. fseg_inode_t* inode;
  3047. ulint space;
  3048. ulint flags;
  3049. ulint zip_size;
  3050. space = page_get_space_id(page_align(header));
  3051. mtr_x_lock(fil_space_get_latch(space, &flags), mtr);
  3052. zip_size = fsp_flags_get_zip_size(flags);
  3053. inode = fseg_inode_get(header, space, zip_size, mtr);
  3054. fseg_print_low(inode, mtr);
  3055. }
  3056. #endif /* UNIV_BTR_PRINT */
  3057. /*******************************************************************//**
  3058. Validates the file space system and its segments.
  3059. @return TRUE if ok */
  3060. UNIV_INTERN
  3061. ibool
  3062. fsp_validate(
  3063. /*=========*/
  3064. ulint space) /*!< in: space id */
  3065. {
  3066. fsp_header_t* header;
  3067. fseg_inode_t* seg_inode;
  3068. page_t* seg_inode_page;
  3069. prio_rw_lock_t* latch;
  3070. ulint size;
  3071. ulint flags;
  3072. ulint zip_size;
  3073. ulint free_limit;
  3074. ulint frag_n_used;
  3075. mtr_t mtr;
  3076. mtr_t mtr2;
  3077. xdes_t* descr;
  3078. fil_addr_t node_addr;
  3079. fil_addr_t next_node_addr;
  3080. ulint descr_count = 0;
  3081. ulint n_used = 0;
  3082. ulint n_used2 = 0;
  3083. ulint n_full_frag_pages;
  3084. ulint n;
  3085. ulint seg_inode_len_free;
  3086. ulint seg_inode_len_full;
  3087. latch = fil_space_get_latch(space, &flags);
  3088. zip_size = fsp_flags_get_zip_size(flags);
  3089. ut_a(ut_is_2pow(zip_size));
  3090. ut_a(zip_size <= UNIV_ZIP_SIZE_MAX);
  3091. ut_a(!zip_size || zip_size >= UNIV_ZIP_SIZE_MIN);
  3092. /* Start first a mini-transaction mtr2 to lock out all other threads
  3093. from the fsp system */
  3094. mtr_start(&mtr2);
  3095. mtr_x_lock(latch, &mtr2);
  3096. mtr_start(&mtr);
  3097. mtr_x_lock(latch, &mtr);
  3098. header = fsp_get_space_header(space, zip_size, &mtr);
  3099. size = mtr_read_ulint(header + FSP_SIZE, MLOG_4BYTES, &mtr);
  3100. free_limit = mtr_read_ulint(header + FSP_FREE_LIMIT,
  3101. MLOG_4BYTES, &mtr);
  3102. frag_n_used = mtr_read_ulint(header + FSP_FRAG_N_USED,
  3103. MLOG_4BYTES, &mtr);
  3104. n_full_frag_pages = FSP_EXTENT_SIZE
  3105. * flst_get_len(header + FSP_FULL_FRAG, &mtr);
  3106. if (UNIV_UNLIKELY(free_limit > size)) {
  3107. ut_a(space != 0);
  3108. ut_a(size < FSP_EXTENT_SIZE);
  3109. }
  3110. flst_validate(header + FSP_FREE, &mtr);
  3111. flst_validate(header + FSP_FREE_FRAG, &mtr);
  3112. flst_validate(header + FSP_FULL_FRAG, &mtr);
  3113. mtr_commit(&mtr);
  3114. /* Validate FSP_FREE list */
  3115. mtr_start(&mtr);
  3116. mtr_x_lock(latch, &mtr);
  3117. header = fsp_get_space_header(space, zip_size, &mtr);
  3118. node_addr = flst_get_first(header + FSP_FREE, &mtr);
  3119. mtr_commit(&mtr);
  3120. while (!fil_addr_is_null(node_addr)) {
  3121. mtr_start(&mtr);
  3122. mtr_x_lock(latch, &mtr);
  3123. descr_count++;
  3124. descr = xdes_lst_get_descriptor(space, zip_size,
  3125. node_addr, &mtr);
  3126. ut_a(xdes_get_n_used(descr, &mtr) == 0);
  3127. ut_a(xdes_get_state(descr, &mtr) == XDES_FREE);
  3128. node_addr = flst_get_next_addr(descr + XDES_FLST_NODE, &mtr);
  3129. mtr_commit(&mtr);
  3130. }
  3131. /* Validate FSP_FREE_FRAG list */
  3132. mtr_start(&mtr);
  3133. mtr_x_lock(latch, &mtr);
  3134. header = fsp_get_space_header(space, zip_size, &mtr);
  3135. node_addr = flst_get_first(header + FSP_FREE_FRAG, &mtr);
  3136. mtr_commit(&mtr);
  3137. while (!fil_addr_is_null(node_addr)) {
  3138. mtr_start(&mtr);
  3139. mtr_x_lock(latch, &mtr);
  3140. descr_count++;
  3141. descr = xdes_lst_get_descriptor(space, zip_size,
  3142. node_addr, &mtr);
  3143. ut_a(xdes_get_n_used(descr, &mtr) > 0);
  3144. ut_a(xdes_get_n_used(descr, &mtr) < FSP_EXTENT_SIZE);
  3145. ut_a(xdes_get_state(descr, &mtr) == XDES_FREE_FRAG);
  3146. n_used += xdes_get_n_used(descr, &mtr);
  3147. node_addr = flst_get_next_addr(descr + XDES_FLST_NODE, &mtr);
  3148. mtr_commit(&mtr);
  3149. }
  3150. /* Validate FSP_FULL_FRAG list */
  3151. mtr_start(&mtr);
  3152. mtr_x_lock(latch, &mtr);
  3153. header = fsp_get_space_header(space, zip_size, &mtr);
  3154. node_addr = flst_get_first(header + FSP_FULL_FRAG, &mtr);
  3155. mtr_commit(&mtr);
  3156. while (!fil_addr_is_null(node_addr)) {
  3157. mtr_start(&mtr);
  3158. mtr_x_lock(latch, &mtr);
  3159. descr_count++;
  3160. descr = xdes_lst_get_descriptor(space, zip_size,
  3161. node_addr, &mtr);
  3162. ut_a(xdes_get_n_used(descr, &mtr) == FSP_EXTENT_SIZE);
  3163. ut_a(xdes_get_state(descr, &mtr) == XDES_FULL_FRAG);
  3164. node_addr = flst_get_next_addr(descr + XDES_FLST_NODE, &mtr);
  3165. mtr_commit(&mtr);
  3166. }
  3167. /* Validate segments */
  3168. mtr_start(&mtr);
  3169. mtr_x_lock(latch, &mtr);
  3170. header = fsp_get_space_header(space, zip_size, &mtr);
  3171. node_addr = flst_get_first(header + FSP_SEG_INODES_FULL, &mtr);
  3172. seg_inode_len_full = flst_get_len(header + FSP_SEG_INODES_FULL, &mtr);
  3173. mtr_commit(&mtr);
  3174. while (!fil_addr_is_null(node_addr)) {
  3175. n = 0;
  3176. do {
  3177. mtr_start(&mtr);
  3178. mtr_x_lock(latch, &mtr);
  3179. seg_inode_page = fut_get_ptr(
  3180. space, zip_size, node_addr, RW_X_LATCH, &mtr)
  3181. - FSEG_INODE_PAGE_NODE;
  3182. seg_inode = fsp_seg_inode_page_get_nth_inode(
  3183. seg_inode_page, n, zip_size, &mtr);
  3184. ut_a(mach_read_from_8(seg_inode + FSEG_ID) != 0);
  3185. fseg_validate_low(seg_inode, &mtr);
  3186. descr_count += flst_get_len(seg_inode + FSEG_FREE,
  3187. &mtr);
  3188. descr_count += flst_get_len(seg_inode + FSEG_FULL,
  3189. &mtr);
  3190. descr_count += flst_get_len(seg_inode + FSEG_NOT_FULL,
  3191. &mtr);
  3192. n_used2 += fseg_get_n_frag_pages(seg_inode, &mtr);
  3193. next_node_addr = flst_get_next_addr(
  3194. seg_inode_page + FSEG_INODE_PAGE_NODE, &mtr);
  3195. mtr_commit(&mtr);
  3196. } while (++n < FSP_SEG_INODES_PER_PAGE(zip_size));
  3197. node_addr = next_node_addr;
  3198. }
  3199. mtr_start(&mtr);
  3200. mtr_x_lock(latch, &mtr);
  3201. header = fsp_get_space_header(space, zip_size, &mtr);
  3202. node_addr = flst_get_first(header + FSP_SEG_INODES_FREE, &mtr);
  3203. seg_inode_len_free = flst_get_len(header + FSP_SEG_INODES_FREE, &mtr);
  3204. mtr_commit(&mtr);
  3205. while (!fil_addr_is_null(node_addr)) {
  3206. n = 0;
  3207. do {
  3208. mtr_start(&mtr);
  3209. mtr_x_lock(latch, &mtr);
  3210. seg_inode_page = fut_get_ptr(
  3211. space, zip_size, node_addr, RW_X_LATCH, &mtr)
  3212. - FSEG_INODE_PAGE_NODE;
  3213. seg_inode = fsp_seg_inode_page_get_nth_inode(
  3214. seg_inode_page, n, zip_size, &mtr);
  3215. if (mach_read_from_8(seg_inode + FSEG_ID)) {
  3216. fseg_validate_low(seg_inode, &mtr);
  3217. descr_count += flst_get_len(
  3218. seg_inode + FSEG_FREE, &mtr);
  3219. descr_count += flst_get_len(
  3220. seg_inode + FSEG_FULL, &mtr);
  3221. descr_count += flst_get_len(
  3222. seg_inode + FSEG_NOT_FULL, &mtr);
  3223. n_used2 += fseg_get_n_frag_pages(
  3224. seg_inode, &mtr);
  3225. }
  3226. next_node_addr = flst_get_next_addr(
  3227. seg_inode_page + FSEG_INODE_PAGE_NODE, &mtr);
  3228. mtr_commit(&mtr);
  3229. } while (++n < FSP_SEG_INODES_PER_PAGE(zip_size));
  3230. node_addr = next_node_addr;
  3231. }
  3232. ut_a(descr_count * FSP_EXTENT_SIZE == free_limit);
  3233. if (!zip_size) {
  3234. ut_a(n_used + n_full_frag_pages
  3235. == n_used2 + 2 * ((free_limit + (UNIV_PAGE_SIZE - 1))
  3236. / UNIV_PAGE_SIZE)
  3237. + seg_inode_len_full + seg_inode_len_free);
  3238. } else {
  3239. ut_a(n_used + n_full_frag_pages
  3240. == n_used2 + 2 * ((free_limit + (zip_size - 1))
  3241. / zip_size)
  3242. + seg_inode_len_full + seg_inode_len_free);
  3243. }
  3244. ut_a(frag_n_used == n_used);
  3245. mtr_commit(&mtr2);
  3246. return(TRUE);
  3247. }
  3248. /*******************************************************************//**
  3249. Prints info of a file space. */
  3250. UNIV_INTERN
  3251. void
  3252. fsp_print(
  3253. /*======*/
  3254. ulint space) /*!< in: space id */
  3255. {
  3256. fsp_header_t* header;
  3257. fseg_inode_t* seg_inode;
  3258. page_t* seg_inode_page;
  3259. prio_rw_lock_t* latch;
  3260. ulint flags;
  3261. ulint zip_size;
  3262. ulint size;
  3263. ulint free_limit;
  3264. ulint frag_n_used;
  3265. fil_addr_t node_addr;
  3266. fil_addr_t next_node_addr;
  3267. ulint n_free;
  3268. ulint n_free_frag;
  3269. ulint n_full_frag;
  3270. ib_id_t seg_id;
  3271. ulint n;
  3272. ulint n_segs = 0;
  3273. mtr_t mtr;
  3274. mtr_t mtr2;
  3275. latch = fil_space_get_latch(space, &flags);
  3276. zip_size = fsp_flags_get_zip_size(flags);
  3277. /* Start first a mini-transaction mtr2 to lock out all other threads
  3278. from the fsp system */
  3279. mtr_start(&mtr2);
  3280. mtr_x_lock(latch, &mtr2);
  3281. mtr_start(&mtr);
  3282. mtr_x_lock(latch, &mtr);
  3283. header = fsp_get_space_header(space, zip_size, &mtr);
  3284. size = mtr_read_ulint(header + FSP_SIZE, MLOG_4BYTES, &mtr);
  3285. free_limit = mtr_read_ulint(header + FSP_FREE_LIMIT, MLOG_4BYTES,
  3286. &mtr);
  3287. frag_n_used = mtr_read_ulint(header + FSP_FRAG_N_USED, MLOG_4BYTES,
  3288. &mtr);
  3289. n_free = flst_get_len(header + FSP_FREE, &mtr);
  3290. n_free_frag = flst_get_len(header + FSP_FREE_FRAG, &mtr);
  3291. n_full_frag = flst_get_len(header + FSP_FULL_FRAG, &mtr);
  3292. seg_id = mach_read_from_8(header + FSP_SEG_ID);
  3293. fprintf(stderr,
  3294. "FILE SPACE INFO: id %lu\n"
  3295. "size %lu, free limit %lu, free extents %lu\n"
  3296. "not full frag extents %lu: used pages %lu,"
  3297. " full frag extents %lu\n"
  3298. "first seg id not used %llu\n",
  3299. (ulong) space,
  3300. (ulong) size, (ulong) free_limit, (ulong) n_free,
  3301. (ulong) n_free_frag, (ulong) frag_n_used, (ulong) n_full_frag,
  3302. (ullint) seg_id);
  3303. mtr_commit(&mtr);
  3304. /* Print segments */
  3305. mtr_start(&mtr);
  3306. mtr_x_lock(latch, &mtr);
  3307. header = fsp_get_space_header(space, zip_size, &mtr);
  3308. node_addr = flst_get_first(header + FSP_SEG_INODES_FULL, &mtr);
  3309. mtr_commit(&mtr);
  3310. while (!fil_addr_is_null(node_addr)) {
  3311. n = 0;
  3312. do {
  3313. mtr_start(&mtr);
  3314. mtr_x_lock(latch, &mtr);
  3315. seg_inode_page = fut_get_ptr(
  3316. space, zip_size, node_addr, RW_X_LATCH, &mtr)
  3317. - FSEG_INODE_PAGE_NODE;
  3318. seg_inode = fsp_seg_inode_page_get_nth_inode(
  3319. seg_inode_page, n, zip_size, &mtr);
  3320. ut_a(mach_read_from_8(seg_inode + FSEG_ID) != 0);
  3321. fseg_print_low(seg_inode, &mtr);
  3322. n_segs++;
  3323. next_node_addr = flst_get_next_addr(
  3324. seg_inode_page + FSEG_INODE_PAGE_NODE, &mtr);
  3325. mtr_commit(&mtr);
  3326. } while (++n < FSP_SEG_INODES_PER_PAGE(zip_size));
  3327. node_addr = next_node_addr;
  3328. }
  3329. mtr_start(&mtr);
  3330. mtr_x_lock(latch, &mtr);
  3331. header = fsp_get_space_header(space, zip_size, &mtr);
  3332. node_addr = flst_get_first(header + FSP_SEG_INODES_FREE, &mtr);
  3333. mtr_commit(&mtr);
  3334. while (!fil_addr_is_null(node_addr)) {
  3335. n = 0;
  3336. do {
  3337. mtr_start(&mtr);
  3338. mtr_x_lock(latch, &mtr);
  3339. seg_inode_page = fut_get_ptr(
  3340. space, zip_size, node_addr, RW_X_LATCH, &mtr)
  3341. - FSEG_INODE_PAGE_NODE;
  3342. seg_inode = fsp_seg_inode_page_get_nth_inode(
  3343. seg_inode_page, n, zip_size, &mtr);
  3344. if (mach_read_from_8(seg_inode + FSEG_ID)) {
  3345. fseg_print_low(seg_inode, &mtr);
  3346. n_segs++;
  3347. }
  3348. next_node_addr = flst_get_next_addr(
  3349. seg_inode_page + FSEG_INODE_PAGE_NODE, &mtr);
  3350. mtr_commit(&mtr);
  3351. } while (++n < FSP_SEG_INODES_PER_PAGE(zip_size));
  3352. node_addr = next_node_addr;
  3353. }
  3354. mtr_commit(&mtr2);
  3355. fprintf(stderr, "NUMBER of file segments: %lu\n", (ulong) n_segs);
  3356. }
  3357. #endif /* !UNIV_HOTBACKUP */