ibuf0ibuf.c 90 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/******************************************************
Insert buffer

(c) 1997 Innobase Oy

Created 7/19/1997 Heikki Tuuri
*******************************************************/

#include "ibuf0ibuf.h"

#ifdef UNIV_NONINL
#include "ibuf0ibuf.ic"
#endif

#include "buf0buf.h"
#include "buf0rea.h"
#include "fsp0fsp.h"
#include "trx0sys.h"
#include "fil0fil.h"
#include "thr0loc.h"
#include "rem0rec.h"
#include "btr0cur.h"
#include "btr0pcur.h"
#include "btr0btr.h"
#include "sync0sync.h"
#include "dict0boot.h"
#include "fut0lst.h"
#include "lock0lock.h"
#include "log0recv.h"
#include "que0que.h"

32
/*	STRUCTURE OF AN INSERT BUFFER RECORD
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

In versions < 4.1.x:

1. The first field is the page number.
2. The second field is an array which stores type info for each subsequent
   field. We store the information which affects the ordering of records, and
   also the physical storage size of an SQL NULL value. E.g., for CHAR(10) it
   is 10 bytes.
3. Next we have the fields of the actual index record.

In versions >= 4.1.x:

Note that contary to what we planned in the 1990's, there will only be one
insert buffer tree, and that is in the system tablespace of InnoDB.

1. The first field is the space id.
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
49
2. The second field is a one-byte marker (0) which differentiates records from
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
50
   the < 4.1.x storage format.
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
51 52 53 54 55 56
3. The third field is the page number.
4. The fourth field contains the type info, where we have also added 2 bytes to
   store the charset. In the compressed table format of 5.0.x we must add more
   information here so that we can build a dummy 'index' struct which 5.0.x
   can use in the binary search on the index page in the ibuf merge phase.
5. The rest of the fields contain the fields of the actual index record.
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
57

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
58 59 60 61 62 63 64 65
In versions >= 5.0.3:

The first byte of the fourth field is an additional marker (0) if the record
is in the compact format.  The presence of this marker can be detected by
looking at the length of the field modulo DATA_NEW_ORDER_NULL_TYPE_BUF_SIZE.

The high-order bit of the character set field in the type info is the
"nullable" flag for the field. */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
66 67


68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
/*	PREVENTING DEADLOCKS IN THE INSERT BUFFER SYSTEM

If an OS thread performs any operation that brings in disk pages from
non-system tablespaces into the buffer pool, or creates such a page there,
then the operation may have as a side effect an insert buffer index tree
compression. Thus, the tree latch of the insert buffer tree may be acquired
in the x-mode, and also the file space latch of the system tablespace may
be acquired in the x-mode.

Also, an insert to an index in a non-system tablespace can have the same
effect. How do we know this cannot lead to a deadlock of OS threads? There
is a problem with the i\o-handler threads: they break the latching order
because they own x-latches to pages which are on a lower level than the
insert buffer tree latch, its page latches, and the tablespace latch an
insert buffer operation can reserve.

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
84 85 86 87
The solution is the following: Let all the tree and page latches connected
with the insert buffer be later in the latching order than the fsp latch and
fsp page latches.

88
Insert buffer pages must be such that the insert buffer is never invoked
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
89
when these pages are accessed as this would result in a recursion violating
90 91 92
the latching order. We let a special i/o-handler thread take care of i/o to
the insert buffer pages and the ibuf bitmap pages, as well as the fsp bitmap
pages and the first inode page, which contains the inode of the ibuf tree: let
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
93 94
us call all these ibuf pages. To prevent deadlocks, we do not let a read-ahead
access both non-ibuf and ibuf pages.
95

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
96 97
Then an i/o-handler for the insert buffer never needs to access recursively the
insert buffer tree and thus obeys the latching order. On the other hand, other
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
i/o-handlers for other tablespaces may require access to the insert buffer,
but because all kinds of latches they need to access there are later in the
latching order, no violation of the latching order occurs in this case,
either.

A problem is how to grow and contract an insert buffer tree. As it is later
in the latching order than the fsp management, we have to reserve the fsp
latch first, before adding or removing pages from the insert buffer tree.
We let the insert buffer tree have its own file space management: a free
list of pages linked to the tree root. To prevent recursive using of the
insert buffer when adding pages to the tree, we must first load these pages
to memory, obtaining a latch on them, and only after that add them to the
free list of the insert buffer tree. More difficult is removing of pages
from the free list. If there is an excess of pages in the free list of the
ibuf tree, they might be needed if some thread reserves the fsp latch,
intending to allocate more file space. So we do the following: if a thread
reserves the fsp latch, we check the writer count field of the latch. If
this field has value 1, it means that the thread did not own the latch
before entering the fsp system, and the mtr of the thread contains no
modifications to the fsp pages. Now we are free to reserve the ibuf latch,
and check if there is an excess of pages in the free list. We can then, in a
separate mini-transaction, take them out of the free list and free them to
the fsp system.

To avoid deadlocks in the ibuf system, we divide file pages into three levels:

(1) non-ibuf pages,
(2) ibuf tree pages and the pages in the ibuf tree free list, and
(3) ibuf bitmap pages.

No OS thread is allowed to access higher level pages if it has latches to
lower level pages; even if the thread owns a B-tree latch it must not access
the B-tree non-leaf pages if it has latches on lower level pages. Read-ahead
is only allowed for level 1 and 2 pages. Dedicated i/o-handler threads handle
exclusively level 1 i/o. A dedicated i/o handler thread handles exclusively
level 2 i/o. However, if an OS thread does the i/o handling for itself, i.e.,
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
134 135
it uses synchronous aio, it can access any pages, as long as it obeys the
access order rules. */
136 137 138 139 140

/* Buffer pool size per the maximum insert buffer size */
#define IBUF_POOL_SIZE_PER_MAX_SIZE	2

/* The insert buffer control structure */
141
ibuf_t*	ibuf			= NULL;
142

143
static ulint ibuf_rnd		= 986058871;
144 145 146

ulint	ibuf_flush_count	= 0;

147
#ifdef UNIV_IBUF_DEBUG
148
/* Dimensions for the ibuf_count array */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
149 150
#define IBUF_COUNT_N_SPACES	500
#define IBUF_COUNT_N_PAGES	2000
151 152

/* Buffered entry counts for file pages, used in debugging */
153
static ulint	ibuf_counts[IBUF_COUNT_N_SPACES][IBUF_COUNT_N_PAGES];
154

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
/**********************************************************************
Checks that the indexes to ibuf_counts[][] are within limits. */
UNIV_INLINE
void
ibuf_count_check(
/*=============*/
	ulint	space_id,	/* in: space identifier */
	ulint	page_no)	/* in: page number */
{
	if (space_id < IBUF_COUNT_N_SPACES && page_no < IBUF_COUNT_N_PAGES) {
		return;
	}

	fprintf(stderr,
		"InnoDB: UNIV_IBUF_DEBUG limits space_id and page_no\n"
		"InnoDB: and breaks crash recovery.\n"
		"InnoDB: space_id=%lu, should be 0<=space_id<%lu\n"
		"InnoDB: page_no=%lu, should be 0<=page_no<%lu\n",
		(ulint) space_id, (ulint) IBUF_COUNT_N_SPACES,
		(ulint) page_no, (ulint) IBUF_COUNT_N_PAGES);
	ut_error;
}
177
#endif
178 179 180 181 182 183 184 185 186 187 188 189 190

/* The start address for an insert buffer bitmap page bitmap */
#define IBUF_BITMAP		PAGE_DATA

/* Offsets in bits for the bits describing a single page in the bitmap */
#define	IBUF_BITMAP_FREE	0
#define IBUF_BITMAP_BUFFERED	2
#define IBUF_BITMAP_IBUF	3	/* TRUE if page is a part of the ibuf
					tree, excluding the root page, or is
					in the free list of the ibuf */

/* Number of bits describing a single page */
#define IBUF_BITS_PER_PAGE	4
191 192 193
#if IBUF_BITS_PER_PAGE % 2
# error "IBUF_BITS_PER_PAGE must be an even number!"
#endif
194 195

/* The mutex used to block pessimistic inserts to ibuf trees */
196
static mutex_t	ibuf_pessimistic_insert_mutex;
197 198

/* The mutex protecting the insert buffer structs */
199
static mutex_t	ibuf_mutex;
200 201

/* The mutex protecting the insert buffer bitmaps */
202
static mutex_t	ibuf_bitmap_mutex;
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234

/* The area in pages from which contract looks for page numbers for merge */
#define	IBUF_MERGE_AREA			8

/* Inside the merge area, pages which have at most 1 per this number less
buffered entries compared to maximum volume that can buffered for a single
page are merged along with the page whose buffer became full */
#define IBUF_MERGE_THRESHOLD		4

/* In ibuf_contract at most this number of pages is read to memory in one
batch, in order to merge the entries for them in the insert buffer */
#define	IBUF_MAX_N_PAGES_MERGED		IBUF_MERGE_AREA

/* If the combined size of the ibuf trees exceeds ibuf->max_size by this
many pages, we start to contract it in connection to inserts there, using
non-synchronous contract */
#define IBUF_CONTRACT_ON_INSERT_NON_SYNC	0

/* Same as above, but use synchronous contract */
#define IBUF_CONTRACT_ON_INSERT_SYNC		5

/* Same as above, but no insert is done, only contract is called */
#define IBUF_CONTRACT_DO_NOT_INSERT		10

/* TODO: how to cope with drop table if there are records in the insert
buffer for the indexes of the table? Is there actually any problem,
because ibuf merge is done to a page when it is read in, and it is
still physically like the index page even if the index would have been
dropped! So, there seems to be no problem. */

/**********************************************************************
Validates the ibuf data structures when the caller owns ibuf_mutex. */
235

236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
ibool
ibuf_validate_low(void);
/*===================*/
			/* out: TRUE if ok */

/**********************************************************************
Sets the flag in the current OS thread local storage denoting that it is
inside an insert buffer routine. */
UNIV_INLINE
void
ibuf_enter(void)
/*============*/
{
	ibool*	ptr;

	ptr = thr_local_get_in_ibuf_field();

	ut_ad(*ptr == FALSE);

	*ptr = TRUE;
}

/**********************************************************************
Sets the flag in the current OS thread local storage denoting that it is
exiting an insert buffer routine. */
UNIV_INLINE
void
ibuf_exit(void)
/*===========*/
{
	ibool*	ptr;

	ptr = thr_local_get_in_ibuf_field();

	ut_ad(*ptr == TRUE);

	*ptr = FALSE;
}

/**********************************************************************
Returns TRUE if the current OS thread is performing an insert buffer
routine. */

ibool
ibuf_inside(void)
/*=============*/
		/* out: TRUE if inside an insert buffer routine: for instance,
		a read-ahead of non-ibuf pages is then forbidden */
{
	return(*thr_local_get_in_ibuf_field());
}

/**********************************************************************
Gets the ibuf header page and x-latches it. */
static
page_t*
ibuf_header_page_get(
/*=================*/
			/* out: insert buffer header page */
	ulint	space,	/* in: space id */
	mtr_t*	mtr)	/* in: mtr */
{
	page_t*	page;

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
300 301
	ut_a(space == 0);

302 303 304 305
	ut_ad(!ibuf_inside());

	page = buf_page_get(space, FSP_IBUF_HEADER_PAGE_NO, RW_X_LATCH, mtr);

306
#ifdef UNIV_SYNC_DEBUG
307
	buf_page_dbg_add_level(page, SYNC_IBUF_HEADER);
308
#endif /* UNIV_SYNC_DEBUG */
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325

	return(page);
}

/**********************************************************************
Gets the root page and x-latches it. */
static
page_t*
ibuf_tree_root_get(
/*===============*/
				/* out: insert buffer tree root page */
	ibuf_data_t*	data,	/* in: ibuf data */
	ulint		space,	/* in: space id */
	mtr_t*		mtr)	/* in: mtr */
{
	page_t*	page;

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
326
	ut_a(space == 0);
327 328
	ut_ad(ibuf_inside());

329
	mtr_x_lock(dict_index_get_lock(data->index), mtr);
330 331

	page = buf_page_get(space, FSP_IBUF_TREE_ROOT_PAGE_NO, RW_X_LATCH,
332
			    mtr);
333
#ifdef UNIV_SYNC_DEBUG
334
	buf_page_dbg_add_level(page, SYNC_TREE_NODE);
335
#endif /* UNIV_SYNC_DEBUG */
336 337 338

	return(page);
}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
339

340
#ifdef UNIV_IBUF_DEBUG
341 342 343 344 345 346 347 348 349 350 351
/**********************************************************************
Gets the ibuf count for a given page. */

ulint
ibuf_count_get(
/*===========*/
			/* out: number of entries in the insert buffer
			currently buffered for this page */
	ulint	space,	/* in: space id */
	ulint	page_no)/* in: page number */
{
352
	ibuf_count_check(space, page_no);
353

354
	return(ibuf_counts[space][page_no]);
355 356 357 358 359 360 361 362 363 364 365 366
}

/**********************************************************************
Sets the ibuf count for a given page. */
static
void
ibuf_count_set(
/*===========*/
	ulint	space,	/* in: space id */
	ulint	page_no,/* in: page number */
	ulint	val)	/* in: value to set */
{
367
	ibuf_count_check(space, page_no);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
368
	ut_a(val < UNIV_PAGE_SIZE);
369

370
	ibuf_counts[space][page_no] = val;
371
}
372
#endif
373 374

/**********************************************************************
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
375 376
Creates the insert buffer data structure at a database startup and initializes
the data structures for the insert buffer. */
377 378 379 380 381 382 383 384 385 386

void
ibuf_init_at_db_start(void)
/*=======================*/
{
	ibuf = mem_alloc(sizeof(ibuf_t));

	/* Note that also a pessimistic delete can sometimes make a B-tree
	grow in size, as the references on the upper levels of the tree can
	change */
387

388
	ibuf->max_size = buf_pool_get_curr_size() / UNIV_PAGE_SIZE
389
		/ IBUF_POOL_SIZE_PER_MAX_SIZE;
390 391 392

	UT_LIST_INIT(ibuf->data_list);

393
	ibuf->size = 0;
394

395
	mutex_create(&ibuf_pessimistic_insert_mutex,
396
		     SYNC_IBUF_PESS_INSERT_MUTEX);
397

398
	mutex_create(&ibuf_mutex, SYNC_IBUF_MUTEX);
399

400
	mutex_create(&ibuf_bitmap_mutex, SYNC_IBUF_BITMAP_MUTEX);
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420

	fil_ibuf_init_at_db_start();
}

/**********************************************************************
Updates the size information in an ibuf data, assuming the segment size has
not changed. */
static
void
ibuf_data_sizes_update(
/*===================*/
	ibuf_data_t*	data,	/* in: ibuf data struct */
	page_t*		root,	/* in: ibuf tree root */
	mtr_t*		mtr)	/* in: mtr */
{
	ulint	old_size;

	ut_ad(mutex_own(&ibuf_mutex));

	old_size = data->size;
421

422 423 424 425 426 427
	data->free_list_len = flst_get_len(root + PAGE_HEADER
					   + PAGE_BTR_IBUF_FREE_LIST, mtr);

	data->height = 1 + btr_page_get_level(root, mtr);

	data->size = data->seg_size - (1 + data->free_list_len);
428
	/* the '1 +' is the ibuf header page */
429 430 431 432 433 434 435 436 437 438 439 440 441
	ut_ad(data->size < data->seg_size);

	if (page_get_n_recs(root) == 0) {

		data->empty = TRUE;
	} else {
		data->empty = FALSE;
	}

	ut_ad(ibuf->size + data->size >= old_size);

	ibuf->size = ibuf->size + data->size - old_size;

442 443 444 445
#if 0
	fprintf(stderr, "ibuf size %lu, space ibuf size %lu\n",
		ibuf->size, data->size);
#endif
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
446
}
447 448 449 450 451

