lock0lock.c 154 KB
Newer Older
vasil's avatar
vasil committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*****************************************************************************

Copyright (c) 1996, 2009, Innobase Oy. All Rights Reserved.

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA

*****************************************************************************/

19 20
/**************************************************//**
@file lock/lock0lock.c
osku's avatar
osku committed
21 22 23 24 25
The transaction lock system

Created 5/7/1996 Heikki Tuuri
*******************************************************/

26 27
#define LOCK_MODULE_IMPLEMENTATION

osku's avatar
osku committed
28
#include "lock0lock.h"
29
#include "lock0priv.h"
osku's avatar
osku committed
30 31 32

#ifdef UNIV_NONINL
#include "lock0lock.ic"
33
#include "lock0priv.ic"
osku's avatar
osku committed
34 35
#endif

vasil's avatar
vasil committed
36
#include "ha_prototypes.h"
osku's avatar
osku committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
#include "usr0sess.h"
#include "trx0purge.h"
#include "dict0mem.h"
#include "trx0sys.h"

/* Restricts the length of search we will do in the waits-for
graph of transactions */
#define LOCK_MAX_N_STEPS_IN_DEADLOCK_CHECK 1000000

/* Restricts the recursion depth of the search we will do in the waits-for
graph of transactions */
#define LOCK_MAX_DEPTH_IN_DEADLOCK_CHECK 200

/* When releasing transaction locks, this specifies how often we release
the kernel mutex for a moment to give also others access to it */

#define LOCK_RELEASE_KERNEL_INTERVAL	1000

/* Safety margin when creating a new record lock: this many extra records
can be inserted to the page without need to create a lock with a bigger
bitmap */

#define LOCK_PAGE_BITMAP_MARGIN		64

/* An explicit record lock affects both the record and the gap before it.
An implicit x-lock does not affect the gap, it only locks the index
63
record from read or update.
osku's avatar
osku committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216

If a transaction has modified or inserted an index record, then
it owns an implicit x-lock on the record. On a secondary index record,
a transaction has an implicit x-lock also if it has modified the
clustered index record, the max trx id of the page where the secondary
index record resides is >= trx id of the transaction (or database recovery
is running), and there are no explicit non-gap lock requests on the
secondary index record.

This complicated definition for a secondary index comes from the
implementation: we want to be able to determine if a secondary index
record has an implicit x-lock, just by looking at the present clustered
index record, not at the historical versions of the record. The
complicated definition can be explained to the user so that there is
nondeterminism in the access path when a query is answered: we may,
or may not, access the clustered index record and thus may, or may not,
bump into an x-lock set there.

Different transaction can have conflicting locks set on the gap at the
same time. The locks on the gap are purely inhibitive: an insert cannot
be made, or a select cursor may have to wait if a different transaction
has a conflicting lock on the gap. An x-lock on the gap does not give
the right to insert into the gap.

An explicit lock can be placed on a user record or the supremum record of
a page. The locks on the supremum record are always thought to be of the gap
type, though the gap bit is not set. When we perform an update of a record
where the size of the record changes, we may temporarily store its explicit
locks on the infimum record of the page, though the infimum otherwise never
carries locks.

A waiting record lock can also be of the gap type. A waiting lock request
can be granted when there is no conflicting mode lock request by another
transaction ahead of it in the explicit lock queue.

In version 4.0.5 we added yet another explicit lock type: LOCK_REC_NOT_GAP.
It only locks the record it is placed on, not the gap before the record.
This lock type is necessary to emulate an Oracle-like READ COMMITTED isolation
level.

-------------------------------------------------------------------------
RULE 1: If there is an implicit x-lock on a record, and there are non-gap
-------
lock requests waiting in the queue, then the transaction holding the implicit
x-lock also has an explicit non-gap record x-lock. Therefore, as locks are
released, we can grant locks to waiting lock requests purely by looking at
the explicit lock requests in the queue.

RULE 3: Different transactions cannot have conflicting granted non-gap locks
-------
on a record at the same time. However, they can have conflicting granted gap
locks.
RULE 4: If a there is a waiting lock request in a queue, no lock request,
-------
gap or not, can be inserted ahead of it in the queue. In record deletes
and page splits new gap type locks can be created by the database manager
for a transaction, and without rule 4, the waits-for graph of transactions
might become cyclic without the database noticing it, as the deadlock check
is only performed when a transaction itself requests a lock!
-------------------------------------------------------------------------

An insert is allowed to a gap if there are no explicit lock requests by
other transactions on the next record. It does not matter if these lock
requests are granted or waiting, gap bit set or not, with the exception
that a gap type request set by another transaction to wait for
its turn to do an insert is ignored. On the other hand, an
implicit x-lock by another transaction does not prevent an insert, which
allows for more concurrency when using an Oracle-style sequence number
generator for the primary key with many transactions doing inserts
concurrently.

A modify of a record is allowed if the transaction has an x-lock on the
record, or if other transactions do not have any non-gap lock requests on the
record.

A read of a single user record with a cursor is allowed if the transaction
has a non-gap explicit, or an implicit lock on the record, or if the other
transactions have no x-lock requests on the record. At a page supremum a
read is always allowed.

In summary, an implicit lock is seen as a granted x-lock only on the
record, not on the gap. An explicit lock with no gap bit set is a lock
both on the record and the gap. If the gap bit is set, the lock is only
on the gap. Different transaction cannot own conflicting locks on the
record at the same time, but they may own conflicting locks on the gap.
Granted locks on a record give an access right to the record, but gap type
locks just inhibit operations.

NOTE: Finding out if some transaction has an implicit x-lock on a secondary
index record can be cumbersome. We may have to look at previous versions of
the corresponding clustered index record to find out if a delete marked
secondary index record was delete marked by an active transaction, not by
a committed one.

FACT A: If a transaction has inserted a row, it can delete it any time
without need to wait for locks.

PROOF: The transaction has an implicit x-lock on every index record inserted
for the row, and can thus modify each record without the need to wait. Q.E.D.

FACT B: If a transaction has read some result set with a cursor, it can read
it again, and retrieves the same result set, if it has not modified the
result set in the meantime. Hence, there is no phantom problem. If the
biggest record, in the alphabetical order, touched by the cursor is removed,
a lock wait may occur, otherwise not.

PROOF: When a read cursor proceeds, it sets an s-lock on each user record
it passes, and a gap type s-lock on each page supremum. The cursor must
wait until it has these locks granted. Then no other transaction can
have a granted x-lock on any of the user records, and therefore cannot
modify the user records. Neither can any other transaction insert into
the gaps which were passed over by the cursor. Page splits and merges,
and removal of obsolete versions of records do not affect this, because
when a user record or a page supremum is removed, the next record inherits
its locks as gap type locks, and therefore blocks inserts to the same gap.
Also, if a page supremum is inserted, it inherits its locks from the successor
record. When the cursor is positioned again at the start of the result set,
the records it will touch on its course are either records it touched
during the last pass or new inserted page supremums. It can immediately
access all these records, and when it arrives at the biggest record, it
notices that the result set is complete. If the biggest record was removed,
lock wait can occur because the next record only inherits a gap type lock,
and a wait may be needed. Q.E.D. */

/* If an index record should be changed or a new inserted, we must check
the lock on the record or the next. When a read cursor starts reading,
we will set a record level s-lock on each record it passes, except on the
initial record on which the cursor is positioned before we start to fetch
records. Our index tree search has the convention that the B-tree
cursor is positioned BEFORE the first possibly matching record in
the search. Optimizations are possible here: if the record is searched
on an equality condition to a unique key, we could actually set a special
lock on the record, a lock which would not prevent any insert before
this record. In the next key locking an x-lock set on a record also
prevents inserts just before that record.
	There are special infimum and supremum records on each page.
A supremum record can be locked by a read cursor. This records cannot be
updated but the lock prevents insert of a user record to the end of
the page.
	Next key locks will prevent the phantom problem where new rows
could appear to SELECT result sets after the select operation has been
performed. Prevention of phantoms ensures the serilizability of
transactions.
	What should we check if an insert of a new record is wanted?
Only the lock on the next record on the same page, because also the
supremum record can carry a lock. An s-lock prevents insertion, but
what about an x-lock? If it was set by a searched update, then there
is implicitly an s-lock, too, and the insert should be prevented.
What if our transaction owns an x-lock to the next record, but there is
a waiting s-lock request on the next record? If this s-lock was placed
by a read cursor moving in the ascending order in the index, we cannot
do the insert immediately, because when we finally commit our transaction,
the read cursor should see also the new inserted record. So we should
217
move the read cursor backward from the next record for it to pass over
osku's avatar
osku committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 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
the new inserted record. This move backward may be too cumbersome to
implement. If we in this situation just enqueue a second x-lock request
for our transaction on the next record, then the deadlock mechanism
notices a deadlock between our transaction and the s-lock request
transaction. This seems to be an ok solution.
	We could have the convention that granted explicit record locks,
lock the corresponding records from changing, and also lock the gaps
before them from inserting. A waiting explicit lock request locks the gap
before from inserting. Implicit record x-locks, which we derive from the
transaction id in the clustered index record, only lock the record itself
from modification, not the gap before it from inserting.
	How should we store update locks? If the search is done by a unique
key, we could just modify the record trx id. Otherwise, we could put a record
x-lock on the record. If the update changes ordering fields of the
clustered index record, the inserted new record needs no record lock in
lock table, the trx id is enough. The same holds for a secondary index
record. Searched delete is similar to update.

PROBLEM:
What about waiting lock requests? If a transaction is waiting to make an
update to a record which another modified, how does the other transaction
know to send the end-lock-wait signal to the waiting transaction? If we have
the convention that a transaction may wait for just one lock at a time, how
do we preserve it if lock wait ends?

PROBLEM:
Checking the trx id label of a secondary index record. In the case of a
modification, not an insert, is this necessary? A secondary index record
is modified only by setting or resetting its deleted flag. A secondary index
record contains fields to uniquely determine the corresponding clustered
index record. A secondary index record is therefore only modified if we
also modify the clustered index record, and the trx id checking is done
on the clustered index record, before we come to modify the secondary index
record. So, in the case of delete marking or unmarking a secondary index
record, we do not have to care about trx ids, only the locks in the lock
table must be checked. In the case of a select from a secondary index, the
trx id is relevant, and in this case we may have to search the clustered
index record.

PROBLEM: How to update record locks when page is split or merged, or
--------------------------------------------------------------------
a record is deleted or updated?
If the size of fields in a record changes, we perform the update by
a delete followed by an insert. How can we retain the locks set or
waiting on the record? Because a record lock is indexed in the bitmap
by the heap number of the record, when we remove the record from the
record list, it is possible still to keep the lock bits. If the page
is reorganized, we could make a table of old and new heap numbers,
and permute the bitmaps in the locks accordingly. We can add to the
table a row telling where the updated record ended. If the update does
not require a reorganization of the page, we can simply move the lock
bits for the updated record to the position determined by its new heap
number (we may have to allocate a new lock, if we run out of the bitmap
in the old one).
	A more complicated case is the one where the reinsertion of the
updated record is done pessimistically, because the structure of the
tree may change.

PROBLEM: If a supremum record is removed in a page merge, or a record
---------------------------------------------------------------------
removed in a purge, what to do to the waiting lock requests? In a split to
the right, we just move the lock requests to the new supremum. If a record
is removed, we could move the waiting lock request to its inheritor, the
next record in the index. But, the next record may already have lock
requests on its own queue. A new deadlock check should be made then. Maybe
it is easier just to release the waiting transactions. They can then enqueue
new lock requests on appropriate records.

PROBLEM: When a record is inserted, what locks should it inherit from the
-------------------------------------------------------------------------
upper neighbor? An insert of a new supremum record in a page split is
always possible, but an insert of a new user record requires that the upper
neighbor does not have any lock requests by other transactions, granted or
waiting, in its lock queue. Solution: We can copy the locks as gap type
locks, so that also the waiting locks are transformed to granted gap type
locks on the inserted record. */

/* LOCK COMPATIBILITY MATRIX
 *    IS IX S  X  AI
297 298 299 300 301
 * IS +	 +  +  -  +
 * IX +	 +  -  -  +
 * S  +	 -  +  -  -
 * X  -	 -  -  -  -
 * AI +	 +  -  -  -
osku's avatar
osku committed
302 303 304 305 306 307 308 309
 *
 * Note that for rows, InnoDB only acquires S or X locks.
 * For tables, InnoDB normally acquires IS or IX locks.
 * S or X table locks are only acquired for LOCK TABLES.
 * Auto-increment (AI) locks are needed because of
 * statement-level MySQL binlog.
 * See also lock_mode_compatible().
 */
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
#define LK(a,b) (1 << ((a) * LOCK_NUM + (b)))
#define LKS(a,b) LK(a,b) | LK(b,a)

/* Define the lock compatibility matrix in a ulint.  The first line below
defines the diagonal entries.  The following lines define the compatibility
for LOCK_IX, LOCK_S, and LOCK_AUTO_INC using LKS(), since the matrix
is symmetric. */
#define LOCK_MODE_COMPATIBILITY 0					\
 | LK(LOCK_IS, LOCK_IS) | LK(LOCK_IX, LOCK_IX) | LK(LOCK_S, LOCK_S)	\
 | LKS(LOCK_IX, LOCK_IS) | LKS(LOCK_IS, LOCK_AUTO_INC)			\
 | LKS(LOCK_S, LOCK_IS)							\
 | LKS(LOCK_AUTO_INC, LOCK_IS) | LKS(LOCK_AUTO_INC, LOCK_IX)

/* STRONGER-OR-EQUAL RELATION (mode1=row, mode2=column)
 *    IS IX S  X  AI
 * IS +  -  -  -  -
 * IX +  +  -  -  -
 * S  +  -  +  -  -
 * X  +  +  +  +  +
 * AI -  -  -  -  +
 * See lock_mode_stronger_or_eq().
 */

/* Define the stronger-or-equal lock relation in a ulint.  This relation
contains all pairs LK(mode1, mode2) where mode1 is stronger than or
equal to mode2. */
#define LOCK_MODE_STRONGER_OR_EQ 0					\
 | LK(LOCK_IS, LOCK_IS)							\
 | LK(LOCK_IX, LOCK_IS) | LK(LOCK_IX, LOCK_IX)				\
 | LK(LOCK_S, LOCK_IS) | LK(LOCK_S, LOCK_S)				\
 | LK(LOCK_AUTO_INC, LOCK_AUTO_INC)					\
 | LK(LOCK_X, LOCK_IS) | LK(LOCK_X, LOCK_IX) | LK(LOCK_X, LOCK_S)	\
 | LK(LOCK_X, LOCK_AUTO_INC) | LK(LOCK_X, LOCK_X)
osku's avatar
osku committed
343 344

#ifdef UNIV_DEBUG
345
UNIV_INTERN ibool	lock_print_waits	= FALSE;
346

347
/*********************************************************************//**
348 349
Validates the lock system.
@return	TRUE if ok */
350 351 352 353
static
ibool
lock_validate(void);
/*===============*/
354

355
/*********************************************************************//**
356 357
Validates the record lock queues on a page.
@return	TRUE if ok */
358 359 360 361
static
ibool
lock_rec_validate_page(
/*===================*/
362
	ulint	space,	/*!< in: space id */
363 364
	ulint	zip_size,/*!< in: compressed page size in bytes
			or 0 for uncompressed pages */
365
	ulint	page_no);/*!< in: page number */
osku's avatar
osku committed
366 367 368
#endif /* UNIV_DEBUG */

/* The lock system */
369
UNIV_INTERN lock_sys_t*	lock_sys	= NULL;
osku's avatar
osku committed
370 371 372

/* We store info on the latest deadlock error to this buffer. InnoDB
Monitor will then fetch it and print */
373 374
UNIV_INTERN ibool	lock_deadlock_found = FALSE;
UNIV_INTERN FILE*	lock_latest_err_file;
osku's avatar
osku committed
375 376 377 378 379

/* Flags for recursive deadlock search */
#define LOCK_VICTIM_IS_START	1
#define LOCK_VICTIM_IS_OTHER	2

380
/********************************************************************//**
381
Checks if a lock request results in a deadlock.
382 383 384
@return TRUE if a deadlock was detected and we chose trx as a victim;
FALSE if no deadlock, or there was a deadlock, but we chose other
transaction(s) as victim(s) */
osku's avatar
osku committed
385 386 387 388
static
ibool
lock_deadlock_occurs(
/*=================*/
389 390
	lock_t*	lock,	/*!< in: lock the transaction is requesting */
	trx_t*	trx);	/*!< in: transaction */
391
/********************************************************************//**
392
Looks recursively for a deadlock.
393 394 395 396 397
@return 0 if no deadlock found, LOCK_VICTIM_IS_START if there was a
deadlock and we chose 'start' as the victim, LOCK_VICTIM_IS_OTHER if a
deadlock was found and we chose some other trx as a victim: we must do
the search again in this last case because there may be another
deadlock! */
osku's avatar
osku committed
398 399 400 401
static
ulint
lock_deadlock_recursive(
/*====================*/
402 403
	trx_t*	start,		/*!< in: recursion starting point */
	trx_t*	trx,		/*!< in: a transaction waiting for a lock */
404
	lock_t*	wait_lock,	/*!< in:  lock that is waiting to be granted */
405
	ulint*	cost,		/*!< in/out: number of calculation steps thus
osku's avatar
osku committed
406 407
				far: if this exceeds LOCK_MAX_N_STEPS_...
				we return LOCK_VICTIM_IS_START */
408
	ulint	depth);		/*!< in: recursion depth: if this exceeds
osku's avatar
osku committed
409 410 411
				LOCK_MAX_DEPTH_IN_DEADLOCK_CHECK, we
				return LOCK_VICTIM_IS_START */

412
/*********************************************************************//**
413
Gets the nth bit of a record lock.
414
@return	TRUE if bit set also if i == ULINT_UNDEFINED return FALSE*/
osku's avatar
osku committed
415 416 417 418
UNIV_INLINE
ibool
lock_rec_get_nth_bit(
/*=================*/
419 420
	const lock_t*	lock,	/*!< in: record lock */
	ulint		i)	/*!< in: index of the bit */
osku's avatar
osku committed
421 422 423 424 425
{
	ulint	byte_index;
	ulint	bit_index;

	ut_ad(lock);
426
	ut_ad(lock_get_type_low(lock) == LOCK_REC);
osku's avatar
osku committed
427 428 429 430 431 432 433 434 435

	if (i >= lock->un_member.rec_lock.n_bits) {

		return(FALSE);
	}

	byte_index = i / 8;
	bit_index = i % 8;

436
	return(1 & ((const byte*) &lock[1])[byte_index] >> bit_index);
437
}
osku's avatar
osku committed
438 439 440 441 442 443

/*************************************************************************/

#define lock_mutex_enter_kernel()	mutex_enter(&kernel_mutex)
#define lock_mutex_exit_kernel()	mutex_exit(&kernel_mutex)

444
/*********************************************************************//**
445 446
Checks that a transaction id is sensible, i.e., not in the future.
@return	TRUE if ok */
447
UNIV_INTERN
osku's avatar
osku committed
448 449 450
ibool
lock_check_trx_id_sanity(
/*=====================*/
451 452 453 454 455
	trx_id_t	trx_id,		/*!< in: trx id */
	const rec_t*	rec,		/*!< in: user record */
	dict_index_t*	index,		/*!< in: index */
	const ulint*	offsets,	/*!< in: rec_get_offsets(rec, index) */
	ibool		has_kernel_mutex)/*!< in: TRUE if the caller owns the
osku's avatar
osku committed
456 457 458
					kernel mutex */
{
	ibool	is_ok		= TRUE;
459

osku's avatar
osku committed
460 461 462 463 464 465 466 467 468 469 470
	ut_ad(rec_offs_validate(rec, index, offsets));

	if (!has_kernel_mutex) {
		mutex_enter(&kernel_mutex);
	}

	/* A sanity check: the trx_id in rec must be smaller than the global
	trx id counter */

	if (ut_dulint_cmp(trx_id, trx_sys->max_trx_id) >= 0) {
		ut_print_timestamp(stderr);
471
		fputs("  InnoDB: Error: transaction id associated"
472 473
		      " with record\n",
		      stderr);
osku's avatar
osku committed
474 475 476 477
		rec_print_new(stderr, rec, offsets);
		fputs("InnoDB: in ", stderr);
		dict_index_name_print(stderr, NULL, index);
		fprintf(stderr, "\n"
vasil's avatar
vasil committed
478 479
			"InnoDB: is " TRX_ID_FMT " which is higher than the"
			" global trx id counter " TRX_ID_FMT "!\n"
480 481
			"InnoDB: The table is corrupt. You have to do"
			" dump + drop + reimport.\n",
vasil's avatar
vasil committed
482 483
			TRX_ID_PREP_PRINTF(trx_id),
			TRX_ID_PREP_PRINTF(trx_sys->max_trx_id));
osku's avatar
osku committed
484 485 486

		is_ok = FALSE;
	}
487

osku's avatar
osku committed
488 489 490 491 492 493 494
	if (!has_kernel_mutex) {
		mutex_exit(&kernel_mutex);
	}

	return(is_ok);
}

495
/*********************************************************************//**
496
Checks that a record is seen in a consistent read.
497 498
@return TRUE if sees, or FALSE if an earlier version of the record
should be retrieved */
499
UNIV_INTERN
osku's avatar
osku committed
500 501 502
ibool
lock_clust_rec_cons_read_sees(
/*==========================*/
503
	const rec_t*	rec,	/*!< in: user record which should be read or
osku's avatar
osku committed
504
				passed over by a read cursor */
505 506 507
	dict_index_t*	index,	/*!< in: clustered index */
	const ulint*	offsets,/*!< in: rec_get_offsets(rec, index) */
	read_view_t*	view)	/*!< in: consistent read view */
osku's avatar
osku committed
508
{
509
	trx_id_t	trx_id;
osku's avatar
osku committed
510

511
	ut_ad(dict_index_is_clust(index));
osku's avatar
osku committed
512 513 514 515 516 517 518 519
	ut_ad(page_rec_is_user_rec(rec));
	ut_ad(rec_offs_validate(rec, index, offsets));

	/* NOTE that we call this function while holding the search
	system latch. To obey the latching order we must NOT reserve the
	kernel mutex here! */

	trx_id = row_get_rec_trx_id(rec, index, offsets);
520

osku's avatar
osku committed
521 522 523
	return(read_view_sees_trx_id(view, trx_id));
}

524
/*********************************************************************//**
525
Checks that a non-clustered index record is seen in a consistent read.
526 527 528 529 530 531 532 533

NOTE that a non-clustered index page contains so little information on
its modifications that also in the case FALSE, the present version of
rec may be the right, but we must check this from the clustered index
record.

@return TRUE if certainly sees, or FALSE if an earlier version of the
clustered index record might be needed */
534
UNIV_INTERN
osku's avatar
osku committed
535 536 537
ulint
lock_sec_rec_cons_read_sees(
/*========================*/
538
	const rec_t*		rec,	/*!< in: user record which
539 540
					should be read or passed over
					by a read cursor */
541
	const read_view_t*	view)	/*!< in: consistent read view */
osku's avatar
osku committed
542
{
543
	trx_id_t	max_trx_id;
544

osku's avatar
osku committed
545 546 547 548 549 550 551 552 553 554 555
	ut_ad(page_rec_is_user_rec(rec));

	/* NOTE that we might call this function while holding the search
	system latch. To obey the latching order we must NOT reserve the
	kernel mutex here! */

	if (recv_recovery_is_on()) {

		return(FALSE);
	}

556
	max_trx_id = page_get_max_trx_id(page_align(rec));
557
	ut_ad(!ut_dulint_is_zero(max_trx_id));
osku's avatar
osku committed
558

559
	return(ut_dulint_cmp(max_trx_id, view->up_limit_id) < 0);
osku's avatar
osku committed
560 561
}

562
/*********************************************************************//**
osku's avatar
osku committed
563
Creates the lock system at database start. */
564
UNIV_INTERN
osku's avatar
osku committed
565 566 567
void
lock_sys_create(
/*============*/
568
	ulint	n_cells)	/*!< in: number of slots in lock hash table */
osku's avatar
osku committed
569 570 571 572 573 574 575 576 577
{
	lock_sys = mem_alloc(sizeof(lock_sys_t));

	lock_sys->rec_hash = hash_create(n_cells);

	/* hash_create_mutexes(lock_sys->rec_hash, 2, SYNC_REC_LOCK); */

	lock_latest_err_file = os_file_create_tmpfile();
	ut_a(lock_latest_err_file);
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
}

/*********************************************************************//**
Closes the lock system at database shutdown. */
UNIV_INTERN
void
lock_sys_close(void)
/*================*/
{
	if (lock_latest_err_file != NULL) {
		fclose(lock_latest_err_file);
		lock_latest_err_file = NULL;
	}

	hash_table_free(lock_sys->rec_hash);
	mem_free(lock_sys);
	lock_sys = NULL;
osku's avatar
osku committed
595 596
}

597
/*********************************************************************//**
598 599
Gets the size of a lock struct.
@return	size in bytes */
600
UNIV_INTERN
osku's avatar
osku committed
601 602 603 604 605 606 607
ulint
lock_get_size(void)
/*===============*/
{
	return((ulint)sizeof(lock_t));
}

608
/*********************************************************************//**
609 610
Gets the mode of a lock.
@return	mode */
osku's avatar
osku committed
611
UNIV_INLINE
612
enum lock_mode
osku's avatar
osku committed
613 614
lock_get_mode(
/*==========*/
615
	const lock_t*	lock)	/*!< in: lock */
osku's avatar
osku committed
616 617 618 619 620 621
{
	ut_ad(lock);

	return(lock->type_mode & LOCK_MODE_MASK);
}

622
/*********************************************************************//**
623 624
Gets the wait flag of a lock.
@return	TRUE if waiting */
osku's avatar
osku committed
625 626 627 628
UNIV_INLINE
ibool
lock_get_wait(
/*==========*/
629
	const lock_t*	lock)	/*!< in: lock */
osku's avatar
osku committed
630 631 632
{
	ut_ad(lock);

633
	if (UNIV_UNLIKELY(lock->type_mode & LOCK_WAIT)) {
osku's avatar
osku committed
634 635 636 637 638 639 640

		return(TRUE);
	}

	return(FALSE);
}

641
/*********************************************************************//**
osku's avatar
osku committed
642
Gets the source table of an ALTER TABLE transaction.  The table must be
643
covered by an IX or IS table lock.
644 645 646 647
@return the source table of transaction, if it is covered by an IX or
IS table lock; dest if there is no source table, and NULL if the
transaction is locking more than two tables or an inconsistency is
found */
648
UNIV_INTERN
osku's avatar
osku committed
649 650 651
dict_table_t*
lock_get_src_table(
/*===============*/
652 653 654
	trx_t*		trx,	/*!< in: transaction */
	dict_table_t*	dest,	/*!< in: destination of ALTER TABLE */
	enum lock_mode*	mode)	/*!< out: lock mode of the source table */
osku's avatar
osku committed
655 656 657 658 659 660 661 662 663 664 665
{
	dict_table_t*	src;
	lock_t*		lock;

	src = NULL;
	*mode = LOCK_NONE;

	for (lock = UT_LIST_GET_FIRST(trx->trx_locks);
	     lock;
	     lock = UT_LIST_GET_NEXT(trx_locks, lock)) {
		lock_table_t*	tab_lock;
666
		enum lock_mode	lock_mode;
667
		if (!(lock_get_type_low(lock) & LOCK_TABLE)) {
osku's avatar
osku committed
668 669 670 671 672 673 674 675 676 677
			/* We are only interested in table locks. */
			continue;
		}
		tab_lock = &lock->un_member.tab_lock;
		if (dest == tab_lock->table) {
			/* We are not interested in the destination table. */
			continue;
		} else if (!src) {
			/* This presumably is the source table. */
			src = tab_lock->table;
678 679
			if (UT_LIST_GET_LEN(src->locks) != 1
			    || UT_LIST_GET_FIRST(src->locks) != lock) {
osku's avatar
osku committed
680 681 682 683 684 685 686 687 688 689 690 691 692
				/* We only support the case when
				there is only one lock on this table. */
				return(NULL);
			}
		} else if (src != tab_lock->table) {
			/* The transaction is locking more than
			two tables (src and dest): abort */
			return(NULL);
		}

		/* Check that the source table is locked by
		LOCK_IX or LOCK_IS. */
		lock_mode = lock_get_mode(lock);
693
		if (lock_mode == LOCK_IX || lock_mode == LOCK_IS) {
osku's avatar
osku committed
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
			if (*mode != LOCK_NONE && *mode != lock_mode) {
				/* There are multiple locks on src. */
				return(NULL);
			}
			*mode = lock_mode;
		}
	}

	if (!src) {
		/* No source table lock found: flag the situation to caller */
		src = dest;
	}

	return(src);
}

