fil0fil.c 111 KB
Newer Older
1
/******************************************************
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2
The tablespace memory cache
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

(c) 1995 Innobase Oy

Created 10/25/1995 Heikki Tuuri
*******************************************************/

#include "fil0fil.h"

#include "mem0mem.h"
#include "sync0sync.h"
#include "hash0hash.h"
#include "os0file.h"
#include "os0sync.h"
#include "mach0data.h"
#include "ibuf0ibuf.h"
#include "buf0buf.h"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
19 20
#include "buf0flu.h"
#include "buf0lru.h"
21 22 23
#include "log0log.h"
#include "log0recv.h"
#include "fsp0fsp.h"
24
#include "srv0srv.h"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
25
#include "srv0start.h"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
26 27
#include "mtr0mtr.h"
#include "mtr0log.h"
28

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
29
	 
30
/*
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
31 32
		IMPLEMENTATION OF THE TABLESPACE MEMORY CACHE
		=============================================
33

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
34
The tablespace cache is responsible for providing fast read/write access to
35 36 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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
tablespaces and logs of the database. File creation and deletion is done
in other modules which know more of the logic of the operation, however.

A tablespace consists of a chain of files. The size of the files does not
have to be divisible by the database block size, because we may just leave
the last incomplete block unused. When a new file is appended to the
tablespace, the maximum size of the file is also specified. At the moment,
we think that it is best to extend the file to its maximum size already at
the creation of the file, because then we can avoid dynamically extending
the file when more space is needed for the tablespace.

A block's position in the tablespace is specified with a 32-bit unsigned
integer. The files in the chain are thought to be catenated, and the block
corresponding to an address n is the nth block in the catenated file (where
the first block is named the 0th block, and the incomplete block fragments
at the end of files are not taken into account). A tablespace can be extended
by appending a new file at the end of the chain.

Our tablespace concept is similar to the one of Oracle.

To acquire more speed in disk transfers, a technique called disk striping is
sometimes used. This means that logical block addresses are divided in a
round-robin fashion across several disks. Windows NT supports disk striping,
so there we do not need to support it in the database. Disk striping is
implemented in hardware in RAID disks. We conclude that it is not necessary
to implement it in the database. Oracle 7 does not support disk striping,
either.

Another trick used at some database sites is replacing tablespace files by
raw disks, that is, the whole physical disk drive, or a partition of it, is
opened as a single file, and it is accessed through byte offsets calculated
from the start of the disk or the partition. This is recommended in some
books on database tuning to achieve more speed in i/o. Using raw disk
certainly prevents the OS from fragmenting disk space, but it is not clear
if it really adds speed. We measured on the Pentium 100 MHz + NT + NTFS file
system + EIDE Conner disk only a negligible difference in speed when reading
from a file, versus reading from a raw disk. 

To have fast access to a tablespace or a log file, we put the data structures
to a hash table. Each tablespace and log file is given an unique 32-bit
identifier.

Some operating systems do not support many open files at the same time,
though NT seems to tolerate at least 900 open files. Therefore, we put the
open files in an LRU-list. If we need to open another file, we may close the
file at the end of the LRU-list. When an i/o-operation is pending on a file,
the file cannot be closed. We take the file nodes with pending i/o-operations
out of the LRU-list and keep a count of pending operations. When an operation
completes, we decrement the count and return the file node to the LRU-list if
the count drops to zero. */

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
86
/* When mysqld is run, the default directory "." is the mysqld datadir,
87 88
but in the MySQL Embedded Server Library and ibbackup it is not the default
directory, and we must set the base file path explicitly */
monty@mishka.local's avatar
monty@mishka.local committed
89
const char*	fil_path_to_mysql_datadir	= ".";
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
90

91 92 93
ulint	fil_n_pending_log_flushes		= 0;
ulint	fil_n_pending_tablespace_flushes	= 0;

94 95 96
/* Null file address */
fil_addr_t	fil_addr_null = {FIL_NULL, 0};

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
97
/* File node of a tablespace or the log data space */
98 99
typedef	struct fil_node_struct	fil_node_t;
struct fil_node_struct {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
100 101 102
	fil_space_t*	space;	/* backpointer to the space where this node
				belongs */
	char*		name;	/* path to the file */
103 104
	ibool		open;	/* TRUE if file open */
	os_file_t	handle;	/* OS handle to the file, if file open */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
105 106 107 108
	ibool		is_raw_disk;/* TRUE if the 'file' is actually a raw
				device or a raw disk partition */
	ulint		size;	/* size of the file in database pages, 0 if
				not known yet; the possible last incomplete
109
				megabyte may be ignored if space == 0 */
110
	ulint		n_pending;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
111 112 113 114 115 116 117 118 119 120 121
				/* count of pending i/o's on this file;
				closing of the file is not allowed if
				this is > 0 */
	ulint		n_pending_flushes;
				/* count of pending flushes on this file;
				closing of the file is not allowed if
				this is > 0 */	
	ib_longlong	modification_counter;/* when we write to the file we
				increment this by one */
	ib_longlong	flush_counter;/* up to what modification_counter value
				we have flushed the modifications to disk */
122 123 124 125 126 127 128 129 130
	UT_LIST_NODE_T(fil_node_t) chain;
				/* link field for the file chain */
	UT_LIST_NODE_T(fil_node_t) LRU;
				/* link field for the LRU list */
	ulint		magic_n;
};

#define	FIL_NODE_MAGIC_N	89389

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
131
/* Tablespace or log data space: let us call them by a common name space */
132
struct fil_space_struct {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
133 134
	char*		name;	/* space name = the path to the first file in
				it */
135
	ulint		id;	/* space id */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
	ib_longlong	tablespace_version;
				/* in DISCARD/IMPORT this timestamp is used to
				check if we should ignore an insert buffer
				merge request for a page because it actually
				was for the previous incarnation of the
				space */
	ibool		mark;	/* this is set to TRUE at database startup if
				the space corresponds to a table in the InnoDB
				data dictionary; so we can print a warning of
				orphaned tablespaces */
	ibool		stop_ios;/* TRUE if we want to rename the .ibd file of
				tablespace and want to stop temporarily
				posting of new i/o requests on the file */
	ibool		stop_ibuf_merges;
				/* we set this TRUE when we start deleting a
				single-table tablespace */
	ibool		is_being_deleted;
				/* this is set to TRUE when we start
				deleting a single-table tablespace and its
				file; when this flag is set no further i/o
				or flush requests can be placed on this space,
				though there may be such requests still being
				processed on this space */
159 160 161
	ulint		purpose;/* FIL_TABLESPACE, FIL_LOG, or FIL_ARCH_LOG */
	UT_LIST_BASE_NODE_T(fil_node_t) chain;
				/* base node for the file chain */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
162
	ulint		size;	/* space size in pages; 0 if a single-table
163 164 165
				tablespace whose size we do not know yet;
				last incomplete megabytes in data files may be
				ignored if space == 0 */ 
166 167 168
	ulint		n_reserved_extents;
				/* number of reserved free extents for
				ongoing operations like B-tree page split */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
169 170 171 172 173 174 175 176
	ulint		n_pending_flushes; /* this is > 0 when flushing
				the tablespace to disk; dropping of the
				tablespace is forbidden if this is > 0 */
	ulint		n_pending_ibuf_merges;/* this is > 0 when merging
				insert buffer entries to a page so that we
				may need to access the ibuf bitmap page in the
				tablespade: dropping of the tablespace is
				forbidden if this is > 0 */
177
	hash_node_t	hash; 	/* hash chain node */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
178
	hash_node_t	name_hash;/* hash chain the name_hash table */
179 180 181 182 183 184 185 186 187 188 189
	rw_lock_t	latch;	/* latch protecting the file space storage
				allocation */
	UT_LIST_NODE_T(fil_space_t) space_list;
				/* list of all spaces */
	ibuf_data_t*	ibuf_data;
				/* insert buffer data */
	ulint		magic_n;
};

#define	FIL_SPACE_MAGIC_N	89472

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
190 191 192
/* The tablespace memory cache; also the totality of logs = the log data space,
is stored here; below we talk about tablespaces, but also the ib_logfiles
form a 'space' and it is handled here */
193 194 195

typedef	struct fil_system_struct	fil_system_t;
struct fil_system_struct {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
196
	mutex_t		mutex;		/* The mutex protecting the cache */
197
	hash_table_t*	spaces;		/* The hash table of spaces in the
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
198 199 200 201
					system; they are hashed on the space
					id */
	hash_table_t*	name_hash;	/* hash table based on the space
					name */
202 203
	UT_LIST_BASE_NODE_T(fil_node_t) LRU;
					/* base node for the LRU list of the
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
					most recently used open files with no
					pending i/o's; if we start an i/o on
					the file, we first remove it from this
					list, and return it to the start of
					the list when the i/o ends;
					log files and the system tablespace are
					not put to this list: they are opened
					after the startup, and kept open until
					shutdown */
	ulint		n_open;		/* number of files currently open */
	ulint		max_n_open;	/* n_open is not allowed to exceed
					this */
	ib_longlong	modification_counter;/* when we write to a file we
					increment this by one */
	ulint		max_assigned_id;/* maximum space id in the existing
					tables, or assigned during the time
					mysqld has been up; at an InnoDB
					startup we scan the data dictionary
					and set here the maximum of the
					space id's of the tables there */
	ib_longlong	tablespace_version;
					/* a counter which is incremented for
					every space object memory creation;
					every space mem object gets a
					'timestamp' from this; in DISCARD/
					IMPORT this is used to check if we
					should ignore an insert buffer merge
					request */
232 233 234 235
	UT_LIST_BASE_NODE_T(fil_space_t) space_list;
					/* list of all file spaces */
};

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
236 237
/* The tablespace memory cache. This variable is NULL before the module is
initialized. */
238 239
fil_system_t*	fil_system	= NULL;

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
240 241
/* The tablespace memory cache hash table size */
#define	FIL_SYSTEM_HASH_SIZE	50 /* TODO: make bigger! */
242 243


heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
244 245
/************************************************************************
NOTE: you must call fil_mutex_enter_and_prepare_for_io() first!
246

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
247 248 249 250 251
Prepares a file node for i/o. Opens the file if it is closed. Updates the
pending i/o's field in the node and the system appropriately. Takes the node
off the LRU list if it is in the LRU list. The caller must hold the fil_sys
mutex. */
static
252
void
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
fil_node_prepare_for_io(
/*====================*/
	fil_node_t*	node,	/* in: file node */
	fil_system_t*	system,	/* in: tablespace memory cache */
	fil_space_t*	space);	/* in: space */
/************************************************************************
Updates the data structures when an i/o operation finishes. Updates the
pending i/o's field in the node appropriately. */
static
void
fil_node_complete_io(
/*=================*/
	fil_node_t*	node,	/* in: file node */
	fil_system_t*	system,	/* in: tablespace memory cache */
	ulint		type);	/* in: OS_FILE_WRITE or OS_FILE_READ; marks
				the node as modified if
				type == OS_FILE_WRITE */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
270 271 272 273 274 275 276 277 278
/***********************************************************************
Checks if a single-table tablespace for a given table name exists in the
tablespace memory cache. */
static
ulint
fil_get_space_id_for_table(
/*=======================*/
				/* out: space id, ULINT_UNDEFINED if not
				found */
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
279
	const char*	name);	/* in: table name in the standard
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
280
				'databasename/tablename' format */
281 282


heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
283 284
/***********************************************************************
Returns the version number of a tablespace, -1 if not found. */
285

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
286 287 288 289 290 291 292 293 294 295
ib_longlong
fil_space_get_version(
/*==================*/
			/* out: version number, -1 if the tablespace does not
			exist in the memory cache */
	ulint	id)	/* in: space id */
{
	fil_system_t*	system		= fil_system;
	fil_space_t*	space;
	ib_longlong	version		= -1;
296

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
297
	ut_ad(system);
298

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
299
	mutex_enter(&(system->mutex));
300

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
301
	HASH_SEARCH(hash, system->spaces, id, space, space->id == id);
302

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
303 304
	if (space) {
		version = space->tablespace_version;
305 306
	}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
307
	mutex_exit(&(system->mutex));
308

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
309
	return(version);
310 311 312 313 314 315 316 317 318 319 320 321
}

/***********************************************************************
Returns the latch of a file space. */

rw_lock_t*
fil_space_get_latch(
/*================*/
			/* out: latch protecting storage allocation */
	ulint	id)	/* in: space id */
{
	fil_system_t*	system		= fil_system;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
322
	fil_space_t*	space;
323 324 325 326 327 328 329

	ut_ad(system);

	mutex_enter(&(system->mutex));

	HASH_SEARCH(hash, system->spaces, id, space, space->id == id);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
330 331
	ut_a(space);

332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
	mutex_exit(&(system->mutex));

	return(&(space->latch));
}

/***********************************************************************
Returns the type of a file space. */

ulint
fil_space_get_type(
/*===============*/
			/* out: FIL_TABLESPACE or FIL_LOG */
	ulint	id)	/* in: space id */
{
	fil_system_t*	system		= fil_system;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
347
	fil_space_t*	space;
348 349 350 351 352 353 354

	ut_ad(system);

	mutex_enter(&(system->mutex));

	HASH_SEARCH(hash, system->spaces, id, space, space->id == id);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
355 356
	ut_a(space);

357 358 359 360 361 362 363 364 365 366 367 368 369 370
	mutex_exit(&(system->mutex));

	return(space->purpose);
}

/***********************************************************************
Returns the ibuf data of a file space. */

ibuf_data_t*
fil_space_get_ibuf_data(
/*====================*/
			/* out: ibuf data for this space */
	ulint	id)	/* in: space id */
{
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
371
	fil_system_t*	system		= fil_system;
372 373 374 375
	fil_space_t*	space;

	ut_ad(system);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
376 377
	ut_a(id == 0);

378 379 380 381 382 383
	mutex_enter(&(system->mutex));

	HASH_SEARCH(hash, system->spaces, id, space, space->id == id);

	mutex_exit(&(system->mutex));

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
384 385
	ut_a(space);

386 387 388 389 390 391 392 393 394
	return(space->ibuf_data);
}

/***********************************************************************
Appends a new file to the chain of files of a space. File must be closed. */

void
fil_node_create(
/*============*/
395 396 397 398 399 400
	const char*	name,	/* in: file name (file must be closed) */
	ulint		size,	/* in: file size in database blocks, rounded
				downwards to an integer */
	ulint		id,	/* in: space id where to append */
	ibool		is_raw)	/* in: TRUE if a raw device or
				a raw disk partition */
401
{
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
402
	fil_system_t*	system	= fil_system;
403 404 405 406 407 408 409 410 411 412
	fil_node_t*	node;
	fil_space_t*	space;

	ut_a(system);
	ut_a(name);

	mutex_enter(&(system->mutex));

	node = mem_alloc(sizeof(fil_node_t));

413
	node->name = mem_strdup(name);
414
	node->open = FALSE;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
415 416 417 418

	ut_a(!is_raw || srv_start_raw_disk_in_use);

	node->is_raw_disk = is_raw;
419 420 421
	node->size = size;
	node->magic_n = FIL_NODE_MAGIC_N;
	node->n_pending = 0;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
422
	node->n_pending_flushes = 0;
423

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
424 425
	node->modification_counter = 0;
	node->flush_counter = 0;
426 427 428
	
	HASH_SEARCH(hash, system->spaces, id, space, space->id == id);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
429 430 431 432
	if (!space) {
		ut_print_timestamp(stderr);
		fprintf(stderr,
"  InnoDB: Error: Could not find tablespace %lu for\n"
433 434 435
"InnoDB: file ", (ulong) id);
		ut_print_filename(stderr, name);
		fputs(" in the tablespace memory cache.\n", stderr);
monty@mishka.local's avatar
monty@mishka.local committed
436
		mem_free(node->name);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
437 438 439 440 441 442 443 444

		mem_free(node);

		mutex_exit(&(system->mutex));

		return;
	}

445 446
	space->size += size;

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
447 448
	node->space = space;

449 450 451 452 453
	UT_LIST_ADD_LAST(chain, space->chain, node);
				
	mutex_exit(&(system->mutex));
}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
/************************************************************************
Opens a the file of a node of a tablespace. The caller must own the fil_system
mutex. */
static
void
fil_node_open_file(
/*===============*/
	fil_node_t*	node,	/* in: file node */
	fil_system_t*	system,	/* in: tablespace memory cache */
	fil_space_t*	space)	/* in: space */
{
	ib_longlong	size_bytes;
	ulint		size_low;
	ulint		size_high;
	ibool		ret;

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
470
#ifdef UNIV_SYNC_DEBUG
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
471
	ut_ad(mutex_own(&(system->mutex)));
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
472
#endif /* UNIV_SYNC_DEBUG */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
	ut_a(node->n_pending == 0);
	ut_a(node->open == FALSE);

	/* printf("Opening file %s\n", node->name); */

	if (space->purpose == FIL_LOG) {	
		node->handle = os_file_create(node->name, OS_FILE_OPEN,
					OS_FILE_AIO, OS_LOG_FILE, &ret);
	} else if (node->is_raw_disk) {
		node->handle = os_file_create(node->name,
				        OS_FILE_OPEN_RAW,
					OS_FILE_AIO, OS_DATA_FILE, &ret);
	} else {
		node->handle = os_file_create(node->name, OS_FILE_OPEN,
					OS_FILE_AIO, OS_DATA_FILE, &ret);
	}
		
	ut_a(ret);
		
	node->open = TRUE;

	system->n_open++;

	if (node->size == 0) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
497 498 499 500 501 502 503 504
		os_file_get_size(node->handle, &size_low, &size_high);

		size_bytes = (((ib_longlong)size_high) << 32)
				     		+ (ib_longlong)size_low;
#ifdef UNIV_HOTBACKUP
		node->size = (ulint) (size_bytes / UNIV_PAGE_SIZE);

#else
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
505 506 507 508 509 510 511 512 513 514 515
		/* It must be a single-table tablespace and we do not know the
		size of the file yet */

		ut_a(space->id != 0);

		if (size_bytes >= FSP_EXTENT_SIZE * UNIV_PAGE_SIZE) {
			node->size = (ulint) ((size_bytes / (1024 * 1024))
					   * ((1024 * 1024) / UNIV_PAGE_SIZE));
		} else {
			node->size = (ulint) (size_bytes / UNIV_PAGE_SIZE);
		}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
516 517
#endif
		space->size += node->size;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
518 519 520 521 522 523 524 525
	}

	if (space->purpose == FIL_TABLESPACE && space->id != 0) {
		/* Put the node to the LRU list */
		UT_LIST_ADD_FIRST(LRU, system->LRU, node);
	}
}