/**********************************************************************
Creates the insert buffer data struct for a single tablespace. Reads the
root page of the insert buffer tree in the tablespace. This function can
be called only after the dictionary system has been initialized, as this
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
452
creates also the insert buffer table and index into this tablespace. */
453 454 455 456 457

ibuf_data_t*
ibuf_data_init_for_space(
/*=====================*/
			/* out, own: ibuf data struct, linked to the list
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
458
			in ibuf control structure */
459 460 461 462 463 464
	ulint	space)	/* in: space id */
{
	ibuf_data_t*	data;
	page_t*		root;
	page_t*		header_page;
	mtr_t		mtr;
465 466
	char*		buf;
	mem_heap_t*	heap;
467 468 469
	dict_table_t*	table;
	dict_index_t*	index;
	ulint		n_used;
470

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
471 472
	ut_a(space == 0);

473 474 475 476 477 478 479 480 481 482 483 484 485
	data = mem_alloc(sizeof(ibuf_data_t));

	data->space = space;

	mtr_start(&mtr);

	mutex_enter(&ibuf_mutex);

	mtr_x_lock(fil_space_get_latch(space), &mtr);

	header_page = ibuf_header_page_get(space, &mtr);

	fseg_n_reserved_pages(header_page + IBUF_HEADER + IBUF_TREE_SEG_HEADER,
486
			      &n_used, &mtr);
487
	ibuf_enter();
488

489 490 491
	ut_ad(n_used >= 2);

	data->seg_size = n_used;
492

493
	root = buf_page_get(space, FSP_IBUF_TREE_ROOT_PAGE_NO, RW_X_LATCH,
494
			    &mtr);
495
#ifdef UNIV_SYNC_DEBUG
496
	buf_page_dbg_add_level(root, SYNC_TREE_NODE);
497
#endif /* UNIV_SYNC_DEBUG */
498 499 500 501 502

	data->size = 0;
	data->n_inserts = 0;
	data->n_merges = 0;
	data->n_merged_recs = 0;
503

504
	ibuf_data_sizes_update(data, root, &mtr);
505
	/*
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
506
	if (!data->empty) {
507 508
	fprintf(stderr,
	"InnoDB: index entries found in the insert buffer\n");
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
509
	} else {
510 511
	fprintf(stderr,
	"InnoDB: insert buffer empty\n");
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
512
	}
513
	*/
514 515 516 517 518 519
	mutex_exit(&ibuf_mutex);

	mtr_commit(&mtr);

	ibuf_exit();

520 521 522
	heap = mem_heap_create(450);
	buf = mem_heap_alloc(heap, 50);

523
	sprintf(buf, "SYS_IBUF_TABLE_%lu", (ulong) space);
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
524
	/* use old-style record format for the insert buffer */
525
	table = dict_mem_table_create(buf, space, 2, 0);
526

527 528
	dict_mem_table_add_col(table, heap, "PAGE_NO", DATA_BINARY, 0, 0);
	dict_mem_table_add_col(table, heap, "TYPES", DATA_BINARY, 0, 0);
529 530 531

	table->id = ut_dulint_add(DICT_IBUF_ID_MIN, space);

532 533
	dict_table_add_to_cache(table, heap);
	mem_heap_free(heap);
534

535 536 537
	index = dict_mem_index_create(
		buf, "CLUST_IND", space,
		DICT_CLUSTERED | DICT_UNIVERSAL | DICT_IBUF, 2);
538

539 540
	dict_mem_index_add_field(index, "PAGE_NO", 0);
	dict_mem_index_add_field(index, "TYPES", 0);
541 542 543

	index->id = ut_dulint_add(DICT_IBUF_ID_MIN, space);

544
	dict_index_add_to_cache(table, index, FSP_IBUF_TREE_ROOT_PAGE_NO);
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572

	data->index = dict_table_get_first_index(table);

	mutex_enter(&ibuf_mutex);

	UT_LIST_ADD_LAST(data_list, ibuf->data_list, data);

	mutex_exit(&ibuf_mutex);

	return(data);
}

/*************************************************************************
Initializes an ibuf bitmap page. */

void
ibuf_bitmap_page_init(
/*==================*/
	page_t*	page,	/* in: bitmap page */
	mtr_t*	mtr)	/* in: mtr */
{
	ulint	bit_offset;
	ulint	byte_offset;

	/* Write all zeros to the bitmap */

	bit_offset = XDES_DESCRIBED_PER_PAGE * IBUF_BITS_PER_PAGE;

573 574
	byte_offset = bit_offset / 8 + 1;
	/* better: byte_offset = UT_BITS_IN_BYTES(bit_offset); */
575

576
	fil_page_set_type(page, FIL_PAGE_IBUF_BITMAP);
577

578 579 580
	memset(page + IBUF_BITMAP, 0, byte_offset);

	/* The remaining area (up to the page trailer) is uninitialized. */
581 582 583 584 585 586 587 588 589 590 591 592

	mlog_write_initial_log_record(page, MLOG_IBUF_BITMAP_INIT, mtr);
}

/*************************************************************************
Parses a redo log record of an ibuf bitmap page init. */

byte*
ibuf_parse_bitmap_init(
/*===================*/
			/* out: end of log record or NULL */
	byte*	ptr,	/* in: buffer */
593
	byte*	end_ptr __attribute__((unused)), /* in: buffer end */
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
	page_t*	page,	/* in: page or NULL */
	mtr_t*	mtr)	/* in: mtr or NULL */
{
	ut_ad(ptr && end_ptr);

	if (page) {
		ibuf_bitmap_page_init(page, mtr);
	}

	return(ptr);
}

/************************************************************************
Gets the desired bits for a given page from a bitmap page. */
UNIV_INLINE
ulint
ibuf_bitmap_page_get_bits(
/*======================*/
			/* out: value of bits */
	page_t*	page,	/* in: bitmap page */
	ulint	page_no,/* in: page whose bits to get */
	ulint	bit,	/* in: IBUF_BITMAP_FREE, IBUF_BITMAP_BUFFERED, ... */
616 617 618
	mtr_t*	mtr __attribute__((unused)))	/* in: mtr containing an
						x-latch to the bitmap
						page */
619 620 621 622 623 624 625
{
	ulint	byte_offset;
	ulint	bit_offset;
	ulint	map_byte;
	ulint	value;

	ut_ad(bit < IBUF_BITS_PER_PAGE);
626 627 628
#if IBUF_BITS_PER_PAGE % 2
# error "IBUF_BITS_PER_PAGE % 2 != 0"
#endif
629
	ut_ad(mtr_memo_contains(mtr, buf_block_align(page),
630
				MTR_MEMO_PAGE_X_FIX));
631 632

	bit_offset = (page_no % XDES_DESCRIBED_PER_PAGE) * IBUF_BITS_PER_PAGE
633
		+ bit;
634 635 636 637 638 639 640 641 642 643 644 645

	byte_offset = bit_offset / 8;
	bit_offset = bit_offset % 8;

	ut_ad(byte_offset + IBUF_BITMAP < UNIV_PAGE_SIZE);

	map_byte = mach_read_from_1(page + IBUF_BITMAP + byte_offset);

	value = ut_bit_get_nth(map_byte, bit_offset);

	if (bit == IBUF_BITMAP_FREE) {
		ut_ad(bit_offset + 1 < 8);
646

647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
		value = value * 2 + ut_bit_get_nth(map_byte, bit_offset + 1);
	}

	return(value);
}

/************************************************************************
Sets the desired bit for a given page in a bitmap page. */
static
void
ibuf_bitmap_page_set_bits(
/*======================*/
	page_t*	page,	/* in: bitmap page */
	ulint	page_no,/* in: page whose bits to set */
	ulint	bit,	/* in: IBUF_BITMAP_FREE, IBUF_BITMAP_BUFFERED, ... */
	ulint	val,	/* in: value to set */
	mtr_t*	mtr)	/* in: mtr containing an x-latch to the bitmap page */
{
	ulint	byte_offset;
	ulint	bit_offset;
	ulint	map_byte;

	ut_ad(bit < IBUF_BITS_PER_PAGE);
670 671 672
#if IBUF_BITS_PER_PAGE % 2
# error "IBUF_BITS_PER_PAGE % 2 != 0"
#endif
673
	ut_ad(mtr_memo_contains(mtr, buf_block_align(page),
674
				MTR_MEMO_PAGE_X_FIX));
675 676
#ifdef UNIV_IBUF_DEBUG
	ut_a((bit != IBUF_BITMAP_BUFFERED) || (val != FALSE)
677 678
	     || (0 == ibuf_count_get(buf_frame_get_space_id(page),
				     page_no)));
679 680
#endif
	bit_offset = (page_no % XDES_DESCRIBED_PER_PAGE) * IBUF_BITS_PER_PAGE
681
		+ bit;
682 683 684 685 686 687 688 689 690 691 692

	byte_offset = bit_offset / 8;
	bit_offset = bit_offset % 8;

	ut_ad(byte_offset + IBUF_BITMAP < UNIV_PAGE_SIZE);

	map_byte = mach_read_from_1(page + IBUF_BITMAP + byte_offset);

	if (bit == IBUF_BITMAP_FREE) {
		ut_ad(bit_offset + 1 < 8);
		ut_ad(val <= 3);
693

694 695 696 697 698 699
		map_byte = ut_bit_set_nth(map_byte, bit_offset, val / 2);
		map_byte = ut_bit_set_nth(map_byte, bit_offset + 1, val % 2);
	} else {
		ut_ad(val <= 1);
		map_byte = ut_bit_set_nth(map_byte, bit_offset, val);
	}
700

701
	mlog_write_ulint(page + IBUF_BITMAP + byte_offset, map_byte,
702
			 MLOG_1BYTE, mtr);
703 704 705 706 707 708 709 710 711 712 713 714 715
}

/************************************************************************
Calculates the bitmap page number for a given page number. */
UNIV_INLINE
ulint
ibuf_bitmap_page_no_calc(
/*=====================*/
				/* out: the bitmap page number where
				the file page is mapped */
	ulint	page_no)	/* in: tablespace page number */
{
	return(FSP_IBUF_BITMAP_OFFSET
716 717
	       + XDES_DESCRIBED_PER_PAGE
	       * (page_no / XDES_DESCRIBED_PER_PAGE));
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
}

/************************************************************************
Gets the ibuf bitmap page where the bits describing a given file page are
stored. */
static
page_t*
ibuf_bitmap_get_map_page(
/*=====================*/
			/* out: bitmap page where the file page is mapped,
			that is, the bitmap page containing the descriptor
			bits for the file page; the bitmap page is
			x-latched */
	ulint	space,	/* in: space id of the file page */
	ulint	page_no,/* in: page number of the file page */
	mtr_t*	mtr)	/* in: mtr */
{
	page_t*	page;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
736

737
	page = buf_page_get(space, ibuf_bitmap_page_no_calc(page_no),
738
			    RW_X_LATCH, mtr);
739
#ifdef UNIV_SYNC_DEBUG
740
	buf_page_dbg_add_level(page, SYNC_IBUF_BITMAP);
741
#endif /* UNIV_SYNC_DEBUG */
742 743 744 745 746 747 748

	return(page);
}

/****************************************************************************
Sets the free bits of the page in the ibuf bitmap. This is done in a separate
mini-transaction, hence this operation does not restrict further work to only
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
749
ibuf bitmap operations, which would result if the latch to the bitmap page
750 751 752 753 754 755
were kept. */
UNIV_INLINE
void
ibuf_set_free_bits_low(
/*===================*/
	ulint	type,	/* in: index type */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
756 757
	page_t*	page,	/* in: index page; free bit is set if the index is
			non-clustered and page level is 0 */
758 759 760 761 762
	ulint	val,	/* in: value to set: < 4 */
	mtr_t*	mtr)	/* in: mtr */
{
	page_t*	bitmap_page;

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
763
	if (type & DICT_CLUSTERED) {
764 765 766 767 768 769 770 771 772

		return;
	}

	if (btr_page_get_level_low(page) != 0) {

		return;
	}

773 774 775
	bitmap_page = ibuf_bitmap_get_map_page(
		buf_frame_get_space_id(page),
		buf_frame_get_page_no(page), mtr);
776
#ifdef UNIV_IBUF_DEBUG
777 778
# if 0
	fprintf(stderr,
779
		"Setting page no %lu free bits to %lu should be %lu\n",
780 781 782
		buf_frame_get_page_no(page), val,
		ibuf_index_page_calc_free(page));
# endif
783

784
	ut_a(val <= ibuf_index_page_calc_free(page));
785
#endif /* UNIV_IBUF_DEBUG */
786
	ibuf_bitmap_page_set_bits(bitmap_page, buf_frame_get_page_no(page),
787
				  IBUF_BITMAP_FREE, val, mtr);
788 789 790 791 792 793 794 795 796 797 798 799 800

}

/****************************************************************************
Sets the free bit of the page in the ibuf bitmap. This is done in a separate
mini-transaction, hence this operation does not restrict further work to only
ibuf bitmap operations, which would result if the latch to the bitmap page
were kept. */

void
ibuf_set_free_bits(
/*===============*/
	ulint	type,	/* in: index type */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
801 802
	page_t*	page,	/* in: index page; free bit is set if the index is
			non-clustered and page level is 0 */
803 804 805 806 807 808 809 810
	ulint	val,	/* in: value to set: < 4 */
	ulint	max_val)/* in: ULINT_UNDEFINED or a maximum value which
			the bits must have before setting; this is for
			debugging */
{
	mtr_t	mtr;
	page_t*	bitmap_page;

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
811
	if (type & DICT_CLUSTERED) {
812 813 814 815 816 817 818 819 820 821

		return;
	}

	if (btr_page_get_level_low(page) != 0) {

		return;
	}

	mtr_start(&mtr);
822

823 824 825
	bitmap_page = ibuf_bitmap_get_map_page(
		buf_frame_get_space_id(page), buf_frame_get_page_no(page),
		&mtr);
826 827 828 829 830

	if (max_val != ULINT_UNDEFINED) {
#ifdef UNIV_IBUF_DEBUG
		ulint	old_val;

831 832 833
		old_val = ibuf_bitmap_page_get_bits(
			bitmap_page, buf_frame_get_page_no(page),
			IBUF_BITMAP_FREE, &mtr);
834
# if 0
835
		if (old_val != max_val) {
836 837 838 839
			fprintf(stderr,
				"Ibuf: page %lu old val %lu max val %lu\n",
				buf_frame_get_page_no(page),
				old_val, max_val);
840
		}
841
# endif
842 843 844 845 846

		ut_a(old_val <= max_val);
#endif
	}
#ifdef UNIV_IBUF_DEBUG
847 848 849 850 851
# if 0
	fprintf(stderr, "Setting page no %lu free bits to %lu should be %lu\n",
		buf_frame_get_page_no(page), val,
		ibuf_index_page_calc_free(page));
# endif
852 853

	ut_a(val <= ibuf_index_page_calc_free(page));
854
#endif
855
	ibuf_bitmap_page_set_bits(bitmap_page, buf_frame_get_page_no(page),
856
				  IBUF_BITMAP_FREE, val, &mtr);
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
	mtr_commit(&mtr);
}

/****************************************************************************
Resets the free bits of the page in the ibuf bitmap. This is done in a
separate mini-transaction, hence this operation does not restrict further
work to only ibuf bitmap operations, which would result if the latch to the
bitmap page were kept. */

void
ibuf_reset_free_bits_with_type(
/*===========================*/
	ulint	type,	/* in: index type */
	page_t*	page)	/* in: index page; free bits are set to 0 if the index
			is non-clustered and non-unique and the page level is
			0 */
{
	ibuf_set_free_bits(type, page, 0, ULINT_UNDEFINED);
}