710
/*********************************************************************//**
osku's avatar
osku committed
711 712
Determine if the given table is exclusively "owned" by the given
transaction, i.e., transaction holds LOCK_IX and possibly LOCK_AUTO_INC
713
on the table.
714 715
@return TRUE if table is only locked by trx, with LOCK_IX, and
possibly LOCK_AUTO_INC */
716
UNIV_INTERN
osku's avatar
osku committed
717 718 719
ibool
lock_is_table_exclusive(
/*====================*/
720 721
	dict_table_t*	table,	/*!< in: table */
	trx_t*		trx)	/*!< in: transaction */
osku's avatar
osku committed
722
{
723 724
	const lock_t*	lock;
	ibool		ok	= FALSE;
osku's avatar
osku committed
725

726 727 728 729
	ut_ad(table);
	ut_ad(trx);

	lock_mutex_enter_kernel();
osku's avatar
osku committed
730 731 732 733 734 735 736

	for (lock = UT_LIST_GET_FIRST(table->locks);
	     lock;
	     lock = UT_LIST_GET_NEXT(locks, &lock->un_member.tab_lock)) {
		if (lock->trx != trx) {
			/* A lock on the table is held
			by some other transaction. */
737
			goto not_ok;
osku's avatar
osku committed
738 739
		}

740
		if (!(lock_get_type_low(lock) & LOCK_TABLE)) {
osku's avatar
osku committed
741 742 743 744 745 746 747 748 749 750 751 752 753
			/* We are interested in table locks only. */
			continue;
		}

		switch (lock_get_mode(lock)) {
		case LOCK_IX:
			ok = TRUE;
			break;
		case LOCK_AUTO_INC:
			/* It is allowed for trx to hold an
			auto_increment lock. */
			break;
		default:
754
not_ok:
osku's avatar
osku committed
755
			/* Other table locks than LOCK_IX are not allowed. */
756 757
			ok = FALSE;
			goto func_exit;
osku's avatar
osku committed
758 759 760
		}
	}

761 762 763
func_exit:
	lock_mutex_exit_kernel();

osku's avatar
osku committed
764 765 766
	return(ok);
}

767
/*********************************************************************//**
osku's avatar
osku committed
768 769 770 771 772
Sets the wait flag of a lock and the back pointer in trx to lock. */
UNIV_INLINE
void
lock_set_lock_and_trx_wait(
/*=======================*/
773 774
	lock_t*	lock,	/*!< in: lock */
	trx_t*	trx)	/*!< in: trx */
osku's avatar
osku committed
775 776 777
{
	ut_ad(lock);
	ut_ad(trx->wait_lock == NULL);
778

osku's avatar
osku committed
779
	trx->wait_lock = lock;
780
	lock->type_mode |= LOCK_WAIT;
osku's avatar
osku committed
781 782
}

783
/**********************************************************************//**
osku's avatar
osku committed
784 785 786 787 788 789
The back pointer to a waiting lock request in the transaction is set to NULL
and the wait bit in lock type_mode is reset. */
UNIV_INLINE
void
lock_reset_lock_and_trx_wait(
/*=========================*/
790
	lock_t*	lock)	/*!< in: record lock */
osku's avatar
osku committed
791 792 793 794 795 796 797
{
	ut_ad((lock->trx)->wait_lock == lock);
	ut_ad(lock_get_wait(lock));

	/* Reset the back pointer in trx to this waiting lock request */

	(lock->trx)->wait_lock = NULL;
798
	lock->type_mode &= ~LOCK_WAIT;
osku's avatar
osku committed
799 800
}

801
/*********************************************************************//**
802 803
Gets the gap flag of a record lock.
@return	TRUE if gap flag set */
osku's avatar
osku committed
804 805 806 807
UNIV_INLINE
ibool
lock_rec_get_gap(
/*=============*/
808
	const lock_t*	lock)	/*!< in: record lock */
osku's avatar
osku committed
809 810
{
	ut_ad(lock);
811
	ut_ad(lock_get_type_low(lock) == LOCK_REC);
osku's avatar
osku committed
812 813 814 815 816 817 818 819 820

	if (lock->type_mode & LOCK_GAP) {

		return(TRUE);
	}

	return(FALSE);
}

821
/*********************************************************************//**
822 823
Gets the LOCK_REC_NOT_GAP flag of a record lock.
@return	TRUE if LOCK_REC_NOT_GAP flag set */
osku's avatar
osku committed
824 825 826 827
UNIV_INLINE
ibool
lock_rec_get_rec_not_gap(
/*=====================*/
828
	const lock_t*	lock)	/*!< in: record lock */
osku's avatar
osku committed
829 830
{
	ut_ad(lock);
831
	ut_ad(lock_get_type_low(lock) == LOCK_REC);
osku's avatar
osku committed
832 833 834 835 836 837 838 839 840

	if (lock->type_mode & LOCK_REC_NOT_GAP) {

		return(TRUE);
	}

	return(FALSE);
}

841
/*********************************************************************//**
842 843
Gets the waiting insert flag of a record lock.
@return	TRUE if gap flag set */
osku's avatar
osku committed
844 845 846 847
UNIV_INLINE
ibool
lock_rec_get_insert_intention(
/*==========================*/
848
	const lock_t*	lock)	/*!< in: record lock */
osku's avatar
osku committed
849 850
{
	ut_ad(lock);
851
	ut_ad(lock_get_type_low(lock) == LOCK_REC);
osku's avatar
osku committed
852 853 854 855 856 857 858 859 860

	if (lock->type_mode & LOCK_INSERT_INTENTION) {

		return(TRUE);
	}

	return(FALSE);
}

861
/*********************************************************************//**
862 863
Calculates if lock mode 1 is stronger or equal to lock mode 2.
@return	nonzero if mode1 stronger or equal to mode2 */
osku's avatar
osku committed
864
UNIV_INLINE
865
ulint
osku's avatar
osku committed
866 867
lock_mode_stronger_or_eq(
/*=====================*/
868 869
	enum lock_mode	mode1,	/*!< in: lock mode */
	enum lock_mode	mode2)	/*!< in: lock mode */
osku's avatar
osku committed
870 871
{
	ut_ad(mode1 == LOCK_X || mode1 == LOCK_S || mode1 == LOCK_IX
872
	      || mode1 == LOCK_IS || mode1 == LOCK_AUTO_INC);
osku's avatar
osku committed
873
	ut_ad(mode2 == LOCK_X || mode2 == LOCK_S || mode2 == LOCK_IX
874
	      || mode2 == LOCK_IS || mode2 == LOCK_AUTO_INC);
osku's avatar
osku committed
875

876
	return((LOCK_MODE_STRONGER_OR_EQ) & LK(mode1, mode2));
osku's avatar
osku committed
877 878
}

879
/*********************************************************************//**
880 881
Calculates if lock mode 1 is compatible with lock mode 2.
@return	nonzero if mode1 compatible with mode2 */
osku's avatar
osku committed
882
UNIV_INLINE
883
ulint
osku's avatar
osku committed
884 885
lock_mode_compatible(
/*=================*/
886 887
	enum lock_mode	mode1,	/*!< in: lock mode */
	enum lock_mode	mode2)	/*!< in: lock mode */
osku's avatar
osku committed
888 889
{
	ut_ad(mode1 == LOCK_X || mode1 == LOCK_S || mode1 == LOCK_IX
890
	      || mode1 == LOCK_IS || mode1 == LOCK_AUTO_INC);
osku's avatar
osku committed
891
	ut_ad(mode2 == LOCK_X || mode2 == LOCK_S || mode2 == LOCK_IX
892
	      || mode2 == LOCK_IS || mode2 == LOCK_AUTO_INC);
osku's avatar
osku committed
893

894
	return((LOCK_MODE_COMPATIBILITY) & LK(mode1, mode2));
osku's avatar
osku committed
895 896
}

897
/*********************************************************************//**
898 899
Checks if a lock request for a new lock has to wait for request lock2.
@return	TRUE if new lock has to wait for lock2 to be removed */
osku's avatar
osku committed
900 901 902 903
UNIV_INLINE
ibool
lock_rec_has_to_wait(
/*=================*/
904 905
	const trx_t*	trx,	/*!< in: trx of new lock */
	ulint		type_mode,/*!< in: precise mode of the new lock
906 907 908
				to set: LOCK_S or LOCK_X, possibly
				ORed to LOCK_GAP or LOCK_REC_NOT_GAP,
				LOCK_INSERT_INTENTION */
909
	const lock_t*	lock2,	/*!< in: another record lock; NOTE that
910 911 912
				it is assumed that this has a lock bit
				set on the same record as in the new
				lock we are setting */
913
	ibool lock_is_on_supremum)  /*!< in: TRUE if we are setting the
914 915 916
				lock on the 'supremum' record of an
				index page: we know then that the lock
				request is really for a 'gap' type lock */
osku's avatar
osku committed
917 918
{
	ut_ad(trx && lock2);
919
	ut_ad(lock_get_type_low(lock2) == LOCK_REC);
osku's avatar
osku committed
920 921

	if (trx != lock2->trx
922 923
	    && !lock_mode_compatible(LOCK_MODE_MASK & type_mode,
				     lock_get_mode(lock2))) {
osku's avatar
osku committed
924 925 926 927 928

		/* We have somewhat complex rules when gap type record locks
		cause waits */

		if ((lock_is_on_supremum || (type_mode & LOCK_GAP))
929
		    && !(type_mode & LOCK_INSERT_INTENTION)) {
osku's avatar
osku committed
930 931

			/* Gap type locks without LOCK_INSERT_INTENTION flag
932 933
			do not need to wait for anything. This is because
			different users can have conflicting lock types
osku's avatar
osku committed
934
			on gaps. */
935

osku's avatar
osku committed
936 937
			return(FALSE);
		}
938

osku's avatar
osku committed
939
		if (!(type_mode & LOCK_INSERT_INTENTION)
940
		    && lock_rec_get_gap(lock2)) {
osku's avatar
osku committed
941 942 943 944 945 946 947 948

			/* Record lock (LOCK_ORDINARY or LOCK_REC_NOT_GAP
			does not need to wait for a gap type lock */

			return(FALSE);
		}

		if ((type_mode & LOCK_GAP)
949
		    && lock_rec_get_rec_not_gap(lock2)) {
950

osku's avatar
osku committed
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
			/* Lock on gap does not need to wait for
			a LOCK_REC_NOT_GAP type lock */

			return(FALSE);
		}

		if (lock_rec_get_insert_intention(lock2)) {

			/* No lock request needs to wait for an insert
			intention lock to be removed. This is ok since our
			rules allow conflicting locks on gaps. This eliminates
			a spurious deadlock caused by a next-key lock waiting
			for an insert intention lock; when the insert
			intention lock was granted, the insert deadlocked on
			the waiting next-key lock.

			Also, insert intention locks do not disturb each
			other. */
969

osku's avatar
osku committed
970 971 972 973 974 975 976 977 978
			return(FALSE);
		}

		return(TRUE);
	}

	return(FALSE);
}

979
/*********************************************************************//**
980 981
Checks if a lock request lock1 has to wait for request lock2.
@return	TRUE if lock1 has to wait for lock2 to be removed */
982
UNIV_INTERN
osku's avatar
osku committed
983 984 985
ibool
lock_has_to_wait(
/*=============*/
986 987
	const lock_t*	lock1,	/*!< in: waiting lock */
	const lock_t*	lock2)	/*!< in: another lock; NOTE that it is
988 989 990
				assumed that this has a lock bit set
				on the same record as in lock1 if the
				locks are record locks */
osku's avatar
osku committed
991 992 993 994
{
	ut_ad(lock1 && lock2);

	if (lock1->trx != lock2->trx
995 996
	    && !lock_mode_compatible(lock_get_mode(lock1),
				     lock_get_mode(lock2))) {
997 998
		if (lock_get_type_low(lock1) == LOCK_REC) {
			ut_ad(lock_get_type_low(lock2) == LOCK_REC);
osku's avatar
osku committed
999 1000 1001

			/* If this lock request is for a supremum record
			then the second bit on the lock bitmap is set */
1002

osku's avatar
osku committed
1003
			return(lock_rec_has_to_wait(lock1->trx,
1004
						    lock1->type_mode, lock2,
1005 1006
						    lock_rec_get_nth_bit(
							    lock1, 1)));
osku's avatar
osku committed
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
		}

		return(TRUE);
	}

	return(FALSE);
}

/*============== RECORD LOCK BASIC FUNCTIONS ============================*/

1017
/*********************************************************************//**
1018 1019
Gets the number of bits in a record lock bitmap.
@return	number of bits */
osku's avatar
osku committed
1020 1021 1022 1023
UNIV_INLINE
ulint
lock_rec_get_n_bits(
/*================*/
1024
	const lock_t*	lock)	/*!< in: record lock */
osku's avatar
osku committed
1025 1026 1027 1028
{
	return(lock->un_member.rec_lock.n_bits);
}

1029
/**********************************************************************//**
osku's avatar
osku committed
1030 1031 1032 1033
Sets the nth bit of a record lock to TRUE. */
UNIV_INLINE
void
lock_rec_set_nth_bit(
1034
/*=================*/
1035 1036
	lock_t*	lock,	/*!< in: record lock */
	ulint	i)	/*!< in: index of the bit */
osku's avatar
osku committed
1037 1038 1039
{
	ulint	byte_index;
	ulint	bit_index;
1040

osku's avatar
osku committed
1041
	ut_ad(lock);
1042
	ut_ad(lock_get_type_low(lock) == LOCK_REC);
osku's avatar
osku committed
1043
	ut_ad(i < lock->un_member.rec_lock.n_bits);
1044

osku's avatar
osku committed
1045 1046 1047
	byte_index = i / 8;
	bit_index = i % 8;

1048
	((byte*) &lock[1])[byte_index] |= 1 << bit_index;
1049
}
osku's avatar
osku committed
1050

1051
/**********************************************************************//**
osku's avatar
osku committed
1052
Looks for a set bit in a record lock bitmap. Returns ULINT_UNDEFINED,
1053
if none found.
1054 1055
@return bit index == heap number of the record, or ULINT_UNDEFINED if
none found */
1056
UNIV_INTERN
osku's avatar
osku committed
1057 1058 1059
ulint
lock_rec_find_set_bit(
/*==================*/
1060
	const lock_t*	lock)	/*!< in: record lock with at least one bit set */
osku's avatar
osku committed
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
{
	ulint	i;

	for (i = 0; i < lock_rec_get_n_bits(lock); i++) {

		if (lock_rec_get_nth_bit(lock, i)) {

			return(i);
		}
	}

	return(ULINT_UNDEFINED);
}

1075
/**********************************************************************//**
osku's avatar
osku committed
1076 1077 1078 1079 1080
Resets the nth bit of a record lock. */
UNIV_INLINE
void
lock_rec_reset_nth_bit(
/*===================*/
1081 1082
	lock_t*	lock,	/*!< in: record lock */
	ulint	i)	/*!< in: index of the bit which must be set to TRUE
osku's avatar
osku committed
1083 1084 1085 1086
			when this function is called */
{
	ulint	byte_index;
	ulint	bit_index;
1087

osku's avatar
osku committed
1088
	ut_ad(lock);
1089
	ut_ad(lock_get_type_low(lock) == LOCK_REC);
osku's avatar
osku committed
1090
	ut_ad(i < lock->un_member.rec_lock.n_bits);
1091

osku's avatar
osku committed
1092 1093 1094
	byte_index = i / 8;
	bit_index = i % 8;

1095
	((byte*) &lock[1])[byte_index] &= ~(1 << bit_index);
1096
}
osku's avatar
osku committed
1097

1098
/*********************************************************************//**
1099 1100
Gets the first or next record lock on a page.
@return	next lock, NULL if none exists */
osku's avatar
osku committed
1101 1102 1103 1104
UNIV_INLINE
lock_t*
lock_rec_get_next_on_page(
/*======================*/
1105
	lock_t*	lock)	/*!< in: a record lock */
osku's avatar
osku committed
1106 1107 1108 1109 1110
{
	ulint	space;
	ulint	page_no;

	ut_ad(mutex_own(&kernel_mutex));
1111
	ut_ad(lock_get_type_low(lock) == LOCK_REC);
osku's avatar
osku committed
1112 1113 1114

	space = lock->un_member.rec_lock.space;
	page_no = lock->un_member.rec_lock.page_no;
1115

osku's avatar
osku committed
1116 1117 1118 1119 1120 1121 1122 1123
	for (;;) {
		lock = HASH_GET_NEXT(hash, lock);

		if (!lock) {

			break;
		}

1124
		if ((lock->un_member.rec_lock.space == space)
1125
		    && (lock->un_member.rec_lock.page_no == page_no)) {
osku's avatar
osku committed
1126 1127 1128 1129

			break;
		}
	}
1130

osku's avatar
osku committed
1131 1132 1133
	return(lock);
}

1134
/*********************************************************************//**
osku's avatar
osku committed
1135
Gets the first record lock on a page, where the page is identified by its
1136 1137
file address.
@return	first lock, NULL if none exists */
osku's avatar
osku committed
1138 1139 1140 1141
UNIV_INLINE
lock_t*
lock_rec_get_first_on_page_addr(
/*============================*/
1142 1143
	ulint	space,	/*!< in: space */
	ulint	page_no)/*!< in: page number */
osku's avatar
osku committed
1144 1145 1146 1147 1148 1149
{
	lock_t*	lock;

	ut_ad(mutex_own(&kernel_mutex));

	lock = HASH_GET_FIRST(lock_sys->rec_hash,
1150
			      lock_rec_hash(space, page_no));
osku's avatar
osku committed
1151
	while (lock) {
1152
		if ((lock->un_member.rec_lock.space == space)
1153
		    && (lock->un_member.rec_lock.page_no == page_no)) {
osku's avatar
osku committed
1154 1155 1156 1157 1158 1159 1160 1161 1162

			break;
		}

		lock = HASH_GET_NEXT(hash, lock);
	}

	return(lock);
}
1163

1164
/*********************************************************************//**
1165 1166
Returns TRUE if there are explicit record locks on a page.
@return	TRUE if there are explicit record locks on the page */
1167
UNIV_INTERN
osku's avatar
osku committed
1168 1169 1170
ibool
lock_rec_expl_exist_on_page(
/*========================*/
1171 1172
	ulint	space,	/*!< in: space id */
	ulint	page_no)/*!< in: page number */
osku's avatar
osku committed
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
{
	ibool	ret;

	mutex_enter(&kernel_mutex);

	if (lock_rec_get_first_on_page_addr(space, page_no)) {
		ret = TRUE;
	} else {
		ret = FALSE;
	}

	mutex_exit(&kernel_mutex);
1185

osku's avatar
osku committed
1186 1187 1188
	return(ret);
}

1189
/*********************************************************************//**
osku's avatar
osku committed
1190
Gets the first record lock on a page, where the page is identified by a
1191 1192
pointer to it.
@return	first lock, NULL if none exists */
osku's avatar
osku committed
1193 1194 1195 1196
UNIV_INLINE
lock_t*
lock_rec_get_first_on_page(
/*=======================*/
1197
	const buf_block_t*	block)	/*!< in: buffer block */
osku's avatar
osku committed
1198 1199 1200
{
	ulint	hash;
	lock_t*	lock;
1201 1202
	ulint	space	= buf_block_get_space(block);
	ulint	page_no	= buf_block_get_page_no(block);
osku's avatar
osku committed
1203 1204

	ut_ad(mutex_own(&kernel_mutex));
1205

1206
	hash = buf_block_get_lock_hash_val(block);
osku's avatar
osku committed
1207 1208 1209 1210

	lock = HASH_GET_FIRST(lock_sys->rec_hash, hash);

	while (lock) {
1211
		if ((lock->un_member.rec_lock.space == space)
1212
		    && (lock->un_member.rec_lock.page_no == page_no)) {
osku's avatar
osku committed
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222

			break;
		}

		lock = HASH_GET_NEXT(hash, lock);
	}

	return(lock);
}

1223
/*********************************************************************//**
1224
Gets the next explicit lock request on a record.
1225
@return	next lock, NULL if none exists or if heap_no == ULINT_UNDEFINED */
osku's avatar
osku committed
1226 1227 1228 1229
UNIV_INLINE
lock_t*
lock_rec_get_next(
/*==============*/
1230 1231
	ulint	heap_no,/*!< in: heap number of the record */
	lock_t*	lock)	/*!< in: lock */
osku's avatar
osku committed
1232 1233 1234
{
	ut_ad(mutex_own(&kernel_mutex));

marko's avatar
marko committed
1235
	do {
1236
		ut_ad(lock_get_type_low(lock) == LOCK_REC);
marko's avatar
marko committed
1237 1238
		lock = lock_rec_get_next_on_page(lock);
	} while (lock && !lock_rec_get_nth_bit(lock, heap_no));
osku's avatar
osku committed
1239 1240 1241 1242

	return(lock);
}

1243
/*********************************************************************//**
1244 1245
Gets the first explicit lock request on a record.
@return	first lock, NULL if none exists */
osku's avatar
osku committed
1246 1247 1248 1249
UNIV_INLINE
lock_t*
lock_rec_get_first(
/*===============*/
1250 1251
	const buf_block_t*	block,	/*!< in: block containing the record */
	ulint			heap_no)/*!< in: heap number of the record */
osku's avatar
osku committed
1252 1253 1254 1255 1256
{
	lock_t*	lock;

	ut_ad(mutex_own(&kernel_mutex));

1257 1258 1259 1260 1261
	for (lock = lock_rec_get_first_on_page(block); lock;
	     lock = lock_rec_get_next_on_page(lock)) {
		if (lock_rec_get_nth_bit(lock, heap_no)) {
			break;
		}
osku's avatar
osku committed
1262 1263 1264 1265 1266
	}

	return(lock);
}

1267
/*********************************************************************//**
osku's avatar
osku committed
1268 1269 1270 1271 1272 1273 1274
Resets the record lock bitmap to zero. NOTE: does not touch the wait_lock
pointer in the transaction! This function is used in lock object creation
and resetting. */
static
void
lock_rec_bitmap_reset(
/*==================*/
1275
	lock_t*	lock)	/*!< in: record lock */
osku's avatar
osku committed
1276 1277 1278
{
	ulint	n_bytes;

1279
	ut_ad(lock_get_type_low(lock) == LOCK_REC);
osku's avatar
osku committed
1280 1281 1282 1283 1284 1285 1286

	/* Reset to zero the bitmap which resides immediately after the lock
	struct */

	n_bytes = lock_rec_get_n_bits(lock) / 8;

	ut_ad((lock_rec_get_n_bits(lock) % 8) == 0);
1287

1288
	memset(&lock[1], 0, n_bytes);
osku's avatar
osku committed
1289 1290
}

1291
/*********************************************************************//**
1292 1293
Copies a record lock to heap.
@return	copy of lock */
osku's avatar
osku committed
1294 1295 1296 1297
static
lock_t*
lock_rec_copy(
/*==========*/
1298 1299
	const lock_t*	lock,	/*!< in: record lock */
	mem_heap_t*	heap)	/*!< in: memory heap */
osku's avatar
osku committed
1300 1301 1302
{
	ulint	size;

1303
	ut_ad(lock_get_type_low(lock) == LOCK_REC);
osku's avatar
osku committed
1304

1305
	size = sizeof(lock_t) + lock_rec_get_n_bits(lock) / 8;
osku's avatar
osku committed
1306

1307
	return(mem_heap_dup(heap, lock, size));
osku's avatar
osku committed
1308 1309
}

1310
/*********************************************************************//**
1311 1312
Gets the previous record lock set on a record.
@return	previous lock on the same record, NULL if none exists */
1313
UNIV_INTERN
1314
const lock_t*
osku's avatar
osku committed
1315 1316
lock_rec_get_prev(
/*==============*/
1317 1318
	const lock_t*	in_lock,/*!< in: record lock */
	ulint		heap_no)/*!< in: heap number of the record */
osku's avatar
osku committed
1319
{
1320 1321 1322 1323
	lock_t*	lock;
	ulint	space;
	ulint	page_no;
	lock_t*	found_lock	= NULL;
osku's avatar
osku committed
1324 1325

	ut_ad(mutex_own(&kernel_mutex));
1326
	ut_ad(lock_get_type_low(in_lock) == LOCK_REC);
osku's avatar
osku committed
1327 1328 1329 1330 1331 1332 1333 1334

	space = in_lock->un_member.rec_lock.space;
	page_no = in_lock->un_member.rec_lock.page_no;

	lock = lock_rec_get_first_on_page_addr(space, page_no);

	for (;;) {
		ut_ad(lock);
1335

osku's avatar
osku committed
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345
		if (lock == in_lock) {

			return(found_lock);
		}

		if (lock_rec_get_nth_bit(lock, heap_no)) {

			found_lock = lock;
		}

1346
		lock = lock_rec_get_next_on_page(lock);
1347
	}
osku's avatar
osku committed
1348 1349 1350 1351
}

/*============= FUNCTIONS FOR ANALYZING TABLE LOCK QUEUE ================*/

1352
/*********************************************************************//**
1353 1354
Checks if a transaction has the specified table lock, or stronger.
@return	lock or NULL */
osku's avatar
osku committed
1355 1356 1357 1358
UNIV_INLINE
lock_t*
lock_table_has(
/*===========*/
1359 1360 1361
	trx_t*		trx,	/*!< in: transaction */
	dict_table_t*	table,	/*!< in: table */
	enum lock_mode	mode)	/*!< in: lock mode */
osku's avatar
osku committed
1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
{
	lock_t*	lock;

	ut_ad(mutex_own(&kernel_mutex));

	/* Look for stronger locks the same trx already has on the table */

	lock = UT_LIST_GET_LAST(table->locks);

	while (lock != NULL) {

		if (lock->trx == trx
1374
		    && lock_mode_stronger_or_eq(lock_get_mode(lock), mode)) {
osku's avatar
osku committed
1375

1376
			/* The same trx already has locked the table in
osku's avatar
osku committed
1377 1378
			a mode stronger or equal to the mode given */

1379
			ut_ad(!lock_get_wait(lock));
osku's avatar
osku committed
1380 1381 1382 1383 1384 1385 1386 1387 1388

			return(lock);
		}

		lock = UT_LIST_GET_PREV(un_member.tab_lock.locks, lock);
	}

	return(NULL);
}
1389

osku's avatar
osku committed
1390 1391
/*============= FUNCTIONS FOR ANALYZING RECORD LOCK QUEUE ================*/

1392
/*********************************************************************//**
osku's avatar
osku committed
1393
Checks if a transaction has a GRANTED explicit lock on rec stronger or equal
1394 1395
to precise_mode.
@return	lock or NULL */
osku's avatar
osku committed
1396 1397 1398 1399
UNIV_INLINE
lock_t*
lock_rec_has_expl(
/*==============*/
1400
	ulint			precise_mode,/*!< in: LOCK_S or LOCK_X
1401 1402 1403 1404
					possibly ORed to LOCK_GAP or
					LOCK_REC_NOT_GAP, for a
					supremum record we regard this
					always a gap type request */
1405
	const buf_block_t*	block,	/*!< in: buffer block containing
1406
					the record */
1407 1408
	ulint			heap_no,/*!< in: heap number of the record */
	trx_t*			trx)	/*!< in: transaction */
osku's avatar
osku committed
1409 1410 1411 1412 1413
{
	lock_t*	lock;

	ut_ad(mutex_own(&kernel_mutex));
	ut_ad((precise_mode & LOCK_MODE_MASK) == LOCK_S
1414
	      || (precise_mode & LOCK_MODE_MASK) == LOCK_X);
osku's avatar
osku committed
1415
	ut_ad(!(precise_mode & LOCK_INSERT_INTENTION));
1416

1417
	lock = lock_rec_get_first(block, heap_no);
osku's avatar
osku committed
1418 1419 1420

	while (lock) {
		if (lock->trx == trx
1421 1422 1423 1424 1425
		    && lock_mode_stronger_or_eq(lock_get_mode(lock),
						precise_mode & LOCK_MODE_MASK)
		    && !lock_get_wait(lock)
		    && (!lock_rec_get_rec_not_gap(lock)
			|| (precise_mode & LOCK_REC_NOT_GAP)
1426
			|| heap_no == PAGE_HEAP_NO_SUPREMUM)
1427 1428
		    && (!lock_rec_get_gap(lock)
			|| (precise_mode & LOCK_GAP)
1429
			|| heap_no == PAGE_HEAP_NO_SUPREMUM)
1430
		    && (!lock_rec_get_insert_intention(lock))) {
osku's avatar
osku committed
1431

1432
			return(lock);
osku's avatar
osku committed
1433 1434
		}

marko's avatar
marko committed
1435
		lock = lock_rec_get_next(heap_no, lock);
osku's avatar
osku committed
1436 1437 1438 1439
	}

	return(NULL);
}
1440

1441
#ifdef UNIV_DEBUG
1442
/*********************************************************************//**
1443 1444
Checks if some other transaction has a lock request in the queue.
@return	lock or NULL */
osku's avatar
osku committed
1445 1446 1447 1448
static
lock_t*
lock_rec_other_has_expl_req(
/*========================*/
1449 1450
	enum lock_mode		mode,	/*!< in: LOCK_S or LOCK_X */
	ulint			gap,	/*!< in: LOCK_GAP if also gap
1451 1452
					locks are taken into account,
					or 0 if not */
1453
	ulint			wait,	/*!< in: LOCK_WAIT if also
1454 1455
					waiting locks are taken into
					account, or 0 if not */
1456
	const buf_block_t*	block,	/*!< in: buffer block containing
1457
					the record */
1458 1459
	ulint			heap_no,/*!< in: heap number of the record */
	const trx_t*		trx)	/*!< in: transaction, or NULL if
1460 1461
					requests by all transactions
					are taken into account */