526 527 528 529
/**************************************************************************
Closes a file. */
static
void
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
530 531
fil_node_close_file(
/*================*/
532
	fil_node_t*	node,	/* in: file node */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
533
	fil_system_t*	system)	/* in: tablespace memory cache */
534 535 536 537
{
	ibool	ret;

	ut_ad(node && system);
538
#ifdef UNIV_SYNC_DEBUG
539
	ut_ad(mutex_own(&(system->mutex)));
540
#endif /* UNIV_SYNC_DEBUG */
541 542
	ut_a(node->open);
	ut_a(node->n_pending == 0);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
543
	ut_a(node->n_pending_flushes == 0);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
544
	ut_a(node->modification_counter == node->flush_counter);
545 546 547 548

	ret = os_file_close(node->handle);
	ut_a(ret);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
549 550
	/* printf("Closing file %s\n", node->name); */

551
	node->open = FALSE;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
	ut_a(system->n_open > 0);
	system->n_open--;

	if (node->space->purpose == FIL_TABLESPACE && node->space->id != 0) {
		ut_a(UT_LIST_GET_LEN(system->LRU) > 0);

		/* The node is in the LRU list, remove it */
		UT_LIST_REMOVE(LRU, system->LRU, node);
	}
}

/************************************************************************
Tries to close a file in the LRU list. The caller must hold the fil_sys
mutex. */
static
ibool
fil_try_to_close_file_in_LRU(
/*=========================*/
				/* out: TRUE if success, FALSE if should retry
				later; since i/o's generally complete in < 
				100 ms, and as InnoDB writes at most 128 pages
				from the buffer pool in a batch, and then
				immediately flushes the files, there is a good
				chance that the next time we find a suitable
				node from the LRU list */
	ibool	print_info)	/* in: if TRUE, prints information why it
				cannot close a file */
{
	fil_system_t*	system		= fil_system;
	fil_node_t*	node;

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
583
#ifdef UNIV_SYNC_DEBUG
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
584
	ut_ad(mutex_own(&(system->mutex)));
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
585
#endif /* UNIV_SYNC_DEBUG */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
586 587 588 589
	node = UT_LIST_GET_LAST(system->LRU);

	if (print_info) {
		fprintf(stderr,
590
"InnoDB: fil_sys open file LRU len %lu\n", (ulong) UT_LIST_GET_LEN(system->LRU));
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
591 592 593 594 595
	}

	while (node != NULL) {
		if (node->modification_counter == node->flush_counter
		    && node->n_pending_flushes == 0) {
596

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
597 598 599 600 601 602
			fil_node_close_file(node, system);
			
			return(TRUE);
		}
		
		if (print_info && node->n_pending_flushes > 0) {
603 604 605
			fputs("InnoDB: cannot close file ", stderr);
			ut_print_filename(stderr, node->name);
			fprintf(stderr, ", because n_pending_flushes %lu\n",
606
				       (ulong) node->n_pending_flushes);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
607 608 609 610
		}

		if (print_info
		    && node->modification_counter != node->flush_counter) {
611 612
			fputs("InnoDB: cannot close file ", stderr);
			ut_print_filename(stderr, node->name);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
613
			fprintf(stderr,
614 615
				", because mod_count %lld != fl_count %lld\n",
				node->modification_counter,
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
				node->flush_counter);
		}

		node = UT_LIST_GET_PREV(LRU, node);
	}

	return(FALSE);
}

/***********************************************************************
Reserves the fil_system mutex and tries to make sure we can open at least one
file while holding it. This should be called before calling
fil_node_prepare_for_io(), because that function may need to open a file. */
static
void
fil_mutex_enter_and_prepare_for_io(
/*===============================*/
	ulint	space_id)	/* in: space id */
{
	fil_system_t*	system		= fil_system;
	fil_space_t*	space;
	ibool		success;
	ibool		print_info	= FALSE;
	ulint		count		= 0;
	ulint		count2		= 0;

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
642
#ifdef UNIV_SYNC_DEBUG
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
643
	ut_ad(!mutex_own(&(system->mutex)));
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
644
#endif /* UNIV_SYNC_DEBUG */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
retry:
	mutex_enter(&(system->mutex));

	if (space_id == 0 || space_id >= SRV_LOG_SPACE_FIRST_ID) {
		/* We keep log files and system tablespace files always open;
		this is important in preventing deadlocks in this module, as
		a page read completion often performs another read from the
		insert buffer. The insert buffer is in tablespace 0, and we
		cannot end up waiting in this function. */

		return;
	}

	if (system->n_open < system->max_n_open) {

		return;
	}

	HASH_SEARCH(hash, system->spaces, space_id, space,
							space->id == space_id);
	if (space != NULL && space->stop_ios) {
		/* We are going to do a rename file and want to stop new i/o's
		for a while */

		if (count2 > 20000) {
670 671
			fputs("InnoDB: Warning: tablespace ", stderr);
			ut_print_filename(stderr, space->name);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
672
			fprintf(stderr,
673 674
				" has i/o ops stopped for a long time %lu\n",
				(ulong) count2);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
		}

		mutex_exit(&(system->mutex));

		os_thread_sleep(20000);

		count2++;

		goto retry;
	}

	/* If the file is already open, no need to do anything; if the space
	does not exist, we handle the situation in the function which called
	this function */

	if (!space || UT_LIST_GET_FIRST(space->chain)->open) {

		return;
	}

	if (count > 1) {
		print_info = TRUE;
	}

	/* Too many files are open, try to close some */
close_more:
	success = fil_try_to_close_file_in_LRU(print_info);

	if (success && system->n_open >= system->max_n_open) {

		goto close_more;
	}

	if (system->n_open < system->max_n_open) {
		/* Ok */

		return;
	}

	if (count >= 2) {
		ut_print_timestamp(stderr);
		fprintf(stderr,
"  InnoDB: Warning: too many (%lu) files stay open while the maximum\n"
"InnoDB: allowed value would be %lu.\n"
"InnoDB: You may need to raise the value of innodb_max_files_open in\n"
720
"InnoDB: my.cnf.\n", (ulong) system->n_open, (ulong) system->max_n_open);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
721 722 723 724 725 726

		return;
	}

	mutex_exit(&(system->mutex));

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
727
#ifndef UNIV_HOTBACKUP
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
728 729 730 731 732
	/* Wake the i/o-handler threads to make sure pending i/o's are
	performed */
	os_aio_simulated_wake_handler_threads();

	os_thread_sleep(20000);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
733
#endif
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
734 735 736 737 738 739 740 741
	/* Flush tablespaces so that we can close modified files in the LRU
	list */

	fil_flush_file_spaces(FIL_TABLESPACE);		

	count++;

	goto retry;
742 743 744
}

/***********************************************************************
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
745
Frees a file node object from a tablespace memory cache. */
746 747 748 749 750
static
void
fil_node_free(
/*==========*/
	fil_node_t*	node,	/* in, own: file node */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
751
	fil_system_t*	system,	/* in: tablespace memory cache */
752 753 754
	fil_space_t*	space)	/* in: space where the file node is chained */
{
	ut_ad(node && system && space);
755
#ifdef UNIV_SYNC_DEBUG
756
	ut_ad(mutex_own(&(system->mutex)));
757
#endif /* UNIV_SYNC_DEBUG */
758
	ut_a(node->magic_n == FIL_NODE_MAGIC_N);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
759
	ut_a(node->n_pending == 0);
760 761

	if (node->open) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
762 763 764 765 766
		/* We fool the assertion in fil_node_close_file() to think
		there are no unflushed modifications in the file */

		node->modification_counter = node->flush_counter;

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
767
		fil_node_close_file(node, system);
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
	}

	space->size -= node->size;
	
	UT_LIST_REMOVE(chain, space->chain, node);

	mem_free(node->name);
	mem_free(node);
}

/********************************************************************
Drops files from the start of a file space, so that its size is cut by
the amount given. */

void
fil_space_truncate_start(
/*=====================*/
	ulint	id,		/* in: space id */
	ulint	trunc_len)	/* in: truncate by this much; it is an error
				if this does not equal to the combined size of
				some initial files in the space */
{
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
790
	fil_system_t*	system		= fil_system;
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
	fil_node_t*	node;
	fil_space_t*	space;

	mutex_enter(&(system->mutex));

	HASH_SEARCH(hash, system->spaces, id, space, space->id == id);

	ut_a(space);
	
	while (trunc_len > 0) {
		node = UT_LIST_GET_FIRST(space->chain);

		ut_a(node->size * UNIV_PAGE_SIZE >= trunc_len);

		trunc_len -= node->size * UNIV_PAGE_SIZE;

		fil_node_free(node, system, space);
	}	
				
	mutex_exit(&(system->mutex));
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
811
}
812

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
813 814 815 816 817 818 819
/***********************************************************************
Creates a space memory object and puts it to the tablespace memory cache. If
there is an error, prints an error message to the .err log. */

ibool
fil_space_create(
/*=============*/
820 821 822 823
				/* out: TRUE if success */
	const char*	name,	/* in: space name */
	ulint		id,	/* in: space id */
	ulint		purpose)/* in: FIL_TABLESPACE, or FIL_LOG if log */
824
{
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
825 826 827 828 829 830 831
	fil_system_t*	system		= fil_system;
	fil_space_t*	space;	
	ulint		namesake_id;
try_again:
	/*printf(
	"InnoDB: Adding tablespace %lu of name %s, purpose %lu\n", id, name,
	  purpose);*/
832

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
833 834
	ut_a(system);
	ut_a(name);
835

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
836
	mutex_enter(&(system->mutex));
837

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
838 839 840 841 842 843
	HASH_SEARCH(name_hash, system->name_hash, ut_fold_string(name), space,
					0 == strcmp(name, space->name));
	if (space != NULL) {
		ut_print_timestamp(stderr);
		fprintf(stderr,
"  InnoDB: Warning: trying to init to the tablespace memory cache\n"
844 845 846 847
"InnoDB: a tablespace %lu of name ", (ulong) id);
		ut_print_filename(stderr, name);
		fprintf(stderr, ",\n"
"InnoDB: but a tablespace %lu of the same name\n"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
848
"InnoDB: already exists in the tablespace memory cache!\n",
849
			(ulong) space->id);
850

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
851
		if (id == 0 || purpose != FIL_TABLESPACE) {
852

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
853
			mutex_exit(&(system->mutex));
854

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
855 856
			return(FALSE);
		}
857

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
858 859 860 861 862 863 864 865
		fprintf(stderr,
"InnoDB: We assume that InnoDB did a crash recovery, and you had\n"
"InnoDB: an .ibd file for which the table did not exist in the\n"
"InnoDB: InnoDB internal data dictionary in the ibdata files.\n"
"InnoDB: We assume that you later removed the .ibd and .frm files,\n"
"InnoDB: and are now trying to recreate the table. We now remove the\n"
"InnoDB: conflicting tablespace object from the memory cache and try\n"
"InnoDB: the init again.\n");
866

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
867
		namesake_id = space->id;
868

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
869
		mutex_exit(&(system->mutex));
870

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
871
		fil_space_free(namesake_id);
872

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
873 874
		goto try_again;
	}
875

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
876
	HASH_SEARCH(hash, system->spaces, id, space, space->id == id);
877

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
878 879
	if (space != NULL) {
		fprintf(stderr,
880 881 882
"InnoDB: Error: trying to add tablespace %lu of name ", (ulong) id);
		ut_print_filename(stderr, name);
		fprintf(stderr, "\n"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
883
"InnoDB: to the tablespace memory cache, but tablespace\n"
884 885 886 887
"InnoDB: %lu of name ", (ulong) space->id);
		ut_print_filename(stderr, space->name);
		fputs(" already exists in the tablespace\n"
"InnoDB: memory cache!\n", stderr);
888

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
889
		mutex_exit(&(system->mutex));
890

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
891
		return(FALSE);
892 893
	}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
894
	space = mem_alloc(sizeof(fil_space_t));
895

monty@mishka.local's avatar
monty@mishka.local committed
896
	space->name = mem_strdup(name);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
897
	space->id = id;
898

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
899
	system->tablespace_version++;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
900
	space->tablespace_version = system->tablespace_version;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
901
	space->mark = FALSE;
902

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
903 904
	if (purpose == FIL_TABLESPACE && id > system->max_assigned_id) {
		system->max_assigned_id = id;
905 906
	}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
907 908 909 910 911
	space->stop_ios = FALSE;
	space->stop_ibuf_merges = FALSE;
	space->is_being_deleted = FALSE;
	space->purpose = purpose;
	space->size = 0;
912

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
913
	space->n_reserved_extents = 0;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
914
	
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
915 916
	space->n_pending_flushes = 0;
	space->n_pending_ibuf_merges = 0;
917

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
918 919
	UT_LIST_INIT(space->chain);
	space->magic_n = FIL_SPACE_MAGIC_N;
920

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
921 922 923 924 925 926
	space->ibuf_data = NULL;
	
	rw_lock_create(&(space->latch));
	rw_lock_set_level(&(space->latch), SYNC_FSP);
	
	HASH_INSERT(fil_space_t, hash, system->spaces, id, space);
927

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
928 929 930 931 932
	HASH_INSERT(fil_space_t, name_hash, system->name_hash,
						ut_fold_string(name), space);
	UT_LIST_ADD_LAST(space_list, system->space_list, space);
				
	mutex_exit(&(system->mutex));
933

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
934
	return(TRUE);
935 936 937
}

/***********************************************************************
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
938 939 940 941 942 943 944 945 946
Assigns a new space id for a new single-table tablespace. This works simply by
incrementing the global counter. If 4 billion id's is not enough, we may need
to recycle id's. */
static
ulint
fil_assign_new_space_id(void)
/*=========================*/
			/* out: new tablespace id; ULINT_UNDEFINED if could
			not assign an id */
947 948
{
	fil_system_t*	system = fil_system;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
949
	ulint		id;
950 951 952

	mutex_enter(&(system->mutex));

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
953
	system->max_assigned_id++;
954

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
955
	id = system->max_assigned_id;
956

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
957 958 959 960 961 962
	if (id > (SRV_LOG_SPACE_FIRST_ID / 2) && (id % 1000000UL == 0)) {
	        ut_print_timestamp(stderr);
	        fprintf(stderr,
"InnoDB: Warning: you are running out of new single-table tablespace id's.\n"
"InnoDB: Current counter is %lu and it must not exceed %lu!\n"
"InnoDB: To reset the counter to zero you have to dump all your tables and\n"
963
"InnoDB: recreate the whole InnoDB installation.\n", (ulong) id,
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
964
					(ulong) SRV_LOG_SPACE_FIRST_ID);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
965
	}
966

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
967 968 969 970 971 972
	if (id >= SRV_LOG_SPACE_FIRST_ID) {
	        ut_print_timestamp(stderr);
	        fprintf(stderr,
"InnoDB: You have run out of single-table tablespace id's!\n"
"InnoDB: Current counter is %lu.\n"
"InnoDB: To reset the counter to zero you have to dump all your tables and\n"
973
"InnoDB: recreate the whole InnoDB installation.\n", (ulong) id);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
974
		system->max_assigned_id--;
975

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
976 977
		id = ULINT_UNDEFINED;
	}
978 979

	mutex_exit(&(system->mutex));
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
980 981

	return(id);
982 983 984
}

/***********************************************************************
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
985 986 987
Frees a space object from the tablespace memory cache. Closes the files in
the chain but does not delete them. There must not be any pending i/o's or
flushes on the files. */
988

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
989
ibool
990 991
fil_space_free(
/*===========*/
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
992
			/* out: TRUE if success */
993 994
	ulint	id)	/* in: space id */
{
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
995
	fil_system_t*	system = fil_system;
996
	fil_space_t*	space;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
997
	fil_space_t*	namespace;
998
	fil_node_t*	fil_node;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
999

1000 1001 1002 1003
	mutex_enter(&(system->mutex));

	HASH_SEARCH(hash, system->spaces, id, space, space->id == id);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1004 1005 1006 1007
	if (!space) {
		ut_print_timestamp(stderr);
		fprintf(stderr,
"  InnoDB: Error: trying to remove tablespace %lu from the cache but\n"
1008
"InnoDB: it is not there.\n", (ulong) id);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1009 1010 1011 1012 1013 1014

		mutex_exit(&(system->mutex));
		
		return(FALSE);
	}

1015 1016
	HASH_DELETE(fil_space_t, hash, system->spaces, id, space);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1017 1018 1019 1020 1021 1022 1023 1024
	HASH_SEARCH(name_hash, system->name_hash, ut_fold_string(space->name),
		    namespace, 0 == strcmp(space->name, namespace->name));
	ut_a(namespace);
	ut_a(space == namespace);

	HASH_DELETE(fil_space_t, name_hash, system->name_hash,
					   ut_fold_string(space->name), space);

1025 1026 1027
	UT_LIST_REMOVE(space_list, system->space_list, space);

	ut_a(space->magic_n == FIL_SPACE_MAGIC_N);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1028
	ut_a(0 == space->n_pending_flushes);
1029 1030 1031 1032 1033 1034 1035 1036 1037

	fil_node = UT_LIST_GET_FIRST(space->chain);

	while (fil_node != NULL) {
		fil_node_free(fil_node, system, space);

		fil_node = UT_LIST_GET_FIRST(space->chain);
	}	
	
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1038
	ut_a(0 == UT_LIST_GET_LEN(space->chain));
1039 1040 1041

	mutex_exit(&(system->mutex));

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1042 1043
	rw_lock_free(&(space->latch));

1044 1045
	mem_free(space->name);
	mem_free(space);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1046 1047

	return(TRUE);
1048 1049
}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
#ifdef UNIV_HOTBACKUP
/***********************************************************************
Returns the tablespace object for a given id, or NULL if not found from the
tablespace memory cache. */
static
fil_space_t*
fil_get_space_for_id_low(
/*=====================*/
			/* out: tablespace object or NULL; NOTE that you must
			own &(fil_system->mutex) to call this function! */
	ulint	id)	/* in: space id */
{
	fil_system_t*	system		= fil_system;
	fil_space_t*	space;

	ut_ad(system);

	HASH_SEARCH(hash, system->spaces, id, space, space->id == id);

	return(space);
}
#endif

