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.

1613 lines
43 KiB

  1. /*****************************************************************************
  2. Copyright (c) 1996, 2009, Innobase Oy. 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., 59 Temple
  11. Place, Suite 330, Boston, MA 02111-1307 USA
  12. *****************************************************************************/
  13. /**************************************************//**
  14. @file trx/trx0sys.c
  15. Transaction system
  16. Created 3/26/1996 Heikki Tuuri
  17. *******************************************************/
  18. #include "trx0sys.h"
  19. #ifdef UNIV_NONINL
  20. #include "trx0sys.ic"
  21. #endif
  22. #ifndef UNIV_HOTBACKUP
  23. #include "fsp0fsp.h"
  24. #include "mtr0log.h"
  25. #include "mtr0log.h"
  26. #include "trx0trx.h"
  27. #include "trx0rseg.h"
  28. #include "trx0undo.h"
  29. #include "srv0srv.h"
  30. #include "trx0purge.h"
  31. #include "log0log.h"
  32. #include "os0file.h"
  33. #include "read0read.h"
  34. /** The file format tag structure with id and name. */
  35. struct file_format_struct {
  36. ulint id; /*!< id of the file format */
  37. const char* name; /*!< text representation of the
  38. file format */
  39. mutex_t mutex; /*!< covers changes to the above
  40. fields */
  41. };
  42. /** The file format tag */
  43. typedef struct file_format_struct file_format_t;
  44. /** The transaction system */
  45. UNIV_INTERN trx_sys_t* trx_sys = NULL;
  46. /** The doublewrite buffer */
  47. UNIV_INTERN trx_doublewrite_t* trx_doublewrite = NULL;
  48. /** The following is set to TRUE when we are upgrading from pre-4.1
  49. format data files to the multiple tablespaces format data files */
  50. UNIV_INTERN ibool trx_doublewrite_must_reset_space_ids = FALSE;
  51. /** Set to TRUE when the doublewrite buffer is being created */
  52. UNIV_INTERN ibool trx_doublewrite_buf_is_being_created = FALSE;
  53. /** The following is TRUE when we are using the database in the
  54. post-4.1 format, i.e., we have successfully upgraded, or have created
  55. a new database installation */
  56. UNIV_INTERN ibool trx_sys_multiple_tablespace_format = FALSE;
  57. /** In a MySQL replication slave, in crash recovery we store the master log
  58. file name and position here. */
  59. /* @{ */
  60. /** Master binlog file name */
  61. UNIV_INTERN char trx_sys_mysql_master_log_name[TRX_SYS_MYSQL_LOG_NAME_LEN];
  62. /** Master binlog file position. We have successfully got the updates
  63. up to this position. -1 means that no crash recovery was needed, or
  64. there was no master log position info inside InnoDB.*/
  65. UNIV_INTERN ib_int64_t trx_sys_mysql_master_log_pos = -1;
  66. /* @} */
  67. /** If this MySQL server uses binary logging, after InnoDB has been inited
  68. and if it has done a crash recovery, we store the binlog file name and position
  69. here. */
  70. /* @{ */
  71. /** Binlog file name */
  72. UNIV_INTERN char trx_sys_mysql_bin_log_name[TRX_SYS_MYSQL_LOG_NAME_LEN];
  73. /** Binlog file position, or -1 if unknown */
  74. UNIV_INTERN ib_int64_t trx_sys_mysql_bin_log_pos = -1;
  75. /* @} */
  76. #endif /* !UNIV_HOTBACKUP */
  77. /** List of animal names representing file format. */
  78. static const char* file_format_name_map[] = {
  79. "Antelope",
  80. "Barracuda",
  81. "Cheetah",
  82. "Dragon",
  83. "Elk",
  84. "Fox",
  85. "Gazelle",
  86. "Hornet",
  87. "Impala",
  88. "Jaguar",
  89. "Kangaroo",
  90. "Leopard",
  91. "Moose",
  92. "Nautilus",
  93. "Ocelot",
  94. "Porpoise",
  95. "Quail",
  96. "Rabbit",
  97. "Shark",
  98. "Tiger",
  99. "Urchin",
  100. "Viper",
  101. "Whale",
  102. "Xenops",
  103. "Yak",
  104. "Zebra"
  105. };
  106. /** The number of elements in the file format name array. */
  107. static const ulint FILE_FORMAT_NAME_N
  108. = sizeof(file_format_name_map) / sizeof(file_format_name_map[0]);
  109. #ifndef UNIV_HOTBACKUP
  110. /** This is used to track the maximum file format id known to InnoDB. It's
  111. updated via SET GLOBAL innodb_file_format_check = 'x' or when we open
  112. or create a table. */
  113. static file_format_t file_format_max;
  114. /****************************************************************//**
  115. Determines if a page number is located inside the doublewrite buffer.
  116. @return TRUE if the location is inside the two blocks of the
  117. doublewrite buffer */
  118. UNIV_INTERN
  119. ibool
  120. trx_doublewrite_page_inside(
  121. /*========================*/
  122. ulint page_no) /*!< in: page number */
  123. {
  124. if (trx_doublewrite == NULL) {
  125. return(FALSE);
  126. }
  127. if (page_no >= trx_doublewrite->block1
  128. && page_no < trx_doublewrite->block1
  129. + TRX_SYS_DOUBLEWRITE_BLOCK_SIZE) {
  130. return(TRUE);
  131. }
  132. if (page_no >= trx_doublewrite->block2
  133. && page_no < trx_doublewrite->block2
  134. + TRX_SYS_DOUBLEWRITE_BLOCK_SIZE) {
  135. return(TRUE);
  136. }
  137. return(FALSE);
  138. }
  139. /****************************************************************//**
  140. Creates or initialializes the doublewrite buffer at a database start. */
  141. static
  142. void
  143. trx_doublewrite_init(
  144. /*=================*/
  145. byte* doublewrite) /*!< in: pointer to the doublewrite buf
  146. header on trx sys page */
  147. {
  148. trx_doublewrite = mem_alloc(sizeof(trx_doublewrite_t));
  149. /* Since we now start to use the doublewrite buffer, no need to call
  150. fsync() after every write to a data file */
  151. #ifdef UNIV_DO_FLUSH
  152. os_do_not_call_flush_at_each_write = TRUE;
  153. #endif /* UNIV_DO_FLUSH */
  154. mutex_create(&trx_doublewrite->mutex, SYNC_DOUBLEWRITE);
  155. trx_doublewrite->first_free = 0;
  156. trx_doublewrite->block1 = mach_read_from_4(
  157. doublewrite + TRX_SYS_DOUBLEWRITE_BLOCK1);
  158. trx_doublewrite->block2 = mach_read_from_4(
  159. doublewrite + TRX_SYS_DOUBLEWRITE_BLOCK2);
  160. trx_doublewrite->write_buf_unaligned = ut_malloc(
  161. (1 + 2 * TRX_SYS_DOUBLEWRITE_BLOCK_SIZE) * UNIV_PAGE_SIZE);
  162. trx_doublewrite->write_buf = ut_align(
  163. trx_doublewrite->write_buf_unaligned, UNIV_PAGE_SIZE);
  164. trx_doublewrite->buf_block_arr = mem_alloc(
  165. 2 * TRX_SYS_DOUBLEWRITE_BLOCK_SIZE * sizeof(void*));
  166. }
  167. /****************************************************************//**
  168. Marks the trx sys header when we have successfully upgraded to the >= 4.1.x
  169. multiple tablespace format. */
  170. UNIV_INTERN
  171. void
  172. trx_sys_mark_upgraded_to_multiple_tablespaces(void)
  173. /*===============================================*/
  174. {
  175. buf_block_t* block;
  176. byte* doublewrite;
  177. mtr_t mtr;
  178. /* We upgraded to 4.1.x and reset the space id fields in the
  179. doublewrite buffer. Let us mark to the trx_sys header that the upgrade
  180. has been done. */
  181. mtr_start(&mtr);
  182. block = buf_page_get(TRX_SYS_SPACE, 0, TRX_SYS_PAGE_NO,
  183. RW_X_LATCH, &mtr);
  184. buf_block_dbg_add_level(block, SYNC_NO_ORDER_CHECK);
  185. doublewrite = buf_block_get_frame(block) + TRX_SYS_DOUBLEWRITE;
  186. mlog_write_ulint(doublewrite + TRX_SYS_DOUBLEWRITE_SPACE_ID_STORED,
  187. TRX_SYS_DOUBLEWRITE_SPACE_ID_STORED_N,
  188. MLOG_4BYTES, &mtr);
  189. mtr_commit(&mtr);
  190. /* Flush the modified pages to disk and make a checkpoint */
  191. log_make_checkpoint_at(IB_ULONGLONG_MAX, TRUE);
  192. trx_sys_multiple_tablespace_format = TRUE;
  193. }
  194. /****************************************************************//**
  195. Creates the doublewrite buffer to a new InnoDB installation. The header of the
  196. doublewrite buffer is placed on the trx system header page. */
  197. UNIV_INTERN
  198. void
  199. trx_sys_create_doublewrite_buf(void)
  200. /*================================*/
  201. {
  202. buf_block_t* block;
  203. buf_block_t* block2;
  204. buf_block_t* new_block;
  205. byte* doublewrite;
  206. byte* fseg_header;
  207. ulint page_no;
  208. ulint prev_page_no;
  209. ulint i;
  210. mtr_t mtr;
  211. if (trx_doublewrite) {
  212. /* Already inited */
  213. return;
  214. }
  215. start_again:
  216. mtr_start(&mtr);
  217. trx_doublewrite_buf_is_being_created = TRUE;
  218. block = buf_page_get(TRX_SYS_SPACE, 0, TRX_SYS_PAGE_NO,
  219. RW_X_LATCH, &mtr);
  220. buf_block_dbg_add_level(block, SYNC_NO_ORDER_CHECK);
  221. doublewrite = buf_block_get_frame(block) + TRX_SYS_DOUBLEWRITE;
  222. if (mach_read_from_4(doublewrite + TRX_SYS_DOUBLEWRITE_MAGIC)
  223. == TRX_SYS_DOUBLEWRITE_MAGIC_N) {
  224. /* The doublewrite buffer has already been created:
  225. just read in some numbers */
  226. trx_doublewrite_init(doublewrite);
  227. mtr_commit(&mtr);
  228. trx_doublewrite_buf_is_being_created = FALSE;
  229. } else {
  230. fprintf(stderr,
  231. "InnoDB: Doublewrite buffer not found:"
  232. " creating new\n");
  233. if (buf_pool_get_curr_size()
  234. < ((2 * TRX_SYS_DOUBLEWRITE_BLOCK_SIZE
  235. + FSP_EXTENT_SIZE / 2 + 100)
  236. * UNIV_PAGE_SIZE)) {
  237. fprintf(stderr,
  238. "InnoDB: Cannot create doublewrite buffer:"
  239. " you must\n"
  240. "InnoDB: increase your buffer pool size.\n"
  241. "InnoDB: Cannot continue operation.\n");
  242. exit(1);
  243. }
  244. block2 = fseg_create(TRX_SYS_SPACE, TRX_SYS_PAGE_NO,
  245. TRX_SYS_DOUBLEWRITE
  246. + TRX_SYS_DOUBLEWRITE_FSEG, &mtr);
  247. /* fseg_create acquires a second latch on the page,
  248. therefore we must declare it: */
  249. buf_block_dbg_add_level(block2, SYNC_NO_ORDER_CHECK);
  250. if (block2 == NULL) {
  251. fprintf(stderr,
  252. "InnoDB: Cannot create doublewrite buffer:"
  253. " you must\n"
  254. "InnoDB: increase your tablespace size.\n"
  255. "InnoDB: Cannot continue operation.\n");
  256. /* We exit without committing the mtr to prevent
  257. its modifications to the database getting to disk */
  258. exit(1);
  259. }
  260. fseg_header = buf_block_get_frame(block)
  261. + TRX_SYS_DOUBLEWRITE + TRX_SYS_DOUBLEWRITE_FSEG;
  262. prev_page_no = 0;
  263. for (i = 0; i < 2 * TRX_SYS_DOUBLEWRITE_BLOCK_SIZE
  264. + FSP_EXTENT_SIZE / 2; i++) {
  265. page_no = fseg_alloc_free_page(fseg_header,
  266. prev_page_no + 1,
  267. FSP_UP, &mtr);
  268. if (page_no == FIL_NULL) {
  269. fprintf(stderr,
  270. "InnoDB: Cannot create doublewrite"
  271. " buffer: you must\n"
  272. "InnoDB: increase your"
  273. " tablespace size.\n"
  274. "InnoDB: Cannot continue operation.\n"
  275. );
  276. exit(1);
  277. }
  278. /* We read the allocated pages to the buffer pool;
  279. when they are written to disk in a flush, the space
  280. id and page number fields are also written to the
  281. pages. When we at database startup read pages
  282. from the doublewrite buffer, we know that if the
  283. space id and page number in them are the same as
  284. the page position in the tablespace, then the page
  285. has not been written to in doublewrite. */
  286. new_block = buf_page_get(TRX_SYS_SPACE, 0, page_no,
  287. RW_X_LATCH, &mtr);
  288. buf_block_dbg_add_level(new_block,
  289. SYNC_NO_ORDER_CHECK);
  290. if (i == FSP_EXTENT_SIZE / 2) {
  291. ut_a(page_no == FSP_EXTENT_SIZE);
  292. mlog_write_ulint(doublewrite
  293. + TRX_SYS_DOUBLEWRITE_BLOCK1,
  294. page_no, MLOG_4BYTES, &mtr);
  295. mlog_write_ulint(doublewrite
  296. + TRX_SYS_DOUBLEWRITE_REPEAT
  297. + TRX_SYS_DOUBLEWRITE_BLOCK1,
  298. page_no, MLOG_4BYTES, &mtr);
  299. } else if (i == FSP_EXTENT_SIZE / 2
  300. + TRX_SYS_DOUBLEWRITE_BLOCK_SIZE) {
  301. ut_a(page_no == 2 * FSP_EXTENT_SIZE);
  302. mlog_write_ulint(doublewrite
  303. + TRX_SYS_DOUBLEWRITE_BLOCK2,
  304. page_no, MLOG_4BYTES, &mtr);
  305. mlog_write_ulint(doublewrite
  306. + TRX_SYS_DOUBLEWRITE_REPEAT
  307. + TRX_SYS_DOUBLEWRITE_BLOCK2,
  308. page_no, MLOG_4BYTES, &mtr);
  309. } else if (i > FSP_EXTENT_SIZE / 2) {
  310. ut_a(page_no == prev_page_no + 1);
  311. }
  312. prev_page_no = page_no;
  313. }
  314. mlog_write_ulint(doublewrite + TRX_SYS_DOUBLEWRITE_MAGIC,
  315. TRX_SYS_DOUBLEWRITE_MAGIC_N,
  316. MLOG_4BYTES, &mtr);
  317. mlog_write_ulint(doublewrite + TRX_SYS_DOUBLEWRITE_MAGIC
  318. + TRX_SYS_DOUBLEWRITE_REPEAT,
  319. TRX_SYS_DOUBLEWRITE_MAGIC_N,
  320. MLOG_4BYTES, &mtr);
  321. mlog_write_ulint(doublewrite
  322. + TRX_SYS_DOUBLEWRITE_SPACE_ID_STORED,
  323. TRX_SYS_DOUBLEWRITE_SPACE_ID_STORED_N,
  324. MLOG_4BYTES, &mtr);
  325. mtr_commit(&mtr);
  326. /* Flush the modified pages to disk and make a checkpoint */
  327. log_make_checkpoint_at(IB_ULONGLONG_MAX, TRUE);
  328. fprintf(stderr, "InnoDB: Doublewrite buffer created\n");
  329. trx_sys_multiple_tablespace_format = TRUE;
  330. goto start_again;
  331. }
  332. }
  333. /****************************************************************//**
  334. At a database startup initializes the doublewrite buffer memory structure if
  335. we already have a doublewrite buffer created in the data files. If we are
  336. upgrading to an InnoDB version which supports multiple tablespaces, then this
  337. function performs the necessary update operations. If we are in a crash
  338. recovery, this function uses a possible doublewrite buffer to restore
  339. half-written pages in the data files. */
  340. UNIV_INTERN
  341. void
  342. trx_sys_doublewrite_init_or_restore_pages(
  343. /*======================================*/
  344. ibool restore_corrupt_pages) /*!< in: TRUE=restore pages */
  345. {
  346. byte* buf;
  347. byte* read_buf;
  348. byte* unaligned_read_buf;
  349. ulint block1;
  350. ulint block2;
  351. ulint source_page_no;
  352. byte* page;
  353. byte* doublewrite;
  354. ulint space_id;
  355. ulint page_no;
  356. ulint i;
  357. /* We do the file i/o past the buffer pool */
  358. unaligned_read_buf = ut_malloc(2 * UNIV_PAGE_SIZE);
  359. read_buf = ut_align(unaligned_read_buf, UNIV_PAGE_SIZE);
  360. /* Read the trx sys header to check if we are using the doublewrite
  361. buffer */
  362. fil_io(OS_FILE_READ, TRUE, TRX_SYS_SPACE, 0, TRX_SYS_PAGE_NO, 0,
  363. UNIV_PAGE_SIZE, read_buf, NULL);
  364. doublewrite = read_buf + TRX_SYS_DOUBLEWRITE;
  365. if (mach_read_from_4(doublewrite + TRX_SYS_DOUBLEWRITE_MAGIC)
  366. == TRX_SYS_DOUBLEWRITE_MAGIC_N) {
  367. /* The doublewrite buffer has been created */
  368. trx_doublewrite_init(doublewrite);
  369. block1 = trx_doublewrite->block1;
  370. block2 = trx_doublewrite->block2;
  371. buf = trx_doublewrite->write_buf;
  372. } else {
  373. goto leave_func;
  374. }
  375. if (mach_read_from_4(doublewrite + TRX_SYS_DOUBLEWRITE_SPACE_ID_STORED)
  376. != TRX_SYS_DOUBLEWRITE_SPACE_ID_STORED_N) {
  377. /* We are upgrading from a version < 4.1.x to a version where
  378. multiple tablespaces are supported. We must reset the space id
  379. field in the pages in the doublewrite buffer because starting
  380. from this version the space id is stored to
  381. FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID. */
  382. trx_doublewrite_must_reset_space_ids = TRUE;
  383. fprintf(stderr,
  384. "InnoDB: Resetting space id's in the"
  385. " doublewrite buffer\n");
  386. } else {
  387. trx_sys_multiple_tablespace_format = TRUE;
  388. }
  389. /* Read the pages from the doublewrite buffer to memory */
  390. fil_io(OS_FILE_READ, TRUE, TRX_SYS_SPACE, 0, block1, 0,
  391. TRX_SYS_DOUBLEWRITE_BLOCK_SIZE * UNIV_PAGE_SIZE,
  392. buf, NULL);
  393. fil_io(OS_FILE_READ, TRUE, TRX_SYS_SPACE, 0, block2, 0,
  394. TRX_SYS_DOUBLEWRITE_BLOCK_SIZE * UNIV_PAGE_SIZE,
  395. buf + TRX_SYS_DOUBLEWRITE_BLOCK_SIZE * UNIV_PAGE_SIZE,
  396. NULL);
  397. /* Check if any of these pages is half-written in data files, in the
  398. intended position */
  399. page = buf;
  400. for (i = 0; i < TRX_SYS_DOUBLEWRITE_BLOCK_SIZE * 2; i++) {
  401. page_no = mach_read_from_4(page + FIL_PAGE_OFFSET);
  402. if (trx_doublewrite_must_reset_space_ids) {
  403. space_id = 0;
  404. mach_write_to_4(page
  405. + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID, 0);
  406. /* We do not need to calculate new checksums for the
  407. pages because the field .._SPACE_ID does not affect
  408. them. Write the page back to where we read it from. */
  409. if (i < TRX_SYS_DOUBLEWRITE_BLOCK_SIZE) {
  410. source_page_no = block1 + i;
  411. } else {
  412. source_page_no = block2
  413. + i - TRX_SYS_DOUBLEWRITE_BLOCK_SIZE;
  414. }
  415. fil_io(OS_FILE_WRITE, TRUE, 0, 0, source_page_no, 0,
  416. UNIV_PAGE_SIZE, page, NULL);
  417. /* printf("Resetting space id in page %lu\n",
  418. source_page_no); */
  419. } else {
  420. space_id = mach_read_from_4(
  421. page + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID);
  422. }
  423. if (!restore_corrupt_pages) {
  424. /* The database was shut down gracefully: no need to
  425. restore pages */
  426. } else if (!fil_tablespace_exists_in_mem(space_id)) {
  427. /* Maybe we have dropped the single-table tablespace
  428. and this page once belonged to it: do nothing */
  429. } else if (!fil_check_adress_in_tablespace(space_id,
  430. page_no)) {
  431. fprintf(stderr,
  432. "InnoDB: Warning: a page in the"
  433. " doublewrite buffer is not within space\n"
  434. "InnoDB: bounds; space id %lu"
  435. " page number %lu, page %lu in"
  436. " doublewrite buf.\n",
  437. (ulong) space_id, (ulong) page_no, (ulong) i);
  438. } else if (space_id == TRX_SYS_SPACE
  439. && ((page_no >= block1
  440. && page_no
  441. < block1 + TRX_SYS_DOUBLEWRITE_BLOCK_SIZE)
  442. || (page_no >= block2
  443. && page_no
  444. < (block2
  445. + TRX_SYS_DOUBLEWRITE_BLOCK_SIZE)))) {
  446. /* It is an unwritten doublewrite buffer page:
  447. do nothing */
  448. } else {
  449. ulint zip_size = fil_space_get_zip_size(space_id);
  450. /* Read in the actual page from the file */
  451. fil_io(OS_FILE_READ, TRUE, space_id, zip_size,
  452. page_no, 0,
  453. zip_size ? zip_size : UNIV_PAGE_SIZE,
  454. read_buf, NULL);
  455. /* Check if the page is corrupt */
  456. if (UNIV_UNLIKELY
  457. (buf_page_is_corrupted(read_buf, zip_size))) {
  458. fprintf(stderr,
  459. "InnoDB: Warning: database page"
  460. " corruption or a failed\n"
  461. "InnoDB: file read of"
  462. " space %lu page %lu.\n"
  463. "InnoDB: Trying to recover it from"
  464. " the doublewrite buffer.\n",
  465. (ulong) space_id, (ulong) page_no);
  466. if (buf_page_is_corrupted(page, zip_size)) {
  467. fprintf(stderr,
  468. "InnoDB: Dump of the page:\n");
  469. buf_page_print(read_buf, zip_size);
  470. fprintf(stderr,
  471. "InnoDB: Dump of"
  472. " corresponding page"
  473. " in doublewrite buffer:\n");
  474. buf_page_print(page, zip_size);
  475. fprintf(stderr,
  476. "InnoDB: Also the page in the"
  477. " doublewrite buffer"
  478. " is corrupt.\n"
  479. "InnoDB: Cannot continue"
  480. " operation.\n"
  481. "InnoDB: You can try to"
  482. " recover the database"
  483. " with the my.cnf\n"
  484. "InnoDB: option:\n"
  485. "InnoDB: set-variable="
  486. "innodb_force_recovery=6\n");
  487. exit(1);
  488. }
  489. /* Write the good page from the
  490. doublewrite buffer to the intended
  491. position */
  492. fil_io(OS_FILE_WRITE, TRUE, space_id,
  493. zip_size, page_no, 0,
  494. zip_size ? zip_size : UNIV_PAGE_SIZE,
  495. page, NULL);
  496. fprintf(stderr,
  497. "InnoDB: Recovered the page from"
  498. " the doublewrite buffer.\n");
  499. }
  500. }
  501. page += UNIV_PAGE_SIZE;
  502. }
  503. fil_flush_file_spaces(FIL_TABLESPACE);
  504. leave_func:
  505. ut_free(unaligned_read_buf);
  506. }
  507. /****************************************************************//**
  508. Checks that trx is in the trx list.
  509. @return TRUE if is in */
  510. UNIV_INTERN
  511. ibool
  512. trx_in_trx_list(
  513. /*============*/
  514. trx_t* in_trx) /*!< in: trx */
  515. {
  516. trx_t* trx;
  517. ut_ad(mutex_own(&(kernel_mutex)));
  518. trx = UT_LIST_GET_FIRST(trx_sys->trx_list);
  519. while (trx != NULL) {
  520. if (trx == in_trx) {
  521. return(TRUE);
  522. }
  523. trx = UT_LIST_GET_NEXT(trx_list, trx);
  524. }
  525. return(FALSE);
  526. }
  527. /*****************************************************************//**
  528. Writes the value of max_trx_id to the file based trx system header. */
  529. UNIV_INTERN
  530. void
  531. trx_sys_flush_max_trx_id(void)
  532. /*==========================*/
  533. {
  534. trx_sysf_t* sys_header;
  535. mtr_t mtr;
  536. ut_ad(mutex_own(&kernel_mutex));
  537. mtr_start(&mtr);
  538. sys_header = trx_sysf_get(&mtr);
  539. mlog_write_dulint(sys_header + TRX_SYS_TRX_ID_STORE,
  540. trx_sys->max_trx_id, &mtr);
  541. mtr_commit(&mtr);
  542. }
  543. /*****************************************************************//**
  544. Updates the offset information about the end of the MySQL binlog entry
  545. which corresponds to the transaction just being committed. In a MySQL
  546. replication slave updates the latest master binlog position up to which
  547. replication has proceeded. */
  548. UNIV_INTERN
  549. void
  550. trx_sys_update_mysql_binlog_offset(
  551. /*===============================*/
  552. const char* file_name,/*!< in: MySQL log file name */
  553. ib_int64_t offset, /*!< in: position in that log file */
  554. ulint field, /*!< in: offset of the MySQL log info field in
  555. the trx sys header */
  556. mtr_t* mtr) /*!< in: mtr */
  557. {
  558. trx_sysf_t* sys_header;
  559. if (ut_strlen(file_name) >= TRX_SYS_MYSQL_LOG_NAME_LEN) {
  560. /* We cannot fit the name to the 512 bytes we have reserved */
  561. return;
  562. }
  563. sys_header = trx_sysf_get(mtr);
  564. if (mach_read_from_4(sys_header + field
  565. + TRX_SYS_MYSQL_LOG_MAGIC_N_FLD)
  566. != TRX_SYS_MYSQL_LOG_MAGIC_N) {
  567. mlog_write_ulint(sys_header + field
  568. + TRX_SYS_MYSQL_LOG_MAGIC_N_FLD,
  569. TRX_SYS_MYSQL_LOG_MAGIC_N,
  570. MLOG_4BYTES, mtr);
  571. }
  572. if (0 != strcmp((char*) (sys_header + field + TRX_SYS_MYSQL_LOG_NAME),
  573. file_name)) {
  574. mlog_write_string(sys_header + field
  575. + TRX_SYS_MYSQL_LOG_NAME,
  576. (byte*) file_name, 1 + ut_strlen(file_name),
  577. mtr);
  578. }
  579. if (mach_read_from_4(sys_header + field
  580. + TRX_SYS_MYSQL_LOG_OFFSET_HIGH) > 0
  581. || (offset >> 32) > 0) {
  582. mlog_write_ulint(sys_header + field
  583. + TRX_SYS_MYSQL_LOG_OFFSET_HIGH,
  584. (ulint)(offset >> 32),
  585. MLOG_4BYTES, mtr);
  586. }
  587. mlog_write_ulint(sys_header + field
  588. + TRX_SYS_MYSQL_LOG_OFFSET_LOW,
  589. (ulint)(offset & 0xFFFFFFFFUL),
  590. MLOG_4BYTES, mtr);
  591. }
  592. /*****************************************************************//**
  593. Stores the MySQL binlog offset info in the trx system header if
  594. the magic number shows it valid, and print the info to stderr */
  595. UNIV_INTERN
  596. void
  597. trx_sys_print_mysql_binlog_offset(void)
  598. /*===================================*/
  599. {
  600. trx_sysf_t* sys_header;
  601. mtr_t mtr;
  602. ulint trx_sys_mysql_bin_log_pos_high;
  603. ulint trx_sys_mysql_bin_log_pos_low;
  604. mtr_start(&mtr);
  605. sys_header = trx_sysf_get(&mtr);
  606. if (mach_read_from_4(sys_header + TRX_SYS_MYSQL_LOG_INFO
  607. + TRX_SYS_MYSQL_LOG_MAGIC_N_FLD)
  608. != TRX_SYS_MYSQL_LOG_MAGIC_N) {
  609. mtr_commit(&mtr);
  610. return;
  611. }
  612. trx_sys_mysql_bin_log_pos_high = mach_read_from_4(
  613. sys_header + TRX_SYS_MYSQL_LOG_INFO
  614. + TRX_SYS_MYSQL_LOG_OFFSET_HIGH);
  615. trx_sys_mysql_bin_log_pos_low = mach_read_from_4(
  616. sys_header + TRX_SYS_MYSQL_LOG_INFO
  617. + TRX_SYS_MYSQL_LOG_OFFSET_LOW);
  618. trx_sys_mysql_bin_log_pos
  619. = (((ib_int64_t)trx_sys_mysql_bin_log_pos_high) << 32)
  620. + (ib_int64_t)trx_sys_mysql_bin_log_pos_low;
  621. ut_memcpy(trx_sys_mysql_bin_log_name,
  622. sys_header + TRX_SYS_MYSQL_LOG_INFO
  623. + TRX_SYS_MYSQL_LOG_NAME, TRX_SYS_MYSQL_LOG_NAME_LEN);
  624. fprintf(stderr,
  625. "InnoDB: Last MySQL binlog file position %lu %lu,"
  626. " file name %s\n",
  627. trx_sys_mysql_bin_log_pos_high, trx_sys_mysql_bin_log_pos_low,
  628. trx_sys_mysql_bin_log_name);
  629. mtr_commit(&mtr);
  630. }
  631. /*****************************************************************//**
  632. Prints to stderr the MySQL master log offset info in the trx system header if
  633. the magic number shows it valid. */
  634. UNIV_INTERN
  635. void
  636. trx_sys_print_mysql_master_log_pos(void)
  637. /*====================================*/
  638. {
  639. trx_sysf_t* sys_header;
  640. mtr_t mtr;
  641. mtr_start(&mtr);
  642. sys_header = trx_sysf_get(&mtr);
  643. if (mach_read_from_4(sys_header + TRX_SYS_MYSQL_MASTER_LOG_INFO
  644. + TRX_SYS_MYSQL_LOG_MAGIC_N_FLD)
  645. != TRX_SYS_MYSQL_LOG_MAGIC_N) {
  646. mtr_commit(&mtr);
  647. return;
  648. }
  649. fprintf(stderr,
  650. "InnoDB: In a MySQL replication slave the last"
  651. " master binlog file\n"
  652. "InnoDB: position %lu %lu, file name %s\n",
  653. (ulong) mach_read_from_4(sys_header
  654. + TRX_SYS_MYSQL_MASTER_LOG_INFO
  655. + TRX_SYS_MYSQL_LOG_OFFSET_HIGH),
  656. (ulong) mach_read_from_4(sys_header
  657. + TRX_SYS_MYSQL_MASTER_LOG_INFO
  658. + TRX_SYS_MYSQL_LOG_OFFSET_LOW),
  659. sys_header + TRX_SYS_MYSQL_MASTER_LOG_INFO
  660. + TRX_SYS_MYSQL_LOG_NAME);
  661. /* Copy the master log position info to global variables we can
  662. use in ha_innobase.cc to initialize glob_mi to right values */
  663. ut_memcpy(trx_sys_mysql_master_log_name,
  664. sys_header + TRX_SYS_MYSQL_MASTER_LOG_INFO
  665. + TRX_SYS_MYSQL_LOG_NAME,
  666. TRX_SYS_MYSQL_LOG_NAME_LEN);
  667. trx_sys_mysql_master_log_pos
  668. = (((ib_int64_t) mach_read_from_4(
  669. sys_header + TRX_SYS_MYSQL_MASTER_LOG_INFO
  670. + TRX_SYS_MYSQL_LOG_OFFSET_HIGH)) << 32)
  671. + ((ib_int64_t) mach_read_from_4(
  672. sys_header + TRX_SYS_MYSQL_MASTER_LOG_INFO
  673. + TRX_SYS_MYSQL_LOG_OFFSET_LOW));
  674. mtr_commit(&mtr);
  675. }
  676. /****************************************************************//**
  677. Looks for a free slot for a rollback segment in the trx system file copy.
  678. @return slot index or ULINT_UNDEFINED if not found */
  679. UNIV_INTERN
  680. ulint
  681. trx_sysf_rseg_find_free(
  682. /*====================*/
  683. mtr_t* mtr) /*!< in: mtr */
  684. {
  685. trx_sysf_t* sys_header;
  686. ulint page_no;
  687. ulint i;
  688. ut_ad(mutex_own(&(kernel_mutex)));
  689. sys_header = trx_sysf_get(mtr);
  690. for (i = 0; i < TRX_SYS_N_RSEGS; i++) {
  691. page_no = trx_sysf_rseg_get_page_no(sys_header, i, mtr);
  692. if (page_no == FIL_NULL) {
  693. return(i);
  694. }
  695. }
  696. return(ULINT_UNDEFINED);
  697. }
  698. /*****************************************************************//**
  699. Creates the file page for the transaction system. This function is called only
  700. at the database creation, before trx_sys_init. */
  701. static
  702. void
  703. trx_sysf_create(
  704. /*============*/
  705. mtr_t* mtr) /*!< in: mtr */
  706. {
  707. trx_sysf_t* sys_header;
  708. ulint slot_no;
  709. buf_block_t* block;
  710. page_t* page;
  711. ulint page_no;
  712. ulint i;
  713. ut_ad(mtr);
  714. /* Note that below we first reserve the file space x-latch, and
  715. then enter the kernel: we must do it in this order to conform
  716. to the latching order rules. */
  717. mtr_x_lock(fil_space_get_latch(TRX_SYS_SPACE, NULL), mtr);
  718. mutex_enter(&kernel_mutex);
  719. /* Create the trx sys file block in a new allocated file segment */
  720. block = fseg_create(TRX_SYS_SPACE, 0, TRX_SYS + TRX_SYS_FSEG_HEADER,
  721. mtr);
  722. buf_block_dbg_add_level(block, SYNC_TRX_SYS_HEADER);
  723. ut_a(buf_block_get_page_no(block) == TRX_SYS_PAGE_NO);
  724. page = buf_block_get_frame(block);
  725. mlog_write_ulint(page + FIL_PAGE_TYPE, FIL_PAGE_TYPE_TRX_SYS,
  726. MLOG_2BYTES, mtr);
  727. /* Reset the doublewrite buffer magic number to zero so that we
  728. know that the doublewrite buffer has not yet been created (this
  729. suppresses a Valgrind warning) */
  730. mlog_write_ulint(page + TRX_SYS_DOUBLEWRITE
  731. + TRX_SYS_DOUBLEWRITE_MAGIC, 0, MLOG_4BYTES, mtr);
  732. sys_header = trx_sysf_get(mtr);
  733. /* Start counting transaction ids from number 1 up */
  734. mlog_write_dulint(sys_header + TRX_SYS_TRX_ID_STORE,
  735. ut_dulint_create(0, 1), mtr);
  736. /* Reset the rollback segment slots */
  737. for (i = 0; i < TRX_SYS_N_RSEGS; i++) {
  738. trx_sysf_rseg_set_space(sys_header, i, ULINT_UNDEFINED, mtr);
  739. trx_sysf_rseg_set_page_no(sys_header, i, FIL_NULL, mtr);
  740. }
  741. /* The remaining area (up to the page trailer) is uninitialized.
  742. Silence Valgrind warnings about it. */
  743. UNIV_MEM_VALID(sys_header + (TRX_SYS_RSEGS
  744. + TRX_SYS_N_RSEGS * TRX_SYS_RSEG_SLOT_SIZE
  745. + TRX_SYS_RSEG_SPACE),
  746. (UNIV_PAGE_SIZE - FIL_PAGE_DATA_END
  747. - (TRX_SYS_RSEGS
  748. + TRX_SYS_N_RSEGS * TRX_SYS_RSEG_SLOT_SIZE
  749. + TRX_SYS_RSEG_SPACE))
  750. + page - sys_header);
  751. /* Create the first rollback segment in the SYSTEM tablespace */
  752. page_no = trx_rseg_header_create(TRX_SYS_SPACE, 0, ULINT_MAX, &slot_no,
  753. mtr);
  754. ut_a(slot_no == TRX_SYS_SYSTEM_RSEG_ID);
  755. ut_a(page_no != FIL_NULL);
  756. mutex_exit(&kernel_mutex);
  757. }
  758. /*****************************************************************//**
  759. Creates and initializes the central memory structures for the transaction
  760. system. This is called when the database is started. */
  761. UNIV_INTERN
  762. void
  763. trx_sys_init_at_db_start(void)
  764. /*==========================*/
  765. {
  766. trx_sysf_t* sys_header;
  767. ib_int64_t rows_to_undo = 0;
  768. const char* unit = "";
  769. trx_t* trx;
  770. mtr_t mtr;
  771. mtr_start(&mtr);
  772. ut_ad(trx_sys == NULL);
  773. mutex_enter(&kernel_mutex);
  774. trx_sys = mem_alloc(sizeof(trx_sys_t));
  775. sys_header = trx_sysf_get(&mtr);
  776. trx_rseg_list_and_array_init(sys_header, &mtr);
  777. trx_sys->latest_rseg = UT_LIST_GET_FIRST(trx_sys->rseg_list);
  778. /* VERY important: after the database is started, max_trx_id value is
  779. divisible by TRX_SYS_TRX_ID_WRITE_MARGIN, and the 'if' in
  780. trx_sys_get_new_trx_id will evaluate to TRUE when the function
  781. is first time called, and the value for trx id will be written
  782. to the disk-based header! Thus trx id values will not overlap when
  783. the database is repeatedly started! */
  784. trx_sys->max_trx_id = ut_dulint_add(
  785. ut_dulint_align_up(mtr_read_dulint(
  786. sys_header
  787. + TRX_SYS_TRX_ID_STORE, &mtr),
  788. TRX_SYS_TRX_ID_WRITE_MARGIN),
  789. 2 * TRX_SYS_TRX_ID_WRITE_MARGIN);
  790. UT_LIST_INIT(trx_sys->mysql_trx_list);
  791. trx_dummy_sess = sess_open();
  792. trx_lists_init_at_db_start();
  793. if (UT_LIST_GET_LEN(trx_sys->trx_list) > 0) {
  794. trx = UT_LIST_GET_FIRST(trx_sys->trx_list);
  795. for (;;) {
  796. if ( trx->conc_state != TRX_PREPARED) {
  797. rows_to_undo += ut_conv_dulint_to_longlong(
  798. trx->undo_no);
  799. }
  800. trx = UT_LIST_GET_NEXT(trx_list, trx);
  801. if (!trx) {
  802. break;
  803. }
  804. }
  805. if (rows_to_undo > 1000000000) {
  806. unit = "M";
  807. rows_to_undo = rows_to_undo / 1000000;
  808. }
  809. fprintf(stderr,
  810. "InnoDB: %lu transaction(s) which must be"
  811. " rolled back or cleaned up\n"
  812. "InnoDB: in total %lu%s row operations to undo\n",
  813. (ulong) UT_LIST_GET_LEN(trx_sys->trx_list),
  814. (ulong) rows_to_undo, unit);
  815. fprintf(stderr, "InnoDB: Trx id counter is " TRX_ID_FMT "\n",
  816. TRX_ID_PREP_PRINTF(trx_sys->max_trx_id));
  817. }
  818. UT_LIST_INIT(trx_sys->view_list);
  819. trx_purge_sys_create();
  820. mutex_exit(&kernel_mutex);
  821. mtr_commit(&mtr);
  822. }
  823. /*****************************************************************//**
  824. Creates and initializes the transaction system at the database creation. */
  825. UNIV_INTERN
  826. void
  827. trx_sys_create(void)
  828. /*================*/
  829. {
  830. mtr_t mtr;
  831. mtr_start(&mtr);
  832. trx_sysf_create(&mtr);
  833. mtr_commit(&mtr);
  834. trx_sys_init_at_db_start();
  835. }
  836. /*****************************************************************//**
  837. Update the file format tag.
  838. @return always TRUE */
  839. static
  840. ibool
  841. trx_sys_file_format_max_write(
  842. /*==========================*/
  843. ulint format_id, /*!< in: file format id */
  844. const char** name) /*!< out: max file format name, can
  845. be NULL */
  846. {
  847. mtr_t mtr;
  848. byte* ptr;
  849. buf_block_t* block;
  850. ulint tag_value_low;
  851. mtr_start(&mtr);
  852. block = buf_page_get(
  853. TRX_SYS_SPACE, 0, TRX_SYS_PAGE_NO, RW_X_LATCH, &mtr);
  854. file_format_max.id = format_id;
  855. file_format_max.name = trx_sys_file_format_id_to_name(format_id);
  856. ptr = buf_block_get_frame(block) + TRX_SYS_FILE_FORMAT_TAG;
  857. tag_value_low = format_id + TRX_SYS_FILE_FORMAT_TAG_MAGIC_N_LOW;
  858. if (name) {
  859. *name = file_format_max.name;
  860. }
  861. mlog_write_dulint(
  862. ptr,
  863. ut_dulint_create(TRX_SYS_FILE_FORMAT_TAG_MAGIC_N_HIGH,
  864. tag_value_low),
  865. &mtr);
  866. mtr_commit(&mtr);
  867. return(TRUE);
  868. }
  869. /*****************************************************************//**
  870. Read the file format tag.
  871. @return the file format or ULINT_UNDEFINED if not set. */
  872. static
  873. ulint
  874. trx_sys_file_format_max_read(void)
  875. /*==============================*/
  876. {
  877. mtr_t mtr;
  878. const byte* ptr;
  879. const buf_block_t* block;
  880. ulint format_id;
  881. dulint file_format_id;
  882. /* Since this is called during the startup phase it's safe to
  883. read the value without a covering mutex. */
  884. mtr_start(&mtr);
  885. block = buf_page_get(
  886. TRX_SYS_SPACE, 0, TRX_SYS_PAGE_NO, RW_X_LATCH, &mtr);
  887. ptr = buf_block_get_frame(block) + TRX_SYS_FILE_FORMAT_TAG;
  888. file_format_id = mach_read_from_8(ptr);
  889. mtr_commit(&mtr);
  890. format_id = file_format_id.low - TRX_SYS_FILE_FORMAT_TAG_MAGIC_N_LOW;
  891. if (file_format_id.high != TRX_SYS_FILE_FORMAT_TAG_MAGIC_N_HIGH
  892. || format_id >= FILE_FORMAT_NAME_N) {
  893. /* Either it has never been tagged, or garbage in it. */
  894. return(ULINT_UNDEFINED);
  895. }
  896. return(format_id);
  897. }
  898. /*****************************************************************//**
  899. Get the name representation of the file format from its id.
  900. @return pointer to the name */
  901. UNIV_INTERN
  902. const char*
  903. trx_sys_file_format_id_to_name(
  904. /*===========================*/
  905. const ulint id) /*!< in: id of the file format */
  906. {
  907. ut_a(id < FILE_FORMAT_NAME_N);
  908. return(file_format_name_map[id]);
  909. }
  910. /*****************************************************************//**
  911. Check for the max file format tag stored on disk. Note: If max_format_id
  912. is == DICT_TF_FORMAT_MAX + 1 then we only print a warning.
  913. @return DB_SUCCESS or error code */
  914. UNIV_INTERN
  915. ulint
  916. trx_sys_file_format_max_check(
  917. /*==========================*/
  918. ulint max_format_id) /*!< in: max format id to check */
  919. {
  920. ulint format_id;
  921. /* Check the file format in the tablespace. Do not try to
  922. recover if the file format is not supported by the engine
  923. unless forced by the user. */
  924. format_id = trx_sys_file_format_max_read();
  925. if (format_id == ULINT_UNDEFINED) {
  926. /* Format ID was not set. Set it to minimum possible
  927. value. */
  928. format_id = DICT_TF_FORMAT_51;
  929. }
  930. ut_print_timestamp(stderr);
  931. fprintf(stderr,
  932. " InnoDB: highest supported file format is %s.\n",
  933. trx_sys_file_format_id_to_name(DICT_TF_FORMAT_MAX));
  934. if (format_id > DICT_TF_FORMAT_MAX) {
  935. ut_a(format_id < FILE_FORMAT_NAME_N);
  936. ut_print_timestamp(stderr);
  937. fprintf(stderr,
  938. " InnoDB: %s: the system tablespace is in a file "
  939. "format that this version doesn't support - %s\n",
  940. ((max_format_id <= DICT_TF_FORMAT_MAX)
  941. ? "Error" : "Warning"),
  942. trx_sys_file_format_id_to_name(format_id));
  943. if (max_format_id <= DICT_TF_FORMAT_MAX) {
  944. return(DB_ERROR);
  945. }
  946. }
  947. format_id = (format_id > max_format_id) ? format_id : max_format_id;
  948. /* We don't need a mutex here, as this function should only
  949. be called once at start up. */
  950. file_format_max.id = format_id;
  951. file_format_max.name = trx_sys_file_format_id_to_name(format_id);
  952. return(DB_SUCCESS);
  953. }
  954. /*****************************************************************//**
  955. Set the file format id unconditionally except if it's already the
  956. same value.
  957. @return TRUE if value updated */
  958. UNIV_INTERN
  959. ibool
  960. trx_sys_file_format_max_set(
  961. /*========================*/
  962. ulint format_id, /*!< in: file format id */
  963. const char** name) /*!< out: max file format name or
  964. NULL if not needed. */
  965. {
  966. ibool ret = FALSE;
  967. ut_a(format_id <= DICT_TF_FORMAT_MAX);
  968. mutex_enter(&file_format_max.mutex);
  969. /* Only update if not already same value. */
  970. if (format_id != file_format_max.id) {
  971. ret = trx_sys_file_format_max_write(format_id, name);
  972. }
  973. mutex_exit(&file_format_max.mutex);
  974. return(ret);
  975. }
  976. /********************************************************************//**
  977. Tags the system table space with minimum format id if it has not been
  978. tagged yet.
  979. WARNING: This function is only called during the startup and AFTER the
  980. redo log application during recovery has finished. */
  981. UNIV_INTERN
  982. void
  983. trx_sys_file_format_tag_init(void)
  984. /*==============================*/
  985. {
  986. ulint format_id;
  987. format_id = trx_sys_file_format_max_read();
  988. /* If format_id is not set then set it to the minimum. */
  989. if (format_id == ULINT_UNDEFINED) {
  990. trx_sys_file_format_max_set(DICT_TF_FORMAT_51, NULL);
  991. }
  992. }
  993. /********************************************************************//**
  994. Update the file format tag in the system tablespace only if the given
  995. format id is greater than the known max id.
  996. @return TRUE if format_id was bigger than the known max id */
  997. UNIV_INTERN
  998. ibool
  999. trx_sys_file_format_max_upgrade(
  1000. /*============================*/
  1001. const char** name, /*!< out: max file format name */
  1002. ulint format_id) /*!< in: file format identifier */
  1003. {
  1004. ibool ret = FALSE;
  1005. ut_a(name);
  1006. ut_a(file_format_max.name != NULL);
  1007. ut_a(format_id <= DICT_TF_FORMAT_MAX);
  1008. mutex_enter(&file_format_max.mutex);
  1009. if (format_id > file_format_max.id) {
  1010. ret = trx_sys_file_format_max_write(format_id, name);
  1011. }
  1012. mutex_exit(&file_format_max.mutex);
  1013. return(ret);
  1014. }
  1015. /*****************************************************************//**
  1016. Get the name representation of the file format from its id.
  1017. @return pointer to the max format name */
  1018. UNIV_INTERN
  1019. const char*
  1020. trx_sys_file_format_max_get(void)
  1021. /*=============================*/
  1022. {
  1023. return(file_format_max.name);
  1024. }
  1025. /*****************************************************************//**
  1026. Initializes the tablespace tag system. */
  1027. UNIV_INTERN
  1028. void
  1029. trx_sys_file_format_init(void)
  1030. /*==========================*/
  1031. {
  1032. mutex_create(&file_format_max.mutex, SYNC_FILE_FORMAT_TAG);
  1033. /* We don't need a mutex here, as this function should only
  1034. be called once at start up. */
  1035. file_format_max.id = DICT_TF_FORMAT_51;
  1036. file_format_max.name = trx_sys_file_format_id_to_name(
  1037. file_format_max.id);
  1038. }
  1039. /*****************************************************************//**
  1040. Closes the tablespace tag system. */
  1041. UNIV_INTERN
  1042. void
  1043. trx_sys_file_format_close(void)
  1044. /*===========================*/
  1045. {
  1046. /* Does nothing at the moment */
  1047. }
  1048. #else /* !UNIV_HOTBACKUP */
  1049. /*****************************************************************//**
  1050. Prints to stderr the MySQL binlog info in the system header if the
  1051. magic number shows it valid. */
  1052. UNIV_INTERN
  1053. void
  1054. trx_sys_print_mysql_binlog_offset_from_page(
  1055. /*========================================*/
  1056. const byte* page) /*!< in: buffer containing the trx
  1057. system header page, i.e., page number
  1058. TRX_SYS_PAGE_NO in the tablespace */
  1059. {
  1060. const trx_sysf_t* sys_header;
  1061. sys_header = page + TRX_SYS;
  1062. if (mach_read_from_4(sys_header + TRX_SYS_MYSQL_LOG_INFO
  1063. + TRX_SYS_MYSQL_LOG_MAGIC_N_FLD)
  1064. == TRX_SYS_MYSQL_LOG_MAGIC_N) {
  1065. fprintf(stderr,
  1066. "ibbackup: Last MySQL binlog file position %lu %lu,"
  1067. " file name %s\n",
  1068. (ulong) mach_read_from_4(
  1069. sys_header + TRX_SYS_MYSQL_LOG_INFO
  1070. + TRX_SYS_MYSQL_LOG_OFFSET_HIGH),
  1071. (ulong) mach_read_from_4(
  1072. sys_header + TRX_SYS_MYSQL_LOG_INFO
  1073. + TRX_SYS_MYSQL_LOG_OFFSET_LOW),
  1074. sys_header + TRX_SYS_MYSQL_LOG_INFO
  1075. + TRX_SYS_MYSQL_LOG_NAME);
  1076. }
  1077. }
  1078. /* THESE ARE COPIED FROM NON-HOTBACKUP PART OF THE INNODB SOURCE TREE
  1079. (This code duplicaton should be fixed at some point!)
  1080. */
  1081. #define TRX_SYS_SPACE 0 /* the SYSTEM tablespace */
  1082. /* The offset of the file format tag on the trx system header page */
  1083. #define TRX_SYS_FILE_FORMAT_TAG (UNIV_PAGE_SIZE - 16)
  1084. /* We use these random constants to reduce the probability of reading
  1085. garbage (from previous versions) that maps to an actual format id. We
  1086. use these as bit masks at the time of reading and writing from/to disk. */
  1087. #define TRX_SYS_FILE_FORMAT_TAG_MAGIC_N_LOW 3645922177UL
  1088. #define TRX_SYS_FILE_FORMAT_TAG_MAGIC_N_HIGH 2745987765UL
  1089. /* END OF COPIED DEFINITIONS */
  1090. /*****************************************************************//**
  1091. Reads the file format id from the first system table space file.
  1092. Even if the call succeeds and returns TRUE, the returned format id
  1093. may be ULINT_UNDEFINED signalling that the format id was not present
  1094. in the data file.
  1095. @return TRUE if call succeeds */
  1096. UNIV_INTERN
  1097. ibool
  1098. trx_sys_read_file_format_id(
  1099. /*========================*/
  1100. const char *pathname, /*!< in: pathname of the first system
  1101. table space file */
  1102. ulint *format_id) /*!< out: file format of the system table
  1103. space */
  1104. {
  1105. os_file_t file;
  1106. ibool success;
  1107. byte buf[UNIV_PAGE_SIZE * 2];
  1108. page_t* page = ut_align(buf, UNIV_PAGE_SIZE);
  1109. const byte* ptr;
  1110. dulint file_format_id;
  1111. *format_id = ULINT_UNDEFINED;
  1112. file = os_file_create_simple_no_error_handling(
  1113. pathname,
  1114. OS_FILE_OPEN,
  1115. OS_FILE_READ_ONLY,
  1116. &success
  1117. );
  1118. if (!success) {
  1119. /* The following call prints an error message */
  1120. os_file_get_last_error(TRUE);
  1121. ut_print_timestamp(stderr);
  1122. fprintf(stderr,
  1123. " ibbackup: Error: trying to read system tablespace file format,\n"
  1124. " ibbackup: but could not open the tablespace file %s!\n",
  1125. pathname
  1126. );
  1127. return(FALSE);
  1128. }
  1129. /* Read the page on which file format is stored */
  1130. success = os_file_read_no_error_handling(
  1131. file, page, TRX_SYS_PAGE_NO * UNIV_PAGE_SIZE, 0, UNIV_PAGE_SIZE
  1132. );
  1133. if (!success) {
  1134. /* The following call prints an error message */
  1135. os_file_get_last_error(TRUE);
  1136. ut_print_timestamp(stderr);
  1137. fprintf(stderr,
  1138. " ibbackup: Error: trying to read system table space file format,\n"
  1139. " ibbackup: but failed to read the tablespace file %s!\n",
  1140. pathname
  1141. );
  1142. os_file_close(file);
  1143. return(FALSE);
  1144. }
  1145. os_file_close(file);
  1146. /* get the file format from the page */
  1147. ptr = page + TRX_SYS_FILE_FORMAT_TAG;
  1148. file_format_id = mach_read_from_8(ptr);
  1149. *format_id = file_format_id.low - TRX_SYS_FILE_FORMAT_TAG_MAGIC_N_LOW;
  1150. if (file_format_id.high != TRX_SYS_FILE_FORMAT_TAG_MAGIC_N_HIGH
  1151. || *format_id >= FILE_FORMAT_NAME_N) {
  1152. /* Either it has never been tagged, or garbage in it. */
  1153. *format_id = ULINT_UNDEFINED;
  1154. return(TRUE);
  1155. }
  1156. return(TRUE);
  1157. }
  1158. /*****************************************************************//**
  1159. Reads the file format id from the given per-table data file.
  1160. @return TRUE if call succeeds */
  1161. UNIV_INTERN
  1162. ibool
  1163. trx_sys_read_pertable_file_format_id(
  1164. /*=================================*/
  1165. const char *pathname, /*!< in: pathname of a per-table
  1166. datafile */
  1167. ulint *format_id) /*!< out: file format of the per-table
  1168. data file */
  1169. {
  1170. os_file_t file;
  1171. ibool success;
  1172. byte buf[UNIV_PAGE_SIZE * 2];
  1173. page_t* page = ut_align(buf, UNIV_PAGE_SIZE);
  1174. const byte* ptr;
  1175. ib_uint32_t flags;
  1176. *format_id = ULINT_UNDEFINED;
  1177. file = os_file_create_simple_no_error_handling(
  1178. pathname,
  1179. OS_FILE_OPEN,
  1180. OS_FILE_READ_ONLY,
  1181. &success
  1182. );
  1183. if (!success) {
  1184. /* The following call prints an error message */
  1185. os_file_get_last_error(TRUE);
  1186. ut_print_timestamp(stderr);
  1187. fprintf(stderr,
  1188. " ibbackup: Error: trying to read per-table tablespace format,\n"
  1189. " ibbackup: but could not open the tablespace file %s!\n",
  1190. pathname
  1191. );
  1192. return(FALSE);
  1193. }
  1194. /* Read the first page of the per-table datafile */
  1195. success = os_file_read_no_error_handling(
  1196. file, page, 0, 0, UNIV_PAGE_SIZE
  1197. );
  1198. if (!success) {
  1199. /* The following call prints an error message */
  1200. os_file_get_last_error(TRUE);
  1201. ut_print_timestamp(stderr);
  1202. fprintf(stderr,
  1203. " ibbackup: Error: trying to per-table data file format,\n"
  1204. " ibbackup: but failed to read the tablespace file %s!\n",
  1205. pathname
  1206. );
  1207. os_file_close(file);
  1208. return(FALSE);
  1209. }
  1210. os_file_close(file);
  1211. /* get the file format from the page */
  1212. ptr = page + 54;
  1213. flags = mach_read_from_4(ptr);
  1214. if (flags == 0) {
  1215. /* file format is Antelope */
  1216. *format_id = 0;
  1217. return (TRUE);
  1218. } else if (flags & 1) {
  1219. /* tablespace flags are ok */
  1220. *format_id = (flags / 32) % 128;
  1221. return (TRUE);
  1222. } else {
  1223. /* bad tablespace flags */
  1224. return(FALSE);
  1225. }
  1226. }
  1227. /*****************************************************************//**
  1228. Get the name representation of the file format from its id.
  1229. @return pointer to the name */
  1230. UNIV_INTERN
  1231. const char*
  1232. trx_sys_file_format_id_to_name(
  1233. /*===========================*/
  1234. const ulint id) /*!< in: id of the file format */
  1235. {
  1236. if (!(id < FILE_FORMAT_NAME_N)) {
  1237. /* unknown id */
  1238. return ("Unknown");
  1239. }
  1240. return(file_format_name_map[id]);
  1241. }
  1242. #endif /* !UNIV_HOTBACKUP */
  1243. /*********************************************************************
  1244. Shutdown/Close the transaction system. */
  1245. UNIV_INTERN
  1246. void
  1247. trx_sys_close(void)
  1248. /*===============*/
  1249. {
  1250. trx_rseg_t* rseg;
  1251. read_view_t* view;
  1252. ut_ad(trx_sys != NULL);
  1253. /* Check that all read views are closed except read view owned
  1254. by a purge. */
  1255. if (UT_LIST_GET_LEN(trx_sys->view_list) > 1) {
  1256. fprintf(stderr,
  1257. "InnoDB: Error: all read views were not closed"
  1258. " before shutdown:\n"
  1259. "InnoDB: %lu read views open \n",
  1260. UT_LIST_GET_LEN(trx_sys->view_list) - 1);
  1261. }
  1262. sess_close(trx_dummy_sess);
  1263. trx_dummy_sess = NULL;
  1264. trx_purge_sys_close();
  1265. mutex_enter(&kernel_mutex);
  1266. /* Free the double write data structures. */
  1267. ut_a(trx_doublewrite != NULL);
  1268. ut_free(trx_doublewrite->write_buf_unaligned);
  1269. trx_doublewrite->write_buf_unaligned = NULL;
  1270. mem_free(trx_doublewrite->buf_block_arr);
  1271. trx_doublewrite->buf_block_arr = NULL;
  1272. mutex_free(&trx_doublewrite->mutex);
  1273. mem_free(trx_doublewrite);
  1274. trx_doublewrite = NULL;
  1275. /* There can't be any active transactions. */
  1276. rseg = UT_LIST_GET_FIRST(trx_sys->rseg_list);
  1277. while (rseg != NULL) {
  1278. trx_rseg_t* prev_rseg = rseg;
  1279. rseg = UT_LIST_GET_NEXT(rseg_list, prev_rseg);
  1280. UT_LIST_REMOVE(rseg_list, trx_sys->rseg_list, prev_rseg);
  1281. trx_rseg_mem_free(prev_rseg);
  1282. }
  1283. view = UT_LIST_GET_FIRST(trx_sys->view_list);
  1284. while (view != NULL) {
  1285. read_view_t* prev_view = view;
  1286. view = UT_LIST_GET_NEXT(view_list, prev_view);
  1287. /* Views are allocated from the trx_sys->global_read_view_heap.
  1288. So, we simply remove the element here. */
  1289. UT_LIST_REMOVE(view_list, trx_sys->view_list, prev_view);
  1290. }
  1291. ut_a(UT_LIST_GET_LEN(trx_sys->trx_list) == 0);
  1292. ut_a(UT_LIST_GET_LEN(trx_sys->rseg_list) == 0);
  1293. ut_a(UT_LIST_GET_LEN(trx_sys->view_list) == 0);
  1294. ut_a(UT_LIST_GET_LEN(trx_sys->mysql_trx_list) == 0);
  1295. mem_free(trx_sys);
  1296. trx_sys = NULL;
  1297. mutex_exit(&kernel_mutex);
  1298. }