osku's avatar
osku committed
1462 1463
{
	lock_t*	lock;
1464

osku's avatar
osku committed
1465 1466 1467 1468 1469
	ut_ad(mutex_own(&kernel_mutex));
	ut_ad(mode == LOCK_X || mode == LOCK_S);
	ut_ad(gap == 0 || gap == LOCK_GAP);
	ut_ad(wait == 0 || wait == LOCK_WAIT);

1470
	lock = lock_rec_get_first(block, heap_no);
osku's avatar
osku committed
1471 1472 1473

	while (lock) {
		if (lock->trx != trx
1474 1475
		    && (gap
			|| !(lock_rec_get_gap(lock)
1476
			     || heap_no == PAGE_HEAP_NO_SUPREMUM))
1477 1478
		    && (wait || !lock_get_wait(lock))
		    && lock_mode_stronger_or_eq(lock_get_mode(lock), mode)) {
osku's avatar
osku committed
1479

1480
			return(lock);
osku's avatar
osku committed
1481 1482
		}

marko's avatar
marko committed
1483
		lock = lock_rec_get_next(heap_no, lock);
osku's avatar
osku committed
1484 1485 1486 1487
	}

	return(NULL);
}
1488
#endif /* UNIV_DEBUG */
osku's avatar
osku committed
1489

1490
/*********************************************************************//**
osku's avatar
osku committed
1491
Checks if some other transaction has a conflicting explicit lock request
1492 1493
in the queue, so that we have to wait.
@return	lock or NULL */
osku's avatar
osku committed
1494 1495 1496 1497
static
lock_t*
lock_rec_other_has_conflicting(
/*===========================*/
1498
	enum lock_mode		mode,	/*!< in: LOCK_S or LOCK_X,
1499 1500 1501
					possibly ORed to LOCK_GAP or
					LOC_REC_NOT_GAP,
					LOCK_INSERT_INTENTION */
1502
	const buf_block_t*	block,	/*!< in: buffer block containing
1503
					the record */
1504 1505
	ulint			heap_no,/*!< in: heap number of the record */
	trx_t*			trx)	/*!< in: our transaction */
osku's avatar
osku committed
1506 1507
{
	lock_t*	lock;
1508

osku's avatar
osku committed
1509 1510
	ut_ad(mutex_own(&kernel_mutex));

1511
	lock = lock_rec_get_first(block, heap_no);
osku's avatar
osku committed
1512

marko's avatar
marko committed
1513
	if (UNIV_LIKELY_NULL(lock)) {
1514
		if (UNIV_UNLIKELY(heap_no == PAGE_HEAP_NO_SUPREMUM)) {
osku's avatar
osku committed
1515

marko's avatar
marko committed
1516 1517
			do {
				if (lock_rec_has_to_wait(trx, mode, lock,
1518
							 TRUE)) {
marko's avatar
marko committed
1519 1520 1521 1522 1523 1524 1525 1526 1527
					return(lock);
				}

				lock = lock_rec_get_next(heap_no, lock);
			} while (lock);
		} else {

			do {
				if (lock_rec_has_to_wait(trx, mode, lock,
1528
							 FALSE)) {
marko's avatar
marko committed
1529 1530 1531 1532 1533
					return(lock);
				}

				lock = lock_rec_get_next(heap_no, lock);
			} while (lock);
osku's avatar
osku committed
1534 1535 1536 1537 1538 1539
		}
	}

	return(NULL);
}

1540
/*********************************************************************//**
osku's avatar
osku committed
1541 1542
Looks for a suitable type record lock struct by the same trx on the same page.
This can be used to save space when a new record lock should be set on a page:
1543 1544
no new struct is needed, if a suitable old is found.
@return	lock or NULL */
osku's avatar
osku committed
1545 1546 1547 1548
UNIV_INLINE
lock_t*
lock_rec_find_similar_on_page(
/*==========================*/
1549 1550 1551 1552
	ulint		type_mode,	/*!< in: lock type_mode field */
	ulint		heap_no,	/*!< in: heap number of the record */
	lock_t*		lock,		/*!< in: lock_rec_get_first_on_page() */
	const trx_t*	trx)		/*!< in: transaction */
osku's avatar
osku committed
1553 1554 1555 1556 1557
{
	ut_ad(mutex_own(&kernel_mutex));

	while (lock != NULL) {
		if (lock->trx == trx
1558 1559
		    && lock->type_mode == type_mode
		    && lock_rec_get_n_bits(lock) > heap_no) {
1560

osku's avatar
osku committed
1561 1562
			return(lock);
		}
1563

osku's avatar
osku committed
1564 1565 1566 1567 1568 1569
		lock = lock_rec_get_next_on_page(lock);
	}

	return(NULL);
}

1570
/*********************************************************************//**
osku's avatar
osku committed
1571
Checks if some transaction has an implicit x-lock on a record in a secondary
1572 1573
index.
@return	transaction which has the x-lock, or NULL */
1574
static
osku's avatar
osku committed
1575 1576 1577
trx_t*
lock_sec_rec_some_has_impl_off_kernel(
/*==================================*/
1578 1579 1580
	const rec_t*	rec,	/*!< in: user record */
	dict_index_t*	index,	/*!< in: secondary index */
	const ulint*	offsets)/*!< in: rec_get_offsets(rec, index) */
osku's avatar
osku committed
1581
{
1582
	const page_t*	page = page_align(rec);
1583

osku's avatar
osku committed
1584
	ut_ad(mutex_own(&kernel_mutex));
1585
	ut_ad(!dict_index_is_clust(index));
osku's avatar
osku committed
1586 1587 1588 1589 1590 1591 1592 1593 1594 1595
	ut_ad(page_rec_is_user_rec(rec));
	ut_ad(rec_offs_validate(rec, index, offsets));

	/* Some transaction may have an implicit x-lock on the record only
	if the max trx id for the page >= min trx id for the trx list, or
	database recovery is running. We do not write the changes of a page
	max trx id to the log, and therefore during recovery, this value
	for a page may be incorrect. */

	if (!(ut_dulint_cmp(page_get_max_trx_id(page),
1596 1597
			    trx_list_get_min_trx_id()) >= 0)
	    && !recv_recovery_is_on()) {
osku's avatar
osku committed
1598 1599 1600 1601 1602 1603

		return(NULL);
	}

	/* Ok, in this case it is possible that some transaction has an
	implicit x-lock. We have to look in the clustered index. */
1604

osku's avatar
osku committed
1605
	if (!lock_check_trx_id_sanity(page_get_max_trx_id(page),
1606
				      rec, index, offsets, TRUE)) {
1607
		buf_page_print(page, 0);
1608

osku's avatar
osku committed
1609 1610 1611 1612 1613 1614 1615 1616
		/* The page is corrupt: try to avoid a crash by returning
		NULL */
		return(NULL);
	}

	return(row_vers_impl_x_locked_off_kernel(rec, index, offsets));
}

1617
/*********************************************************************//**
1618 1619 1620
Return approximate number or record locks (bits set in the bitmap) for
this transaction. Since delete-marked records may be removed, the
record count will not be precise. */
1621
UNIV_INTERN
1622 1623 1624
ulint
lock_number_of_rows_locked(
/*=======================*/
1625
	trx_t*	trx)	/*!< in: transaction */
1626 1627 1628 1629 1630 1631 1632 1633 1634
{
	lock_t*	lock;
	ulint   n_records = 0;
	ulint	n_bits;
	ulint	n_bit;

	lock = UT_LIST_GET_FIRST(trx->trx_locks);

	while (lock) {
1635
		if (lock_get_type_low(lock) == LOCK_REC) {
1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650
			n_bits = lock_rec_get_n_bits(lock);

			for (n_bit = 0; n_bit < n_bits; n_bit++) {
				if (lock_rec_get_nth_bit(lock, n_bit)) {
					n_records++;
				}
			}
		}

		lock = UT_LIST_GET_NEXT(trx_locks, lock);
	}

	return (n_records);
}

osku's avatar
osku committed
1651 1652
/*============== RECORD LOCK CREATION AND QUEUE MANAGEMENT =============*/

1653
/*********************************************************************//**
osku's avatar
osku committed
1654
Creates a new record lock and inserts it to the lock queue. Does NOT check
1655 1656
for deadlocks or lock compatibility!
@return	created lock */
osku's avatar
osku committed
1657 1658 1659 1660
static
lock_t*
lock_rec_create(
/*============*/
1661
	ulint			type_mode,/*!< in: lock mode and wait
1662 1663
					flag, type is ignored and
					replaced by LOCK_REC */
1664
	const buf_block_t*	block,	/*!< in: buffer block containing
1665
					the record */
1666 1667 1668
	ulint			heap_no,/*!< in: heap number of the record */
	dict_index_t*		index,	/*!< in: index of record */
	trx_t*			trx)	/*!< in: transaction */
osku's avatar
osku committed
1669
{
1670 1671 1672 1673 1674 1675
	lock_t*		lock;
	ulint		page_no;
	ulint		space;
	ulint		n_bits;
	ulint		n_bytes;
	const page_t*	page;
1676

osku's avatar
osku committed
1677 1678
	ut_ad(mutex_own(&kernel_mutex));

1679 1680 1681
	space = buf_block_get_space(block);
	page_no	= buf_block_get_page_no(block);
	page = block->frame;
osku's avatar
osku committed
1682

1683
	ut_ad(!!page_is_comp(page) == dict_table_is_comp(index->table));
osku's avatar
osku committed
1684 1685 1686 1687 1688

	/* If rec is the supremum record, then we reset the gap and
	LOCK_REC_NOT_GAP bits, as all locks on the supremum are
	automatically of the gap type */

1689
	if (UNIV_UNLIKELY(heap_no == PAGE_HEAP_NO_SUPREMUM)) {
osku's avatar
osku committed
1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706
		ut_ad(!(type_mode & LOCK_REC_NOT_GAP));

		type_mode = type_mode & ~(LOCK_GAP | LOCK_REC_NOT_GAP);
	}

	/* Make lock bitmap bigger by a safety margin */
	n_bits = page_dir_get_n_heap(page) + LOCK_PAGE_BITMAP_MARGIN;
	n_bytes = 1 + n_bits / 8;

	lock = mem_heap_alloc(trx->lock_heap, sizeof(lock_t) + n_bytes);

	UT_LIST_ADD_LAST(trx_locks, trx->trx_locks, lock);

	lock->trx = trx;

	lock->type_mode = (type_mode & ~LOCK_TYPE_MASK) | LOCK_REC;
	lock->index = index;
1707

osku's avatar
osku committed
1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720
	lock->un_member.rec_lock.space = space;
	lock->un_member.rec_lock.page_no = page_no;
	lock->un_member.rec_lock.n_bits = n_bytes * 8;

	/* Reset to zero the bitmap which resides immediately after the
	lock struct */

	lock_rec_bitmap_reset(lock);

	/* Set the bit corresponding to rec */
	lock_rec_set_nth_bit(lock, heap_no);

	HASH_INSERT(lock_t, hash, lock_sys->rec_hash,
1721
		    lock_rec_fold(space, page_no), lock);
1722
	if (UNIV_UNLIKELY(type_mode & LOCK_WAIT)) {
osku's avatar
osku committed
1723 1724 1725

		lock_set_lock_and_trx_wait(lock, trx);
	}
1726

osku's avatar
osku committed
1727 1728 1729
	return(lock);
}

1730
/*********************************************************************//**
osku's avatar
osku committed
1731
Enqueues a waiting request for a lock which cannot be granted immediately.
1732
Checks for deadlocks.
1733 1734 1735 1736
@return DB_LOCK_WAIT, DB_DEADLOCK, or DB_QUE_THR_SUSPENDED, or
DB_SUCCESS; DB_SUCCESS means that there was a deadlock, but another
transaction was chosen as a victim, and we got the lock immediately:
no need to wait then */
osku's avatar
osku committed
1737 1738 1739 1740
static
ulint
lock_rec_enqueue_waiting(
/*=====================*/
1741
	ulint			type_mode,/*!< in: lock mode this
1742 1743 1744 1745 1746 1747 1748 1749
					transaction is requesting:
					LOCK_S or LOCK_X, possibly
					ORed with LOCK_GAP or
					LOCK_REC_NOT_GAP, ORed with
					LOCK_INSERT_INTENTION if this
					waiting lock request is set
					when performing an insert of
					an index record */
1750
	const buf_block_t*	block,	/*!< in: buffer block containing
1751
					the record */
1752 1753 1754
	ulint			heap_no,/*!< in: heap number of the record */
	dict_index_t*		index,	/*!< in: index of record */
	que_thr_t*		thr)	/*!< in: query thread */
osku's avatar
osku committed
1755 1756 1757
{
	lock_t*	lock;
	trx_t*	trx;
1758

osku's avatar
osku committed
1759 1760 1761 1762 1763 1764
	ut_ad(mutex_own(&kernel_mutex));

	/* Test if there already is some other reason to suspend thread:
	we do not enqueue a lock request if the query thread should be
	stopped anyway */

marko's avatar
marko committed
1765
	if (UNIV_UNLIKELY(que_thr_stop(thr))) {
osku's avatar
osku committed
1766 1767 1768 1769 1770

		ut_error;

		return(DB_QUE_THR_SUSPENDED);
	}
1771

osku's avatar
osku committed
1772 1773
	trx = thr_get_trx(thr);

1774 1775 1776 1777 1778
	switch (trx_get_dict_operation(trx)) {
	case TRX_DICT_OP_NONE:
		break;
	case TRX_DICT_OP_TABLE:
	case TRX_DICT_OP_INDEX:
osku's avatar
osku committed
1779
		ut_print_timestamp(stderr);
1780 1781
		fputs("  InnoDB: Error: a record lock wait happens"
		      " in a dictionary operation!\n"
1782 1783
		      "InnoDB: ", stderr);
		dict_index_name_print(stderr, trx, index);
osku's avatar
osku committed
1784
		fputs(".\n"
1785 1786 1787
		      "InnoDB: Submit a detailed bug report"
		      " to http://bugs.mysql.com\n",
		      stderr);
osku's avatar
osku committed
1788
	}
marko's avatar
marko committed
1789

osku's avatar
osku committed
1790
	/* Enqueue the lock request that will wait to be granted */
1791 1792
	lock = lock_rec_create(type_mode | LOCK_WAIT,
			       block, heap_no, index, trx);
osku's avatar
osku committed
1793 1794 1795

	/* Check if a deadlock occurs: if yes, remove the lock request and
	return an error code */
1796

marko's avatar
marko committed
1797
	if (UNIV_UNLIKELY(lock_deadlock_occurs(lock, trx))) {
osku's avatar
osku committed
1798 1799

		lock_reset_lock_and_trx_wait(lock);
marko's avatar
marko committed
1800
		lock_rec_reset_nth_bit(lock, heap_no);
osku's avatar
osku committed
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822

		return(DB_DEADLOCK);
	}

	/* If there was a deadlock but we chose another transaction as a
	victim, it is possible that we already have the lock now granted! */

	if (trx->wait_lock == NULL) {

		return(DB_SUCCESS);
	}

	trx->que_state = TRX_QUE_LOCK_WAIT;
	trx->was_chosen_as_deadlock_victim = FALSE;
	trx->wait_started = time(NULL);

	ut_a(que_thr_stop(thr));

#ifdef UNIV_DEBUG
	if (lock_print_waits) {
		fprintf(stderr, "Lock wait for trx %lu in index ",
			(ulong) ut_dulint_get_low(trx->id));
1823
		ut_print_name(stderr, trx, FALSE, index->name);
osku's avatar
osku committed
1824 1825
	}
#endif /* UNIV_DEBUG */
1826 1827

	return(DB_LOCK_WAIT);
osku's avatar
osku committed
1828 1829
}

1830
/*********************************************************************//**
osku's avatar
osku committed
1831 1832 1833 1834 1835
Adds a record lock request in the record queue. The request is normally
added as the last in the queue, but if there are no waiting lock requests
on the record, and the request to be added is not a waiting request, we
can reuse a suitable record lock object already existing on the same page,
just setting the appropriate bit in its bitmap. This is a low-level function
1836 1837
which does NOT check for deadlocks or lock compatibility!
@return	lock where the bit was set */
osku's avatar
osku committed
1838 1839 1840 1841
static
lock_t*
lock_rec_add_to_queue(
/*==================*/
1842
	ulint			type_mode,/*!< in: lock mode, wait, gap
1843 1844
					etc. flags; type is ignored
					and replaced by LOCK_REC */
1845
	const buf_block_t*	block,	/*!< in: buffer block containing
1846
					the record */
1847 1848 1849
	ulint			heap_no,/*!< in: heap number of the record */
	dict_index_t*		index,	/*!< in: index of record */
	trx_t*			trx)	/*!< in: transaction */
osku's avatar
osku committed
1850 1851
{
	lock_t*	lock;
1852

osku's avatar
osku committed
1853
	ut_ad(mutex_own(&kernel_mutex));
1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874
#ifdef UNIV_DEBUG
	switch (type_mode & LOCK_MODE_MASK) {
	case LOCK_X:
	case LOCK_S:
		break;
	default:
		ut_error;
	}

	if (!(type_mode & (LOCK_WAIT | LOCK_GAP))) {
		enum lock_mode	mode = (type_mode & LOCK_MODE_MASK) == LOCK_S
			? LOCK_X
			: LOCK_S;
		lock_t*		other_lock
			= lock_rec_other_has_expl_req(mode, 0, LOCK_WAIT,
						      block, heap_no, trx);
		ut_a(!other_lock);
	}
#endif /* UNIV_DEBUG */

	type_mode |= LOCK_REC;
osku's avatar
osku committed
1875 1876 1877 1878 1879 1880

	/* If rec is the supremum record, then we can reset the gap bit, as
	all locks on the supremum are automatically of the gap type, and we
	try to avoid unnecessary memory consumption of a new record lock
	struct for a gap type lock */

1881
	if (UNIV_UNLIKELY(heap_no == PAGE_HEAP_NO_SUPREMUM)) {
osku's avatar
osku committed
1882 1883 1884 1885
		ut_ad(!(type_mode & LOCK_REC_NOT_GAP));

		/* There should never be LOCK_REC_NOT_GAP on a supremum
		record, but let us play safe */
1886

osku's avatar
osku committed
1887 1888 1889 1890 1891
		type_mode = type_mode & ~(LOCK_GAP | LOCK_REC_NOT_GAP);
	}

	/* Look for a waiting lock request on the same record or on a gap */

1892
	lock = lock_rec_get_first_on_page(block);
osku's avatar
osku committed
1893 1894 1895

	while (lock != NULL) {
		if (lock_get_wait(lock)
1896
		    && (lock_rec_get_nth_bit(lock, heap_no))) {
osku's avatar
osku committed
1897

marko's avatar
marko committed
1898
			goto somebody_waits;
osku's avatar
osku committed
1899 1900 1901 1902 1903
		}

		lock = lock_rec_get_next_on_page(lock);
	}

1904
	if (UNIV_LIKELY(!(type_mode & LOCK_WAIT))) {
osku's avatar
osku committed
1905

marko's avatar
marko committed
1906 1907 1908
		/* Look for a similar record lock on the same page:
		if one is found and there are no waiting lock requests,
		we can just set the bit */
osku's avatar
osku committed
1909

1910 1911
		lock = lock_rec_find_similar_on_page(
			type_mode, heap_no,
1912
			lock_rec_get_first_on_page(block), trx);
osku's avatar
osku committed
1913

marko's avatar
marko committed
1914
		if (lock) {
osku's avatar
osku committed
1915

marko's avatar
marko committed
1916 1917 1918 1919
			lock_rec_set_nth_bit(lock, heap_no);

			return(lock);
		}
osku's avatar
osku committed
1920 1921
	}

marko's avatar
marko committed
1922
somebody_waits:
1923
	return(lock_rec_create(type_mode, block, heap_no, index, trx));
osku's avatar
osku committed
1924 1925
}

1926
/*********************************************************************//**
osku's avatar
osku committed
1927 1928 1929 1930 1931
This is a fast routine for locking a record in the most common cases:
there are no explicit locks on the page, or there is just one lock, owned
by this transaction, and of the right type_mode. This is a low-level function
which does NOT look at implicit locks! Checks lock compatibility within
explicit locks. This function sets a normal next-key lock, or in the case of
1932 1933
a page supremum record, a gap type lock.
@return	TRUE if locking succeeded */
osku's avatar
osku committed
1934 1935 1936 1937
UNIV_INLINE
ibool
lock_rec_lock_fast(
/*===============*/
1938
	ibool			impl,	/*!< in: if TRUE, no lock is set
1939 1940 1941
					if no wait is necessary: we
					assume that the caller will
					set an implicit lock */
1942
	ulint			mode,	/*!< in: lock mode: LOCK_X or
1943 1944
					LOCK_S possibly ORed to either
					LOCK_GAP or LOCK_REC_NOT_GAP */
1945
	const buf_block_t*	block,	/*!< in: buffer block containing
1946
					the record */
1947 1948 1949
	ulint			heap_no,/*!< in: heap number of record */
	dict_index_t*		index,	/*!< in: index of record */
	que_thr_t*		thr)	/*!< in: query thread */
osku's avatar
osku committed
1950 1951
{
	lock_t*	lock;
1952
	trx_t*	trx;
osku's avatar
osku committed
1953 1954 1955

	ut_ad(mutex_own(&kernel_mutex));
	ut_ad((LOCK_MODE_MASK & mode) != LOCK_S
1956
	      || lock_table_has(thr_get_trx(thr), index->table, LOCK_IS));
osku's avatar
osku committed
1957
	ut_ad((LOCK_MODE_MASK & mode) != LOCK_X
1958
	      || lock_table_has(thr_get_trx(thr), index->table, LOCK_IX));
osku's avatar
osku committed
1959
	ut_ad((LOCK_MODE_MASK & mode) == LOCK_S
1960
	      || (LOCK_MODE_MASK & mode) == LOCK_X);
osku's avatar
osku committed
1961
	ut_ad(mode - (LOCK_MODE_MASK & mode) == LOCK_GAP
1962 1963
	      || mode - (LOCK_MODE_MASK & mode) == 0
	      || mode - (LOCK_MODE_MASK & mode) == LOCK_REC_NOT_GAP);
1964

1965
	lock = lock_rec_get_first_on_page(block);
osku's avatar
osku committed
1966 1967 1968 1969 1970

	trx = thr_get_trx(thr);

	if (lock == NULL) {
		if (!impl) {
1971
			lock_rec_create(mode, block, heap_no, index, trx);
osku's avatar
osku committed
1972
		}
1973

osku's avatar
osku committed
1974 1975
		return(TRUE);
	}
1976

osku's avatar
osku committed
1977 1978 1979 1980 1981 1982
	if (lock_rec_get_next_on_page(lock)) {

		return(FALSE);
	}

	if (lock->trx != trx
1983 1984
	    || lock->type_mode != (mode | LOCK_REC)
	    || lock_rec_get_n_bits(lock) <= heap_no) {
osku's avatar
osku committed
1985

1986
		return(FALSE);
osku's avatar
osku committed
1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000
	}

	if (!impl) {
		/* If the nth bit of the record lock is already set then we
		do not set a new lock bit, otherwise we do set */

		if (!lock_rec_get_nth_bit(lock, heap_no)) {
			lock_rec_set_nth_bit(lock, heap_no);
		}
	}

	return(TRUE);
}

2001
/*********************************************************************//**
osku's avatar
osku committed
2002 2003 2004
This is the general, and slower, routine for locking a record. This is a
low-level function which does NOT look at implicit locks! Checks lock
compatibility within explicit locks. This function sets a normal next-key
2005 2006
lock, or in the case of a page supremum record, a gap type lock.
@return	DB_SUCCESS, DB_LOCK_WAIT, or error code */
osku's avatar
osku committed
2007 2008 2009 2010
static
ulint
lock_rec_lock_slow(
/*===============*/
2011
	ibool			impl,	/*!< in: if TRUE, no lock is set
2012 2013 2014
					if no wait is necessary: we
					assume that the caller will
					set an implicit lock */
2015
	ulint			mode,	/*!< in: lock mode: LOCK_X or
2016 2017
					LOCK_S possibly ORed to either
					LOCK_GAP or LOCK_REC_NOT_GAP */
2018
	const buf_block_t*	block,	/*!< in: buffer block containing
2019
					the record */
2020 2021 2022
	ulint			heap_no,/*!< in: heap number of record */
	dict_index_t*		index,	/*!< in: index of record */
	que_thr_t*		thr)	/*!< in: query thread */
osku's avatar
osku committed
2023 2024 2025 2026 2027 2028
{
	trx_t*	trx;
	ulint	err;

	ut_ad(mutex_own(&kernel_mutex));
	ut_ad((LOCK_MODE_MASK & mode) != LOCK_S
2029
	      || lock_table_has(thr_get_trx(thr), index->table, LOCK_IS));
osku's avatar
osku committed
2030
	ut_ad((LOCK_MODE_MASK & mode) != LOCK_X
2031
	      || lock_table_has(thr_get_trx(thr), index->table, LOCK_IX));
osku's avatar
osku committed
2032
	ut_ad((LOCK_MODE_MASK & mode) == LOCK_S
2033
	      || (LOCK_MODE_MASK & mode) == LOCK_X);
osku's avatar
osku committed
2034
	ut_ad(mode - (LOCK_MODE_MASK & mode) == LOCK_GAP
2035 2036
	      || mode - (LOCK_MODE_MASK & mode) == 0
	      || mode - (LOCK_MODE_MASK & mode) == LOCK_REC_NOT_GAP);
2037

osku's avatar
osku committed
2038
	trx = thr_get_trx(thr);
2039

2040
	if (lock_rec_has_expl(mode, block, heap_no, trx)) {
osku's avatar
osku committed
2041 2042 2043 2044
		/* The trx already has a strong enough lock on rec: do
		nothing */

		err = DB_SUCCESS;
2045
	} else if (lock_rec_other_has_conflicting(mode, block, heap_no, trx)) {
osku's avatar
osku committed
2046 2047 2048 2049

		/* If another transaction has a non-gap conflicting request in
		the queue, as this transaction does not have a lock strong
		enough already granted on the record, we have to wait. */
2050

2051 2052
		err = lock_rec_enqueue_waiting(mode, block, heap_no,
					       index, thr);
osku's avatar
osku committed
2053 2054 2055 2056
	} else {
		if (!impl) {
			/* Set the requested lock on the record */

2057
			lock_rec_add_to_queue(LOCK_REC | mode, block,
2058
					      heap_no, index, trx);
osku's avatar
osku committed
2059 2060 2061 2062 2063 2064 2065 2066
		}

		err = DB_SUCCESS;
	}

	return(err);
}

2067
/*********************************************************************//**
osku's avatar
osku committed
2068 2069 2070 2071
Tries to lock the specified record in the mode requested. If not immediately
possible, enqueues a waiting lock request. This is a low-level function
which does NOT look at implicit locks! Checks lock compatibility within
explicit locks. This function sets a normal next-key lock, or in the case
2072 2073
of a page supremum record, a gap type lock.
@return	DB_SUCCESS, DB_LOCK_WAIT, or error code */
osku's avatar
osku committed
2074 2075 2076 2077
static
ulint
lock_rec_lock(
/*==========*/
2078
	ibool			impl,	/*!< in: if TRUE, no lock is set
2079 2080 2081
					if no wait is necessary: we
					assume that the caller will
					set an implicit lock */
2082
	ulint			mode,	/*!< in: lock mode: LOCK_X or
2083 2084
					LOCK_S possibly ORed to either
					LOCK_GAP or LOCK_REC_NOT_GAP */
2085
	const buf_block_t*	block,	/*!< in: buffer block containing
2086
					the record */
2087 2088 2089
	ulint			heap_no,/*!< in: heap number of record */
	dict_index_t*		index,	/*!< in: index of record */
	que_thr_t*		thr)	/*!< in: query thread */
osku's avatar
osku committed
2090 2091 2092 2093 2094
{
	ulint	err;

	ut_ad(mutex_own(&kernel_mutex));
	ut_ad((LOCK_MODE_MASK & mode) != LOCK_S
2095
	      || lock_table_has(thr_get_trx(thr), index->table, LOCK_IS));
osku's avatar
osku committed
2096
	ut_ad((LOCK_MODE_MASK & mode) != LOCK_X
2097
	      || lock_table_has(thr_get_trx(thr), index->table, LOCK_IX));
osku's avatar
osku committed
2098
	ut_ad((LOCK_MODE_MASK & mode) == LOCK_S
2099
	      || (LOCK_MODE_MASK & mode) == LOCK_X);
osku's avatar
osku committed
2100
	ut_ad(mode - (LOCK_MODE_MASK & mode) == LOCK_GAP
2101 2102
	      || mode - (LOCK_MODE_MASK & mode) == LOCK_REC_NOT_GAP
	      || mode - (LOCK_MODE_MASK & mode) == 0);
2103

2104
	if (lock_rec_lock_fast(impl, mode, block, heap_no, index, thr)) {
osku's avatar
osku committed
2105 2106 2107 2108 2109 2110

		/* We try a simplified and faster subroutine for the most
		common cases */

		err = DB_SUCCESS;
	} else {
2111 2112
		err = lock_rec_lock_slow(impl, mode, block,
					 heap_no, index, thr);
osku's avatar
osku committed
2113 2114 2115 2116 2117
	}

	return(err);
}