1073
/***********************************************************************
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1074 1075
Returns the size of the space in pages. The tablespace must be cached in the
memory cache. */
1076 1077 1078 1079

ulint
fil_space_get_size(
/*===============*/
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1080
			/* out: space size, 0 if space not found */
1081 1082
	ulint	id)	/* in: space id */
{
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1083 1084
	fil_system_t*	system 		= fil_system;
	fil_node_t*	node;
1085 1086 1087 1088 1089
	fil_space_t*	space;
	ulint		size;

	ut_ad(system);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1090
	fil_mutex_enter_and_prepare_for_io(id);
1091 1092 1093

	HASH_SEARCH(hash, system->spaces, id, space, space->id == id);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
	if (space == NULL) {
		mutex_exit(&(system->mutex));

		return(0);
	}

	if (space->size == 0 && space->purpose == FIL_TABLESPACE) {
		ut_a(id != 0);

		ut_a(1 == UT_LIST_GET_LEN(space->chain));

		node = UT_LIST_GET_FIRST(space->chain);

		/* It must be a single-table tablespace and we have not opened
		the file yet; the following calls will open it and update the
		size fields */

		fil_node_prepare_for_io(node, system, space);
		fil_node_complete_io(node, system, OS_FILE_READ);
	}

1115 1116 1117 1118
	size = space->size;
	
	mutex_exit(&(system->mutex));

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
	return(size);
}

/***********************************************************************
Checks if the pair space, page_no refers to an existing page in a tablespace
file space. The tablespace must be cached in the memory cache. */

ibool
fil_check_adress_in_tablespace(
/*===========================*/
			/* out: TRUE if the address is meaningful */
	ulint	id,	/* in: space id */
	ulint	page_no)/* in: page number */
{
	if (fil_space_get_size(id) > page_no) {

		return(TRUE);
	}

	return(FALSE);
}		

/********************************************************************
Creates a the tablespace memory cache. */
static
fil_system_t*
fil_system_create(
/*==============*/
				/* out, own: tablespace memory cache */
	ulint	hash_size,	/* in: hash table size */
	ulint	max_n_open)	/* in: maximum number of open files; must be
				> 10 */
{
	fil_system_t*	system;

	ut_a(hash_size > 0);
	ut_a(max_n_open > 0);

	system = mem_alloc(sizeof(fil_system_t));

	mutex_create(&(system->mutex));

	mutex_set_level(&(system->mutex), SYNC_ANY_LATCH);

	system->spaces = hash_create(hash_size);
	system->name_hash = hash_create(hash_size);

	UT_LIST_INIT(system->LRU);

	system->n_open = 0;
	system->max_n_open = max_n_open;

	system->modification_counter = 0;
	system->max_assigned_id = 0;

	system->tablespace_version = 0;

	UT_LIST_INIT(system->space_list);

	return(system);
}

/********************************************************************
Initializes the tablespace memory cache. */

void
fil_init(
/*=====*/
	ulint	max_n_open)	/* in: max number of open files */
{
	ut_a(fil_system == NULL);

	/*printf("Initializing the tablespace cache with max %lu open files\n",
							       max_n_open); */
	fil_system = fil_system_create(FIL_SYSTEM_HASH_SIZE, max_n_open);
}

/***********************************************************************
Opens all log files and system tablespace data files. They stay open until the
database server shutdown. This should be called at a server startup after the
space objects for the log and the system tablespace have been created. The
purpose of this operation is to make sure we never run out of file descriptors
if we need to read from the insert buffer or to write to the log. */

void
fil_open_log_and_system_tablespace_files(void)
/*==========================================*/
{
	fil_system_t*	system = fil_system;
	fil_space_t*	space;
	fil_node_t*	node;

	mutex_enter(&(system->mutex));

	space = UT_LIST_GET_FIRST(system->space_list);

	while (space != NULL) {
		if (space->purpose != FIL_TABLESPACE || space->id == 0) {
			node = UT_LIST_GET_FIRST(space->chain);

			while (node != NULL) {
				if (!node->open) {
					fil_node_open_file(node, system,
									space);
				}
				if (system->max_n_open < 10 + system->n_open) {
					fprintf(stderr,
"InnoDB: Warning: you must raise the value of innodb_max_open_files in\n"
"InnoDB: my.cnf! Remember that InnoDB keeps all log files and all system\n"
"InnoDB: tablespace files open for the whole time mysqld is running, and\n"
"InnoDB: needs to open also some .ibd files if the file-per-table storage\n"
"InnoDB: model is used. Current open files %lu, max allowed open files %lu.\n",
1231 1232
				     (ulong) system->n_open,
				     (ulong) system->max_n_open);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
				}
				node = UT_LIST_GET_NEXT(chain, node);
			}
		}
		space = UT_LIST_GET_NEXT(space_list, space);
	}

	mutex_exit(&(system->mutex));
}

/***********************************************************************
Closes all open files. There must not be any pending i/o's or not flushed
modifications in the files. */

void
fil_close_all_files(void)
/*=====================*/
{
	fil_system_t*	system = fil_system;
	fil_space_t*	space;
	fil_node_t*	node;

	mutex_enter(&(system->mutex));

	space = UT_LIST_GET_FIRST(system->space_list);

	while (space != NULL) {
		node = UT_LIST_GET_FIRST(space->chain);

		while (node != NULL) {
			if (node->open) {
				fil_node_close_file(node, system);
			}
			node = UT_LIST_GET_NEXT(chain, node);
		}
		space = UT_LIST_GET_NEXT(space_list, space);
	}

	mutex_exit(&(system->mutex));
}

/***********************************************************************
Sets the max tablespace id counter if the given number is bigger than the
previous value. */

void
fil_set_max_space_id_if_bigger(
/*===========================*/
	ulint	max_id)	/* in: maximum known id */
{
	fil_system_t*	system = fil_system;

	if (max_id >= SRV_LOG_SPACE_FIRST_ID) {
		fprintf(stderr,
1287
"InnoDB: Fatal error: max tablespace id is too high, %lu\n", (ulong) max_id);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
		ut_a(0);
	}

	mutex_enter(&(system->mutex));

	if (system->max_assigned_id < max_id) {

		system->max_assigned_id = max_id;
	}

	mutex_exit(&(system->mutex));
}

/********************************************************************
Initializes the ibuf data structure for space 0 == the system tablespace.
This can be called after the file space headers have been created and the
dictionary system has been initialized. */

void
fil_ibuf_init_at_db_start(void)
/*===========================*/
{
	fil_space_t*	space;

	space = UT_LIST_GET_FIRST(fil_system->space_list);

	ut_a(space);
        ut_a(space->purpose == FIL_TABLESPACE);	

	space->ibuf_data = ibuf_data_init_for_space(space->id);
}

/********************************************************************
Writes the flushed lsn and the latest archived log number to the page header
of the first page of a data file. */
static
ulint
fil_write_lsn_and_arch_no_to_file(
/*==============================*/
	ulint	space_id,	/* in: space number */
	ulint	sum_of_sizes,	/* in: combined size of previous files in
				space, in database pages */
	dulint	lsn,		/* in: lsn to write */
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1331 1332
	ulint	arch_log_no	/* in: archived log number to write */
	__attribute__((unused)))
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
{
	byte*	buf1;
	byte*	buf;

	buf1 = mem_alloc(2 * UNIV_PAGE_SIZE);
	buf = ut_align(buf1, UNIV_PAGE_SIZE);

	fil_read(TRUE, space_id, sum_of_sizes, 0, UNIV_PAGE_SIZE, buf, NULL);

	mach_write_to_8(buf + FIL_PAGE_FILE_FLUSH_LSN, lsn);

	fil_write(TRUE, space_id, sum_of_sizes, 0, UNIV_PAGE_SIZE, buf, NULL);

	return(DB_SUCCESS);	
}

/********************************************************************
Writes the flushed lsn and the latest archived log number to the page
header of the first page of each data file in the system tablespace. */

ulint
fil_write_flushed_lsn_to_data_files(
/*================================*/
				/* out: DB_SUCCESS or error number */
	dulint	lsn,		/* in: lsn to write */
	ulint	arch_log_no)	/* in: latest archived log file number */
{
	fil_space_t*	space;
	fil_node_t*	node;
	ulint		sum_of_sizes;
	ulint		err;

	mutex_enter(&(fil_system->mutex));
	
	space = UT_LIST_GET_FIRST(fil_system->space_list);
	
	while (space) {
1370 1371 1372 1373 1374
		/* We only write the lsn to all existing data files which have
		been open during the lifetime of the mysqld process; they are
		represented by the space objects in the tablespace memory
		cache. Note that all data files in the system tablespace 0 are
		always open. */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1375

1376
		if (space->purpose == FIL_TABLESPACE) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
			sum_of_sizes = 0;

			node = UT_LIST_GET_FIRST(space->chain);
			while (node) {
				mutex_exit(&(fil_system->mutex));

				err = fil_write_lsn_and_arch_no_to_file(
						space->id, sum_of_sizes,
						lsn, arch_log_no);
				if (err != DB_SUCCESS) {

					return(err);
				}

				mutex_enter(&(fil_system->mutex));

				sum_of_sizes += node->size;
				node = UT_LIST_GET_NEXT(chain, node);
			}
		}
		space = UT_LIST_GET_NEXT(space_list, space);
	}

	mutex_exit(&(fil_system->mutex));

	return(DB_SUCCESS);
}

/***********************************************************************
Reads the flushed lsn and arch no fields from a data file at database
startup. */

void
fil_read_flushed_lsn_and_arch_log_no(
/*=================================*/
	os_file_t data_file,		/* in: open data file */
	ibool	one_read_already,	/* in: TRUE if min and max parameters
					below already contain sensible data */
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1415
#ifdef UNIV_LOG_ARCHIVE
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1416
	ulint*	min_arch_log_no,	/* in/out: */
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1417 1418 1419 1420
	ulint*	max_arch_log_no,	/* in/out: */
#endif /* UNIV_LOG_ARCHIVE */
	dulint*	min_flushed_lsn,	/* in/out: */
	dulint*	max_flushed_lsn)	/* in/out: */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1421 1422 1423 1424
{
	byte*	buf;
	byte*	buf2;
	dulint	flushed_lsn;
1425

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438
	buf2 = ut_malloc(2 * UNIV_PAGE_SIZE);
	/* Align the memory for a possible read from a raw device */
	buf = ut_align(buf2, UNIV_PAGE_SIZE);
	
	os_file_read(data_file, buf, 0, 0, UNIV_PAGE_SIZE);

	flushed_lsn = mach_read_from_8(buf + FIL_PAGE_FILE_FLUSH_LSN);

	ut_free(buf2);

	if (!one_read_already) {
		*min_flushed_lsn = flushed_lsn;
		*max_flushed_lsn = flushed_lsn;
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1439
#ifdef UNIV_LOG_ARCHIVE
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1440 1441
		*min_arch_log_no = arch_log_no;
		*max_arch_log_no = arch_log_no;
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1442
#endif /* UNIV_LOG_ARCHIVE */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1443 1444 1445 1446 1447 1448 1449 1450 1451
		return;
	}

	if (ut_dulint_cmp(*min_flushed_lsn, flushed_lsn) > 0) {
		*min_flushed_lsn = flushed_lsn;
	}
	if (ut_dulint_cmp(*max_flushed_lsn, flushed_lsn) < 0) {
		*max_flushed_lsn = flushed_lsn;
	}
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1452
#ifdef UNIV_LOG_ARCHIVE
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1453 1454 1455 1456 1457 1458
	if (*min_arch_log_no > arch_log_no) {
		*min_arch_log_no = arch_log_no;
	}
	if (*max_arch_log_no < arch_log_no) {
		*max_arch_log_no = arch_log_no;
	}
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1459
#endif /* UNIV_LOG_ARCHIVE */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483
}

/*================ SINGLE-TABLE TABLESPACES ==========================*/

/***********************************************************************
Increments the count of pending insert buffer page merges, if space is not
being deleted. */

ibool
fil_inc_pending_ibuf_merges(
/*========================*/
			/* out: TRUE if being deleted, and ibuf merges should
			be skipped */
	ulint	id)	/* in: space id */
{
	fil_system_t*	system		= fil_system;
	fil_space_t*	space;
	
	mutex_enter(&(system->mutex));

	HASH_SEARCH(hash, system->spaces, id, space, space->id == id);

	if (space == NULL) {
		fprintf(stderr,
1484 1485
"InnoDB: Error: trying to do ibuf merge to a dropped tablespace %lu\n",
			(ulong) id);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517
	}

	if (space == NULL || space->stop_ibuf_merges) {
		mutex_exit(&(system->mutex));

		return(TRUE);
	}

	space->n_pending_ibuf_merges++;

	mutex_exit(&(system->mutex));

	return(FALSE);
}

/***********************************************************************
Decrements the count of pending insert buffer page merges. */

void
fil_decr_pending_ibuf_merges(
/*========================*/
	ulint	id)	/* in: space id */
{
	fil_system_t*	system		= fil_system;
	fil_space_t*	space;
	
	mutex_enter(&(system->mutex));

	HASH_SEARCH(hash, system->spaces, id, space, space->id == id);

	if (space == NULL) {
		fprintf(stderr,
1518 1519
"InnoDB: Error: decrementing ibuf merge of a dropped tablespace %lu\n",
			(ulong) id);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1520 1521 1522 1523 1524 1525 1526 1527 1528
	}

	if (space != NULL) {
		space->n_pending_ibuf_merges--;
	}

	mutex_exit(&(system->mutex));
}

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1529 1530
/************************************************************
Creates the database directory for a table if it does not exist yet. */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1531 1532 1533 1534
static
void
fil_create_directory_for_tablename(
/*===============================*/
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1535 1536
	const char*	name)	/* in: name in the standard
				'databasename/tablename' format */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1537
{
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1538 1539 1540
	const char*	namend;
	char*		path;
	ulint		len;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1541

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1542 1543 1544 1545
	len = strlen(fil_path_to_mysql_datadir);
	namend = strchr(name, '/');
	ut_a(namend);
	path = mem_alloc(len + (namend - name) + 2);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1546

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1547 1548 1549 1550
	memcpy(path, fil_path_to_mysql_datadir, len);
	path[len] = '/';
	memcpy(path + len + 1, name, namend - name);
	path[len + (namend - name) + 1] = 0;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1551 1552 1553 1554

	srv_normalize_path_for_win(path);

	ut_a(os_file_create_directory(path, FALSE));
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1555
	mem_free(path);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1556 1557 1558 1559 1560 1561 1562 1563 1564
}

#ifndef UNIV_HOTBACKUP
/************************************************************
Writes a log record about an .ibd file create/rename/delete. */
static
void
fil_op_write_log(
/*=============*/
1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576
	ulint		type,		/* in: MLOG_FILE_CREATE,
					MLOG_FILE_DELETE, or
					MLOG_FILE_RENAME */
	ulint		space_id,	/* in: space id */
	const char*	name,		/* in: table name in the familiar
					'databasename/tablename' format, or
					the file path in the case of
					MLOG_FILE_DELETE */ 
	const char*	new_name,	/* in: if type is MLOG_FILE_RENAME,
					the new table name in the
					'databasename/tablename' format */
	mtr_t*		mtr)		/* in: mini-transaction handle */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591
{
	byte*	log_ptr;

	log_ptr = mlog_open(mtr, 30);
	
	log_ptr = mlog_write_initial_log_record_for_file_op(type, space_id, 0,
								log_ptr, mtr);
	/* Let us store the strings as null-terminated for easier readability
	and handling */

	mach_write_to_2(log_ptr, ut_strlen(name) + 1);
	log_ptr += 2;
	
	mlog_close(mtr, log_ptr);

1592
	mlog_catenate_string(mtr, (byte*) name, ut_strlen(name) + 1);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1593 1594 1595 1596 1597 1598 1599 1600

	if (type == MLOG_FILE_RENAME) {
		log_ptr = mlog_open(mtr, 30);
		mach_write_to_2(log_ptr, ut_strlen(new_name) + 1);
		log_ptr += 2;
	
		mlog_close(mtr, log_ptr);

1601 1602
		mlog_catenate_string(mtr, (byte*) new_name,
						ut_strlen(new_name) + 1);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636
	}
}
#endif

/***********************************************************************
Parses the body of a log record written about an .ibd file operation. That is,
the log record part after the standard (type, space id, page no) header of the
log record.

If desired, also replays the delete or rename operation if the .ibd file
exists and the space id in it matches. Replays the create operation if a file
at that path does not exist yet. If the database directory for the file to be
created does not exist, then we create the directory, too.

Note that ibbackup --apply-log sets fil_path_to_mysql_datadir to point to the
datadir that we should use in replaying the file operations. */