/****************************************************************************
Resets the free bits of the page in the ibuf bitmap. This is done in a
separate mini-transaction, hence this operation does not restrict further
work to solely ibuf bitmap operations, which would result if the latch to
the bitmap page were kept. */

void
ibuf_reset_free_bits(
/*=================*/
	dict_index_t*	index,	/* in: index */
	page_t*		page)	/* in: index page; free bits are set to 0 if
				the index is non-clustered and non-unique and
				the page level is 0 */
{
	ibuf_set_free_bits(index->type, page, 0, ULINT_UNDEFINED);
}

/**************************************************************************
Updates the free bits for a page to reflect the present state. Does this
in the mtr given, which means that the latching order rules virtually prevent
any further operations for this OS thread until mtr is committed. */

void
ibuf_update_free_bits_low(
/*======================*/
	dict_index_t*	index,		/* in: index */
	page_t*		page,		/* in: index page */
	ulint		max_ins_size,	/* in: value of maximum insert size
					with reorganize before the latest
					operation performed to the page */
	mtr_t*		mtr)		/* in: mtr */
{
	ulint	before;
	ulint	after;

	before = ibuf_index_page_calc_free_bits(max_ins_size);

	after = ibuf_index_page_calc_free(page);

	if (before != after) {
		ibuf_set_free_bits_low(index->type, page, after, mtr);
	}
}

/**************************************************************************
Updates the free bits for the two pages to reflect the present state. Does
this in the mtr given, which means that the latching order rules virtually
prevent any further operations until mtr is committed. */

void
ibuf_update_free_bits_for_two_pages_low(
/*====================================*/
	dict_index_t*	index,	/* in: index */
	page_t*		page1,	/* in: index page */
	page_t*		page2,	/* in: index page */
	mtr_t*		mtr)	/* in: mtr */
{
	ulint	state;

	/* As we have to x-latch two random bitmap pages, we have to acquire
	the bitmap mutex to prevent a deadlock with a similar operation
	performed by another OS thread. */

	mutex_enter(&ibuf_bitmap_mutex);
941

942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
	state = ibuf_index_page_calc_free(page1);

	ibuf_set_free_bits_low(index->type, page1, state, mtr);

	state = ibuf_index_page_calc_free(page2);

	ibuf_set_free_bits_low(index->type, page2, state, mtr);

	mutex_exit(&ibuf_bitmap_mutex);
}

/**************************************************************************
Returns TRUE if the page is one of the fixed address ibuf pages. */
UNIV_INLINE
ibool
ibuf_fixed_addr_page(
/*=================*/
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
959
			/* out: TRUE if a fixed address ibuf i/o page */
960
	ulint	space,	/* in: space id */
961 962
	ulint	page_no)/* in: page number */
{
963
	return((space == 0 && page_no == IBUF_TREE_ROOT_PAGE_NO)
964
	       || ibuf_bitmap_page(page_no));
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
}

/***************************************************************************
Checks if a page is a level 2 or 3 page in the ibuf hierarchy of pages. */

ibool
ibuf_page(
/*======*/
			/* out: TRUE if level 2 or level 3 page */
	ulint	space,	/* in: space id */
	ulint	page_no)/* in: page number */
{
	page_t*	bitmap_page;
	mtr_t	mtr;
	ibool	ret;

	if (recv_no_ibuf_operations) {
		/* Recovery is running: no ibuf operations should be
		performed */

		return(FALSE);
	}

988
	if (ibuf_fixed_addr_page(space, page_no)) {
989 990 991 992

		return(TRUE);
	}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
993 994 995 996 997 998
	if (space != 0) {
		/* Currently we only have an ibuf tree in space 0 */

		return(FALSE);
	}

999 1000 1001 1002 1003 1004 1005
	ut_ad(fil_space_get_type(space) == FIL_TABLESPACE);

	mtr_start(&mtr);

	bitmap_page = ibuf_bitmap_get_map_page(space, page_no, &mtr);

	ret = ibuf_bitmap_page_get_bits(bitmap_page, page_no, IBUF_BITMAP_IBUF,
1006
					&mtr);
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
	mtr_commit(&mtr);

	return(ret);
}

/***************************************************************************
Checks if a page is a level 2 or 3 page in the ibuf hierarchy of pages. */

ibool
ibuf_page_low(
/*==========*/
			/* out: TRUE if level 2 or level 3 page */
	ulint	space,	/* in: space id */
	ulint	page_no,/* in: page number */
	mtr_t*	mtr)	/* in: mtr which will contain an x-latch to the
			bitmap page if the page is not one of the fixed
			address ibuf pages */
{
	page_t*	bitmap_page;
	ibool	ret;

1028
	if (ibuf_fixed_addr_page(space, page_no)) {
1029 1030 1031 1032 1033 1034 1035

		return(TRUE);
	}

	bitmap_page = ibuf_bitmap_get_map_page(space, page_no, mtr);

	ret = ibuf_bitmap_page_get_bits(bitmap_page, page_no, IBUF_BITMAP_IBUF,
1036
					mtr);
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
	return(ret);
}

/************************************************************************
Returns the page number field of an ibuf record. */
static
ulint
ibuf_rec_get_page_no(
/*=================*/
			/* out: page number */
	rec_t*	rec)	/* in: ibuf record */
{
	byte*	field;
	ulint	len;

	ut_ad(ibuf_inside());
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1053
	ut_ad(rec_get_n_fields_old(rec) > 2);
1054

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1055
	field = rec_get_nth_field_old(rec, 1, &len);
1056

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1057 1058 1059 1060
	if (len == 1) {
		/* This is of the >= 4.1.x record format */
		ut_a(trx_sys_multiple_tablespace_format);

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1061
		field = rec_get_nth_field_old(rec, 2, &len);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1062 1063 1064 1065
	} else {
		ut_a(trx_doublewrite_must_reset_space_ids);
		ut_a(!trx_sys_multiple_tablespace_format);

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1066
		field = rec_get_nth_field_old(rec, 0, &len);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1067 1068 1069
	}

	ut_a(len == 4);
1070 1071 1072 1073

	return(mach_read_from_4(field));
}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
/************************************************************************
Returns the space id field of an ibuf record. For < 4.1.x format records
returns 0. */
static
ulint
ibuf_rec_get_space(
/*===============*/
			/* out: space id */
	rec_t*	rec)	/* in: ibuf record */
{
	byte*	field;
	ulint	len;

	ut_ad(ibuf_inside());
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1088
	ut_ad(rec_get_n_fields_old(rec) > 2);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1089

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1090
	field = rec_get_nth_field_old(rec, 1, &len);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1091 1092 1093 1094 1095

	if (len == 1) {
		/* This is of the >= 4.1.x record format */

		ut_a(trx_sys_multiple_tablespace_format);
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1096
		field = rec_get_nth_field_old(rec, 0, &len);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
		ut_a(len == 4);

		return(mach_read_from_4(field));
	}

	ut_a(trx_doublewrite_must_reset_space_ids);
	ut_a(!trx_sys_multiple_tablespace_format);

	return(0);
}

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
/************************************************************************
Creates a dummy index for inserting a record to a non-clustered index.
*/
static
dict_index_t*
ibuf_dummy_index_create(
/*====================*/
				/* out: dummy index */
	ulint		n,	/* in: number of fields */
	ibool		comp)	/* in: TRUE=use compact record format */
{
	dict_table_t*	table;
	dict_index_t*	index;
1121

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1122
	table = dict_mem_table_create("IBUF_DUMMY",
1123 1124
				      DICT_HDR_SPACE, n,
				      comp ? DICT_TF_COMPACT : 0);
1125

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1126
	index = dict_mem_index_create("IBUF_DUMMY", "IBUF_DUMMY",
1127
				      DICT_HDR_SPACE, 0, n);
1128

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1129
	index->table = table;
1130

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1131 1132
	/* avoid ut_ad(index->cached) in dict_index_get_n_unique_in_tree */
	index->cached = TRUE;
1133

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1134 1135 1136 1137 1138 1139 1140
	return(index);
}
/************************************************************************
Add a column to the dummy index */
static
void
ibuf_dummy_index_add_col(
1141
/*=====================*/
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1142
	dict_index_t*	index,	/* in: dummy index */
1143 1144
	dtype_t*	type,	/* in: the data type of the column */
	ulint		len)	/* in: length of the column */
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1145 1146
{
	ulint	i	= index->table->n_def;
1147
	dict_mem_table_add_col(index->table, NULL, NULL,
1148 1149
			       dtype_get_mtype(type),
			       dtype_get_prtype(type),
1150 1151
			       dtype_get_len(type));
	dict_index_add_col(index, index->table, (dict_col_t*)
1152
			   dict_table_get_nth_col(index->table, i), len);
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1153 1154 1155 1156 1157 1158 1159
}
/************************************************************************
Deallocates a dummy index for inserting a record to a non-clustered index.
*/
static
void
ibuf_dummy_index_free(
1160
/*==================*/
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1161 1162 1163
	dict_index_t*	index)	/* in: dummy index */
{
	dict_table_t*	table = index->table;
1164 1165 1166

	dict_mem_index_free(index);
	dict_mem_table_free(table);
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
}

/*************************************************************************
Builds the entry to insert into a non-clustered index when we have the
corresponding record in an ibuf index. */
static
dtuple_t*
ibuf_build_entry_from_ibuf_rec(
/*===========================*/
					/* out, own: entry to insert to
					a non-clustered index; NOTE that
					as we copy pointers to fields in
					ibuf_rec, the caller must hold a
					latch to the ibuf_rec page as long
					as the entry is used! */
	rec_t*		ibuf_rec,	/* in: record in an insert buffer */
	mem_heap_t*	heap,		/* in: heap where built */
	dict_index_t**	pindex)		/* out, own: dummy index that
					describes the entry */
{
	dtuple_t*	tuple;
	dfield_t*	field;
	ulint		n_fields;
	byte*		types;
	const byte*	data;
	ulint		len;
	ulint		i;
	dict_index_t*	index;

	data = rec_get_nth_field_old(ibuf_rec, 1, &len);

	if (len > 1) {
		/* This a < 4.1.x format record */

		ut_a(trx_doublewrite_must_reset_space_ids);
		ut_a(!trx_sys_multiple_tablespace_format);

		n_fields = rec_get_n_fields_old(ibuf_rec) - 2;
		tuple = dtuple_create(heap, n_fields);
		types = rec_get_nth_field_old(ibuf_rec, 1, &len);

		ut_a(len == n_fields * DATA_ORDER_NULL_TYPE_BUF_SIZE);

		for (i = 0; i < n_fields; i++) {
			field = dtuple_get_nth_field(tuple, i);

			data = rec_get_nth_field_old(ibuf_rec, i + 2, &len);

			dfield_set_data(field, data, len);

1217 1218 1219
			dtype_read_for_order_and_null_size(
				dfield_get_type(field),
				types + i * DATA_ORDER_NULL_TYPE_BUF_SIZE);
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
		}

		*pindex = ibuf_dummy_index_create(n_fields, FALSE);
		return(tuple);
	}

	/* This a >= 4.1.x format record */

	ut_a(trx_sys_multiple_tablespace_format);
	ut_a(*data == 0);
	ut_a(rec_get_n_fields_old(ibuf_rec) > 4);

	n_fields = rec_get_n_fields_old(ibuf_rec) - 4;

	tuple = dtuple_create(heap, n_fields);

	types = rec_get_nth_field_old(ibuf_rec, 3, &len);

	ut_a(len % DATA_NEW_ORDER_NULL_TYPE_BUF_SIZE <= 1);
1239 1240
	index = ibuf_dummy_index_create(
		n_fields, len % DATA_NEW_ORDER_NULL_TYPE_BUF_SIZE);
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257

	if (len % DATA_NEW_ORDER_NULL_TYPE_BUF_SIZE) {
		/* compact record format */
		len--;
		ut_a(*types == 0);
		types++;
	}

	ut_a(len == n_fields * DATA_NEW_ORDER_NULL_TYPE_BUF_SIZE);

	for (i = 0; i < n_fields; i++) {
		field = dtuple_get_nth_field(tuple, i);

		data = rec_get_nth_field_old(ibuf_rec, i + 4, &len);

		dfield_set_data(field, data, len);

1258 1259 1260
		dtype_new_read_for_order_and_null_size(
			dfield_get_type(field),
			types + i * DATA_NEW_ORDER_NULL_TYPE_BUF_SIZE);
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1261

1262
		ibuf_dummy_index_add_col(index, dfield_get_type(field), len);
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1263 1264 1265 1266 1267 1268
	}

	*pindex = index;
	return(tuple);
}

1269 1270 1271 1272 1273 1274 1275 1276 1277
/************************************************************************
Returns the space taken by a stored non-clustered index entry if converted to
an index record. */
static
ulint
ibuf_rec_get_volume(
/*================*/
			/* out: size of index record in bytes + an upper
			limit of the space taken in the page directory */
1278
	rec_t*	ibuf_rec)/* in: ibuf record */
1279
{
1280
	dtype_t	dtype;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1281
	ibool	new_format	= FALSE;
1282
	ulint	data_size	= 0;
1283
	ulint	n_fields;
1284 1285
	byte*	types;
	byte*	data;
1286
	ulint	len;
1287
	ulint	i;
1288 1289

	ut_ad(ibuf_inside());
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1290 1291 1292
	ut_ad(rec_get_n_fields_old(ibuf_rec) > 2);

	data = rec_get_nth_field_old(ibuf_rec, 1, &len);
1293

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1294
	if (len > 1) {
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1295
		/* < 4.1.x format record */
1296

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1297 1298 1299
		ut_a(trx_doublewrite_must_reset_space_ids);
		ut_a(!trx_sys_multiple_tablespace_format);

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1300
		n_fields = rec_get_n_fields_old(ibuf_rec) - 2;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1301

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1302
		types = rec_get_nth_field_old(ibuf_rec, 1, &len);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1303 1304 1305

		ut_ad(len == n_fields * DATA_ORDER_NULL_TYPE_BUF_SIZE);
	} else {
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1306
		/* >= 4.1.x format record */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1307 1308

		ut_a(trx_sys_multiple_tablespace_format);
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318
		ut_a(*data == 0);

		types = rec_get_nth_field_old(ibuf_rec, 3, &len);

		ut_a(len % DATA_NEW_ORDER_NULL_TYPE_BUF_SIZE <= 1);
		if (len % DATA_NEW_ORDER_NULL_TYPE_BUF_SIZE) {
			/* compact record format */
			ulint		volume;
			dict_index_t*	dummy_index;
			mem_heap_t*	heap = mem_heap_create(500);
1319 1320
			dtuple_t*	entry = ibuf_build_entry_from_ibuf_rec(
				ibuf_rec, heap, &dummy_index);
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1321 1322 1323 1324 1325
			volume = rec_get_converted_size(dummy_index, entry);
			ibuf_dummy_index_free(dummy_index);
			mem_heap_free(heap);
			return(volume + page_dir_calc_reserved_space(1));
		}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1326

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1327
		n_fields = rec_get_n_fields_old(ibuf_rec) - 4;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1328

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1329
		new_format = TRUE;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1330
	}
1331

1332
	for (i = 0; i < n_fields; i++) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1333
		if (new_format) {
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1334
			data = rec_get_nth_field_old(ibuf_rec, i + 4, &len);
1335

1336 1337 1338
			dtype_new_read_for_order_and_null_size(
				&dtype, types + i
				* DATA_NEW_ORDER_NULL_TYPE_BUF_SIZE);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1339
		} else {
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1340
			data = rec_get_nth_field_old(ibuf_rec, i + 2, &len);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1341

1342 1343 1344
			dtype_read_for_order_and_null_size(
				&dtype, types + i
				* DATA_ORDER_NULL_TYPE_BUF_SIZE);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1345
		}
1346 1347 1348 1349 1350 1351 1352

		if (len == UNIV_SQL_NULL) {
			data_size += dtype_get_sql_null_size(&dtype);
		} else {
			data_size += len;
		}
	}