2118
/*********************************************************************//**
2119 2120
Checks if a waiting record lock request still has to wait in a queue.
@return	TRUE if still has to wait */
osku's avatar
osku committed
2121 2122 2123 2124
static
ibool
lock_rec_has_to_wait_in_queue(
/*==========================*/
2125
	lock_t*	wait_lock)	/*!< in: waiting record lock */
osku's avatar
osku committed
2126 2127 2128 2129 2130 2131 2132
{
	lock_t*	lock;
	ulint	space;
	ulint	page_no;
	ulint	heap_no;

	ut_ad(mutex_own(&kernel_mutex));
2133
	ut_ad(lock_get_wait(wait_lock));
2134
	ut_ad(lock_get_type_low(wait_lock) == LOCK_REC);
2135

osku's avatar
osku committed
2136 2137 2138 2139 2140 2141 2142 2143 2144
	space = wait_lock->un_member.rec_lock.space;
	page_no = wait_lock->un_member.rec_lock.page_no;
	heap_no = lock_rec_find_set_bit(wait_lock);

	lock = lock_rec_get_first_on_page_addr(space, page_no);

	while (lock != wait_lock) {

		if (lock_rec_get_nth_bit(lock, heap_no)
2145
		    && lock_has_to_wait(wait_lock, lock)) {
osku's avatar
osku committed
2146

2147
			return(TRUE);
osku's avatar
osku committed
2148 2149 2150 2151 2152 2153 2154 2155
		}

		lock = lock_rec_get_next_on_page(lock);
	}

	return(FALSE);
}

2156
/*************************************************************//**
osku's avatar
osku committed
2157 2158 2159 2160 2161 2162
Grants a lock to a waiting lock request and releases the waiting
transaction. */
static
void
lock_grant(
/*=======*/
2163
	lock_t*	lock)	/*!< in/out: waiting lock request */
osku's avatar
osku committed
2164 2165 2166 2167 2168
{
	ut_ad(mutex_own(&kernel_mutex));

	lock_reset_lock_and_trx_wait(lock);

2169
	if (lock_get_mode(lock) == LOCK_AUTO_INC) {
sunny's avatar
sunny committed
2170 2171
		trx_t*		trx = lock->trx;
		dict_table_t*	table = lock->un_member.tab_lock.table;
osku's avatar
osku committed
2172

sunny's avatar
sunny committed
2173
		if (table->autoinc_trx == trx) {
2174
			fprintf(stderr,
2175 2176
				"InnoDB: Error: trx already had"
				" an AUTO-INC lock!\n");
sunny's avatar
sunny committed
2177 2178
		} else {
			table->autoinc_trx = trx;
osku's avatar
osku committed
2179

sunny's avatar
sunny committed
2180 2181
			ib_vector_push(trx->autoinc_locks, lock);
		}
2182
	}
osku's avatar
osku committed
2183 2184 2185 2186

#ifdef UNIV_DEBUG
	if (lock_print_waits) {
		fprintf(stderr, "Lock wait for trx %lu ends\n",
2187
			(ulong) ut_dulint_get_low(lock->trx->id));
osku's avatar
osku committed
2188 2189 2190 2191 2192 2193 2194
	}
#endif /* UNIV_DEBUG */

	/* If we are resolving a deadlock by choosing another transaction
	as a victim, then our original transaction may not be in the
	TRX_QUE_LOCK_WAIT state, and there is no need to end the lock wait
	for it */
2195 2196

	if (lock->trx->que_state == TRX_QUE_LOCK_WAIT) {
osku's avatar
osku committed
2197 2198 2199 2200
		trx_end_lock_wait(lock->trx);
	}
}

2201
/*************************************************************//**
osku's avatar
osku committed
2202 2203 2204 2205 2206 2207 2208
Cancels a waiting record lock request and releases the waiting transaction
that requested it. NOTE: does NOT check if waiting lock requests behind this
one can now be granted! */
static
void
lock_rec_cancel(
/*============*/
2209
	lock_t*	lock)	/*!< in: waiting record lock request */
osku's avatar
osku committed
2210 2211
{
	ut_ad(mutex_own(&kernel_mutex));
2212
	ut_ad(lock_get_type_low(lock) == LOCK_REC);
osku's avatar
osku committed
2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224

	/* Reset the bit (there can be only one set bit) in the lock bitmap */
	lock_rec_reset_nth_bit(lock, lock_rec_find_set_bit(lock));

	/* Reset the wait flag and the back pointer to lock in trx */

	lock_reset_lock_and_trx_wait(lock);

	/* The following function releases the trx from lock wait */

	trx_end_lock_wait(lock->trx);
}
2225

2226
/*************************************************************//**
osku's avatar
osku committed
2227 2228 2229 2230 2231 2232 2233
Removes a record lock request, waiting or granted, from the queue and
grants locks to other transactions in the queue if they now are entitled
to a lock. NOTE: all record locks contained in in_lock are removed. */
static
void
lock_rec_dequeue_from_page(
/*=======================*/
2234
	lock_t*	in_lock)/*!< in: record lock object: all record locks which
osku's avatar
osku committed
2235 2236 2237 2238 2239 2240 2241 2242
			are contained in this lock object are removed;
			transactions waiting behind will get their lock
			requests granted, if they are now qualified to it */
{
	ulint	space;
	ulint	page_no;
	lock_t*	lock;
	trx_t*	trx;
2243

osku's avatar
osku committed
2244
	ut_ad(mutex_own(&kernel_mutex));
2245
	ut_ad(lock_get_type_low(in_lock) == LOCK_REC);
osku's avatar
osku committed
2246 2247 2248 2249 2250 2251 2252

	trx = in_lock->trx;

	space = in_lock->un_member.rec_lock.space;
	page_no = in_lock->un_member.rec_lock.page_no;

	HASH_DELETE(lock_t, hash, lock_sys->rec_hash,
2253
		    lock_rec_fold(space, page_no), in_lock);
osku's avatar
osku committed
2254 2255 2256 2257 2258 2259 2260 2261

	UT_LIST_REMOVE(trx_locks, trx->trx_locks, in_lock);

	/* Check if waiting locks in the queue can now be granted: grant
	locks if there are no conflicting locks ahead. */

	lock = lock_rec_get_first_on_page_addr(space, page_no);

2262
	while (lock != NULL) {
osku's avatar
osku committed
2263
		if (lock_get_wait(lock)
2264
		    && !lock_rec_has_to_wait_in_queue(lock)) {
osku's avatar
osku committed
2265 2266 2267 2268 2269 2270 2271

			/* Grant the lock */
			lock_grant(lock);
		}

		lock = lock_rec_get_next_on_page(lock);
	}
2272
}
osku's avatar
osku committed
2273

2274
/*************************************************************//**
osku's avatar
osku committed
2275 2276 2277 2278 2279
Removes a record lock request, waiting or granted, from the queue. */
static
void
lock_rec_discard(
/*=============*/
2280
	lock_t*	in_lock)/*!< in: record lock object: all record locks which
osku's avatar
osku committed
2281 2282 2283 2284 2285
			are contained in this lock object are removed */
{
	ulint	space;
	ulint	page_no;
	trx_t*	trx;
2286

osku's avatar
osku committed
2287
	ut_ad(mutex_own(&kernel_mutex));
2288
	ut_ad(lock_get_type_low(in_lock) == LOCK_REC);
osku's avatar
osku committed
2289 2290

	trx = in_lock->trx;
2291

osku's avatar
osku committed
2292 2293 2294 2295
	space = in_lock->un_member.rec_lock.space;
	page_no = in_lock->un_member.rec_lock.page_no;

	HASH_DELETE(lock_t, hash, lock_sys->rec_hash,
2296
		    lock_rec_fold(space, page_no), in_lock);
osku's avatar
osku committed
2297 2298 2299 2300

	UT_LIST_REMOVE(trx_locks, trx->trx_locks, in_lock);
}

2301
/*************************************************************//**
osku's avatar
osku committed
2302 2303 2304 2305 2306 2307 2308
Removes record lock objects set on an index page which is discarded. This
function does not move locks, or check for waiting locks, therefore the
lock bitmaps must already be reset when this function is called. */
static
void
lock_rec_free_all_from_discard_page(
/*================================*/
2309
	const buf_block_t*	block)	/*!< in: page to be discarded */
osku's avatar
osku committed
2310 2311 2312 2313 2314
{
	ulint	space;
	ulint	page_no;
	lock_t*	lock;
	lock_t*	next_lock;
2315

osku's avatar
osku committed
2316
	ut_ad(mutex_own(&kernel_mutex));
2317

2318 2319
	space = buf_block_get_space(block);
	page_no = buf_block_get_page_no(block);
osku's avatar
osku committed
2320 2321 2322 2323 2324 2325 2326 2327

	lock = lock_rec_get_first_on_page_addr(space, page_no);

	while (lock != NULL) {
		ut_ad(lock_rec_find_set_bit(lock) == ULINT_UNDEFINED);
		ut_ad(!lock_get_wait(lock));

		next_lock = lock_rec_get_next_on_page(lock);
2328

osku's avatar
osku committed
2329
		lock_rec_discard(lock);
2330

osku's avatar
osku committed
2331 2332
		lock = next_lock;
	}
2333
}
osku's avatar
osku committed
2334 2335 2336

/*============= RECORD LOCK MOVING AND INHERITING ===================*/

2337
/*************************************************************//**
osku's avatar
osku committed
2338 2339
Resets the lock bits for a single record. Releases transactions waiting for
lock requests here. */
2340
static
osku's avatar
osku committed
2341 2342 2343
void
lock_rec_reset_and_release_wait(
/*============================*/
2344
	const buf_block_t*	block,	/*!< in: buffer block containing
2345
					the record */
2346
	ulint			heap_no)/*!< in: heap number of record */
osku's avatar
osku committed
2347 2348 2349 2350 2351
{
	lock_t*	lock;

	ut_ad(mutex_own(&kernel_mutex));

2352
	lock = lock_rec_get_first(block, heap_no);
osku's avatar
osku committed
2353 2354 2355 2356 2357 2358 2359 2360

	while (lock != NULL) {
		if (lock_get_wait(lock)) {
			lock_rec_cancel(lock);
		} else {
			lock_rec_reset_nth_bit(lock, heap_no);
		}

marko's avatar
marko committed
2361
		lock = lock_rec_get_next(heap_no, lock);
osku's avatar
osku committed
2362
	}
2363
}
osku's avatar
osku committed
2364

2365
/*************************************************************//**
osku's avatar
osku committed
2366 2367 2368 2369
Makes a record to inherit the locks (except LOCK_INSERT_INTENTION type)
of another record as gap type locks, but does not reset the lock bits of
the other record. Also waiting lock requests on rec are inherited as
GRANTED gap locks. */
2370
static
osku's avatar
osku committed
2371 2372 2373
void
lock_rec_inherit_to_gap(
/*====================*/
2374
	const buf_block_t*	heir_block,	/*!< in: block containing the
2375
						record which inherits */
2376
	const buf_block_t*	block,		/*!< in: block containing the
2377 2378 2379
						record from which inherited;
						does NOT reset the locks on
						this record */
2380
	ulint			heir_heap_no,	/*!< in: heap_no of the
2381
						inheriting record */
2382
	ulint			heap_no)	/*!< in: heap_no of the
2383
						donating record */
osku's avatar
osku committed
2384 2385
{
	lock_t*	lock;
2386

osku's avatar
osku committed
2387
	ut_ad(mutex_own(&kernel_mutex));
2388

2389
	lock = lock_rec_get_first(block, heap_no);
osku's avatar
osku committed
2390

2391 2392
	/* If srv_locks_unsafe_for_binlog is TRUE or session is using
	READ COMMITTED isolation level, we do not want locks set
osku's avatar
osku committed
2393 2394 2395 2396 2397 2398
	by an UPDATE or a DELETE to be inherited as gap type locks. But we
	DO want S-locks set by a consistency constraint to be inherited also
	then. */

	while (lock != NULL) {
		if (!lock_rec_get_insert_intention(lock)
2399 2400 2401 2402 2403 2404 2405
		    && !((srv_locks_unsafe_for_binlog
			  || lock->trx->isolation_level
			  == TRX_ISO_READ_COMMITTED)
			 && lock_get_mode(lock) == LOCK_X)) {

			lock_rec_add_to_queue(LOCK_REC | LOCK_GAP
					      | lock_get_mode(lock),
2406
					      heir_block, heir_heap_no,
2407
					      lock->index, lock->trx);
2408 2409
		}

marko's avatar
marko committed
2410
		lock = lock_rec_get_next(heap_no, lock);
osku's avatar
osku committed
2411
	}
2412
}
osku's avatar
osku committed
2413

2414
/*************************************************************//**
osku's avatar
osku committed
2415 2416 2417 2418 2419 2420 2421
Makes a record to inherit the gap locks (except LOCK_INSERT_INTENTION type)
of another record as gap type locks, but does not reset the lock bits of the
other record. Also waiting lock requests are inherited as GRANTED gap locks. */
static
void
lock_rec_inherit_to_gap_if_gap_lock(
/*================================*/
2422 2423
	const buf_block_t*	block,		/*!< in: buffer block */
	ulint			heir_heap_no,	/*!< in: heap_no of
2424
						record which inherits */
2425
	ulint			heap_no)	/*!< in: heap_no of record
2426 2427 2428
						from which inherited;
						does NOT reset the locks
						on this record */
osku's avatar
osku committed
2429 2430
{
	lock_t*	lock;
2431

osku's avatar
osku committed
2432
	ut_ad(mutex_own(&kernel_mutex));
2433

2434
	lock = lock_rec_get_first(block, heap_no);
osku's avatar
osku committed
2435 2436 2437

	while (lock != NULL) {
		if (!lock_rec_get_insert_intention(lock)
2438
		    && (heap_no == PAGE_HEAP_NO_SUPREMUM
2439
			|| !lock_rec_get_rec_not_gap(lock))) {
2440

2441 2442
			lock_rec_add_to_queue(LOCK_REC | LOCK_GAP
					      | lock_get_mode(lock),
2443
					      block, heir_heap_no,
2444
					      lock->index, lock->trx);
2445
		}
osku's avatar
osku committed
2446

marko's avatar
marko committed
2447
		lock = lock_rec_get_next(heap_no, lock);
osku's avatar
osku committed
2448
	}
2449
}
osku's avatar
osku committed
2450

2451
/*************************************************************//**
osku's avatar
osku committed
2452 2453 2454 2455 2456 2457
Moves the locks of a record to another record and resets the lock bits of
the donating record. */
static
void
lock_rec_move(
/*==========*/
2458
	const buf_block_t*	receiver,	/*!< in: buffer block containing
2459
						the receiving record */
2460
	const buf_block_t*	donator,	/*!< in: buffer block containing
2461
						the donating record */
2462
	ulint			receiver_heap_no,/*!< in: heap_no of the record
2463 2464 2465
						which gets the locks; there
						must be no lock requests
						on it! */
2466
	ulint			donator_heap_no)/*!< in: heap_no of the record
2467
						which gives the locks */
osku's avatar
osku committed
2468 2469
{
	lock_t*	lock;
2470

osku's avatar
osku committed
2471 2472
	ut_ad(mutex_own(&kernel_mutex));

marko's avatar
marko committed
2473
	lock = lock_rec_get_first(donator, donator_heap_no);
osku's avatar
osku committed
2474

marko's avatar
marko committed
2475
	ut_ad(lock_rec_get_first(receiver, receiver_heap_no) == NULL);
osku's avatar
osku committed
2476 2477

	while (lock != NULL) {
2478
		const ulint	type_mode = lock->type_mode;
2479

marko's avatar
marko committed
2480
		lock_rec_reset_nth_bit(lock, donator_heap_no);
osku's avatar
osku committed
2481

2482
		if (UNIV_UNLIKELY(type_mode & LOCK_WAIT)) {
osku's avatar
osku committed
2483
			lock_reset_lock_and_trx_wait(lock);
2484
		}
osku's avatar
osku committed
2485 2486 2487 2488

		/* Note that we FIRST reset the bit, and then set the lock:
		the function works also if donator == receiver */

marko's avatar
marko committed
2489
		lock_rec_add_to_queue(type_mode, receiver, receiver_heap_no,
2490
				      lock->index, lock->trx);
marko's avatar
marko committed
2491
		lock = lock_rec_get_next(donator_heap_no, lock);
osku's avatar
osku committed
2492 2493
	}

marko's avatar
marko committed
2494
	ut_ad(lock_rec_get_first(donator, donator_heap_no) == NULL);
2495
}
osku's avatar
osku committed
2496

2497
/*************************************************************//**
osku's avatar
osku committed
2498 2499 2500 2501
Updates the lock table when we have reorganized a page. NOTE: we copy
also the locks set on the infimum of the page; the infimum may carry
locks if an update of a record is occurring on the page, and its locks
were temporarily stored on the infimum. */
2502
UNIV_INTERN
osku's avatar
osku committed
2503 2504 2505
void
lock_move_reorganize_page(
/*======================*/
2506
	const buf_block_t*	block,	/*!< in: old index page, now
2507
					reorganized */
2508
	const buf_block_t*	oblock)	/*!< in: copy of the old, not
2509
					reorganized page */
osku's avatar
osku committed
2510 2511 2512 2513 2514 2515 2516 2517
{
	lock_t*		lock;
	UT_LIST_BASE_NODE_T(lock_t)	old_locks;
	mem_heap_t*	heap		= NULL;
	ulint		comp;

	lock_mutex_enter_kernel();

2518
	lock = lock_rec_get_first_on_page(block);
osku's avatar
osku committed
2519 2520 2521 2522 2523 2524 2525 2526

	if (lock == NULL) {
		lock_mutex_exit_kernel();

		return;
	}

	heap = mem_heap_create(256);
2527

osku's avatar
osku committed
2528 2529 2530 2531 2532
	/* Copy first all the locks on the page to heap and reset the
	bitmaps in the original locks; chain the copies of the locks
	using the trx_locks field in them. */

	UT_LIST_INIT(old_locks);
2533

2534
	do {
osku's avatar
osku committed
2535
		/* Make a copy of the lock */
2536
		lock_t*	old_lock = lock_rec_copy(lock, heap);
osku's avatar
osku committed
2537 2538 2539 2540 2541 2542 2543 2544

		UT_LIST_ADD_LAST(trx_locks, old_locks, old_lock);

		/* Reset bitmap of lock */
		lock_rec_bitmap_reset(lock);

		if (lock_get_wait(lock)) {
			lock_reset_lock_and_trx_wait(lock);
2545
		}
osku's avatar
osku committed
2546 2547

		lock = lock_rec_get_next_on_page(lock);
2548
	} while (lock != NULL);
osku's avatar
osku committed
2549

2550 2551
	comp = page_is_comp(block->frame);
	ut_ad(comp == page_is_comp(oblock->frame));
osku's avatar
osku committed
2552

2553 2554
	for (lock = UT_LIST_GET_FIRST(old_locks); lock;
	     lock = UT_LIST_GET_NEXT(trx_locks, lock)) {
osku's avatar
osku committed
2555 2556 2557 2558
		/* NOTE: we copy also the locks set on the infimum and
		supremum of the page; the infimum may carry locks if an
		update of a record is occurring on the page, and its locks
		were temporarily stored on the infimum */
2559 2560
		page_cur_t	cur1;
		page_cur_t	cur2;
2561

2562 2563
		page_cur_set_before_first(block, &cur1);
		page_cur_set_before_first(oblock, &cur2);
osku's avatar
osku committed
2564 2565 2566

		/* Set locks according to old locks */
		for (;;) {
2567 2568 2569
			ulint	old_heap_no;
			ulint	new_heap_no;

2570 2571
			ut_ad(comp || !memcmp(page_cur_get_rec(&cur1),
					      page_cur_get_rec(&cur2),
2572 2573 2574
					      rec_get_data_size_old(
						      page_cur_get_rec(
							      &cur2))));
marko's avatar
marko committed
2575
			if (UNIV_LIKELY(comp)) {
2576 2577 2578 2579
				old_heap_no = rec_get_heap_no_new(
					page_cur_get_rec(&cur2));
				new_heap_no = rec_get_heap_no_new(
					page_cur_get_rec(&cur1));
marko's avatar
marko committed
2580
			} else {
2581 2582 2583 2584
				old_heap_no = rec_get_heap_no_old(
					page_cur_get_rec(&cur2));
				new_heap_no = rec_get_heap_no_old(
					page_cur_get_rec(&cur1));
marko's avatar
marko committed
2585
			}
osku's avatar
osku committed
2586 2587 2588

			if (lock_rec_get_nth_bit(lock, old_heap_no)) {

2589 2590 2591 2592
				/* Clear the bit in old_lock. */
				ut_d(lock_rec_reset_nth_bit(lock,
							    old_heap_no));

osku's avatar
osku committed
2593 2594 2595
				/* NOTE that the old lock bitmap could be too
				small for the new heap number! */

2596
				lock_rec_add_to_queue(lock->type_mode, block,
2597 2598
						      new_heap_no,
						      lock->index, lock->trx);
osku's avatar
osku committed
2599

2600
				/* if (new_heap_no == PAGE_HEAP_NO_SUPREMUM
2601 2602
				&& lock_get_wait(lock)) {
				fprintf(stderr,
osku's avatar
osku committed
2603
				"---\n--\n!!!Lock reorg: supr type %lu\n",
2604
				lock->type_mode);
osku's avatar
osku committed
2605 2606 2607
				} */
			}

2608 2609
			if (UNIV_UNLIKELY
			    (new_heap_no == PAGE_HEAP_NO_SUPREMUM)) {
osku's avatar
osku committed
2610

2611
				ut_ad(old_heap_no == PAGE_HEAP_NO_SUPREMUM);
osku's avatar
osku committed
2612 2613 2614 2615 2616
				break;
			}

			page_cur_move_to_next(&cur1);
			page_cur_move_to_next(&cur2);
2617
		}
osku's avatar
osku committed
2618

2619
#ifdef UNIV_DEBUG
2620 2621 2622 2623 2624 2625 2626 2627 2628 2629
		{
			ulint	i = lock_rec_find_set_bit(lock);

			/* Check that all locks were moved. */
			if (UNIV_UNLIKELY(i != ULINT_UNDEFINED)) {
				fprintf(stderr,
					"lock_move_reorganize_page():"
					" %lu not moved in %p\n",
					(ulong) i, (void*) lock);
				ut_error;
2630 2631
			}
		}
2632
#endif /* UNIV_DEBUG */
osku's avatar
osku committed
2633 2634 2635 2636 2637 2638
	}

	lock_mutex_exit_kernel();

	mem_heap_free(heap);

2639
#ifdef UNIV_DEBUG_LOCK_VALIDATE
2640
	ut_ad(lock_rec_validate_page(buf_block_get_space(block),
2641
				     buf_block_get_zip_size(block),
2642
				     buf_block_get_page_no(block)));
2643
#endif
2644
}
osku's avatar
osku committed
2645

2646
/*************************************************************//**
osku's avatar
osku committed
2647 2648
Moves the explicit locks on user records to another page if a record
list end is moved to another page. */
2649
UNIV_INTERN
osku's avatar
osku committed
2650 2651 2652
void
lock_move_rec_list_end(
/*===================*/
2653 2654 2655
	const buf_block_t*	new_block,	/*!< in: index page to move to */
	const buf_block_t*	block,		/*!< in: index page */
	const rec_t*		rec)		/*!< in: record on page: this
2656
						is the first record moved */
osku's avatar
osku committed
2657 2658
{
	lock_t*		lock;
2659
	const ulint	comp	= page_rec_is_comp(rec);
osku's avatar
osku committed
2660 2661 2662 2663 2664 2665 2666 2667 2668

	lock_mutex_enter_kernel();

	/* Note: when we move locks from record to record, waiting locks
	and possible granted gap type locks behind them are enqueued in
	the original order, because new elements are inserted to a hash
	table to the end of the hash chain, and lock_rec_add_to_queue
	does not reuse locks if there are waiters in the queue. */

2669 2670
	for (lock = lock_rec_get_first_on_page(block); lock;
	     lock = lock_rec_get_next_on_page(lock)) {
2671 2672 2673
		page_cur_t	cur1;
		page_cur_t	cur2;
		const ulint	type_mode = lock->type_mode;
2674

2675
		page_cur_position(rec, block, &cur1);
osku's avatar
osku committed
2676 2677 2678 2679 2680

		if (page_cur_is_before_first(&cur1)) {
			page_cur_move_to_next(&cur1);
		}

2681
		page_cur_set_before_first(new_block, &cur2);
osku's avatar
osku committed
2682
		page_cur_move_to_next(&cur2);
2683

osku's avatar
osku committed
2684 2685 2686
		/* Copy lock requests on user records to new page and
		reset the lock bits on the old */

marko's avatar
marko committed
2687
		while (!page_cur_is_after_last(&cur1)) {
2688 2689
			ulint	heap_no;

2690
			if (comp) {
2691 2692
				heap_no = rec_get_heap_no_new(
					page_cur_get_rec(&cur1));
marko's avatar
marko committed
2693
			} else {
2694 2695
				heap_no = rec_get_heap_no_old(
					page_cur_get_rec(&cur1));
2696 2697 2698 2699
				ut_ad(!memcmp(page_cur_get_rec(&cur1),
					 page_cur_get_rec(&cur2),
					 rec_get_data_size_old(
						 page_cur_get_rec(&cur2))));
marko's avatar
marko committed
2700
			}
osku's avatar
osku committed
2701 2702 2703 2704

			if (lock_rec_get_nth_bit(lock, heap_no)) {
				lock_rec_reset_nth_bit(lock, heap_no);

2705
				if (UNIV_UNLIKELY(type_mode & LOCK_WAIT)) {
osku's avatar
osku committed
2706
					lock_reset_lock_and_trx_wait(lock);
2707
				}
osku's avatar
osku committed
2708

2709
				if (comp) {
2710 2711
					heap_no = rec_get_heap_no_new(
						page_cur_get_rec(&cur2));
marko's avatar
marko committed
2712
				} else {
2713 2714
					heap_no = rec_get_heap_no_old(
						page_cur_get_rec(&cur2));
marko's avatar
marko committed
2715 2716
				}

osku's avatar
osku committed
2717
				lock_rec_add_to_queue(type_mode,
2718
						      new_block, heap_no,
2719
						      lock->index, lock->trx);
osku's avatar
osku committed
2720 2721 2722 2723 2724 2725
			}

			page_cur_move_to_next(&cur1);
			page_cur_move_to_next(&cur2);
		}
	}
2726

osku's avatar
osku committed
2727 2728
	lock_mutex_exit_kernel();

2729
#ifdef UNIV_DEBUG_LOCK_VALIDATE
2730
	ut_ad(lock_rec_validate_page(buf_block_get_space(block),
2731
				     buf_block_get_zip_size(block),
2732 2733
				     buf_block_get_page_no(block)));
	ut_ad(lock_rec_validate_page(buf_block_get_space(new_block),
2734
				     buf_block_get_zip_size(block),
2735
				     buf_block_get_page_no(new_block)));
2736
#endif
2737
}
osku's avatar
osku committed
2738

2739
/*************************************************************//**
osku's avatar
osku committed
2740 2741
Moves the explicit locks on user records to another page if a record
list start is moved to another page. */
2742
UNIV_INTERN
osku's avatar
osku committed
2743 2744 2745
void
lock_move_rec_list_start(
/*=====================*/
2746 2747 2748
	const buf_block_t*	new_block,	/*!< in: index page to move to */
	const buf_block_t*	block,		/*!< in: index page */
	const rec_t*		rec,		/*!< in: record on page:
2749 2750
						this is the first
						record NOT copied */
2751
	const rec_t*		old_end)	/*!< in: old
2752 2753 2754 2755
						previous-to-last
						record on new_page
						before the records
						were copied */