byte*
fil_op_log_parse_or_replay(
/*=======================*/
                        	/* out: end of log record, or NULL if the
				record was not completely contained between
				ptr and end_ptr */
        byte*   ptr,    	/* in: buffer containing the log record body,
				or an initial segment of it, if the record does
				not fir completely between ptr and end_ptr */
        byte*   end_ptr,	/* in: buffer end */
	ulint	type,		/* in: the type of this log record */
	ibool	do_replay,	/* in: TRUE if we want to replay the
				operation, and not just parse the log record */
	ulint	space_id)	/* in: if do_replay is TRUE, the space id of
				the tablespace in question; otherwise
				ignored */
{
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1637 1638 1639 1640
	ulint		name_len;
	ulint		new_name_len;
	const char*	name;
	const char*	new_name	= NULL;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655

	if (end_ptr < ptr + 2) {

		return(NULL);
	}

	name_len = mach_read_from_2(ptr);

	ptr += 2;

	if (end_ptr < ptr + name_len) {
		
		return(NULL);
	}

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1656
	name = (const char*) ptr;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674

	ptr += name_len;

	if (type == MLOG_FILE_RENAME) {
		if (end_ptr < ptr + 2) {

			return(NULL);
		}

		new_name_len = mach_read_from_2(ptr);
		
		ptr += 2;

		if (end_ptr < ptr + new_name_len) {
		
			return(NULL);
		}

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1675
		new_name = (const char*) ptr;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706

		ptr += new_name_len;
	}

	/* We managed to parse a full log record body */
/*
	printf("Parsed log rec of type %lu space %lu\n"
		"name %s\n", type, space_id, name);

	if (type == MLOG_FILE_RENAME) {
		printf("new name %s\n", new_name);
	}
*/
	if (do_replay == FALSE) {

		return(ptr);
	}

	/* Let us try to perform the file operation, if sensible. Note that
	ibbackup has at this stage already read in all space id info to the
	fil0fil.c data structures.
	
	NOTE that our algorithm is not guaranteed to work correctly if there
	were renames of tables during the backup. See ibbackup code for more
	on the problem. */

	if (type == MLOG_FILE_DELETE) {
		if (fil_tablespace_exists_in_mem(space_id)) {
			ut_a(fil_delete_tablespace(space_id));
		}
	} else if (type == MLOG_FILE_RENAME) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1707 1708 1709 1710 1711 1712
		/* We do the rename based on space id, not old file name;
		this should guarantee that after the log replay each .ibd file
		has the correct name for the latest log sequence number; the
		proof is left as an exercise :) */

		if (fil_tablespace_exists_in_mem(space_id)) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1713 1714 1715 1716 1717 1718 1719 1720 1721
			/* Create the database directory for the new name, if
			it does not exist yet */
			fil_create_directory_for_tablename(new_name);
	
			/* Rename the table if there is not yet a tablespace
			with the same name */

			if (fil_get_space_id_for_table(new_name)
			    == ULINT_UNDEFINED) {
1722 1723 1724
				/* We do not care of the old name, that is
				why we pass NULL as the first argument */
				ut_a(fil_rename_tablespace(NULL, space_id,
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744
								new_name));
			}
		}
	} else {
		ut_a(type == MLOG_FILE_CREATE);

		if (fil_tablespace_exists_in_mem(space_id)) {
			/* Do nothing */
		} else if (fil_get_space_id_for_table(name) !=
							ULINT_UNDEFINED) {
			/* Do nothing */
		} else {
			/* Create the database directory for name, if it does
			not exist yet */
			fil_create_directory_for_tablename(name);

			ut_a(space_id != 0);

			ut_a(DB_SUCCESS == 
				fil_create_new_single_table_tablespace(
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1745
						&space_id, name, FALSE,
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1746 1747 1748 1749 1750 1751 1752
						FIL_IBD_FILE_INITIAL_SIZE));
		}
	}

	return(ptr);
}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767
/***********************************************************************
Deletes a single-table tablespace. The tablespace must be cached in the
memory cache. */

ibool
fil_delete_tablespace(
/*==================*/
			/* out: TRUE if success */
	ulint	id)	/* in: space id */
{
	fil_system_t*	system		= fil_system;
	ibool		success;
	fil_space_t*	space;
	fil_node_t*	node;
	ulint		count		= 0;
1768
	char*		path;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787

	ut_a(id != 0);
stop_ibuf_merges:
	mutex_enter(&(system->mutex));

	HASH_SEARCH(hash, system->spaces, id, space, space->id == id);

	if (space != NULL) {
		space->stop_ibuf_merges = TRUE;

		if (space->n_pending_ibuf_merges == 0) {
			mutex_exit(&(system->mutex));

			count = 0;

			goto try_again;
		} else {
			if (count > 5000) {
			   ut_print_timestamp(stderr);
1788 1789 1790 1791
			   fputs(
"  InnoDB: Warning: trying to delete tablespace ", stderr);
			   ut_print_filename(stderr, space->name);
			   fprintf(stderr, ",\n"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1792
"InnoDB: but there are %lu pending ibuf merges on it.\n"
1793
"InnoDB: Loop %lu.\n", (ulong) space->n_pending_ibuf_merges,
1794
				   (ulong) count);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816
			}

			mutex_exit(&(system->mutex));

			os_thread_sleep(20000);
			count++;

			goto stop_ibuf_merges;
		}
	}

	mutex_exit(&(system->mutex));
	count = 0;

try_again:
	mutex_enter(&(system->mutex));

	HASH_SEARCH(hash, system->spaces, id, space, space->id == id);

	if (space == NULL) {
		ut_print_timestamp(stderr);
		fprintf(stderr,
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1817
"  InnoDB: Error: cannot delete tablespace %lu\n"
1818 1819
"InnoDB: because it is not found in the tablespace memory cache.\n",
			(ulong) id);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836

		mutex_exit(&(system->mutex));
	
		return(FALSE);
	}	

	ut_a(space);
	ut_a(space->n_pending_ibuf_merges == 0);

	space->is_being_deleted = TRUE;

	ut_a(UT_LIST_GET_LEN(space->chain) == 1);
	node = UT_LIST_GET_FIRST(space->chain);

	if (space->n_pending_flushes > 0 || node->n_pending > 0) {
		if (count > 1000) {
			ut_print_timestamp(stderr);
1837 1838 1839 1840
			fputs(
"  InnoDB: Warning: trying to delete tablespace ", stderr);
			ut_print_filename(stderr, space->name);
			fprintf(stderr, ",\n"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1841
"InnoDB: but there are %lu flushes and %lu pending i/o's on it\n"
1842
"InnoDB: Loop %lu.\n", (ulong) space->n_pending_flushes,
1843 1844
				(ulong) node->n_pending,
				(ulong) count);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1845 1846 1847 1848 1849 1850 1851 1852 1853
		}
		mutex_exit(&(system->mutex));
		os_thread_sleep(20000);

		count++;

		goto try_again;
	}

1854 1855
	path = mem_strdup(space->name);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1856
	mutex_exit(&(system->mutex));
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1857
#ifndef UNIV_HOTBACKUP
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1858 1859 1860 1861 1862 1863 1864 1865
	/* Invalidate in the buffer pool all pages belonging to the
	tablespace. Since we have set space->is_being_deleted = TRUE, readahead
	or ibuf merge can no longer read more pages of this tablespace to the
	buffer pool. Thus we can clean the tablespace out of the buffer pool
	completely and permanently. The flag is_being_deleted also prevents
	fil_flush() from being applied to this tablespace. */

	buf_LRU_invalidate_tablespace(id);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1866 1867
#endif
	/* printf("Deleting tablespace %s id %lu\n", space->name, id); */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1868 1869 1870 1871 1872

	success = fil_space_free(id);

	if (success) {
		success = os_file_delete(path);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1873 1874 1875 1876

		if (!success) {
			success = os_file_delete_if_exists(path);
		}
1877 1878 1879
	}

	if (success) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1880
#ifndef UNIV_HOTBACKUP
1881 1882 1883 1884 1885
		/* Write a log record about the deletion of the .ibd
		file, so that ibbackup can replay it in the
		--apply-log phase. We use a dummy mtr and the familiar
		log write mechanism. */
		mtr_t		mtr;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1886

1887 1888 1889
		/* When replaying the operation in ibbackup, do not try
		to write any log record */
		mtr_start(&mtr);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1890

1891 1892
		fil_op_write_log(MLOG_FILE_DELETE, id, path, NULL, &mtr);
		mtr_commit(&mtr);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1893
#endif
1894 1895
		mem_free(path);

1896
		return(TRUE);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1897 1898
	}

1899 1900
	mem_free(path);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926
	return(FALSE);
}

/***********************************************************************
Discards a single-table tablespace. The tablespace must be cached in the
memory cache. Discarding is like deleting a tablespace, but
1) we do not drop the table from the data dictionary;
2) we remove all insert buffer entries for the tablespace immediately; in DROP
TABLE they are only removed gradually in the background;
3) when the user does IMPORT TABLESPACE, the tablespace will have the same id
as it originally had. */

ibool
fil_discard_tablespace(
/*===================*/
			/* out: TRUE if success */
	ulint	id)	/* in: space id */
{
	ibool	success;

	success = fil_delete_tablespace(id);

	if (!success) {
		fprintf(stderr,
"InnoDB: Warning: cannot delete tablespace %lu in DISCARD TABLESPACE.\n"
"InnoDB: But let us remove the insert buffer entries for this tablespace.\n",
1927
			(ulong) id); 
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945
	}

	/* Remove all insert buffer entries for the tablespace */

	ibuf_delete_for_discarded_space(id);

	return(TRUE);
}

/***********************************************************************
Renames the memory cache structures of a single-table tablespace. */
static
ibool
fil_rename_tablespace_in_mem(
/*=========================*/
				/* out: TRUE if success */
	fil_space_t*	space,	/* in: tablespace memory object */
	fil_node_t*	node,	/* in: file node of that tablespace */
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1946
	const char*	path)	/* in: new name */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1947 1948 1949
{
	fil_system_t*	system		= fil_system;
	fil_space_t*	space2;
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1950
	const char*	old_name	= space->name;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1951 1952 1953 1954
	
	HASH_SEARCH(name_hash, system->name_hash, ut_fold_string(old_name),
			       space2, 0 == strcmp(old_name, space2->name));
	if (space != space2) {
1955 1956 1957
		fputs("InnoDB: Error: cannot find ", stderr);
		ut_print_filename(stderr, old_name);
		fputs(" in tablespace memory cache\n", stderr);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1958 1959 1960 1961 1962 1963 1964

		return(FALSE);
	}

	HASH_SEARCH(name_hash, system->name_hash, ut_fold_string(path),
			       space2, 0 == strcmp(path, space2->name));
	if (space2 != NULL) {
1965 1966 1967
		fputs("InnoDB: Error: ", stderr);
		ut_print_filename(stderr, path);
		fputs(" is already in tablespace memory cache\n", stderr);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1968 1969 1970 1971 1972 1973 1974 1975 1976
		
		return(FALSE);
	}

	HASH_DELETE(fil_space_t, name_hash, system->name_hash,
					   ut_fold_string(space->name), space);
	mem_free(space->name);
	mem_free(node->name);

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1977 1978
	space->name = mem_strdup(path);
	node->name = mem_strdup(path);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1979 1980 1981 1982 1983 1984

	HASH_INSERT(fil_space_t, name_hash, system->name_hash,
						ut_fold_string(path), space);
	return(TRUE);
}

1985
/***********************************************************************
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1986 1987
Allocates a file name for a single-table tablespace. The string must be freed
by caller with mem_free(). */
1988 1989 1990 1991 1992
static
char*
fil_make_ibd_name(
/*==============*/
					/* out, own: file name */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1993 1994 1995
	const char*	name,		/* in: table name or a dir path of a
					TEMPORARY table */
	ibool		is_temp)	/* in: TRUE if it is a dir path */
1996 1997 1998 1999 2000
{
	ulint	namelen		= strlen(name);
	ulint	dirlen		= strlen(fil_path_to_mysql_datadir);
	char*	filename	= mem_alloc(namelen + dirlen + sizeof "/.ibd");

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2001 2002 2003 2004 2005 2006 2007 2008 2009 2010
	if (is_temp) {
		memcpy(filename, name, namelen);
		memcpy(filename + namelen, ".ibd", sizeof ".ibd");
	} else {
		memcpy(filename, fil_path_to_mysql_datadir, dirlen);
		filename[dirlen] = '/';

		memcpy(filename + dirlen + 1, name, namelen);
		memcpy(filename + dirlen + namelen + 1, ".ibd", sizeof ".ibd");
	}
2011 2012

	srv_normalize_path_for_win(filename);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2013

2014 2015 2016
	return(filename);
}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2017 2018 2019 2020 2021 2022 2023
/***********************************************************************
Renames a single-table tablespace. The tablespace must be cached in the
tablespace memory cache. */

ibool
fil_rename_tablespace(
/*==================*/
2024 2025 2026 2027 2028 2029 2030 2031 2032
					/* out: TRUE if success */
	const char*	old_name,	/* in: old table name in the standard
					databasename/tablename format of
					InnoDB, or NULL if we do the rename
					based on the space id only */
	ulint		id,		/* in: space id */
	const char*	new_name)	/* in: new table name in the standard
					databasename/tablename format
					of InnoDB */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2033 2034 2035 2036 2037 2038
{
	fil_system_t*	system		= fil_system;
	ibool		success;
	fil_space_t*	space;
	fil_node_t*	node;
	ulint		count		= 0;
2039
	char*		path;
2040
	ibool		old_name_was_specified 		= TRUE;
2041
	char*		old_path;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2042 2043

	ut_a(id != 0);
2044 2045
	
	if (old_name == NULL) {
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
2046
		old_name = "(name not specified)";
2047 2048
		old_name_was_specified = FALSE;
	}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2049 2050 2051 2052 2053
retry:
	count++;

	if (count > 1000) {
		ut_print_timestamp(stderr);
2054 2055 2056 2057 2058
		fputs("  InnoDB: Warning: problems renaming ", stderr);
		ut_print_filename(stderr, old_name);
		fputs(" to ", stderr);
		ut_print_filename(stderr, new_name);
		fprintf(stderr, ", %lu iterations\n", (ulong) count);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2059 2060 2061 2062 2063 2064 2065 2066 2067
	}

	mutex_enter(&(system->mutex));

	HASH_SEARCH(hash, system->spaces, id, space, space->id == id);

	if (space == NULL) {
		fprintf(stderr,
"InnoDB: Error: cannot find space id %lu from the tablespace memory cache\n"
2068 2069 2070
"InnoDB: though the table ", (ulong) id);
		ut_print_filename(stderr, old_name);
		fputs(" in a rename operation should have that id\n", stderr);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120
		mutex_exit(&(system->mutex));

		return(FALSE);
	}

	if (count > 25000) {
		space->stop_ios = FALSE;
		mutex_exit(&(system->mutex));

		return(FALSE);
	}

	/* We temporarily close the .ibd file because we do not trust that
	operating systems can rename an open file. For the closing we have to
	wait until there are no pending i/o's or flushes on the file. */

	space->stop_ios = TRUE;

	ut_a(UT_LIST_GET_LEN(space->chain) == 1);
	node = UT_LIST_GET_FIRST(space->chain);

	if (node->n_pending > 0 || node->n_pending_flushes > 0) {
		/* There are pending i/o's or flushes, sleep for a while and
		retry */

		mutex_exit(&(system->mutex));

		os_thread_sleep(20000);

		goto retry;

	} else if (node->modification_counter > node->flush_counter) {
		/* Flush the space */

		mutex_exit(&(system->mutex));

		os_thread_sleep(20000);

		fil_flush(id);

		goto retry;

	} else if (node->open) {
		/* Close the file */

		fil_node_close_file(node, system);
	}

	/* Check that the old name in the space is right */

2121
	if (old_name_was_specified) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2122
		old_path = fil_make_ibd_name(old_name, FALSE);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2123

2124 2125 2126
		ut_a(strcmp(space->name, old_path) == 0);
		ut_a(strcmp(node->name, old_path) == 0);
	} else {
2127
		old_path = mem_strdup(space->name);
2128
	}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2129 2130

	/* Rename the tablespace and the node in the memory cache */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2131
	path = fil_make_ibd_name(new_name, FALSE);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2132 2133
	success = fil_rename_tablespace_in_mem(space, node, path);

2134 2135
	if (success) {
		success = os_file_rename(old_path, path);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2136

2137 2138 2139
		if (!success) {
			/* We have to revert the changes we made
			to the tablespace memory cache */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2140

2141 2142 2143
			ut_a(fil_rename_tablespace_in_mem(space, node,
								old_path));
		}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2144
	}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2145

2146 2147 2148
	mem_free(path);
	mem_free(old_path);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2149 2150 2151 2152
	space->stop_ios = FALSE;

	mutex_exit(&(system->mutex));

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163
#ifndef UNIV_HOTBACKUP	
	if (success) {
		mtr_t		mtr;

		mtr_start(&mtr);

		fil_op_write_log(MLOG_FILE_RENAME, id, old_name, new_name,
								&mtr);
		mtr_commit(&mtr);
	}
#endif
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2164 2165 2166 2167 2168 2169 2170
	return(success);
}

/***********************************************************************
Creates a new single-table tablespace to a database directory of MySQL.
Database directories are under the 'datadir' of MySQL. The datadir is the
directory of a running mysqld program. We can refer to it by simply the
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2171 2172
path '.'. Tables created with CREATE TEMPORARY TABLE we place in the temp
dir of the mysqld server. */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2173 2174 2175 2176