1353 1354

	return(data_size + rec_get_converted_extra_size(data_size, n_fields)
1355
	       + page_dir_calc_reserved_space(1));
1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
}

/*************************************************************************
Builds the tuple to insert to an ibuf tree when we have an entry for a
non-clustered index. */
static
dtuple_t*
ibuf_entry_build(
/*=============*/
				/* out, own: entry to insert into an ibuf
				index tree; NOTE that the original entry
				must be kept because we copy pointers to its
				fields */
1369
	dict_index_t*	index,	/* in: non-clustered index */
1370
	dtuple_t*	entry,	/* in: entry for a non-clustered index */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1371
	ulint		space,	/* in: space id */
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
	ulint		page_no,/* in: index page number where entry should
				be inserted */
	mem_heap_t*	heap)	/* in: heap into which to build */
{
	dtuple_t*	tuple;
	dfield_t*	field;
	dfield_t*	entry_field;
	ulint		n_fields;
	byte*		buf;
	byte*		buf2;
	ulint		i;
1383

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1384 1385
	/* Starting from 4.1.x, we have to build a tuple whose
	(1) first field is the space id,
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1386
	(2) the second field a single marker byte (0) to tell that this
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1387 1388 1389
	is a new format record,
	(3) the third contains the page number, and
	(4) the fourth contains the relevent type information of each data
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1390
	field; the length of this field % DATA_NEW_ORDER_NULL_TYPE_BUF_SIZE is
1391 1392 1393
	(a) 0 for b-trees in the old format, and
	(b) 1 for b-trees in the compact format, the first byte of the field
	being the marker (0);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1394 1395 1396
	(5) and the rest of the fields are copied from entry. All fields
	in the tuple are ordered like the type binary in our insert buffer
	tree. */
1397 1398 1399

	n_fields = dtuple_get_n_fields(entry);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1400
	tuple = dtuple_create(heap, n_fields + 4);
1401

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1402
	/* Store the space id in tuple */
1403 1404 1405 1406 1407

	field = dtuple_get_nth_field(tuple, 0);

	buf = mem_heap_alloc(heap, 4);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1408
	mach_write_to_4(buf, space);
1409 1410 1411

	dfield_set_data(field, buf, 4);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1412 1413 1414
	/* Store the marker byte field in tuple */

	field = dtuple_get_nth_field(tuple, 1);
1415

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1416
	buf = mem_heap_alloc(heap, 1);
1417

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1418
	/* We set the marker byte zero */
1419

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1420
	mach_write_to_1(buf, 0);
1421

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1422 1423 1424 1425 1426
	dfield_set_data(field, buf, 1);

	/* Store the page number in tuple */

	field = dtuple_get_nth_field(tuple, 2);
1427

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1428 1429 1430 1431 1432 1433 1434 1435 1436
	buf = mem_heap_alloc(heap, 4);

	mach_write_to_4(buf, page_no);

	dfield_set_data(field, buf, 4);

	/* Store the type info in buf2, and add the fields from entry to
	tuple */
	buf2 = mem_heap_alloc(heap, n_fields
1437
			      * DATA_NEW_ORDER_NULL_TYPE_BUF_SIZE
1438 1439
			      + dict_table_is_comp(index->table));
	if (dict_table_is_comp(index->table)) {
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1440 1441
		*buf2++ = 0; /* write the compact format indicator */
	}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1442
	for (i = 0; i < n_fields; i++) {
1443 1444 1445
		ulint			fixed_len;
		const dict_field_t*	ifield;

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1446 1447 1448 1449 1450
		/* We add 4 below because we have the 4 extra fields at the
		start of an ibuf record */

		field = dtuple_get_nth_field(tuple, i + 4);
		entry_field = dtuple_get_nth_field(entry, i);
1451 1452
		dfield_copy(field, entry_field);

1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473
		ifield = dict_index_get_nth_field(index, i);
		/* Prefix index columns of fixed-length columns are of
		fixed length.  However, in the function call below,
		dfield_get_type(entry_field) contains the fixed length
		of the column in the clustered index.  Replace it with
		the fixed length of the secondary index column. */
		fixed_len = ifield->fixed_len;

#ifdef UNIV_DEBUG
		if (fixed_len) {
			/* dict_index_add_col() should guarantee these */
			ut_ad(fixed_len <= (ulint) entry_field->type.len);
			if (ifield->prefix_len) {
				ut_ad(ifield->prefix_len == fixed_len);
			} else {
				ut_ad(fixed_len
				      == (ulint) entry_field->type.len);
			}
		}
#endif /* UNIV_DEBUG */

1474 1475
		dtype_new_store_for_order_and_null_size(
			buf2 + i * DATA_NEW_ORDER_NULL_TYPE_BUF_SIZE,
1476
			dfield_get_type(entry_field), fixed_len);
1477 1478
	}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1479
	/* Store the type info in buf2 to field 3 of tuple */
1480

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1481
	field = dtuple_get_nth_field(tuple, 3);
1482

1483
	if (dict_table_is_comp(index->table)) {
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1484 1485 1486
		buf2--;
	}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1487
	dfield_set_data(field, buf2, n_fields
1488
			* DATA_NEW_ORDER_NULL_TYPE_BUF_SIZE
1489
			+ dict_table_is_comp(index->table));
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1490
	/* Set all the types in the new tuple binary */
1491

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1492
	dtuple_set_types_binary(tuple, n_fields + 4);
1493 1494

	return(tuple);
1495
}
1496 1497

/*************************************************************************
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1498 1499
Builds a search tuple used to search buffered inserts for an index page.
This is for < 4.1.x format records */
1500 1501 1502 1503 1504
static
dtuple_t*
ibuf_search_tuple_build(
/*====================*/
				/* out, own: search tuple */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1505
	ulint		space,	/* in: space id */
1506 1507 1508 1509 1510 1511
	ulint		page_no,/* in: index page number */
	mem_heap_t*	heap)	/* in: heap into which to build */
{
	dtuple_t*	tuple;
	dfield_t*	field;
	byte*		buf;
1512

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1513 1514 1515 1516
	ut_a(space == 0);
	ut_a(trx_doublewrite_must_reset_space_ids);
	ut_a(!trx_sys_multiple_tablespace_format);

1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533
	tuple = dtuple_create(heap, 1);

	/* Store the page number in tuple */

	field = dtuple_get_nth_field(tuple, 0);

	buf = mem_heap_alloc(heap, 4);

	mach_write_to_4(buf, page_no);

	dfield_set_data(field, buf, 4);

	dtuple_set_types_binary(tuple, 1);

	return(tuple);
}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548
/*************************************************************************
Builds a search tuple used to search buffered inserts for an index page.
This is for >= 4.1.x format records. */
static
dtuple_t*
ibuf_new_search_tuple_build(
/*========================*/
				/* out, own: search tuple */
	ulint		space,	/* in: space id */
	ulint		page_no,/* in: index page number */
	mem_heap_t*	heap)	/* in: heap into which to build */
{
	dtuple_t*	tuple;
	dfield_t*	field;
	byte*		buf;
1549

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588
	ut_a(trx_sys_multiple_tablespace_format);

	tuple = dtuple_create(heap, 3);

	/* Store the space id in tuple */

	field = dtuple_get_nth_field(tuple, 0);

	buf = mem_heap_alloc(heap, 4);

	mach_write_to_4(buf, space);

	dfield_set_data(field, buf, 4);

	/* Store the new format record marker byte */

	field = dtuple_get_nth_field(tuple, 1);

	buf = mem_heap_alloc(heap, 1);

	mach_write_to_1(buf, 0);

	dfield_set_data(field, buf, 1);

	/* Store the page number in tuple */

	field = dtuple_get_nth_field(tuple, 2);

	buf = mem_heap_alloc(heap, 4);

	mach_write_to_4(buf, page_no);

	dfield_set_data(field, buf, 4);

	dtuple_set_types_binary(tuple, 3);

	return(tuple);
}

1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626
/*************************************************************************
Checks if there are enough pages in the free list of the ibuf tree that we
dare to start a pessimistic insert to the insert buffer. */
UNIV_INLINE
ibool
ibuf_data_enough_free_for_insert(
/*=============================*/
				/* out: TRUE if enough free pages in list */
	ibuf_data_t*	data)	/* in: ibuf data for the space */
{
	ut_ad(mutex_own(&ibuf_mutex));

	/* We want a big margin of free pages, because a B-tree can sometimes
	grow in size also if records are deleted from it, as the node pointers
	can change, and we must make sure that we are able to delete the
	inserts buffered for pages that we read to the buffer pool, without
	any risk of running out of free space in the insert buffer. */

	if (data->free_list_len >= data->size / 2 + 3 * data->height) {

		return(TRUE);
	}

	return(FALSE);
}

/*************************************************************************
Checks if there are enough pages in the free list of the ibuf tree that we
should remove them and free to the file space management. */
UNIV_INLINE
ibool
ibuf_data_too_much_free(
/*====================*/
				/* out: TRUE if enough free pages in list */
	ibuf_data_t*	data)	/* in: ibuf data for the space */
{
	ut_ad(mutex_own(&ibuf_mutex));

1627
	return(data->free_list_len >= 3 + data->size / 2 + 3 * data->height);
1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648
}

/*************************************************************************
Allocates a new page from the ibuf file segment and adds it to the free
list. */
static
ulint
ibuf_add_free_page(
/*===============*/
					/* out: DB_SUCCESS, or DB_STRONG_FAIL
					if no space left */
	ulint		space,		/* in: space id */
	ibuf_data_t*	ibuf_data)	/* in: ibuf data for the space */
{
	mtr_t	mtr;
	page_t*	header_page;
	ulint	page_no;
	page_t*	page;
	page_t*	root;
	page_t*	bitmap_page;

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1649 1650
	ut_a(space == 0);

1651 1652 1653 1654 1655
	mtr_start(&mtr);

	/* Acquire the fsp latch before the ibuf header, obeying the latching
	order */
	mtr_x_lock(fil_space_get_latch(space), &mtr);
1656

1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669
	header_page = ibuf_header_page_get(space, &mtr);

	/* Allocate a new page: NOTE that if the page has been a part of a
	non-clustered index which has subsequently been dropped, then the
	page may have buffered inserts in the insert buffer, and these
	should be deleted from there. These get deleted when the page
	allocation creates the page in buffer. Thus the call below may end
	up calling the insert buffer routines and, as we yet have no latches
	to insert buffer tree pages, these routines can run without a risk
	of a deadlock. This is the reason why we created a special ibuf
	header page apart from the ibuf tree. */

	page_no = fseg_alloc_free_page(header_page + IBUF_HEADER
1670 1671
				       + IBUF_TREE_SEG_HEADER, 0, FSP_UP,
				       &mtr);
1672 1673 1674 1675 1676 1677 1678 1679
	if (page_no == FIL_NULL) {
		mtr_commit(&mtr);

		return(DB_STRONG_FAIL);
	}

	page = buf_page_get(space, page_no, RW_X_LATCH, &mtr);

1680
#ifdef UNIV_SYNC_DEBUG
1681
	buf_page_dbg_add_level(page, SYNC_TREE_NODE_NEW);
1682
#endif /* UNIV_SYNC_DEBUG */
1683 1684 1685 1686 1687 1688 1689 1690 1691 1692

	ibuf_enter();

	mutex_enter(&ibuf_mutex);

	root = ibuf_tree_root_get(ibuf_data, space, &mtr);

	/* Add the page to the free list and update the ibuf size data */

	flst_add_last(root + PAGE_HEADER + PAGE_BTR_IBUF_FREE_LIST,
1693
		      page + PAGE_HEADER + PAGE_BTR_IBUF_FREE_LIST_NODE, &mtr);
1694

1695
	mlog_write_ulint(page + FIL_PAGE_TYPE, FIL_PAGE_IBUF_FREE_LIST,
1696
			 MLOG_2BYTES, &mtr);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1697

1698 1699 1700 1701 1702 1703 1704 1705 1706
	ibuf_data->seg_size++;
	ibuf_data->free_list_len++;

	/* Set the bit indicating that this page is now an ibuf tree page
	(level 2 page) */

	bitmap_page = ibuf_bitmap_get_map_page(space, page_no, &mtr);

	ibuf_bitmap_page_set_bits(bitmap_page, page_no, IBUF_BITMAP_IBUF,
1707
				  TRUE, &mtr);
1708 1709 1710 1711 1712
	mtr_commit(&mtr);

	mutex_exit(&ibuf_mutex);

	ibuf_exit();
1713 1714

	return(DB_SUCCESS);
1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733
}

/*************************************************************************
Removes a page from the free list and frees it to the fsp system. */
static
void
ibuf_remove_free_page(
/*==================*/
	ulint		space,		/* in: space id */
	ibuf_data_t*	ibuf_data)	/* in: ibuf data for the space */
{
	mtr_t	mtr;
	mtr_t	mtr2;
	page_t*	header_page;
	ulint	page_no;
	page_t*	page;
	page_t*	root;
	page_t*	bitmap_page;

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1734 1735
	ut_a(space == 0);

1736 1737 1738 1739 1740
	mtr_start(&mtr);

	/* Acquire the fsp latch before the ibuf header, obeying the latching
	order */
	mtr_x_lock(fil_space_get_latch(space), &mtr);
1741

1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755
	header_page = ibuf_header_page_get(space, &mtr);

	/* Prevent pessimistic inserts to insert buffer trees for a while */
	mutex_enter(&ibuf_pessimistic_insert_mutex);

	ibuf_enter();

	mutex_enter(&ibuf_mutex);

	if (!ibuf_data_too_much_free(ibuf_data)) {

		mutex_exit(&ibuf_mutex);

		ibuf_exit();
1756

1757 1758 1759 1760 1761 1762
		mutex_exit(&ibuf_pessimistic_insert_mutex);

		mtr_commit(&mtr);

		return;
	}
1763

1764
	mtr_start(&mtr2);
1765

1766 1767 1768
	root = ibuf_tree_root_get(ibuf_data, space, &mtr2);

	page_no = flst_get_last(root + PAGE_HEADER + PAGE_BTR_IBUF_FREE_LIST,
1769 1770
				&mtr2)
		.page;
1771 1772 1773 1774

	/* NOTE that we must release the latch on the ibuf tree root
	because in fseg_free_page we access level 1 pages, and the root
	is a level 2 page. */
1775

1776 1777 1778 1779
	mtr_commit(&mtr2);
	mutex_exit(&ibuf_mutex);

	ibuf_exit();
1780

1781 1782 1783 1784 1785
	/* Since pessimistic inserts were prevented, we know that the
	page is still in the free list. NOTE that also deletes may take
	pages from the free list, but they take them from the start, and
	the free list was so long that they cannot have taken the last
	page from it. */
1786

1787
	fseg_free_page(header_page + IBUF_HEADER + IBUF_TREE_SEG_HEADER,
1788
		       space, page_no, &mtr);
1789 1790 1791
#ifdef UNIV_DEBUG_FILE_ACCESSES
	buf_page_reset_file_page_was_freed(space, page_no);
#endif
1792
	ibuf_enter();
1793

1794 1795 1796 1797 1798
	mutex_enter(&ibuf_mutex);

	root = ibuf_tree_root_get(ibuf_data, space, &mtr);

	ut_ad(page_no == flst_get_last(root + PAGE_HEADER
1799 1800
				       + PAGE_BTR_IBUF_FREE_LIST, &mtr)
	      .page);
1801 1802 1803

	page = buf_page_get(space, page_no, RW_X_LATCH, &mtr);

1804
#ifdef UNIV_SYNC_DEBUG
1805
	buf_page_dbg_add_level(page, SYNC_TREE_NODE);
1806
#endif /* UNIV_SYNC_DEBUG */
1807 1808

	/* Remove the page from the free list and update the ibuf size data */
1809

1810
	flst_remove(root + PAGE_HEADER + PAGE_BTR_IBUF_FREE_LIST,
1811
		    page + PAGE_HEADER + PAGE_BTR_IBUF_FREE_LIST_NODE, &mtr);
1812 1813 1814

	ibuf_data->seg_size--;
	ibuf_data->free_list_len--;
1815

1816 1817 1818 1819 1820 1821 1822 1823
	mutex_exit(&ibuf_pessimistic_insert_mutex);

	/* Set the bit indicating that this page is no more an ibuf tree page
	(level 2 page) */

	bitmap_page = ibuf_bitmap_get_map_page(space, page_no, &mtr);

	ibuf_bitmap_page_set_bits(bitmap_page, page_no, IBUF_BITMAP_IBUF,
1824
				  FALSE, &mtr);
1825 1826 1827
#ifdef UNIV_DEBUG_FILE_ACCESSES
	buf_page_set_file_page_was_freed(space, page_no);
#endif
1828 1829 1830 1831 1832 1833 1834 1835 1836 1837
	mtr_commit(&mtr);

	mutex_exit(&ibuf_mutex);

	ibuf_exit();
}