osku's avatar
osku committed
2756 2757
{
	lock_t*		lock;
2758
	const ulint	comp	= page_rec_is_comp(rec);
osku's avatar
osku committed
2759

2760 2761
	ut_ad(block->frame == page_align(rec));
	ut_ad(new_block->frame == page_align(old_end));
osku's avatar
osku committed
2762 2763 2764

	lock_mutex_enter_kernel();

2765 2766
	for (lock = lock_rec_get_first_on_page(block); lock;
	     lock = lock_rec_get_next_on_page(lock)) {
2767 2768 2769
		page_cur_t	cur1;
		page_cur_t	cur2;
		const ulint	type_mode = lock->type_mode;
2770

2771
		page_cur_set_before_first(block, &cur1);
osku's avatar
osku committed
2772 2773
		page_cur_move_to_next(&cur1);

2774
		page_cur_position(old_end, new_block, &cur2);
osku's avatar
osku committed
2775 2776 2777 2778 2779 2780
		page_cur_move_to_next(&cur2);

		/* Copy lock requests on user records to new page and
		reset the lock bits on the old */

		while (page_cur_get_rec(&cur1) != rec) {
2781 2782
			ulint	heap_no;

2783
			if (comp) {
2784 2785
				heap_no = rec_get_heap_no_new(
					page_cur_get_rec(&cur1));
marko's avatar
marko committed
2786
			} else {
2787 2788
				heap_no = rec_get_heap_no_old(
					page_cur_get_rec(&cur1));
2789 2790 2791 2792 2793
				ut_ad(!memcmp(page_cur_get_rec(&cur1),
					      page_cur_get_rec(&cur2),
					      rec_get_data_size_old(
						      page_cur_get_rec(
							      &cur2))));
marko's avatar
marko committed
2794
			}
osku's avatar
osku committed
2795 2796 2797 2798

			if (lock_rec_get_nth_bit(lock, heap_no)) {
				lock_rec_reset_nth_bit(lock, heap_no);

2799
				if (UNIV_UNLIKELY(type_mode & LOCK_WAIT)) {
osku's avatar
osku committed
2800
					lock_reset_lock_and_trx_wait(lock);
2801
				}
osku's avatar
osku committed
2802

2803
				if (comp) {
2804 2805
					heap_no = rec_get_heap_no_new(
						page_cur_get_rec(&cur2));
marko's avatar
marko committed
2806
				} else {
2807 2808
					heap_no = rec_get_heap_no_old(
						page_cur_get_rec(&cur2));
marko's avatar
marko committed
2809 2810
				}

osku's avatar
osku committed
2811
				lock_rec_add_to_queue(type_mode,
2812
						      new_block, heap_no,
2813
						      lock->index, lock->trx);
osku's avatar
osku committed
2814 2815 2816 2817 2818 2819
			}

			page_cur_move_to_next(&cur1);
			page_cur_move_to_next(&cur2);
		}

2820 2821 2822 2823
#ifdef UNIV_DEBUG
		if (page_rec_is_supremum(rec)) {
			ulint	i;

2824 2825
			for (i = PAGE_HEAP_NO_USER_LOW;
			     i < lock_rec_get_n_bits(lock); i++) {
2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837
				if (UNIV_UNLIKELY
				    (lock_rec_get_nth_bit(lock, i))) {

					fprintf(stderr,
						"lock_move_rec_list_start():"
						" %lu not moved in %p\n",
						(ulong) i, (void*) lock);
					ut_error;
				}
			}
		}
#endif /* UNIV_DEBUG */
osku's avatar
osku committed
2838
	}
2839

osku's avatar
osku committed
2840
	lock_mutex_exit_kernel();
2841

2842
#ifdef UNIV_DEBUG_LOCK_VALIDATE
2843
	ut_ad(lock_rec_validate_page(buf_block_get_space(block),
2844
				     buf_block_get_zip_size(block),
2845
				     buf_block_get_page_no(block)));
2846
#endif
2847
}
osku's avatar
osku committed
2848

2849
/*************************************************************//**
osku's avatar
osku committed
2850
Updates the lock table when a page is split to the right. */
2851
UNIV_INTERN
osku's avatar
osku committed
2852 2853 2854
void
lock_update_split_right(
/*====================*/
2855 2856
	const buf_block_t*	right_block,	/*!< in: right page */
	const buf_block_t*	left_block)	/*!< in: left page */
osku's avatar
osku committed
2857
{
2858 2859
	ulint	heap_no = lock_get_min_heap_no(right_block);

osku's avatar
osku committed
2860 2861 2862 2863 2864
	lock_mutex_enter_kernel();

	/* Move the locks on the supremum of the left page to the supremum
	of the right page */

2865 2866
	lock_rec_move(right_block, left_block,
		      PAGE_HEAP_NO_SUPREMUM, PAGE_HEAP_NO_SUPREMUM);
2867

osku's avatar
osku committed
2868 2869 2870
	/* Inherit the locks to the supremum of left page from the successor
	of the infimum on right page */

2871 2872
	lock_rec_inherit_to_gap(left_block, right_block,
				PAGE_HEAP_NO_SUPREMUM, heap_no);
osku's avatar
osku committed
2873 2874

	lock_mutex_exit_kernel();
2875
}
osku's avatar
osku committed
2876

2877
/*************************************************************//**
osku's avatar
osku committed
2878
Updates the lock table when a page is merged to the right. */
2879
UNIV_INTERN
osku's avatar
osku committed
2880 2881 2882
void
lock_update_merge_right(
/*====================*/
2883
	const buf_block_t*	right_block,	/*!< in: right page to
2884
						which merged */
2885
	const rec_t*		orig_succ,	/*!< in: original
2886 2887 2888
						successor of infimum
						on the right page
						before merge */
2889
	const buf_block_t*	left_block)	/*!< in: merged index
2890 2891
						page which will be
						discarded */
osku's avatar
osku committed
2892 2893
{
	lock_mutex_enter_kernel();
2894

osku's avatar
osku committed
2895 2896 2897 2898
	/* Inherit the locks from the supremum of the left page to the
	original successor of infimum on the right page, to which the left
	page was merged */

2899 2900 2901
	lock_rec_inherit_to_gap(right_block, left_block,
				page_rec_get_heap_no(orig_succ),
				PAGE_HEAP_NO_SUPREMUM);
osku's avatar
osku committed
2902 2903 2904 2905

	/* Reset the locks on the supremum of the left page, releasing
	waiting transactions */

2906 2907
	lock_rec_reset_and_release_wait(left_block,
					PAGE_HEAP_NO_SUPREMUM);
2908

2909
	lock_rec_free_all_from_discard_page(left_block);
osku's avatar
osku committed
2910 2911 2912 2913

	lock_mutex_exit_kernel();
}

2914
/*************************************************************//**
osku's avatar
osku committed
2915 2916 2917 2918 2919 2920
Updates the lock table when the root page is copied to another in
btr_root_raise_and_insert. Note that we leave lock structs on the
root page, even though they do not make sense on other than leaf
pages: the reason is that in a pessimistic update the infimum record
of the root page will act as a dummy carrier of the locks of the record
to be updated. */
2921
UNIV_INTERN
osku's avatar
osku committed
2922 2923 2924
void
lock_update_root_raise(
/*===================*/
2925 2926
	const buf_block_t*	block,	/*!< in: index page to which copied */
	const buf_block_t*	root)	/*!< in: root page */
osku's avatar
osku committed
2927 2928 2929 2930
{
	lock_mutex_enter_kernel();

	/* Move the locks on the supremum of the root to the supremum
2931
	of block */
osku's avatar
osku committed
2932

2933 2934
	lock_rec_move(block, root,
		      PAGE_HEAP_NO_SUPREMUM, PAGE_HEAP_NO_SUPREMUM);
osku's avatar
osku committed
2935 2936 2937
	lock_mutex_exit_kernel();
}

2938
/*************************************************************//**
osku's avatar
osku committed
2939 2940
Updates the lock table when a page is copied to another and the original page
is removed from the chain of leaf pages, except if page is the root! */
2941
UNIV_INTERN
osku's avatar
osku committed
2942 2943 2944
void
lock_update_copy_and_discard(
/*=========================*/
2945
	const buf_block_t*	new_block,	/*!< in: index page to
2946
						which copied */
2947
	const buf_block_t*	block)		/*!< in: index page;
2948
						NOT the root! */
osku's avatar
osku committed
2949 2950 2951 2952 2953 2954
{
	lock_mutex_enter_kernel();

	/* Move the locks on the supremum of the old page to the supremum
	of new_page */

2955 2956 2957
	lock_rec_move(new_block, block,
		      PAGE_HEAP_NO_SUPREMUM, PAGE_HEAP_NO_SUPREMUM);
	lock_rec_free_all_from_discard_page(block);
osku's avatar
osku committed
2958 2959 2960 2961

	lock_mutex_exit_kernel();
}

2962
/*************************************************************//**
osku's avatar
osku committed
2963
Updates the lock table when a page is split to the left. */
2964
UNIV_INTERN
osku's avatar
osku committed
2965 2966 2967
void
lock_update_split_left(
/*===================*/
2968 2969
	const buf_block_t*	right_block,	/*!< in: right page */
	const buf_block_t*	left_block)	/*!< in: left page */
osku's avatar
osku committed
2970
{
2971 2972
	ulint	heap_no = lock_get_min_heap_no(right_block);

osku's avatar
osku committed
2973
	lock_mutex_enter_kernel();
2974

osku's avatar
osku committed
2975 2976 2977
	/* Inherit the locks to the supremum of the left page from the
	successor of the infimum on the right page */

2978 2979
	lock_rec_inherit_to_gap(left_block, right_block,
				PAGE_HEAP_NO_SUPREMUM, heap_no);
osku's avatar
osku committed
2980 2981 2982 2983

	lock_mutex_exit_kernel();
}

2984
/*************************************************************//**
osku's avatar
osku committed
2985
Updates the lock table when a page is merged to the left. */
2986
UNIV_INTERN
osku's avatar
osku committed
2987 2988 2989
void
lock_update_merge_left(
/*===================*/
2990
	const buf_block_t*	left_block,	/*!< in: left page to
2991
						which merged */
2992
	const rec_t*		orig_pred,	/*!< in: original predecessor
2993 2994
						of supremum on the left page
						before merge */
2995
	const buf_block_t*	right_block)	/*!< in: merged index page
2996
						which will be discarded */
osku's avatar
osku committed
2997
{
2998 2999
	const rec_t*	left_next_rec;

3000
	ut_ad(left_block->frame == page_align(orig_pred));
3001

osku's avatar
osku committed
3002 3003
	lock_mutex_enter_kernel();

3004
	left_next_rec = page_rec_get_next_const(orig_pred);
osku's avatar
osku committed
3005

3006
	if (!page_rec_is_supremum(left_next_rec)) {
osku's avatar
osku committed
3007 3008 3009 3010

		/* Inherit the locks on the supremum of the left page to the
		first record which was moved from the right page */

3011 3012 3013
		lock_rec_inherit_to_gap(left_block, left_block,
					page_rec_get_heap_no(left_next_rec),
					PAGE_HEAP_NO_SUPREMUM);
osku's avatar
osku committed
3014 3015 3016 3017

		/* Reset the locks on the supremum of the left page,
		releasing waiting transactions */

3018 3019
		lock_rec_reset_and_release_wait(left_block,
						PAGE_HEAP_NO_SUPREMUM);
osku's avatar
osku committed
3020 3021 3022 3023
	}

	/* Move the locks from the supremum of right page to the supremum
	of the left page */
3024

3025 3026
	lock_rec_move(left_block, right_block,
		      PAGE_HEAP_NO_SUPREMUM, PAGE_HEAP_NO_SUPREMUM);
osku's avatar
osku committed
3027

3028
	lock_rec_free_all_from_discard_page(right_block);
osku's avatar
osku committed
3029 3030 3031 3032

	lock_mutex_exit_kernel();
}

3033
/*************************************************************//**
osku's avatar
osku committed
3034 3035
Resets the original locks on heir and replaces them with gap type locks
inherited from rec. */
3036
UNIV_INTERN
osku's avatar
osku committed
3037 3038 3039
void
lock_rec_reset_and_inherit_gap_locks(
/*=================================*/
3040
	const buf_block_t*	heir_block,	/*!< in: block containing the
3041
						record which inherits */
3042
	const buf_block_t*	block,		/*!< in: block containing the
3043 3044 3045
						record from which inherited;
						does NOT reset the locks on
						this record */
3046
	ulint			heir_heap_no,	/*!< in: heap_no of the
3047
						inheriting record */
3048
	ulint			heap_no)	/*!< in: heap_no of the
3049
						donating record */
osku's avatar
osku committed
3050
{
3051
	mutex_enter(&kernel_mutex);
osku's avatar
osku committed
3052

3053
	lock_rec_reset_and_release_wait(heir_block, heir_heap_no);
3054

3055
	lock_rec_inherit_to_gap(heir_block, block, heir_heap_no, heap_no);
osku's avatar
osku committed
3056

3057
	mutex_exit(&kernel_mutex);
osku's avatar
osku committed
3058 3059
}

3060
/*************************************************************//**
osku's avatar
osku committed
3061
Updates the lock table when a page is discarded. */
3062
UNIV_INTERN
osku's avatar
osku committed
3063 3064 3065
void
lock_update_discard(
/*================*/
3066
	const buf_block_t*	heir_block,	/*!< in: index page
3067
						which will inherit the locks */
3068
	ulint			heir_heap_no,	/*!< in: heap_no of the record
3069
						which will inherit the locks */
3070
	const buf_block_t*	block)		/*!< in: index page
3071
						which will be discarded */
osku's avatar
osku committed
3072
{
3073 3074 3075
	const page_t*	page = block->frame;
	const rec_t*	rec;
	ulint		heap_no;
osku's avatar
osku committed
3076 3077

	lock_mutex_enter_kernel();
3078

3079
	if (!lock_rec_get_first_on_page(block)) {
osku's avatar
osku committed
3080 3081 3082 3083 3084 3085
		/* No locks exist on page, nothing to do */

		lock_mutex_exit_kernel();

		return;
	}
3086

osku's avatar
osku committed
3087 3088 3089
	/* Inherit all the locks on the page to the record and reset all
	the locks on the page */

3090 3091
	if (page_is_comp(page)) {
		rec = page + PAGE_NEW_INFIMUM;
osku's avatar
osku committed
3092

3093 3094
		do {
			heap_no = rec_get_heap_no_new(rec);
osku's avatar
osku committed
3095

3096 3097
			lock_rec_inherit_to_gap(heir_block, block,
						heir_heap_no, heap_no);
osku's avatar
osku committed
3098

3099
			lock_rec_reset_and_release_wait(block, heap_no);
osku's avatar
osku committed
3100

3101
			rec = page + rec_get_next_offs(rec, TRUE);
3102
		} while (heap_no != PAGE_HEAP_NO_SUPREMUM);
3103 3104
	} else {
		rec = page + PAGE_OLD_INFIMUM;
osku's avatar
osku committed
3105

3106 3107 3108 3109 3110
		do {
			heap_no = rec_get_heap_no_old(rec);

			lock_rec_inherit_to_gap(heir_block, block,
						heir_heap_no, heap_no);
3111

3112 3113 3114
			lock_rec_reset_and_release_wait(block, heap_no);

			rec = page + rec_get_next_offs(rec, FALSE);
3115
		} while (heap_no != PAGE_HEAP_NO_SUPREMUM);
osku's avatar
osku committed
3116 3117
	}

3118
	lock_rec_free_all_from_discard_page(block);
osku's avatar
osku committed
3119 3120 3121 3122

	lock_mutex_exit_kernel();
}

3123
/*************************************************************//**
osku's avatar
osku committed
3124
Updates the lock table when a new user record is inserted. */
3125
UNIV_INTERN
osku's avatar
osku committed
3126 3127 3128
void
lock_update_insert(
/*===============*/
3129 3130
	const buf_block_t*	block,	/*!< in: buffer block containing rec */
	const rec_t*		rec)	/*!< in: the inserted record */
osku's avatar
osku committed
3131
{
3132 3133 3134
	ulint	receiver_heap_no;
	ulint	donator_heap_no;

3135
	ut_ad(block->frame == page_align(rec));
osku's avatar
osku committed
3136 3137 3138 3139

	/* Inherit the gap-locking locks for rec, in gap mode, from the next
	record */

3140 3141 3142
	if (page_rec_is_comp(rec)) {
		receiver_heap_no = rec_get_heap_no_new(rec);
		donator_heap_no = rec_get_heap_no_new(
3143
			page_rec_get_next_low(rec, TRUE));
3144 3145 3146
	} else {
		receiver_heap_no = rec_get_heap_no_old(rec);
		donator_heap_no = rec_get_heap_no_old(
3147
			page_rec_get_next_low(rec, FALSE));
3148
	}
osku's avatar
osku committed
3149

3150 3151 3152
	lock_mutex_enter_kernel();
	lock_rec_inherit_to_gap_if_gap_lock(block,
					    receiver_heap_no, donator_heap_no);
osku's avatar
osku committed
3153 3154 3155
	lock_mutex_exit_kernel();
}

3156
/*************************************************************//**
osku's avatar
osku committed
3157
Updates the lock table when a record is removed. */
3158
UNIV_INTERN
osku's avatar
osku committed
3159 3160 3161
void
lock_update_delete(
/*===============*/
3162 3163
	const buf_block_t*	block,	/*!< in: buffer block containing rec */
	const rec_t*		rec)	/*!< in: the record to be removed */
osku's avatar
osku committed
3164
{
3165 3166 3167 3168
	const page_t*	page = block->frame;
	ulint		heap_no;
	ulint		next_heap_no;

marko's avatar
marko committed
3169 3170
	ut_ad(page == page_align(rec));

3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182
	if (page_is_comp(page)) {
		heap_no = rec_get_heap_no_new(rec);
		next_heap_no = rec_get_heap_no_new(page
						   + rec_get_next_offs(rec,
								       TRUE));
	} else {
		heap_no = rec_get_heap_no_old(rec);
		next_heap_no = rec_get_heap_no_old(page
						   + rec_get_next_offs(rec,
								       FALSE));
	}

osku's avatar
osku committed
3183 3184 3185 3186
	lock_mutex_enter_kernel();

	/* Let the next record inherit the locks from rec, in gap mode */

3187
	lock_rec_inherit_to_gap(block, block, next_heap_no, heap_no);
osku's avatar
osku committed
3188 3189 3190

	/* Reset the lock bits on rec and release waiting transactions */

3191
	lock_rec_reset_and_release_wait(block, heap_no);
osku's avatar
osku committed
3192 3193 3194

	lock_mutex_exit_kernel();
}
3195

3196
/*********************************************************************//**
osku's avatar
osku committed
3197 3198 3199 3200 3201 3202
Stores on the page infimum record the explicit locks of another record.
This function is used to store the lock state of a record when it is
updated and the size of the record changes in the update. The record
is moved in such an update, perhaps to another page. The infimum record
acts as a dummy carrier record, taking care of lock releases while the
actual record is being moved. */
3203
UNIV_INTERN
osku's avatar
osku committed
3204 3205 3206
void
lock_rec_store_on_page_infimum(
/*===========================*/
3207 3208
	const buf_block_t*	block,	/*!< in: buffer block containing rec */
	const rec_t*		rec)	/*!< in: record whose lock state
3209 3210 3211 3212
					is stored on the infimum
					record of the same page; lock
					bits are reset on the
					record */
osku's avatar
osku committed
3213
{
3214 3215
	ulint	heap_no = page_rec_get_heap_no(rec);

3216
	ut_ad(block->frame == page_align(rec));
osku's avatar
osku committed
3217 3218

	lock_mutex_enter_kernel();
3219

3220
	lock_rec_move(block, block, PAGE_HEAP_NO_INFIMUM, heap_no);
osku's avatar
osku committed
3221

3222
	lock_mutex_exit_kernel();
osku's avatar
osku committed
3223 3224
}

3225
/*********************************************************************//**
osku's avatar
osku committed
3226 3227
Restores the state of explicit lock requests on a single record, where the
state was stored on the infimum of the page. */
3228
UNIV_INTERN
osku's avatar
osku committed
3229 3230 3231
void
lock_rec_restore_from_page_infimum(
/*===============================*/
3232 3233
	const buf_block_t*	block,	/*!< in: buffer block containing rec */
	const rec_t*		rec,	/*!< in: record whose lock state
marko's avatar
marko committed
3234
					is restored */
3235
	const buf_block_t*	donator)/*!< in: page (rec is not
3236 3237 3238 3239
					necessarily on this page)
					whose infimum stored the lock
					state; lock bits are reset on
					the infimum */
osku's avatar
osku committed
3240
{
3241 3242
	ulint	heap_no = page_rec_get_heap_no(rec);

osku's avatar
osku committed
3243 3244
	lock_mutex_enter_kernel();

3245
	lock_rec_move(block, donator, heap_no, PAGE_HEAP_NO_INFIMUM);
3246

osku's avatar
osku committed
3247 3248 3249 3250 3251
	lock_mutex_exit_kernel();
}

/*=========== DEADLOCK CHECKING ======================================*/

3252
/********************************************************************//**
3253
Checks if a lock request results in a deadlock.
3254 3255 3256
@return TRUE if a deadlock was detected and we chose trx as a victim;
FALSE if no deadlock, or there was a deadlock, but we chose other
transaction(s) as victim(s) */
osku's avatar
osku committed
3257 3258 3259 3260
static
ibool
lock_deadlock_occurs(
/*=================*/
3261 3262
	lock_t*	lock,	/*!< in: lock the transaction is requesting */
	trx_t*	trx)	/*!< in: transaction */
osku's avatar
osku committed
3263 3264 3265 3266 3267 3268 3269
{
	dict_table_t*	table;
	dict_index_t*	index;
	trx_t*		mark_trx;
	ulint		ret;
	ulint		cost	= 0;

3270 3271
	ut_ad(trx);
	ut_ad(lock);
osku's avatar
osku committed
3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293
	ut_ad(mutex_own(&kernel_mutex));
retry:
	/* We check that adding this trx to the waits-for graph
	does not produce a cycle. First mark all active transactions
	with 0: */

	mark_trx = UT_LIST_GET_FIRST(trx_sys->trx_list);

	while (mark_trx) {
		mark_trx->deadlock_mark = 0;
		mark_trx = UT_LIST_GET_NEXT(trx_list, mark_trx);
	}

	ret = lock_deadlock_recursive(trx, trx, lock, &cost, 0);

	if (ret == LOCK_VICTIM_IS_OTHER) {
		/* We chose some other trx as a victim: retry if there still
		is a deadlock */

		goto retry;
	}

marko's avatar
marko committed
3294
	if (UNIV_UNLIKELY(ret == LOCK_VICTIM_IS_START)) {
3295
		if (lock_get_type_low(lock) & LOCK_TABLE) {
osku's avatar
osku committed
3296 3297 3298 3299 3300 3301 3302 3303 3304 3305
			table = lock->un_member.tab_lock.table;
			index = NULL;
		} else {
			index = lock->index;
			table = index->table;
		}

		lock_deadlock_found = TRUE;

		fputs("*** WE ROLL BACK TRANSACTION (2)\n",
3306
		      lock_latest_err_file);
osku's avatar
osku committed
3307 3308 3309

		return(TRUE);
	}
3310

osku's avatar
osku committed
3311 3312 3313
	return(FALSE);
}

3314
/********************************************************************//**
3315
Looks recursively for a deadlock.
3316 3317 3318 3319 3320
@return 0 if no deadlock found, LOCK_VICTIM_IS_START if there was a
deadlock and we chose 'start' as the victim, LOCK_VICTIM_IS_OTHER if a
deadlock was found and we chose some other trx as a victim: we must do
the search again in this last case because there may be another
deadlock! */
osku's avatar
osku committed
3321 3322 3323 3324
static
ulint
lock_deadlock_recursive(
/*====================*/
3325 3326
	trx_t*	start,		/*!< in: recursion starting point */
	trx_t*	trx,		/*!< in: a transaction waiting for a lock */
3327
	lock_t*	wait_lock,	/*!< in: lock that is waiting to be granted */
3328
	ulint*	cost,		/*!< in/out: number of calculation steps thus
osku's avatar
osku committed
3329 3330
				far: if this exceeds LOCK_MAX_N_STEPS_...
				we return LOCK_VICTIM_IS_START */
3331
	ulint	depth)		/*!< in: recursion depth: if this exceeds
osku's avatar
osku committed
3332 3333 3334
				LOCK_MAX_DEPTH_IN_DEADLOCK_CHECK, we
				return LOCK_VICTIM_IS_START */
{
3335
	ulint	ret;
osku's avatar
osku committed
3336 3337
	lock_t*	lock;
	trx_t*	lock_trx;
3338
	ulint	heap_no		= ULINT_UNDEFINED;
3339

3340 3341 3342
	ut_a(trx);
	ut_a(start);
	ut_a(wait_lock);
osku's avatar
osku committed
3343
	ut_ad(mutex_own(&kernel_mutex));
3344

osku's avatar
osku committed
3345 3346 3347 3348 3349 3350 3351 3352 3353
	if (trx->deadlock_mark == 1) {
		/* We have already exhaustively searched the subtree starting
		from this trx */

		return(0);
	}

	*cost = *cost + 1;

3354
	if (lock_get_type_low(wait_lock) == LOCK_REC) {
3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376
		ulint		space;
		ulint		page_no;

		heap_no = lock_rec_find_set_bit(wait_lock);
		ut_a(heap_no != ULINT_UNDEFINED);

		space = wait_lock->un_member.rec_lock.space;
		page_no = wait_lock->un_member.rec_lock.page_no;

		lock = lock_rec_get_first_on_page_addr(space, page_no);

		/* Position the iterator on the first matching record lock. */
		while (lock != NULL
		       && lock != wait_lock
		       && !lock_rec_get_nth_bit(lock, heap_no)) {

			lock = lock_rec_get_next_on_page(lock);
		}

		if (lock == wait_lock) {
			lock = NULL;
		}
osku's avatar
osku committed
3377

3378
		ut_ad(lock == NULL || lock_rec_get_nth_bit(lock, heap_no));
osku's avatar
osku committed
3379

3380 3381
	} else {
		lock = wait_lock;
osku's avatar
osku committed
3382 3383 3384 3385 3386
	}

	/* Look at the locks ahead of wait_lock in the lock queue */

	for (;;) {
3387 3388
		/* Get previous table lock. */
		if (heap_no == ULINT_UNDEFINED) {
osku's avatar
osku committed
3389

3390 3391
			lock = UT_LIST_GET_PREV(
				un_member.tab_lock.locks, lock);
osku's avatar
osku committed
3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402
		}

		if (lock == NULL) {
			/* We can mark this subtree as searched */
			trx->deadlock_mark = 1;

			return(FALSE);
		}

		if (lock_has_to_wait(wait_lock, lock)) {

3403 3404 3405 3406
			ibool	too_far
				= depth > LOCK_MAX_DEPTH_IN_DEADLOCK_CHECK
				|| *cost > LOCK_MAX_N_STEPS_IN_DEADLOCK_CHECK;

osku's avatar
osku committed
3407 3408
			lock_trx = lock->trx;

3409 3410
			if (lock_trx == start || too_far) {

osku's avatar
osku committed
3411
				/* We came back to the recursion starting
3412 3413 3414
				point: a deadlock detected; or we have
				searched the waits-for graph too long */

osku's avatar
osku committed
3415
				FILE*	ef = lock_latest_err_file;
3416

osku's avatar
osku committed
3417 3418 3419 3420 3421 3422 3423
				rewind(ef);
				ut_print_timestamp(ef);

				fputs("\n*** (1) TRANSACTION:\n", ef);

				trx_print(ef, wait_lock->trx, 3000);

3424 3425
				fputs("*** (1) WAITING FOR THIS LOCK"
				      " TO BE GRANTED:\n", ef);
3426

3427
				if (lock_get_type_low(wait_lock) == LOCK_REC) {
osku's avatar
osku committed
3428 3429 3430 3431
					lock_rec_print(ef, wait_lock);
				} else {
					lock_table_print(ef, wait_lock);
				}
3432

osku's avatar
osku committed
3433 3434 3435 3436 3437
				fputs("*** (2) TRANSACTION:\n", ef);

				trx_print(ef, lock->trx, 3000);

				fputs("*** (2) HOLDS THE LOCK(S):\n", ef);
3438

3439
				if (lock_get_type_low(lock) == LOCK_REC) {
osku's avatar
osku committed
3440 3441 3442 3443
					lock_rec_print(ef, lock);
				} else {
					lock_table_print(ef, lock);
				}
3444

3445 3446
				fputs("*** (2) WAITING FOR THIS LOCK"
				      " TO BE GRANTED:\n", ef);
3447

3448
				if (lock_get_type_low(start->wait_lock)
3449
				    == LOCK_REC) {
osku's avatar
osku committed
3450 3451 3452 3453 3454 3455
					lock_rec_print(ef, start->wait_lock);
				} else {
					lock_table_print(ef, start->wait_lock);
				}
#ifdef UNIV_DEBUG
				if (lock_print_waits) {
3456 3457 3458
					fputs("Deadlock detected"
					      " or too long search\n",
					      stderr);
osku's avatar
osku committed
3459 3460
				}
#endif /* UNIV_DEBUG */
3461 3462 3463 3464 3465 3466 3467 3468 3469
				if (too_far) {

					fputs("TOO DEEP OR LONG SEARCH"
					      " IN THE LOCK TABLE"
					      " WAITS-FOR GRAPH\n", ef);

					return(LOCK_VICTIM_IS_START);
				}

3470 3471
				if (trx_weight_cmp(wait_lock->trx,
						   start) >= 0) {
osku's avatar
osku committed
3472 3473 3474 3475 3476 3477
					/* Our recursion starting point
					transaction is 'smaller', let us
					choose 'start' as the victim and roll
					back it */

					return(LOCK_VICTIM_IS_START);
3478
				}
osku's avatar
osku committed
3479 3480 3481 3482 3483 3484

				lock_deadlock_found = TRUE;

				/* Let us choose the transaction of wait_lock
				as a victim to try to avoid deadlocking our
				recursion starting point transaction */
3485

osku's avatar
osku committed
3486
				fputs("*** WE ROLL BACK TRANSACTION (1)\n",
3487
				      ef);
3488

osku's avatar
osku committed
3489
				wait_lock->trx->was_chosen_as_deadlock_victim
3490
					= TRUE;
3491

osku's avatar
osku committed
3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502
				lock_cancel_waiting_and_release(wait_lock);

				/* Since trx and wait_lock are no longer
				in the waits-for graph, we can return FALSE;
				note that our selective algorithm can choose
				several transactions as victims, but still
				we may end up rolling back also the recursion
				starting point transaction! */

				return(LOCK_VICTIM_IS_OTHER);
			}
3503

osku's avatar
osku committed
3504 3505 3506 3507 3508 3509
			if (lock_trx->que_state == TRX_QUE_LOCK_WAIT) {

				/* Another trx ahead has requested lock	in an
				incompatible mode, and is itself waiting for
				a lock */

3510 3511 3512
				ret = lock_deadlock_recursive(
					start, lock_trx,
					lock_trx->wait_lock, cost, depth + 1);
3513

osku's avatar
osku committed
3514 3515 3516 3517 3518 3519
				if (ret != 0) {

					return(ret);
				}
			}
		}
3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534
		/* Get the next record lock to check. */
		if (heap_no != ULINT_UNDEFINED) {

			ut_a(lock != NULL);

			do {
				lock = lock_rec_get_next_on_page(lock);
			} while (lock != NULL
				&& lock != wait_lock
				&& !lock_rec_get_nth_bit(lock, heap_no));

			if (lock == wait_lock) {
				lock = NULL;
			}
		}
osku's avatar
osku committed
3535 3536 3537 3538 3539
	}/* end of the 'for (;;)'-loop */
}