ulint
fil_create_new_single_table_tablespace(
/*===================================*/
2177 2178 2179 2180 2181 2182
					/* out: DB_SUCCESS or error code */
	ulint*		space_id,	/* in/out: space id; if this is != 0,
					then this is an input parameter,
					otherwise output */
	const char*	tablename,	/* in: the table name in the usual
					databasename/tablename format
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2183 2184 2185 2186
					of InnoDB, or a dir path to a temp
					table */
	ibool		is_temp,	/* in: TRUE if a table created with
					CREATE TEMPORARY TABLE */
2187 2188 2189
	ulint		size)		/* in: the initial size of the
					tablespace file in pages,
					must be >= FIL_IBD_FILE_INITIAL_SIZE */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2190 2191 2192 2193
{
	os_file_t       file;
	ibool		ret;
	ulint		err;
2194
	byte*		buf2;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2195 2196
	byte*		page;
	ibool		success;
2197
	char*		path;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2198

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2199
	ut_a(size >= FIL_IBD_FILE_INITIAL_SIZE);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2200

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2201
	path = fil_make_ibd_name(tablename, is_temp);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2202 2203 2204 2205 2206
	
	file = os_file_create(path, OS_FILE_CREATE, OS_FILE_NORMAL,
						    OS_DATA_FILE, &ret);
	if (ret == FALSE) {
		ut_print_timestamp(stderr);
2207 2208 2209
		fputs("  InnoDB: Error creating file ", stderr);
		ut_print_filename(stderr, path);
		fputs(".\n", stderr);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2210 2211 2212 2213 2214 2215

		/* The following call will print an error message */
		 
		err = os_file_get_last_error(TRUE);
		
		if (err == OS_FILE_ALREADY_EXISTS) {
2216
		        fputs(
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2217 2218 2219 2220 2221
"InnoDB: The file already exists though the corresponding table did not\n"
"InnoDB: exist in the InnoDB data dictionary. Have you moved InnoDB\n"
"InnoDB: .ibd files around without using the SQL commands\n"
"InnoDB: DISCARD TABLESPACE and IMPORT TABLESPACE, or did\n"
"InnoDB: mysqld crash in the middle of CREATE TABLE? You can\n"
2222 2223 2224 2225
"InnoDB: resolve the problem by removing the file ", stderr);
			ut_print_filename(stderr, path);
			fputs("\n"
"InnoDB: under the 'datadir' of MySQL.\n", stderr);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2226

2227
			mem_free(path);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2228 2229 2230 2231 2232
			return(DB_TABLESPACE_ALREADY_EXISTS);
		}

		if (err == OS_FILE_DISK_FULL) {

2233
			mem_free(path);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2234 2235 2236
			return(DB_OUT_OF_FILE_SPACE);
		}

2237
		mem_free(path);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2238 2239 2240
		return(DB_ERROR);
	}

2241 2242 2243
	buf2 = ut_malloc(2 * UNIV_PAGE_SIZE);
	/* Align the memory for file i/o if we might have O_DIRECT set */
	page = ut_align(buf2, UNIV_PAGE_SIZE);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2244 2245 2246 2247

	ret = os_file_set_size(path, file, size * UNIV_PAGE_SIZE, 0);
	
	if (!ret) {
2248
		ut_free(buf2);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2249 2250 2251
		os_file_close(file);
		os_file_delete(path);

2252
		mem_free(path);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2253 2254 2255
		return(DB_OUT_OF_FILE_SPACE);
	}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2256 2257 2258 2259 2260
	if (*space_id == 0) {
		*space_id = fil_assign_new_space_id();
	}

	/* printf("Creating tablespace %s id %lu\n", path, *space_id); */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2261 2262

	if (*space_id == ULINT_UNDEFINED) {
2263
		ut_free(buf2);
2264
	error_exit:
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2265
		os_file_close(file);
2266
	error_exit2:
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2267 2268
		os_file_delete(path);

2269
		mem_free(path);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289
		return(DB_ERROR);
	}

	/* We have to write the space id to the file immediately and flush the
	file to disk. This is because in crash recovery we must be aware what
	tablespaces exist and what are their space id's, so that we can apply
	the log records to the right file. It may take quite a while until
	buffer pool flush algorithms write anything to the file and flush it to
	disk. If we would not write here anything, the file would be filled
	with zeros from the call of os_file_set_size(), until a buffer pool
	flush would write to it. */

	memset(page, '\0', UNIV_PAGE_SIZE);

	fsp_header_write_space_id(page, *space_id);		

	buf_flush_init_for_writing(page, ut_dulint_zero, *space_id, 0);

	ret = os_file_write(path, file, page, 0, 0, UNIV_PAGE_SIZE);

2290
	ut_free(buf2);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2291 2292

	if (!ret) {
2293 2294 2295 2296
		fputs(
"InnoDB: Error: could not write the first page to tablespace ", stderr);
		ut_print_filename(stderr, path);
		putc('\n', stderr);
2297
		goto error_exit;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2298 2299 2300 2301 2302
	}

	ret = os_file_flush(file);

	if (!ret) {
2303 2304 2305 2306
		fputs(
"InnoDB: Error: file flush of tablespace ", stderr);
		ut_print_filename(stderr, path);
		fputs(" failed\n", stderr);
2307
		goto error_exit;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2308 2309 2310 2311 2312
	}

	os_file_close(file);

	if (*space_id == ULINT_UNDEFINED) {
2313
		goto error_exit2;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2314 2315 2316 2317 2318
	}

	success = fil_space_create(path, *space_id, FIL_TABLESPACE);
	
	if (!success) {
2319
		goto error_exit2;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2320 2321 2322 2323
	}	

	fil_node_create(path, size, *space_id, FALSE);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334
#ifndef UNIV_HOTBACKUP	
	{
	mtr_t		mtr;

	mtr_start(&mtr);

	fil_op_write_log(MLOG_FILE_CREATE, *space_id, tablename, NULL, &mtr);

	mtr_commit(&mtr);
	}
#endif
2335
	mem_free(path);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2336 2337 2338
	return(DB_SUCCESS);
}

2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351
/************************************************************************
It is possible, though very improbable, that the lsn's in the tablespace to be
imported have risen above the current system lsn, if a lengthy purge, ibuf
merge, or rollback was performed on a backup taken with ibbackup. If that is
the case, reset page lsn's in the file. We assume that mysqld was shut down
after it performed these cleanup operations on the .ibd file, so that it at
the shutdown stamped the latest lsn to the FIL_PAGE_FILE_FLUSH_LSN in the
first page of the .ibd file, and we can determine whether we need to reset the
lsn's just by looking at that flush lsn. */

ibool
fil_reset_too_high_lsns(
/*====================*/
2352 2353 2354 2355 2356 2357
					/* out: TRUE if success */
	const char*	name,		/* in: table name in the
					databasename/tablename format */
	dulint		current_lsn)	/* in: reset lsn's if the lsn stamped
					to FIL_PAGE_FILE_FLUSH_LSN in the
					first page is too high */
2358 2359 2360 2361
{
	os_file_t	file;
	char*		filepath;
	byte*		page;
2362
	byte*		buf2;
2363 2364 2365 2366 2367 2368 2369
	dulint		flush_lsn;
	ulint		space_id;
	ib_longlong	file_size;
	ib_longlong	offset;
	ulint		page_no;
	ibool		success;

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2370
	filepath = fil_make_ibd_name(name, FALSE);
2371 2372 2373 2374

	file = os_file_create_simple_no_error_handling(filepath, OS_FILE_OPEN,
						OS_FILE_READ_WRITE, &success);
	if (!success) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2375 2376 2377 2378 2379 2380 2381 2382 2383 2384
		/* The following call prints an error message */
		os_file_get_last_error(TRUE);

		ut_print_timestamp(stderr);

	        fputs(
"  InnoDB: Error: trying to open a table, but could not\n"
"InnoDB: open the tablespace file ", stderr);
		ut_print_filename(stderr, filepath);
		fputs("!\n", stderr);
2385
		mem_free(filepath);
2386 2387 2388 2389 2390 2391

		return(FALSE);
	}

	/* Read the first page of the tablespace */

2392 2393 2394
	buf2 = ut_malloc(2 * UNIV_PAGE_SIZE);
	/* Align the memory for file i/o if we might have O_DIRECT set */
	page = ut_align(buf2, UNIV_PAGE_SIZE);
2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418

	success = os_file_read(file, page, 0, 0, UNIV_PAGE_SIZE);
	if (!success) {

		goto func_exit;
	}

	/* We have to read the file flush lsn from the header of the file */

	flush_lsn = mach_read_from_8(page + FIL_PAGE_FILE_FLUSH_LSN);

	if (ut_dulint_cmp(current_lsn, flush_lsn) >= 0) {
		/* Ok */
		success = TRUE;

		goto func_exit;
	}

	space_id = fsp_header_get_space_id(page);
	
	ut_print_timestamp(stderr);
	fprintf(stderr,
" InnoDB: Flush lsn in the tablespace file %lu to be imported\n"
"InnoDB: is %lu %lu, which exceeds current system lsn %lu %lu.\n"
2419
"InnoDB: We reset the lsn's in the file ",
2420 2421 2422 2423
			    (ulong) space_id,
			    (ulong) ut_dulint_get_high(flush_lsn),
			    (ulong) ut_dulint_get_low(flush_lsn),
			    (ulong) ut_dulint_get_high(current_lsn),
2424 2425 2426
			    (ulong) ut_dulint_get_low(current_lsn));
	ut_print_filename(stderr, filepath);
	fputs(".\n", stderr);
2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482

	/* Loop through all the pages in the tablespace and reset the lsn and
	the page checksum if necessary */

	file_size = os_file_get_size_as_iblonglong(file);

	for (offset = 0; offset < file_size; offset += UNIV_PAGE_SIZE) {
		success = os_file_read(file, page,
				(ulint)(offset & 0xFFFFFFFFUL),
				(ulint)(offset >> 32), UNIV_PAGE_SIZE);
		if (!success) {

			goto func_exit;
		}
		if (ut_dulint_cmp(mach_read_from_8(page + FIL_PAGE_LSN),
				  current_lsn) > 0) {
			/* We have to reset the lsn */
			space_id = mach_read_from_4(page
					+ FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID);
			page_no = mach_read_from_4(page + FIL_PAGE_OFFSET);
			
			buf_flush_init_for_writing(page, current_lsn, space_id,
								      page_no);
			success = os_file_write(filepath, file, page,
				(ulint)(offset & 0xFFFFFFFFUL),
				(ulint)(offset >> 32), UNIV_PAGE_SIZE);
			if (!success) {

				goto func_exit;
			}
		}
	}

	success = os_file_flush(file);
	if (!success) {

		goto func_exit;
	}

	/* We now update the flush_lsn stamp at the start of the file */
	success = os_file_read(file, page, 0, 0, UNIV_PAGE_SIZE);
	if (!success) {

		goto func_exit;
	}

	mach_write_to_8(page + FIL_PAGE_FILE_FLUSH_LSN, current_lsn);

	success = os_file_write(filepath, file, page, 0, 0, UNIV_PAGE_SIZE);
	if (!success) {

		goto func_exit;
	}
	success = os_file_flush(file);
func_exit:
	os_file_close(file);
2483 2484
	ut_free(buf2);
	mem_free(filepath);
2485 2486 2487 2488

	return(success);
}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500
/************************************************************************
Tries to open a single-table tablespace and checks the space id is right in
it. If does not succeed, prints an error message to the .err log. This
function is used to open the tablespace when we load a table definition
to the dictionary cache. NOTE that we assume this operation is used under the
protection of the dictionary mutex, so that two users cannot race here. This
operation does not leave the file associated with the tablespace open, but
closes it after we have looked at the space id in it. */

ibool
fil_open_single_table_tablespace(
/*=============================*/
2501 2502 2503 2504
				/* out: TRUE if success */
	ulint		id,	/* in: space id */
	const char*	name)	/* in: table name in the
				databasename/tablename format */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2505 2506 2507 2508
{
	os_file_t	file;
	char*		filepath;
	ibool		success;
2509
	byte*		buf2;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2510 2511 2512 2513
	byte*		page;
	ulint		space_id;
	ibool		ret		= TRUE;

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2514
	filepath = fil_make_ibd_name(name, FALSE);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2515 2516 2517 2518 2519 2520 2521 2522 2523

	file = os_file_create_simple_no_error_handling(filepath, OS_FILE_OPEN,
						OS_FILE_READ_ONLY, &success);
	if (!success) {
		/* The following call prints an error message */
		os_file_get_last_error(TRUE);

		ut_print_timestamp(stderr);

2524
	        fputs(
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2525
"  InnoDB: Error: trying to open a table, but could not\n"
2526 2527 2528 2529
"InnoDB: open the tablespace file ", stderr);
		ut_print_filename(stderr, filepath);
		fputs("!\n"
"InnoDB: Have you moved InnoDB .ibd files around without using the\n"
2530
"InnoDB: commands DISCARD TABLESPACE and IMPORT TABLESPACE?\n"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2531 2532
"InnoDB: It is also possible that this is a table created with\n"
"InnoDB: CREATE TEMPORARY TABLE, and MySQL removed the .ibd file for this.\n"
2533 2534 2535 2536
"InnoDB: Please refer to\n"
"InnoDB:"
" http://dev.mysql.com/doc/mysql/en/InnoDB_troubleshooting_datadict.html\n"
"InnoDB: how to resolve the issue.\n", stderr);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2537

2538
		mem_free(filepath);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2539 2540 2541 2542 2543 2544

		return(FALSE);
	}

	/* Read the first page of the tablespace */

2545 2546 2547
	buf2 = ut_malloc(2 * UNIV_PAGE_SIZE);
	/* Align the memory for file i/o if we might have O_DIRECT set */
	page = ut_align(buf2, UNIV_PAGE_SIZE);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2548 2549 2550 2551 2552 2553 2554 2555 2556 2557

	success = os_file_read(file, page, 0, 0, UNIV_PAGE_SIZE);

	/* We have to read the tablespace id from the file */

	space_id = fsp_header_get_space_id(page);

	if (space_id != id) {
		ut_print_timestamp(stderr);

2558 2559 2560 2561 2562
	        fputs(
"  InnoDB: Error: tablespace id in file ", stderr);
		ut_print_filename(stderr, filepath);
		fprintf(stderr, " is %lu, but in the InnoDB\n"
"InnoDB: data dictionary it is %lu.\n"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2563
"InnoDB: Have you moved InnoDB .ibd files around without using the\n"
2564
"InnoDB: commands DISCARD TABLESPACE and IMPORT TABLESPACE?\n"
2565 2566 2567 2568
"InnoDB: Please refer to\n"
"InnoDB:"
" http://dev.mysql.com/doc/mysql/en/InnoDB_troubleshooting_datadict.html\n"
"InnoDB: how to resolve the issue.\n", (ulong) space_id, (ulong) id);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586

		ret = FALSE;

		goto func_exit;
	}

	success = fil_space_create(filepath, space_id, FIL_TABLESPACE);

	if (!success) {
		goto func_exit;
	}

	/* We do not measure the size of the file, that is why we pass the 0
	below */

	fil_node_create(filepath, 0, space_id, FALSE);
func_exit:
	os_file_close(file);
2587 2588
	ut_free(buf2);
	mem_free(filepath);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2589 2590 2591 2592

	return(ret);
}