/***************************************************************************
Frees excess pages from the ibuf free list. This function is called when an OS
thread calls fsp services to allocate a new file segment, or a new page to a
1838
file segment, and the thread did not own the fsp latch before this call. */
1839 1840 1841 1842 1843 1844 1845 1846

void
ibuf_free_excess_pages(
/*===================*/
	ulint	space)	/* in: space id */
{
	ibuf_data_t*	ibuf_data;
	ulint		i;
monty@mysql.com's avatar
monty@mysql.com committed
1847

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1848
	if (space != 0) {
1849
		fprintf(stderr,
1850 1851
			"InnoDB: Error: calling ibuf_free_excess_pages"
			" for space %lu\n", (ulong) space);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1852 1853 1854
		return;
	}

1855
#ifdef UNIV_SYNC_DEBUG
1856
	ut_ad(rw_lock_own(fil_space_get_latch(space), RW_LOCK_EX));
1857
#endif /* UNIV_SYNC_DEBUG */
1858 1859
	ut_ad(rw_lock_get_x_lock_count(fil_space_get_latch(space)) == 1);
	ut_ad(!ibuf_inside());
1860

1861 1862 1863 1864 1865 1866 1867 1868 1869
	/* NOTE: We require that the thread did not own the latch before,
	because then we know that we can obey the correct latching order
	for ibuf latches */

	ibuf_data = fil_space_get_ibuf_data(space);

	if (ibuf_data == NULL) {
		/* Not yet initialized */

1870 1871 1872
#if 0 /* defined UNIV_DEBUG */
		fprintf(stderr,
			"Ibuf for space %lu not yet initialized\n", space);
1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909
#endif

		return;
	}

	/* Free at most a few pages at a time, so that we do not delay the
	requested service too much */

	for (i = 0; i < 4; i++) {

		mutex_enter(&ibuf_mutex);

		if (!ibuf_data_too_much_free(ibuf_data)) {

			mutex_exit(&ibuf_mutex);

			return;
		}

		mutex_exit(&ibuf_mutex);

		ibuf_remove_free_page(space, ibuf_data);
	}
}

/*************************************************************************
Reads page numbers from a leaf in an ibuf tree. */
static
ulint
ibuf_get_merge_page_nos(
/*====================*/
				/* out: a lower limit for the combined volume
				of records which will be merged */
	ibool		contract,/* in: TRUE if this function is called to
				contract the tree, FALSE if this is called
				when a single page becomes full and we look
				if it pays to read also nearby pages */
1910
	rec_t*		rec,	/* in: record from which we read up and down
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1911 1912 1913 1914 1915
				in the chain of records */
	ulint*		space_ids,/* in/out: space id's of the pages */
	ib_longlong*	space_versions,/* in/out: tablespace version
				timestamps; used to prevent reading in old
				pages after DISCARD + IMPORT tablespace */
1916 1917 1918 1919 1920 1921 1922
	ulint*		page_nos,/* in/out: buffer for at least
				IBUF_MAX_N_PAGES_MERGED many page numbers;
				the page numbers are in an ascending order */
	ulint*		n_stored)/* out: number of page numbers stored to
				page_nos in this function */
{
	ulint	prev_page_no;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1923
	ulint	prev_space_id;
1924
	ulint	first_page_no;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1925
	ulint	first_space_id;
1926
	ulint	rec_page_no;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1927
	ulint	rec_space_id;
1928 1929 1930 1931 1932 1933 1934 1935 1936 1937
	ulint	sum_volumes;
	ulint	volume_for_page;
	ulint	rec_volume;
	ulint	limit;
	ulint	n_pages;

	*n_stored = 0;

	limit = ut_min(IBUF_MAX_N_PAGES_MERGED, buf_pool->curr_size / 4);

1938
	if (page_rec_is_supremum(rec)) {
1939

1940
		rec = page_rec_get_prev(rec);
1941 1942
	}

1943
	if (page_rec_is_infimum(rec)) {
1944

1945
		rec = page_rec_get_next(rec);
1946 1947
	}

1948
	if (page_rec_is_supremum(rec)) {
1949 1950 1951 1952

		return(0);
	}

1953 1954
	first_page_no = ibuf_rec_get_page_no(rec);
	first_space_id = ibuf_rec_get_space(rec);
1955 1956
	n_pages = 0;
	prev_page_no = 0;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1957
	prev_space_id = 0;
1958

1959
	/* Go backwards from the first rec until we reach the border of the
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1960 1961 1962
	'merge area', or the page start or the limit of storeable pages is
	reached */

1963
	while (!page_rec_is_infimum(rec) && UNIV_LIKELY(n_pages < limit)) {
1964 1965

		rec_page_no = ibuf_rec_get_page_no(rec);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1966
		rec_space_id = ibuf_rec_get_space(rec);
1967

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1968
		if (rec_space_id != first_space_id
1969 1970
		    || rec_page_no / IBUF_MERGE_AREA
		    != first_page_no / IBUF_MERGE_AREA) {
1971

1972
			break;
1973
		}
1974

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1975
		if (rec_page_no != prev_page_no
1976
		    || rec_space_id != prev_space_id) {
1977 1978 1979 1980
			n_pages++;
		}

		prev_page_no = rec_page_no;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1981
		prev_space_id = rec_space_id;
1982 1983 1984 1985 1986 1987

		rec = page_rec_get_prev(rec);
	}

	rec = page_rec_get_next(rec);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1988 1989 1990 1991
	/* At the loop start there is no prev page; we mark this with a pair
	of space id, page no (0, 0) for which there can never be entries in
	the insert buffer */

1992
	prev_page_no = 0;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1993
	prev_space_id = 0;
1994 1995
	sum_volumes = 0;
	volume_for_page = 0;
1996

1997
	while (*n_stored < limit) {
1998
		if (page_rec_is_supremum(rec)) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1999 2000
			/* When no more records available, mark this with
			another 'impossible' pair of space id, page no */
2001
			rec_page_no = 1;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2002
			rec_space_id = 0;
2003 2004
		} else {
			rec_page_no = ibuf_rec_get_page_no(rec);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2005
			rec_space_id = ibuf_rec_get_space(rec);
2006 2007 2008 2009 2010 2011
			ut_ad(rec_page_no > IBUF_TREE_ROOT_PAGE_NO);
		}

#ifdef UNIV_IBUF_DEBUG
		ut_a(*n_stored < IBUF_MAX_N_PAGES_MERGED);
#endif
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2012
		if ((rec_space_id != prev_space_id
2013 2014
		     || rec_page_no != prev_page_no)
		    && (prev_space_id != 0 || prev_page_no != 0)) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2015 2016

			if ((prev_page_no == first_page_no
2017 2018 2019 2020 2021 2022 2023
			     && prev_space_id == first_space_id)
			    || contract
			    || (volume_for_page
				> ((IBUF_MERGE_THRESHOLD - 1)
				   * 4 * UNIV_PAGE_SIZE
				   / IBUF_PAGE_SIZE_PER_FREE_SPACE)
				/ IBUF_MERGE_THRESHOLD)) {
2024 2025

				space_ids[*n_stored] = prev_space_id;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2026
				space_versions[*n_stored]
2027
					= fil_space_get_version(prev_space_id);
2028 2029 2030 2031 2032 2033 2034
				page_nos[*n_stored] = prev_page_no;

				(*n_stored)++;

				sum_volumes += volume_for_page;
			}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2035
			if (rec_space_id != first_space_id
2036 2037
			    || rec_page_no / IBUF_MERGE_AREA
			    != first_page_no / IBUF_MERGE_AREA) {
2038

2039
				break;
2040 2041 2042 2043 2044
			}

			volume_for_page = 0;
		}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2045
		if (rec_page_no == 1 && rec_space_id == 0) {
2046 2047 2048 2049 2050 2051 2052 2053
			/* Supremum record */

			break;
		}

		rec_volume = ibuf_rec_get_volume(rec);

		volume_for_page += rec_volume;
2054

2055
		prev_page_no = rec_page_no;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2056
		prev_space_id = rec_space_id;
2057 2058 2059 2060 2061 2062 2063

		rec = page_rec_get_next(rec);
	}

#ifdef UNIV_IBUF_DEBUG
	ut_a(*n_stored <= IBUF_MAX_N_PAGES_MERGED);
#endif
2064 2065 2066 2067
#if 0
	fprintf(stderr, "Ibuf merge batch %lu pages %lu volume\n",
		*n_stored, sum_volumes);
#endif
2068 2069 2070 2071 2072
	return(sum_volumes);
}

/*************************************************************************
Contracts insert buffer trees by reading pages to the buffer pool. */
2073
static
2074
ulint
2075 2076
ibuf_contract_ext(
/*==============*/
2077 2078 2079
			/* out: a lower limit for the combined size in bytes
			of entries which will be merged from ibuf trees to the
			pages read, 0 if ibuf is empty */
2080
	ulint*	n_pages,/* out: number of pages to which merged */
2081 2082 2083 2084 2085 2086 2087 2088 2089 2090
	ibool	sync)	/* in: TRUE if the caller wants to wait for the
			issued read with the highest tablespace address
			to complete */
{
	ulint		rnd_pos;
	ibuf_data_t*	data;
	btr_pcur_t	pcur;
	ulint		space;
	ibool		all_trees_empty;
	ulint		page_nos[IBUF_MAX_N_PAGES_MERGED];
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2091 2092
	ulint		space_ids[IBUF_MAX_N_PAGES_MERGED];
	ib_longlong	space_versions[IBUF_MAX_N_PAGES_MERGED];
2093 2094 2095
	ulint		n_stored;
	ulint		sum_sizes;
	mtr_t		mtr;
2096 2097

	*n_pages = 0;
2098 2099 2100 2101 2102
loop:
	ut_ad(!ibuf_inside());

	mutex_enter(&ibuf_mutex);

2103
	ut_ad(ibuf_validate_low());
2104

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2105 2106
	/* Choose an ibuf tree at random (though there really is only one tree
	in the current implementation) */
2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117
	ibuf_rnd += 865558671;

	rnd_pos = ibuf_rnd % ibuf->size;

	all_trees_empty = TRUE;

	data = UT_LIST_GET_FIRST(ibuf->data_list);

	for (;;) {
		if (!data->empty) {
			all_trees_empty = FALSE;
2118

2119 2120 2121 2122
			if (rnd_pos < data->size) {

				break;
			}
2123

2124 2125
			rnd_pos -= data->size;
		}
2126

2127 2128 2129 2130 2131 2132 2133 2134
		data = UT_LIST_GET_NEXT(data_list, data);

		if (data == NULL) {
			if (all_trees_empty) {
				mutex_exit(&ibuf_mutex);

				return(0);
			}
2135

2136 2137 2138 2139 2140 2141
			data = UT_LIST_GET_FIRST(ibuf->data_list);
		}
	}

	ut_ad(data);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2142
	space = data->index->space;
2143

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2144 2145
	ut_a(space == 0);	/* We currently only have an ibuf tree in
				space 0 */
2146 2147 2148
	mtr_start(&mtr);

	ibuf_enter();
2149

2150 2151 2152 2153 2154
	/* Open a cursor to a randomly chosen leaf of the tree, at a random
	position within the leaf */

	btr_pcur_open_at_rnd_pos(data->index, BTR_SEARCH_LEAF, &pcur, &mtr);

2155
	if (0 == page_get_n_recs(btr_pcur_get_page(&pcur))) {
2156 2157 2158

		/* This tree is empty */

2159 2160 2161
		data->empty = TRUE;

		ibuf_exit();
2162

2163 2164
		mtr_commit(&mtr);
		btr_pcur_close(&pcur);
2165

2166
		mutex_exit(&ibuf_mutex);
2167

2168
		goto loop;
2169
	}
2170

2171 2172 2173
	mutex_exit(&ibuf_mutex);

	sum_sizes = ibuf_get_merge_page_nos(TRUE, btr_pcur_get_rec(&pcur),
2174 2175 2176 2177 2178
					    space_ids, space_versions,
					    page_nos, &n_stored);
#if 0 /* defined UNIV_IBUF_DEBUG */
	fprintf(stderr, "Ibuf contract sync %lu pages %lu volume %lu\n",
		sync, n_stored, sum_sizes);
2179 2180 2181 2182 2183 2184
#endif
	ibuf_exit();

	mtr_commit(&mtr);
	btr_pcur_close(&pcur);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2185
	buf_read_ibuf_merge_pages(sync, space_ids, space_versions, page_nos,
2186
				  n_stored);
2187
	*n_pages = n_stored;
2188

2189 2190 2191
	return(sum_sizes + 1);
}

2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226
/*************************************************************************
Contracts insert buffer trees by reading pages to the buffer pool. */

ulint
ibuf_contract(
/*==========*/
			/* out: a lower limit for the combined size in bytes
			of entries which will be merged from ibuf trees to the
			pages read, 0 if ibuf is empty */
	ibool	sync)	/* in: TRUE if the caller wants to wait for the
			issued read with the highest tablespace address
			to complete */
{
	ulint	n_pages;

	return(ibuf_contract_ext(&n_pages, sync));
}

/*************************************************************************
Contracts insert buffer trees by reading pages to the buffer pool. */

ulint
ibuf_contract_for_n_pages(
/*======================*/
			/* out: a lower limit for the combined size in bytes
			of entries which will be merged from ibuf trees to the
			pages read, 0 if ibuf is empty */
	ibool	sync,	/* in: TRUE if the caller wants to wait for the
			issued read with the highest tablespace address
			to complete */
	ulint	n_pages)/* in: try to read at least this many pages to
			the buffer pool and merge the ibuf contents to
			them */
{
	ulint	sum_bytes	= 0;
2227
	ulint	sum_pages	= 0;
2228 2229
	ulint	n_bytes;
	ulint	n_pag2;
2230

2231 2232
	while (sum_pages < n_pages) {
		n_bytes = ibuf_contract_ext(&n_pag2, sync);
2233

2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244
		if (n_bytes == 0) {
			return(sum_bytes);
		}

		sum_bytes += n_bytes;
		sum_pages += n_pag2;
	}

	return(sum_bytes);
}