/*========================= TABLE LOCKS ==============================*/

3540
/*********************************************************************//**
osku's avatar
osku committed
3541
Creates a table lock object and adds it as the last in the lock queue
3542 3543
of the table. Does NOT check for deadlocks or lock compatibility.
@return	own: new lock object */
osku's avatar
osku committed
3544 3545 3546 3547
UNIV_INLINE
lock_t*
lock_table_create(
/*==============*/
3548 3549
	dict_table_t*	table,	/*!< in: database table in dictionary cache */
	ulint		type_mode,/*!< in: lock mode possibly ORed with
osku's avatar
osku committed
3550
				LOCK_WAIT */
3551
	trx_t*		trx)	/*!< in: trx */
osku's avatar
osku committed
3552 3553 3554 3555 3556 3557
{
	lock_t*	lock;

	ut_ad(table && trx);
	ut_ad(mutex_own(&kernel_mutex));

3558 3559 3560 3561
	if ((type_mode & LOCK_MODE_MASK) == LOCK_AUTO_INC) {
		++table->n_waiting_or_granted_auto_inc_locks;
	}

sunny's avatar
sunny committed
3562 3563 3564
	/* For AUTOINC locking we reuse the lock instance only if
	there is no wait involved else we allocate the waiting lock
	from the transaction lock heap. */
osku's avatar
osku committed
3565 3566
	if (type_mode == LOCK_AUTO_INC) {

sunny's avatar
sunny committed
3567 3568 3569
		lock = table->autoinc_lock;

		table->autoinc_trx = trx;
osku's avatar
osku committed
3570

sunny's avatar
sunny committed
3571
		ib_vector_push(trx->autoinc_locks, lock);
osku's avatar
osku committed
3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584
	} else {
		lock = mem_heap_alloc(trx->lock_heap, sizeof(lock_t));
	}

	UT_LIST_ADD_LAST(trx_locks, trx->trx_locks, lock);

	lock->type_mode = type_mode | LOCK_TABLE;
	lock->trx = trx;

	lock->un_member.tab_lock.table = table;

	UT_LIST_ADD_LAST(un_member.tab_lock.locks, table->locks, lock);

3585
	if (UNIV_UNLIKELY(type_mode & LOCK_WAIT)) {
osku's avatar
osku committed
3586 3587 3588 3589 3590 3591 3592

		lock_set_lock_and_trx_wait(lock, trx);
	}

	return(lock);
}

3593
/*************************************************************//**
osku's avatar
osku committed
3594 3595 3596 3597 3598 3599 3600
Removes a table lock request from the queue and the trx list of locks;
this is a low-level function which does NOT check if waiting requests
can now be granted. */
UNIV_INLINE
void
lock_table_remove_low(
/*==================*/
3601
	lock_t*	lock)	/*!< in: table lock */
osku's avatar
osku committed
3602 3603
{
	trx_t*		trx;
sunny's avatar
sunny committed
3604
	dict_table_t*	table;
osku's avatar
osku committed
3605 3606 3607 3608

	ut_ad(mutex_own(&kernel_mutex));

	trx = lock->trx;
sunny's avatar
sunny committed
3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628
	table = lock->un_member.tab_lock.table;

	/* Remove the table from the transaction's AUTOINC vector, if
	the lock that is being release is an AUTOINC lock. */
	if (lock_get_mode(lock) == LOCK_AUTO_INC) {

		/* The table's AUTOINC lock can get transferred to
		another transaction before we get here. */
		if (table->autoinc_trx == trx) {
			table->autoinc_trx = NULL;
		}

		/* The locks must be freed in the reverse order from
		the one in which they were acquired. This is to avoid
		traversing the AUTOINC lock vector unnecessarily. 

		We only store locks that were granted in the
		trx->autoinc_locks vector (see lock_table_create()
		and lock_grant()). Therefore it can be empty and we
		need to check for that. */
osku's avatar
osku committed
3629

3630 3631
		if (!lock_get_wait(lock)
		    && !ib_vector_is_empty(trx->autoinc_locks)) {
sunny's avatar
sunny committed
3632 3633 3634 3635 3636
			lock_t*	autoinc_lock;

			autoinc_lock = ib_vector_pop(trx->autoinc_locks);
			ut_a(autoinc_lock == lock);
		}
3637 3638 3639

		ut_a(table->n_waiting_or_granted_auto_inc_locks > 0);
		--table->n_waiting_or_granted_auto_inc_locks;
osku's avatar
osku committed
3640 3641 3642 3643
	}

	UT_LIST_REMOVE(trx_locks, trx->trx_locks, lock);
	UT_LIST_REMOVE(un_member.tab_lock.locks, table->locks, lock);
3644
}
osku's avatar
osku committed
3645

3646
/*********************************************************************//**
osku's avatar
osku committed
3647
Enqueues a waiting request for a table lock which cannot be granted
3648
immediately. Checks for deadlocks.
3649 3650 3651 3652
@return DB_LOCK_WAIT, DB_DEADLOCK, or DB_QUE_THR_SUSPENDED, or
DB_SUCCESS; DB_SUCCESS means that there was a deadlock, but another
transaction was chosen as a victim, and we got the lock immediately:
no need to wait then */
osku's avatar
osku committed
3653 3654 3655 3656
static
ulint
lock_table_enqueue_waiting(
/*=======================*/
3657
	ulint		mode,	/*!< in: lock mode this transaction is
osku's avatar
osku committed
3658
				requesting */
3659 3660
	dict_table_t*	table,	/*!< in: table */
	que_thr_t*	thr)	/*!< in: query thread */
osku's avatar
osku committed
3661 3662 3663
{
	lock_t*	lock;
	trx_t*	trx;
3664

osku's avatar
osku committed
3665
	ut_ad(mutex_own(&kernel_mutex));
3666

osku's avatar
osku committed
3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678
	/* Test if there already is some other reason to suspend thread:
	we do not enqueue a lock request if the query thread should be
	stopped anyway */

	if (que_thr_stop(thr)) {
		ut_error;

		return(DB_QUE_THR_SUSPENDED);
	}

	trx = thr_get_trx(thr);

3679 3680 3681 3682 3683
	switch (trx_get_dict_operation(trx)) {
	case TRX_DICT_OP_NONE:
		break;
	case TRX_DICT_OP_TABLE:
	case TRX_DICT_OP_INDEX:
osku's avatar
osku committed
3684
		ut_print_timestamp(stderr);
3685 3686 3687
		fputs("  InnoDB: Error: a table lock wait happens"
		      " in a dictionary operation!\n"
		      "InnoDB: Table name ", stderr);
3688
		ut_print_name(stderr, trx, TRUE, table->name);
osku's avatar
osku committed
3689
		fputs(".\n"
3690 3691 3692
		      "InnoDB: Submit a detailed bug report"
		      " to http://bugs.mysql.com\n",
		      stderr);
osku's avatar
osku committed
3693
	}
3694

osku's avatar
osku committed
3695 3696 3697 3698 3699 3700 3701 3702 3703
	/* Enqueue the lock request that will wait to be granted */

	lock = lock_table_create(table, mode | LOCK_WAIT, trx);

	/* Check if a deadlock occurs: if yes, remove the lock request and
	return an error code */

	if (lock_deadlock_occurs(lock, trx)) {

3704 3705
		/* The order here is important, we don't want to
		lose the state of the lock before calling remove. */
osku's avatar
osku committed
3706
		lock_table_remove_low(lock);
3707
		lock_reset_lock_and_trx_wait(lock);
osku's avatar
osku committed
3708 3709 3710 3711 3712 3713 3714

		return(DB_DEADLOCK);
	}

	if (trx->wait_lock == NULL) {
		/* Deadlock resolution chose another transaction as a victim,
		and we accidentally got our lock granted! */
3715

osku's avatar
osku committed
3716 3717
		return(DB_SUCCESS);
	}
3718

osku's avatar
osku committed
3719 3720 3721 3722 3723 3724 3725 3726 3727
	trx->que_state = TRX_QUE_LOCK_WAIT;
	trx->was_chosen_as_deadlock_victim = FALSE;
	trx->wait_started = time(NULL);

	ut_a(que_thr_stop(thr));

	return(DB_LOCK_WAIT);
}

3728
/*********************************************************************//**
osku's avatar
osku committed
3729 3730 3731 3732 3733 3734
Checks if other transactions have an incompatible mode lock request in
the lock queue. */
UNIV_INLINE
ibool
lock_table_other_has_incompatible(
/*==============================*/
3735
	trx_t*		trx,	/*!< in: transaction, or NULL if all
osku's avatar
osku committed
3736
				transactions should be included */
3737
	ulint		wait,	/*!< in: LOCK_WAIT if also waiting locks are
osku's avatar
osku committed
3738
				taken into account, or 0 if not */
3739 3740
	dict_table_t*	table,	/*!< in: table */
	enum lock_mode	mode)	/*!< in: lock mode */
osku's avatar
osku committed
3741 3742 3743 3744 3745 3746 3747 3748 3749
{
	lock_t*	lock;

	ut_ad(mutex_own(&kernel_mutex));

	lock = UT_LIST_GET_LAST(table->locks);

	while (lock != NULL) {

3750
		if ((lock->trx != trx)
3751 3752
		    && (!lock_mode_compatible(lock_get_mode(lock), mode))
		    && (wait || !(lock_get_wait(lock)))) {
osku's avatar
osku committed
3753

3754
			return(TRUE);
osku's avatar
osku committed
3755 3756 3757 3758 3759 3760 3761 3762
		}

		lock = UT_LIST_GET_PREV(un_member.tab_lock.locks, lock);
	}

	return(FALSE);
}

3763
/*********************************************************************//**
osku's avatar
osku committed
3764
Locks the specified database table in the mode given. If the lock cannot
3765 3766
be granted immediately, the query thread is put to wait.
@return	DB_SUCCESS, DB_LOCK_WAIT, DB_DEADLOCK, or DB_QUE_THR_SUSPENDED */
3767
UNIV_INTERN
osku's avatar
osku committed
3768 3769 3770
ulint
lock_table(
/*=======*/
3771
	ulint		flags,	/*!< in: if BTR_NO_LOCKING_FLAG bit is set,
osku's avatar
osku committed
3772
				does nothing */
3773 3774 3775
	dict_table_t*	table,	/*!< in: database table in dictionary cache */
	enum lock_mode	mode,	/*!< in: lock mode */
	que_thr_t*	thr)	/*!< in: query thread */
osku's avatar
osku committed
3776 3777 3778
{
	trx_t*	trx;
	ulint	err;
3779

osku's avatar
osku committed
3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805
	ut_ad(table && thr);

	if (flags & BTR_NO_LOCKING_FLAG) {

		return(DB_SUCCESS);
	}

	ut_a(flags == 0);

	trx = thr_get_trx(thr);

	lock_mutex_enter_kernel();

	/* Look for stronger locks the same trx already has on the table */

	if (lock_table_has(trx, table, mode)) {

		lock_mutex_exit_kernel();

		return(DB_SUCCESS);
	}

	/* We have to check if the new lock is compatible with any locks
	other transactions have in the table lock queue. */

	if (lock_table_other_has_incompatible(trx, LOCK_WAIT, table, mode)) {
3806

osku's avatar
osku committed
3807 3808 3809 3810
		/* Another trx has a request on the table in an incompatible
		mode: this trx may have to wait */

		err = lock_table_enqueue_waiting(mode | flags, table, thr);
3811

osku's avatar
osku committed
3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825
		lock_mutex_exit_kernel();

		return(err);
	}

	lock_table_create(table, mode | flags, trx);

	ut_a(!flags || mode == LOCK_S || mode == LOCK_X);

	lock_mutex_exit_kernel();

	return(DB_SUCCESS);
}

3826
/*********************************************************************//**
3827 3828
Checks if a waiting table lock request still has to wait in a queue.
@return	TRUE if still has to wait */
osku's avatar
osku committed
3829 3830 3831 3832
static
ibool
lock_table_has_to_wait_in_queue(
/*============================*/
3833
	lock_t*	wait_lock)	/*!< in: waiting table lock */
osku's avatar
osku committed
3834 3835 3836 3837
{
	dict_table_t*	table;
	lock_t*		lock;

3838
	ut_ad(mutex_own(&kernel_mutex));
3839 3840
	ut_ad(lock_get_wait(wait_lock));

osku's avatar
osku committed
3841 3842 3843 3844 3845 3846 3847 3848
	table = wait_lock->un_member.tab_lock.table;

	lock = UT_LIST_GET_FIRST(table->locks);

	while (lock != wait_lock) {

		if (lock_has_to_wait(wait_lock, lock)) {

3849
			return(TRUE);
osku's avatar
osku committed
3850 3851 3852 3853 3854 3855 3856 3857
		}

		lock = UT_LIST_GET_NEXT(un_member.tab_lock.locks, lock);
	}

	return(FALSE);
}

3858
/*************************************************************//**
osku's avatar
osku committed
3859 3860 3861 3862 3863 3864 3865
Removes a table lock request, waiting or granted, from the queue and grants
locks to other transactions in the queue, if they now are entitled to a
lock. */
static
void
lock_table_dequeue(
/*===============*/
3866
	lock_t*	in_lock)/*!< in: table lock object; transactions waiting
osku's avatar
osku committed
3867 3868 3869 3870
			behind will get their lock requests granted, if
			they are now qualified to it */
{
	lock_t*	lock;
3871

osku's avatar
osku committed
3872
	ut_ad(mutex_own(&kernel_mutex));
3873
	ut_a(lock_get_type_low(in_lock) == LOCK_TABLE);
osku's avatar
osku committed
3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884

	lock = UT_LIST_GET_NEXT(un_member.tab_lock.locks, in_lock);

	lock_table_remove_low(in_lock);

	/* Check if waiting locks in the queue can now be granted: grant
	locks if there are no conflicting locks ahead. */

	while (lock != NULL) {

		if (lock_get_wait(lock)
3885
		    && !lock_table_has_to_wait_in_queue(lock)) {
osku's avatar
osku committed
3886 3887 3888 3889 3890 3891 3892

			/* Grant the lock */
			lock_grant(lock);
		}

		lock = UT_LIST_GET_NEXT(un_member.tab_lock.locks, lock);
	}
3893
}
osku's avatar
osku committed
3894 3895 3896

/*=========================== LOCK RELEASE ==============================*/

3897
/*************************************************************//**
3898 3899 3900
Removes a granted record lock of a transaction from the queue and grants
locks to other transactions waiting in the queue if they now are entitled
to a lock. */
3901
UNIV_INTERN
3902 3903 3904
void
lock_rec_unlock(
/*============*/
3905
	trx_t*			trx,	/*!< in: transaction that has
3906
					set a record lock */
3907 3908 3909
	const buf_block_t*	block,	/*!< in: buffer block containing rec */
	const rec_t*		rec,	/*!< in: record */
	enum lock_mode		lock_mode)/*!< in: LOCK_S or LOCK_X */
3910 3911 3912 3913 3914 3915
{
	lock_t*	lock;
	lock_t*	release_lock	= NULL;
	ulint	heap_no;

	ut_ad(trx && rec);
3916
	ut_ad(block->frame == page_align(rec));
3917

3918
	heap_no = page_rec_get_heap_no(rec);
3919

3920
	mutex_enter(&kernel_mutex);
3921

3922
	lock = lock_rec_get_first(block, heap_no);
3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943

	/* Find the last lock with the same lock_mode and transaction
	from the record. */

	while (lock != NULL) {
		if (lock->trx == trx && lock_get_mode(lock) == lock_mode) {
			release_lock = lock;
			ut_a(!lock_get_wait(lock));
		}

		lock = lock_rec_get_next(heap_no, lock);
	}

	/* If a record lock is found, release the record lock */

	if (UNIV_LIKELY(release_lock != NULL)) {
		lock_rec_reset_nth_bit(release_lock, heap_no);
	} else {
		mutex_exit(&kernel_mutex);
		ut_print_timestamp(stderr);
		fprintf(stderr,
3944 3945 3946
			"  InnoDB: Error: unlock row could not"
			" find a %lu mode lock on the record\n",
			(ulong) lock_mode);
3947 3948 3949 3950 3951 3952

		return;
	}

	/* Check if we can now grant waiting lock requests */

3953
	lock = lock_rec_get_first(block, heap_no);
3954 3955 3956

	while (lock != NULL) {
		if (lock_get_wait(lock)
3957
		    && !lock_rec_has_to_wait_in_queue(lock)) {
3958 3959 3960 3961 3962 3963 3964 3965 3966

			/* Grant the lock */
			lock_grant(lock);
		}

		lock = lock_rec_get_next(heap_no, lock);
	}

	mutex_exit(&kernel_mutex);
3967
}
3968

3969
/*********************************************************************//**
osku's avatar
osku committed
3970 3971
Releases transaction locks, and releases possible other transactions waiting
because of these locks. */
3972
UNIV_INTERN
osku's avatar
osku committed
3973 3974 3975
void
lock_release_off_kernel(
/*====================*/
3976
	trx_t*	trx)	/*!< in: transaction */
osku's avatar
osku committed
3977 3978 3979 3980 3981 3982 3983 3984
{
	dict_table_t*	table;
	ulint		count;
	lock_t*		lock;

	ut_ad(mutex_own(&kernel_mutex));

	lock = UT_LIST_GET_LAST(trx->trx_locks);
3985

osku's avatar
osku committed
3986 3987 3988 3989 3990 3991
	count = 0;

	while (lock != NULL) {

		count++;

3992
		if (lock_get_type_low(lock) == LOCK_REC) {
3993

osku's avatar
osku committed
3994 3995
			lock_rec_dequeue_from_page(lock);
		} else {
3996
			ut_ad(lock_get_type_low(lock) & LOCK_TABLE);
osku's avatar
osku committed
3997 3998

			if (lock_get_mode(lock) != LOCK_IS
3999
			    && !ut_dulint_is_zero(trx->undo_no)) {
osku's avatar
osku committed
4000

4001 4002 4003
				/* The trx may have modified the table. We
				block the use of the MySQL query cache for
				all currently active transactions. */
osku's avatar
osku committed
4004 4005

				table = lock->un_member.tab_lock.table;
4006

4007 4008
				table->query_cache_inv_trx_id
					= trx_sys->max_trx_id;
osku's avatar
osku committed
4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022
			}

			lock_table_dequeue(lock);
		}

		if (count == LOCK_RELEASE_KERNEL_INTERVAL) {
			/* Release the kernel mutex for a while, so that we
			do not monopolize it */

			lock_mutex_exit_kernel();

			lock_mutex_enter_kernel();

			count = 0;
4023
		}
osku's avatar
osku committed
4024 4025 4026 4027

		lock = UT_LIST_GET_LAST(trx->trx_locks);
	}

sunny's avatar
sunny committed
4028
	ut_a(ib_vector_size(trx->autoinc_locks) == 0);
osku's avatar
osku committed
4029

sunny's avatar
sunny committed
4030
	mem_heap_empty(trx->lock_heap);
osku's avatar
osku committed
4031 4032
}

4033
/*********************************************************************//**
osku's avatar
osku committed
4034 4035
Cancels a waiting lock request and releases possible other transactions
waiting behind it. */
4036
UNIV_INTERN
osku's avatar
osku committed
4037 4038 4039
void
lock_cancel_waiting_and_release(
/*============================*/
4040
	lock_t*	lock)	/*!< in: waiting lock request */
osku's avatar
osku committed
4041 4042 4043
{
	ut_ad(mutex_own(&kernel_mutex));

4044
	if (lock_get_type_low(lock) == LOCK_REC) {
4045

osku's avatar
osku committed
4046 4047
		lock_rec_dequeue_from_page(lock);
	} else {
4048
		ut_ad(lock_get_type_low(lock) & LOCK_TABLE);
osku's avatar
osku committed
4049

sunny's avatar
sunny committed
4050 4051 4052 4053 4054
		if (lock->trx->autoinc_locks != NULL) {
			/* Release the transaction's AUTOINC locks/ */
			lock_release_autoinc_locks(lock->trx);
		}

osku's avatar
osku committed
4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066
		lock_table_dequeue(lock);
	}

	/* Reset the wait flag and the back pointer to lock in trx */

	lock_reset_lock_and_trx_wait(lock);

	/* The following function releases the trx from lock wait */

	trx_end_lock_wait(lock->trx);
}

4067 4068 4069 4070 4071 4072
/* True if a lock mode is S or X */
#define IS_LOCK_S_OR_X(lock) \
	(lock_get_mode(lock) == LOCK_S \
	 || lock_get_mode(lock) == LOCK_X)


4073
/*********************************************************************//**
4074 4075 4076 4077
Removes locks of a transaction on a table to be dropped.
If remove_also_table_sx_locks is TRUE then table-level S and X locks are
also removed in addition to other table-level and record-level locks.
No lock, that is going to be removed, is allowed to be a wait lock. */
osku's avatar
osku committed
4078 4079
static
void
4080 4081
lock_remove_all_on_table_for_trx(
/*=============================*/
4082 4083 4084
	dict_table_t*	table,			/*!< in: table to be dropped */
	trx_t*		trx,			/*!< in: a transaction */
	ibool		remove_also_table_sx_locks)/*!< in: also removes
4085
						table S and X locks */
osku's avatar
osku committed
4086 4087 4088 4089 4090 4091 4092
{
	lock_t*	lock;
	lock_t*	prev_lock;

	ut_ad(mutex_own(&kernel_mutex));

	lock = UT_LIST_GET_LAST(trx->trx_locks);
4093

osku's avatar
osku committed
4094 4095
	while (lock != NULL) {
		prev_lock = UT_LIST_GET_PREV(trx_locks, lock);
4096

4097
		if (lock_get_type_low(lock) == LOCK_REC
4098
		    && lock->index->table == table) {
osku's avatar
osku committed
4099
			ut_a(!lock_get_wait(lock));
4100

osku's avatar
osku committed
4101
			lock_rec_discard(lock);
4102
		} else if (lock_get_type_low(lock) & LOCK_TABLE
4103 4104 4105
			   && lock->un_member.tab_lock.table == table
			   && (remove_also_table_sx_locks
			       || !IS_LOCK_S_OR_X(lock))) {
osku's avatar
osku committed
4106 4107

			ut_a(!lock_get_wait(lock));
4108

osku's avatar
osku committed
4109 4110 4111 4112 4113 4114 4115
			lock_table_remove_low(lock);
		}

		lock = prev_lock;
	}
}

4116
/*********************************************************************//**
4117 4118 4119 4120
Removes locks on a table to be dropped or truncated.
If remove_also_table_sx_locks is TRUE then table-level S and X locks are
also removed in addition to other table-level and record-level locks.
No lock, that is going to be removed, is allowed to be a wait lock. */
4121
UNIV_INTERN
osku's avatar
osku committed
4122
void
4123 4124
lock_remove_all_on_table(
/*=====================*/
4125
	dict_table_t*	table,			/*!< in: table to be dropped
4126
						or truncated */
4127
	ibool		remove_also_table_sx_locks)/*!< in: also removes
4128
						table S and X locks */
osku's avatar
osku committed
4129 4130
{
	lock_t*	lock;
4131
	lock_t*	prev_lock;
osku's avatar
osku committed
4132 4133 4134 4135 4136

	mutex_enter(&kernel_mutex);

	lock = UT_LIST_GET_FIRST(table->locks);

4137 4138 4139 4140
	while (lock != NULL) {

		prev_lock = UT_LIST_GET_PREV(un_member.tab_lock.locks,
					     lock);
osku's avatar
osku committed
4141

4142 4143 4144 4145 4146 4147
		/* If we should remove all locks (remove_also_table_sx_locks
		is TRUE), or if the lock is not table-level S or X lock,
		then check we are not going to remove a wait lock. */
		if (remove_also_table_sx_locks
		    || !(lock_get_type(lock) == LOCK_TABLE
			 && IS_LOCK_S_OR_X(lock))) {
osku's avatar
osku committed
4148

4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175
			ut_a(!lock_get_wait(lock));
		}

		lock_remove_all_on_table_for_trx(table, lock->trx,
						 remove_also_table_sx_locks);

		if (prev_lock == NULL) {
			if (lock == UT_LIST_GET_FIRST(table->locks)) {
				/* lock was not removed, pick its successor */
				lock = UT_LIST_GET_NEXT(
					un_member.tab_lock.locks, lock);
			} else {
				/* lock was removed, pick the first one */
				lock = UT_LIST_GET_FIRST(table->locks);
			}
		} else if (UT_LIST_GET_NEXT(un_member.tab_lock.locks,
					    prev_lock) != lock) {
			/* If lock was removed by
			lock_remove_all_on_table_for_trx() then pick the
			successor of prev_lock ... */
			lock = UT_LIST_GET_NEXT(
				un_member.tab_lock.locks, prev_lock);
		} else {
			/* ... otherwise pick the successor of lock. */
			lock = UT_LIST_GET_NEXT(
				un_member.tab_lock.locks, lock);
		}
osku's avatar
osku committed
4176 4177 4178 4179 4180 4181 4182
	}

	mutex_exit(&kernel_mutex);
}

/*===================== VALIDATION AND DEBUGGING  ====================*/

4183
/*********************************************************************//**
osku's avatar
osku committed
4184
Prints info of a table lock. */
4185
UNIV_INTERN
osku's avatar
osku committed
4186 4187 4188
void
lock_table_print(
/*=============*/
4189 4190
	FILE*		file,	/*!< in: file where to print */
	const lock_t*	lock)	/*!< in: table type lock */
osku's avatar
osku committed
4191 4192
{
	ut_ad(mutex_own(&kernel_mutex));
4193
	ut_a(lock_get_type_low(lock) == LOCK_TABLE);
osku's avatar
osku committed
4194 4195

	fputs("TABLE LOCK table ", file);
4196
	ut_print_name(file, lock->trx, TRUE,
4197
		      lock->un_member.tab_lock.table->name);
vasil's avatar
vasil committed
4198 4199
	fprintf(file, " trx id " TRX_ID_FMT,
		TRX_ID_PREP_PRINTF(lock->trx->id));
osku's avatar
osku committed
4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211

	if (lock_get_mode(lock) == LOCK_S) {
		fputs(" lock mode S", file);
	} else if (lock_get_mode(lock) == LOCK_X) {
		fputs(" lock mode X", file);
	} else if (lock_get_mode(lock) == LOCK_IS) {
		fputs(" lock mode IS", file);
	} else if (lock_get_mode(lock) == LOCK_IX) {
		fputs(" lock mode IX", file);
	} else if (lock_get_mode(lock) == LOCK_AUTO_INC) {
		fputs(" lock mode AUTO-INC", file);
	} else {
4212 4213
		fprintf(file, " unknown lock mode %lu",
			(ulong) lock_get_mode(lock));
osku's avatar
osku committed
4214 4215 4216 4217 4218 4219 4220
	}

	if (lock_get_wait(lock)) {
		fputs(" waiting", file);
	}

	putc('\n', file);
4221 4222
}

4223
/*********************************************************************//**
osku's avatar
osku committed
4224
Prints info of a record lock. */
4225
UNIV_INTERN
osku's avatar
osku committed
4226 4227 4228
void
lock_rec_print(
/*===========*/
4229 4230
	FILE*		file,	/*!< in: file where to print */
	const lock_t*	lock)	/*!< in: record type lock */