2593 2594 2595
#ifdef UNIV_HOTBACKUP
/***********************************************************************
Allocates a file name for an old version of a single-table tablespace.
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2596
The string must be freed by caller with mem_free()! */
2597 2598 2599 2600 2601 2602 2603 2604 2605
static
char*
fil_make_ibbackup_old_name(
/*=======================*/
					/* out, own: file name */
	const char*	name)		/* in: original file name */
{
	static const char suffix[] = "_ibbackup_old_vers_";
	ulint	len	= strlen(name);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2606
	char*	path	= mem_alloc(len + (15 + sizeof suffix));
2607 2608 2609 2610 2611 2612 2613 2614

	memcpy(path, name, len);
	memcpy(path + len, suffix, (sizeof suffix) - 1);
	ut_sprintf_timestamp_without_extra_chars(path + len + sizeof suffix);
	return(path);
}
#endif /* UNIV_HOTBACKUP */

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2615 2616 2617 2618 2619 2620 2621
/************************************************************************
Opens an .ibd file and adds the associated single-table tablespace to the
InnoDB fil0fil.c data structures. */
static
void
fil_load_single_table_tablespace(
/*=============================*/
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
2622 2623 2624
	const char*	dbname,		/* in: database name */
	const char*	filename)	/* in: file name (not a path),
					including the .ibd extension */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2625 2626 2627 2628
{
	os_file_t	file;
	char*		filepath;
	ibool		success;
2629
	byte*		buf2;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2630 2631 2632 2633 2634
	byte*		page;
	ulint		space_id;
	ulint		size_low;
	ulint		size_high;
	ib_longlong	size;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2635 2636 2637
#ifdef UNIV_HOTBACKUP
	fil_space_t*	space;
#endif
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2638
	filepath = mem_alloc(strlen(dbname) + strlen(filename) 
2639
			+ strlen(fil_path_to_mysql_datadir) + 3);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2640

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2641 2642
	sprintf(filepath, "%s/%s/%s", fil_path_to_mysql_datadir, dbname,
								filename);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2643 2644 2645 2646 2647 2648 2649 2650 2651 2652
	srv_normalize_path_for_win(filepath);

	file = os_file_create_simple_no_error_handling(filepath, OS_FILE_OPEN,
						OS_FILE_READ_ONLY, &success);
	if (!success) {
		/* The following call prints an error message */
		os_file_get_last_error(TRUE);

	        fprintf(stderr,
"InnoDB: Error: could not open single-table tablespace file\n"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2653
"InnoDB: %s!\n"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2654
"InnoDB: We do not continue crash recovery, because the table will become\n"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2655 2656 2657 2658 2659 2660 2661 2662 2663 2664
"InnoDB: corrupt if we cannot apply the log records in the InnoDB log to it.\n"
"InnoDB: To fix the problem and start mysqld:\n"
"InnoDB: 1) If there is a permission problem in the file and mysqld cannot\n"
"InnoDB: open the file, you should modify the permissions.\n"
"InnoDB: 2) If the table is not needed, or you can restore it from a backup,\n"
"InnoDB: then you can remove the .ibd file, and InnoDB will do a normal\n"
"InnoDB: crash recovery and ignore that table.\n"
"InnoDB: 3) If the file system or the disk is broken, and you cannot remove\n"
"InnoDB: the .ibd file, you can set innodb_force_recovery > 0 in my.cnf\n"
"InnoDB: and force InnoDB to continue crash recovery here.\n", filepath);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2665

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2666
		mem_free(filepath);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2667

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2668 2669 2670 2671 2672 2673 2674 2675 2676
		if (srv_force_recovery > 0) {
			fprintf(stderr,
"InnoDB: innodb_force_recovery was set to %lu. Continuing crash recovery\n"
"InnoDB: even though we cannot access the .ibd file of this table.\n",
							srv_force_recovery);
			return;
		}

		exit(1);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2677 2678 2679 2680 2681 2682 2683 2684 2685 2686
	}

	success = os_file_get_size(file, &size_low, &size_high);

	if (!success) {
		/* The following call prints an error message */
		os_file_get_last_error(TRUE);

	        fprintf(stderr,
"InnoDB: Error: could not measure the size of single-table tablespace file\n"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2687
"InnoDB: %s!\n"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2688
"InnoDB: We do not continue crash recovery, because the table will become\n"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2689 2690 2691 2692 2693 2694 2695 2696 2697 2698
"InnoDB: corrupt if we cannot apply the log records in the InnoDB log to it.\n"
"InnoDB: To fix the problem and start mysqld:\n"
"InnoDB: 1) If there is a permission problem in the file and mysqld cannot\n"
"InnoDB: access the file, you should modify the permissions.\n"
"InnoDB: 2) If the table is not needed, or you can restore it from a backup,\n"
"InnoDB: then you can remove the .ibd file, and InnoDB will do a normal\n"
"InnoDB: crash recovery and ignore that table.\n"
"InnoDB: 3) If the file system or the disk is broken, and you cannot remove\n"
"InnoDB: the .ibd file, you can set innodb_force_recovery > 0 in my.cnf\n"
"InnoDB: and force InnoDB to continue crash recovery here.\n", filepath);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2699 2700

		os_file_close(file);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2701
		mem_free(filepath);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2702

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2703 2704 2705 2706 2707 2708 2709 2710 2711
		if (srv_force_recovery > 0) {
			fprintf(stderr,
"InnoDB: innodb_force_recovery was set to %lu. Continuing crash recovery\n"
"InnoDB: even though we cannot access the .ibd file of this table.\n",
							srv_force_recovery);
			return;
		}

		exit(1);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2712 2713
	}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2714 2715 2716
	/* TODO: What to do in other cases where we cannot access an .ibd
	file during a crash recovery? */

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2717 2718
	/* Every .ibd file is created >= 4 pages in size. Smaller files
	cannot be ok. */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2719

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2720 2721 2722
	size = (((ib_longlong)size_high) << 32) + (ib_longlong)size_low;
#ifndef UNIV_HOTBACKUP
	if (size < FIL_IBD_FILE_INITIAL_SIZE * UNIV_PAGE_SIZE) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2723 2724
	        fprintf(stderr,
"InnoDB: Error: the size of single-table tablespace file %s\n"
2725 2726 2727
"InnoDB: is only %lu %lu, should be at least %lu!", filepath,
			(ulong) size_high,
			(ulong) size_low, (ulong) (4 * UNIV_PAGE_SIZE));
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2728
		os_file_close(file);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2729
		mem_free(filepath);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2730 2731 2732

		return;
	}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2733 2734
#endif
	/* Read the first page of the tablespace if the size big enough */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2735

2736 2737 2738
	buf2 = ut_malloc(2 * UNIV_PAGE_SIZE);
	/* Align the memory for file i/o if we might have O_DIRECT set */
	page = ut_align(buf2, UNIV_PAGE_SIZE);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2739

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2740 2741
	if (size >= FIL_IBD_FILE_INITIAL_SIZE * UNIV_PAGE_SIZE) {
		success = os_file_read(file, page, 0, 0, UNIV_PAGE_SIZE);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2742

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2743
		/* We have to read the tablespace id from the file */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2744

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2745 2746 2747 2748
		space_id = fsp_header_get_space_id(page);
	} else {
		space_id = ULINT_UNDEFINED;
	}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2749

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2750
#ifndef UNIV_HOTBACKUP
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2751 2752
	if (space_id == ULINT_UNDEFINED || space_id == 0) {
	        fprintf(stderr,
2753 2754 2755
"InnoDB: Error: tablespace id %lu in file %s is not sensible\n",
			(ulong) space_id,
			filepath);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2756 2757
		goto func_exit;
	}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770
#else
	if (space_id == ULINT_UNDEFINED || space_id == 0) {
		char*	new_path;

		fprintf(stderr,
"InnoDB: Renaming tablespace %s of id %lu,\n"
"InnoDB: to %s_ibbackup_old_vers_<timestamp>\n"
"InnoDB: because its size %lld is too small (< 4 pages 16 kB each),\n"
"InnoDB: or the space id in the file header is not sensible.\n"
"InnoDB: This can happen in an ibbackup run, and is not dangerous.\n",
				filepath, space_id, filepath, size);
		os_file_close(file);

2771
		new_path = fil_make_ibbackup_old_name(filepath);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2772 2773
		ut_a(os_file_rename(filepath, new_path));

2774
		ut_free(buf2);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2775 2776
		mem_free(filepath);
		mem_free(new_path);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803

		return;
	}

	/* A backup may contain the same space several times, if the space got
	renamed at a sensitive time. Since it is enough to have one version of
	the space, we rename the file if a space with the same space id
	already exists in the tablespace memory cache. We rather rename the
	file than delete it, because if there is a bug, we do not want to
	destroy valuable data. */

	mutex_enter(&(fil_system->mutex));

	space = fil_get_space_for_id_low(space_id);

	if (space) {
		char*	new_path;

		fprintf(stderr,
"InnoDB: Renaming tablespace %s of id %lu,\n"
"InnoDB: to %s_ibbackup_old_vers_<timestamp>\n"
"InnoDB: because space %s with the same id\n"
"InnoDB: was scanned earlier. This can happen if you have renamed tables\n"
"InnoDB: during an ibbackup run.\n", filepath, space_id, filepath,
								space->name);
		os_file_close(file);

2804
		new_path = fil_make_ibbackup_old_name(filepath);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2805 2806 2807 2808 2809

		mutex_exit(&(fil_system->mutex));

		ut_a(os_file_rename(filepath, new_path));

2810
		ut_free(buf2);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2811 2812
		mem_free(filepath);
		mem_free(new_path);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2813 2814 2815 2816 2817

		return;
	}
	mutex_exit(&(fil_system->mutex));
#endif
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830
	success = fil_space_create(filepath, space_id, FIL_TABLESPACE);

	if (!success) {

		goto func_exit;
	}

	/* We do not measure the size of the file, that is why we pass the 0
	below */

	fil_node_create(filepath, 0, space_id, FALSE);
func_exit:
	os_file_close(file);
2831
	ut_free(buf2);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2832
	mem_free(filepath);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848
}

/************************************************************************
At the server startup, if we need crash recovery, scans the database
directories under the MySQL datadir, looking for .ibd files. Those files are
single-table tablespaces. We need to know the space id in each of them so that
we know into which file we should look to check the contents of a page stored
in the doublewrite buffer, also to know where to apply log records where the
space id is != 0. */

ulint
fil_load_single_table_tablespaces(void)
/*===================================*/
			/* out: DB_SUCCESS or error number */
{
	int		ret;
2849
	char*		dbpath		= NULL;
2850
	ulint		dbpath_len	= 100;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2851 2852 2853 2854 2855 2856 2857
	os_file_dir_t	dir;
	os_file_dir_t	dbdir;
	os_file_stat_t	dbinfo;
	os_file_stat_t	fileinfo;

	/* The datadir of MySQL is always the default directory of mysqld */

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
2858
	dir = os_file_opendir(fil_path_to_mysql_datadir, TRUE);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2859 2860 2861 2862 2863 2864

	if (dir == NULL) {

		return(DB_ERROR);
	}

2865
	dbpath = mem_alloc(dbpath_len);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2866 2867 2868 2869

	/* Scan all directories under the datadir. They are the database
	directories of MySQL. */

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
2870
	ret = os_file_readdir_next_file(fil_path_to_mysql_datadir, dir,
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2871
								&dbinfo);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2872
	while (ret == 0) {
2873
		ulint len;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884
		/* printf("Looking at %s in datadir\n", dbinfo.name); */

		if (dbinfo.type == OS_FILE_TYPE_FILE
		    || dbinfo.type == OS_FILE_TYPE_UNKNOWN) {

		        goto next_datadir_item;
		}

		/* We found a symlink or a directory; try opening it to see
		if a symlink is a directory */

2885 2886 2887 2888
		len = strlen(fil_path_to_mysql_datadir)
				+ strlen (dbinfo.name) + 2;
		if (len > dbpath_len) {
			dbpath_len = len;
2889

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2890 2891
			if (dbpath) {
				mem_free(dbpath);
2892
			}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2893 2894

			dbpath = mem_alloc(dbpath_len);
2895
		}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2896 2897
		sprintf(dbpath, "%s/%s", fil_path_to_mysql_datadir,
								dbinfo.name);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934
		srv_normalize_path_for_win(dbpath);

		dbdir = os_file_opendir(dbpath, FALSE);

		if (dbdir != NULL) {
			/* printf("Opened dir %s\n", dbinfo.name); */

			/* We found a database directory; loop through it,
			looking for possible .ibd files in it */

			ret = os_file_readdir_next_file(dbpath, dbdir,
								&fileinfo);
			while (ret == 0) {
				/* printf(
"     Looking at file %s\n", fileinfo.name); */

			        if (fileinfo.type == OS_FILE_TYPE_DIR
				    || dbinfo.type == OS_FILE_TYPE_UNKNOWN) {
				        goto next_file_item;
				}

				/* We found a symlink or a file */
				if (strlen(fileinfo.name) > 4
				    && 0 == strcmp(fileinfo.name + 
						strlen(fileinfo.name) - 4,
						".ibd")) {
				        /* The name ends in .ibd; try opening
					the file */
					fil_load_single_table_tablespace(
						dbinfo.name, fileinfo.name);
				}
next_file_item:
				ret = os_file_readdir_next_file(dbpath, dbdir,
								&fileinfo);
			}

			if (0 != os_file_closedir(dbdir)) {
2935 2936 2937 2938
				 fputs(
"InnoDB: Warning: could not close database directory ", stderr);
				 ut_print_filename(stderr, dbpath);
				 putc('\n', stderr);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2939 2940 2941 2942
			}
		}
		
next_datadir_item:
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
2943
		ret = os_file_readdir_next_file(fil_path_to_mysql_datadir,
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2944
								dir, &dbinfo);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2945 2946
	}

2947
	mem_free(dbpath);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990

	/* At the end of directory we should get 1 as the return value, -1
	if there was an error */
	if (ret != 1) {
		fprintf(stderr,
"InnoDB: Error: os_file_readdir_next_file returned %d in MySQL datadir\n",
							       ret);
		os_file_closedir(dir);

		return(DB_ERROR);
	}

	if (0 != os_file_closedir(dir)) {
		fprintf(stderr,
"InnoDB: Error: could not close MySQL datadir\n");

		return(DB_ERROR);
	}

	return(DB_SUCCESS);
}

/************************************************************************
If we need crash recovery, and we have called
fil_load_single_table_tablespaces() and dict_load_single_table_tablespaces(),
we can call this function to print an error message of orphaned .ibd files
for which there is not a data dictionary entry with a matching table name
and space id. */

void
fil_print_orphaned_tablespaces(void)
/*================================*/
{
	fil_system_t*	system 		= fil_system;
	fil_space_t*	space;

	mutex_enter(&(system->mutex));

	space = UT_LIST_GET_FIRST(system->space_list);

	while (space) {
	        if (space->purpose == FIL_TABLESPACE && space->id != 0
							  && !space->mark) {
2991 2992 2993 2994
			fputs("InnoDB: Warning: tablespace ", stderr);
			ut_print_filename(stderr, space->name);
			fprintf(stderr, " of id %lu has no matching table in\n"
"InnoDB: the InnoDB data dictionary.\n", (ulong) space->id);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031
		}

		space = UT_LIST_GET_NEXT(space_list, space);
	}

	mutex_exit(&(system->mutex));	
}

/***********************************************************************
Returns TRUE if a single-table tablespace does not exist in the memory cache,
or is being deleted there. */

ibool
fil_tablespace_deleted_or_being_deleted_in_mem(
/*===========================================*/
				/* out: TRUE if does not exist or is being\
				deleted */
	ulint		id,	/* in: space id */
	ib_longlong	version)/* in: tablespace_version should be this; if
				you pass -1 as the value of this, then this
				parameter is ignored */
{
	fil_system_t*	system	= fil_system;
	fil_space_t*	space;

	ut_ad(system);

	mutex_enter(&(system->mutex));

	HASH_SEARCH(hash, system->spaces, id, space, space->id == id);

	if (space == NULL || space->is_being_deleted) {
		mutex_exit(&(system->mutex));

		return(TRUE);
	}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3032 3033
	if (version != ((ib_longlong)-1)
				&& space->tablespace_version != version) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080
		mutex_exit(&(system->mutex));

		return(TRUE);
	}

	mutex_exit(&(system->mutex));

	return(FALSE);
}

/***********************************************************************
Returns TRUE if a single-table tablespace exists in the memory cache. */

ibool
fil_tablespace_exists_in_mem(
/*=========================*/
			/* out: TRUE if exists */
	ulint	id)	/* in: space id */
{
	fil_system_t*	system		= fil_system;
	fil_space_t*	space;

	ut_ad(system);

	mutex_enter(&(system->mutex));

	HASH_SEARCH(hash, system->spaces, id, space, space->id == id);

	if (space == NULL) {
		mutex_exit(&(system->mutex));

		return(FALSE);
	}

	mutex_exit(&(system->mutex));

	return(TRUE);
}

/***********************************************************************
Returns TRUE if a matching tablespace exists in the InnoDB tablespace memory
cache. Note that if we have not done a crash recovery at the database startup,
there may be many tablespaces which are not yet in the memory cache. */

ibool
fil_space_for_table_exists_in_mem(
/*==============================*/
3081 3082 3083 3084
					/* out: TRUE if a matching tablespace
					exists in the memory cache */
	ulint		id,		/* in: space id */
	const char*	name,		/* in: table name in the standard
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3085 3086 3087 3088
					'databasename/tablename' format or
					the dir path to a temp table */
	ibool		is_temp,	/* in: TRUE if created with CREATE
					TEMPORARY TABLE */
3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099
	ibool		mark_space,	/* in: in crash recovery, at database
					startup we mark all spaces which have
					an associated table in the InnoDB
					data dictionary, so that
					we can print a warning about orphaned
					tablespaces */
	ibool		print_error_if_does_not_exist)
					/* in: print detailed error
					information to the .err log if a
					matching tablespace is not found from
					memory */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3100 3101 3102 3103
{
	fil_system_t*	system		= fil_system;
	fil_space_t*	namespace;
	fil_space_t*	space;
3104
	char*		path;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3105 3106 3107 3108 3109

	ut_ad(system);

	mutex_enter(&(system->mutex));

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3110
	path = fil_make_ibd_name(name, is_temp);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121

	/* Look if there is a space with the same id */

	HASH_SEARCH(hash, system->spaces, id, space, space->id == id);

	/* Look if there is a space with the same name; the name is the
	directory path from the datadir to the file */

	HASH_SEARCH(name_hash, system->name_hash,
					ut_fold_string(path), namespace,
					0 == strcmp(namespace->name, path));
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3122 3123 3124 3125 3126 3127
	if (space && space == namespace) {
		/* Found */
		
		if (mark_space) {
			space->mark = TRUE;
		}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3128

3129
		mem_free(path);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3130
		mutex_exit(&(system->mutex));
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3131

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3132 3133 3134 3135
		return(TRUE);
	}

	if (!print_error_if_does_not_exist) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3136
		
3137
		mem_free(path);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3138 3139 3140 3141 3142 3143 3144 3145
		mutex_exit(&(system->mutex));
		
		return(FALSE);
	}

	if (space == NULL) {
		if (namespace == NULL) {
		        ut_print_timestamp(stderr);
3146 3147 3148
			fputs("  InnoDB: Error: table ", stderr);
			ut_print_filename(stderr, name);
			fprintf(stderr, "\n"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3149 3150
"InnoDB: in InnoDB data dictionary has tablespace id %lu,\n"
"InnoDB: but tablespace with that id or name does not exist. Have\n"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3151 3152 3153 3154
"InnoDB: you deleted or moved .ibd files?\n"
"InnoDB: This may also be a table created with CREATE TEMPORARY TABLE\n"
"InnoDB: whose .ibd and .frm files MySQL automatically removed, but the\n"
"InnoDB: table still exists in the InnoDB internal data dictionary.\n",
3155
				(ulong) id);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3156 3157
		} else {
		        ut_print_timestamp(stderr);
3158 3159 3160
			fputs("  InnoDB: Error: table ", stderr);
			ut_print_filename(stderr, name);
			fprintf(stderr, "\n"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3161 3162
"InnoDB: in InnoDB data dictionary has tablespace id %lu,\n"
"InnoDB: but tablespace with that id does not exist. There is\n"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3163
"InnoDB: a tablespace of name %s and id %lu, though. Have\n"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3164
"InnoDB: you deleted or moved .ibd files?\n",
3165
				(ulong) id, namespace->name,
3166
				(ulong) namespace->id);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3167
		}
3168 3169 3170 3171 3172 3173
	error_exit:
		fputs(
"InnoDB: Please refer to\n"
"InnoDB:"
" http://dev.mysql.com/doc/mysql/en/InnoDB_troubleshooting_datadict.html\n"
"InnoDB: how to resolve the issue.\n", stderr);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3174

3175
		mem_free(path);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3176 3177 3178 3179 3180 3181 3182
		mutex_exit(&(system->mutex));

		return(FALSE);
	}

	if (0 != strcmp(space->name, path)) {
		ut_print_timestamp(stderr);
3183 3184 3185
		fputs("  InnoDB: Error: table ", stderr);
		ut_print_filename(stderr, name);
		fprintf(stderr, "\n"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3186 3187
"InnoDB: in InnoDB data dictionary has tablespace id %lu,\n"
"InnoDB: but tablespace with that id has name %s.\n"
3188
"InnoDB: Have you deleted or moved .ibd files?\n", (ulong) id, space->name);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3189

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3190
		if (namespace != NULL) {
3191
			fputs(
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3192
"InnoDB: There is a tablespace with the right name\n"
3193 3194 3195 3196
"InnoDB: ", stderr);
			ut_print_filename(stderr, namespace->name);
			fprintf(stderr, ", but its id is %lu.\n",
				(ulong) namespace->id);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3197 3198
		}

3199
		goto error_exit;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3200 3201
	}