2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266
/*************************************************************************
Contract insert buffer trees after insert if they are too big. */
UNIV_INLINE
void
ibuf_contract_after_insert(
/*=======================*/
	ulint	entry_size)	/* in: size of a record which was inserted
				into an ibuf tree */
{
	ibool	sync;
	ulint	sum_sizes;
	ulint	size;

	mutex_enter(&ibuf_mutex);

	if (ibuf->size < ibuf->max_size + IBUF_CONTRACT_ON_INSERT_NON_SYNC) {
		mutex_exit(&ibuf_mutex);

		return;
	}

	sync = FALSE;
2267

2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313
	if (ibuf->size >= ibuf->max_size + IBUF_CONTRACT_ON_INSERT_SYNC) {

		sync = TRUE;
	}

	mutex_exit(&ibuf_mutex);

	/* Contract at least entry_size many bytes */
	sum_sizes = 0;
	size = 1;

	while ((size > 0) && (sum_sizes < entry_size)) {

		size = ibuf_contract(sync);
		sum_sizes += size;
	}
}

/*************************************************************************
Gets an upper limit for the combined size of entries buffered in the insert
buffer for a given page. */

ulint
ibuf_get_volume_buffered(
/*=====================*/
				/* out: upper limit for the volume of
				buffered inserts for the index page, in bytes;
				we may also return UNIV_PAGE_SIZE, if the
				entries for the index page span on several
				pages in the insert buffer */
	btr_pcur_t*	pcur,	/* in: pcur positioned at a place in an
				insert buffer tree where we would insert an
				entry for the index page whose number is
				page_no, latch mode has to be BTR_MODIFY_PREV
				or BTR_MODIFY_TREE */
	ulint		space,	/* in: space id */
	ulint		page_no,/* in: page number of an index page */
	mtr_t*		mtr)	/* in: mtr */
{
	ulint	volume;
	rec_t*	rec;
	page_t*	page;
	ulint	prev_page_no;
	page_t*	prev_page;
	ulint	next_page_no;
	page_t*	next_page;
2314

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2315 2316
	ut_a(trx_sys_multiple_tablespace_format);

2317
	ut_ad((pcur->latch_mode == BTR_MODIFY_PREV)
2318
	      || (pcur->latch_mode == BTR_MODIFY_TREE));
2319 2320 2321 2322 2323

	/* Count the volume of records earlier in the alphabetical order than
	pcur */

	volume = 0;
2324

2325 2326 2327 2328
	rec = btr_pcur_get_rec(pcur);

	page = buf_frame_align(rec);

2329
	if (page_rec_is_supremum(rec)) {
2330 2331 2332 2333
		rec = page_rec_get_prev(rec);
	}

	for (;;) {
2334
		if (page_rec_is_infimum(rec)) {
2335 2336 2337

			break;
		}
2338

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2339
		if (page_no != ibuf_rec_get_page_no(rec)
2340
		    || space != ibuf_rec_get_space(rec)) {
2341 2342 2343 2344 2345 2346 2347 2348 2349 2350

			goto count_later;
		}

		volume += ibuf_rec_get_volume(rec);

		rec = page_rec_get_prev(rec);
	}

	/* Look at the previous page */
2351

2352 2353 2354 2355 2356 2357 2358
	prev_page_no = btr_page_get_prev(page, mtr);

	if (prev_page_no == FIL_NULL) {

		goto count_later;
	}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2359
	prev_page = buf_page_get(0, prev_page_no, RW_X_LATCH, mtr);
2360 2361
#ifdef UNIV_BTR_DEBUG
	ut_a(btr_page_get_next(prev_page, mtr)
2362
	     == buf_frame_get_page_no(page));
2363
#endif /* UNIV_BTR_DEBUG */
2364

2365
#ifdef UNIV_SYNC_DEBUG
2366
	buf_page_dbg_add_level(prev_page, SYNC_TREE_NODE);
2367
#endif /* UNIV_SYNC_DEBUG */
2368 2369 2370

	rec = page_get_supremum_rec(prev_page);
	rec = page_rec_get_prev(rec);
2371

2372
	for (;;) {
2373
		if (page_rec_is_infimum(rec)) {
2374 2375 2376 2377

			/* We cannot go to yet a previous page, because we
			do not have the x-latch on it, and cannot acquire one
			because of the latching order: we have to give up */
2378

2379 2380
			return(UNIV_PAGE_SIZE);
		}
2381

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2382
		if (page_no != ibuf_rec_get_page_no(rec)
2383
		    || space != ibuf_rec_get_space(rec)) {
2384 2385 2386 2387 2388 2389 2390 2391

			goto count_later;
		}

		volume += ibuf_rec_get_volume(rec);

		rec = page_rec_get_prev(rec);
	}
2392

2393 2394 2395
count_later:
	rec = btr_pcur_get_rec(pcur);

2396
	if (!page_rec_is_supremum(rec)) {
2397 2398 2399 2400
		rec = page_rec_get_next(rec);
	}

	for (;;) {
2401
		if (page_rec_is_supremum(rec)) {
2402 2403 2404

			break;
		}
2405

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2406
		if (page_no != ibuf_rec_get_page_no(rec)
2407
		    || space != ibuf_rec_get_space(rec)) {
2408 2409 2410 2411 2412 2413 2414 2415 2416 2417

			return(volume);
		}

		volume += ibuf_rec_get_volume(rec);

		rec = page_rec_get_next(rec);
	}

	/* Look at the next page */
2418

2419 2420 2421 2422 2423 2424 2425
	next_page_no = btr_page_get_next(page, mtr);

	if (next_page_no == FIL_NULL) {

		return(volume);
	}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2426
	next_page = buf_page_get(0, next_page_no, RW_X_LATCH, mtr);
2427 2428
#ifdef UNIV_BTR_DEBUG
	ut_a(btr_page_get_prev(next_page, mtr)
2429
	     == buf_frame_get_page_no(page));
2430
#endif /* UNIV_BTR_DEBUG */
2431

2432
#ifdef UNIV_SYNC_DEBUG
2433
	buf_page_dbg_add_level(next_page, SYNC_TREE_NODE);
2434
#endif /* UNIV_SYNC_DEBUG */
2435 2436 2437 2438 2439

	rec = page_get_infimum_rec(next_page);
	rec = page_rec_get_next(rec);

	for (;;) {
2440
		if (page_rec_is_supremum(rec)) {
2441 2442

			/* We give up */
2443

2444 2445
			return(UNIV_PAGE_SIZE);
		}
2446

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2447
		if (page_no != ibuf_rec_get_page_no(rec)
2448
		    || space != ibuf_rec_get_space(rec)) {
2449 2450 2451 2452 2453 2454 2455 2456 2457 2458

			return(volume);
		}

		volume += ibuf_rec_get_volume(rec);

		rec = page_rec_get_next(rec);
	}
}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478
/*************************************************************************
Reads the biggest tablespace id from the high end of the insert buffer
tree and updates the counter in fil_system. */

void
ibuf_update_max_tablespace_id(void)
/*===============================*/
{
	ulint		max_space_id;
	rec_t*		rec;
	byte*		field;
	ulint		len;
	ibuf_data_t*	ibuf_data;
	dict_index_t*	ibuf_index;
	btr_pcur_t	pcur;
	mtr_t		mtr;

	ibuf_data = fil_space_get_ibuf_data(0);

	ibuf_index = ibuf_data->index;
2479
	ut_a(!dict_table_is_comp(ibuf_index->table));
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2480 2481 2482 2483 2484 2485

	ibuf_enter();

	mtr_start(&mtr);

	btr_pcur_open_at_index_side(FALSE, ibuf_index, BTR_SEARCH_LEAF,
2486
				    &pcur, TRUE, &mtr);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2487 2488 2489 2490 2491 2492 2493 2494 2495
	btr_pcur_move_to_prev(&pcur, &mtr);

	if (btr_pcur_is_before_first_on_page(&pcur, &mtr)) {
		/* The tree is empty */

		max_space_id = 0;
	} else {
		rec = btr_pcur_get_rec(&pcur);

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
2496
		field = rec_get_nth_field_old(rec, 0, &len);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2497 2498

		ut_a(len == 4);
2499

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510
		max_space_id = mach_read_from_4(field);
	}

	mtr_commit(&mtr);
	ibuf_exit();

	/* printf("Maximum space id in insert buffer %lu\n", max_space_id); */

	fil_set_max_space_id_if_bigger(max_space_id);
}

2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526
/*************************************************************************
Makes an index insert to the insert buffer, instead of directly to the disk
page, if this is possible. */
static
ulint
ibuf_insert_low(
/*============*/
				/* out: DB_SUCCESS, DB_FAIL, DB_STRONG_FAIL */
	ulint		mode,	/* in: BTR_MODIFY_PREV or BTR_MODIFY_TREE */
	dtuple_t*	entry,	/* in: index entry to insert */
	dict_index_t*	index,	/* in: index where to insert; must not be
				unique or clustered */
	ulint		space,	/* in: space id where to insert */
	ulint		page_no,/* in: page number where to insert */
	que_thr_t*	thr)	/* in: query thread */
{
2527
	big_rec_t*	dummy_big_rec;
2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541
	ulint		entry_size;
	btr_pcur_t	pcur;
	btr_cur_t*	cursor;
	dtuple_t*	ibuf_entry;
	mem_heap_t*	heap;
	ulint		buffered;
	rec_t*		ins_rec;
	ibool		old_bit_value;
	page_t*		bitmap_page;
	ibuf_data_t*	ibuf_data;
	dict_index_t*	ibuf_index;
	page_t*		root;
	ulint		err;
	ibool		do_merge;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2542 2543
	ulint		space_ids[IBUF_MAX_N_PAGES_MERGED];
	ib_longlong	space_versions[IBUF_MAX_N_PAGES_MERGED];
2544 2545 2546
	ulint		page_nos[IBUF_MAX_N_PAGES_MERGED];
	ulint		n_stored;
	ulint		bits;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2547 2548
	mtr_t		mtr;
	mtr_t		bitmap_mtr;
2549

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2550
	ut_a(!(index->type & DICT_CLUSTERED));
2551 2552
	ut_ad(dtuple_check_typed(entry));

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2553 2554
	ut_a(trx_sys_multiple_tablespace_format);

2555
	do_merge = FALSE;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2556 2557 2558 2559 2560

	/* Currently the insert buffer of space 0 takes care of inserts to all
	tablespaces */

	ibuf_data = fil_space_get_ibuf_data(0);
2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572

	ibuf_index = ibuf_data->index;

	mutex_enter(&ibuf_mutex);

	if (ibuf->size >= ibuf->max_size + IBUF_CONTRACT_DO_NOT_INSERT) {
		/* Insert buffer is now too big, contract it but do not try
		to insert */

		mutex_exit(&ibuf_mutex);

#ifdef UNIV_IBUF_DEBUG
2573
		fputs("Ibuf too big\n", stderr);
2574
#endif
2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586
		/* Use synchronous contract (== TRUE) */
		ibuf_contract(TRUE);

		return(DB_STRONG_FAIL);
	}

	mutex_exit(&ibuf_mutex);

	if (mode == BTR_MODIFY_TREE) {
		mutex_enter(&ibuf_pessimistic_insert_mutex);

		ibuf_enter();
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2587

2588 2589 2590 2591 2592 2593 2594
		mutex_enter(&ibuf_mutex);

		while (!ibuf_data_enough_free_for_insert(ibuf_data)) {

			mutex_exit(&ibuf_mutex);

			ibuf_exit();
2595

2596 2597
			mutex_exit(&ibuf_pessimistic_insert_mutex);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2598
			err = ibuf_add_free_page(0, ibuf_data);
2599 2600 2601 2602 2603 2604 2605 2606 2607

			if (err == DB_STRONG_FAIL) {

				return(err);
			}

			mutex_enter(&ibuf_pessimistic_insert_mutex);

			ibuf_enter();
2608

2609 2610 2611 2612 2613 2614
			mutex_enter(&ibuf_mutex);
		}
	} else {
		ibuf_enter();
	}

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
2615
	entry_size = rec_get_converted_size(index, entry);
2616 2617 2618

	heap = mem_heap_create(512);

2619
	/* Build the entry which contains the space id and the page number as
2620 2621 2622
	the first fields and the type information for other fields, and which
	will be inserted to the insert buffer. */

2623
	ibuf_entry = ibuf_entry_build(index, entry, space, page_no, heap);
2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639

	/* Open a cursor to the insert buffer tree to calculate if we can add
	the new entry to it without exceeding the free space limit for the
	page. */

	mtr_start(&mtr);

	btr_pcur_open(ibuf_index, ibuf_entry, PAGE_CUR_LE, mode, &pcur, &mtr);

	/* Find out the volume of already buffered inserts for the same index
	page */
	buffered = ibuf_get_volume_buffered(&pcur, space, page_no, &mtr);

#ifdef UNIV_IBUF_DEBUG
	ut_a((buffered == 0) || ibuf_count_get(space, page_no));
#endif
2640
	mtr_start(&bitmap_mtr);
2641 2642 2643 2644 2645 2646

	bitmap_page = ibuf_bitmap_get_map_page(space, page_no, &bitmap_mtr);

	/* We check if the index page is suitable for buffered entries */

	if (buf_page_peek(space, page_no)
2647
	    || lock_rec_expl_exist_on_page(space, page_no)) {
2648 2649 2650 2651 2652 2653 2654 2655
		err = DB_STRONG_FAIL;

		mtr_commit(&bitmap_mtr);

		goto function_exit;
	}

	bits = ibuf_bitmap_page_get_bits(bitmap_page, page_no,
2656
					 IBUF_BITMAP_FREE, &bitmap_mtr);
2657 2658

	if (buffered + entry_size + page_dir_calc_reserved_space(1)
2659
	    > ibuf_index_page_calc_free_from_bits(bits)) {
2660 2661
		mtr_commit(&bitmap_mtr);

2662
		/* It may not fit */
2663 2664
		err = DB_STRONG_FAIL;

2665
		do_merge = TRUE;
2666 2667

		ibuf_get_merge_page_nos(FALSE, btr_pcur_get_rec(&pcur),
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
2668 2669
					space_ids, space_versions,
					page_nos, &n_stored);
2670
		goto function_exit;
2671
	}
2672 2673 2674 2675 2676

	/* Set the bitmap bit denoting that the insert buffer contains
	buffered entries for this index page, if the bit is not set yet */

	old_bit_value = ibuf_bitmap_page_get_bits(bitmap_page, page_no,
2677 2678
						  IBUF_BITMAP_BUFFERED,
						  &bitmap_mtr);
2679 2680
	if (!old_bit_value) {
		ibuf_bitmap_page_set_bits(bitmap_page, page_no,
2681 2682
					  IBUF_BITMAP_BUFFERED, TRUE,
					  &bitmap_mtr);
2683 2684 2685
	}

	mtr_commit(&bitmap_mtr);
2686

2687
	cursor = btr_pcur_get_btr_cur(&pcur);
2688

2689 2690
	if (mode == BTR_MODIFY_PREV) {
		err = btr_cur_optimistic_insert(BTR_NO_LOCKING_FLAG, cursor,
2691 2692
						ibuf_entry, &ins_rec,
						&dummy_big_rec, thr,
2693 2694 2695 2696
						&mtr);
		if (err == DB_SUCCESS) {
			/* Update the page max trx id field */
			page_update_max_trx_id(buf_frame_align(ins_rec),
2697
					       thr_get_trx(thr)->id);
2698 2699 2700 2701 2702 2703 2704 2705
		}
	} else {
		ut_ad(mode == BTR_MODIFY_TREE);

		/* We acquire an x-latch to the root page before the insert,
		because a pessimistic insert releases the tree x-latch,
		which would cause the x-latching of the root after that to
		break the latching order. */
2706

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2707
		root = ibuf_tree_root_get(ibuf_data, 0, &mtr);
2708 2709

		err = btr_cur_pessimistic_insert(BTR_NO_LOCKING_FLAG
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2710
						 | BTR_NO_UNDO_LOG_FLAG,
2711 2712 2713 2714
						 cursor,
						 ibuf_entry, &ins_rec,
						 &dummy_big_rec, thr,
						 &mtr);
2715 2716 2717
		if (err == DB_SUCCESS) {
			/* Update the page max trx id field */
			page_update_max_trx_id(buf_frame_align(ins_rec),
2718
					       thr_get_trx(thr)->id);
2719 2720 2721 2722 2723 2724 2725 2726
		}

		ibuf_data_sizes_update(ibuf_data, root, &mtr);
	}

function_exit:
#ifdef UNIV_IBUF_DEBUG
	if (err == DB_SUCCESS) {
2727 2728 2729 2730
		fprintf(stderr,
			"Incrementing ibuf count of space %lu page %lu\n"
			"from %lu by 1\n", space, page_no,
			ibuf_count_get(space, page_no));
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2731

2732
		ibuf_count_set(space, page_no,
2733
			       ibuf_count_get(space, page_no) + 1);
2734 2735
	}
#endif
2736
	if (mode == BTR_MODIFY_TREE) {
2737 2738 2739 2740 2741
		ut_ad(ibuf_validate_low());

		mutex_exit(&ibuf_mutex);
		mutex_exit(&ibuf_pessimistic_insert_mutex);
	}
2742

2743
	mtr_commit(&mtr);
2744
	btr_pcur_close(&pcur);
2745 2746
	ibuf_exit();

2747
	mem_heap_free(heap);
2748 2749 2750 2751 2752 2753 2754

	mutex_enter(&ibuf_mutex);

	if (err == DB_SUCCESS) {
		ibuf_data->empty = FALSE;
		ibuf_data->n_inserts++;
	}
2755

2756 2757
	mutex_exit(&ibuf_mutex);

2758
	if ((mode == BTR_MODIFY_TREE) && (err == DB_SUCCESS)) {
2759 2760
		ibuf_contract_after_insert(entry_size);
	}
2761

2762 2763 2764 2765
	if (do_merge) {
#ifdef UNIV_IBUF_DEBUG
		ut_a(n_stored <= IBUF_MAX_N_PAGES_MERGED);
#endif
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2766
		buf_read_ibuf_merge_pages(FALSE, space_ids, space_versions,
2767
					  page_nos, n_stored);
2768
	}
2769

2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789
	return(err);
}