osku's avatar
osku committed
4231
{
4232 4233 4234 4235 4236 4237 4238 4239
	const buf_block_t*	block;
	ulint			space;
	ulint			page_no;
	ulint			i;
	mtr_t			mtr;
	mem_heap_t*		heap		= NULL;
	ulint			offsets_[REC_OFFS_NORMAL_SIZE];
	ulint*			offsets		= offsets_;
4240
	rec_offs_init(offsets_);
osku's avatar
osku committed
4241 4242

	ut_ad(mutex_own(&kernel_mutex));
4243
	ut_a(lock_get_type_low(lock) == LOCK_REC);
osku's avatar
osku committed
4244 4245

	space = lock->un_member.rec_lock.space;
4246
	page_no = lock->un_member.rec_lock.page_no;
osku's avatar
osku committed
4247 4248

	fprintf(file, "RECORD LOCKS space id %lu page no %lu n bits %lu ",
4249 4250
		(ulong) space, (ulong) page_no,
		(ulong) lock_rec_get_n_bits(lock));
osku's avatar
osku committed
4251
	dict_index_name_print(file, lock->trx, lock->index);
vasil's avatar
vasil committed
4252 4253
	fprintf(file, " trx id " TRX_ID_FMT,
		TRX_ID_PREP_PRINTF(lock->trx->id));
osku's avatar
osku committed
4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282

	if (lock_get_mode(lock) == LOCK_S) {
		fputs(" lock mode S", file);
	} else if (lock_get_mode(lock) == LOCK_X) {
		fputs(" lock_mode X", file);
	} else {
		ut_error;
	}

	if (lock_rec_get_gap(lock)) {
		fputs(" locks gap before rec", file);
	}

	if (lock_rec_get_rec_not_gap(lock)) {
		fputs(" locks rec but not gap", file);
	}

	if (lock_rec_get_insert_intention(lock)) {
		fputs(" insert intention", file);
	}

	if (lock_get_wait(lock)) {
		fputs(" waiting", file);
	}

	mtr_start(&mtr);

	putc('\n', file);

vasil's avatar
vasil committed
4283
	block = buf_page_try_get(space, page_no, &mtr);
4284

4285
	if (block) {
4286
		for (i = 0; i < lock_rec_get_n_bits(lock); i++) {
osku's avatar
osku committed
4287

4288
			if (lock_rec_get_nth_bit(lock, i)) {
osku's avatar
osku committed
4289

4290
				const rec_t*	rec
4291 4292
					= page_find_rec_with_heap_no(
						buf_block_get_frame(block), i);
4293 4294 4295
				offsets = rec_get_offsets(
					rec, lock->index, offsets,
					ULINT_UNDEFINED, &heap);
4296 4297 4298

				fprintf(file, "Record lock, heap no %lu ",
					(ulong) i);
osku's avatar
osku committed
4299
				rec_print_new(file, rec, offsets);
4300
				putc('\n', file);
osku's avatar
osku committed
4301
			}
4302 4303 4304 4305
		}
	} else {
		for (i = 0; i < lock_rec_get_n_bits(lock); i++) {
			fprintf(file, "Record lock, heap no %lu\n", (ulong) i);
osku's avatar
osku committed
4306 4307 4308 4309 4310 4311 4312 4313 4314
		}
	}

	mtr_commit(&mtr);
	if (UNIV_LIKELY_NULL(heap)) {
		mem_heap_free(heap);
	}
}

4315 4316 4317 4318 4319 4320 4321 4322
#ifdef UNIV_DEBUG
/* Print the number of lock structs from lock_print_info_summary() only
in non-production builds for performance reasons, see
http://bugs.mysql.com/36942 */
#define PRINT_NUM_OF_LOCK_STRUCTS
#endif /* UNIV_DEBUG */

#ifdef PRINT_NUM_OF_LOCK_STRUCTS
4323
/*********************************************************************//**
4324 4325
Calculates the number of record lock structs in the record lock hash table.
@return	number of record locks */
osku's avatar
osku committed
4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349
static
ulint
lock_get_n_rec_locks(void)
/*======================*/
{
	lock_t*	lock;
	ulint	n_locks	= 0;
	ulint	i;

	ut_ad(mutex_own(&kernel_mutex));

	for (i = 0; i < hash_get_n_cells(lock_sys->rec_hash); i++) {

		lock = HASH_GET_FIRST(lock_sys->rec_hash, i);

		while (lock) {
			n_locks++;

			lock = HASH_GET_NEXT(hash, lock);
		}
	}

	return(n_locks);
}
4350
#endif /* PRINT_NUM_OF_LOCK_STRUCTS */
osku's avatar
osku committed
4351

4352
/*********************************************************************//**
4353 4354 4355
Prints info of locks for all transactions.
@return FALSE if not able to obtain kernel mutex
and exits without printing info */
4356
UNIV_INTERN
4357
ibool
osku's avatar
osku committed
4358 4359
lock_print_info_summary(
/*====================*/
4360 4361
	FILE*	file,	/*!< in: file where to print */
	ibool   nowait)	/*!< in: whether to wait for the kernel mutex */
osku's avatar
osku committed
4362
{
4363 4364 4365 4366 4367 4368 4369 4370 4371 4372
	/* if nowait is FALSE, wait on the kernel mutex,
	otherwise return immediately if fail to obtain the
	mutex. */
	if (!nowait) {
		lock_mutex_enter_kernel();
	} else if (mutex_enter_nowait(&kernel_mutex)) {
		fputs("FAIL TO OBTAIN KERNEL MUTEX, "
		      "SKIP LOCK INFO PRINTING\n", file);
		return(FALSE);
	}
osku's avatar
osku committed
4373 4374

	if (lock_deadlock_found) {
4375 4376 4377
		fputs("------------------------\n"
		      "LATEST DETECTED DEADLOCK\n"
		      "------------------------\n", file);
osku's avatar
osku committed
4378 4379 4380 4381

		ut_copy_file(file, lock_latest_err_file);
	}

4382 4383 4384
	fputs("------------\n"
	      "TRANSACTIONS\n"
	      "------------\n", file);
osku's avatar
osku committed
4385

vasil's avatar
vasil committed
4386 4387
	fprintf(file, "Trx id counter " TRX_ID_FMT "\n",
		TRX_ID_PREP_PRINTF(trx_sys->max_trx_id));
osku's avatar
osku committed
4388 4389

	fprintf(file,
vasil's avatar
vasil committed
4390 4391 4392 4393
		"Purge done for trx's n:o < " TRX_ID_FMT
		" undo n:o < " TRX_ID_FMT "\n",
		TRX_ID_PREP_PRINTF(purge_sys->purge_trx_no),
		TRX_ID_PREP_PRINTF(purge_sys->purge_undo_no));
osku's avatar
osku committed
4394 4395

	fprintf(file,
4396 4397
		"History list length %lu\n",
		(ulong) trx_sys->rseg_history_len);
osku's avatar
osku committed
4398

4399
#ifdef PRINT_NUM_OF_LOCK_STRUCTS
osku's avatar
osku committed
4400 4401
	fprintf(file,
		"Total number of lock structs in row lock hash table %lu\n",
4402
		(ulong) lock_get_n_rec_locks());
4403
#endif /* PRINT_NUM_OF_LOCK_STRUCTS */
4404
	return(TRUE);
osku's avatar
osku committed
4405 4406
}

4407
/*********************************************************************//**
osku's avatar
osku committed
4408
Prints info of locks for each transaction. */
4409
UNIV_INTERN
osku's avatar
osku committed
4410 4411 4412
void
lock_print_info_all_transactions(
/*=============================*/
4413
	FILE*	file)	/*!< in: file where to print */
osku's avatar
osku committed
4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433
{
	lock_t*	lock;
	ibool	load_page_first = TRUE;
	ulint	nth_trx		= 0;
	ulint	nth_lock	= 0;
	ulint	i;
	mtr_t	mtr;
	trx_t*	trx;

	fprintf(file, "LIST OF TRANSACTIONS FOR EACH SESSION:\n");

	/* First print info on non-active transactions */

	trx = UT_LIST_GET_FIRST(trx_sys->mysql_trx_list);

	while (trx) {
		if (trx->conc_state == TRX_NOT_STARTED) {
			fputs("---", file);
			trx_print(file, trx, 600);
		}
4434

osku's avatar
osku committed
4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446
		trx = UT_LIST_GET_NEXT(mysql_trx_list, trx);
	}

loop:
	trx = UT_LIST_GET_FIRST(trx_sys->trx_list);

	i = 0;

	/* Since we temporarily release the kernel mutex when
	reading a database page in below, variable trx may be
	obsolete now and we must loop through the trx list to
	get probably the same trx, or some other trx. */
4447

osku's avatar
osku committed
4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463
	while (trx && (i < nth_trx)) {
		trx = UT_LIST_GET_NEXT(trx_list, trx);
		i++;
	}

	if (trx == NULL) {
		lock_mutex_exit_kernel();

		ut_ad(lock_validate());

		return;
	}

	if (nth_lock == 0) {
		fputs("---", file);
		trx_print(file, trx, 600);
4464 4465

		if (trx->read_view) {
osku's avatar
osku committed
4466
			fprintf(file,
4467
				"Trx read view will not see trx with"
vasil's avatar
vasil committed
4468 4469 4470
				" id >= " TRX_ID_FMT
				", sees < " TRX_ID_FMT "\n",
				TRX_ID_PREP_PRINTF(
4471
					trx->read_view->low_limit_id),
vasil's avatar
vasil committed
4472
				TRX_ID_PREP_PRINTF(
4473
					trx->read_view->up_limit_id));
4474
		}
osku's avatar
osku committed
4475 4476 4477

		if (trx->que_state == TRX_QUE_LOCK_WAIT) {
			fprintf(file,
4478 4479 4480 4481
				"------- TRX HAS BEEN WAITING %lu SEC"
				" FOR THIS LOCK TO BE GRANTED:\n",
				(ulong) difftime(time(NULL),
						 trx->wait_started));
osku's avatar
osku committed
4482

4483
			if (lock_get_type_low(trx->wait_lock) == LOCK_REC) {
osku's avatar
osku committed
4484 4485 4486 4487 4488 4489 4490 4491 4492 4493
				lock_rec_print(file, trx->wait_lock);
			} else {
				lock_table_print(file, trx->wait_lock);
			}

			fputs("------------------\n", file);
		}
	}

	if (!srv_print_innodb_lock_monitor) {
4494 4495
		nth_trx++;
		goto loop;
osku's avatar
osku committed
4496 4497 4498 4499 4500 4501
	}

	i = 0;

	/* Look at the note about the trx loop above why we loop here:
	lock may be an obsolete pointer now. */
4502

osku's avatar
osku committed
4503
	lock = UT_LIST_GET_FIRST(trx->trx_locks);
4504

osku's avatar
osku committed
4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516
	while (lock && (i < nth_lock)) {
		lock = UT_LIST_GET_NEXT(trx_locks, lock);
		i++;
	}

	if (lock == NULL) {
		nth_trx++;
		nth_lock = 0;

		goto loop;
	}

4517
	if (lock_get_type_low(lock) == LOCK_REC) {
4518
		if (load_page_first) {
4519 4520 4521 4522
			ulint	space	= lock->un_member.rec_lock.space;
			ulint	zip_size= fil_space_get_zip_size(space);
			ulint	page_no = lock->un_member.rec_lock.page_no;

4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536
			if (UNIV_UNLIKELY(zip_size == ULINT_UNDEFINED)) {

				/* It is a single table tablespace and
				the .ibd file is missing (TRUNCATE
				TABLE probably stole the locks): just
				print the lock without attempting to
				load the page in the buffer pool. */

				fprintf(file, "RECORD LOCKS on"
					" non-existing space %lu\n",
					(ulong) space);
				goto print_rec;
			}

osku's avatar
osku committed
4537 4538 4539
			lock_mutex_exit_kernel();

			mtr_start(&mtr);
4540

4541 4542
			buf_page_get_with_no_latch(space, zip_size,
						   page_no, &mtr);
osku's avatar
osku committed
4543 4544 4545 4546 4547 4548 4549 4550 4551

			mtr_commit(&mtr);

			load_page_first = FALSE;

			lock_mutex_enter_kernel();

			goto loop;
		}
4552

4553
print_rec:
osku's avatar
osku committed
4554 4555
		lock_rec_print(file, lock);
	} else {
4556
		ut_ad(lock_get_type_low(lock) & LOCK_TABLE);
4557

osku's avatar
osku committed
4558 4559 4560 4561 4562 4563 4564 4565
		lock_table_print(file, lock);
	}

	load_page_first = TRUE;

	nth_lock++;

	if (nth_lock >= 10) {
4566 4567 4568
		fputs("10 LOCKS PRINTED FOR THIS TRX:"
		      " SUPPRESSING FURTHER PRINTS\n",
		      file);
4569

osku's avatar
osku committed
4570 4571 4572 4573 4574 4575 4576 4577 4578
		nth_trx++;
		nth_lock = 0;

		goto loop;
	}

	goto loop;
}

4579
#ifdef UNIV_DEBUG
4580
/*********************************************************************//**
4581 4582
Validates the lock queue on a table.
@return	TRUE if ok */
4583
static
osku's avatar
osku committed
4584 4585 4586
ibool
lock_table_queue_validate(
/*======================*/
4587
	dict_table_t*	table)	/*!< in: table */
osku's avatar
osku committed
4588 4589 4590 4591 4592 4593 4594 4595 4596
{
	lock_t*	lock;

	ut_ad(mutex_own(&kernel_mutex));

	lock = UT_LIST_GET_FIRST(table->locks);

	while (lock) {
		ut_a(((lock->trx)->conc_state == TRX_ACTIVE)
4597 4598
		     || ((lock->trx)->conc_state == TRX_PREPARED)
		     || ((lock->trx)->conc_state == TRX_COMMITTED_IN_MEMORY));
4599

osku's avatar
osku committed
4600 4601
		if (!lock_get_wait(lock)) {

4602 4603 4604
			ut_a(!lock_table_other_has_incompatible(
				     lock->trx, 0, table,
				     lock_get_mode(lock)));
osku's avatar
osku committed
4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615
		} else {

			ut_a(lock_table_has_to_wait_in_queue(lock));
		}

		lock = UT_LIST_GET_NEXT(un_member.tab_lock.locks, lock);
	}

	return(TRUE);
}

4616
/*********************************************************************//**
4617 4618
Validates the lock queue on a single record.
@return	TRUE if ok */
4619
static
osku's avatar
osku committed
4620 4621 4622
ibool
lock_rec_queue_validate(
/*====================*/
4623 4624 4625 4626
	const buf_block_t*	block,	/*!< in: buffer block containing rec */
	const rec_t*		rec,	/*!< in: record to look at */
	dict_index_t*		index,	/*!< in: index, or NULL if not known */
	const ulint*		offsets)/*!< in: rec_get_offsets(rec, index) */
osku's avatar
osku committed
4627
{
4628
	trx_t*	impl_trx;
osku's avatar
osku committed
4629
	lock_t*	lock;
marko's avatar
marko committed
4630
	ulint	heap_no;
osku's avatar
osku committed
4631 4632

	ut_a(rec);
4633
	ut_a(block->frame == page_align(rec));
osku's avatar
osku committed
4634 4635 4636
	ut_ad(rec_offs_validate(rec, index, offsets));
	ut_ad(!page_rec_is_comp(rec) == !rec_offs_comp(offsets));

4637
	heap_no = page_rec_get_heap_no(rec);
osku's avatar
osku committed
4638

4639
	lock_mutex_enter_kernel();
marko's avatar
marko committed
4640

osku's avatar
osku committed
4641 4642
	if (!page_rec_is_user_rec(rec)) {

4643
		lock = lock_rec_get_first(block, heap_no);
osku's avatar
osku committed
4644 4645

		while (lock) {
4646 4647 4648 4649 4650 4651 4652 4653
			switch(lock->trx->conc_state) {
			case TRX_ACTIVE:
			case TRX_PREPARED:
			case TRX_COMMITTED_IN_MEMORY:
				break;
			default:
				ut_error;
			}
4654

osku's avatar
osku committed
4655
			ut_a(trx_in_trx_list(lock->trx));
4656

osku's avatar
osku committed
4657 4658 4659 4660 4661 4662 4663 4664
			if (lock_get_wait(lock)) {
				ut_a(lock_rec_has_to_wait_in_queue(lock));
			}

			if (index) {
				ut_a(lock->index == index);
			}

marko's avatar
marko committed
4665
			lock = lock_rec_get_next(heap_no, lock);
osku's avatar
osku committed
4666 4667 4668 4669
		}

		lock_mutex_exit_kernel();

4670
		return(TRUE);
osku's avatar
osku committed
4671 4672
	}

marko's avatar
marko committed
4673
	if (!index);
4674
	else if (dict_index_is_clust(index)) {
4675

osku's avatar
osku committed
4676 4677
		impl_trx = lock_clust_rec_some_has_impl(rec, index, offsets);

4678 4679 4680
		if (impl_trx
		    && lock_rec_other_has_expl_req(LOCK_S, 0, LOCK_WAIT,
						   block, heap_no, impl_trx)) {
osku's avatar
osku committed
4681

4682
			ut_a(lock_rec_has_expl(LOCK_X | LOCK_REC_NOT_GAP,
4683
					       block, heap_no, impl_trx));
osku's avatar
osku committed
4684
		}
marko's avatar
marko committed
4685
	} else {
4686

osku's avatar
osku committed
4687 4688 4689
		/* The kernel mutex may get released temporarily in the
		next function call: we have to release lock table mutex
		to obey the latching order */
4690

4691 4692 4693 4694
		/* If this thread is holding the file space latch
		(fil_space_t::latch), the following check WILL break
		latching order and may cause a deadlock of threads. */

4695 4696
		impl_trx = lock_sec_rec_some_has_impl_off_kernel(
			rec, index, offsets);
osku's avatar
osku committed
4697

4698 4699 4700
		if (impl_trx
		    && lock_rec_other_has_expl_req(LOCK_S, 0, LOCK_WAIT,
						   block, heap_no, impl_trx)) {
osku's avatar
osku committed
4701 4702

			ut_a(lock_rec_has_expl(LOCK_X | LOCK_REC_NOT_GAP,
4703
					       block, heap_no, impl_trx));
osku's avatar
osku committed
4704 4705 4706
		}
	}

4707
	lock = lock_rec_get_first(block, heap_no);
osku's avatar
osku committed
4708 4709 4710

	while (lock) {
		ut_a(lock->trx->conc_state == TRX_ACTIVE
4711 4712
		     || lock->trx->conc_state == TRX_PREPARED
		     || lock->trx->conc_state == TRX_COMMITTED_IN_MEMORY);
osku's avatar
osku committed
4713
		ut_a(trx_in_trx_list(lock->trx));
4714

osku's avatar
osku committed
4715 4716 4717 4718 4719 4720
		if (index) {
			ut_a(lock->index == index);
		}

		if (!lock_rec_get_gap(lock) && !lock_get_wait(lock)) {

4721
			enum lock_mode	mode;
4722

osku's avatar
osku committed
4723 4724 4725 4726 4727
			if (lock_get_mode(lock) == LOCK_S) {
				mode = LOCK_X;
			} else {
				mode = LOCK_S;
			}
4728
			ut_a(!lock_rec_other_has_expl_req(
4729
				     mode, 0, 0, block, heap_no, lock->trx));
osku's avatar
osku committed
4730 4731 4732 4733 4734 4735

		} else if (lock_get_wait(lock) && !lock_rec_get_gap(lock)) {

			ut_a(lock_rec_has_to_wait_in_queue(lock));
		}

marko's avatar
marko committed
4736
		lock = lock_rec_get_next(heap_no, lock);
osku's avatar
osku committed
4737 4738 4739 4740 4741 4742 4743
	}

	lock_mutex_exit_kernel();

	return(TRUE);
}

4744
/*********************************************************************//**
4745 4746
Validates the record lock queues on a page.
@return	TRUE if ok */
4747
static
osku's avatar
osku committed
4748 4749 4750
ibool
lock_rec_validate_page(
/*===================*/
4751
	ulint	space,	/*!< in: space id */
4752 4753
	ulint	zip_size,/*!< in: compressed page size in bytes
			or 0 for uncompressed pages */
4754
	ulint	page_no)/*!< in: page number */
osku's avatar
osku committed
4755 4756
{
	dict_index_t*	index;
4757
	buf_block_t*	block;
4758
	const page_t*	page;
4759
	lock_t*		lock;
4760
	const rec_t*	rec;
4761 4762 4763 4764
	ulint		nth_lock	= 0;
	ulint		nth_bit		= 0;
	ulint		i;
	mtr_t		mtr;
osku's avatar
osku committed
4765 4766 4767
	mem_heap_t*	heap		= NULL;
	ulint		offsets_[REC_OFFS_NORMAL_SIZE];
	ulint*		offsets		= offsets_;
4768
	rec_offs_init(offsets_);
osku's avatar
osku committed
4769 4770 4771 4772

	ut_ad(!mutex_own(&kernel_mutex));

	mtr_start(&mtr);
4773

4774 4775
	ut_ad(zip_size != ULINT_UNDEFINED);
	block = buf_page_get(space, zip_size, page_no, RW_X_LATCH, &mtr);
4776
	buf_block_dbg_add_level(block, SYNC_NO_ORDER_CHECK);
marko's avatar
marko committed
4777

4778
	page = block->frame;
osku's avatar
osku committed
4779 4780

	lock_mutex_enter_kernel();
4781
loop:
osku's avatar
osku committed
4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798
	lock = lock_rec_get_first_on_page_addr(space, page_no);

	if (!lock) {
		goto function_exit;
	}

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

		lock = lock_rec_get_next_on_page(lock);

		if (!lock) {
			goto function_exit;
		}
	}

	ut_a(trx_in_trx_list(lock->trx));
	ut_a(lock->trx->conc_state == TRX_ACTIVE
4799 4800
	     || lock->trx->conc_state == TRX_PREPARED
	     || lock->trx->conc_state == TRX_COMMITTED_IN_MEMORY);
4801

4802 4803 4804 4805 4806 4807 4808
# ifdef UNIV_SYNC_DEBUG
	/* Only validate the record queues when this thread is not
	holding a space->latch.  Deadlocks are possible due to
	latching order violation when UNIV_DEBUG is defined while
	UNIV_SYNC_DEBUG is not. */
	if (!sync_thread_levels_contains(SYNC_FSP))
# endif /* UNIV_SYNC_DEBUG */
osku's avatar
osku committed
4809 4810 4811 4812 4813 4814
	for (i = nth_bit; i < lock_rec_get_n_bits(lock); i++) {

		if (i == 1 || lock_rec_get_nth_bit(lock, i)) {

			index = lock->index;
			rec = page_find_rec_with_heap_no(page, i);
4815
			ut_a(rec);
osku's avatar
osku committed
4816
			offsets = rec_get_offsets(rec, index, offsets,
4817
						  ULINT_UNDEFINED, &heap);
osku's avatar
osku committed
4818 4819

			fprintf(stderr,
4820 4821
				"Validating %lu %lu\n",
				(ulong) space, (ulong) page_no);
osku's avatar
osku committed
4822 4823 4824

			lock_mutex_exit_kernel();

4825 4826 4827 4828 4829
			/* If this thread is holding the file space
			latch (fil_space_t::latch), the following
			check WILL break the latching order and may
			cause a deadlock of threads. */

4830
			lock_rec_queue_validate(block, rec, index, offsets);
osku's avatar
osku committed
4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853

			lock_mutex_enter_kernel();

			nth_bit = i + 1;

			goto loop;
		}
	}

	nth_bit = 0;
	nth_lock++;

	goto loop;

function_exit:
	lock_mutex_exit_kernel();

	mtr_commit(&mtr);

	if (UNIV_LIKELY_NULL(heap)) {
		mem_heap_free(heap);
	}
	return(TRUE);
4854 4855
}

4856
/*********************************************************************//**
4857 4858
Validates the lock system.
@return	TRUE if ok */
4859
static
osku's avatar
osku committed
4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871
ibool
lock_validate(void)
/*===============*/
{
	lock_t*	lock;
	trx_t*	trx;
	dulint	limit;
	ulint	space;
	ulint	page_no;
	ulint	i;

	lock_mutex_enter_kernel();
4872

osku's avatar
osku committed
4873 4874 4875 4876
	trx = UT_LIST_GET_FIRST(trx_sys->trx_list);

	while (trx) {
		lock = UT_LIST_GET_FIRST(trx->trx_locks);
4877

osku's avatar
osku committed
4878
		while (lock) {
4879
			if (lock_get_type_low(lock) & LOCK_TABLE) {
4880

4881 4882
				lock_table_queue_validate(
					lock->un_member.tab_lock.table);
osku's avatar
osku committed
4883
			}
4884

osku's avatar
osku committed
4885 4886
			lock = UT_LIST_GET_NEXT(trx_locks, lock);
		}
4887

osku's avatar
osku committed
4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902
		trx = UT_LIST_GET_NEXT(trx_list, trx);
	}

	for (i = 0; i < hash_get_n_cells(lock_sys->rec_hash); i++) {

		limit = ut_dulint_zero;

		for (;;) {
			lock = HASH_GET_FIRST(lock_sys->rec_hash, i);

			while (lock) {
				ut_a(trx_in_trx_list(lock->trx));

				space = lock->un_member.rec_lock.space;
				page_no = lock->un_member.rec_lock.page_no;
4903

4904 4905 4906
				if (ut_dulint_cmp(
					    ut_dulint_create(space, page_no),
					    limit) >= 0) {
osku's avatar
osku committed
4907 4908 4909 4910 4911 4912 4913 4914 4915 4916
					break;
				}

				lock = HASH_GET_NEXT(hash, lock);
			}

			if (!lock) {

				break;
			}
4917

osku's avatar
osku committed
4918 4919
			lock_mutex_exit_kernel();

4920 4921 4922
			lock_rec_validate_page(space,
					       fil_space_get_zip_size(space),
					       page_no);
osku's avatar
osku committed
4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933

			lock_mutex_enter_kernel();

			limit = ut_dulint_create(space, page_no + 1);
		}
	}

	lock_mutex_exit_kernel();

	return(TRUE);
}
4934
#endif /* UNIV_DEBUG */
osku's avatar
osku committed
4935 4936
/*============ RECORD LOCK CHECKS FOR ROW OPERATIONS ====================*/

4937
/*********************************************************************//**
osku's avatar
osku committed
4938 4939 4940 4941
Checks if locks of other transactions prevent an immediate insert of
a record. If they do, first tests if the query thread should anyway
be suspended for some reason; if not, then puts the transaction and
the query thread to the lock wait state and inserts a waiting request
4942 4943
for a gap x-lock to the lock queue.
@return	DB_SUCCESS, DB_LOCK_WAIT, DB_DEADLOCK, or DB_QUE_THR_SUSPENDED */
4944
UNIV_INTERN
osku's avatar
osku committed
4945 4946 4947
ulint
lock_rec_insert_check_and_lock(
/*===========================*/
4948
	ulint		flags,	/*!< in: if BTR_NO_LOCKING_FLAG bit is
4949
				set, does nothing */
4950 4951 4952 4953 4954 4955
	const rec_t*	rec,	/*!< in: record after which to insert */
	buf_block_t*	block,	/*!< in/out: buffer block of rec */
	dict_index_t*	index,	/*!< in: index */
	que_thr_t*	thr,	/*!< in: query thread */
	mtr_t*		mtr,	/*!< in/out: mini-transaction */
	ibool*		inherit)/*!< out: set to TRUE if the new
4956 4957 4958
				inserted record maybe should inherit
				LOCK_GAP type locks from the successor
				record */