3202
	mem_free(path);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3203
	mutex_exit(&(system->mutex));
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3204

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216
	return(FALSE);
}

/***********************************************************************
Checks if a single-table tablespace for a given table name exists in the
tablespace memory cache. */
static
ulint
fil_get_space_id_for_table(
/*=======================*/
				/* out: space id, ULINT_UNDEFINED if not
				found */
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
3217
	const char*	name)	/* in: table name in the standard
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3218 3219 3220 3221 3222
				'databasename/tablename' format */
{
	fil_system_t*	system		= fil_system;
	fil_space_t*	namespace;
	ulint		id		= ULINT_UNDEFINED;
3223
	char*		path;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3224 3225 3226 3227 3228

	ut_ad(system);

	mutex_enter(&(system->mutex));

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3229
	path = fil_make_ibd_name(name, FALSE);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3230 3231 3232 3233 3234 3235 3236 3237 3238 3239

	/* Look if there is a space with the same name; the name is the
	directory path to the file */

	HASH_SEARCH(name_hash, system->name_hash,
					ut_fold_string(path), namespace,
					0 == strcmp(namespace->name, path));
	if (namespace) {
		id = namespace->id;
	}	
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3240

3241 3242
	mem_free(path);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3243 3244
	mutex_exit(&(system->mutex));

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3245
	return(id);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3246 3247 3248
}

/**************************************************************************
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3249 3250 3251
Tries to extend a data file so that it would accommodate the number of pages
given. The tablespace must be cached in the memory cache. If the space is big
enough already, does nothing. */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3252 3253

ibool
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3254 3255 3256 3257 3258 3259
fil_extend_space_to_desired_size(
/*=============================*/
				/* out: TRUE if success */
	ulint*	actual_size,	/* out: size of the space after extension;
				if we ran out of disk space this may be lower
				than the desired size */
3260
	ulint	space_id,	/* in: space id */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3261 3262 3263
	ulint	size_after_extend)/* in: desired size in pages after the
				extension; if the current space size is bigger
				than this already, the function does nothing */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3264 3265 3266 3267 3268 3269
{
	fil_system_t*	system		= fil_system;
	fil_node_t*	node;
	fil_space_t*	space;
	byte*		buf2;
	byte*		buf;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3270 3271 3272 3273 3274 3275
	ulint		start_page_no;
	ulint		file_start_page_no;
	ulint		n_pages;
	ulint		offset_high;
	ulint		offset_low;
	ibool		success		= TRUE;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3276 3277 3278 3279 3280 3281

	fil_mutex_enter_and_prepare_for_io(space_id);

	HASH_SEARCH(hash, system->spaces, space_id, space,
						space->id == space_id);
	ut_a(space);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3282 3283 3284 3285 3286 3287 3288 3289 3290 3291

	if (space->size >= size_after_extend) {
		/* Space already big enough */

		*actual_size = space->size;

		mutex_exit(&(system->mutex));	

		return(TRUE);
	}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3292 3293 3294 3295 3296
	
	node = UT_LIST_GET_LAST(space->chain);

	fil_node_prepare_for_io(node, system, space);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3297
	/* Extend 1 MB at a time */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3298 3299 3300 3301 3302 3303

	buf2 = mem_alloc(1024 * 1024 + UNIV_PAGE_SIZE);
	buf = ut_align(buf2, UNIV_PAGE_SIZE);

	memset(buf, '\0', 1024 * 1024);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3304 3305 3306 3307 3308 3309 3310 3311 3312
	start_page_no = space->size;
	file_start_page_no = space->size - node->size;

	while (start_page_no < size_after_extend) {	
		n_pages = size_after_extend - start_page_no;

		if (n_pages > (1024 * 1024) / UNIV_PAGE_SIZE) {
			n_pages = (1024 * 1024) / UNIV_PAGE_SIZE;
		}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3313

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3314 3315 3316 3317 3318 3319 3320 3321 3322 3323
		offset_high = (start_page_no - file_start_page_no)
				/ (4096 * ((1024 * 1024) / UNIV_PAGE_SIZE));
		offset_low  = ((start_page_no - file_start_page_no)
				% (4096 * ((1024 * 1024) / UNIV_PAGE_SIZE)))
			      * UNIV_PAGE_SIZE;
#ifdef UNIV_HOTBACKUP
		success = os_file_write(node->name, node->handle, buf,
					offset_low, offset_high,
					UNIV_PAGE_SIZE * n_pages);
#else
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3324 3325
		success = os_aio(OS_FILE_WRITE, OS_AIO_SYNC,
			node->name, node->handle, buf,
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344
			offset_low, offset_high,
			UNIV_PAGE_SIZE * n_pages,
			NULL, NULL);
#endif
		if (success) {
			node->size += n_pages;
			space->size += n_pages;

			os_has_said_disk_full = FALSE;
		} else {
			/* Let us measure the size of the file to determine
			how much we were able to extend it */
			
			n_pages = ((ulint)
				(os_file_get_size_as_iblonglong(node->handle)
				/ UNIV_PAGE_SIZE)) - node->size;

			node->size += n_pages;
			space->size += n_pages;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3345 3346 3347 3348

			break;
		}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3349
		start_page_no += n_pages;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3350 3351 3352 3353 3354 3355
	}

	mem_free(buf2);

	fil_node_complete_io(node, system, OS_FILE_WRITE);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3356
	*actual_size = space->size;
3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367

	if (space_id == 0) {
		ulint pages_per_mb = (1024 * 1024) / UNIV_PAGE_SIZE;

		/* Keep the last data file size info up to date, rounded to
		full megabytes */

		srv_data_file_sizes[srv_n_data_files - 1] =
				(node->size / pages_per_mb) * pages_per_mb;
	}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3368 3369 3370
	/*
        printf("Extended %s to %lu, actual size %lu pages\n", space->name,
                                        size_after_extend, *actual_size); */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3371 3372 3373 3374
	mutex_exit(&(system->mutex));	

	fil_flush(space_id);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3375
	return(success);
3376 3377
}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3378 3379 3380 3381
#ifdef UNIV_HOTBACKUP
/************************************************************************
Extends all tablespaces to the size stored in the space header. During the
ibbackup --apply-log phase we extended the spaces on-demand so that log records
3382
could be applied, but that may have left spaces still too small compared to
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3383
the size stored in the space header. */
3384

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3385 3386 3387
void
fil_extend_tablespaces_to_stored_len(void)
/*======================================*/
3388
{
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3389
	fil_system_t*	system 		= fil_system;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3390 3391
	fil_space_t*	space;
	byte*		buf;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3392 3393 3394
	ulint		actual_size;
	ulint		size_in_header;
	ulint		error;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3395 3396
	ibool		success;

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3397
	buf = mem_alloc(UNIV_PAGE_SIZE);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3398

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3399
	mutex_enter(&(system->mutex));
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3400

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3401
	space = UT_LIST_GET_FIRST(system->space_list);
3402

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3403 3404
	while (space) {
	        ut_a(space->purpose == FIL_TABLESPACE);
3405

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3406 3407 3408 3409 3410 3411
		mutex_exit(&(system->mutex)); /* no need to protect with a
					      mutex, because this is a single-
					      threaded operation */
		error = fil_read(TRUE, space->id, 0, 0, UNIV_PAGE_SIZE, buf,
									NULL);
		ut_a(error == DB_SUCCESS);
3412

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3413
		size_in_header = fsp_get_size_low(buf);
3414

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425
		success = fil_extend_space_to_desired_size(&actual_size,
						space->id, size_in_header);
		if (!success) {
			fprintf(stderr,
"InnoDB: Error: could not extend the tablespace of %s\n"
"InnoDB: to the size stored in header, %lu pages;\n"
"InnoDB: size after extension %lu pages\n"
"InnoDB: Check that you have free disk space and retry!\n", space->name,
					size_in_header, actual_size);
			exit(1);				
		}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3426

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3427
		mutex_enter(&(system->mutex));
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3428

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3429
		space = UT_LIST_GET_NEXT(space_list, space);
3430 3431
	}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3432
	mutex_exit(&(system->mutex));
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3433

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3434
	mem_free(buf);
3435
}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3436
#endif
3437

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3438 3439
/*========== RESERVE FREE EXTENTS (for a B-tree split, for example) ===*/

3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451
/***********************************************************************
Tries to reserve free extents in a file space. */

ibool
fil_space_reserve_free_extents(
/*===========================*/
				/* out: TRUE if succeed */
	ulint	id,		/* in: space id */
	ulint	n_free_now,	/* in: number of free extents now */
	ulint	n_to_reserve)	/* in: how many one wants to reserve */
{
	fil_system_t*	system		= fil_system;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3452
	fil_space_t*	space;
3453 3454 3455 3456 3457 3458 3459 3460
	ibool		success;

	ut_ad(system);

	mutex_enter(&(system->mutex));

	HASH_SEARCH(hash, system->spaces, id, space, space->id == id);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3461 3462
	ut_a(space);

3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484
	if (space->n_reserved_extents + n_to_reserve > n_free_now) {
		success = FALSE;
	} else {
		space->n_reserved_extents += n_to_reserve;
		success = TRUE;
	}
	
	mutex_exit(&(system->mutex));

	return(success);
}

/***********************************************************************
Releases free extents in a file space. */

void
fil_space_release_free_extents(
/*===========================*/
	ulint	id,		/* in: space id */
	ulint	n_reserved)	/* in: how many one reserved */
{
	fil_system_t*	system		= fil_system;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3485
	fil_space_t*	space;
3486 3487 3488 3489 3490 3491 3492

	ut_ad(system);

	mutex_enter(&(system->mutex));

	HASH_SEARCH(hash, system->spaces, id, space, space->id == id);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3493
	ut_a(space);
3494 3495 3496 3497 3498 3499 3500
	ut_a(space->n_reserved_extents >= n_reserved);
	
	space->n_reserved_extents -= n_reserved;
	
	mutex_exit(&(system->mutex));
}

3501 3502 3503 3504 3505 3506 3507 3508 3509 3510
/***********************************************************************
Gets the number of reserved extents. If the database is silent, this number
should be zero. */

ulint
fil_space_get_n_reserved_extents(
/*=============================*/
	ulint	id)		/* in: space id */
{
	fil_system_t*	system		= fil_system;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3511
	fil_space_t*	space;
3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528
	ulint		n;

	ut_ad(system);

	mutex_enter(&(system->mutex));

	HASH_SEARCH(hash, system->spaces, id, space, space->id == id);
	
	ut_a(space);

	n = space->n_reserved_extents;
	
	mutex_exit(&(system->mutex));

	return(n);
}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3529 3530
/*============================ FILE I/O ================================*/

3531
/************************************************************************
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3532 3533
NOTE: you must call fil_mutex_enter_and_prepare_for_io() first!

3534 3535
Prepares a file node for i/o. Opens the file if it is closed. Updates the
pending i/o's field in the node and the system appropriately. Takes the node
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3536 3537
off the LRU list if it is in the LRU list. The caller must hold the fil_sys
mutex. */
3538 3539 3540 3541 3542
static
void
fil_node_prepare_for_io(
/*====================*/
	fil_node_t*	node,	/* in: file node */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3543
	fil_system_t*	system,	/* in: tablespace memory cache */
3544 3545 3546
	fil_space_t*	space)	/* in: space */
{
	ut_ad(node && system && space);
3547
#ifdef UNIV_SYNC_DEBUG
3548
	ut_ad(mutex_own(&(system->mutex)));
3549
#endif /* UNIV_SYNC_DEBUG */
3550
	
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3551 3552 3553 3554
	if (system->n_open > system->max_n_open + 5) {
		ut_print_timestamp(stderr);
		fprintf(stderr,
"  InnoDB: Warning: open files %lu exceeds the limit %lu\n",
3555 3556
			(ulong) system->n_open,
			(ulong) system->max_n_open);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3557 3558
	}

3559
	if (node->open == FALSE) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3560
		/* File is closed: open it */
3561 3562
		ut_a(node->n_pending == 0);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3563
		fil_node_open_file(node, system, space);
3564 3565
	}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3566 3567
	if (node->n_pending == 0 && space->purpose == FIL_TABLESPACE
						      && space->id != 0) {
3568 3569
		/* The node is in the LRU list, remove it */

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3570
		ut_a(UT_LIST_GET_LEN(system->LRU) > 0);
3571

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3572
		UT_LIST_REMOVE(LRU, system->LRU, node);
3573
	}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3574 3575

	node->n_pending++;
3576 3577 3578 3579
}

/************************************************************************
Updates the data structures when an i/o operation finishes. Updates the
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3580
pending i/o's field in the node appropriately. */
3581 3582 3583 3584 3585
static
void
fil_node_complete_io(
/*=================*/
	fil_node_t*	node,	/* in: file node */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3586 3587 3588 3589
	fil_system_t*	system,	/* in: tablespace memory cache */
	ulint		type)	/* in: OS_FILE_WRITE or OS_FILE_READ; marks
				the node as modified if
				type == OS_FILE_WRITE */
3590 3591 3592
{
	ut_ad(node);
	ut_ad(system);
3593
#ifdef UNIV_SYNC_DEBUG
3594
	ut_ad(mutex_own(&(system->mutex)));
3595
#endif /* UNIV_SYNC_DEBUG */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3596

3597 3598 3599 3600
	ut_a(node->n_pending > 0);
	
	node->n_pending--;

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3601 3602 3603
	if (type == OS_FILE_WRITE) {
		system->modification_counter++;
		node->modification_counter = system->modification_counter;
3604 3605
	}
	
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3606 3607
	if (node->n_pending == 0 && node->space->purpose == FIL_TABLESPACE
					&& node->space->id != 0) {
3608 3609
		/* The node must be put back to the LRU list */
		UT_LIST_ADD_FIRST(LRU, system->LRU, node);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3610 3611 3612
	}
}