/*************************************************************************
Makes an index insert to the insert buffer, instead of directly to the disk
page, if this is possible. Does not do insert if the index is clustered
or unique. */

ibool
ibuf_insert(
/*========*/
				/* out: TRUE if success */
	dtuple_t*	entry,	/* in: index entry to insert */
	dict_index_t*	index,	/* in: index where to insert */
	ulint		space,	/* in: space id where to insert */
	ulint		page_no,/* in: page number where to insert */
	que_thr_t*	thr)	/* in: query thread */
{
	ulint	err;

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2790
	ut_a(trx_sys_multiple_tablespace_format);
2791 2792
	ut_ad(dtuple_check_typed(entry));

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2793
	ut_a(!(index->type & DICT_CLUSTERED));
2794

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
2795
	if (rec_get_converted_size(index, entry)
2796 2797
	    >= (page_get_free_space_of_empty(dict_table_is_comp(index->table))
		/ 2)) {
2798 2799
		return(FALSE);
	}
2800

2801
	err = ibuf_insert_low(BTR_MODIFY_PREV, entry, index, space, page_no,
2802
			      thr);
2803 2804
	if (err == DB_FAIL) {
		err = ibuf_insert_low(BTR_MODIFY_TREE, entry, index, space,
2805
				      page_no, thr);
2806
	}
2807

2808 2809
	if (err == DB_SUCCESS) {
#ifdef UNIV_IBUF_DEBUG
2810
		/* fprintf(stderr, "Ibuf insert for page no %lu of index %s\n",
2811
		page_no, index->name); */
2812 2813 2814 2815 2816 2817 2818 2819 2820
#endif
		return(TRUE);

	} else {
		ut_a(err == DB_STRONG_FAIL);

		return(FALSE);
	}
}
2821

2822 2823 2824 2825 2826 2827 2828 2829 2830 2831
/************************************************************************
During merge, inserts to an index page a secondary index entry extracted
from the insert buffer. */
static
void
ibuf_insert_to_index_page(
/*======================*/
	dtuple_t*	entry,	/* in: buffered entry to insert */
	page_t*		page,	/* in: index page where the buffered entry
				should be placed */
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
2832
	dict_index_t*	index,	/* in: record descriptor */
2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843
	mtr_t*		mtr)	/* in: mtr */
{
	page_cur_t	page_cur;
	ulint		low_match;
	rec_t*		rec;
	page_t*		bitmap_page;
	ulint		old_bits;

	ut_ad(ibuf_inside());
	ut_ad(dtuple_check_typed(entry));

2844
	if (UNIV_UNLIKELY(dict_table_is_comp(index->table)
2845 2846 2847 2848 2849
			  != (ibool)!!page_is_comp(page))) {
		fputs("InnoDB: Trying to insert a record from"
		      " the insert buffer to an index page\n"
		      "InnoDB: but the 'compact' flag does not match!\n",
		      stderr);
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
2850 2851 2852 2853
		goto dump;
	}

	rec = page_rec_get_next(page_get_infimum_rec(page));
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2854

2855
	if (UNIV_UNLIKELY(rec_get_n_fields(rec, index)
2856 2857 2858 2859 2860 2861
			  != dtuple_get_n_fields(entry))) {
		fputs("InnoDB: Trying to insert a record from"
		      " the insert buffer to an index page\n"
		      "InnoDB: but the number of fields does not match!\n",
		      stderr);
dump:
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2862 2863
		buf_page_print(page);

2864
		dtuple_print(stderr, entry);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2865

2866 2867 2868 2869 2870 2871 2872
		fputs("InnoDB: The table where where"
		      " this index record belongs\n"
		      "InnoDB: is now probably corrupt."
		      " Please run CHECK TABLE on\n"
		      "InnoDB: your tables.\n"
		      "InnoDB: Submit a detailed bug report to"
		      " http://bugs.mysql.com!\n", stderr);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2873 2874 2875 2876

		return;
	}

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
2877
	low_match = page_cur_search(page, index, entry,
2878
				    PAGE_CUR_LE, &page_cur);
2879

2880 2881
	if (low_match == dtuple_get_n_fields(entry)) {
		rec = page_cur_get_rec(&page_cur);
2882

2883
		btr_cur_del_unmark_for_ibuf(rec, mtr);
2884
	} else {
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
2885
		rec = page_cur_tuple_insert(&page_cur, entry, index, mtr);
2886

2887 2888 2889
		if (rec == NULL) {
			/* If the record did not fit, reorganize */

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
2890
			btr_page_reorganize(page, index, mtr);
2891

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
2892
			page_cur_search(page, index, entry,
2893
					PAGE_CUR_LE, &page_cur);
2894 2895

			/* This time the record must fit */
2896 2897 2898
			if (UNIV_UNLIKELY(!page_cur_tuple_insert(
						  &page_cur, entry, index,
						  mtr))) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2899 2900 2901 2902

				ut_print_timestamp(stderr);

				fprintf(stderr,
2903 2904 2905
					"InnoDB: Error: Insert buffer insert"
					" fails; page free %lu,"
					" dtuple size %lu\n",
2906 2907 2908 2909
					(ulong) page_get_max_insert_size(
						page, 1),
					(ulong) rec_get_converted_size(
						index, entry));
2910
				fputs("InnoDB: Cannot insert index record ",
2911
				      stderr);
2912
				dtuple_print(stderr, entry);
2913 2914 2915 2916 2917 2918
				fputs("\nInnoDB: The table where"
				      " this index record belongs\n"
				      "InnoDB: is now probably corrupt."
				      " Please run CHECK TABLE on\n"
				      "InnoDB: that table.\n", stderr);

2919 2920 2921 2922 2923 2924 2925 2926
				bitmap_page = ibuf_bitmap_get_map_page(
					buf_frame_get_space_id(page),
					buf_frame_get_page_no(page),
					mtr);
				old_bits = ibuf_bitmap_page_get_bits(
					bitmap_page,
					buf_frame_get_page_no(page),
					IBUF_BITMAP_FREE, mtr);
2927 2928 2929 2930 2931 2932

				fprintf(stderr, "Bitmap bits %lu\n",
					(ulong) old_bits);

				fputs("InnoDB: Submit a detailed bug report"
				      " to http://bugs.mysql.com\n", stderr);
2933
			}
2934 2935 2936
		}
	}
}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2937

2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952
/*************************************************************************
Deletes from ibuf the record on which pcur is positioned. If we have to
resort to a pessimistic delete, this function commits mtr and closes
the cursor. */
static
ibool
ibuf_delete_rec(
/*============*/
				/* out: TRUE if mtr was committed and pcur
				closed in this operation */
	ulint		space,	/* in: space id */
	ulint		page_no,/* in: index page number where the record
				should belong */
	btr_pcur_t*	pcur,	/* in: pcur positioned on the record to
				delete, having latch mode BTR_MODIFY_LEAF */
2953 2954
	dtuple_t*	search_tuple,
				/* in: search tuple for entries of page_no */
2955 2956 2957 2958 2959 2960
	mtr_t*		mtr)	/* in: mtr */
{
	ibool		success;
	ibuf_data_t*	ibuf_data;
	page_t*		root;
	ulint		err;
2961

2962 2963
	ut_ad(ibuf_inside());

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2964
	success = btr_cur_optimistic_delete(btr_pcur_get_btr_cur(pcur), mtr);
2965 2966 2967

	if (success) {
#ifdef UNIV_IBUF_DEBUG
2968 2969 2970 2971
		fprintf(stderr,
			"Decrementing ibuf count of space %lu page %lu\n"
			"from %lu by 1\n", space, page_no,
			ibuf_count_get(space, page_no));
2972
		ibuf_count_set(space, page_no,
2973
			       ibuf_count_get(space, page_no) - 1);
2974 2975 2976
#endif
		return(FALSE);
	}
2977

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2978
	/* We have to resort to a pessimistic delete from ibuf */
2979 2980 2981 2982
	btr_pcur_store_position(pcur, mtr);

	btr_pcur_commit_specify_mtr(pcur, mtr);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2983 2984 2985 2986
	/* Currently the insert buffer of space 0 takes care of inserts to all
	tablespaces */

	ibuf_data = fil_space_get_ibuf_data(0);
2987 2988 2989 2990

	mutex_enter(&ibuf_mutex);

	mtr_start(mtr);
2991

2992 2993 2994 2995
	success = btr_pcur_restore_position(BTR_MODIFY_TREE, pcur, mtr);

	if (!success) {
		fprintf(stderr,
2996 2997 2998 2999 3000
			"InnoDB: ERROR: Submit the output to"
			" http://bugs.mysql.com\n"
			"InnoDB: ibuf cursor restoration fails!\n"
			"InnoDB: ibuf record inserted to page %lu\n",
			(ulong) page_no);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3001 3002
		fflush(stderr);

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
3003 3004
		rec_print_old(stderr, btr_pcur_get_rec(pcur));
		rec_print_old(stderr, pcur->old_rec);
3005
		dtuple_print(stderr, search_tuple);
3006

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
3007
		rec_print_old(stderr,
3008
			      page_rec_get_next(btr_pcur_get_rec(pcur)));
3009
		fflush(stderr);
3010

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3011
		btr_pcur_commit_specify_mtr(pcur, mtr);
3012

3013
		fputs("InnoDB: Validating insert buffer tree:\n", stderr);
3014
		if (!btr_validate_index(ibuf_data->index, NULL)) {
3015 3016
			ut_error;
		}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3017 3018 3019

		fprintf(stderr, "InnoDB: ibuf tree ok\n");
		fflush(stderr);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3020 3021 3022 3023 3024 3025

		btr_pcur_close(pcur);

		mutex_exit(&ibuf_mutex);

		return(TRUE);
3026
	}
3027

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3028
	root = ibuf_tree_root_get(ibuf_data, 0, mtr);
3029 3030

	btr_cur_pessimistic_delete(&err, TRUE, btr_pcur_get_btr_cur(pcur),
3031
				   FALSE, mtr);
3032 3033 3034 3035
	ut_a(err == DB_SUCCESS);

#ifdef UNIV_IBUF_DEBUG
	ibuf_count_set(space, page_no, ibuf_count_get(space, page_no) - 1);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3036 3037
#else
	UT_NOT_USED(space);
3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065
#endif
	ibuf_data_sizes_update(ibuf_data, root, mtr);

	ut_ad(ibuf_validate_low());

	btr_pcur_commit_specify_mtr(pcur, mtr);

	btr_pcur_close(pcur);

	mutex_exit(&ibuf_mutex);

	return(TRUE);
}

/*************************************************************************
When an index page is read from a disk to the buffer pool, this function
inserts to the page the possible index entries buffered in the insert buffer.
The entries are deleted from the insert buffer. If the page is not read, but
created in the buffer pool, this function deletes its buffered entries from
the insert buffer; there can exist entries for such a page if the page
belonged to an index which subsequently was dropped. */

void
ibuf_merge_or_delete_for_page(
/*==========================*/
	page_t*	page,	/* in: if page has been read from disk, pointer to
			the page x-latched, else NULL */
	ulint	space,	/* in: space id of the index page */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3066 3067 3068 3069 3070
	ulint	page_no,/* in: page number of the index page */
	ibool	update_ibuf_bitmap)/* in: normally this is set to TRUE, but if
			we have deleted or are deleting the tablespace, then we
			naturally do not want to update a non-existent bitmap
			page */
3071 3072 3073 3074 3075 3076 3077 3078 3079 3080
{
	mem_heap_t*	heap;
	btr_pcur_t	pcur;
	dtuple_t*	entry;
	dtuple_t*	search_tuple;
	rec_t*		ibuf_rec;
	buf_block_t*	block;
	page_t*		bitmap_page;
	ibuf_data_t*	ibuf_data;
	ulint		n_inserts;
3081
#ifdef UNIV_IBUF_DEBUG
3082
	ulint		volume;
3083
#endif
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3084
	ibool		tablespace_being_deleted = FALSE;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3085
	ibool		corruption_noticed	= FALSE;
3086 3087
	mtr_t		mtr;

3088
	if (srv_force_recovery >= SRV_FORCE_NO_IBUF_MERGE) {
3089

3090 3091
		return;
	}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3092

3093
	if (ibuf_fixed_addr_page(space, page_no) || fsp_descr_page(page_no)
3094
	    || trx_sys_hdr_page(space, page_no)) {
3095 3096 3097
		return;
	}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3098 3099 3100 3101 3102
	if (update_ibuf_bitmap) {
		/* If the following returns FALSE, we get the counter
		incremented, and must decrement it when we leave this
		function. When the counter is > 0, that prevents tablespace
		from being dropped. */
3103

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3104 3105 3106 3107 3108 3109 3110 3111 3112 3113
		tablespace_being_deleted = fil_inc_pending_ibuf_merges(space);

		if (tablespace_being_deleted) {
			/* Do not try to read the bitmap page from space;
			just delete the ibuf records for the page */

			page = NULL;
			update_ibuf_bitmap = FALSE;
		}
	}
3114

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3115 3116 3117 3118 3119
	if (update_ibuf_bitmap) {
		mtr_start(&mtr);
		bitmap_page = ibuf_bitmap_get_map_page(space, page_no, &mtr);

		if (!ibuf_bitmap_page_get_bits(bitmap_page, page_no,
3120
					       IBUF_BITMAP_BUFFERED, &mtr)) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3121 3122
			/* No inserts buffered for this page */
			mtr_commit(&mtr);
3123

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3124 3125 3126
			if (!tablespace_being_deleted) {
				fil_decr_pending_ibuf_merges(space);
			}
3127

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3128 3129 3130
			return;
		}
		mtr_commit(&mtr);
3131 3132
	}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3133 3134
	/* Currently the insert buffer of space 0 takes care of inserts to all
	tablespaces */