osku's avatar
osku committed
4959
{
4960 4961 4962 4963 4964
	const rec_t*	next_rec;
	trx_t*		trx;
	lock_t*		lock;
	ulint		err;
	ulint		next_rec_heap_no;
osku's avatar
osku committed
4965

4966 4967
	ut_ad(block->frame == page_align(rec));

osku's avatar
osku committed
4968 4969 4970 4971 4972 4973
	if (flags & BTR_NO_LOCKING_FLAG) {

		return(DB_SUCCESS);
	}

	trx = thr_get_trx(thr);
4974
	next_rec = page_rec_get_next_const(rec);
marko's avatar
marko committed
4975
	next_rec_heap_no = page_rec_get_heap_no(next_rec);
osku's avatar
osku committed
4976 4977 4978

	lock_mutex_enter_kernel();

4979 4980
	/* When inserting a record into an index, the table must be at
	least IX-locked or we must be building an index, in which case
marko's avatar
marko committed
4981
	the table must be at least S-locked. */
4982 4983
	ut_ad(lock_table_has(trx, index->table, LOCK_IX)
	      || (*index->name == TEMP_INDEX_PREFIX
marko's avatar
marko committed
4984
		  && lock_table_has(trx, index->table, LOCK_S)));
osku's avatar
osku committed
4985

4986
	lock = lock_rec_get_first(block, next_rec_heap_no);
marko's avatar
marko committed
4987 4988

	if (UNIV_LIKELY(lock == NULL)) {
osku's avatar
osku committed
4989 4990 4991 4992
		/* We optimize CPU time usage in the simplest case */

		lock_mutex_exit_kernel();

4993
		if (!dict_index_is_clust(index)) {
osku's avatar
osku committed
4994
			/* Update the page max trx id field */
marko's avatar
marko committed
4995
			page_update_max_trx_id(block,
4996
					       buf_block_get_page_zip(block),
4997
					       trx->id, mtr);
osku's avatar
osku committed
4998
		}
4999

marko's avatar
marko committed
5000 5001
		*inherit = FALSE;

osku's avatar
osku committed
5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016
		return(DB_SUCCESS);
	}

	*inherit = TRUE;

	/* If another transaction has an explicit lock request which locks
	the gap, waiting or granted, on the successor, the insert has to wait.

	An exception is the case where the lock by the another transaction
	is a gap type lock which it placed to wait for its turn to insert. We
	do not consider that kind of a lock conflicting with our insert. This
	eliminates an unnecessary deadlock which resulted when 2 transactions
	had to wait for their insert. Both had waiting gap type lock requests
	on the successor, which produced an unnecessary deadlock. */

5017 5018
	if (lock_rec_other_has_conflicting(
		    LOCK_X | LOCK_GAP | LOCK_INSERT_INTENTION,
5019
		    block, next_rec_heap_no, trx)) {
osku's avatar
osku committed
5020 5021 5022

		/* Note that we may get DB_SUCCESS also here! */
		err = lock_rec_enqueue_waiting(LOCK_X | LOCK_GAP
5023
					       | LOCK_INSERT_INTENTION,
5024 5025
					       block, next_rec_heap_no,
					       index, thr);
osku's avatar
osku committed
5026 5027 5028 5029 5030 5031
	} else {
		err = DB_SUCCESS;
	}

	lock_mutex_exit_kernel();

5032
	if ((err == DB_SUCCESS) && !dict_index_is_clust(index)) {
osku's avatar
osku committed
5033
		/* Update the page max trx id field */
marko's avatar
marko committed
5034
		page_update_max_trx_id(block,
5035
				       buf_block_get_page_zip(block),
5036
				       trx->id, mtr);
osku's avatar
osku committed
5037 5038 5039 5040 5041 5042 5043
	}

#ifdef UNIV_DEBUG
	{
		mem_heap_t*	heap		= NULL;
		ulint		offsets_[REC_OFFS_NORMAL_SIZE];
		const ulint*	offsets;
5044
		rec_offs_init(offsets_);
osku's avatar
osku committed
5045 5046

		offsets = rec_get_offsets(next_rec, index, offsets_,
5047
					  ULINT_UNDEFINED, &heap);
5048 5049
		ut_ad(lock_rec_queue_validate(block,
					      next_rec, index, offsets));
osku's avatar
osku committed
5050 5051 5052 5053 5054 5055 5056 5057 5058
		if (UNIV_LIKELY_NULL(heap)) {
			mem_heap_free(heap);
		}
	}
#endif /* UNIV_DEBUG */

	return(err);
}

5059
/*********************************************************************//**
osku's avatar
osku committed
5060 5061 5062 5063 5064 5065 5066
If a transaction has an implicit x-lock on a record, but no explicit x-lock
set on the record, sets one for it. NOTE that in the case of a secondary
index, the kernel mutex may get temporarily released. */
static
void
lock_rec_convert_impl_to_expl(
/*==========================*/
5067 5068 5069 5070
	const buf_block_t*	block,	/*!< in: buffer block of rec */
	const rec_t*		rec,	/*!< in: user record on page */
	dict_index_t*		index,	/*!< in: index of record */
	const ulint*		offsets)/*!< in: rec_get_offsets(rec, index) */
osku's avatar
osku committed
5071 5072 5073 5074 5075 5076 5077 5078
{
	trx_t*	impl_trx;

	ut_ad(mutex_own(&kernel_mutex));
	ut_ad(page_rec_is_user_rec(rec));
	ut_ad(rec_offs_validate(rec, index, offsets));
	ut_ad(!page_rec_is_comp(rec) == !rec_offs_comp(offsets));

5079
	if (dict_index_is_clust(index)) {
osku's avatar
osku committed
5080 5081
		impl_trx = lock_clust_rec_some_has_impl(rec, index, offsets);
	} else {
5082 5083
		impl_trx = lock_sec_rec_some_has_impl_off_kernel(
			rec, index, offsets);
osku's avatar
osku committed
5084 5085 5086
	}

	if (impl_trx) {
5087 5088
		ulint	heap_no = page_rec_get_heap_no(rec);

osku's avatar
osku committed
5089 5090 5091
		/* If the transaction has no explicit x-lock set on the
		record, set one for it */

5092
		if (!lock_rec_has_expl(LOCK_X | LOCK_REC_NOT_GAP, block,
5093
				       heap_no, impl_trx)) {
osku's avatar
osku committed
5094

5095 5096
			lock_rec_add_to_queue(
				LOCK_REC | LOCK_X | LOCK_REC_NOT_GAP,
5097
				block, heap_no, index, impl_trx);
osku's avatar
osku committed
5098 5099 5100 5101
		}
	}
}

5102
/*********************************************************************//**
osku's avatar
osku committed
5103 5104 5105 5106 5107
Checks if locks of other transactions prevent an immediate modify (update,
delete mark, or delete unmark) of a clustered index record. If they do,
first tests if the query thread should anyway be suspended for some
reason; if not, then puts the transaction and the query thread to the
lock wait state and inserts a waiting request for a record x-lock to the
5108 5109
lock queue.
@return	DB_SUCCESS, DB_LOCK_WAIT, DB_DEADLOCK, or DB_QUE_THR_SUSPENDED */
5110
UNIV_INTERN
osku's avatar
osku committed
5111 5112 5113
ulint
lock_clust_rec_modify_check_and_lock(
/*=================================*/
5114
	ulint			flags,	/*!< in: if BTR_NO_LOCKING_FLAG
5115
					bit is set, does nothing */
5116 5117
	const buf_block_t*	block,	/*!< in: buffer block of rec */
	const rec_t*		rec,	/*!< in: record which should be
5118
					modified */
5119 5120 5121
	dict_index_t*		index,	/*!< in: clustered index */
	const ulint*		offsets,/*!< in: rec_get_offsets(rec, index) */
	que_thr_t*		thr)	/*!< in: query thread */
osku's avatar
osku committed
5122 5123
{
	ulint	err;
5124
	ulint	heap_no;
osku's avatar
osku committed
5125 5126

	ut_ad(rec_offs_validate(rec, index, offsets));
5127
	ut_ad(dict_index_is_clust(index));
5128
	ut_ad(block->frame == page_align(rec));
osku's avatar
osku committed
5129 5130 5131 5132 5133 5134

	if (flags & BTR_NO_LOCKING_FLAG) {

		return(DB_SUCCESS);
	}

5135 5136 5137 5138
	heap_no = rec_offs_comp(offsets)
		? rec_get_heap_no_new(rec)
		: rec_get_heap_no_old(rec);

osku's avatar
osku committed
5139 5140 5141 5142 5143 5144 5145
	lock_mutex_enter_kernel();

	ut_ad(lock_table_has(thr_get_trx(thr), index->table, LOCK_IX));

	/* If a transaction has no explicit x-lock set on the record, set one
	for it */

5146
	lock_rec_convert_impl_to_expl(block, rec, index, offsets);
osku's avatar
osku committed
5147

5148 5149
	err = lock_rec_lock(TRUE, LOCK_X | LOCK_REC_NOT_GAP,
			    block, heap_no, index, thr);
osku's avatar
osku committed
5150 5151 5152

	lock_mutex_exit_kernel();

5153
	ut_ad(lock_rec_queue_validate(block, rec, index, offsets));
osku's avatar
osku committed
5154 5155 5156 5157

	return(err);
}

5158
/*********************************************************************//**
osku's avatar
osku committed
5159
Checks if locks of other transactions prevent an immediate modify (delete
5160 5161
mark or delete unmark) of a secondary index record.
@return	DB_SUCCESS, DB_LOCK_WAIT, DB_DEADLOCK, or DB_QUE_THR_SUSPENDED */
5162
UNIV_INTERN
osku's avatar
osku committed
5163 5164 5165
ulint
lock_sec_rec_modify_check_and_lock(
/*===============================*/
5166
	ulint		flags,	/*!< in: if BTR_NO_LOCKING_FLAG
5167
				bit is set, does nothing */
5168 5169
	buf_block_t*	block,	/*!< in/out: buffer block of rec */
	const rec_t*	rec,	/*!< in: record which should be
5170 5171 5172 5173
				modified; NOTE: as this is a secondary
				index, we always have to modify the
				clustered index record first: see the
				comment below */
5174 5175 5176
	dict_index_t*	index,	/*!< in: secondary index */
	que_thr_t*	thr,	/*!< in: query thread */
	mtr_t*		mtr)	/*!< in/out: mini-transaction */
osku's avatar
osku committed
5177 5178
{
	ulint	err;
5179
	ulint	heap_no;
5180

5181 5182 5183
	ut_ad(!dict_index_is_clust(index));
	ut_ad(block->frame == page_align(rec));

osku's avatar
osku committed
5184 5185 5186 5187 5188
	if (flags & BTR_NO_LOCKING_FLAG) {

		return(DB_SUCCESS);
	}

5189 5190
	heap_no = page_rec_get_heap_no(rec);

osku's avatar
osku committed
5191 5192 5193 5194 5195 5196 5197 5198 5199
	/* Another transaction cannot have an implicit lock on the record,
	because when we come here, we already have modified the clustered
	index record, and this would not have been possible if another active
	transaction had modified this secondary index record. */

	lock_mutex_enter_kernel();

	ut_ad(lock_table_has(thr_get_trx(thr), index->table, LOCK_IX));

5200 5201
	err = lock_rec_lock(TRUE, LOCK_X | LOCK_REC_NOT_GAP,
			    block, heap_no, index, thr);
osku's avatar
osku committed
5202 5203 5204 5205 5206 5207 5208 5209

	lock_mutex_exit_kernel();

#ifdef UNIV_DEBUG
	{
		mem_heap_t*	heap		= NULL;
		ulint		offsets_[REC_OFFS_NORMAL_SIZE];
		const ulint*	offsets;
5210
		rec_offs_init(offsets_);
osku's avatar
osku committed
5211 5212

		offsets = rec_get_offsets(rec, index, offsets_,
5213
					  ULINT_UNDEFINED, &heap);
5214
		ut_ad(lock_rec_queue_validate(block, rec, index, offsets));
osku's avatar
osku committed
5215 5216 5217 5218 5219 5220 5221
		if (UNIV_LIKELY_NULL(heap)) {
			mem_heap_free(heap);
		}
	}
#endif /* UNIV_DEBUG */

	if (err == DB_SUCCESS) {
5222
		/* Update the page max trx id field */
marko's avatar
marko committed
5223
		page_update_max_trx_id(block,
5224
				       buf_block_get_page_zip(block),
5225
				       thr_get_trx(thr)->id, mtr);
osku's avatar
osku committed
5226 5227 5228 5229 5230
	}

	return(err);
}

5231
/*********************************************************************//**
osku's avatar
osku committed
5232
Like the counterpart for a clustered index below, but now we read a
5233 5234
secondary index record.
@return	DB_SUCCESS, DB_LOCK_WAIT, DB_DEADLOCK, or DB_QUE_THR_SUSPENDED */
5235
UNIV_INTERN
osku's avatar
osku committed
5236 5237 5238
ulint
lock_sec_rec_read_check_and_lock(
/*=============================*/
5239
	ulint			flags,	/*!< in: if BTR_NO_LOCKING_FLAG
5240
					bit is set, does nothing */
5241 5242
	const buf_block_t*	block,	/*!< in: buffer block of rec */
	const rec_t*		rec,	/*!< in: user record or page
5243 5244 5245
					supremum record which should
					be read or passed over by a
					read cursor */
5246 5247 5248
	dict_index_t*		index,	/*!< in: secondary index */
	const ulint*		offsets,/*!< in: rec_get_offsets(rec, index) */
	enum lock_mode		mode,	/*!< in: mode of the lock which
5249 5250 5251 5252
					the read cursor should set on
					records: LOCK_S or LOCK_X; the
					latter is possible in
					SELECT FOR UPDATE */
5253
	ulint			gap_mode,/*!< in: LOCK_ORDINARY, LOCK_GAP, or
5254
					LOCK_REC_NOT_GAP */
5255
	que_thr_t*		thr)	/*!< in: query thread */
osku's avatar
osku committed
5256 5257
{
	ulint	err;
5258
	ulint	heap_no;
osku's avatar
osku committed
5259

5260
	ut_ad(!dict_index_is_clust(index));
5261
	ut_ad(block->frame == page_align(rec));
osku's avatar
osku committed
5262 5263
	ut_ad(page_rec_is_user_rec(rec) || page_rec_is_supremum(rec));
	ut_ad(rec_offs_validate(rec, index, offsets));
marko's avatar
marko committed
5264
	ut_ad(mode == LOCK_X || mode == LOCK_S);
osku's avatar
osku committed
5265 5266 5267 5268 5269 5270

	if (flags & BTR_NO_LOCKING_FLAG) {

		return(DB_SUCCESS);
	}

5271 5272
	heap_no = page_rec_get_heap_no(rec);

osku's avatar
osku committed
5273 5274 5275
	lock_mutex_enter_kernel();

	ut_ad(mode != LOCK_X
5276
	      || lock_table_has(thr_get_trx(thr), index->table, LOCK_IX));
osku's avatar
osku committed
5277
	ut_ad(mode != LOCK_S
5278
	      || lock_table_has(thr_get_trx(thr), index->table, LOCK_IS));
osku's avatar
osku committed
5279 5280 5281 5282 5283

	/* Some transaction may have an implicit x-lock on the record only
	if the max trx id for the page >= min trx id for the trx list or a
	database recovery is running. */

5284
	if (((ut_dulint_cmp(page_get_max_trx_id(block->frame),
5285 5286 5287
			    trx_list_get_min_trx_id()) >= 0)
	     || recv_recovery_is_on())
	    && !page_rec_is_supremum(rec)) {
osku's avatar
osku committed
5288

5289
		lock_rec_convert_impl_to_expl(block, rec, index, offsets);
osku's avatar
osku committed
5290 5291
	}

5292 5293
	err = lock_rec_lock(FALSE, mode | gap_mode,
			    block, heap_no, index, thr);
osku's avatar
osku committed
5294 5295 5296

	lock_mutex_exit_kernel();

5297
	ut_ad(lock_rec_queue_validate(block, rec, index, offsets));
osku's avatar
osku committed
5298 5299 5300 5301

	return(err);
}

5302
/*********************************************************************//**
osku's avatar
osku committed
5303 5304 5305 5306 5307
Checks if locks of other transactions prevent an immediate read, or passing
over by a read cursor, of a clustered index record. If they do, first tests
if the query thread should anyway be suspended for some reason; if not, then
puts the transaction and the query thread to the lock wait state and inserts a
waiting request for a record lock to the lock queue. Sets the requested mode
5308 5309
lock on the record.
@return	DB_SUCCESS, DB_LOCK_WAIT, DB_DEADLOCK, or DB_QUE_THR_SUSPENDED */
5310
UNIV_INTERN
osku's avatar
osku committed
5311 5312 5313
ulint
lock_clust_rec_read_check_and_lock(
/*===============================*/
5314
	ulint			flags,	/*!< in: if BTR_NO_LOCKING_FLAG
5315
					bit is set, does nothing */
5316 5317
	const buf_block_t*	block,	/*!< in: buffer block of rec */
	const rec_t*		rec,	/*!< in: user record or page
5318 5319 5320
					supremum record which should
					be read or passed over by a
					read cursor */
5321 5322 5323
	dict_index_t*		index,	/*!< in: clustered index */
	const ulint*		offsets,/*!< in: rec_get_offsets(rec, index) */
	enum lock_mode		mode,	/*!< in: mode of the lock which
5324 5325 5326 5327
					the read cursor should set on
					records: LOCK_S or LOCK_X; the
					latter is possible in
					SELECT FOR UPDATE */
5328
	ulint			gap_mode,/*!< in: LOCK_ORDINARY, LOCK_GAP, or
5329
					LOCK_REC_NOT_GAP */
5330
	que_thr_t*		thr)	/*!< in: query thread */
osku's avatar
osku committed
5331 5332
{
	ulint	err;
5333
	ulint	heap_no;
osku's avatar
osku committed
5334

5335
	ut_ad(dict_index_is_clust(index));
5336
	ut_ad(block->frame == page_align(rec));
osku's avatar
osku committed
5337 5338
	ut_ad(page_rec_is_user_rec(rec) || page_rec_is_supremum(rec));
	ut_ad(gap_mode == LOCK_ORDINARY || gap_mode == LOCK_GAP
5339
	      || gap_mode == LOCK_REC_NOT_GAP);
osku's avatar
osku committed
5340 5341 5342 5343 5344 5345 5346
	ut_ad(rec_offs_validate(rec, index, offsets));

	if (flags & BTR_NO_LOCKING_FLAG) {

		return(DB_SUCCESS);
	}

5347 5348
	heap_no = page_rec_get_heap_no(rec);

osku's avatar
osku committed
5349 5350 5351
	lock_mutex_enter_kernel();

	ut_ad(mode != LOCK_X
5352
	      || lock_table_has(thr_get_trx(thr), index->table, LOCK_IX));
osku's avatar
osku committed
5353
	ut_ad(mode != LOCK_S
5354
	      || lock_table_has(thr_get_trx(thr), index->table, LOCK_IS));
5355

5356
	if (UNIV_LIKELY(heap_no != PAGE_HEAP_NO_SUPREMUM)) {
5357

5358
		lock_rec_convert_impl_to_expl(block, rec, index, offsets);
osku's avatar
osku committed
5359 5360
	}

5361 5362
	err = lock_rec_lock(FALSE, mode | gap_mode,
			    block, heap_no, index, thr);
osku's avatar
osku committed
5363 5364 5365

	lock_mutex_exit_kernel();

5366
	ut_ad(lock_rec_queue_validate(block, rec, index, offsets));
osku's avatar
osku committed
5367 5368 5369

	return(err);
}
5370
/*********************************************************************//**
osku's avatar
osku committed
5371 5372 5373 5374 5375 5376 5377
Checks if locks of other transactions prevent an immediate read, or passing
over by a read cursor, of a clustered index record. If they do, first tests
if the query thread should anyway be suspended for some reason; if not, then
puts the transaction and the query thread to the lock wait state and inserts a
waiting request for a record lock to the lock queue. Sets the requested mode
lock on the record. This is an alternative version of
lock_clust_rec_read_check_and_lock() that does not require the parameter
5378 5379
"offsets".
@return	DB_SUCCESS, DB_LOCK_WAIT, DB_DEADLOCK, or DB_QUE_THR_SUSPENDED */
5380
UNIV_INTERN
osku's avatar
osku committed
5381 5382 5383
ulint
lock_clust_rec_read_check_and_lock_alt(
/*===================================*/
5384
	ulint			flags,	/*!< in: if BTR_NO_LOCKING_FLAG
5385
					bit is set, does nothing */
5386 5387
	const buf_block_t*	block,	/*!< in: buffer block of rec */
	const rec_t*		rec,	/*!< in: user record or page
5388 5389 5390
					supremum record which should
					be read or passed over by a
					read cursor */
5391 5392
	dict_index_t*		index,	/*!< in: clustered index */
	enum lock_mode		mode,	/*!< in: mode of the lock which
5393 5394 5395 5396
					the read cursor should set on
					records: LOCK_S or LOCK_X; the
					latter is possible in
					SELECT FOR UPDATE */
5397
	ulint			gap_mode,/*!< in: LOCK_ORDINARY, LOCK_GAP, or
5398
					LOCK_REC_NOT_GAP */
5399
	que_thr_t*		thr)	/*!< in: query thread */
osku's avatar
osku committed
5400 5401 5402 5403 5404
{
	mem_heap_t*	tmp_heap	= NULL;
	ulint		offsets_[REC_OFFS_NORMAL_SIZE];
	ulint*		offsets		= offsets_;
	ulint		ret;
5405
	rec_offs_init(offsets_);
osku's avatar
osku committed
5406 5407

	offsets = rec_get_offsets(rec, index, offsets,
5408
				  ULINT_UNDEFINED, &tmp_heap);
5409
	ret = lock_clust_rec_read_check_and_lock(flags, block, rec, index,
5410
						 offsets, mode, gap_mode, thr);
osku's avatar
osku committed
5411 5412 5413 5414 5415
	if (tmp_heap) {
		mem_heap_free(tmp_heap);
	}
	return(ret);
}
5416

5417
/*******************************************************************//**
sunny's avatar
sunny committed
5418 5419 5420 5421 5422
Release the last lock from the transaction's autoinc locks. */
UNIV_INLINE
void
lock_release_autoinc_last_lock(
/*===========================*/
5423
	ib_vector_t*	autoinc_locks)	/*!< in/out: vector of AUTOINC locks */
sunny's avatar
sunny committed
5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444
{
	ulint		last;
	lock_t*		lock;

	ut_ad(mutex_own(&kernel_mutex));
	ut_a(!ib_vector_is_empty(autoinc_locks));

	/* The lock to be release must be the last lock acquired. */
	last = ib_vector_size(autoinc_locks) - 1;
	lock = ib_vector_get(autoinc_locks, last);

	/* Should have only AUTOINC locks in the vector. */
	ut_a(lock_get_mode(lock) == LOCK_AUTO_INC);
	ut_a(lock_get_type(lock) == LOCK_TABLE);

	ut_a(lock->un_member.tab_lock.table != NULL);

	/* This will remove the lock from the trx autoinc_locks too. */
	lock_table_dequeue(lock);
}

5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458
/*******************************************************************//**
Check if a transaction holds any autoinc locks. 
@return TRUE if the transaction holds any AUTOINC locks. */
UNIV_INTERN
ibool
lock_trx_holds_autoinc_locks(
/*=========================*/
	const trx_t*	trx)		/*!< in: transaction */
{
	ut_a(trx->autoinc_locks != NULL);

	return(!ib_vector_is_empty(trx->autoinc_locks));
}

5459
/*******************************************************************//**
sunny's avatar
sunny committed
5460 5461 5462 5463 5464
Release all the transaction's autoinc locks. */
UNIV_INTERN
void
lock_release_autoinc_locks(
/*=======================*/
5465
	trx_t*		trx)		/*!< in/out: transaction */
sunny's avatar
sunny committed
5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484
{
	ut_ad(mutex_own(&kernel_mutex));

	ut_a(trx->autoinc_locks != NULL);

	/* We release the locks in the reverse order. This is to
	avoid searching the vector for the element to delete at
	the lower level. See (lock_table_remove_low()) for details. */
	while (!ib_vector_is_empty(trx->autoinc_locks)) {

		/* lock_table_remove_low() will also remove the lock from
		the transaction's autoinc_locks vector. */
		lock_release_autoinc_last_lock(trx->autoinc_locks);
	}

	/* Should release all locks. */
	ut_a(ib_vector_is_empty(trx->autoinc_locks));
}

5485
/*******************************************************************//**
5486
Gets the type of a lock. Non-inline version for using outside of the
5487 5488
lock module.
@return	LOCK_TABLE or LOCK_REC */
5489
UNIV_INTERN
5490 5491 5492
ulint
lock_get_type(
/*==========*/
5493
	const lock_t*	lock)	/*!< in: lock */
5494 5495 5496 5497
{
	return(lock_get_type_low(lock));
}

5498
/*******************************************************************//**
5499 5500
Gets the id of the transaction owning a lock.
@return	transaction id */
5501
UNIV_INTERN
5502 5503 5504
ullint
lock_get_trx_id(
/*============*/
5505
	const lock_t*	lock)	/*!< in: lock */
5506 5507 5508 5509
{
	return(trx_get_id(lock->trx));
}

5510
/*******************************************************************//**
5511
Gets the mode of a lock in a human readable string.
5512 5513
The string should not be free()'d or modified.
@return	lock mode */
5514
UNIV_INTERN
5515 5516 5517
const char*
lock_get_mode_str(
/*==============*/
5518
	const lock_t*	lock)	/*!< in: lock */
5519
{
vasil's avatar
vasil committed
5520 5521 5522 5523 5524
	ibool	is_gap_lock;

	is_gap_lock = lock_get_type_low(lock) == LOCK_REC
		&& lock_rec_get_gap(lock);

5525 5526
	switch (lock_get_mode(lock)) {
	case LOCK_S:
vasil's avatar
vasil committed
5527 5528 5529 5530 5531
		if (is_gap_lock) {
			return("S,GAP");
		} else {
			return("S");
		}
5532
	case LOCK_X:
vasil's avatar
vasil committed
5533 5534 5535 5536 5537
		if (is_gap_lock) {
			return("X,GAP");
		} else {
			return("X");
		}
5538
	case LOCK_IS:
vasil's avatar
vasil committed
5539 5540 5541 5542 5543
		if (is_gap_lock) {
			return("IS,GAP");
		} else {
			return("IS");
		}
5544
	case LOCK_IX:
vasil's avatar
vasil committed
5545 5546 5547 5548 5549
		if (is_gap_lock) {
			return("IX,GAP");
		} else {
			return("IX");
		}
5550 5551 5552 5553 5554 5555 5556
	case LOCK_AUTO_INC:
		return("AUTO_INC");
	default:
		return("UNKNOWN");
	}
}

5557
/*******************************************************************//**
5558
Gets the type of a lock in a human readable string.
5559 5560
The string should not be free()'d or modified.
@return	lock type */
5561
UNIV_INTERN
5562 5563 5564
const char*
lock_get_type_str(
/*==============*/
5565
	const lock_t*	lock)	/*!< in: lock */
5566
{
5567
	switch (lock_get_type_low(lock)) {
5568 5569 5570 5571 5572 5573 5574 5575 5576
	case LOCK_REC:
		return("RECORD");
	case LOCK_TABLE:
		return("TABLE");
	default:
		return("UNKNOWN");
	}
}

5577
/*******************************************************************//**
5578 5579
Gets the table on which the lock is.
@return	table */
5580 5581 5582 5583
UNIV_INLINE
dict_table_t*
lock_get_table(
/*===========*/
5584
	const lock_t*	lock)	/*!< in: lock */
5585
{
5586
	switch (lock_get_type_low(lock)) {
5587 5588 5589 5590 5591 5592
	case LOCK_REC:
		return(lock->index->table);
	case LOCK_TABLE:
		return(lock->un_member.tab_lock.table);
	default:
		ut_error;
5593
		return(NULL);
5594 5595 5596
	}
}

5597
/*******************************************************************//**
5598 5599
Gets the id of the table on which the lock is.
@return	id of the table */
5600
UNIV_INTERN
5601 5602 5603
ullint
lock_get_table_id(
/*==============*/
5604
	const lock_t*	lock)	/*!< in: lock */
5605 5606 5607 5608 5609 5610 5611 5612
{
	dict_table_t*	table;

	table = lock_get_table(lock);

	return((ullint)ut_conv_dulint_to_longlong(table->id));
}

5613
/*******************************************************************//**
5614
Gets the name of the table on which the lock is.
5615 5616
The string should not be free()'d or modified.
@return	name of the table */
5617
UNIV_INTERN
5618 5619 5620
const char*
lock_get_table_name(
/*================*/
5621
	const lock_t*	lock)	/*!< in: lock */
5622 5623 5624 5625 5626 5627 5628 5629
{
	dict_table_t*	table;

	table = lock_get_table(lock);

	return(table->name);
}

5630
/*******************************************************************//**
5631 5632
For a record lock, gets the index on which the lock is.
@return	index */
5633
UNIV_INTERN
vasil's avatar
vasil committed
5634 5635 5636
const dict_index_t*
lock_rec_get_index(
/*===============*/
5637
	const lock_t*	lock)	/*!< in: lock */
vasil's avatar
vasil committed
5638 5639 5640 5641 5642 5643
{
	ut_a(lock_get_type_low(lock) == LOCK_REC);

	return(lock->index);
}

5644
/*******************************************************************//**
5645
For a record lock, gets the name of the index on which the lock is.
5646 5647
The string should not be free()'d or modified.
@return	name of the index */
5648
UNIV_INTERN
5649 5650 5651
const char*
lock_rec_get_index_name(
/*====================*/
5652
	const lock_t*	lock)	/*!< in: lock */
5653
{
5654
	ut_a(lock_get_type_low(lock) == LOCK_REC);
5655 5656 5657 5658

	return(lock->index->name);
}

5659
/*******************************************************************//**
5660 5661
For a record lock, gets the tablespace number on which the lock is.
@return	tablespace number */
5662
UNIV_INTERN
5663 5664 5665
ulint
lock_rec_get_space_id(
/*==================*/
5666
	const lock_t*	lock)	/*!< in: lock */
5667
{
5668
	ut_a(lock_get_type_low(lock) == LOCK_REC);
5669 5670 5671 5672

	return(lock->un_member.rec_lock.space);
}

5673
/*******************************************************************//**
5674 5675
For a record lock, gets the page number on which the lock is.
@return	page number */
5676
UNIV_INTERN
5677 5678 5679
ulint
lock_rec_get_page_no(
/*=================*/
5680
	const lock_t*	lock)	/*!< in: lock */
5681
{
5682
	ut_a(lock_get_type_low(lock) == LOCK_REC);
5683 5684 5685

	return(lock->un_member.rec_lock.page_no);
}