3613 3614 3615
/************************************************************************
Reads or writes data. This operation is asynchronous (aio). */

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3616
ulint
3617 3618
fil_io(
/*===*/
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3619 3620 3621
				/* out: DB_SUCCESS, or DB_TABLESPACE_DELETED
				if we are trying to do i/o on a tablespace
				which does not exist */
3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636
	ulint	type,		/* in: OS_FILE_READ or OS_FILE_WRITE,
				ORed to OS_FILE_LOG, if a log i/o
				and ORed to OS_AIO_SIMULATED_WAKE_LATER
				if simulated aio and we want to post a
				batch of i/os; NOTE that a simulated batch
				may introduce hidden chances of deadlocks,
				because i/os are not actually handled until
				all have been posted: use with great
				caution! */
	ibool	sync,		/* in: TRUE if synchronous aio is desired */
	ulint	space_id,	/* in: space id */
	ulint	block_offset,	/* in: offset in number of blocks */
	ulint	byte_offset,	/* in: remainder of offset in bytes; in
				aio this must be divisible by the OS block
				size */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3637 3638 3639
	ulint	len,		/* in: how many bytes to read or write; this
				must not cross a file boundary; in aio this
				must be a block size multiple */
3640 3641 3642 3643 3644 3645
	void*	buf,		/* in/out: buffer where to store read data
				or from where to write; in aio this must be
				appropriately aligned */
	void*	message)	/* in: message for aio handler if non-sync
				aio used, else ignored */
{
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3646
	fil_system_t*	system		= fil_system;
3647 3648 3649 3650 3651 3652 3653 3654
	ulint		mode;
	fil_space_t*	space;
	fil_node_t*	node;
	ulint		offset_high;
	ulint		offset_low;
	ibool		ret;
	ulint		is_log;
	ulint		wake_later;
3655
	
3656 3657 3658 3659 3660 3661 3662 3663 3664
	is_log = type & OS_FILE_LOG;
	type = type & ~OS_FILE_LOG;

	wake_later = type & OS_AIO_SIMULATED_WAKE_LATER;
	type = type & ~OS_AIO_SIMULATED_WAKE_LATER;
	
	ut_ad(byte_offset < UNIV_PAGE_SIZE);
	ut_ad(buf);
	ut_ad(len > 0);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3665
	ut_a((1 << UNIV_PAGE_SIZE_SHIFT) == UNIV_PAGE_SIZE);
3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677
	ut_ad(fil_validate());
#ifndef UNIV_LOG_DEBUG
	/* ibuf bitmap pages must be read in the sync aio mode: */
	ut_ad(recv_no_ibuf_operations || (type == OS_FILE_WRITE)
		|| !ibuf_bitmap_page(block_offset) || sync || is_log);
#ifdef UNIV_SYNC_DEBUG
	ut_ad(!ibuf_inside() || is_log || (type == OS_FILE_WRITE)
					|| ibuf_page(space_id, block_offset));
#endif
#endif
	if (sync) {
		mode = OS_AIO_SYNC;
3678
	} else if (type == OS_FILE_READ && !is_log
3679 3680 3681 3682 3683 3684 3685 3686
				&& ibuf_page(space_id, block_offset)) {
		mode = OS_AIO_IBUF;
	} else if (is_log) {
		mode = OS_AIO_LOG;
	} else {
		mode = OS_AIO_NORMAL;
	}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3687 3688
	/* Reserve the fil_system mutex and make sure that we can open at
	least one file while holding it, if the file is not already open */
3689

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3690
	fil_mutex_enter_and_prepare_for_io(space_id);
3691
	
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3692 3693 3694
	HASH_SEARCH(hash, system->spaces, space_id, space,
							space->id == space_id);
	if (!space) {
3695 3696
		mutex_exit(&(system->mutex));

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3697
		ut_print_timestamp(stderr);
3698
		fprintf(stderr,
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3699 3700
"  InnoDB: Error: trying to do i/o to a tablespace which does not exist.\n"
"InnoDB: i/o type %lu, space id %lu, page no. %lu, i/o length %lu bytes\n",
3701 3702
			(ulong) type, (ulong) space_id, (ulong) block_offset,
			(ulong) len);
3703

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3704 3705
		return(DB_TABLESPACE_DELETED);
	}
3706 3707 3708 3709 3710 3711

	ut_ad((mode != OS_AIO_IBUF) || (space->purpose == FIL_TABLESPACE));

	node = UT_LIST_GET_FIRST(space->chain);

	for (;;) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3712 3713 3714 3715 3716 3717 3718
		if (space->id != 0 && node->size == 0) {
			/* We do not know the size of a single-table tablespace
			before we open the file */

			break;
		}

heikki@donna.mysql.fi's avatar
heikki@donna.mysql.fi committed
3719 3720
		if (node == NULL) {
			fprintf(stderr,
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3721 3722
	"InnoDB: Error: trying to access page number %lu in space %lu,\n"
	"InnoDB: space name %s,\n"
heikki@donna.mysql.fi's avatar
heikki@donna.mysql.fi committed
3723 3724
	"InnoDB: which is outside the tablespace bounds.\n"
	"InnoDB: Byte offset %lu, len %lu, i/o type %lu\n", 
3725 3726 3727
 			(ulong) block_offset, (ulong) space_id,
			space->name, (ulong) byte_offset, (ulong) len,
			(ulong) type);
heikki@donna.mysql.fi's avatar
heikki@donna.mysql.fi committed
3728
 			
3729
			ut_error;
heikki@donna.mysql.fi's avatar
heikki@donna.mysql.fi committed
3730
		}
3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743

		if (node->size > block_offset) {
			/* Found! */
			break;
		} else {
			block_offset -= node->size;
			node = UT_LIST_GET_NEXT(chain, node);
		}
	}		
	
	/* Open file if closed */
	fil_node_prepare_for_io(node, system, space);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3744 3745 3746 3747 3748 3749 3750 3751 3752 3753
	/* Check that at least the start offset is within the bounds of a
	single-table tablespace */
	if (space->purpose == FIL_TABLESPACE && space->id != 0
	    && node->size <= block_offset) {

	        fprintf(stderr,
	"InnoDB: Error: trying to access page number %lu in space %lu,\n"
	"InnoDB: space name %s,\n"
	"InnoDB: which is outside the tablespace bounds.\n"
	"InnoDB: Byte offset %lu, len %lu, i/o type %lu\n", 
3754 3755 3756
 			(ulong) block_offset, (ulong) space_id,
			space->name, (ulong) byte_offset, (ulong) len,
			(ulong) type);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3757 3758 3759
 		ut_a(0);
	}

3760 3761 3762 3763 3764 3765
	/* Now we have made the changes in the data structures of system */
	mutex_exit(&(system->mutex));

	/* Calculate the low 32 bits and the high 32 bits of the file offset */

	offset_high = (block_offset >> (32 - UNIV_PAGE_SIZE_SHIFT));
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3766
	offset_low  = ((block_offset << UNIV_PAGE_SIZE_SHIFT) & 0xFFFFFFFFUL)
3767 3768 3769 3770 3771 3772 3773
			+ byte_offset;

	ut_a(node->size - block_offset >=
 		(byte_offset + len + (UNIV_PAGE_SIZE - 1)) / UNIV_PAGE_SIZE);

	/* Do aio */

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3774 3775
	ut_a(byte_offset % OS_FILE_LOG_BLOCK_SIZE == 0);
	ut_a((len % OS_FILE_LOG_BLOCK_SIZE) == 0);
3776

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3777 3778 3779 3780 3781 3782 3783 3784 3785 3786
#ifdef UNIV_HOTBACKUP
	/* In ibbackup do normal i/o, not aio */
	if (type == OS_FILE_READ) {
		ret = os_file_read(node->handle, buf, offset_low, offset_high,
									len);
	} else {
		ret = os_file_write(node->name, node->handle, buf,
					offset_low, offset_high, len);
	}
#else
3787 3788 3789
	/* Queue the aio request */
	ret = os_aio(type, mode | wake_later, node->name, node->handle, buf,
				offset_low, offset_high, len, node, message);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3790
#endif
3791 3792 3793 3794 3795 3796 3797 3798
	ut_a(ret);

	if (mode == OS_AIO_SYNC) {
		/* The i/o operation is already completed when we return from
		os_aio: */
		
		mutex_enter(&(system->mutex));

3799
		fil_node_complete_io(node, system, type);
3800 3801 3802 3803 3804

		mutex_exit(&(system->mutex));

		ut_ad(fil_validate());
	}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3805 3806

	return(DB_SUCCESS);
3807 3808 3809 3810 3811 3812 3813
}

/************************************************************************
Reads data from a space to a buffer. Remember that the possible incomplete
blocks at the end of file are ignored: they are not taken into account when
calculating the byte offset within a space. */

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3814
ulint
3815 3816
fil_read(
/*=====*/
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3817 3818 3819
				/* out: DB_SUCCESS, or DB_TABLESPACE_DELETED
				if we are trying to do i/o on a tablespace
				which does not exist */
3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832
	ibool	sync,		/* in: TRUE if synchronous aio is desired */
	ulint	space_id,	/* in: space id */
	ulint	block_offset,	/* in: offset in number of blocks */
	ulint	byte_offset,	/* in: remainder of offset in bytes; in aio
				this must be divisible by the OS block size */
	ulint	len,		/* in: how many bytes to read; this must not
				cross a file boundary; in aio this must be a
				block size multiple */
	void*	buf,		/* in/out: buffer where to store data read;
				in aio this must be appropriately aligned */
	void*	message)	/* in: message for aio handler if non-sync
				aio used, else ignored */
{
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3833 3834
	return(fil_io(OS_FILE_READ, sync, space_id, block_offset,
					  byte_offset, len, buf, message));
3835 3836 3837 3838 3839 3840 3841
}

/************************************************************************
Writes data to a space from a buffer. Remember that the possible incomplete
blocks at the end of file are ignored: they are not taken into account when
calculating the byte offset within a space. */

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3842
ulint
3843 3844
fil_write(
/*======*/
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3845 3846 3847
				/* out: DB_SUCCESS, or DB_TABLESPACE_DELETED
				if we are trying to do i/o on a tablespace
				which does not exist */
3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860
	ibool	sync,		/* in: TRUE if synchronous aio is desired */
	ulint	space_id,	/* in: space id */
	ulint	block_offset,	/* in: offset in number of blocks */
	ulint	byte_offset,	/* in: remainder of offset in bytes; in aio
				this must be divisible by the OS block size */
	ulint	len,		/* in: how many bytes to write; this must
				not cross a file boundary; in aio this must
				be a block size multiple */
	void*	buf,		/* in: buffer from which to write; in aio
				this must be appropriately aligned */
	void*	message)	/* in: message for aio handler if non-sync
				aio used, else ignored */
{
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3861 3862
	return(fil_io(OS_FILE_WRITE, sync, space_id, block_offset,
					   byte_offset, len, buf, message));
3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876
}

/**************************************************************************
Waits for an aio operation to complete. This function is used to write the
handler for completed requests. The aio array of pending requests is divided
into segments (see os0file.c for more info). The thread specifies which
segment it wants to wait for. */

void
fil_aio_wait(
/*=========*/
	ulint	segment)	/* in: the number of the segment in the aio
				array to wait for */ 
{
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3877
	fil_system_t*	system		= fil_system;
3878 3879 3880
	ibool		ret;		
	fil_node_t*	fil_node;
	void*		message;
3881
	ulint		type;
3882 3883 3884 3885
	
	ut_ad(fil_validate());

	if (os_aio_use_native_aio) {
3886
		srv_set_io_thread_op_info(segment, "native aio handle");
3887
#ifdef WIN_ASYNC_IO
3888 3889
		ret = os_aio_windows_handle(segment, 0, (void**) &fil_node,
					    &message, &type);
3890 3891 3892
#elif defined(POSIX_ASYNC_IO)
		ret = os_aio_posix_handle(segment, &fil_node, &message);
#else
3893
		ret = 0; /* Eliminate compiler warning */
3894
		ut_error;
3895 3896
#endif
	} else {
3897
		srv_set_io_thread_op_info(segment, "simulated aio handle");
3898

3899
		ret = os_aio_simulated_handle(segment, (void**) &fil_node,
3900
	                                               &message, &type);
3901 3902 3903
	}
	
	ut_a(ret);
3904

3905
	srv_set_io_thread_op_info(segment, "complete io for fil node");
3906

3907 3908
	mutex_enter(&(system->mutex));

3909
	fil_node_complete_io(fil_node, fil_system, type);
3910 3911 3912 3913 3914 3915

	mutex_exit(&(system->mutex));

	ut_ad(fil_validate());

	/* Do the i/o handling */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3916 3917 3918 3919
	/* IMPORTANT: since i/o handling for reads will read also the insert
	buffer in tablespace 0, you have to be very careful not to introduce
	deadlocks in the i/o system. We keep tablespace 0 data files always
	open, and use a special i/o thread to serve insert buffer requests. */
3920 3921

	if (buf_pool_is_block(message)) {
3922
		srv_set_io_thread_op_info(segment, "complete io for buf page");
3923 3924
		buf_page_io_complete(message);
	} else {
3925
		srv_set_io_thread_op_info(segment, "complete io for log");
3926 3927 3928 3929 3930
		log_io_complete(message);
	}
}

/**************************************************************************
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3931 3932
Flushes to disk possible writes cached by the OS. If the space does not exist
or is being dropped, does not do anything. */
3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943

void
fil_flush(
/*======*/
	ulint	space_id)	/* in: file space id (this can be a group of
				log files or a tablespace of the database) */
{
	fil_system_t*	system	= fil_system;
	fil_space_t*	space;
	fil_node_t*	node;
	os_file_t	file;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3944
	ib_longlong	old_mod_counter;
3945 3946 3947 3948

	mutex_enter(&(system->mutex));
	
	HASH_SEARCH(hash, system->spaces, space_id, space,
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3949 3950 3951 3952 3953 3954
							space->id == space_id);
	if (!space || space->is_being_deleted) {
		mutex_exit(&(system->mutex));

		return;
	}
3955

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3956 3957
	space->n_pending_flushes++;	/* prevent dropping of the space while
					we are flushing */
3958 3959 3960
	node = UT_LIST_GET_FIRST(space->chain);

	while (node) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3961 3962 3963 3964 3965 3966
		if (node->modification_counter > node->flush_counter) {
			ut_a(node->open);

			/* We want to flush the changes at least up to
			old_mod_counter */
			old_mod_counter = node->modification_counter;
3967

3968 3969 3970 3971 3972
			if (space->purpose == FIL_TABLESPACE) {
				fil_n_pending_tablespace_flushes++;
			} else {
				fil_n_pending_log_flushes++;
			}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3973 3974
#ifdef __WIN__
			if (node->is_raw_disk) {
3975

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995
				goto skip_flush;
			}
#endif
retry:			
			if (node->n_pending_flushes > 0) {
				/* We want to avoid calling os_file_flush() on
				the file twice at the same time, because we do
				not know what bugs OS's may contain in file
				i/o; sleep for a while */

				mutex_exit(&(system->mutex));

				os_thread_sleep(20000);

				mutex_enter(&(system->mutex));

				if (node->flush_counter >= old_mod_counter) {

					goto skip_flush;
				}
3996

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3997 3998 3999 4000 4001 4002 4003 4004
				goto retry;
			}

			ut_a(node->open);
			file = node->handle;
			node->n_pending_flushes++;

			mutex_exit(&(system->mutex));
4005

4006 4007 4008
			/* fprintf(stderr, "Flushing to file %s\n",
				node->name); */

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
4009 4010
			os_file_flush(file);		

4011
			mutex_enter(&(system->mutex));
4012

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
4013 4014 4015 4016 4017 4018
			node->n_pending_flushes--;
skip_flush:
			if (node->flush_counter < old_mod_counter) {
				node->flush_counter = old_mod_counter;
			}

4019 4020 4021 4022 4023
			if (space->purpose == FIL_TABLESPACE) {
				fil_n_pending_tablespace_flushes--;
			} else {
				fil_n_pending_log_flushes--;
			}
4024 4025 4026 4027 4028
		}

		node = UT_LIST_GET_NEXT(chain, node);
	}		

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
4029 4030
	space->n_pending_flushes--;

4031 4032 4033 4034
	mutex_exit(&(system->mutex));
}

/**************************************************************************
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
4035
Flushes to disk the writes in file spaces of the given type possibly cached by
4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051
the OS. */

void
fil_flush_file_spaces(
/*==================*/
	ulint	purpose)	/* in: FIL_TABLESPACE, FIL_LOG */
{
	fil_system_t*	system	= fil_system;
	fil_space_t*	space;

	mutex_enter(&(system->mutex));

	space = UT_LIST_GET_FIRST(system->space_list);

	while (space) {
		if (space->purpose == purpose) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
4052 4053 4054
			space->n_pending_flushes++; /* prevent dropping of the
						    space while we are
						    flushing */
4055 4056 4057 4058 4059 4060
			mutex_exit(&(system->mutex));

			fil_flush(space->id);

			mutex_enter(&(system->mutex));

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
4061 4062
			space->n_pending_flushes--;
		}
4063 4064 4065 4066 4067 4068 4069
		space = UT_LIST_GET_NEXT(space_list, space);
	}
	
	mutex_exit(&(system->mutex));
}

/**********************************************************************
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
4070
Checks the consistency of the tablespace cache. */
4071 4072 4073 4074 4075 4076

ibool
fil_validate(void)
/*==============*/
			/* out: TRUE if ok */
{	
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
4077
	fil_system_t*	system		= fil_system;
4078 4079
	fil_space_t*	space;
	fil_node_t*	fil_node;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
4080
	ulint		n_open		= 0;
4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100
	ulint		i;
	
	mutex_enter(&(system->mutex));

	/* Look for spaces in the hash table */

	for (i = 0; i < hash_get_n_cells(system->spaces); i++) {

		space = HASH_GET_FIRST(system->spaces, i);
	
		while (space != NULL) {
			UT_LIST_VALIDATE(chain, fil_node_t, space->chain); 

			fil_node = UT_LIST_GET_FIRST(space->chain);

			while (fil_node != NULL) {
				if (fil_node->n_pending > 0) {
					ut_a(fil_node->open);
				}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
4101 4102 4103
				if (fil_node->open) {
					n_open++;
				}
4104 4105 4106 4107 4108 4109
				fil_node = UT_LIST_GET_NEXT(chain, fil_node);
			}
			space = HASH_GET_NEXT(hash, space);
		}
	}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
4110
	ut_a(system->n_open == n_open);
4111 4112 4113 4114 4115 4116 4117 4118

	UT_LIST_VALIDATE(LRU, fil_node_t, system->LRU);

	fil_node = UT_LIST_GET_FIRST(system->LRU);

	while (fil_node != NULL) {
		ut_a(fil_node->n_pending == 0);
		ut_a(fil_node->open);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
4119 4120
		ut_a(fil_node->space->purpose == FIL_TABLESPACE);
		ut_a(fil_node->space->id != 0);
4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 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 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187

		fil_node = UT_LIST_GET_NEXT(LRU, fil_node);
	}
	
	mutex_exit(&(system->mutex));

	return(TRUE);
}

/************************************************************************
Returns TRUE if file address is undefined. */
ibool
fil_addr_is_null(
/*=============*/
				/* out: TRUE if undefined */
	fil_addr_t	addr)	/* in: address */
{
	if (addr.page == FIL_NULL) {

		return(TRUE);
	}

	return(FALSE);
}

/************************************************************************
Accessor functions for a file page */

ulint
fil_page_get_prev(byte*	page)
{
	return(mach_read_from_4(page + FIL_PAGE_PREV));
}

ulint
fil_page_get_next(byte*	page)
{
	return(mach_read_from_4(page + FIL_PAGE_NEXT));
}

/*************************************************************************
Sets the file page type. */

void
fil_page_set_type(
/*==============*/
	byte* 	page,	/* in: file page */
	ulint	type)	/* in: type */
{
	ut_ad(page);

	mach_write_to_2(page + FIL_PAGE_TYPE, type);
}	

/*************************************************************************
Gets the file page type. */

ulint
fil_page_get_type(
/*==============*/
			/* out: type; NOTE that if the type has not been
			written to page, the return value not defined */
	byte* 	page)	/* in: file page */
{
	ut_ad(page);

	return(mach_read_from_2(page + FIL_PAGE_TYPE));
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
4188
}