3135

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3136
	ibuf_data = fil_space_get_ibuf_data(0);
3137 3138 3139 3140 3141

	ibuf_enter();

	heap = mem_heap_create(512);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3142 3143
	if (!trx_sys_multiple_tablespace_format) {
		ut_a(trx_doublewrite_must_reset_space_ids);
3144
		search_tuple = ibuf_search_tuple_build(space, page_no, heap);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3145
	} else {
3146
		search_tuple = ibuf_new_search_tuple_build(space, page_no,
3147
							   heap);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3148
	}
3149

3150 3151 3152 3153 3154 3155 3156 3157
	if (page) {
		/* Move the ownership of the x-latch on the page to this OS
		thread, so that we can acquire a second x-latch on it. This
		is needed for the insert operations to the index page to pass
		the debug checks. */

		block = buf_block_align(page);
		rw_lock_x_lock_move_ownership(&(block->lock));
3158

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3159 3160 3161
		if (fil_page_get_type(page) != FIL_PAGE_INDEX) {

			corruption_noticed = TRUE;
3162

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3163 3164 3165 3166
			ut_print_timestamp(stderr);

			mtr_start(&mtr);

3167
			fputs("  InnoDB: Dump of the ibuf bitmap page:\n",
3168
			      stderr);
3169

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3170
			bitmap_page = ibuf_bitmap_get_map_page(space, page_no,
3171
							       &mtr);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3172
			buf_page_print(bitmap_page);
3173

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3174 3175
			mtr_commit(&mtr);

3176
			fputs("\nInnoDB: Dump of the page:\n", stderr);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3177 3178 3179 3180

			buf_page_print(page);

			fprintf(stderr,
3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194
				"InnoDB: Error: corruption in the tablespace."
				" Bitmap shows insert\n"
				"InnoDB: buffer records to page n:o %lu"
				" though the page\n"
				"InnoDB: type is %lu, which is"
				" not an index page!\n"
				"InnoDB: We try to resolve the problem"
				" by skipping the insert buffer\n"
				"InnoDB: merge for this page."
				" Please run CHECK TABLE on your tables\n"
				"InnoDB: to determine if they are corrupt"
				" after this.\n\n"
				"InnoDB: Please submit a detailed bug report"
				" to http://bugs.mysql.com\n\n",
3195 3196
				(ulong) page_no,
				(ulong) fil_page_get_type(page));
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3197
		}
3198 3199 3200
	}

	n_inserts = 0;
3201
#ifdef UNIV_IBUF_DEBUG
3202
	volume = 0;
3203
#endif
3204 3205 3206 3207
loop:
	mtr_start(&mtr);

	if (page) {
3208
		ibool success = buf_page_get_known_nowait(RW_X_LATCH, page,
3209 3210 3211
							  BUF_KEEP_OLD,
							  __FILE__, __LINE__,
							  &mtr);
3212
		ut_a(success);
3213
#ifdef UNIV_SYNC_DEBUG
3214
		buf_page_dbg_add_level(page, SYNC_TREE_NODE);
3215
#endif /* UNIV_SYNC_DEBUG */
3216
	}
3217

3218 3219 3220
	/* Position pcur in the insert buffer at the first entry for this
	index page */
	btr_pcur_open_on_user_rec(ibuf_data->index, search_tuple, PAGE_CUR_GE,
3221
				  BTR_MODIFY_LEAF, &pcur, &mtr);
3222 3223 3224 3225 3226 3227 3228 3229 3230 3231
	if (!btr_pcur_is_on_user_rec(&pcur, &mtr)) {
		ut_ad(btr_pcur_is_after_last_in_tree(&pcur, &mtr));

		goto reset_bit;
	}

	for (;;) {
		ut_ad(btr_pcur_is_on_user_rec(&pcur, &mtr));

		ibuf_rec = btr_pcur_get_rec(&pcur);
3232

3233
		/* Check if the entry is for this index page */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3234
		if (ibuf_rec_get_page_no(ibuf_rec) != page_no
3235
		    || ibuf_rec_get_space(ibuf_rec) != space) {
3236 3237 3238 3239 3240
			if (page) {
				page_header_reset_last_insert(page, &mtr);
			}
			goto reset_bit;
		}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3241 3242

		if (corruption_noticed) {
3243
			fputs("InnoDB: Discarding record\n ", stderr);
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
3244
			rec_print_old(stderr, ibuf_rec);
3245
			fputs("\n from the insert buffer!\n\n", stderr);
3246
		} else if (page) {
3247 3248 3249 3250 3251
			/* Now we have at pcur a record which should be
			inserted to the index page; NOTE that the call below
			copies pointers to fields in ibuf_rec, and we must
			keep the latch to the ibuf_rec page until the
			insertion is finished! */
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
3252
			dict_index_t*	dummy_index;
3253 3254
			dulint		max_trx_id = page_get_max_trx_id(
				buf_frame_align(ibuf_rec));
3255
			page_update_max_trx_id(page, max_trx_id);
3256

3257 3258
			entry = ibuf_build_entry_from_ibuf_rec(
				ibuf_rec, heap, &dummy_index);
3259
#ifdef UNIV_IBUF_DEBUG
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
3260
			volume += rec_get_converted_size(dummy_index, entry)
3261
				+ page_dir_calc_reserved_space(1);
3262
			ut_a(volume <= 4 * UNIV_PAGE_SIZE
3263
			     / IBUF_PAGE_SIZE_PER_FREE_SPACE);
3264
#endif
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
3265
			ibuf_insert_to_index_page(entry, page,
3266
						  dummy_index, &mtr);
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
3267
			ibuf_dummy_index_free(dummy_index);
3268
		}
3269 3270

		n_inserts++;
3271

3272
		/* Delete the record from ibuf */
3273
		if (ibuf_delete_rec(space, page_no, &pcur, search_tuple,
3274
				    &mtr)) {
3275 3276 3277 3278 3279 3280 3281 3282
			/* Deletion was pessimistic and mtr was committed:
			we start from the beginning again */

			goto loop;
		}

		if (btr_pcur_is_after_last_on_page(&pcur, &mtr)) {
			mtr_commit(&mtr);
3283
			btr_pcur_close(&pcur);
3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295

			goto loop;
		}
	}

reset_bit:
#ifdef UNIV_IBUF_DEBUG
	if (ibuf_count_get(space, page_no) > 0) {
		/* btr_print_tree(ibuf_data->index->tree, 100);
		ibuf_print(); */
	}
#endif
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3296 3297 3298
	if (update_ibuf_bitmap) {
		bitmap_page = ibuf_bitmap_get_map_page(space, page_no, &mtr);
		ibuf_bitmap_page_set_bits(bitmap_page, page_no,
3299 3300
					  IBUF_BITMAP_BUFFERED, FALSE, &mtr);
		if (page) {
3301 3302
			ulint old_bits = ibuf_bitmap_page_get_bits(
				bitmap_page, page_no, IBUF_BITMAP_FREE, &mtr);
3303 3304 3305 3306 3307
			ulint new_bits = ibuf_index_page_calc_free(page);
#if 0 /* defined UNIV_IBUF_DEBUG */
			fprintf(stderr, "Old bits %lu new bits %lu"
				" max size %lu\n",
				old_bits, new_bits,
3308 3309
				page_get_max_insert_size_after_reorganize(
					page, 1));
3310
#endif
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3311 3312
			if (old_bits != new_bits) {
				ibuf_bitmap_page_set_bits(bitmap_page, page_no,
3313 3314
							  IBUF_BITMAP_FREE,
							  new_bits, &mtr);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3315
			}
3316 3317
		}
	}
3318 3319
#if 0 /* defined UNIV_IBUF_DEBUG */
	fprintf(stderr,
3320
		"Ibuf merge %lu records volume %lu to page no %lu\n",
3321
		n_inserts, volume, page_no);
3322 3323
#endif
	mtr_commit(&mtr);
3324
	btr_pcur_close(&pcur);
3325 3326
	mem_heap_free(heap);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3327 3328 3329
	/* Protect our statistics keeping from race conditions */
	mutex_enter(&ibuf_mutex);

3330
	ibuf_data->n_merges++;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3331 3332 3333 3334
	ibuf_data->n_merged_recs += n_inserts;

	mutex_exit(&ibuf_mutex);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3335 3336 3337 3338 3339
	if (update_ibuf_bitmap && !tablespace_being_deleted) {

		fil_decr_pending_ibuf_merges(space);
	}

3340 3341 3342 3343 3344 3345
	ibuf_exit();
#ifdef UNIV_IBUF_DEBUG
	ut_a(ibuf_count_get(space, page_no) == 0);
#endif
}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377
/*************************************************************************
Deletes all entries in the insert buffer for a given space id. This is used
in DISCARD TABLESPACE and IMPORT TABLESPACE.
NOTE: this does not update the page free bitmaps in the space. The space will
become CORRUPT when you call this function! */

void
ibuf_delete_for_discarded_space(
/*============================*/
	ulint	space)	/* in: space id */
{
	mem_heap_t*	heap;
	btr_pcur_t	pcur;
	dtuple_t*	search_tuple;
	rec_t*		ibuf_rec;
	ulint		page_no;
	ibool		closed;
	ibuf_data_t*	ibuf_data;
	ulint		n_inserts;
	mtr_t		mtr;

	/* Currently the insert buffer of space 0 takes care of inserts to all
	tablespaces */

	ibuf_data = fil_space_get_ibuf_data(0);

	heap = mem_heap_create(512);

	/* Use page number 0 to build the search tuple so that we get the
	cursor positioned at the first entry for this space id */

	search_tuple = ibuf_new_search_tuple_build(space, 0, heap);
3378

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3379 3380 3381 3382 3383 3384 3385 3386 3387
	n_inserts = 0;
loop:
	ibuf_enter();

	mtr_start(&mtr);

	/* Position pcur in the insert buffer at the first entry for the
	space */
	btr_pcur_open_on_user_rec(ibuf_data->index, search_tuple, PAGE_CUR_GE,
3388
				  BTR_MODIFY_LEAF, &pcur, &mtr);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408
	if (!btr_pcur_is_on_user_rec(&pcur, &mtr)) {
		ut_ad(btr_pcur_is_after_last_in_tree(&pcur, &mtr));

		goto leave_loop;
	}

	for (;;) {
		ut_ad(btr_pcur_is_on_user_rec(&pcur, &mtr));

		ibuf_rec = btr_pcur_get_rec(&pcur);

		/* Check if the entry is for this space */
		if (ibuf_rec_get_space(ibuf_rec) != space) {

			goto leave_loop;
		}

		page_no = ibuf_rec_get_page_no(ibuf_rec);

		n_inserts++;
3409

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3410 3411
		/* Delete the record from ibuf */
		closed = ibuf_delete_rec(space, page_no, &pcur, search_tuple,
3412
					 &mtr);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423
		if (closed) {
			/* Deletion was pessimistic and mtr was committed:
			we start from the beginning again */

			ibuf_exit();

			goto loop;
		}

		if (btr_pcur_is_after_last_on_page(&pcur, &mtr)) {
			mtr_commit(&mtr);
3424
			btr_pcur_close(&pcur);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3425 3426 3427 3428 3429 3430 3431 3432 3433

			ibuf_exit();

			goto loop;
		}
	}

leave_loop:
	mtr_commit(&mtr);
3434
	btr_pcur_close(&pcur);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3435 3436 3437 3438

	/* Protect our statistics keeping from race conditions */
	mutex_enter(&ibuf_mutex);

3439
	ibuf_data->n_merges++;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3440 3441 3442
	ibuf_data->n_merged_recs += n_inserts;

	mutex_exit(&ibuf_mutex);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3443
	/*
3444
	fprintf(stderr,
3445 3446
	"InnoDB: Discarded %lu ibuf entries for space %lu\n",
	(ulong) n_inserts, (ulong) space);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3447
	*/
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3448 3449 3450 3451 3452
	ibuf_exit();

	mem_heap_free(heap);
}

monty@mysql.com's avatar
monty@mysql.com committed
3453

3454 3455
/**********************************************************************
Validates the ibuf data structures when the caller owns ibuf_mutex. */
3456

3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467
ibool
ibuf_validate_low(void)
/*===================*/
			/* out: TRUE if ok */
{
	ibuf_data_t*	data;
	ulint		sum_sizes;

	ut_ad(mutex_own(&ibuf_mutex));

	sum_sizes = 0;
3468

3469 3470 3471 3472
	data = UT_LIST_GET_FIRST(ibuf->data_list);

	while (data) {
		sum_sizes += data->size;
3473

3474 3475 3476 3477 3478 3479 3480 3481
		data = UT_LIST_GET_NEXT(data_list, data);
	}

	ut_a(sum_sizes == ibuf->size);

	return(TRUE);
}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510
/**********************************************************************
Looks if the insert buffer is empty. */

ibool
ibuf_is_empty(void)
/*===============*/
			/* out: TRUE if empty */
{
	ibuf_data_t*	data;
	ibool		is_empty;
	page_t*		root;
	mtr_t		mtr;

	ibuf_enter();

	mutex_enter(&ibuf_mutex);

	data = UT_LIST_GET_FIRST(ibuf->data_list);

	mtr_start(&mtr);

	root = ibuf_tree_root_get(data, 0, &mtr);

	if (page_get_n_recs(root) == 0) {

		is_empty = TRUE;

		if (data->empty == FALSE) {
			fprintf(stderr,
3511 3512 3513 3514 3515
				"InnoDB: Warning: insert buffer tree is empty"
				" but the data struct does not\n"
				"InnoDB: know it. This condition is legal"
				" if the master thread has not yet\n"
				"InnoDB: run to completion.\n");
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3516 3517
		}
	} else {
3518
		ut_a(data->empty == FALSE);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533

		is_empty = FALSE;
	}

	mtr_commit(&mtr);

	ut_a(data->space == 0);

	mutex_exit(&ibuf_mutex);

	ibuf_exit();

	return(is_empty);
}

3534 3535 3536 3537
/**********************************************************************
Prints info of ibuf. */

void
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3538 3539
ibuf_print(
/*=======*/
3540
	FILE*	file)	/* in: file where to print */
3541 3542 3543 3544 3545
{
	ibuf_data_t*	data;
#ifdef UNIV_IBUF_DEBUG
	ulint		i;
#endif
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3546

3547 3548 3549 3550 3551
	mutex_enter(&ibuf_mutex);

	data = UT_LIST_GET_FIRST(ibuf->data_list);

	while (data) {
3552
		fprintf(file,
3553 3554
			"Ibuf: size %lu, free list len %lu, seg size %lu,\n"
			"%lu inserts, %lu merged recs, %lu merges\n",
3555 3556 3557 3558 3559 3560
			(ulong) data->size,
			(ulong) data->free_list_len,
			(ulong) data->seg_size,
			(ulong) data->n_inserts,
			(ulong) data->n_merged_recs,
			(ulong) data->n_merges);
3561 3562 3563 3564
#ifdef UNIV_IBUF_DEBUG
		for (i = 0; i < IBUF_COUNT_N_PAGES; i++) {
			if (ibuf_count_get(data->space, i) > 0) {

3565 3566
				fprintf(stderr,
					"Ibuf count for page %lu is %lu\n",
3567
					(ulong) i,
3568 3569
					(ulong)
					ibuf_count_get(data->space, i));
3570 3571 3572 3573 3574 3575 3576 3577
			}
		}
#endif
		data = UT_LIST_GET_NEXT(data_list, data);
	}

	mutex_exit(&ibuf_mutex);
}