~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/innobase/fil/fil0fil.c

  • Committer: Brian Aker
  • Date: 2010-02-07 01:33:54 UTC
  • Revision ID: brian@gaz-20100207013354-d2pg1n68u5c09pgo
Remove giant include header to its own file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****************************************************************************
 
2
 
 
3
Copyright (c) 1995, 2009, Innobase Oy. All Rights Reserved.
 
4
 
 
5
This program is free software; you can redistribute it and/or modify it under
 
6
the terms of the GNU General Public License as published by the Free Software
 
7
Foundation; version 2 of the License.
 
8
 
 
9
This program is distributed in the hope that it will be useful, but WITHOUT
 
10
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
11
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 
12
 
 
13
You should have received a copy of the GNU General Public License along with
 
14
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 
15
Place, Suite 330, Boston, MA 02111-1307 USA
 
16
 
 
17
*****************************************************************************/
 
18
 
 
19
/**************************************************//**
 
20
@file fil/fil0fil.c
 
21
The tablespace memory cache
 
22
 
 
23
Created 10/25/1995 Heikki Tuuri
 
24
*******************************************************/
 
25
 
 
26
#include "fil0fil.h"
 
27
 
 
28
#include "mem0mem.h"
 
29
#include "hash0hash.h"
 
30
#include "os0file.h"
 
31
#include "mach0data.h"
 
32
#include "buf0buf.h"
 
33
#include "buf0flu.h"
 
34
#include "log0recv.h"
 
35
#include "fsp0fsp.h"
 
36
#include "srv0srv.h"
 
37
#include "srv0start.h"
 
38
#include "mtr0mtr.h"
 
39
#include "mtr0log.h"
 
40
#include "dict0dict.h"
 
41
#include "page0zip.h"
 
42
#ifndef UNIV_HOTBACKUP
 
43
# include "buf0lru.h"
 
44
# include "ibuf0ibuf.h"
 
45
# include "sync0sync.h"
 
46
# include "os0sync.h"
 
47
#else /* !UNIV_HOTBACKUP */
 
48
static ulint srv_data_read, srv_data_written;
 
49
#endif /* !UNIV_HOTBACKUP */
 
50
 
 
51
/*
 
52
                IMPLEMENTATION OF THE TABLESPACE MEMORY CACHE
 
53
                =============================================
 
54
 
 
55
The tablespace cache is responsible for providing fast read/write access to
 
56
tablespaces and logs of the database. File creation and deletion is done
 
57
in other modules which know more of the logic of the operation, however.
 
58
 
 
59
A tablespace consists of a chain of files. The size of the files does not
 
60
have to be divisible by the database block size, because we may just leave
 
61
the last incomplete block unused. When a new file is appended to the
 
62
tablespace, the maximum size of the file is also specified. At the moment,
 
63
we think that it is best to extend the file to its maximum size already at
 
64
the creation of the file, because then we can avoid dynamically extending
 
65
the file when more space is needed for the tablespace.
 
66
 
 
67
A block's position in the tablespace is specified with a 32-bit unsigned
 
68
integer. The files in the chain are thought to be catenated, and the block
 
69
corresponding to an address n is the nth block in the catenated file (where
 
70
the first block is named the 0th block, and the incomplete block fragments
 
71
at the end of files are not taken into account). A tablespace can be extended
 
72
by appending a new file at the end of the chain.
 
73
 
 
74
Our tablespace concept is similar to the one of Oracle.
 
75
 
 
76
To acquire more speed in disk transfers, a technique called disk striping is
 
77
sometimes used. This means that logical block addresses are divided in a
 
78
round-robin fashion across several disks. Windows NT supports disk striping,
 
79
so there we do not need to support it in the database. Disk striping is
 
80
implemented in hardware in RAID disks. We conclude that it is not necessary
 
81
to implement it in the database. Oracle 7 does not support disk striping,
 
82
either.
 
83
 
 
84
Another trick used at some database sites is replacing tablespace files by
 
85
raw disks, that is, the whole physical disk drive, or a partition of it, is
 
86
opened as a single file, and it is accessed through byte offsets calculated
 
87
from the start of the disk or the partition. This is recommended in some
 
88
books on database tuning to achieve more speed in i/o. Using raw disk
 
89
certainly prevents the OS from fragmenting disk space, but it is not clear
 
90
if it really adds speed. We measured on the Pentium 100 MHz + NT + NTFS file
 
91
system + EIDE Conner disk only a negligible difference in speed when reading
 
92
from a file, versus reading from a raw disk.
 
93
 
 
94
To have fast access to a tablespace or a log file, we put the data structures
 
95
to a hash table. Each tablespace and log file is given an unique 32-bit
 
96
identifier.
 
97
 
 
98
Some operating systems do not support many open files at the same time,
 
99
though NT seems to tolerate at least 900 open files. Therefore, we put the
 
100
open files in an LRU-list. If we need to open another file, we may close the
 
101
file at the end of the LRU-list. When an i/o-operation is pending on a file,
 
102
the file cannot be closed. We take the file nodes with pending i/o-operations
 
103
out of the LRU-list and keep a count of pending operations. When an operation
 
104
completes, we decrement the count and return the file node to the LRU-list if
 
105
the count drops to zero. */
 
106
 
 
107
/** When mysqld is run, the default directory "." is the mysqld datadir,
 
108
but in the MySQL Embedded Server Library and ibbackup it is not the default
 
109
directory, and we must set the base file path explicitly */
 
110
UNIV_INTERN const char* fil_path_to_mysql_datadir       = ".";
 
111
 
 
112
/** The number of fsyncs done to the log */
 
113
UNIV_INTERN ulint       fil_n_log_flushes                       = 0;
 
114
 
 
115
/** Number of pending redo log flushes */
 
116
UNIV_INTERN ulint       fil_n_pending_log_flushes               = 0;
 
117
/** Number of pending tablespace flushes */
 
118
UNIV_INTERN ulint       fil_n_pending_tablespace_flushes        = 0;
 
119
 
 
120
/** The null file address */
 
121
UNIV_INTERN fil_addr_t  fil_addr_null = {FIL_NULL, 0};
 
122
 
 
123
/** File node of a tablespace or the log data space */
 
124
struct fil_node_struct {
 
125
        fil_space_t*    space;  /*!< backpointer to the space where this node
 
126
                                belongs */
 
127
        char*           name;   /*!< path to the file */
 
128
        ibool           open;   /*!< TRUE if file open */
 
129
        os_file_t       handle; /*!< OS handle to the file, if file open */
 
130
        ibool           is_raw_disk;/*!< TRUE if the 'file' is actually a raw
 
131
                                device or a raw disk partition */
 
132
        ulint           size;   /*!< size of the file in database pages, 0 if
 
133
                                not known yet; the possible last incomplete
 
134
                                megabyte may be ignored if space == 0 */
 
135
        ulint           n_pending;
 
136
                                /*!< count of pending i/o's on this file;
 
137
                                closing of the file is not allowed if
 
138
                                this is > 0 */
 
139
        ulint           n_pending_flushes;
 
140
                                /*!< count of pending flushes on this file;
 
141
                                closing of the file is not allowed if
 
142
                                this is > 0 */
 
143
        ib_int64_t      modification_counter;/*!< when we write to the file we
 
144
                                increment this by one */
 
145
        ib_int64_t      flush_counter;/*!< up to what
 
146
                                modification_counter value we have
 
147
                                flushed the modifications to disk */
 
148
        UT_LIST_NODE_T(fil_node_t) chain;
 
149
                                /*!< link field for the file chain */
 
150
        UT_LIST_NODE_T(fil_node_t) LRU;
 
151
                                /*!< link field for the LRU list */
 
152
        ulint           magic_n;/*!< FIL_NODE_MAGIC_N */
 
153
};
 
154
 
 
155
/** Value of fil_node_struct::magic_n */
 
156
#define FIL_NODE_MAGIC_N        89389
 
157
 
 
158
/** Tablespace or log data space: let us call them by a common name space */
 
159
struct fil_space_struct {
 
160
        char*           name;   /*!< space name = the path to the first file in
 
161
                                it */
 
162
        ulint           id;     /*!< space id */
 
163
        ib_int64_t      tablespace_version;
 
164
                                /*!< in DISCARD/IMPORT this timestamp
 
165
                                is used to check if we should ignore
 
166
                                an insert buffer merge request for a
 
167
                                page because it actually was for the
 
168
                                previous incarnation of the space */
 
169
        ibool           mark;   /*!< this is set to TRUE at database startup if
 
170
                                the space corresponds to a table in the InnoDB
 
171
                                data dictionary; so we can print a warning of
 
172
                                orphaned tablespaces */
 
173
        ibool           stop_ios;/*!< TRUE if we want to rename the
 
174
                                .ibd file of tablespace and want to
 
175
                                stop temporarily posting of new i/o
 
176
                                requests on the file */
 
177
        ibool           stop_ibuf_merges;
 
178
                                /*!< we set this TRUE when we start
 
179
                                deleting a single-table tablespace */
 
180
        ibool           is_being_deleted;
 
181
                                /*!< this is set to TRUE when we start
 
182
                                deleting a single-table tablespace and its
 
183
                                file; when this flag is set no further i/o
 
184
                                or flush requests can be placed on this space,
 
185
                                though there may be such requests still being
 
186
                                processed on this space */
 
187
        ulint           purpose;/*!< FIL_TABLESPACE, FIL_LOG, or
 
188
                                FIL_ARCH_LOG */
 
189
        UT_LIST_BASE_NODE_T(fil_node_t) chain;
 
190
                                /*!< base node for the file chain */
 
191
        ulint           size;   /*!< space size in pages; 0 if a single-table
 
192
                                tablespace whose size we do not know yet;
 
193
                                last incomplete megabytes in data files may be
 
194
                                ignored if space == 0 */
 
195
        ulint           flags;  /*!< compressed page size and file format, or 0 */
 
196
        ulint           n_reserved_extents;
 
197
                                /*!< number of reserved free extents for
 
198
                                ongoing operations like B-tree page split */
 
199
        ulint           n_pending_flushes; /*!< this is positive when flushing
 
200
                                the tablespace to disk; dropping of the
 
201
                                tablespace is forbidden if this is positive */
 
202
        ulint           n_pending_ibuf_merges;/*!< this is positive
 
203
                                when merging insert buffer entries to
 
204
                                a page so that we may need to access
 
205
                                the ibuf bitmap page in the
 
206
                                tablespade: dropping of the tablespace
 
207
                                is forbidden if this is positive */
 
208
        hash_node_t     hash;   /*!< hash chain node */
 
209
        hash_node_t     name_hash;/*!< hash chain the name_hash table */
 
210
#ifndef UNIV_HOTBACKUP
 
211
        rw_lock_t       latch;  /*!< latch protecting the file space storage
 
212
                                allocation */
 
213
#endif /* !UNIV_HOTBACKUP */
 
214
        UT_LIST_NODE_T(fil_space_t) unflushed_spaces;
 
215
                                /*!< list of spaces with at least one unflushed
 
216
                                file we have written to */
 
217
        ibool           is_in_unflushed_spaces; /*!< TRUE if this space is
 
218
                                currently in unflushed_spaces */
 
219
        UT_LIST_NODE_T(fil_space_t) space_list;
 
220
                                /*!< list of all spaces */
 
221
        ulint           magic_n;/*!< FIL_SPACE_MAGIC_N */
 
222
};
 
223
 
 
224
/** Value of fil_space_struct::magic_n */
 
225
#define FIL_SPACE_MAGIC_N       89472
 
226
 
 
227
/** The tablespace memory cache */
 
228
typedef struct fil_system_struct        fil_system_t;
 
229
 
 
230
/** The tablespace memory cache; also the totality of logs (the log
 
231
data space) is stored here; below we talk about tablespaces, but also
 
232
the ib_logfiles form a 'space' and it is handled here */
 
233
 
 
234
struct fil_system_struct {
 
235
#ifndef UNIV_HOTBACKUP
 
236
        mutex_t         mutex;          /*!< The mutex protecting the cache */
 
237
#endif /* !UNIV_HOTBACKUP */
 
238
        hash_table_t*   spaces;         /*!< The hash table of spaces in the
 
239
                                        system; they are hashed on the space
 
240
                                        id */
 
241
        hash_table_t*   name_hash;      /*!< hash table based on the space
 
242
                                        name */
 
243
        UT_LIST_BASE_NODE_T(fil_node_t) LRU;
 
244
                                        /*!< base node for the LRU list of the
 
245
                                        most recently used open files with no
 
246
                                        pending i/o's; if we start an i/o on
 
247
                                        the file, we first remove it from this
 
248
                                        list, and return it to the start of
 
249
                                        the list when the i/o ends;
 
250
                                        log files and the system tablespace are
 
251
                                        not put to this list: they are opened
 
252
                                        after the startup, and kept open until
 
253
                                        shutdown */
 
254
        UT_LIST_BASE_NODE_T(fil_space_t) unflushed_spaces;
 
255
                                        /*!< base node for the list of those
 
256
                                        tablespaces whose files contain
 
257
                                        unflushed writes; those spaces have
 
258
                                        at least one file node where
 
259
                                        modification_counter > flush_counter */
 
260
        ulint           n_open;         /*!< number of files currently open */
 
261
        ulint           max_n_open;     /*!< n_open is not allowed to exceed
 
262
                                        this */
 
263
        ib_int64_t      modification_counter;/*!< when we write to a file we
 
264
                                        increment this by one */
 
265
        ulint           max_assigned_id;/*!< maximum space id in the existing
 
266
                                        tables, or assigned during the time
 
267
                                        mysqld has been up; at an InnoDB
 
268
                                        startup we scan the data dictionary
 
269
                                        and set here the maximum of the
 
270
                                        space id's of the tables there */
 
271
        ib_int64_t      tablespace_version;
 
272
                                        /*!< a counter which is incremented for
 
273
                                        every space object memory creation;
 
274
                                        every space mem object gets a
 
275
                                        'timestamp' from this; in DISCARD/
 
276
                                        IMPORT this is used to check if we
 
277
                                        should ignore an insert buffer merge
 
278
                                        request */
 
279
        UT_LIST_BASE_NODE_T(fil_space_t) space_list;
 
280
                                        /*!< list of all file spaces */
 
281
};
 
282
 
 
283
/** The tablespace memory cache. This variable is NULL before the module is
 
284
initialized. */
 
285
static fil_system_t*    fil_system      = NULL;
 
286
 
 
287
 
 
288
/********************************************************************//**
 
289
NOTE: you must call fil_mutex_enter_and_prepare_for_io() first!
 
290
 
 
291
Prepares a file node for i/o. Opens the file if it is closed. Updates the
 
292
pending i/o's field in the node and the system appropriately. Takes the node
 
293
off the LRU list if it is in the LRU list. The caller must hold the fil_sys
 
294
mutex. */
 
295
static
 
296
void
 
297
fil_node_prepare_for_io(
 
298
/*====================*/
 
299
        fil_node_t*     node,   /*!< in: file node */
 
300
        fil_system_t*   system, /*!< in: tablespace memory cache */
 
301
        fil_space_t*    space); /*!< in: space */
 
302
/********************************************************************//**
 
303
Updates the data structures when an i/o operation finishes. Updates the
 
304
pending i/o's field in the node appropriately. */
 
305
static
 
306
void
 
307
fil_node_complete_io(
 
308
/*=================*/
 
309
        fil_node_t*     node,   /*!< in: file node */
 
310
        fil_system_t*   system, /*!< in: tablespace memory cache */
 
311
        ulint           type);  /*!< in: OS_FILE_WRITE or OS_FILE_READ; marks
 
312
                                the node as modified if
 
313
                                type == OS_FILE_WRITE */
 
314
/*******************************************************************//**
 
315
Checks if a single-table tablespace for a given table name exists in the
 
316
tablespace memory cache.
 
317
@return space id, ULINT_UNDEFINED if not found */
 
318
static
 
319
ulint
 
320
fil_get_space_id_for_table(
 
321
/*=======================*/
 
322
        const char*     name);  /*!< in: table name in the standard
 
323
                                'databasename/tablename' format */
 
324
/********************************************************************//**
 
325
Reads data from a space to a buffer. Remember that the possible incomplete
 
326
blocks at the end of file are ignored: they are not taken into account when
 
327
calculating the byte offset within a space.
 
328
@return DB_SUCCESS, or DB_TABLESPACE_DELETED if we are trying to do
 
329
i/o on a tablespace which does not exist */
 
330
UNIV_INLINE
 
331
ulint
 
332
fil_read(
 
333
/*=====*/
 
334
        ibool   sync,           /*!< in: TRUE if synchronous aio is desired */
 
335
        ulint   space_id,       /*!< in: space id */
 
336
        ulint   zip_size,       /*!< in: compressed page size in bytes;
 
337
                                0 for uncompressed pages */
 
338
        ulint   block_offset,   /*!< in: offset in number of blocks */
 
339
        ulint   byte_offset,    /*!< in: remainder of offset in bytes; in aio
 
340
                                this must be divisible by the OS block size */
 
341
        ulint   len,            /*!< in: how many bytes to read; this must not
 
342
                                cross a file boundary; in aio this must be a
 
343
                                block size multiple */
 
344
        void*   buf,            /*!< in/out: buffer where to store data read;
 
345
                                in aio this must be appropriately aligned */
 
346
        void*   message)        /*!< in: message for aio handler if non-sync
 
347
                                aio used, else ignored */
 
348
{
 
349
        return(fil_io(OS_FILE_READ, sync, space_id, zip_size, block_offset,
 
350
                                          byte_offset, len, buf, message));
 
351
}
 
352
 
 
353
/********************************************************************//**
 
354
Writes data to a space from a buffer. Remember that the possible incomplete
 
355
blocks at the end of file are ignored: they are not taken into account when
 
356
calculating the byte offset within a space.
 
357
@return DB_SUCCESS, or DB_TABLESPACE_DELETED if we are trying to do
 
358
i/o on a tablespace which does not exist */
 
359
UNIV_INLINE
 
360
ulint
 
361
fil_write(
 
362
/*======*/
 
363
        ibool   sync,           /*!< in: TRUE if synchronous aio is desired */
 
364
        ulint   space_id,       /*!< in: space id */
 
365
        ulint   zip_size,       /*!< in: compressed page size in bytes;
 
366
                                0 for uncompressed pages */
 
367
        ulint   block_offset,   /*!< in: offset in number of blocks */
 
368
        ulint   byte_offset,    /*!< in: remainder of offset in bytes; in aio
 
369
                                this must be divisible by the OS block size */
 
370
        ulint   len,            /*!< in: how many bytes to write; this must
 
371
                                not cross a file boundary; in aio this must
 
372
                                be a block size multiple */
 
373
        void*   buf,            /*!< in: buffer from which to write; in aio
 
374
                                this must be appropriately aligned */
 
375
        void*   message)        /*!< in: message for aio handler if non-sync
 
376
                                aio used, else ignored */
 
377
{
 
378
        return(fil_io(OS_FILE_WRITE, sync, space_id, zip_size, block_offset,
 
379
                                           byte_offset, len, buf, message));
 
380
}
 
381
 
 
382
/*******************************************************************//**
 
383
Returns the table space by a given id, NULL if not found. */
 
384
UNIV_INLINE
 
385
fil_space_t*
 
386
fil_space_get_by_id(
 
387
/*================*/
 
388
        ulint   id)     /*!< in: space id */
 
389
{
 
390
        fil_space_t*    space;
 
391
 
 
392
        ut_ad(mutex_own(&fil_system->mutex));
 
393
 
 
394
        HASH_SEARCH(hash, fil_system->spaces, id,
 
395
                    fil_space_t*, space,
 
396
                    ut_ad(space->magic_n == FIL_SPACE_MAGIC_N),
 
397
                    space->id == id);
 
398
 
 
399
        return(space);
 
400
}
 
401
 
 
402
/*******************************************************************//**
 
403
Returns the table space by a given name, NULL if not found. */
 
404
UNIV_INLINE
 
405
fil_space_t*
 
406
fil_space_get_by_name(
 
407
/*==================*/
 
408
        const char*     name)   /*!< in: space name */
 
409
{
 
410
        fil_space_t*    space;
 
411
        ulint           fold;
 
412
 
 
413
        ut_ad(mutex_own(&fil_system->mutex));
 
414
 
 
415
        fold = ut_fold_string(name);
 
416
 
 
417
        HASH_SEARCH(name_hash, fil_system->name_hash, fold,
 
418
                    fil_space_t*, space,
 
419
                    ut_ad(space->magic_n == FIL_SPACE_MAGIC_N),
 
420
                    !strcmp(name, space->name));
 
421
 
 
422
        return(space);
 
423
}
 
424
 
 
425
#ifndef UNIV_HOTBACKUP
 
426
/*******************************************************************//**
 
427
Returns the version number of a tablespace, -1 if not found.
 
428
@return version number, -1 if the tablespace does not exist in the
 
429
memory cache */
 
430
UNIV_INTERN
 
431
ib_int64_t
 
432
fil_space_get_version(
 
433
/*==================*/
 
434
        ulint   id)     /*!< in: space id */
 
435
{
 
436
        fil_space_t*    space;
 
437
        ib_int64_t      version         = -1;
 
438
 
 
439
        ut_ad(fil_system);
 
440
 
 
441
        mutex_enter(&fil_system->mutex);
 
442
 
 
443
        space = fil_space_get_by_id(id);
 
444
 
 
445
        if (space) {
 
446
                version = space->tablespace_version;
 
447
        }
 
448
 
 
449
        mutex_exit(&fil_system->mutex);
 
450
 
 
451
        return(version);
 
452
}
 
453
 
 
454
/*******************************************************************//**
 
455
Returns the latch of a file space.
 
456
@return latch protecting storage allocation */
 
457
UNIV_INTERN
 
458
rw_lock_t*
 
459
fil_space_get_latch(
 
460
/*================*/
 
461
        ulint   id,     /*!< in: space id */
 
462
        ulint*  flags)  /*!< out: tablespace flags */
 
463
{
 
464
        fil_space_t*    space;
 
465
 
 
466
        ut_ad(fil_system);
 
467
 
 
468
        mutex_enter(&fil_system->mutex);
 
469
 
 
470
        space = fil_space_get_by_id(id);
 
471
 
 
472
        ut_a(space);
 
473
 
 
474
        if (flags) {
 
475
                *flags = space->flags;
 
476
        }
 
477
 
 
478
        mutex_exit(&fil_system->mutex);
 
479
 
 
480
        return(&(space->latch));
 
481
}
 
482
 
 
483
/*******************************************************************//**
 
484
Returns the type of a file space.
 
485
@return FIL_TABLESPACE or FIL_LOG */
 
486
UNIV_INTERN
 
487
ulint
 
488
fil_space_get_type(
 
489
/*===============*/
 
490
        ulint   id)     /*!< in: space id */
 
491
{
 
492
        fil_space_t*    space;
 
493
 
 
494
        ut_ad(fil_system);
 
495
 
 
496
        mutex_enter(&fil_system->mutex);
 
497
 
 
498
        space = fil_space_get_by_id(id);
 
499
 
 
500
        ut_a(space);
 
501
 
 
502
        mutex_exit(&fil_system->mutex);
 
503
 
 
504
        return(space->purpose);
 
505
}
 
506
#endif /* !UNIV_HOTBACKUP */
 
507
 
 
508
/**********************************************************************//**
 
509
Checks if all the file nodes in a space are flushed. The caller must hold
 
510
the fil_system mutex.
 
511
@return TRUE if all are flushed */
 
512
static
 
513
ibool
 
514
fil_space_is_flushed(
 
515
/*=================*/
 
516
        fil_space_t*    space)  /*!< in: space */
 
517
{
 
518
        fil_node_t*     node;
 
519
 
 
520
        ut_ad(mutex_own(&fil_system->mutex));
 
521
 
 
522
        node = UT_LIST_GET_FIRST(space->chain);
 
523
 
 
524
        while (node) {
 
525
                if (node->modification_counter > node->flush_counter) {
 
526
 
 
527
                        return(FALSE);
 
528
                }
 
529
 
 
530
                node = UT_LIST_GET_NEXT(chain, node);
 
531
        }
 
532
 
 
533
        return(TRUE);
 
534
}
 
535
 
 
536
/*******************************************************************//**
 
537
Appends a new file to the chain of files of a space. File must be closed. */
 
538
UNIV_INTERN
 
539
void
 
540
fil_node_create(
 
541
/*============*/
 
542
        const char*     name,   /*!< in: file name (file must be closed) */
 
543
        ulint           size,   /*!< in: file size in database blocks, rounded
 
544
                                downwards to an integer */
 
545
        ulint           id,     /*!< in: space id where to append */
 
546
        ibool           is_raw) /*!< in: TRUE if a raw device or
 
547
                                a raw disk partition */
 
548
{
 
549
        fil_node_t*     node;
 
550
        fil_space_t*    space;
 
551
 
 
552
        ut_a(fil_system);
 
553
        ut_a(name);
 
554
 
 
555
        mutex_enter(&fil_system->mutex);
 
556
 
 
557
        node = mem_alloc(sizeof(fil_node_t));
 
558
 
 
559
        node->name = mem_strdup(name);
 
560
        node->open = FALSE;
 
561
 
 
562
        ut_a(!is_raw || srv_start_raw_disk_in_use);
 
563
 
 
564
        node->is_raw_disk = is_raw;
 
565
        node->size = size;
 
566
        node->magic_n = FIL_NODE_MAGIC_N;
 
567
        node->n_pending = 0;
 
568
        node->n_pending_flushes = 0;
 
569
 
 
570
        node->modification_counter = 0;
 
571
        node->flush_counter = 0;
 
572
 
 
573
        space = fil_space_get_by_id(id);
 
574
 
 
575
        if (!space) {
 
576
                ut_print_timestamp(stderr);
 
577
                fprintf(stderr,
 
578
                        "  InnoDB: Error: Could not find tablespace %lu for\n"
 
579
                        "InnoDB: file ", (ulong) id);
 
580
                ut_print_filename(stderr, name);
 
581
                fputs(" in the tablespace memory cache.\n", stderr);
 
582
                mem_free(node->name);
 
583
 
 
584
                mem_free(node);
 
585
 
 
586
                mutex_exit(&fil_system->mutex);
 
587
 
 
588
                return;
 
589
        }
 
590
 
 
591
        space->size += size;
 
592
 
 
593
        node->space = space;
 
594
 
 
595
        UT_LIST_ADD_LAST(chain, space->chain, node);
 
596
 
 
597
        mutex_exit(&fil_system->mutex);
 
598
}
 
599
 
 
600
/********************************************************************//**
 
601
Opens a the file of a node of a tablespace. The caller must own the fil_system
 
602
mutex. */
 
603
static
 
604
void
 
605
fil_node_open_file(
 
606
/*===============*/
 
607
        fil_node_t*     node,   /*!< in: file node */
 
608
        fil_system_t*   system, /*!< in: tablespace memory cache */
 
609
        fil_space_t*    space)  /*!< in: space */
 
610
{
 
611
        ib_int64_t      size_bytes;
 
612
        ulint           size_low;
 
613
        ulint           size_high;
 
614
        ibool           ret;
 
615
        ibool           success;
 
616
#ifndef UNIV_HOTBACKUP
 
617
        byte*           buf2;
 
618
        byte*           page;
 
619
        ulint           space_id;
 
620
        ulint           flags;
 
621
#endif /* !UNIV_HOTBACKUP */
 
622
 
 
623
        ut_ad(mutex_own(&(system->mutex)));
 
624
        ut_a(node->n_pending == 0);
 
625
        ut_a(node->open == FALSE);
 
626
 
 
627
        if (node->size == 0) {
 
628
                /* It must be a single-table tablespace and we do not know the
 
629
                size of the file yet. First we open the file in the normal
 
630
                mode, no async I/O here, for simplicity. Then do some checks,
 
631
                and close the file again.
 
632
                NOTE that we could not use the simple file read function
 
633
                os_file_read() in Windows to read from a file opened for
 
634
                async I/O! */
 
635
 
 
636
                node->handle = os_file_create_simple_no_error_handling(
 
637
                        node->name, OS_FILE_OPEN, OS_FILE_READ_ONLY, &success);
 
638
                if (!success) {
 
639
                        /* The following call prints an error message */
 
640
                        os_file_get_last_error(TRUE);
 
641
 
 
642
                        ut_print_timestamp(stderr);
 
643
 
 
644
                        fprintf(stderr,
 
645
                                "  InnoDB: Fatal error: cannot open %s\n."
 
646
                                "InnoDB: Have you deleted .ibd files"
 
647
                                " under a running mysqld server?\n",
 
648
                                node->name);
 
649
                        ut_a(0);
 
650
                }
 
651
 
 
652
                os_file_get_size(node->handle, &size_low, &size_high);
 
653
 
 
654
                size_bytes = (((ib_int64_t)size_high) << 32)
 
655
                        + (ib_int64_t)size_low;
 
656
#ifdef UNIV_HOTBACKUP
 
657
                node->size = (ulint) (size_bytes / UNIV_PAGE_SIZE);
 
658
                /* TODO: adjust to zip_size, like below? */
 
659
#else
 
660
                ut_a(space->purpose != FIL_LOG);
 
661
                ut_a(space->id != 0);
 
662
 
 
663
                if (size_bytes < FIL_IBD_FILE_INITIAL_SIZE * UNIV_PAGE_SIZE) {
 
664
                        fprintf(stderr,
 
665
                                "InnoDB: Error: the size of single-table"
 
666
                                " tablespace file %s\n"
 
667
                                "InnoDB: is only %lu %lu,"
 
668
                                " should be at least %lu!\n",
 
669
                                node->name,
 
670
                                (ulong) size_high,
 
671
                                (ulong) size_low,
 
672
                                (ulong) (FIL_IBD_FILE_INITIAL_SIZE
 
673
                                         * UNIV_PAGE_SIZE));
 
674
 
 
675
                        ut_a(0);
 
676
                }
 
677
 
 
678
                /* Read the first page of the tablespace */
 
679
 
 
680
                buf2 = ut_malloc(2 * UNIV_PAGE_SIZE);
 
681
                /* Align the memory for file i/o if we might have O_DIRECT
 
682
                set */
 
683
                page = ut_align(buf2, UNIV_PAGE_SIZE);
 
684
 
 
685
                success = os_file_read(node->handle, page, 0, 0,
 
686
                                       UNIV_PAGE_SIZE);
 
687
                space_id = fsp_header_get_space_id(page);
 
688
                flags = fsp_header_get_flags(page);
 
689
 
 
690
                ut_free(buf2);
 
691
 
 
692
                /* Close the file now that we have read the space id from it */
 
693
 
 
694
                os_file_close(node->handle);
 
695
 
 
696
                if (UNIV_UNLIKELY(space_id != space->id)) {
 
697
                        fprintf(stderr,
 
698
                                "InnoDB: Error: tablespace id is %lu"
 
699
                                " in the data dictionary\n"
 
700
                                "InnoDB: but in file %s it is %lu!\n",
 
701
                                space->id, node->name, space_id);
 
702
 
 
703
                        ut_error;
 
704
                }
 
705
 
 
706
                if (UNIV_UNLIKELY(space_id == ULINT_UNDEFINED
 
707
                                  || space_id == 0)) {
 
708
                        fprintf(stderr,
 
709
                                "InnoDB: Error: tablespace id %lu"
 
710
                                " in file %s is not sensible\n",
 
711
                                (ulong) space_id, node->name);
 
712
 
 
713
                        ut_error;
 
714
                }
 
715
 
 
716
                if (UNIV_UNLIKELY(space->flags != flags)) {
 
717
                        fprintf(stderr,
 
718
                                "InnoDB: Error: table flags are %lx"
 
719
                                " in the data dictionary\n"
 
720
                                "InnoDB: but the flags in file %s are %lx!\n",
 
721
                                space->flags, node->name, flags);
 
722
 
 
723
                        ut_error;
 
724
                }
 
725
 
 
726
                if (size_bytes >= 1024 * 1024) {
 
727
                        /* Truncate the size to whole megabytes. */
 
728
                        size_bytes = ut_2pow_round(size_bytes, 1024 * 1024);
 
729
                }
 
730
 
 
731
                if (!(flags & DICT_TF_ZSSIZE_MASK)) {
 
732
                        node->size = (ulint) (size_bytes / UNIV_PAGE_SIZE);
 
733
                } else {
 
734
                        node->size = (ulint)
 
735
                                (size_bytes
 
736
                                 / dict_table_flags_to_zip_size(flags));
 
737
                }
 
738
#endif
 
739
                space->size += node->size;
 
740
        }
 
741
 
 
742
        /* printf("Opening file %s\n", node->name); */
 
743
 
 
744
        /* Open the file for reading and writing, in Windows normally in the
 
745
        unbuffered async I/O mode, though global variables may make
 
746
        os_file_create() to fall back to the normal file I/O mode. */
 
747
 
 
748
        if (space->purpose == FIL_LOG) {
 
749
                node->handle = os_file_create(node->name, OS_FILE_OPEN,
 
750
                                              OS_FILE_AIO, OS_LOG_FILE, &ret);
 
751
        } else if (node->is_raw_disk) {
 
752
                node->handle = os_file_create(node->name,
 
753
                                              OS_FILE_OPEN_RAW,
 
754
                                              OS_FILE_AIO, OS_DATA_FILE, &ret);
 
755
        } else {
 
756
                node->handle = os_file_create(node->name, OS_FILE_OPEN,
 
757
                                              OS_FILE_AIO, OS_DATA_FILE, &ret);
 
758
        }
 
759
 
 
760
        ut_a(ret);
 
761
 
 
762
        node->open = TRUE;
 
763
 
 
764
        system->n_open++;
 
765
 
 
766
        if (space->purpose == FIL_TABLESPACE && space->id != 0) {
 
767
                /* Put the node to the LRU list */
 
768
                UT_LIST_ADD_FIRST(LRU, system->LRU, node);
 
769
        }
 
770
}
 
771
 
 
772
/**********************************************************************//**
 
773
Closes a file. */
 
774
static
 
775
void
 
776
fil_node_close_file(
 
777
/*================*/
 
778
        fil_node_t*     node,   /*!< in: file node */
 
779
        fil_system_t*   system) /*!< in: tablespace memory cache */
 
780
{
 
781
        ibool   ret;
 
782
 
 
783
        ut_ad(node && system);
 
784
        ut_ad(mutex_own(&(system->mutex)));
 
785
        ut_a(node->open);
 
786
        ut_a(node->n_pending == 0);
 
787
        ut_a(node->n_pending_flushes == 0);
 
788
        ut_a(node->modification_counter == node->flush_counter);
 
789
 
 
790
        ret = os_file_close(node->handle);
 
791
        ut_a(ret);
 
792
 
 
793
        /* printf("Closing file %s\n", node->name); */
 
794
 
 
795
        node->open = FALSE;
 
796
        ut_a(system->n_open > 0);
 
797
        system->n_open--;
 
798
 
 
799
        if (node->space->purpose == FIL_TABLESPACE && node->space->id != 0) {
 
800
                ut_a(UT_LIST_GET_LEN(system->LRU) > 0);
 
801
 
 
802
                /* The node is in the LRU list, remove it */
 
803
                UT_LIST_REMOVE(LRU, system->LRU, node);
 
804
        }
 
805
}
 
806
 
 
807
/********************************************************************//**
 
808
Tries to close a file in the LRU list. The caller must hold the fil_sys
 
809
mutex.
 
810
@return TRUE if success, FALSE if should retry later; since i/o's
 
811
generally complete in < 100 ms, and as InnoDB writes at most 128 pages
 
812
from the buffer pool in a batch, and then immediately flushes the
 
813
files, there is a good chance that the next time we find a suitable
 
814
node from the LRU list */
 
815
static
 
816
ibool
 
817
fil_try_to_close_file_in_LRU(
 
818
/*=========================*/
 
819
        ibool   print_info)     /*!< in: if TRUE, prints information why it
 
820
                                cannot close a file */
 
821
{
 
822
        fil_node_t*     node;
 
823
 
 
824
        ut_ad(mutex_own(&fil_system->mutex));
 
825
 
 
826
        node = UT_LIST_GET_LAST(fil_system->LRU);
 
827
 
 
828
        if (print_info) {
 
829
                fprintf(stderr,
 
830
                        "InnoDB: fil_sys open file LRU len %lu\n",
 
831
                        (ulong) UT_LIST_GET_LEN(fil_system->LRU));
 
832
        }
 
833
 
 
834
        while (node != NULL) {
 
835
                if (node->modification_counter == node->flush_counter
 
836
                    && node->n_pending_flushes == 0) {
 
837
 
 
838
                        fil_node_close_file(node, fil_system);
 
839
 
 
840
                        return(TRUE);
 
841
                }
 
842
 
 
843
                if (print_info && node->n_pending_flushes > 0) {
 
844
                        fputs("InnoDB: cannot close file ", stderr);
 
845
                        ut_print_filename(stderr, node->name);
 
846
                        fprintf(stderr, ", because n_pending_flushes %lu\n",
 
847
                                (ulong) node->n_pending_flushes);
 
848
                }
 
849
 
 
850
                if (print_info
 
851
                    && node->modification_counter != node->flush_counter) {
 
852
                        fputs("InnoDB: cannot close file ", stderr);
 
853
                        ut_print_filename(stderr, node->name);
 
854
                        fprintf(stderr,
 
855
                                ", because mod_count %ld != fl_count %ld\n",
 
856
                                (long) node->modification_counter,
 
857
                                (long) node->flush_counter);
 
858
                }
 
859
 
 
860
                node = UT_LIST_GET_PREV(LRU, node);
 
861
        }
 
862
 
 
863
        return(FALSE);
 
864
}
 
865
 
 
866
/*******************************************************************//**
 
867
Reserves the fil_system mutex and tries to make sure we can open at least one
 
868
file while holding it. This should be called before calling
 
869
fil_node_prepare_for_io(), because that function may need to open a file. */
 
870
static
 
871
void
 
872
fil_mutex_enter_and_prepare_for_io(
 
873
/*===============================*/
 
874
        ulint   space_id)       /*!< in: space id */
 
875
{
 
876
        fil_space_t*    space;
 
877
        ibool           success;
 
878
        ibool           print_info      = FALSE;
 
879
        ulint           count           = 0;
 
880
        ulint           count2          = 0;
 
881
 
 
882
retry:
 
883
        mutex_enter(&fil_system->mutex);
 
884
 
 
885
        if (space_id == 0 || space_id >= SRV_LOG_SPACE_FIRST_ID) {
 
886
                /* We keep log files and system tablespace files always open;
 
887
                this is important in preventing deadlocks in this module, as
 
888
                a page read completion often performs another read from the
 
889
                insert buffer. The insert buffer is in tablespace 0, and we
 
890
                cannot end up waiting in this function. */
 
891
 
 
892
                return;
 
893
        }
 
894
 
 
895
        if (fil_system->n_open < fil_system->max_n_open) {
 
896
 
 
897
                return;
 
898
        }
 
899
 
 
900
        space = fil_space_get_by_id(space_id);
 
901
 
 
902
        if (space != NULL && space->stop_ios) {
 
903
                /* We are going to do a rename file and want to stop new i/o's
 
904
                for a while */
 
905
 
 
906
                if (count2 > 20000) {
 
907
                        fputs("InnoDB: Warning: tablespace ", stderr);
 
908
                        ut_print_filename(stderr, space->name);
 
909
                        fprintf(stderr,
 
910
                                " has i/o ops stopped for a long time %lu\n",
 
911
                                (ulong) count2);
 
912
                }
 
913
 
 
914
                mutex_exit(&fil_system->mutex);
 
915
 
 
916
                os_thread_sleep(20000);
 
917
 
 
918
                count2++;
 
919
 
 
920
                goto retry;
 
921
        }
 
922
 
 
923
        /* If the file is already open, no need to do anything; if the space
 
924
        does not exist, we handle the situation in the function which called
 
925
        this function */
 
926
 
 
927
        if (!space || UT_LIST_GET_FIRST(space->chain)->open) {
 
928
 
 
929
                return;
 
930
        }
 
931
 
 
932
        if (count > 1) {
 
933
                print_info = TRUE;
 
934
        }
 
935
 
 
936
        /* Too many files are open, try to close some */
 
937
close_more:
 
938
        success = fil_try_to_close_file_in_LRU(print_info);
 
939
 
 
940
        if (success && fil_system->n_open >= fil_system->max_n_open) {
 
941
 
 
942
                goto close_more;
 
943
        }
 
944
 
 
945
        if (fil_system->n_open < fil_system->max_n_open) {
 
946
                /* Ok */
 
947
 
 
948
                return;
 
949
        }
 
950
 
 
951
        if (count >= 2) {
 
952
                ut_print_timestamp(stderr);
 
953
                fprintf(stderr,
 
954
                        "  InnoDB: Warning: too many (%lu) files stay open"
 
955
                        " while the maximum\n"
 
956
                        "InnoDB: allowed value would be %lu.\n"
 
957
                        "InnoDB: You may need to raise the value of"
 
958
                        " innodb_max_files_open in\n"
 
959
                        "InnoDB: my.cnf.\n",
 
960
                        (ulong) fil_system->n_open,
 
961
                        (ulong) fil_system->max_n_open);
 
962
 
 
963
                return;
 
964
        }
 
965
 
 
966
        mutex_exit(&fil_system->mutex);
 
967
 
 
968
#ifndef UNIV_HOTBACKUP
 
969
        /* Wake the i/o-handler threads to make sure pending i/o's are
 
970
        performed */
 
971
        os_aio_simulated_wake_handler_threads();
 
972
 
 
973
        os_thread_sleep(20000);
 
974
#endif
 
975
        /* Flush tablespaces so that we can close modified files in the LRU
 
976
        list */
 
977
 
 
978
        fil_flush_file_spaces(FIL_TABLESPACE);
 
979
 
 
980
        count++;
 
981
 
 
982
        goto retry;
 
983
}
 
984
 
 
985
/*******************************************************************//**
 
986
Frees a file node object from a tablespace memory cache. */
 
987
static
 
988
void
 
989
fil_node_free(
 
990
/*==========*/
 
991
        fil_node_t*     node,   /*!< in, own: file node */
 
992
        fil_system_t*   system, /*!< in: tablespace memory cache */
 
993
        fil_space_t*    space)  /*!< in: space where the file node is chained */
 
994
{
 
995
        ut_ad(node && system && space);
 
996
        ut_ad(mutex_own(&(system->mutex)));
 
997
        ut_a(node->magic_n == FIL_NODE_MAGIC_N);
 
998
        ut_a(node->n_pending == 0);
 
999
 
 
1000
        if (node->open) {
 
1001
                /* We fool the assertion in fil_node_close_file() to think
 
1002
                there are no unflushed modifications in the file */
 
1003
 
 
1004
                node->modification_counter = node->flush_counter;
 
1005
 
 
1006
                if (space->is_in_unflushed_spaces
 
1007
                    && fil_space_is_flushed(space)) {
 
1008
 
 
1009
                        space->is_in_unflushed_spaces = FALSE;
 
1010
 
 
1011
                        UT_LIST_REMOVE(unflushed_spaces,
 
1012
                                       system->unflushed_spaces,
 
1013
                                       space);
 
1014
                }
 
1015
 
 
1016
                fil_node_close_file(node, system);
 
1017
        }
 
1018
 
 
1019
        space->size -= node->size;
 
1020
 
 
1021
        UT_LIST_REMOVE(chain, space->chain, node);
 
1022
 
 
1023
        mem_free(node->name);
 
1024
        mem_free(node);
 
1025
}
 
1026
 
 
1027
#ifdef UNIV_LOG_ARCHIVE
 
1028
/****************************************************************//**
 
1029
Drops files from the start of a file space, so that its size is cut by
 
1030
the amount given. */
 
1031
UNIV_INTERN
 
1032
void
 
1033
fil_space_truncate_start(
 
1034
/*=====================*/
 
1035
        ulint   id,             /*!< in: space id */
 
1036
        ulint   trunc_len)      /*!< in: truncate by this much; it is an error
 
1037
                                if this does not equal to the combined size of
 
1038
                                some initial files in the space */
 
1039
{
 
1040
        fil_node_t*     node;
 
1041
        fil_space_t*    space;
 
1042
 
 
1043
        mutex_enter(&fil_system->mutex);
 
1044
 
 
1045
        space = fil_space_get_by_id(id);
 
1046
 
 
1047
        ut_a(space);
 
1048
 
 
1049
        while (trunc_len > 0) {
 
1050
                node = UT_LIST_GET_FIRST(space->chain);
 
1051
 
 
1052
                ut_a(node->size * UNIV_PAGE_SIZE <= trunc_len);
 
1053
 
 
1054
                trunc_len -= node->size * UNIV_PAGE_SIZE;
 
1055
 
 
1056
                fil_node_free(node, fil_system, space);
 
1057
        }
 
1058
 
 
1059
        mutex_exit(&fil_system->mutex);
 
1060
}
 
1061
#endif /* UNIV_LOG_ARCHIVE */
 
1062
 
 
1063
/*******************************************************************//**
 
1064
Creates a space memory object and puts it to the tablespace memory cache. If
 
1065
there is an error, prints an error message to the .err log.
 
1066
@return TRUE if success */
 
1067
UNIV_INTERN
 
1068
ibool
 
1069
fil_space_create(
 
1070
/*=============*/
 
1071
        const char*     name,   /*!< in: space name */
 
1072
        ulint           id,     /*!< in: space id */
 
1073
        ulint           flags,  /*!< in: compressed page size
 
1074
                                and file format, or 0 */
 
1075
        ulint           purpose)/*!< in: FIL_TABLESPACE, or FIL_LOG if log */
 
1076
{
 
1077
        fil_space_t*    space;
 
1078
 
 
1079
        /* The tablespace flags (FSP_SPACE_FLAGS) should be 0 for
 
1080
        ROW_FORMAT=COMPACT (table->flags == DICT_TF_COMPACT) and
 
1081
        ROW_FORMAT=REDUNDANT (table->flags == 0).  For any other
 
1082
        format, the tablespace flags should equal table->flags. */
 
1083
        ut_a(flags != DICT_TF_COMPACT);
 
1084
 
 
1085
try_again:
 
1086
        /*printf(
 
1087
        "InnoDB: Adding tablespace %lu of name %s, purpose %lu\n", id, name,
 
1088
        purpose);*/
 
1089
 
 
1090
        ut_a(fil_system);
 
1091
        ut_a(name);
 
1092
 
 
1093
        mutex_enter(&fil_system->mutex);
 
1094
 
 
1095
        space = fil_space_get_by_name(name);
 
1096
 
 
1097
        if (UNIV_LIKELY_NULL(space)) {
 
1098
                ulint   namesake_id;
 
1099
 
 
1100
                ut_print_timestamp(stderr);
 
1101
                fprintf(stderr,
 
1102
                        "  InnoDB: Warning: trying to init to the"
 
1103
                        " tablespace memory cache\n"
 
1104
                        "InnoDB: a tablespace %lu of name ", (ulong) id);
 
1105
                ut_print_filename(stderr, name);
 
1106
                fprintf(stderr, ",\n"
 
1107
                        "InnoDB: but a tablespace %lu of the same name\n"
 
1108
                        "InnoDB: already exists in the"
 
1109
                        " tablespace memory cache!\n",
 
1110
                        (ulong) space->id);
 
1111
 
 
1112
                if (id == 0 || purpose != FIL_TABLESPACE) {
 
1113
 
 
1114
                        mutex_exit(&fil_system->mutex);
 
1115
 
 
1116
                        return(FALSE);
 
1117
                }
 
1118
 
 
1119
                fprintf(stderr,
 
1120
                        "InnoDB: We assume that InnoDB did a crash recovery,"
 
1121
                        " and you had\n"
 
1122
                        "InnoDB: an .ibd file for which the table"
 
1123
                        " did not exist in the\n"
 
1124
                        "InnoDB: InnoDB internal data dictionary in the"
 
1125
                        " ibdata files.\n"
 
1126
                        "InnoDB: We assume that you later removed the"
 
1127
                        " .ibd and .frm files,\n"
 
1128
                        "InnoDB: and are now trying to recreate the table."
 
1129
                        " We now remove the\n"
 
1130
                        "InnoDB: conflicting tablespace object"
 
1131
                        " from the memory cache and try\n"
 
1132
                        "InnoDB: the init again.\n");
 
1133
 
 
1134
                namesake_id = space->id;
 
1135
 
 
1136
                mutex_exit(&fil_system->mutex);
 
1137
 
 
1138
                fil_space_free(namesake_id);
 
1139
 
 
1140
                goto try_again;
 
1141
        }
 
1142
 
 
1143
        space = fil_space_get_by_id(id);
 
1144
 
 
1145
        if (UNIV_LIKELY_NULL(space)) {
 
1146
                fprintf(stderr,
 
1147
                        "InnoDB: Error: trying to add tablespace %lu"
 
1148
                        " of name ", (ulong) id);
 
1149
                ut_print_filename(stderr, name);
 
1150
                fprintf(stderr, "\n"
 
1151
                        "InnoDB: to the tablespace memory cache,"
 
1152
                        " but tablespace\n"
 
1153
                        "InnoDB: %lu of name ", (ulong) space->id);
 
1154
                ut_print_filename(stderr, space->name);
 
1155
                fputs(" already exists in the tablespace\n"
 
1156
                      "InnoDB: memory cache!\n", stderr);
 
1157
 
 
1158
                mutex_exit(&fil_system->mutex);
 
1159
 
 
1160
                return(FALSE);
 
1161
        }
 
1162
 
 
1163
        space = mem_alloc(sizeof(fil_space_t));
 
1164
 
 
1165
        space->name = mem_strdup(name);
 
1166
        space->id = id;
 
1167
 
 
1168
        fil_system->tablespace_version++;
 
1169
        space->tablespace_version = fil_system->tablespace_version;
 
1170
        space->mark = FALSE;
 
1171
 
 
1172
        if (purpose == FIL_TABLESPACE && id > fil_system->max_assigned_id) {
 
1173
                fil_system->max_assigned_id = id;
 
1174
        }
 
1175
 
 
1176
        space->stop_ios = FALSE;
 
1177
        space->stop_ibuf_merges = FALSE;
 
1178
        space->is_being_deleted = FALSE;
 
1179
        space->purpose = purpose;
 
1180
        space->size = 0;
 
1181
        space->flags = flags;
 
1182
 
 
1183
        space->n_reserved_extents = 0;
 
1184
 
 
1185
        space->n_pending_flushes = 0;
 
1186
        space->n_pending_ibuf_merges = 0;
 
1187
 
 
1188
        UT_LIST_INIT(space->chain);
 
1189
        space->magic_n = FIL_SPACE_MAGIC_N;
 
1190
 
 
1191
        rw_lock_create(&space->latch, SYNC_FSP);
 
1192
 
 
1193
        HASH_INSERT(fil_space_t, hash, fil_system->spaces, id, space);
 
1194
 
 
1195
        HASH_INSERT(fil_space_t, name_hash, fil_system->name_hash,
 
1196
                    ut_fold_string(name), space);
 
1197
        space->is_in_unflushed_spaces = FALSE;
 
1198
 
 
1199
        UT_LIST_ADD_LAST(space_list, fil_system->space_list, space);
 
1200
 
 
1201
        mutex_exit(&fil_system->mutex);
 
1202
 
 
1203
        return(TRUE);
 
1204
}
 
1205
 
 
1206
/*******************************************************************//**
 
1207
Assigns a new space id for a new single-table tablespace. This works simply by
 
1208
incrementing the global counter. If 4 billion id's is not enough, we may need
 
1209
to recycle id's.
 
1210
@return new tablespace id; ULINT_UNDEFINED if could not assign an id */
 
1211
static
 
1212
ulint
 
1213
fil_assign_new_space_id(void)
 
1214
/*=========================*/
 
1215
{
 
1216
        ulint           id;
 
1217
 
 
1218
        mutex_enter(&fil_system->mutex);
 
1219
 
 
1220
        fil_system->max_assigned_id++;
 
1221
 
 
1222
        id = fil_system->max_assigned_id;
 
1223
 
 
1224
        if (id > (SRV_LOG_SPACE_FIRST_ID / 2) && (id % 1000000UL == 0)) {
 
1225
                ut_print_timestamp(stderr);
 
1226
                fprintf(stderr,
 
1227
                        "InnoDB: Warning: you are running out of new"
 
1228
                        " single-table tablespace id's.\n"
 
1229
                        "InnoDB: Current counter is %lu and it"
 
1230
                        " must not exceed %lu!\n"
 
1231
                        "InnoDB: To reset the counter to zero"
 
1232
                        " you have to dump all your tables and\n"
 
1233
                        "InnoDB: recreate the whole InnoDB installation.\n",
 
1234
                        (ulong) id,
 
1235
                        (ulong) SRV_LOG_SPACE_FIRST_ID);
 
1236
        }
 
1237
 
 
1238
        if (id >= SRV_LOG_SPACE_FIRST_ID) {
 
1239
                ut_print_timestamp(stderr);
 
1240
                fprintf(stderr,
 
1241
                        "InnoDB: You have run out of single-table"
 
1242
                        " tablespace id's!\n"
 
1243
                        "InnoDB: Current counter is %lu.\n"
 
1244
                        "InnoDB: To reset the counter to zero you"
 
1245
                        " have to dump all your tables and\n"
 
1246
                        "InnoDB: recreate the whole InnoDB installation.\n",
 
1247
                        (ulong) id);
 
1248
                fil_system->max_assigned_id--;
 
1249
 
 
1250
                id = ULINT_UNDEFINED;
 
1251
        }
 
1252
 
 
1253
        mutex_exit(&fil_system->mutex);
 
1254
 
 
1255
        return(id);
 
1256
}
 
1257
 
 
1258
/*******************************************************************//**
 
1259
Frees a space object from the tablespace memory cache. Closes the files in
 
1260
the chain but does not delete them. There must not be any pending i/o's or
 
1261
flushes on the files.
 
1262
@return TRUE if success */
 
1263
UNIV_INTERN
 
1264
ibool
 
1265
fil_space_free(
 
1266
/*===========*/
 
1267
        ulint   id)     /*!< in: space id */
 
1268
{
 
1269
        fil_space_t*    space;
 
1270
        fil_space_t*    namespace;
 
1271
        fil_node_t*     fil_node;
 
1272
 
 
1273
        mutex_enter(&fil_system->mutex);
 
1274
 
 
1275
        space = fil_space_get_by_id(id);
 
1276
 
 
1277
        if (!space) {
 
1278
                ut_print_timestamp(stderr);
 
1279
                fprintf(stderr,
 
1280
                        "  InnoDB: Error: trying to remove tablespace %lu"
 
1281
                        " from the cache but\n"
 
1282
                        "InnoDB: it is not there.\n", (ulong) id);
 
1283
 
 
1284
                mutex_exit(&fil_system->mutex);
 
1285
 
 
1286
                return(FALSE);
 
1287
        }
 
1288
 
 
1289
        HASH_DELETE(fil_space_t, hash, fil_system->spaces, id, space);
 
1290
 
 
1291
        namespace = fil_space_get_by_name(space->name);
 
1292
        ut_a(namespace);
 
1293
        ut_a(space == namespace);
 
1294
 
 
1295
        HASH_DELETE(fil_space_t, name_hash, fil_system->name_hash,
 
1296
                    ut_fold_string(space->name), space);
 
1297
 
 
1298
        if (space->is_in_unflushed_spaces) {
 
1299
                space->is_in_unflushed_spaces = FALSE;
 
1300
 
 
1301
                UT_LIST_REMOVE(unflushed_spaces, fil_system->unflushed_spaces,
 
1302
                               space);
 
1303
        }
 
1304
 
 
1305
        UT_LIST_REMOVE(space_list, fil_system->space_list, space);
 
1306
 
 
1307
        ut_a(space->magic_n == FIL_SPACE_MAGIC_N);
 
1308
        ut_a(0 == space->n_pending_flushes);
 
1309
 
 
1310
        fil_node = UT_LIST_GET_FIRST(space->chain);
 
1311
 
 
1312
        while (fil_node != NULL) {
 
1313
                fil_node_free(fil_node, fil_system, space);
 
1314
 
 
1315
                fil_node = UT_LIST_GET_FIRST(space->chain);
 
1316
        }
 
1317
 
 
1318
        ut_a(0 == UT_LIST_GET_LEN(space->chain));
 
1319
 
 
1320
        mutex_exit(&fil_system->mutex);
 
1321
 
 
1322
        rw_lock_free(&(space->latch));
 
1323
 
 
1324
        mem_free(space->name);
 
1325
        mem_free(space);
 
1326
 
 
1327
        return(TRUE);
 
1328
}
 
1329
 
 
1330
/*******************************************************************//**
 
1331
Returns the size of the space in pages. The tablespace must be cached in the
 
1332
memory cache.
 
1333
@return space size, 0 if space not found */
 
1334
UNIV_INTERN
 
1335
ulint
 
1336
fil_space_get_size(
 
1337
/*===============*/
 
1338
        ulint   id)     /*!< in: space id */
 
1339
{
 
1340
        fil_node_t*     node;
 
1341
        fil_space_t*    space;
 
1342
        ulint           size;
 
1343
 
 
1344
        ut_ad(fil_system);
 
1345
 
 
1346
        fil_mutex_enter_and_prepare_for_io(id);
 
1347
 
 
1348
        space = fil_space_get_by_id(id);
 
1349
 
 
1350
        if (space == NULL) {
 
1351
                mutex_exit(&fil_system->mutex);
 
1352
 
 
1353
                return(0);
 
1354
        }
 
1355
 
 
1356
        if (space->size == 0 && space->purpose == FIL_TABLESPACE) {
 
1357
                ut_a(id != 0);
 
1358
 
 
1359
                ut_a(1 == UT_LIST_GET_LEN(space->chain));
 
1360
 
 
1361
                node = UT_LIST_GET_FIRST(space->chain);
 
1362
 
 
1363
                /* It must be a single-table tablespace and we have not opened
 
1364
                the file yet; the following calls will open it and update the
 
1365
                size fields */
 
1366
 
 
1367
                fil_node_prepare_for_io(node, fil_system, space);
 
1368
                fil_node_complete_io(node, fil_system, OS_FILE_READ);
 
1369
        }
 
1370
 
 
1371
        size = space->size;
 
1372
 
 
1373
        mutex_exit(&fil_system->mutex);
 
1374
 
 
1375
        return(size);
 
1376
}
 
1377
 
 
1378
/*******************************************************************//**
 
1379
Returns the flags of the space. The tablespace must be cached
 
1380
in the memory cache.
 
1381
@return flags, ULINT_UNDEFINED if space not found */
 
1382
UNIV_INTERN
 
1383
ulint
 
1384
fil_space_get_flags(
 
1385
/*================*/
 
1386
        ulint   id)     /*!< in: space id */
 
1387
{
 
1388
        fil_node_t*     node;
 
1389
        fil_space_t*    space;
 
1390
        ulint           flags;
 
1391
 
 
1392
        ut_ad(fil_system);
 
1393
 
 
1394
        if (UNIV_UNLIKELY(!id)) {
 
1395
                return(0);
 
1396
        }
 
1397
 
 
1398
        fil_mutex_enter_and_prepare_for_io(id);
 
1399
 
 
1400
        space = fil_space_get_by_id(id);
 
1401
 
 
1402
        if (space == NULL) {
 
1403
                mutex_exit(&fil_system->mutex);
 
1404
 
 
1405
                return(ULINT_UNDEFINED);
 
1406
        }
 
1407
 
 
1408
        if (space->size == 0 && space->purpose == FIL_TABLESPACE) {
 
1409
                ut_a(id != 0);
 
1410
 
 
1411
                ut_a(1 == UT_LIST_GET_LEN(space->chain));
 
1412
 
 
1413
                node = UT_LIST_GET_FIRST(space->chain);
 
1414
 
 
1415
                /* It must be a single-table tablespace and we have not opened
 
1416
                the file yet; the following calls will open it and update the
 
1417
                size fields */
 
1418
 
 
1419
                fil_node_prepare_for_io(node, fil_system, space);
 
1420
                fil_node_complete_io(node, fil_system, OS_FILE_READ);
 
1421
        }
 
1422
 
 
1423
        flags = space->flags;
 
1424
 
 
1425
        mutex_exit(&fil_system->mutex);
 
1426
 
 
1427
        return(flags);
 
1428
}
 
1429
 
 
1430
/*******************************************************************//**
 
1431
Returns the compressed page size of the space, or 0 if the space
 
1432
is not compressed. The tablespace must be cached in the memory cache.
 
1433
@return compressed page size, ULINT_UNDEFINED if space not found */
 
1434
UNIV_INTERN
 
1435
ulint
 
1436
fil_space_get_zip_size(
 
1437
/*===================*/
 
1438
        ulint   id)     /*!< in: space id */
 
1439
{
 
1440
        ulint   flags;
 
1441
 
 
1442
        flags = fil_space_get_flags(id);
 
1443
 
 
1444
        if (flags && flags != ULINT_UNDEFINED) {
 
1445
 
 
1446
                return(dict_table_flags_to_zip_size(flags));
 
1447
        }
 
1448
 
 
1449
        return(flags);
 
1450
}
 
1451
 
 
1452
/*******************************************************************//**
 
1453
Checks if the pair space, page_no refers to an existing page in a tablespace
 
1454
file space. The tablespace must be cached in the memory cache.
 
1455
@return TRUE if the address is meaningful */
 
1456
UNIV_INTERN
 
1457
ibool
 
1458
fil_check_adress_in_tablespace(
 
1459
/*===========================*/
 
1460
        ulint   id,     /*!< in: space id */
 
1461
        ulint   page_no)/*!< in: page number */
 
1462
{
 
1463
        if (fil_space_get_size(id) > page_no) {
 
1464
 
 
1465
                return(TRUE);
 
1466
        }
 
1467
 
 
1468
        return(FALSE);
 
1469
}
 
1470
 
 
1471
/****************************************************************//**
 
1472
Initializes the tablespace memory cache. */
 
1473
UNIV_INTERN
 
1474
void
 
1475
fil_init(
 
1476
/*=====*/
 
1477
        ulint   hash_size,      /*!< in: hash table size */
 
1478
        ulint   max_n_open)     /*!< in: max number of open files */
 
1479
{
 
1480
        ut_a(fil_system == NULL);
 
1481
 
 
1482
        ut_a(hash_size > 0);
 
1483
        ut_a(max_n_open > 0);
 
1484
 
 
1485
        fil_system = mem_alloc(sizeof(fil_system_t));
 
1486
 
 
1487
        mutex_create(&fil_system->mutex, SYNC_ANY_LATCH);
 
1488
 
 
1489
        fil_system->spaces = hash_create(hash_size);
 
1490
        fil_system->name_hash = hash_create(hash_size);
 
1491
 
 
1492
        UT_LIST_INIT(fil_system->LRU);
 
1493
 
 
1494
        fil_system->n_open = 0;
 
1495
        fil_system->max_n_open = max_n_open;
 
1496
 
 
1497
        fil_system->modification_counter = 0;
 
1498
        fil_system->max_assigned_id = 0;
 
1499
 
 
1500
        fil_system->tablespace_version = 0;
 
1501
 
 
1502
        UT_LIST_INIT(fil_system->unflushed_spaces);
 
1503
        UT_LIST_INIT(fil_system->space_list);
 
1504
}
 
1505
 
 
1506
/*******************************************************************//**
 
1507
Opens all log files and system tablespace data files. They stay open until the
 
1508
database server shutdown. This should be called at a server startup after the
 
1509
space objects for the log and the system tablespace have been created. The
 
1510
purpose of this operation is to make sure we never run out of file descriptors
 
1511
if we need to read from the insert buffer or to write to the log. */
 
1512
UNIV_INTERN
 
1513
void
 
1514
fil_open_log_and_system_tablespace_files(void)
 
1515
/*==========================================*/
 
1516
{
 
1517
        fil_space_t*    space;
 
1518
        fil_node_t*     node;
 
1519
 
 
1520
        mutex_enter(&fil_system->mutex);
 
1521
 
 
1522
        space = UT_LIST_GET_FIRST(fil_system->space_list);
 
1523
 
 
1524
        while (space != NULL) {
 
1525
                if (space->purpose != FIL_TABLESPACE || space->id == 0) {
 
1526
                        node = UT_LIST_GET_FIRST(space->chain);
 
1527
 
 
1528
                        while (node != NULL) {
 
1529
                                if (!node->open) {
 
1530
                                        fil_node_open_file(node, fil_system,
 
1531
                                                           space);
 
1532
                                }
 
1533
                                if (fil_system->max_n_open
 
1534
                                    < 10 + fil_system->n_open) {
 
1535
                                        fprintf(stderr,
 
1536
                                                "InnoDB: Warning: you must"
 
1537
                                                " raise the value of"
 
1538
                                                " innodb_max_open_files in\n"
 
1539
                                                "InnoDB: my.cnf! Remember that"
 
1540
                                                " InnoDB keeps all log files"
 
1541
                                                " and all system\n"
 
1542
                                                "InnoDB: tablespace files open"
 
1543
                                                " for the whole time mysqld is"
 
1544
                                                " running, and\n"
 
1545
                                                "InnoDB: needs to open also"
 
1546
                                                " some .ibd files if the"
 
1547
                                                " file-per-table storage\n"
 
1548
                                                "InnoDB: model is used."
 
1549
                                                " Current open files %lu,"
 
1550
                                                " max allowed"
 
1551
                                                " open files %lu.\n",
 
1552
                                                (ulong) fil_system->n_open,
 
1553
                                                (ulong) fil_system->max_n_open);
 
1554
                                }
 
1555
                                node = UT_LIST_GET_NEXT(chain, node);
 
1556
                        }
 
1557
                }
 
1558
                space = UT_LIST_GET_NEXT(space_list, space);
 
1559
        }
 
1560
 
 
1561
        mutex_exit(&fil_system->mutex);
 
1562
}
 
1563
 
 
1564
/*******************************************************************//**
 
1565
Closes all open files. There must not be any pending i/o's or not flushed
 
1566
modifications in the files. */
 
1567
UNIV_INTERN
 
1568
void
 
1569
fil_close_all_files(void)
 
1570
/*=====================*/
 
1571
{
 
1572
        fil_space_t*    space;
 
1573
        fil_node_t*     node;
 
1574
 
 
1575
        mutex_enter(&fil_system->mutex);
 
1576
 
 
1577
        space = UT_LIST_GET_FIRST(fil_system->space_list);
 
1578
 
 
1579
        while (space != NULL) {
 
1580
                node = UT_LIST_GET_FIRST(space->chain);
 
1581
 
 
1582
                while (node != NULL) {
 
1583
                        if (node->open) {
 
1584
                                fil_node_close_file(node, fil_system);
 
1585
                        }
 
1586
                        node = UT_LIST_GET_NEXT(chain, node);
 
1587
                }
 
1588
                space = UT_LIST_GET_NEXT(space_list, space);
 
1589
        }
 
1590
 
 
1591
        mutex_exit(&fil_system->mutex);
 
1592
}
 
1593
 
 
1594
/*******************************************************************//**
 
1595
Sets the max tablespace id counter if the given number is bigger than the
 
1596
previous value. */
 
1597
UNIV_INTERN
 
1598
void
 
1599
fil_set_max_space_id_if_bigger(
 
1600
/*===========================*/
 
1601
        ulint   max_id) /*!< in: maximum known id */
 
1602
{
 
1603
        if (max_id >= SRV_LOG_SPACE_FIRST_ID) {
 
1604
                fprintf(stderr,
 
1605
                        "InnoDB: Fatal error: max tablespace id"
 
1606
                        " is too high, %lu\n", (ulong) max_id);
 
1607
                ut_error;
 
1608
        }
 
1609
 
 
1610
        mutex_enter(&fil_system->mutex);
 
1611
 
 
1612
        if (fil_system->max_assigned_id < max_id) {
 
1613
 
 
1614
                fil_system->max_assigned_id = max_id;
 
1615
        }
 
1616
 
 
1617
        mutex_exit(&fil_system->mutex);
 
1618
}
 
1619
 
 
1620
/****************************************************************//**
 
1621
Writes the flushed lsn and the latest archived log number to the page header
 
1622
of the first page of a data file of the system tablespace (space 0),
 
1623
which is uncompressed. */
 
1624
static
 
1625
ulint
 
1626
fil_write_lsn_and_arch_no_to_file(
 
1627
/*==============================*/
 
1628
        ulint           sum_of_sizes,   /*!< in: combined size of previous files
 
1629
                                        in space, in database pages */
 
1630
        ib_uint64_t     lsn,            /*!< in: lsn to write */
 
1631
        ulint           arch_log_no __attribute__((unused)))
 
1632
                                        /*!< in: archived log number to write */
 
1633
{
 
1634
        byte*   buf1;
 
1635
        byte*   buf;
 
1636
 
 
1637
        buf1 = mem_alloc(2 * UNIV_PAGE_SIZE);
 
1638
        buf = ut_align(buf1, UNIV_PAGE_SIZE);
 
1639
 
 
1640
        fil_read(TRUE, 0, 0, sum_of_sizes, 0, UNIV_PAGE_SIZE, buf, NULL);
 
1641
 
 
1642
        mach_write_ull(buf + FIL_PAGE_FILE_FLUSH_LSN, lsn);
 
1643
 
 
1644
        fil_write(TRUE, 0, 0, sum_of_sizes, 0, UNIV_PAGE_SIZE, buf, NULL);
 
1645
 
 
1646
        mem_free(buf1);
 
1647
 
 
1648
        return(DB_SUCCESS);
 
1649
}
 
1650
 
 
1651
/****************************************************************//**
 
1652
Writes the flushed lsn and the latest archived log number to the page
 
1653
header of the first page of each data file in the system tablespace.
 
1654
@return DB_SUCCESS or error number */
 
1655
UNIV_INTERN
 
1656
ulint
 
1657
fil_write_flushed_lsn_to_data_files(
 
1658
/*================================*/
 
1659
        ib_uint64_t     lsn,            /*!< in: lsn to write */
 
1660
        ulint           arch_log_no)    /*!< in: latest archived log
 
1661
                                        file number */
 
1662
{
 
1663
        fil_space_t*    space;
 
1664
        fil_node_t*     node;
 
1665
        ulint           sum_of_sizes;
 
1666
        ulint           err;
 
1667
 
 
1668
        mutex_enter(&fil_system->mutex);
 
1669
 
 
1670
        space = UT_LIST_GET_FIRST(fil_system->space_list);
 
1671
 
 
1672
        while (space) {
 
1673
                /* We only write the lsn to all existing data files which have
 
1674
                been open during the lifetime of the mysqld process; they are
 
1675
                represented by the space objects in the tablespace memory
 
1676
                cache. Note that all data files in the system tablespace 0 are
 
1677
                always open. */
 
1678
 
 
1679
                if (space->purpose == FIL_TABLESPACE
 
1680
                    && space->id == 0) {
 
1681
                        sum_of_sizes = 0;
 
1682
 
 
1683
                        node = UT_LIST_GET_FIRST(space->chain);
 
1684
                        while (node) {
 
1685
                                mutex_exit(&fil_system->mutex);
 
1686
 
 
1687
                                err = fil_write_lsn_and_arch_no_to_file(
 
1688
                                        sum_of_sizes, lsn, arch_log_no);
 
1689
                                if (err != DB_SUCCESS) {
 
1690
 
 
1691
                                        return(err);
 
1692
                                }
 
1693
 
 
1694
                                mutex_enter(&fil_system->mutex);
 
1695
 
 
1696
                                sum_of_sizes += node->size;
 
1697
                                node = UT_LIST_GET_NEXT(chain, node);
 
1698
                        }
 
1699
                }
 
1700
                space = UT_LIST_GET_NEXT(space_list, space);
 
1701
        }
 
1702
 
 
1703
        mutex_exit(&fil_system->mutex);
 
1704
 
 
1705
        return(DB_SUCCESS);
 
1706
}
 
1707
 
 
1708
/*******************************************************************//**
 
1709
Reads the flushed lsn and arch no fields from a data file at database
 
1710
startup. */
 
1711
UNIV_INTERN
 
1712
void
 
1713
fil_read_flushed_lsn_and_arch_log_no(
 
1714
/*=================================*/
 
1715
        os_file_t       data_file,              /*!< in: open data file */
 
1716
        ibool           one_read_already,       /*!< in: TRUE if min and max
 
1717
                                                parameters below already
 
1718
                                                contain sensible data */
 
1719
#ifdef UNIV_LOG_ARCHIVE
 
1720
        ulint*          min_arch_log_no,        /*!< in/out: */
 
1721
        ulint*          max_arch_log_no,        /*!< in/out: */
 
1722
#endif /* UNIV_LOG_ARCHIVE */
 
1723
        ib_uint64_t*    min_flushed_lsn,        /*!< in/out: */
 
1724
        ib_uint64_t*    max_flushed_lsn)        /*!< in/out: */
 
1725
{
 
1726
        byte*           buf;
 
1727
        byte*           buf2;
 
1728
        ib_uint64_t     flushed_lsn;
 
1729
 
 
1730
        buf2 = ut_malloc(2 * UNIV_PAGE_SIZE);
 
1731
        /* Align the memory for a possible read from a raw device */
 
1732
        buf = ut_align(buf2, UNIV_PAGE_SIZE);
 
1733
 
 
1734
        os_file_read(data_file, buf, 0, 0, UNIV_PAGE_SIZE);
 
1735
 
 
1736
        flushed_lsn = mach_read_ull(buf + FIL_PAGE_FILE_FLUSH_LSN);
 
1737
 
 
1738
        ut_free(buf2);
 
1739
 
 
1740
        if (!one_read_already) {
 
1741
                *min_flushed_lsn = flushed_lsn;
 
1742
                *max_flushed_lsn = flushed_lsn;
 
1743
#ifdef UNIV_LOG_ARCHIVE
 
1744
                *min_arch_log_no = arch_log_no;
 
1745
                *max_arch_log_no = arch_log_no;
 
1746
#endif /* UNIV_LOG_ARCHIVE */
 
1747
                return;
 
1748
        }
 
1749
 
 
1750
        if (*min_flushed_lsn > flushed_lsn) {
 
1751
                *min_flushed_lsn = flushed_lsn;
 
1752
        }
 
1753
        if (*max_flushed_lsn < flushed_lsn) {
 
1754
                *max_flushed_lsn = flushed_lsn;
 
1755
        }
 
1756
#ifdef UNIV_LOG_ARCHIVE
 
1757
        if (*min_arch_log_no > arch_log_no) {
 
1758
                *min_arch_log_no = arch_log_no;
 
1759
        }
 
1760
        if (*max_arch_log_no < arch_log_no) {
 
1761
                *max_arch_log_no = arch_log_no;
 
1762
        }
 
1763
#endif /* UNIV_LOG_ARCHIVE */
 
1764
}
 
1765
 
 
1766
/*================ SINGLE-TABLE TABLESPACES ==========================*/
 
1767
 
 
1768
#ifndef UNIV_HOTBACKUP
 
1769
/*******************************************************************//**
 
1770
Increments the count of pending insert buffer page merges, if space is not
 
1771
being deleted.
 
1772
@return TRUE if being deleted, and ibuf merges should be skipped */
 
1773
UNIV_INTERN
 
1774
ibool
 
1775
fil_inc_pending_ibuf_merges(
 
1776
/*========================*/
 
1777
        ulint   id)     /*!< in: space id */
 
1778
{
 
1779
        fil_space_t*    space;
 
1780
 
 
1781
        mutex_enter(&fil_system->mutex);
 
1782
 
 
1783
        space = fil_space_get_by_id(id);
 
1784
 
 
1785
        if (space == NULL) {
 
1786
                fprintf(stderr,
 
1787
                        "InnoDB: Error: trying to do ibuf merge to a"
 
1788
                        " dropped tablespace %lu\n",
 
1789
                        (ulong) id);
 
1790
        }
 
1791
 
 
1792
        if (space == NULL || space->stop_ibuf_merges) {
 
1793
                mutex_exit(&fil_system->mutex);
 
1794
 
 
1795
                return(TRUE);
 
1796
        }
 
1797
 
 
1798
        space->n_pending_ibuf_merges++;
 
1799
 
 
1800
        mutex_exit(&fil_system->mutex);
 
1801
 
 
1802
        return(FALSE);
 
1803
}
 
1804
 
 
1805
/*******************************************************************//**
 
1806
Decrements the count of pending insert buffer page merges. */
 
1807
UNIV_INTERN
 
1808
void
 
1809
fil_decr_pending_ibuf_merges(
 
1810
/*=========================*/
 
1811
        ulint   id)     /*!< in: space id */
 
1812
{
 
1813
        fil_space_t*    space;
 
1814
 
 
1815
        mutex_enter(&fil_system->mutex);
 
1816
 
 
1817
        space = fil_space_get_by_id(id);
 
1818
 
 
1819
        if (space == NULL) {
 
1820
                fprintf(stderr,
 
1821
                        "InnoDB: Error: decrementing ibuf merge of a"
 
1822
                        " dropped tablespace %lu\n",
 
1823
                        (ulong) id);
 
1824
        }
 
1825
 
 
1826
        if (space != NULL) {
 
1827
                space->n_pending_ibuf_merges--;
 
1828
        }
 
1829
 
 
1830
        mutex_exit(&fil_system->mutex);
 
1831
}
 
1832
#endif /* !UNIV_HOTBACKUP */
 
1833
 
 
1834
/********************************************************//**
 
1835
Creates the database directory for a table if it does not exist yet. */
 
1836
static
 
1837
void
 
1838
fil_create_directory_for_tablename(
 
1839
/*===============================*/
 
1840
        const char*     name)   /*!< in: name in the standard
 
1841
                                'databasename/tablename' format */
 
1842
{
 
1843
        const char*     namend;
 
1844
        char*           path;
 
1845
        ulint           len;
 
1846
 
 
1847
        len = strlen(fil_path_to_mysql_datadir);
 
1848
        namend = strchr(name, '/');
 
1849
        ut_a(namend);
 
1850
        path = mem_alloc(len + (namend - name) + 2);
 
1851
 
 
1852
        memcpy(path, fil_path_to_mysql_datadir, len);
 
1853
        path[len] = '/';
 
1854
        memcpy(path + len + 1, name, namend - name);
 
1855
        path[len + (namend - name) + 1] = 0;
 
1856
 
 
1857
        srv_normalize_path_for_win(path);
 
1858
 
 
1859
        ut_a(os_file_create_directory(path, FALSE));
 
1860
        mem_free(path);
 
1861
}
 
1862
 
 
1863
#ifndef UNIV_HOTBACKUP
 
1864
/********************************************************//**
 
1865
Writes a log record about an .ibd file create/rename/delete. */
 
1866
static
 
1867
void
 
1868
fil_op_write_log(
 
1869
/*=============*/
 
1870
        ulint           type,           /*!< in: MLOG_FILE_CREATE,
 
1871
                                        MLOG_FILE_CREATE2,
 
1872
                                        MLOG_FILE_DELETE, or
 
1873
                                        MLOG_FILE_RENAME */
 
1874
        ulint           space_id,       /*!< in: space id */
 
1875
        ulint           log_flags,      /*!< in: redo log flags (stored
 
1876
                                        in the page number field) */
 
1877
        ulint           flags,          /*!< in: compressed page size
 
1878
                                        and file format
 
1879
                                        if type==MLOG_FILE_CREATE2, or 0 */
 
1880
        const char*     name,           /*!< in: table name in the familiar
 
1881
                                        'databasename/tablename' format, or
 
1882
                                        the file path in the case of
 
1883
                                        MLOG_FILE_DELETE */
 
1884
        const char*     new_name,       /*!< in: if type is MLOG_FILE_RENAME,
 
1885
                                        the new table name in the
 
1886
                                        'databasename/tablename' format */
 
1887
        mtr_t*          mtr)            /*!< in: mini-transaction handle */
 
1888
{
 
1889
        byte*   log_ptr;
 
1890
        ulint   len;
 
1891
 
 
1892
        log_ptr = mlog_open(mtr, 11 + 2 + 1);
 
1893
 
 
1894
        if (!log_ptr) {
 
1895
                /* Logging in mtr is switched off during crash recovery:
 
1896
                in that case mlog_open returns NULL */
 
1897
                return;
 
1898
        }
 
1899
 
 
1900
        log_ptr = mlog_write_initial_log_record_for_file_op(
 
1901
                type, space_id, log_flags, log_ptr, mtr);
 
1902
        if (type == MLOG_FILE_CREATE2) {
 
1903
                mach_write_to_4(log_ptr, flags);
 
1904
                log_ptr += 4;
 
1905
        }
 
1906
        /* Let us store the strings as null-terminated for easier readability
 
1907
        and handling */
 
1908
 
 
1909
        len = strlen(name) + 1;
 
1910
 
 
1911
        mach_write_to_2(log_ptr, len);
 
1912
        log_ptr += 2;
 
1913
        mlog_close(mtr, log_ptr);
 
1914
 
 
1915
        mlog_catenate_string(mtr, (byte*) name, len);
 
1916
 
 
1917
        if (type == MLOG_FILE_RENAME) {
 
1918
                len = strlen(new_name) + 1;
 
1919
                log_ptr = mlog_open(mtr, 2 + len);
 
1920
                ut_a(log_ptr);
 
1921
                mach_write_to_2(log_ptr, len);
 
1922
                log_ptr += 2;
 
1923
                mlog_close(mtr, log_ptr);
 
1924
 
 
1925
                mlog_catenate_string(mtr, (byte*) new_name, len);
 
1926
        }
 
1927
}
 
1928
#endif
 
1929
 
 
1930
/*******************************************************************//**
 
1931
Parses the body of a log record written about an .ibd file operation. That is,
 
1932
the log record part after the standard (type, space id, page no) header of the
 
1933
log record.
 
1934
 
 
1935
If desired, also replays the delete or rename operation if the .ibd file
 
1936
exists and the space id in it matches. Replays the create operation if a file
 
1937
at that path does not exist yet. If the database directory for the file to be
 
1938
created does not exist, then we create the directory, too.
 
1939
 
 
1940
Note that ibbackup --apply-log sets fil_path_to_mysql_datadir to point to the
 
1941
datadir that we should use in replaying the file operations.
 
1942
@return end of log record, or NULL if the record was not completely
 
1943
contained between ptr and end_ptr */
 
1944
UNIV_INTERN
 
1945
byte*
 
1946
fil_op_log_parse_or_replay(
 
1947
/*=======================*/
 
1948
        byte*   ptr,            /*!< in: buffer containing the log record body,
 
1949
                                or an initial segment of it, if the record does
 
1950
                                not fir completely between ptr and end_ptr */
 
1951
        byte*   end_ptr,        /*!< in: buffer end */
 
1952
        ulint   type,           /*!< in: the type of this log record */
 
1953
        ulint   space_id,       /*!< in: the space id of the tablespace in
 
1954
                                question, or 0 if the log record should
 
1955
                                only be parsed but not replayed */
 
1956
        ulint   log_flags)      /*!< in: redo log flags
 
1957
                                (stored in the page number parameter) */
 
1958
{
 
1959
        ulint           name_len;
 
1960
        ulint           new_name_len;
 
1961
        const char*     name;
 
1962
        const char*     new_name        = NULL;
 
1963
        ulint           flags           = 0;
 
1964
 
 
1965
        if (type == MLOG_FILE_CREATE2) {
 
1966
                if (end_ptr < ptr + 4) {
 
1967
 
 
1968
                        return(NULL);
 
1969
                }
 
1970
 
 
1971
                flags = mach_read_from_4(ptr);
 
1972
                ptr += 4;
 
1973
        }
 
1974
 
 
1975
        if (end_ptr < ptr + 2) {
 
1976
 
 
1977
                return(NULL);
 
1978
        }
 
1979
 
 
1980
        name_len = mach_read_from_2(ptr);
 
1981
 
 
1982
        ptr += 2;
 
1983
 
 
1984
        if (end_ptr < ptr + name_len) {
 
1985
 
 
1986
                return(NULL);
 
1987
        }
 
1988
 
 
1989
        name = (const char*) ptr;
 
1990
 
 
1991
        ptr += name_len;
 
1992
 
 
1993
        if (type == MLOG_FILE_RENAME) {
 
1994
                if (end_ptr < ptr + 2) {
 
1995
 
 
1996
                        return(NULL);
 
1997
                }
 
1998
 
 
1999
                new_name_len = mach_read_from_2(ptr);
 
2000
 
 
2001
                ptr += 2;
 
2002
 
 
2003
                if (end_ptr < ptr + new_name_len) {
 
2004
 
 
2005
                        return(NULL);
 
2006
                }
 
2007
 
 
2008
                new_name = (const char*) ptr;
 
2009
 
 
2010
                ptr += new_name_len;
 
2011
        }
 
2012
 
 
2013
        /* We managed to parse a full log record body */
 
2014
        /*
 
2015
        printf("Parsed log rec of type %lu space %lu\n"
 
2016
        "name %s\n", type, space_id, name);
 
2017
 
 
2018
        if (type == MLOG_FILE_RENAME) {
 
2019
        printf("new name %s\n", new_name);
 
2020
        }
 
2021
        */
 
2022
        if (!space_id) {
 
2023
 
 
2024
                return(ptr);
 
2025
        }
 
2026
 
 
2027
        /* Let us try to perform the file operation, if sensible. Note that
 
2028
        ibbackup has at this stage already read in all space id info to the
 
2029
        fil0fil.c data structures.
 
2030
 
 
2031
        NOTE that our algorithm is not guaranteed to work correctly if there
 
2032
        were renames of tables during the backup. See ibbackup code for more
 
2033
        on the problem. */
 
2034
 
 
2035
        switch (type) {
 
2036
        case MLOG_FILE_DELETE:
 
2037
                if (fil_tablespace_exists_in_mem(space_id)) {
 
2038
                        ut_a(fil_delete_tablespace(space_id));
 
2039
                }
 
2040
 
 
2041
                break;
 
2042
 
 
2043
        case MLOG_FILE_RENAME:
 
2044
                /* We do the rename based on space id, not old file name;
 
2045
                this should guarantee that after the log replay each .ibd file
 
2046
                has the correct name for the latest log sequence number; the
 
2047
                proof is left as an exercise :) */
 
2048
 
 
2049
                if (fil_tablespace_exists_in_mem(space_id)) {
 
2050
                        /* Create the database directory for the new name, if
 
2051
                        it does not exist yet */
 
2052
                        fil_create_directory_for_tablename(new_name);
 
2053
 
 
2054
                        /* Rename the table if there is not yet a tablespace
 
2055
                        with the same name */
 
2056
 
 
2057
                        if (fil_get_space_id_for_table(new_name)
 
2058
                            == ULINT_UNDEFINED) {
 
2059
                                /* We do not care of the old name, that is
 
2060
                                why we pass NULL as the first argument */
 
2061
                                if (!fil_rename_tablespace(NULL, space_id,
 
2062
                                                           new_name)) {
 
2063
                                        ut_error;
 
2064
                                }
 
2065
                        }
 
2066
                }
 
2067
 
 
2068
                break;
 
2069
 
 
2070
        case MLOG_FILE_CREATE:
 
2071
        case MLOG_FILE_CREATE2:
 
2072
                if (fil_tablespace_exists_in_mem(space_id)) {
 
2073
                        /* Do nothing */
 
2074
                } else if (fil_get_space_id_for_table(name)
 
2075
                           != ULINT_UNDEFINED) {
 
2076
                        /* Do nothing */
 
2077
                } else if (log_flags & MLOG_FILE_FLAG_TEMP) {
 
2078
                        /* Temporary table, do nothing */
 
2079
                } else {
 
2080
                        /* Create the database directory for name, if it does
 
2081
                        not exist yet */
 
2082
                        fil_create_directory_for_tablename(name);
 
2083
 
 
2084
                        if (fil_create_new_single_table_tablespace(
 
2085
                                    &space_id, name, FALSE, flags,
 
2086
                                    FIL_IBD_FILE_INITIAL_SIZE) != DB_SUCCESS) {
 
2087
                                ut_error;
 
2088
                        }
 
2089
                }
 
2090
 
 
2091
                break;
 
2092
 
 
2093
        default:
 
2094
                ut_error;
 
2095
        }
 
2096
 
 
2097
        return(ptr);
 
2098
}
 
2099
 
 
2100
/*******************************************************************//**
 
2101
Deletes a single-table tablespace. The tablespace must be cached in the
 
2102
memory cache.
 
2103
@return TRUE if success */
 
2104
UNIV_INTERN
 
2105
ibool
 
2106
fil_delete_tablespace(
 
2107
/*==================*/
 
2108
        ulint   id)     /*!< in: space id */
 
2109
{
 
2110
        ibool           success;
 
2111
        fil_space_t*    space;
 
2112
        fil_node_t*     node;
 
2113
        ulint           count           = 0;
 
2114
        char*           path;
 
2115
 
 
2116
        ut_a(id != 0);
 
2117
stop_ibuf_merges:
 
2118
        mutex_enter(&fil_system->mutex);
 
2119
 
 
2120
        space = fil_space_get_by_id(id);
 
2121
 
 
2122
        if (space != NULL) {
 
2123
                space->stop_ibuf_merges = TRUE;
 
2124
 
 
2125
                if (space->n_pending_ibuf_merges == 0) {
 
2126
                        mutex_exit(&fil_system->mutex);
 
2127
 
 
2128
                        count = 0;
 
2129
 
 
2130
                        goto try_again;
 
2131
                } else {
 
2132
                        if (count > 5000) {
 
2133
                                ut_print_timestamp(stderr);
 
2134
                                fputs("  InnoDB: Warning: trying to"
 
2135
                                      " delete tablespace ", stderr);
 
2136
                                ut_print_filename(stderr, space->name);
 
2137
                                fprintf(stderr, ",\n"
 
2138
                                        "InnoDB: but there are %lu pending"
 
2139
                                        " ibuf merges on it.\n"
 
2140
                                        "InnoDB: Loop %lu.\n",
 
2141
                                        (ulong) space->n_pending_ibuf_merges,
 
2142
                                        (ulong) count);
 
2143
                        }
 
2144
 
 
2145
                        mutex_exit(&fil_system->mutex);
 
2146
 
 
2147
                        os_thread_sleep(20000);
 
2148
                        count++;
 
2149
 
 
2150
                        goto stop_ibuf_merges;
 
2151
                }
 
2152
        }
 
2153
 
 
2154
        mutex_exit(&fil_system->mutex);
 
2155
        count = 0;
 
2156
 
 
2157
try_again:
 
2158
        mutex_enter(&fil_system->mutex);
 
2159
 
 
2160
        space = fil_space_get_by_id(id);
 
2161
 
 
2162
        if (space == NULL) {
 
2163
                ut_print_timestamp(stderr);
 
2164
                fprintf(stderr,
 
2165
                        "  InnoDB: Error: cannot delete tablespace %lu\n"
 
2166
                        "InnoDB: because it is not found in the"
 
2167
                        " tablespace memory cache.\n",
 
2168
                        (ulong) id);
 
2169
 
 
2170
                mutex_exit(&fil_system->mutex);
 
2171
 
 
2172
                return(FALSE);
 
2173
        }
 
2174
 
 
2175
        ut_a(space);
 
2176
        ut_a(space->n_pending_ibuf_merges == 0);
 
2177
 
 
2178
        space->is_being_deleted = TRUE;
 
2179
 
 
2180
        ut_a(UT_LIST_GET_LEN(space->chain) == 1);
 
2181
        node = UT_LIST_GET_FIRST(space->chain);
 
2182
 
 
2183
        if (space->n_pending_flushes > 0 || node->n_pending > 0) {
 
2184
                if (count > 1000) {
 
2185
                        ut_print_timestamp(stderr);
 
2186
                        fputs("  InnoDB: Warning: trying to"
 
2187
                              " delete tablespace ", stderr);
 
2188
                        ut_print_filename(stderr, space->name);
 
2189
                        fprintf(stderr, ",\n"
 
2190
                                "InnoDB: but there are %lu flushes"
 
2191
                                " and %lu pending i/o's on it\n"
 
2192
                                "InnoDB: Loop %lu.\n",
 
2193
                                (ulong) space->n_pending_flushes,
 
2194
                                (ulong) node->n_pending,
 
2195
                                (ulong) count);
 
2196
                }
 
2197
                mutex_exit(&fil_system->mutex);
 
2198
                os_thread_sleep(20000);
 
2199
 
 
2200
                count++;
 
2201
 
 
2202
                goto try_again;
 
2203
        }
 
2204
 
 
2205
        path = mem_strdup(space->name);
 
2206
 
 
2207
        mutex_exit(&fil_system->mutex);
 
2208
#ifndef UNIV_HOTBACKUP
 
2209
        /* Invalidate in the buffer pool all pages belonging to the
 
2210
        tablespace. Since we have set space->is_being_deleted = TRUE, readahead
 
2211
        or ibuf merge can no longer read more pages of this tablespace to the
 
2212
        buffer pool. Thus we can clean the tablespace out of the buffer pool
 
2213
        completely and permanently. The flag is_being_deleted also prevents
 
2214
        fil_flush() from being applied to this tablespace. */
 
2215
 
 
2216
        buf_LRU_invalidate_tablespace(id);
 
2217
#endif
 
2218
        /* printf("Deleting tablespace %s id %lu\n", space->name, id); */
 
2219
 
 
2220
        success = fil_space_free(id);
 
2221
 
 
2222
        if (success) {
 
2223
                success = os_file_delete(path);
 
2224
 
 
2225
                if (!success) {
 
2226
                        success = os_file_delete_if_exists(path);
 
2227
                }
 
2228
        }
 
2229
 
 
2230
        if (success) {
 
2231
#ifndef UNIV_HOTBACKUP
 
2232
                /* Write a log record about the deletion of the .ibd
 
2233
                file, so that ibbackup can replay it in the
 
2234
                --apply-log phase. We use a dummy mtr and the familiar
 
2235
                log write mechanism. */
 
2236
                mtr_t           mtr;
 
2237
 
 
2238
                /* When replaying the operation in ibbackup, do not try
 
2239
                to write any log record */
 
2240
                mtr_start(&mtr);
 
2241
 
 
2242
                fil_op_write_log(MLOG_FILE_DELETE, id, 0, 0, path, NULL, &mtr);
 
2243
                mtr_commit(&mtr);
 
2244
#endif
 
2245
                mem_free(path);
 
2246
 
 
2247
                return(TRUE);
 
2248
        }
 
2249
 
 
2250
        mem_free(path);
 
2251
 
 
2252
        return(FALSE);
 
2253
}
 
2254
 
 
2255
#ifndef UNIV_HOTBACKUP
 
2256
/*******************************************************************//**
 
2257
Discards a single-table tablespace. The tablespace must be cached in the
 
2258
memory cache. Discarding is like deleting a tablespace, but
 
2259
1) we do not drop the table from the data dictionary;
 
2260
2) we remove all insert buffer entries for the tablespace immediately; in DROP
 
2261
TABLE they are only removed gradually in the background;
 
2262
3) when the user does IMPORT TABLESPACE, the tablespace will have the same id
 
2263
as it originally had.
 
2264
@return TRUE if success */
 
2265
UNIV_INTERN
 
2266
ibool
 
2267
fil_discard_tablespace(
 
2268
/*===================*/
 
2269
        ulint   id)     /*!< in: space id */
 
2270
{
 
2271
        ibool   success;
 
2272
 
 
2273
        success = fil_delete_tablespace(id);
 
2274
 
 
2275
        if (!success) {
 
2276
                fprintf(stderr,
 
2277
                        "InnoDB: Warning: cannot delete tablespace %lu"
 
2278
                        " in DISCARD TABLESPACE.\n"
 
2279
                        "InnoDB: But let us remove the"
 
2280
                        " insert buffer entries for this tablespace.\n",
 
2281
                        (ulong) id);
 
2282
        }
 
2283
 
 
2284
        /* Remove all insert buffer entries for the tablespace */
 
2285
 
 
2286
        ibuf_delete_for_discarded_space(id);
 
2287
 
 
2288
        return(success);
 
2289
}
 
2290
#endif /* !UNIV_HOTBACKUP */
 
2291
 
 
2292
/*******************************************************************//**
 
2293
Renames the memory cache structures of a single-table tablespace.
 
2294
@return TRUE if success */
 
2295
static
 
2296
ibool
 
2297
fil_rename_tablespace_in_mem(
 
2298
/*=========================*/
 
2299
        fil_space_t*    space,  /*!< in: tablespace memory object */
 
2300
        fil_node_t*     node,   /*!< in: file node of that tablespace */
 
2301
        const char*     path)   /*!< in: new name */
 
2302
{
 
2303
        fil_space_t*    space2;
 
2304
        const char*     old_name        = space->name;
 
2305
 
 
2306
        ut_ad(mutex_own(&fil_system->mutex));
 
2307
 
 
2308
        space2 = fil_space_get_by_name(old_name);
 
2309
        if (space != space2) {
 
2310
                fputs("InnoDB: Error: cannot find ", stderr);
 
2311
                ut_print_filename(stderr, old_name);
 
2312
                fputs(" in tablespace memory cache\n", stderr);
 
2313
 
 
2314
                return(FALSE);
 
2315
        }
 
2316
 
 
2317
        space2 = fil_space_get_by_name(path);
 
2318
        if (space2 != NULL) {
 
2319
                fputs("InnoDB: Error: ", stderr);
 
2320
                ut_print_filename(stderr, path);
 
2321
                fputs(" is already in tablespace memory cache\n", stderr);
 
2322
 
 
2323
                return(FALSE);
 
2324
        }
 
2325
 
 
2326
        HASH_DELETE(fil_space_t, name_hash, fil_system->name_hash,
 
2327
                    ut_fold_string(space->name), space);
 
2328
        mem_free(space->name);
 
2329
        mem_free(node->name);
 
2330
 
 
2331
        space->name = mem_strdup(path);
 
2332
        node->name = mem_strdup(path);
 
2333
 
 
2334
        HASH_INSERT(fil_space_t, name_hash, fil_system->name_hash,
 
2335
                    ut_fold_string(path), space);
 
2336
        return(TRUE);
 
2337
}
 
2338
 
 
2339
/*******************************************************************//**
 
2340
Allocates a file name for a single-table tablespace. The string must be freed
 
2341
by caller with mem_free().
 
2342
@return own: file name */
 
2343
static
 
2344
char*
 
2345
fil_make_ibd_name(
 
2346
/*==============*/
 
2347
        const char*     name,           /*!< in: table name or a dir path of a
 
2348
                                        TEMPORARY table */
 
2349
        ibool           is_temp)        /*!< in: TRUE if it is a dir path */
 
2350
{
 
2351
        ulint   namelen         = strlen(name);
 
2352
        ulint   dirlen          = strlen(fil_path_to_mysql_datadir);
 
2353
        char*   filename        = mem_alloc(namelen + dirlen + sizeof "/.ibd");
 
2354
 
 
2355
        if (is_temp) {
 
2356
                memcpy(filename, name, namelen);
 
2357
                memcpy(filename + namelen, ".ibd", sizeof ".ibd");
 
2358
        } else {
 
2359
                memcpy(filename, fil_path_to_mysql_datadir, dirlen);
 
2360
                filename[dirlen] = '/';
 
2361
 
 
2362
                memcpy(filename + dirlen + 1, name, namelen);
 
2363
                memcpy(filename + dirlen + namelen + 1, ".ibd", sizeof ".ibd");
 
2364
        }
 
2365
 
 
2366
        srv_normalize_path_for_win(filename);
 
2367
 
 
2368
        return(filename);
 
2369
}
 
2370
 
 
2371
/*******************************************************************//**
 
2372
Renames a single-table tablespace. The tablespace must be cached in the
 
2373
tablespace memory cache.
 
2374
@return TRUE if success */
 
2375
UNIV_INTERN
 
2376
ibool
 
2377
fil_rename_tablespace(
 
2378
/*==================*/
 
2379
        const char*     old_name,       /*!< in: old table name in the standard
 
2380
                                        databasename/tablename format of
 
2381
                                        InnoDB, or NULL if we do the rename
 
2382
                                        based on the space id only */
 
2383
        ulint           id,             /*!< in: space id */
 
2384
        const char*     new_name)       /*!< in: new table name in the standard
 
2385
                                        databasename/tablename format
 
2386
                                        of InnoDB */
 
2387
{
 
2388
        ibool           success;
 
2389
        fil_space_t*    space;
 
2390
        fil_node_t*     node;
 
2391
        ulint           count           = 0;
 
2392
        char*           path;
 
2393
        ibool           old_name_was_specified          = TRUE;
 
2394
        char*           old_path;
 
2395
 
 
2396
        ut_a(id != 0);
 
2397
 
 
2398
        if (old_name == NULL) {
 
2399
                old_name = "(name not specified)";
 
2400
                old_name_was_specified = FALSE;
 
2401
        }
 
2402
retry:
 
2403
        count++;
 
2404
 
 
2405
        if (count > 1000) {
 
2406
                ut_print_timestamp(stderr);
 
2407
                fputs("  InnoDB: Warning: problems renaming ", stderr);
 
2408
                ut_print_filename(stderr, old_name);
 
2409
                fputs(" to ", stderr);
 
2410
                ut_print_filename(stderr, new_name);
 
2411
                fprintf(stderr, ", %lu iterations\n", (ulong) count);
 
2412
        }
 
2413
 
 
2414
        mutex_enter(&fil_system->mutex);
 
2415
 
 
2416
        space = fil_space_get_by_id(id);
 
2417
 
 
2418
        if (space == NULL) {
 
2419
                fprintf(stderr,
 
2420
                        "InnoDB: Error: cannot find space id %lu"
 
2421
                        " in the tablespace memory cache\n"
 
2422
                        "InnoDB: though the table ", (ulong) id);
 
2423
                ut_print_filename(stderr, old_name);
 
2424
                fputs(" in a rename operation should have that id\n", stderr);
 
2425
                mutex_exit(&fil_system->mutex);
 
2426
 
 
2427
                return(FALSE);
 
2428
        }
 
2429
 
 
2430
        if (count > 25000) {
 
2431
                space->stop_ios = FALSE;
 
2432
                mutex_exit(&fil_system->mutex);
 
2433
 
 
2434
                return(FALSE);
 
2435
        }
 
2436
 
 
2437
        /* We temporarily close the .ibd file because we do not trust that
 
2438
        operating systems can rename an open file. For the closing we have to
 
2439
        wait until there are no pending i/o's or flushes on the file. */
 
2440
 
 
2441
        space->stop_ios = TRUE;
 
2442
 
 
2443
        ut_a(UT_LIST_GET_LEN(space->chain) == 1);
 
2444
        node = UT_LIST_GET_FIRST(space->chain);
 
2445
 
 
2446
        if (node->n_pending > 0 || node->n_pending_flushes > 0) {
 
2447
                /* There are pending i/o's or flushes, sleep for a while and
 
2448
                retry */
 
2449
 
 
2450
                mutex_exit(&fil_system->mutex);
 
2451
 
 
2452
                os_thread_sleep(20000);
 
2453
 
 
2454
                goto retry;
 
2455
 
 
2456
        } else if (node->modification_counter > node->flush_counter) {
 
2457
                /* Flush the space */
 
2458
 
 
2459
                mutex_exit(&fil_system->mutex);
 
2460
 
 
2461
                os_thread_sleep(20000);
 
2462
 
 
2463
                fil_flush(id);
 
2464
 
 
2465
                goto retry;
 
2466
 
 
2467
        } else if (node->open) {
 
2468
                /* Close the file */
 
2469
 
 
2470
                fil_node_close_file(node, fil_system);
 
2471
        }
 
2472
 
 
2473
        /* Check that the old name in the space is right */
 
2474
 
 
2475
        if (old_name_was_specified) {
 
2476
                old_path = fil_make_ibd_name(old_name, FALSE);
 
2477
 
 
2478
                ut_a(strcmp(space->name, old_path) == 0);
 
2479
                ut_a(strcmp(node->name, old_path) == 0);
 
2480
        } else {
 
2481
                old_path = mem_strdup(space->name);
 
2482
        }
 
2483
 
 
2484
        /* Rename the tablespace and the node in the memory cache */
 
2485
        path = fil_make_ibd_name(new_name, FALSE);
 
2486
        success = fil_rename_tablespace_in_mem(space, node, path);
 
2487
 
 
2488
        if (success) {
 
2489
                success = os_file_rename(old_path, path);
 
2490
 
 
2491
                if (!success) {
 
2492
                        /* We have to revert the changes we made
 
2493
                        to the tablespace memory cache */
 
2494
 
 
2495
                        ut_a(fil_rename_tablespace_in_mem(space, node,
 
2496
                                                          old_path));
 
2497
                }
 
2498
        }
 
2499
 
 
2500
        mem_free(path);
 
2501
        mem_free(old_path);
 
2502
 
 
2503
        space->stop_ios = FALSE;
 
2504
 
 
2505
        mutex_exit(&fil_system->mutex);
 
2506
 
 
2507
#ifndef UNIV_HOTBACKUP
 
2508
        if (success) {
 
2509
                mtr_t           mtr;
 
2510
 
 
2511
                mtr_start(&mtr);
 
2512
 
 
2513
                fil_op_write_log(MLOG_FILE_RENAME, id, 0, 0, old_name, new_name,
 
2514
                                 &mtr);
 
2515
                mtr_commit(&mtr);
 
2516
        }
 
2517
#endif
 
2518
        return(success);
 
2519
}
 
2520
 
 
2521
/*******************************************************************//**
 
2522
Creates a new single-table tablespace to a database directory of MySQL.
 
2523
Database directories are under the 'datadir' of MySQL. The datadir is the
 
2524
directory of a running mysqld program. We can refer to it by simply the
 
2525
path '.'. Tables created with CREATE TEMPORARY TABLE we place in the temp
 
2526
dir of the mysqld server.
 
2527
@return DB_SUCCESS or error code */
 
2528
UNIV_INTERN
 
2529
ulint
 
2530
fil_create_new_single_table_tablespace(
 
2531
/*===================================*/
 
2532
        ulint*          space_id,       /*!< in/out: space id; if this is != 0,
 
2533
                                        then this is an input parameter,
 
2534
                                        otherwise output */
 
2535
        const char*     tablename,      /*!< in: the table name in the usual
 
2536
                                        databasename/tablename format
 
2537
                                        of InnoDB, or a dir path to a temp
 
2538
                                        table */
 
2539
        ibool           is_temp,        /*!< in: TRUE if a table created with
 
2540
                                        CREATE TEMPORARY TABLE */
 
2541
        ulint           flags,          /*!< in: tablespace flags */
 
2542
        ulint           size)           /*!< in: the initial size of the
 
2543
                                        tablespace file in pages,
 
2544
                                        must be >= FIL_IBD_FILE_INITIAL_SIZE */
 
2545
{
 
2546
        os_file_t       file;
 
2547
        ibool           ret;
 
2548
        ulint           err;
 
2549
        byte*           buf2;
 
2550
        byte*           page;
 
2551
        ibool           success;
 
2552
        char*           path;
 
2553
 
 
2554
        ut_a(size >= FIL_IBD_FILE_INITIAL_SIZE);
 
2555
        /* The tablespace flags (FSP_SPACE_FLAGS) should be 0 for
 
2556
        ROW_FORMAT=COMPACT (table->flags == DICT_TF_COMPACT) and
 
2557
        ROW_FORMAT=REDUNDANT (table->flags == 0).  For any other
 
2558
        format, the tablespace flags should equal table->flags. */
 
2559
        ut_a(flags != DICT_TF_COMPACT);
 
2560
 
 
2561
        path = fil_make_ibd_name(tablename, is_temp);
 
2562
 
 
2563
        file = os_file_create(path, OS_FILE_CREATE, OS_FILE_NORMAL,
 
2564
                              OS_DATA_FILE, &ret);
 
2565
        if (ret == FALSE) {
 
2566
                ut_print_timestamp(stderr);
 
2567
                fputs("  InnoDB: Error creating file ", stderr);
 
2568
                ut_print_filename(stderr, path);
 
2569
                fputs(".\n", stderr);
 
2570
 
 
2571
                /* The following call will print an error message */
 
2572
 
 
2573
                err = os_file_get_last_error(TRUE);
 
2574
 
 
2575
                if (err == OS_FILE_ALREADY_EXISTS) {
 
2576
                        fputs("InnoDB: The file already exists though"
 
2577
                              " the corresponding table did not\n"
 
2578
                              "InnoDB: exist in the InnoDB data dictionary."
 
2579
                              " Have you moved InnoDB\n"
 
2580
                              "InnoDB: .ibd files around without using the"
 
2581
                              " SQL commands\n"
 
2582
                              "InnoDB: DISCARD TABLESPACE and"
 
2583
                              " IMPORT TABLESPACE, or did\n"
 
2584
                              "InnoDB: mysqld crash in the middle of"
 
2585
                              " CREATE TABLE? You can\n"
 
2586
                              "InnoDB: resolve the problem by"
 
2587
                              " removing the file ", stderr);
 
2588
                        ut_print_filename(stderr, path);
 
2589
                        fputs("\n"
 
2590
                              "InnoDB: under the 'datadir' of MySQL.\n",
 
2591
                              stderr);
 
2592
 
 
2593
                        mem_free(path);
 
2594
                        return(DB_TABLESPACE_ALREADY_EXISTS);
 
2595
                }
 
2596
 
 
2597
                if (err == OS_FILE_DISK_FULL) {
 
2598
 
 
2599
                        mem_free(path);
 
2600
                        return(DB_OUT_OF_FILE_SPACE);
 
2601
                }
 
2602
 
 
2603
                mem_free(path);
 
2604
                return(DB_ERROR);
 
2605
        }
 
2606
 
 
2607
        buf2 = ut_malloc(3 * UNIV_PAGE_SIZE);
 
2608
        /* Align the memory for file i/o if we might have O_DIRECT set */
 
2609
        page = ut_align(buf2, UNIV_PAGE_SIZE);
 
2610
 
 
2611
        ret = os_file_set_size(path, file, size * UNIV_PAGE_SIZE, 0);
 
2612
 
 
2613
        if (!ret) {
 
2614
                ut_free(buf2);
 
2615
                os_file_close(file);
 
2616
                os_file_delete(path);
 
2617
 
 
2618
                mem_free(path);
 
2619
                return(DB_OUT_OF_FILE_SPACE);
 
2620
        }
 
2621
 
 
2622
        if (*space_id == 0) {
 
2623
                *space_id = fil_assign_new_space_id();
 
2624
        }
 
2625
 
 
2626
        /* printf("Creating tablespace %s id %lu\n", path, *space_id); */
 
2627
 
 
2628
        if (*space_id == ULINT_UNDEFINED) {
 
2629
                ut_free(buf2);
 
2630
error_exit:
 
2631
                os_file_close(file);
 
2632
error_exit2:
 
2633
                os_file_delete(path);
 
2634
 
 
2635
                mem_free(path);
 
2636
                return(DB_ERROR);
 
2637
        }
 
2638
 
 
2639
        /* We have to write the space id to the file immediately and flush the
 
2640
        file to disk. This is because in crash recovery we must be aware what
 
2641
        tablespaces exist and what are their space id's, so that we can apply
 
2642
        the log records to the right file. It may take quite a while until
 
2643
        buffer pool flush algorithms write anything to the file and flush it to
 
2644
        disk. If we would not write here anything, the file would be filled
 
2645
        with zeros from the call of os_file_set_size(), until a buffer pool
 
2646
        flush would write to it. */
 
2647
 
 
2648
        memset(page, '\0', UNIV_PAGE_SIZE);
 
2649
 
 
2650
        fsp_header_init_fields(page, *space_id, flags);
 
2651
        mach_write_to_4(page + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID, *space_id);
 
2652
 
 
2653
        if (!(flags & DICT_TF_ZSSIZE_MASK)) {
 
2654
                buf_flush_init_for_writing(page, NULL, 0);
 
2655
                ret = os_file_write(path, file, page, 0, 0, UNIV_PAGE_SIZE);
 
2656
        } else {
 
2657
                page_zip_des_t  page_zip;
 
2658
                ulint           zip_size;
 
2659
 
 
2660
                zip_size = ((PAGE_ZIP_MIN_SIZE >> 1)
 
2661
                            << ((flags & DICT_TF_ZSSIZE_MASK)
 
2662
                                >> DICT_TF_ZSSIZE_SHIFT));
 
2663
 
 
2664
                page_zip_set_size(&page_zip, zip_size);
 
2665
                page_zip.data = page + UNIV_PAGE_SIZE;
 
2666
#ifdef UNIV_DEBUG
 
2667
                page_zip.m_start =
 
2668
#endif /* UNIV_DEBUG */
 
2669
                        page_zip.m_end = page_zip.m_nonempty =
 
2670
                        page_zip.n_blobs = 0;
 
2671
                buf_flush_init_for_writing(page, &page_zip, 0);
 
2672
                ret = os_file_write(path, file, page_zip.data, 0, 0, zip_size);
 
2673
        }
 
2674
 
 
2675
        ut_free(buf2);
 
2676
 
 
2677
        if (!ret) {
 
2678
                fputs("InnoDB: Error: could not write the first page"
 
2679
                      " to tablespace ", stderr);
 
2680
                ut_print_filename(stderr, path);
 
2681
                putc('\n', stderr);
 
2682
                goto error_exit;
 
2683
        }
 
2684
 
 
2685
        ret = os_file_flush(file);
 
2686
 
 
2687
        if (!ret) {
 
2688
                fputs("InnoDB: Error: file flush of tablespace ", stderr);
 
2689
                ut_print_filename(stderr, path);
 
2690
                fputs(" failed\n", stderr);
 
2691
                goto error_exit;
 
2692
        }
 
2693
 
 
2694
        os_file_close(file);
 
2695
 
 
2696
        if (*space_id == ULINT_UNDEFINED) {
 
2697
                goto error_exit2;
 
2698
        }
 
2699
 
 
2700
        success = fil_space_create(path, *space_id, flags, FIL_TABLESPACE);
 
2701
 
 
2702
        if (!success) {
 
2703
                goto error_exit2;
 
2704
        }
 
2705
 
 
2706
        fil_node_create(path, size, *space_id, FALSE);
 
2707
 
 
2708
#ifndef UNIV_HOTBACKUP
 
2709
        {
 
2710
                mtr_t           mtr;
 
2711
 
 
2712
                mtr_start(&mtr);
 
2713
 
 
2714
                fil_op_write_log(flags
 
2715
                                 ? MLOG_FILE_CREATE2
 
2716
                                 : MLOG_FILE_CREATE,
 
2717
                                 *space_id,
 
2718
                                 is_temp ? MLOG_FILE_FLAG_TEMP : 0,
 
2719
                                 flags,
 
2720
                                 tablename, NULL, &mtr);
 
2721
 
 
2722
                mtr_commit(&mtr);
 
2723
        }
 
2724
#endif
 
2725
        mem_free(path);
 
2726
        return(DB_SUCCESS);
 
2727
}
 
2728
 
 
2729
#ifndef UNIV_HOTBACKUP
 
2730
/********************************************************************//**
 
2731
It is possible, though very improbable, that the lsn's in the tablespace to be
 
2732
imported have risen above the current system lsn, if a lengthy purge, ibuf
 
2733
merge, or rollback was performed on a backup taken with ibbackup. If that is
 
2734
the case, reset page lsn's in the file. We assume that mysqld was shut down
 
2735
after it performed these cleanup operations on the .ibd file, so that it at
 
2736
the shutdown stamped the latest lsn to the FIL_PAGE_FILE_FLUSH_LSN in the
 
2737
first page of the .ibd file, and we can determine whether we need to reset the
 
2738
lsn's just by looking at that flush lsn.
 
2739
@return TRUE if success */
 
2740
UNIV_INTERN
 
2741
ibool
 
2742
fil_reset_too_high_lsns(
 
2743
/*====================*/
 
2744
        const char*     name,           /*!< in: table name in the
 
2745
                                        databasename/tablename format */
 
2746
        ib_uint64_t     current_lsn)    /*!< in: reset lsn's if the lsn stamped
 
2747
                                        to FIL_PAGE_FILE_FLUSH_LSN in the
 
2748
                                        first page is too high */
 
2749
{
 
2750
        os_file_t       file;
 
2751
        char*           filepath;
 
2752
        byte*           page;
 
2753
        byte*           buf2;
 
2754
        ib_uint64_t     flush_lsn;
 
2755
        ulint           space_id;
 
2756
        ib_int64_t      file_size;
 
2757
        ib_int64_t      offset;
 
2758
        ulint           zip_size;
 
2759
        ibool           success;
 
2760
 
 
2761
        filepath = fil_make_ibd_name(name, FALSE);
 
2762
 
 
2763
        file = os_file_create_simple_no_error_handling(
 
2764
                filepath, OS_FILE_OPEN, OS_FILE_READ_WRITE, &success);
 
2765
        if (!success) {
 
2766
                /* The following call prints an error message */
 
2767
                os_file_get_last_error(TRUE);
 
2768
 
 
2769
                ut_print_timestamp(stderr);
 
2770
 
 
2771
                fputs("  InnoDB: Error: trying to open a table,"
 
2772
                      " but could not\n"
 
2773
                      "InnoDB: open the tablespace file ", stderr);
 
2774
                ut_print_filename(stderr, filepath);
 
2775
                fputs("!\n", stderr);
 
2776
                mem_free(filepath);
 
2777
 
 
2778
                return(FALSE);
 
2779
        }
 
2780
 
 
2781
        /* Read the first page of the tablespace */
 
2782
 
 
2783
        buf2 = ut_malloc(3 * UNIV_PAGE_SIZE);
 
2784
        /* Align the memory for file i/o if we might have O_DIRECT set */
 
2785
        page = ut_align(buf2, UNIV_PAGE_SIZE);
 
2786
 
 
2787
        success = os_file_read(file, page, 0, 0, UNIV_PAGE_SIZE);
 
2788
        if (!success) {
 
2789
 
 
2790
                goto func_exit;
 
2791
        }
 
2792
 
 
2793
        /* We have to read the file flush lsn from the header of the file */
 
2794
 
 
2795
        flush_lsn = mach_read_ull(page + FIL_PAGE_FILE_FLUSH_LSN);
 
2796
 
 
2797
        if (current_lsn >= flush_lsn) {
 
2798
                /* Ok */
 
2799
                success = TRUE;
 
2800
 
 
2801
                goto func_exit;
 
2802
        }
 
2803
 
 
2804
        space_id = fsp_header_get_space_id(page);
 
2805
        zip_size = fsp_header_get_zip_size(page);
 
2806
 
 
2807
        ut_print_timestamp(stderr);
 
2808
        fprintf(stderr,
 
2809
                "  InnoDB: Flush lsn in the tablespace file %lu"
 
2810
                " to be imported\n"
 
2811
                "InnoDB: is %"PRIu64", which exceeds current"
 
2812
                " system lsn %"PRIu64".\n"
 
2813
                "InnoDB: We reset the lsn's in the file ",
 
2814
                (ulong) space_id,
 
2815
                flush_lsn, current_lsn);
 
2816
        ut_print_filename(stderr, filepath);
 
2817
        fputs(".\n", stderr);
 
2818
 
 
2819
        ut_a(ut_is_2pow(zip_size));
 
2820
        ut_a(zip_size <= UNIV_PAGE_SIZE);
 
2821
 
 
2822
        /* Loop through all the pages in the tablespace and reset the lsn and
 
2823
        the page checksum if necessary */
 
2824
 
 
2825
        file_size = os_file_get_size_as_iblonglong(file);
 
2826
 
 
2827
        for (offset = 0; offset < file_size;
 
2828
             offset += zip_size ? zip_size : UNIV_PAGE_SIZE) {
 
2829
                success = os_file_read(file, page,
 
2830
                                       (ulint)(offset & 0xFFFFFFFFUL),
 
2831
                                       (ulint)(offset >> 32),
 
2832
                                       zip_size ? zip_size : UNIV_PAGE_SIZE);
 
2833
                if (!success) {
 
2834
 
 
2835
                        goto func_exit;
 
2836
                }
 
2837
                if (mach_read_ull(page + FIL_PAGE_LSN) > current_lsn) {
 
2838
                        /* We have to reset the lsn */
 
2839
 
 
2840
                        if (zip_size) {
 
2841
                                memcpy(page + UNIV_PAGE_SIZE, page, zip_size);
 
2842
                                buf_flush_init_for_writing(
 
2843
                                        page, page + UNIV_PAGE_SIZE,
 
2844
                                        current_lsn);
 
2845
                        } else {
 
2846
                                buf_flush_init_for_writing(
 
2847
                                        page, NULL, current_lsn);
 
2848
                        }
 
2849
                        success = os_file_write(filepath, file, page,
 
2850
                                                (ulint)(offset & 0xFFFFFFFFUL),
 
2851
                                                (ulint)(offset >> 32),
 
2852
                                                zip_size
 
2853
                                                ? zip_size
 
2854
                                                : UNIV_PAGE_SIZE);
 
2855
                        if (!success) {
 
2856
 
 
2857
                                goto func_exit;
 
2858
                        }
 
2859
                }
 
2860
        }
 
2861
 
 
2862
        success = os_file_flush(file);
 
2863
        if (!success) {
 
2864
 
 
2865
                goto func_exit;
 
2866
        }
 
2867
 
 
2868
        /* We now update the flush_lsn stamp at the start of the file */
 
2869
        success = os_file_read(file, page, 0, 0,
 
2870
                               zip_size ? zip_size : UNIV_PAGE_SIZE);
 
2871
        if (!success) {
 
2872
 
 
2873
                goto func_exit;
 
2874
        }
 
2875
 
 
2876
        mach_write_ull(page + FIL_PAGE_FILE_FLUSH_LSN, current_lsn);
 
2877
 
 
2878
        success = os_file_write(filepath, file, page, 0, 0,
 
2879
                                zip_size ? zip_size : UNIV_PAGE_SIZE);
 
2880
        if (!success) {
 
2881
 
 
2882
                goto func_exit;
 
2883
        }
 
2884
        success = os_file_flush(file);
 
2885
func_exit:
 
2886
        os_file_close(file);
 
2887
        ut_free(buf2);
 
2888
        mem_free(filepath);
 
2889
 
 
2890
        return(success);
 
2891
}
 
2892
 
 
2893
/********************************************************************//**
 
2894
Tries to open a single-table tablespace and optionally checks the space id is
 
2895
right in it. If does not succeed, prints an error message to the .err log. This
 
2896
function is used to open a tablespace when we start up mysqld, and also in
 
2897
IMPORT TABLESPACE.
 
2898
NOTE that we assume this operation is used either at the database startup
 
2899
or under the protection of the dictionary mutex, so that two users cannot
 
2900
race here. This operation does not leave the file associated with the
 
2901
tablespace open, but closes it after we have looked at the space id in it.
 
2902
@return TRUE if success */
 
2903
UNIV_INTERN
 
2904
ibool
 
2905
fil_open_single_table_tablespace(
 
2906
/*=============================*/
 
2907
        ibool           check_space_id, /*!< in: should we check that the space
 
2908
                                        id in the file is right; we assume
 
2909
                                        that this function runs much faster
 
2910
                                        if no check is made, since accessing
 
2911
                                        the file inode probably is much
 
2912
                                        faster (the OS caches them) than
 
2913
                                        accessing the first page of the file */
 
2914
        ulint           id,             /*!< in: space id */
 
2915
        ulint           flags,          /*!< in: tablespace flags */
 
2916
        const char*     name)           /*!< in: table name in the
 
2917
                                        databasename/tablename format */
 
2918
{
 
2919
        os_file_t       file;
 
2920
        char*           filepath;
 
2921
        ibool           success;
 
2922
        byte*           buf2;
 
2923
        byte*           page;
 
2924
        ulint           space_id;
 
2925
        ulint           space_flags;
 
2926
        ibool           ret             = TRUE;
 
2927
 
 
2928
        filepath = fil_make_ibd_name(name, FALSE);
 
2929
 
 
2930
        /* The tablespace flags (FSP_SPACE_FLAGS) should be 0 for
 
2931
        ROW_FORMAT=COMPACT (table->flags == DICT_TF_COMPACT) and
 
2932
        ROW_FORMAT=REDUNDANT (table->flags == 0).  For any other
 
2933
        format, the tablespace flags should equal table->flags. */
 
2934
        ut_a(flags != DICT_TF_COMPACT);
 
2935
 
 
2936
        file = os_file_create_simple_no_error_handling(
 
2937
                filepath, OS_FILE_OPEN, OS_FILE_READ_ONLY, &success);
 
2938
        if (!success) {
 
2939
                /* The following call prints an error message */
 
2940
                os_file_get_last_error(TRUE);
 
2941
 
 
2942
                ut_print_timestamp(stderr);
 
2943
 
 
2944
                fputs("  InnoDB: Error: trying to open a table,"
 
2945
                      " but could not\n"
 
2946
                      "InnoDB: open the tablespace file ", stderr);
 
2947
                ut_print_filename(stderr, filepath);
 
2948
                fputs("!\n"
 
2949
                      "InnoDB: Have you moved InnoDB .ibd files around"
 
2950
                      " without using the\n"
 
2951
                      "InnoDB: commands DISCARD TABLESPACE and"
 
2952
                      " IMPORT TABLESPACE?\n"
 
2953
                      "InnoDB: It is also possible that this is"
 
2954
                      " a temporary table #sql...,\n"
 
2955
                      "InnoDB: and MySQL removed the .ibd file for this.\n"
 
2956
                      "InnoDB: Please refer to\n"
 
2957
                      "InnoDB: " REFMAN "innodb-troubleshooting-datadict.html\n"
 
2958
                      "InnoDB: for how to resolve the issue.\n", stderr);
 
2959
 
 
2960
                mem_free(filepath);
 
2961
 
 
2962
                return(FALSE);
 
2963
        }
 
2964
 
 
2965
        if (!check_space_id) {
 
2966
                space_id = id;
 
2967
 
 
2968
                goto skip_check;
 
2969
        }
 
2970
 
 
2971
        /* Read the first page of the tablespace */
 
2972
 
 
2973
        buf2 = ut_malloc(2 * UNIV_PAGE_SIZE);
 
2974
        /* Align the memory for file i/o if we might have O_DIRECT set */
 
2975
        page = ut_align(buf2, UNIV_PAGE_SIZE);
 
2976
 
 
2977
        success = os_file_read(file, page, 0, 0, UNIV_PAGE_SIZE);
 
2978
 
 
2979
        /* We have to read the tablespace id and flags from the file. */
 
2980
 
 
2981
        space_id = fsp_header_get_space_id(page);
 
2982
        space_flags = fsp_header_get_flags(page);
 
2983
 
 
2984
        ut_free(buf2);
 
2985
 
 
2986
        if (UNIV_UNLIKELY(space_id != id || space_flags != flags)) {
 
2987
                ut_print_timestamp(stderr);
 
2988
 
 
2989
                fputs("  InnoDB: Error: tablespace id and flags in file ",
 
2990
                      stderr);
 
2991
                ut_print_filename(stderr, filepath);
 
2992
                fprintf(stderr, " are %lu and %lu, but in the InnoDB\n"
 
2993
                        "InnoDB: data dictionary they are %lu and %lu.\n"
 
2994
                        "InnoDB: Have you moved InnoDB .ibd files"
 
2995
                        " around without using the\n"
 
2996
                        "InnoDB: commands DISCARD TABLESPACE and"
 
2997
                        " IMPORT TABLESPACE?\n"
 
2998
                        "InnoDB: Please refer to\n"
 
2999
                        "InnoDB: " REFMAN "innodb-troubleshooting-datadict.html\n"
 
3000
                        "InnoDB: for how to resolve the issue.\n",
 
3001
                        (ulong) space_id, (ulong) space_flags,
 
3002
                        (ulong) id, (ulong) flags);
 
3003
 
 
3004
                ret = FALSE;
 
3005
 
 
3006
                goto func_exit;
 
3007
        }
 
3008
 
 
3009
skip_check:
 
3010
        success = fil_space_create(filepath, space_id, flags, FIL_TABLESPACE);
 
3011
 
 
3012
        if (!success) {
 
3013
                goto func_exit;
 
3014
        }
 
3015
 
 
3016
        /* We do not measure the size of the file, that is why we pass the 0
 
3017
        below */
 
3018
 
 
3019
        fil_node_create(filepath, 0, space_id, FALSE);
 
3020
func_exit:
 
3021
        os_file_close(file);
 
3022
        mem_free(filepath);
 
3023
 
 
3024
        return(ret);
 
3025
}
 
3026
#endif /* !UNIV_HOTBACKUP */
 
3027
 
 
3028
#ifdef UNIV_HOTBACKUP
 
3029
/*******************************************************************//**
 
3030
Allocates a file name for an old version of a single-table tablespace.
 
3031
The string must be freed by caller with mem_free()!
 
3032
@return own: file name */
 
3033
static
 
3034
char*
 
3035
fil_make_ibbackup_old_name(
 
3036
/*=======================*/
 
3037
        const char*     name)           /*!< in: original file name */
 
3038
{
 
3039
        static const char suffix[] = "_ibbackup_old_vers_";
 
3040
        ulint   len     = strlen(name);
 
3041
        char*   path    = mem_alloc(len + (15 + sizeof suffix));
 
3042
 
 
3043
        memcpy(path, name, len);
 
3044
        memcpy(path + len, suffix, (sizeof suffix) - 1);
 
3045
        ut_sprintf_timestamp_without_extra_chars(path + len + sizeof suffix);
 
3046
        return(path);
 
3047
}
 
3048
#endif /* UNIV_HOTBACKUP */
 
3049
 
 
3050
/********************************************************************//**
 
3051
Opens an .ibd file and adds the associated single-table tablespace to the
 
3052
InnoDB fil0fil.c data structures. */
 
3053
static
 
3054
void
 
3055
fil_load_single_table_tablespace(
 
3056
/*=============================*/
 
3057
        const char*     dbname,         /*!< in: database name */
 
3058
        const char*     filename)       /*!< in: file name (not a path),
 
3059
                                        including the .ibd extension */
 
3060
{
 
3061
        os_file_t       file;
 
3062
        char*           filepath;
 
3063
        ibool           success;
 
3064
        byte*           buf2;
 
3065
        byte*           page;
 
3066
        ulint           space_id;
 
3067
        ulint           flags;
 
3068
        ulint           size_low;
 
3069
        ulint           size_high;
 
3070
        ib_int64_t      size;
 
3071
#ifdef UNIV_HOTBACKUP
 
3072
        fil_space_t*    space;
 
3073
#endif
 
3074
        filepath = mem_alloc(strlen(dbname) + strlen(filename)
 
3075
                             + strlen(fil_path_to_mysql_datadir) + 3);
 
3076
 
 
3077
        sprintf(filepath, "%s/%s/%s", fil_path_to_mysql_datadir, dbname,
 
3078
                filename);
 
3079
        srv_normalize_path_for_win(filepath);
 
3080
#ifdef __WIN__
 
3081
# ifndef UNIV_HOTBACKUP
 
3082
        /* If lower_case_table_names is 0 or 2, then MySQL allows database
 
3083
        directory names with upper case letters. On Windows, all table and
 
3084
        database names in InnoDB are internally always in lower case. Put the
 
3085
        file path to lower case, so that we are consistent with InnoDB's
 
3086
        internal data dictionary. */
 
3087
 
 
3088
        dict_casedn_str(filepath);
 
3089
# endif /* !UNIV_HOTBACKUP */
 
3090
#endif
 
3091
        file = os_file_create_simple_no_error_handling(
 
3092
                filepath, OS_FILE_OPEN, OS_FILE_READ_ONLY, &success);
 
3093
        if (!success) {
 
3094
                /* The following call prints an error message */
 
3095
                os_file_get_last_error(TRUE);
 
3096
 
 
3097
                fprintf(stderr,
 
3098
                        "InnoDB: Error: could not open single-table tablespace"
 
3099
                        " file\n"
 
3100
                        "InnoDB: %s!\n"
 
3101
                        "InnoDB: We do not continue the crash recovery,"
 
3102
                        " because the table may become\n"
 
3103
                        "InnoDB: corrupt if we cannot apply the log records"
 
3104
                        " in the InnoDB log to it.\n"
 
3105
                        "InnoDB: To fix the problem and start mysqld:\n"
 
3106
                        "InnoDB: 1) If there is a permission problem"
 
3107
                        " in the file and mysqld cannot\n"
 
3108
                        "InnoDB: open the file, you should"
 
3109
                        " modify the permissions.\n"
 
3110
                        "InnoDB: 2) If the table is not needed, or you can"
 
3111
                        " restore it from a backup,\n"
 
3112
                        "InnoDB: then you can remove the .ibd file,"
 
3113
                        " and InnoDB will do a normal\n"
 
3114
                        "InnoDB: crash recovery and ignore that table.\n"
 
3115
                        "InnoDB: 3) If the file system or the"
 
3116
                        " disk is broken, and you cannot remove\n"
 
3117
                        "InnoDB: the .ibd file, you can set"
 
3118
                        " innodb_force_recovery > 0 in my.cnf\n"
 
3119
                        "InnoDB: and force InnoDB to continue crash"
 
3120
                        " recovery here.\n", filepath);
 
3121
 
 
3122
                mem_free(filepath);
 
3123
 
 
3124
                if (srv_force_recovery > 0) {
 
3125
                        fprintf(stderr,
 
3126
                                "InnoDB: innodb_force_recovery"
 
3127
                                " was set to %lu. Continuing crash recovery\n"
 
3128
                                "InnoDB: even though we cannot access"
 
3129
                                " the .ibd file of this table.\n",
 
3130
                                srv_force_recovery);
 
3131
                        return;
 
3132
                }
 
3133
 
 
3134
                exit(1);
 
3135
        }
 
3136
 
 
3137
        success = os_file_get_size(file, &size_low, &size_high);
 
3138
 
 
3139
        if (!success) {
 
3140
                /* The following call prints an error message */
 
3141
                os_file_get_last_error(TRUE);
 
3142
 
 
3143
                fprintf(stderr,
 
3144
                        "InnoDB: Error: could not measure the size"
 
3145
                        " of single-table tablespace file\n"
 
3146
                        "InnoDB: %s!\n"
 
3147
                        "InnoDB: We do not continue crash recovery,"
 
3148
                        " because the table will become\n"
 
3149
                        "InnoDB: corrupt if we cannot apply the log records"
 
3150
                        " in the InnoDB log to it.\n"
 
3151
                        "InnoDB: To fix the problem and start mysqld:\n"
 
3152
                        "InnoDB: 1) If there is a permission problem"
 
3153
                        " in the file and mysqld cannot\n"
 
3154
                        "InnoDB: access the file, you should"
 
3155
                        " modify the permissions.\n"
 
3156
                        "InnoDB: 2) If the table is not needed,"
 
3157
                        " or you can restore it from a backup,\n"
 
3158
                        "InnoDB: then you can remove the .ibd file,"
 
3159
                        " and InnoDB will do a normal\n"
 
3160
                        "InnoDB: crash recovery and ignore that table.\n"
 
3161
                        "InnoDB: 3) If the file system or the disk is broken,"
 
3162
                        " and you cannot remove\n"
 
3163
                        "InnoDB: the .ibd file, you can set"
 
3164
                        " innodb_force_recovery > 0 in my.cnf\n"
 
3165
                        "InnoDB: and force InnoDB to continue"
 
3166
                        " crash recovery here.\n", filepath);
 
3167
 
 
3168
                os_file_close(file);
 
3169
                mem_free(filepath);
 
3170
 
 
3171
                if (srv_force_recovery > 0) {
 
3172
                        fprintf(stderr,
 
3173
                                "InnoDB: innodb_force_recovery"
 
3174
                                " was set to %lu. Continuing crash recovery\n"
 
3175
                                "InnoDB: even though we cannot access"
 
3176
                                " the .ibd file of this table.\n",
 
3177
                                srv_force_recovery);
 
3178
                        return;
 
3179
                }
 
3180
 
 
3181
                exit(1);
 
3182
        }
 
3183
 
 
3184
        /* TODO: What to do in other cases where we cannot access an .ibd
 
3185
        file during a crash recovery? */
 
3186
 
 
3187
        /* Every .ibd file is created >= 4 pages in size. Smaller files
 
3188
        cannot be ok. */
 
3189
 
 
3190
        size = (((ib_int64_t)size_high) << 32) + (ib_int64_t)size_low;
 
3191
#ifndef UNIV_HOTBACKUP
 
3192
        if (size < FIL_IBD_FILE_INITIAL_SIZE * UNIV_PAGE_SIZE) {
 
3193
                fprintf(stderr,
 
3194
                        "InnoDB: Error: the size of single-table tablespace"
 
3195
                        " file %s\n"
 
3196
                        "InnoDB: is only %lu %lu, should be at least %lu!",
 
3197
                        filepath,
 
3198
                        (ulong) size_high,
 
3199
                        (ulong) size_low, (ulong) (4 * UNIV_PAGE_SIZE));
 
3200
                os_file_close(file);
 
3201
                mem_free(filepath);
 
3202
 
 
3203
                return;
 
3204
        }
 
3205
#endif
 
3206
        /* Read the first page of the tablespace if the size big enough */
 
3207
 
 
3208
        buf2 = ut_malloc(2 * UNIV_PAGE_SIZE);
 
3209
        /* Align the memory for file i/o if we might have O_DIRECT set */
 
3210
        page = ut_align(buf2, UNIV_PAGE_SIZE);
 
3211
 
 
3212
        if (size >= FIL_IBD_FILE_INITIAL_SIZE * UNIV_PAGE_SIZE) {
 
3213
                success = os_file_read(file, page, 0, 0, UNIV_PAGE_SIZE);
 
3214
 
 
3215
                /* We have to read the tablespace id from the file */
 
3216
 
 
3217
                space_id = fsp_header_get_space_id(page);
 
3218
                flags = fsp_header_get_flags(page);
 
3219
        } else {
 
3220
                space_id = ULINT_UNDEFINED;
 
3221
                flags = 0;
 
3222
        }
 
3223
 
 
3224
#ifndef UNIV_HOTBACKUP
 
3225
        if (space_id == ULINT_UNDEFINED || space_id == 0) {
 
3226
                fprintf(stderr,
 
3227
                        "InnoDB: Error: tablespace id %lu in file %s"
 
3228
                        " is not sensible\n",
 
3229
                        (ulong) space_id,
 
3230
                        filepath);
 
3231
                goto func_exit;
 
3232
        }
 
3233
#else
 
3234
        if (space_id == ULINT_UNDEFINED || space_id == 0) {
 
3235
                char*   new_path;
 
3236
 
 
3237
                fprintf(stderr,
 
3238
                        "InnoDB: Renaming tablespace %s of id %lu,\n"
 
3239
                        "InnoDB: to %s_ibbackup_old_vers_<timestamp>\n"
 
3240
                        "InnoDB: because its size %lld is too small"
 
3241
                        " (< 4 pages 16 kB each),\n"
 
3242
                        "InnoDB: or the space id in the file header"
 
3243
                        " is not sensible.\n"
 
3244
                        "InnoDB: This can happen in an ibbackup run,"
 
3245
                        " and is not dangerous.\n",
 
3246
                        filepath, space_id, filepath, size);
 
3247
                os_file_close(file);
 
3248
 
 
3249
                new_path = fil_make_ibbackup_old_name(filepath);
 
3250
                ut_a(os_file_rename(filepath, new_path));
 
3251
 
 
3252
                ut_free(buf2);
 
3253
                mem_free(filepath);
 
3254
                mem_free(new_path);
 
3255
 
 
3256
                return;
 
3257
        }
 
3258
 
 
3259
        /* A backup may contain the same space several times, if the space got
 
3260
        renamed at a sensitive time. Since it is enough to have one version of
 
3261
        the space, we rename the file if a space with the same space id
 
3262
        already exists in the tablespace memory cache. We rather rename the
 
3263
        file than delete it, because if there is a bug, we do not want to
 
3264
        destroy valuable data. */
 
3265
 
 
3266
        mutex_enter(&fil_system->mutex);
 
3267
 
 
3268
        space = fil_space_get_by_id(space_id);
 
3269
 
 
3270
        if (space) {
 
3271
                char*   new_path;
 
3272
 
 
3273
                fprintf(stderr,
 
3274
                        "InnoDB: Renaming tablespace %s of id %lu,\n"
 
3275
                        "InnoDB: to %s_ibbackup_old_vers_<timestamp>\n"
 
3276
                        "InnoDB: because space %s with the same id\n"
 
3277
                        "InnoDB: was scanned earlier. This can happen"
 
3278
                        " if you have renamed tables\n"
 
3279
                        "InnoDB: during an ibbackup run.\n",
 
3280
                        filepath, space_id, filepath,
 
3281
                        space->name);
 
3282
                os_file_close(file);
 
3283
 
 
3284
                new_path = fil_make_ibbackup_old_name(filepath);
 
3285
 
 
3286
                mutex_exit(&fil_system->mutex);
 
3287
 
 
3288
                ut_a(os_file_rename(filepath, new_path));
 
3289
 
 
3290
                ut_free(buf2);
 
3291
                mem_free(filepath);
 
3292
                mem_free(new_path);
 
3293
 
 
3294
                return;
 
3295
        }
 
3296
        mutex_exit(&fil_system->mutex);
 
3297
#endif
 
3298
        success = fil_space_create(filepath, space_id, flags, FIL_TABLESPACE);
 
3299
 
 
3300
        if (!success) {
 
3301
 
 
3302
                goto func_exit;
 
3303
        }
 
3304
 
 
3305
        /* We do not use the size information we have about the file, because
 
3306
        the rounding formula for extents and pages is somewhat complex; we
 
3307
        let fil_node_open() do that task. */
 
3308
 
 
3309
        fil_node_create(filepath, 0, space_id, FALSE);
 
3310
func_exit:
 
3311
        os_file_close(file);
 
3312
        ut_free(buf2);
 
3313
        mem_free(filepath);
 
3314
}
 
3315
 
 
3316
/***********************************************************************//**
 
3317
A fault-tolerant function that tries to read the next file name in the
 
3318
directory. We retry 100 times if os_file_readdir_next_file() returns -1. The
 
3319
idea is to read as much good data as we can and jump over bad data.
 
3320
@return 0 if ok, -1 if error even after the retries, 1 if at the end
 
3321
of the directory */
 
3322
static
 
3323
int
 
3324
fil_file_readdir_next_file(
 
3325
/*=======================*/
 
3326
        ulint*          err,    /*!< out: this is set to DB_ERROR if an error
 
3327
                                was encountered, otherwise not changed */
 
3328
        const char*     dirname,/*!< in: directory name or path */
 
3329
        os_file_dir_t   dir,    /*!< in: directory stream */
 
3330
        os_file_stat_t* info)   /*!< in/out: buffer where the info is returned */
 
3331
{
 
3332
        ulint   i;
 
3333
        int     ret;
 
3334
 
 
3335
        for (i = 0; i < 100; i++) {
 
3336
                ret = os_file_readdir_next_file(dirname, dir, info);
 
3337
 
 
3338
                if (ret != -1) {
 
3339
 
 
3340
                        return(ret);
 
3341
                }
 
3342
 
 
3343
                fprintf(stderr,
 
3344
                        "InnoDB: Error: os_file_readdir_next_file()"
 
3345
                        " returned -1 in\n"
 
3346
                        "InnoDB: directory %s\n"
 
3347
                        "InnoDB: Crash recovery may have failed"
 
3348
                        " for some .ibd files!\n", dirname);
 
3349
 
 
3350
                *err = DB_ERROR;
 
3351
        }
 
3352
 
 
3353
        return(-1);
 
3354
}
 
3355
 
 
3356
/********************************************************************//**
 
3357
At the server startup, if we need crash recovery, scans the database
 
3358
directories under the MySQL datadir, looking for .ibd files. Those files are
 
3359
single-table tablespaces. We need to know the space id in each of them so that
 
3360
we know into which file we should look to check the contents of a page stored
 
3361
in the doublewrite buffer, also to know where to apply log records where the
 
3362
space id is != 0.
 
3363
@return DB_SUCCESS or error number */
 
3364
UNIV_INTERN
 
3365
ulint
 
3366
fil_load_single_table_tablespaces(void)
 
3367
/*===================================*/
 
3368
{
 
3369
        int             ret;
 
3370
        char*           dbpath          = NULL;
 
3371
        ulint           dbpath_len      = 100;
 
3372
        os_file_dir_t   dir;
 
3373
        os_file_dir_t   dbdir;
 
3374
        os_file_stat_t  dbinfo;
 
3375
        os_file_stat_t  fileinfo;
 
3376
        ulint           err             = DB_SUCCESS;
 
3377
 
 
3378
        /* The datadir of MySQL is always the default directory of mysqld */
 
3379
 
 
3380
        dir = os_file_opendir(fil_path_to_mysql_datadir, TRUE);
 
3381
 
 
3382
        if (dir == NULL) {
 
3383
 
 
3384
                return(DB_ERROR);
 
3385
        }
 
3386
 
 
3387
        dbpath = mem_alloc(dbpath_len);
 
3388
 
 
3389
        /* Scan all directories under the datadir. They are the database
 
3390
        directories of MySQL. */
 
3391
 
 
3392
        ret = fil_file_readdir_next_file(&err, fil_path_to_mysql_datadir, dir,
 
3393
                                         &dbinfo);
 
3394
        while (ret == 0) {
 
3395
                ulint len;
 
3396
                /* printf("Looking at %s in datadir\n", dbinfo.name); */
 
3397
 
 
3398
                if (dbinfo.type == OS_FILE_TYPE_FILE
 
3399
                    || dbinfo.type == OS_FILE_TYPE_UNKNOWN) {
 
3400
 
 
3401
                        goto next_datadir_item;
 
3402
                }
 
3403
 
 
3404
                /* We found a symlink or a directory; try opening it to see
 
3405
                if a symlink is a directory */
 
3406
 
 
3407
                len = strlen(fil_path_to_mysql_datadir)
 
3408
                        + strlen (dbinfo.name) + 2;
 
3409
                if (len > dbpath_len) {
 
3410
                        dbpath_len = len;
 
3411
 
 
3412
                        if (dbpath) {
 
3413
                                mem_free(dbpath);
 
3414
                        }
 
3415
 
 
3416
                        dbpath = mem_alloc(dbpath_len);
 
3417
                }
 
3418
                sprintf(dbpath, "%s/%s", fil_path_to_mysql_datadir,
 
3419
                        dbinfo.name);
 
3420
                srv_normalize_path_for_win(dbpath);
 
3421
 
 
3422
                dbdir = os_file_opendir(dbpath, FALSE);
 
3423
 
 
3424
                if (dbdir != NULL) {
 
3425
                        /* printf("Opened dir %s\n", dbinfo.name); */
 
3426
 
 
3427
                        /* We found a database directory; loop through it,
 
3428
                        looking for possible .ibd files in it */
 
3429
 
 
3430
                        ret = fil_file_readdir_next_file(&err, dbpath, dbdir,
 
3431
                                                         &fileinfo);
 
3432
                        while (ret == 0) {
 
3433
                                /* printf(
 
3434
                                "     Looking at file %s\n", fileinfo.name); */
 
3435
 
 
3436
                                if (fileinfo.type == OS_FILE_TYPE_DIR) {
 
3437
 
 
3438
                                        goto next_file_item;
 
3439
                                }
 
3440
 
 
3441
                                /* We found a symlink or a file */
 
3442
                                if (strlen(fileinfo.name) > 4
 
3443
                                    && 0 == strcmp(fileinfo.name
 
3444
                                                   + strlen(fileinfo.name) - 4,
 
3445
                                                   ".ibd")) {
 
3446
                                        /* The name ends in .ibd; try opening
 
3447
                                        the file */
 
3448
                                        fil_load_single_table_tablespace(
 
3449
                                                dbinfo.name, fileinfo.name);
 
3450
                                }
 
3451
next_file_item:
 
3452
                                ret = fil_file_readdir_next_file(&err,
 
3453
                                                                 dbpath, dbdir,
 
3454
                                                                 &fileinfo);
 
3455
                        }
 
3456
 
 
3457
                        if (0 != os_file_closedir(dbdir)) {
 
3458
                                fputs("InnoDB: Warning: could not"
 
3459
                                      " close database directory ", stderr);
 
3460
                                ut_print_filename(stderr, dbpath);
 
3461
                                putc('\n', stderr);
 
3462
 
 
3463
                                err = DB_ERROR;
 
3464
                        }
 
3465
                }
 
3466
 
 
3467
next_datadir_item:
 
3468
                ret = fil_file_readdir_next_file(&err,
 
3469
                                                 fil_path_to_mysql_datadir,
 
3470
                                                 dir, &dbinfo);
 
3471
        }
 
3472
 
 
3473
        mem_free(dbpath);
 
3474
 
 
3475
        if (0 != os_file_closedir(dir)) {
 
3476
                fprintf(stderr,
 
3477
                        "InnoDB: Error: could not close MySQL datadir\n");
 
3478
 
 
3479
                return(DB_ERROR);
 
3480
        }
 
3481
 
 
3482
        return(err);
 
3483
}
 
3484
 
 
3485
/********************************************************************//**
 
3486
If we need crash recovery, and we have called
 
3487
fil_load_single_table_tablespaces() and dict_load_single_table_tablespaces(),
 
3488
we can call this function to print an error message of orphaned .ibd files
 
3489
for which there is not a data dictionary entry with a matching table name
 
3490
and space id. */
 
3491
UNIV_INTERN
 
3492
void
 
3493
fil_print_orphaned_tablespaces(void)
 
3494
/*================================*/
 
3495
{
 
3496
        fil_space_t*    space;
 
3497
 
 
3498
        mutex_enter(&fil_system->mutex);
 
3499
 
 
3500
        space = UT_LIST_GET_FIRST(fil_system->space_list);
 
3501
 
 
3502
        while (space) {
 
3503
                if (space->purpose == FIL_TABLESPACE && space->id != 0
 
3504
                    && !space->mark) {
 
3505
                        fputs("InnoDB: Warning: tablespace ", stderr);
 
3506
                        ut_print_filename(stderr, space->name);
 
3507
                        fprintf(stderr, " of id %lu has no matching table in\n"
 
3508
                                "InnoDB: the InnoDB data dictionary.\n",
 
3509
                                (ulong) space->id);
 
3510
                }
 
3511
 
 
3512
                space = UT_LIST_GET_NEXT(space_list, space);
 
3513
        }
 
3514
 
 
3515
        mutex_exit(&fil_system->mutex);
 
3516
}
 
3517
 
 
3518
/*******************************************************************//**
 
3519
Returns TRUE if a single-table tablespace does not exist in the memory cache,
 
3520
or is being deleted there.
 
3521
@return TRUE if does not exist or is being\ deleted */
 
3522
UNIV_INTERN
 
3523
ibool
 
3524
fil_tablespace_deleted_or_being_deleted_in_mem(
 
3525
/*===========================================*/
 
3526
        ulint           id,     /*!< in: space id */
 
3527
        ib_int64_t      version)/*!< in: tablespace_version should be this; if
 
3528
                                you pass -1 as the value of this, then this
 
3529
                                parameter is ignored */
 
3530
{
 
3531
        fil_space_t*    space;
 
3532
 
 
3533
        ut_ad(fil_system);
 
3534
 
 
3535
        mutex_enter(&fil_system->mutex);
 
3536
 
 
3537
        space = fil_space_get_by_id(id);
 
3538
 
 
3539
        if (space == NULL || space->is_being_deleted) {
 
3540
                mutex_exit(&fil_system->mutex);
 
3541
 
 
3542
                return(TRUE);
 
3543
        }
 
3544
 
 
3545
        if (version != ((ib_int64_t)-1)
 
3546
            && space->tablespace_version != version) {
 
3547
                mutex_exit(&fil_system->mutex);
 
3548
 
 
3549
                return(TRUE);
 
3550
        }
 
3551
 
 
3552
        mutex_exit(&fil_system->mutex);
 
3553
 
 
3554
        return(FALSE);
 
3555
}
 
3556
 
 
3557
/*******************************************************************//**
 
3558
Returns TRUE if a single-table tablespace exists in the memory cache.
 
3559
@return TRUE if exists */
 
3560
UNIV_INTERN
 
3561
ibool
 
3562
fil_tablespace_exists_in_mem(
 
3563
/*=========================*/
 
3564
        ulint   id)     /*!< in: space id */
 
3565
{
 
3566
        fil_space_t*    space;
 
3567
 
 
3568
        ut_ad(fil_system);
 
3569
 
 
3570
        mutex_enter(&fil_system->mutex);
 
3571
 
 
3572
        space = fil_space_get_by_id(id);
 
3573
 
 
3574
        mutex_exit(&fil_system->mutex);
 
3575
 
 
3576
        return(space != NULL);
 
3577
}
 
3578
 
 
3579
/*******************************************************************//**
 
3580
Returns TRUE if a matching tablespace exists in the InnoDB tablespace memory
 
3581
cache. Note that if we have not done a crash recovery at the database startup,
 
3582
there may be many tablespaces which are not yet in the memory cache.
 
3583
@return TRUE if a matching tablespace exists in the memory cache */
 
3584
UNIV_INTERN
 
3585
ibool
 
3586
fil_space_for_table_exists_in_mem(
 
3587
/*==============================*/
 
3588
        ulint           id,             /*!< in: space id */
 
3589
        const char*     name,           /*!< in: table name in the standard
 
3590
                                        'databasename/tablename' format or
 
3591
                                        the dir path to a temp table */
 
3592
        ibool           is_temp,        /*!< in: TRUE if created with CREATE
 
3593
                                        TEMPORARY TABLE */
 
3594
        ibool           mark_space,     /*!< in: in crash recovery, at database
 
3595
                                        startup we mark all spaces which have
 
3596
                                        an associated table in the InnoDB
 
3597
                                        data dictionary, so that
 
3598
                                        we can print a warning about orphaned
 
3599
                                        tablespaces */
 
3600
        ibool           print_error_if_does_not_exist)
 
3601
                                        /*!< in: print detailed error
 
3602
                                        information to the .err log if a
 
3603
                                        matching tablespace is not found from
 
3604
                                        memory */
 
3605
{
 
3606
        fil_space_t*    namespace;
 
3607
        fil_space_t*    space;
 
3608
        char*           path;
 
3609
 
 
3610
        ut_ad(fil_system);
 
3611
 
 
3612
        mutex_enter(&fil_system->mutex);
 
3613
 
 
3614
        path = fil_make_ibd_name(name, is_temp);
 
3615
 
 
3616
        /* Look if there is a space with the same id */
 
3617
 
 
3618
        space = fil_space_get_by_id(id);
 
3619
 
 
3620
        /* Look if there is a space with the same name; the name is the
 
3621
        directory path from the datadir to the file */
 
3622
 
 
3623
        namespace = fil_space_get_by_name(path);
 
3624
        if (space && space == namespace) {
 
3625
                /* Found */
 
3626
 
 
3627
                if (mark_space) {
 
3628
                        space->mark = TRUE;
 
3629
                }
 
3630
 
 
3631
                mem_free(path);
 
3632
                mutex_exit(&fil_system->mutex);
 
3633
 
 
3634
                return(TRUE);
 
3635
        }
 
3636
 
 
3637
        if (!print_error_if_does_not_exist) {
 
3638
 
 
3639
                mem_free(path);
 
3640
                mutex_exit(&fil_system->mutex);
 
3641
 
 
3642
                return(FALSE);
 
3643
        }
 
3644
 
 
3645
        if (space == NULL) {
 
3646
                if (namespace == NULL) {
 
3647
                        ut_print_timestamp(stderr);
 
3648
                        fputs("  InnoDB: Error: table ", stderr);
 
3649
                        ut_print_filename(stderr, name);
 
3650
                        fprintf(stderr, "\n"
 
3651
                                "InnoDB: in InnoDB data dictionary"
 
3652
                                " has tablespace id %lu,\n"
 
3653
                                "InnoDB: but tablespace with that id"
 
3654
                                " or name does not exist. Have\n"
 
3655
                                "InnoDB: you deleted or moved .ibd files?\n"
 
3656
                                "InnoDB: This may also be a table created with"
 
3657
                                " CREATE TEMPORARY TABLE\n"
 
3658
                                "InnoDB: whose .ibd and .frm files"
 
3659
                                " MySQL automatically removed, but the\n"
 
3660
                                "InnoDB: table still exists in the"
 
3661
                                " InnoDB internal data dictionary.\n",
 
3662
                                (ulong) id);
 
3663
                } else {
 
3664
                        ut_print_timestamp(stderr);
 
3665
                        fputs("  InnoDB: Error: table ", stderr);
 
3666
                        ut_print_filename(stderr, name);
 
3667
                        fprintf(stderr, "\n"
 
3668
                                "InnoDB: in InnoDB data dictionary has"
 
3669
                                " tablespace id %lu,\n"
 
3670
                                "InnoDB: but a tablespace with that id"
 
3671
                                " does not exist. There is\n"
 
3672
                                "InnoDB: a tablespace of name %s and id %lu,"
 
3673
                                " though. Have\n"
 
3674
                                "InnoDB: you deleted or moved .ibd files?\n",
 
3675
                                (ulong) id, namespace->name,
 
3676
                                (ulong) namespace->id);
 
3677
                }
 
3678
error_exit:
 
3679
                fputs("InnoDB: Please refer to\n"
 
3680
                      "InnoDB: " REFMAN "innodb-troubleshooting-datadict.html\n"
 
3681
                      "InnoDB: for how to resolve the issue.\n", stderr);
 
3682
 
 
3683
                mem_free(path);
 
3684
                mutex_exit(&fil_system->mutex);
 
3685
 
 
3686
                return(FALSE);
 
3687
        }
 
3688
 
 
3689
        if (0 != strcmp(space->name, path)) {
 
3690
                ut_print_timestamp(stderr);
 
3691
                fputs("  InnoDB: Error: table ", stderr);
 
3692
                ut_print_filename(stderr, name);
 
3693
                fprintf(stderr, "\n"
 
3694
                        "InnoDB: in InnoDB data dictionary has"
 
3695
                        " tablespace id %lu,\n"
 
3696
                        "InnoDB: but the tablespace with that id"
 
3697
                        " has name %s.\n"
 
3698
                        "InnoDB: Have you deleted or moved .ibd files?\n",
 
3699
                        (ulong) id, space->name);
 
3700
 
 
3701
                if (namespace != NULL) {
 
3702
                        fputs("InnoDB: There is a tablespace"
 
3703
                              " with the right name\n"
 
3704
                              "InnoDB: ", stderr);
 
3705
                        ut_print_filename(stderr, namespace->name);
 
3706
                        fprintf(stderr, ", but its id is %lu.\n",
 
3707
                                (ulong) namespace->id);
 
3708
                }
 
3709
 
 
3710
                goto error_exit;
 
3711
        }
 
3712
 
 
3713
        mem_free(path);
 
3714
        mutex_exit(&fil_system->mutex);
 
3715
 
 
3716
        return(FALSE);
 
3717
}
 
3718
 
 
3719
/*******************************************************************//**
 
3720
Checks if a single-table tablespace for a given table name exists in the
 
3721
tablespace memory cache.
 
3722
@return space id, ULINT_UNDEFINED if not found */
 
3723
static
 
3724
ulint
 
3725
fil_get_space_id_for_table(
 
3726
/*=======================*/
 
3727
        const char*     name)   /*!< in: table name in the standard
 
3728
                                'databasename/tablename' format */
 
3729
{
 
3730
        fil_space_t*    namespace;
 
3731
        ulint           id              = ULINT_UNDEFINED;
 
3732
        char*           path;
 
3733
 
 
3734
        ut_ad(fil_system);
 
3735
 
 
3736
        mutex_enter(&fil_system->mutex);
 
3737
 
 
3738
        path = fil_make_ibd_name(name, FALSE);
 
3739
 
 
3740
        /* Look if there is a space with the same name; the name is the
 
3741
        directory path to the file */
 
3742
 
 
3743
        namespace = fil_space_get_by_name(path);
 
3744
 
 
3745
        if (namespace) {
 
3746
                id = namespace->id;
 
3747
        }
 
3748
 
 
3749
        mem_free(path);
 
3750
 
 
3751
        mutex_exit(&fil_system->mutex);
 
3752
 
 
3753
        return(id);
 
3754
}
 
3755
 
 
3756
/**********************************************************************//**
 
3757
Tries to extend a data file so that it would accommodate the number of pages
 
3758
given. The tablespace must be cached in the memory cache. If the space is big
 
3759
enough already, does nothing.
 
3760
@return TRUE if success */
 
3761
UNIV_INTERN
 
3762
ibool
 
3763
fil_extend_space_to_desired_size(
 
3764
/*=============================*/
 
3765
        ulint*  actual_size,    /*!< out: size of the space after extension;
 
3766
                                if we ran out of disk space this may be lower
 
3767
                                than the desired size */
 
3768
        ulint   space_id,       /*!< in: space id */
 
3769
        ulint   size_after_extend)/*!< in: desired size in pages after the
 
3770
                                extension; if the current space size is bigger
 
3771
                                than this already, the function does nothing */
 
3772
{
 
3773
        fil_node_t*     node;
 
3774
        fil_space_t*    space;
 
3775
        byte*           buf2;
 
3776
        byte*           buf;
 
3777
        ulint           buf_size;
 
3778
        ulint           start_page_no;
 
3779
        ulint           file_start_page_no;
 
3780
        ulint           offset_high;
 
3781
        ulint           offset_low;
 
3782
        ulint           page_size;
 
3783
        ibool           success         = TRUE;
 
3784
 
 
3785
        fil_mutex_enter_and_prepare_for_io(space_id);
 
3786
 
 
3787
        space = fil_space_get_by_id(space_id);
 
3788
        ut_a(space);
 
3789
 
 
3790
        if (space->size >= size_after_extend) {
 
3791
                /* Space already big enough */
 
3792
 
 
3793
                *actual_size = space->size;
 
3794
 
 
3795
                mutex_exit(&fil_system->mutex);
 
3796
 
 
3797
                return(TRUE);
 
3798
        }
 
3799
 
 
3800
        page_size = dict_table_flags_to_zip_size(space->flags);
 
3801
        if (!page_size) {
 
3802
                page_size = UNIV_PAGE_SIZE;
 
3803
        }
 
3804
 
 
3805
        node = UT_LIST_GET_LAST(space->chain);
 
3806
 
 
3807
        fil_node_prepare_for_io(node, fil_system, space);
 
3808
 
 
3809
        start_page_no = space->size;
 
3810
        file_start_page_no = space->size - node->size;
 
3811
 
 
3812
        /* Extend at most 64 pages at a time */
 
3813
        buf_size = ut_min(64, size_after_extend - start_page_no) * page_size;
 
3814
        buf2 = mem_alloc(buf_size + page_size);
 
3815
        buf = ut_align(buf2, page_size);
 
3816
 
 
3817
        memset(buf, 0, buf_size);
 
3818
 
 
3819
        while (start_page_no < size_after_extend) {
 
3820
                ulint   n_pages = ut_min(buf_size / page_size,
 
3821
                                         size_after_extend - start_page_no);
 
3822
 
 
3823
                offset_high = (start_page_no - file_start_page_no)
 
3824
                        / (4096 * ((1024 * 1024) / page_size));
 
3825
                offset_low  = ((start_page_no - file_start_page_no)
 
3826
                               % (4096 * ((1024 * 1024) / page_size)))
 
3827
                        * page_size;
 
3828
#ifdef UNIV_HOTBACKUP
 
3829
                success = os_file_write(node->name, node->handle, buf,
 
3830
                                        offset_low, offset_high,
 
3831
                                        page_size * n_pages);
 
3832
#else
 
3833
                success = os_aio(OS_FILE_WRITE, OS_AIO_SYNC,
 
3834
                                 node->name, node->handle, buf,
 
3835
                                 offset_low, offset_high,
 
3836
                                 page_size * n_pages,
 
3837
                                 NULL, NULL);
 
3838
#endif
 
3839
                if (success) {
 
3840
                        node->size += n_pages;
 
3841
                        space->size += n_pages;
 
3842
 
 
3843
                        os_has_said_disk_full = FALSE;
 
3844
                } else {
 
3845
                        /* Let us measure the size of the file to determine
 
3846
                        how much we were able to extend it */
 
3847
 
 
3848
                        n_pages = ((ulint)
 
3849
                                   (os_file_get_size_as_iblonglong(
 
3850
                                           node->handle)
 
3851
                                    / page_size)) - node->size;
 
3852
 
 
3853
                        node->size += n_pages;
 
3854
                        space->size += n_pages;
 
3855
 
 
3856
                        break;
 
3857
                }
 
3858
 
 
3859
                start_page_no += n_pages;
 
3860
        }
 
3861
 
 
3862
        mem_free(buf2);
 
3863
 
 
3864
        fil_node_complete_io(node, fil_system, OS_FILE_WRITE);
 
3865
 
 
3866
        *actual_size = space->size;
 
3867
 
 
3868
#ifndef UNIV_HOTBACKUP
 
3869
        if (space_id == 0) {
 
3870
                ulint pages_per_mb = (1024 * 1024) / page_size;
 
3871
 
 
3872
                /* Keep the last data file size info up to date, rounded to
 
3873
                full megabytes */
 
3874
 
 
3875
                srv_data_file_sizes[srv_n_data_files - 1]
 
3876
                        = (node->size / pages_per_mb) * pages_per_mb;
 
3877
        }
 
3878
#endif /* !UNIV_HOTBACKUP */
 
3879
 
 
3880
        /*
 
3881
        printf("Extended %s to %lu, actual size %lu pages\n", space->name,
 
3882
        size_after_extend, *actual_size); */
 
3883
        mutex_exit(&fil_system->mutex);
 
3884
 
 
3885
        fil_flush(space_id);
 
3886
 
 
3887
        return(success);
 
3888
}
 
3889
 
 
3890
#ifdef UNIV_HOTBACKUP
 
3891
/********************************************************************//**
 
3892
Extends all tablespaces to the size stored in the space header. During the
 
3893
ibbackup --apply-log phase we extended the spaces on-demand so that log records
 
3894
could be applied, but that may have left spaces still too small compared to
 
3895
the size stored in the space header. */
 
3896
UNIV_INTERN
 
3897
void
 
3898
fil_extend_tablespaces_to_stored_len(void)
 
3899
/*======================================*/
 
3900
{
 
3901
        fil_space_t*    space;
 
3902
        byte*           buf;
 
3903
        ulint           actual_size;
 
3904
        ulint           size_in_header;
 
3905
        ulint           error;
 
3906
        ibool           success;
 
3907
 
 
3908
        buf = mem_alloc(UNIV_PAGE_SIZE);
 
3909
 
 
3910
        mutex_enter(&fil_system->mutex);
 
3911
 
 
3912
        space = UT_LIST_GET_FIRST(fil_system->space_list);
 
3913
 
 
3914
        while (space) {
 
3915
                ut_a(space->purpose == FIL_TABLESPACE);
 
3916
 
 
3917
                mutex_exit(&fil_system->mutex); /* no need to protect with a
 
3918
                                              mutex, because this is a
 
3919
                                              single-threaded operation */
 
3920
                error = fil_read(TRUE, space->id,
 
3921
                                 dict_table_flags_to_zip_size(space->flags),
 
3922
                                 0, 0, UNIV_PAGE_SIZE, buf, NULL);
 
3923
                ut_a(error == DB_SUCCESS);
 
3924
 
 
3925
                size_in_header = fsp_get_size_low(buf);
 
3926
 
 
3927
                success = fil_extend_space_to_desired_size(
 
3928
                        &actual_size, space->id, size_in_header);
 
3929
                if (!success) {
 
3930
                        fprintf(stderr,
 
3931
                                "InnoDB: Error: could not extend the"
 
3932
                                " tablespace of %s\n"
 
3933
                                "InnoDB: to the size stored in header,"
 
3934
                                " %lu pages;\n"
 
3935
                                "InnoDB: size after extension %lu pages\n"
 
3936
                                "InnoDB: Check that you have free disk space"
 
3937
                                " and retry!\n",
 
3938
                                space->name, size_in_header, actual_size);
 
3939
                        exit(1);
 
3940
                }
 
3941
 
 
3942
                mutex_enter(&fil_system->mutex);
 
3943
 
 
3944
                space = UT_LIST_GET_NEXT(space_list, space);
 
3945
        }
 
3946
 
 
3947
        mutex_exit(&fil_system->mutex);
 
3948
 
 
3949
        mem_free(buf);
 
3950
}
 
3951
#endif
 
3952
 
 
3953
/*========== RESERVE FREE EXTENTS (for a B-tree split, for example) ===*/
 
3954
 
 
3955
/*******************************************************************//**
 
3956
Tries to reserve free extents in a file space.
 
3957
@return TRUE if succeed */
 
3958
UNIV_INTERN
 
3959
ibool
 
3960
fil_space_reserve_free_extents(
 
3961
/*===========================*/
 
3962
        ulint   id,             /*!< in: space id */
 
3963
        ulint   n_free_now,     /*!< in: number of free extents now */
 
3964
        ulint   n_to_reserve)   /*!< in: how many one wants to reserve */
 
3965
{
 
3966
        fil_space_t*    space;
 
3967
        ibool           success;
 
3968
 
 
3969
        ut_ad(fil_system);
 
3970
 
 
3971
        mutex_enter(&fil_system->mutex);
 
3972
 
 
3973
        space = fil_space_get_by_id(id);
 
3974
 
 
3975
        ut_a(space);
 
3976
 
 
3977
        if (space->n_reserved_extents + n_to_reserve > n_free_now) {
 
3978
                success = FALSE;
 
3979
        } else {
 
3980
                space->n_reserved_extents += n_to_reserve;
 
3981
                success = TRUE;
 
3982
        }
 
3983
 
 
3984
        mutex_exit(&fil_system->mutex);
 
3985
 
 
3986
        return(success);
 
3987
}
 
3988
 
 
3989
/*******************************************************************//**
 
3990
Releases free extents in a file space. */
 
3991
UNIV_INTERN
 
3992
void
 
3993
fil_space_release_free_extents(
 
3994
/*===========================*/
 
3995
        ulint   id,             /*!< in: space id */
 
3996
        ulint   n_reserved)     /*!< in: how many one reserved */
 
3997
{
 
3998
        fil_space_t*    space;
 
3999
 
 
4000
        ut_ad(fil_system);
 
4001
 
 
4002
        mutex_enter(&fil_system->mutex);
 
4003
 
 
4004
        space = fil_space_get_by_id(id);
 
4005
 
 
4006
        ut_a(space);
 
4007
        ut_a(space->n_reserved_extents >= n_reserved);
 
4008
 
 
4009
        space->n_reserved_extents -= n_reserved;
 
4010
 
 
4011
        mutex_exit(&fil_system->mutex);
 
4012
}
 
4013
 
 
4014
/*******************************************************************//**
 
4015
Gets the number of reserved extents. If the database is silent, this number
 
4016
should be zero. */
 
4017
UNIV_INTERN
 
4018
ulint
 
4019
fil_space_get_n_reserved_extents(
 
4020
/*=============================*/
 
4021
        ulint   id)             /*!< in: space id */
 
4022
{
 
4023
        fil_space_t*    space;
 
4024
        ulint           n;
 
4025
 
 
4026
        ut_ad(fil_system);
 
4027
 
 
4028
        mutex_enter(&fil_system->mutex);
 
4029
 
 
4030
        space = fil_space_get_by_id(id);
 
4031
 
 
4032
        ut_a(space);
 
4033
 
 
4034
        n = space->n_reserved_extents;
 
4035
 
 
4036
        mutex_exit(&fil_system->mutex);
 
4037
 
 
4038
        return(n);
 
4039
}
 
4040
 
 
4041
/*============================ FILE I/O ================================*/
 
4042
 
 
4043
/********************************************************************//**
 
4044
NOTE: you must call fil_mutex_enter_and_prepare_for_io() first!
 
4045
 
 
4046
Prepares a file node for i/o. Opens the file if it is closed. Updates the
 
4047
pending i/o's field in the node and the system appropriately. Takes the node
 
4048
off the LRU list if it is in the LRU list. The caller must hold the fil_sys
 
4049
mutex. */
 
4050
static
 
4051
void
 
4052
fil_node_prepare_for_io(
 
4053
/*====================*/
 
4054
        fil_node_t*     node,   /*!< in: file node */
 
4055
        fil_system_t*   system, /*!< in: tablespace memory cache */
 
4056
        fil_space_t*    space)  /*!< in: space */
 
4057
{
 
4058
        ut_ad(node && system && space);
 
4059
        ut_ad(mutex_own(&(system->mutex)));
 
4060
 
 
4061
        if (system->n_open > system->max_n_open + 5) {
 
4062
                ut_print_timestamp(stderr);
 
4063
                fprintf(stderr,
 
4064
                        "  InnoDB: Warning: open files %lu"
 
4065
                        " exceeds the limit %lu\n",
 
4066
                        (ulong) system->n_open,
 
4067
                        (ulong) system->max_n_open);
 
4068
        }
 
4069
 
 
4070
        if (node->open == FALSE) {
 
4071
                /* File is closed: open it */
 
4072
                ut_a(node->n_pending == 0);
 
4073
 
 
4074
                fil_node_open_file(node, system, space);
 
4075
        }
 
4076
 
 
4077
        if (node->n_pending == 0 && space->purpose == FIL_TABLESPACE
 
4078
            && space->id != 0) {
 
4079
                /* The node is in the LRU list, remove it */
 
4080
 
 
4081
                ut_a(UT_LIST_GET_LEN(system->LRU) > 0);
 
4082
 
 
4083
                UT_LIST_REMOVE(LRU, system->LRU, node);
 
4084
        }
 
4085
 
 
4086
        node->n_pending++;
 
4087
}
 
4088
 
 
4089
/********************************************************************//**
 
4090
Updates the data structures when an i/o operation finishes. Updates the
 
4091
pending i/o's field in the node appropriately. */
 
4092
static
 
4093
void
 
4094
fil_node_complete_io(
 
4095
/*=================*/
 
4096
        fil_node_t*     node,   /*!< in: file node */
 
4097
        fil_system_t*   system, /*!< in: tablespace memory cache */
 
4098
        ulint           type)   /*!< in: OS_FILE_WRITE or OS_FILE_READ; marks
 
4099
                                the node as modified if
 
4100
                                type == OS_FILE_WRITE */
 
4101
{
 
4102
        ut_ad(node);
 
4103
        ut_ad(system);
 
4104
        ut_ad(mutex_own(&(system->mutex)));
 
4105
 
 
4106
        ut_a(node->n_pending > 0);
 
4107
 
 
4108
        node->n_pending--;
 
4109
 
 
4110
        if (type == OS_FILE_WRITE) {
 
4111
                system->modification_counter++;
 
4112
                node->modification_counter = system->modification_counter;
 
4113
 
 
4114
                if (!node->space->is_in_unflushed_spaces) {
 
4115
 
 
4116
                        node->space->is_in_unflushed_spaces = TRUE;
 
4117
                        UT_LIST_ADD_FIRST(unflushed_spaces,
 
4118
                                          system->unflushed_spaces,
 
4119
                                          node->space);
 
4120
                }
 
4121
        }
 
4122
 
 
4123
        if (node->n_pending == 0 && node->space->purpose == FIL_TABLESPACE
 
4124
            && node->space->id != 0) {
 
4125
                /* The node must be put back to the LRU list */
 
4126
                UT_LIST_ADD_FIRST(LRU, system->LRU, node);
 
4127
        }
 
4128
}
 
4129
 
 
4130
/********************************************************************//**
 
4131
Report information about an invalid page access. */
 
4132
static
 
4133
void
 
4134
fil_report_invalid_page_access(
 
4135
/*===========================*/
 
4136
        ulint           block_offset,   /*!< in: block offset */
 
4137
        ulint           space_id,       /*!< in: space id */
 
4138
        const char*     space_name,     /*!< in: space name */
 
4139
        ulint           byte_offset,    /*!< in: byte offset */
 
4140
        ulint           len,            /*!< in: I/O length */
 
4141
        ulint           type)           /*!< in: I/O type */
 
4142
{
 
4143
        fprintf(stderr,
 
4144
                "InnoDB: Error: trying to access page number %lu"
 
4145
                " in space %lu,\n"
 
4146
                "InnoDB: space name %s,\n"
 
4147
                "InnoDB: which is outside the tablespace bounds.\n"
 
4148
                "InnoDB: Byte offset %lu, len %lu, i/o type %lu.\n"
 
4149
                "InnoDB: If you get this error at mysqld startup,"
 
4150
                " please check that\n"
 
4151
                "InnoDB: your my.cnf matches the ibdata files"
 
4152
                " that you have in the\n"
 
4153
                "InnoDB: MySQL server.\n",
 
4154
                (ulong) block_offset, (ulong) space_id, space_name,
 
4155
                (ulong) byte_offset, (ulong) len, (ulong) type);
 
4156
}
 
4157
 
 
4158
/********************************************************************//**
 
4159
Reads or writes data. This operation is asynchronous (aio).
 
4160
@return DB_SUCCESS, or DB_TABLESPACE_DELETED if we are trying to do
 
4161
i/o on a tablespace which does not exist */
 
4162
UNIV_INTERN
 
4163
ulint
 
4164
fil_io(
 
4165
/*===*/
 
4166
        ulint   type,           /*!< in: OS_FILE_READ or OS_FILE_WRITE,
 
4167
                                ORed to OS_FILE_LOG, if a log i/o
 
4168
                                and ORed to OS_AIO_SIMULATED_WAKE_LATER
 
4169
                                if simulated aio and we want to post a
 
4170
                                batch of i/os; NOTE that a simulated batch
 
4171
                                may introduce hidden chances of deadlocks,
 
4172
                                because i/os are not actually handled until
 
4173
                                all have been posted: use with great
 
4174
                                caution! */
 
4175
        ibool   sync,           /*!< in: TRUE if synchronous aio is desired */
 
4176
        ulint   space_id,       /*!< in: space id */
 
4177
        ulint   zip_size,       /*!< in: compressed page size in bytes;
 
4178
                                0 for uncompressed pages */
 
4179
        ulint   block_offset,   /*!< in: offset in number of blocks */
 
4180
        ulint   byte_offset,    /*!< in: remainder of offset in bytes; in
 
4181
                                aio this must be divisible by the OS block
 
4182
                                size */
 
4183
        ulint   len,            /*!< in: how many bytes to read or write; this
 
4184
                                must not cross a file boundary; in aio this
 
4185
                                must be a block size multiple */
 
4186
        void*   buf,            /*!< in/out: buffer where to store read data
 
4187
                                or from where to write; in aio this must be
 
4188
                                appropriately aligned */
 
4189
        void*   message)        /*!< in: message for aio handler if non-sync
 
4190
                                aio used, else ignored */
 
4191
{
 
4192
        ulint           mode;
 
4193
        fil_space_t*    space;
 
4194
        fil_node_t*     node;
 
4195
        ulint           offset_high;
 
4196
        ulint           offset_low;
 
4197
        ibool           ret;
 
4198
        ulint           is_log;
 
4199
        ulint           wake_later;
 
4200
 
 
4201
        is_log = type & OS_FILE_LOG;
 
4202
        type = type & ~OS_FILE_LOG;
 
4203
 
 
4204
        wake_later = type & OS_AIO_SIMULATED_WAKE_LATER;
 
4205
        type = type & ~OS_AIO_SIMULATED_WAKE_LATER;
 
4206
 
 
4207
        ut_ad(byte_offset < UNIV_PAGE_SIZE);
 
4208
        ut_ad(!zip_size || !byte_offset);
 
4209
        ut_ad(ut_is_2pow(zip_size));
 
4210
        ut_ad(buf);
 
4211
        ut_ad(len > 0);
 
4212
#if (1 << UNIV_PAGE_SIZE_SHIFT) != UNIV_PAGE_SIZE
 
4213
# error "(1 << UNIV_PAGE_SIZE_SHIFT) != UNIV_PAGE_SIZE"
 
4214
#endif
 
4215
        ut_ad(fil_validate());
 
4216
#ifndef UNIV_HOTBACKUP
 
4217
# ifndef UNIV_LOG_DEBUG
 
4218
        /* ibuf bitmap pages must be read in the sync aio mode: */
 
4219
        ut_ad(recv_no_ibuf_operations || (type == OS_FILE_WRITE)
 
4220
              || !ibuf_bitmap_page(zip_size, block_offset)
 
4221
              || sync || is_log);
 
4222
        ut_ad(!ibuf_inside() || is_log || (type == OS_FILE_WRITE)
 
4223
              || ibuf_page(space_id, zip_size, block_offset, NULL));
 
4224
# endif /* UNIV_LOG_DEBUG */
 
4225
        if (sync) {
 
4226
                mode = OS_AIO_SYNC;
 
4227
        } else if (is_log) {
 
4228
                mode = OS_AIO_LOG;
 
4229
        } else if (type == OS_FILE_READ
 
4230
                   && !recv_no_ibuf_operations
 
4231
                   && ibuf_page(space_id, zip_size, block_offset, NULL)) {
 
4232
                mode = OS_AIO_IBUF;
 
4233
        } else {
 
4234
                mode = OS_AIO_NORMAL;
 
4235
        }
 
4236
#else /* !UNIV_HOTBACKUP */
 
4237
        ut_a(sync);
 
4238
        mode = OS_AIO_SYNC;
 
4239
#endif /* !UNIV_HOTBACKUP */
 
4240
 
 
4241
        if (type == OS_FILE_READ) {
 
4242
                srv_data_read+= len;
 
4243
        } else if (type == OS_FILE_WRITE) {
 
4244
                srv_data_written+= len;
 
4245
        }
 
4246
 
 
4247
        /* Reserve the fil_system mutex and make sure that we can open at
 
4248
        least one file while holding it, if the file is not already open */
 
4249
 
 
4250
        fil_mutex_enter_and_prepare_for_io(space_id);
 
4251
 
 
4252
        space = fil_space_get_by_id(space_id);
 
4253
 
 
4254
        if (!space) {
 
4255
                mutex_exit(&fil_system->mutex);
 
4256
 
 
4257
                ut_print_timestamp(stderr);
 
4258
                fprintf(stderr,
 
4259
                        "  InnoDB: Error: trying to do i/o"
 
4260
                        " to a tablespace which does not exist.\n"
 
4261
                        "InnoDB: i/o type %lu, space id %lu,"
 
4262
                        " page no. %lu, i/o length %lu bytes\n",
 
4263
                        (ulong) type, (ulong) space_id, (ulong) block_offset,
 
4264
                        (ulong) len);
 
4265
 
 
4266
                return(DB_TABLESPACE_DELETED);
 
4267
        }
 
4268
 
 
4269
        ut_ad((mode != OS_AIO_IBUF) || (space->purpose == FIL_TABLESPACE));
 
4270
 
 
4271
        node = UT_LIST_GET_FIRST(space->chain);
 
4272
 
 
4273
        for (;;) {
 
4274
                if (UNIV_UNLIKELY(node == NULL)) {
 
4275
                        fil_report_invalid_page_access(
 
4276
                                block_offset, space_id, space->name,
 
4277
                                byte_offset, len, type);
 
4278
 
 
4279
                        ut_error;
 
4280
                }
 
4281
 
 
4282
                if (space->id != 0 && node->size == 0) {
 
4283
                        /* We do not know the size of a single-table tablespace
 
4284
                        before we open the file */
 
4285
 
 
4286
                        break;
 
4287
                }
 
4288
 
 
4289
                if (node->size > block_offset) {
 
4290
                        /* Found! */
 
4291
                        break;
 
4292
                } else {
 
4293
                        block_offset -= node->size;
 
4294
                        node = UT_LIST_GET_NEXT(chain, node);
 
4295
                }
 
4296
        }
 
4297
 
 
4298
        /* Open file if closed */
 
4299
        fil_node_prepare_for_io(node, fil_system, space);
 
4300
 
 
4301
        /* Check that at least the start offset is within the bounds of a
 
4302
        single-table tablespace */
 
4303
        if (UNIV_UNLIKELY(node->size <= block_offset)
 
4304
            && space->id != 0 && space->purpose == FIL_TABLESPACE) {
 
4305
 
 
4306
                fil_report_invalid_page_access(
 
4307
                        block_offset, space_id, space->name, byte_offset,
 
4308
                        len, type);
 
4309
 
 
4310
                ut_error;
 
4311
        }
 
4312
 
 
4313
        /* Now we have made the changes in the data structures of fil_system */
 
4314
        mutex_exit(&fil_system->mutex);
 
4315
 
 
4316
        /* Calculate the low 32 bits and the high 32 bits of the file offset */
 
4317
 
 
4318
        if (!zip_size) {
 
4319
                offset_high = (block_offset >> (32 - UNIV_PAGE_SIZE_SHIFT));
 
4320
                offset_low  = ((block_offset << UNIV_PAGE_SIZE_SHIFT)
 
4321
                               & 0xFFFFFFFFUL) + byte_offset;
 
4322
 
 
4323
                ut_a(node->size - block_offset
 
4324
                     >= ((byte_offset + len + (UNIV_PAGE_SIZE - 1))
 
4325
                         / UNIV_PAGE_SIZE));
 
4326
        } else {
 
4327
                ulint   zip_size_shift;
 
4328
                switch (zip_size) {
 
4329
                case 1024: zip_size_shift = 10; break;
 
4330
                case 2048: zip_size_shift = 11; break;
 
4331
                case 4096: zip_size_shift = 12; break;
 
4332
                case 8192: zip_size_shift = 13; break;
 
4333
                case 16384: zip_size_shift = 14; break;
 
4334
                default: ut_error;
 
4335
                }
 
4336
                offset_high = block_offset >> (32 - zip_size_shift);
 
4337
                offset_low = (block_offset << zip_size_shift & 0xFFFFFFFFUL)
 
4338
                        + byte_offset;
 
4339
                ut_a(node->size - block_offset
 
4340
                     >= (len + (zip_size - 1)) / zip_size);
 
4341
        }
 
4342
 
 
4343
        /* Do aio */
 
4344
 
 
4345
        ut_a(byte_offset % OS_FILE_LOG_BLOCK_SIZE == 0);
 
4346
        ut_a((len % OS_FILE_LOG_BLOCK_SIZE) == 0);
 
4347
 
 
4348
#ifdef UNIV_HOTBACKUP
 
4349
        /* In ibbackup do normal i/o, not aio */
 
4350
        if (type == OS_FILE_READ) {
 
4351
                ret = os_file_read(node->handle, buf, offset_low, offset_high,
 
4352
                                   len);
 
4353
        } else {
 
4354
                ret = os_file_write(node->name, node->handle, buf,
 
4355
                                    offset_low, offset_high, len);
 
4356
        }
 
4357
#else
 
4358
        /* Queue the aio request */
 
4359
        ret = os_aio(type, mode | wake_later, node->name, node->handle, buf,
 
4360
                     offset_low, offset_high, len, node, message);
 
4361
#endif
 
4362
        ut_a(ret);
 
4363
 
 
4364
        if (mode == OS_AIO_SYNC) {
 
4365
                /* The i/o operation is already completed when we return from
 
4366
                os_aio: */
 
4367
 
 
4368
                mutex_enter(&fil_system->mutex);
 
4369
 
 
4370
                fil_node_complete_io(node, fil_system, type);
 
4371
 
 
4372
                mutex_exit(&fil_system->mutex);
 
4373
 
 
4374
                ut_ad(fil_validate());
 
4375
        }
 
4376
 
 
4377
        return(DB_SUCCESS);
 
4378
}
 
4379
 
 
4380
#ifndef UNIV_HOTBACKUP
 
4381
/**********************************************************************//**
 
4382
Waits for an aio operation to complete. This function is used to write the
 
4383
handler for completed requests. The aio array of pending requests is divided
 
4384
into segments (see os0file.c for more info). The thread specifies which
 
4385
segment it wants to wait for. */
 
4386
UNIV_INTERN
 
4387
void
 
4388
fil_aio_wait(
 
4389
/*=========*/
 
4390
        ulint   segment)        /*!< in: the number of the segment in the aio
 
4391
                                array to wait for */
 
4392
{
 
4393
        ibool           ret;
 
4394
        fil_node_t*     fil_node;
 
4395
        void*           message;
 
4396
        ulint           type;
 
4397
 
 
4398
        ut_ad(fil_validate());
 
4399
 
 
4400
        if (os_aio_use_native_aio) {
 
4401
                srv_set_io_thread_op_info(segment, "native aio handle");
 
4402
#ifdef WIN_ASYNC_IO
 
4403
                ret = os_aio_windows_handle(segment, 0, &fil_node,
 
4404
                                            &message, &type);
 
4405
#else
 
4406
                ret = 0; /* Eliminate compiler warning */
 
4407
                ut_error;
 
4408
#endif
 
4409
        } else {
 
4410
                srv_set_io_thread_op_info(segment, "simulated aio handle");
 
4411
 
 
4412
                ret = os_aio_simulated_handle(segment, &fil_node,
 
4413
                                              &message, &type);
 
4414
        }
 
4415
 
 
4416
        ut_a(ret);
 
4417
 
 
4418
        srv_set_io_thread_op_info(segment, "complete io for fil node");
 
4419
 
 
4420
        mutex_enter(&fil_system->mutex);
 
4421
 
 
4422
        fil_node_complete_io(fil_node, fil_system, type);
 
4423
 
 
4424
        mutex_exit(&fil_system->mutex);
 
4425
 
 
4426
        ut_ad(fil_validate());
 
4427
 
 
4428
        /* Do the i/o handling */
 
4429
        /* IMPORTANT: since i/o handling for reads will read also the insert
 
4430
        buffer in tablespace 0, you have to be very careful not to introduce
 
4431
        deadlocks in the i/o system. We keep tablespace 0 data files always
 
4432
        open, and use a special i/o thread to serve insert buffer requests. */
 
4433
 
 
4434
        if (fil_node->space->purpose == FIL_TABLESPACE) {
 
4435
                srv_set_io_thread_op_info(segment, "complete io for buf page");
 
4436
                buf_page_io_complete(message);
 
4437
        } else {
 
4438
                srv_set_io_thread_op_info(segment, "complete io for log");
 
4439
                log_io_complete(message);
 
4440
        }
 
4441
}
 
4442
#endif /* UNIV_HOTBACKUP */
 
4443
 
 
4444
/**********************************************************************//**
 
4445
Flushes to disk possible writes cached by the OS. If the space does not exist
 
4446
or is being dropped, does not do anything. */
 
4447
UNIV_INTERN
 
4448
void
 
4449
fil_flush(
 
4450
/*======*/
 
4451
        ulint   space_id)       /*!< in: file space id (this can be a group of
 
4452
                                log files or a tablespace of the database) */
 
4453
{
 
4454
        fil_space_t*    space;
 
4455
        fil_node_t*     node;
 
4456
        os_file_t       file;
 
4457
        ib_int64_t      old_mod_counter;
 
4458
 
 
4459
        mutex_enter(&fil_system->mutex);
 
4460
 
 
4461
        space = fil_space_get_by_id(space_id);
 
4462
 
 
4463
        if (!space || space->is_being_deleted) {
 
4464
                mutex_exit(&fil_system->mutex);
 
4465
 
 
4466
                return;
 
4467
        }
 
4468
 
 
4469
        space->n_pending_flushes++;     /*!< prevent dropping of the space while
 
4470
                                        we are flushing */
 
4471
        node = UT_LIST_GET_FIRST(space->chain);
 
4472
 
 
4473
        while (node) {
 
4474
                if (node->modification_counter > node->flush_counter) {
 
4475
                        ut_a(node->open);
 
4476
 
 
4477
                        /* We want to flush the changes at least up to
 
4478
                        old_mod_counter */
 
4479
                        old_mod_counter = node->modification_counter;
 
4480
 
 
4481
                        if (space->purpose == FIL_TABLESPACE) {
 
4482
                                fil_n_pending_tablespace_flushes++;
 
4483
                        } else {
 
4484
                                fil_n_pending_log_flushes++;
 
4485
                                fil_n_log_flushes++;
 
4486
                        }
 
4487
#ifdef __WIN__
 
4488
                        if (node->is_raw_disk) {
 
4489
 
 
4490
                                goto skip_flush;
 
4491
                        }
 
4492
#endif
 
4493
retry:
 
4494
                        if (node->n_pending_flushes > 0) {
 
4495
                                /* We want to avoid calling os_file_flush() on
 
4496
                                the file twice at the same time, because we do
 
4497
                                not know what bugs OS's may contain in file
 
4498
                                i/o; sleep for a while */
 
4499
 
 
4500
                                mutex_exit(&fil_system->mutex);
 
4501
 
 
4502
                                os_thread_sleep(20000);
 
4503
 
 
4504
                                mutex_enter(&fil_system->mutex);
 
4505
 
 
4506
                                if (node->flush_counter >= old_mod_counter) {
 
4507
 
 
4508
                                        goto skip_flush;
 
4509
                                }
 
4510
 
 
4511
                                goto retry;
 
4512
                        }
 
4513
 
 
4514
                        ut_a(node->open);
 
4515
                        file = node->handle;
 
4516
                        node->n_pending_flushes++;
 
4517
 
 
4518
                        mutex_exit(&fil_system->mutex);
 
4519
 
 
4520
                        /* fprintf(stderr, "Flushing to file %s\n",
 
4521
                        node->name); */
 
4522
 
 
4523
                        os_file_flush(file);
 
4524
 
 
4525
                        mutex_enter(&fil_system->mutex);
 
4526
 
 
4527
                        node->n_pending_flushes--;
 
4528
skip_flush:
 
4529
                        if (node->flush_counter < old_mod_counter) {
 
4530
                                node->flush_counter = old_mod_counter;
 
4531
 
 
4532
                                if (space->is_in_unflushed_spaces
 
4533
                                    && fil_space_is_flushed(space)) {
 
4534
 
 
4535
                                        space->is_in_unflushed_spaces = FALSE;
 
4536
 
 
4537
                                        UT_LIST_REMOVE(
 
4538
                                                unflushed_spaces,
 
4539
                                                fil_system->unflushed_spaces,
 
4540
                                                space);
 
4541
                                }
 
4542
                        }
 
4543
 
 
4544
                        if (space->purpose == FIL_TABLESPACE) {
 
4545
                                fil_n_pending_tablespace_flushes--;
 
4546
                        } else {
 
4547
                                fil_n_pending_log_flushes--;
 
4548
                        }
 
4549
                }
 
4550
 
 
4551
                node = UT_LIST_GET_NEXT(chain, node);
 
4552
        }
 
4553
 
 
4554
        space->n_pending_flushes--;
 
4555
 
 
4556
        mutex_exit(&fil_system->mutex);
 
4557
}
 
4558
 
 
4559
/**********************************************************************//**
 
4560
Flushes to disk the writes in file spaces of the given type possibly cached by
 
4561
the OS. */
 
4562
UNIV_INTERN
 
4563
void
 
4564
fil_flush_file_spaces(
 
4565
/*==================*/
 
4566
        ulint   purpose)        /*!< in: FIL_TABLESPACE, FIL_LOG */
 
4567
{
 
4568
        fil_space_t*    space;
 
4569
        ulint*          space_ids;
 
4570
        ulint           n_space_ids;
 
4571
        ulint           i;
 
4572
 
 
4573
        mutex_enter(&fil_system->mutex);
 
4574
 
 
4575
        n_space_ids = UT_LIST_GET_LEN(fil_system->unflushed_spaces);
 
4576
        if (n_space_ids == 0) {
 
4577
 
 
4578
                mutex_exit(&fil_system->mutex);
 
4579
                return;
 
4580
        }
 
4581
 
 
4582
        /* Assemble a list of space ids to flush.  Previously, we
 
4583
        traversed fil_system->unflushed_spaces and called UT_LIST_GET_NEXT()
 
4584
        on a space that was just removed from the list by fil_flush().
 
4585
        Thus, the space could be dropped and the memory overwritten. */
 
4586
        space_ids = mem_alloc(n_space_ids * sizeof *space_ids);
 
4587
 
 
4588
        n_space_ids = 0;
 
4589
 
 
4590
        for (space = UT_LIST_GET_FIRST(fil_system->unflushed_spaces);
 
4591
             space;
 
4592
             space = UT_LIST_GET_NEXT(unflushed_spaces, space)) {
 
4593
 
 
4594
                if (space->purpose == purpose && !space->is_being_deleted) {
 
4595
 
 
4596
                        space_ids[n_space_ids++] = space->id;
 
4597
                }
 
4598
        }
 
4599
 
 
4600
        mutex_exit(&fil_system->mutex);
 
4601
 
 
4602
        /* Flush the spaces.  It will not hurt to call fil_flush() on
 
4603
        a non-existing space id. */
 
4604
        for (i = 0; i < n_space_ids; i++) {
 
4605
 
 
4606
                fil_flush(space_ids[i]);
 
4607
        }
 
4608
 
 
4609
        mem_free(space_ids);
 
4610
}
 
4611
 
 
4612
/******************************************************************//**
 
4613
Checks the consistency of the tablespace cache.
 
4614
@return TRUE if ok */
 
4615
UNIV_INTERN
 
4616
ibool
 
4617
fil_validate(void)
 
4618
/*==============*/
 
4619
{
 
4620
        fil_space_t*    space;
 
4621
        fil_node_t*     fil_node;
 
4622
        ulint           n_open          = 0;
 
4623
        ulint           i;
 
4624
 
 
4625
        mutex_enter(&fil_system->mutex);
 
4626
 
 
4627
        /* Look for spaces in the hash table */
 
4628
 
 
4629
        for (i = 0; i < hash_get_n_cells(fil_system->spaces); i++) {
 
4630
 
 
4631
                space = HASH_GET_FIRST(fil_system->spaces, i);
 
4632
 
 
4633
                while (space != NULL) {
 
4634
                        UT_LIST_VALIDATE(chain, fil_node_t, space->chain,
 
4635
                                         ut_a(ut_list_node_313->open
 
4636
                                              || !ut_list_node_313->n_pending));
 
4637
 
 
4638
                        fil_node = UT_LIST_GET_FIRST(space->chain);
 
4639
 
 
4640
                        while (fil_node != NULL) {
 
4641
                                if (fil_node->n_pending > 0) {
 
4642
                                        ut_a(fil_node->open);
 
4643
                                }
 
4644
 
 
4645
                                if (fil_node->open) {
 
4646
                                        n_open++;
 
4647
                                }
 
4648
                                fil_node = UT_LIST_GET_NEXT(chain, fil_node);
 
4649
                        }
 
4650
                        space = HASH_GET_NEXT(hash, space);
 
4651
                }
 
4652
        }
 
4653
 
 
4654
        ut_a(fil_system->n_open == n_open);
 
4655
 
 
4656
        UT_LIST_VALIDATE(LRU, fil_node_t, fil_system->LRU, (void) 0);
 
4657
 
 
4658
        fil_node = UT_LIST_GET_FIRST(fil_system->LRU);
 
4659
 
 
4660
        while (fil_node != NULL) {
 
4661
                ut_a(fil_node->n_pending == 0);
 
4662
                ut_a(fil_node->open);
 
4663
                ut_a(fil_node->space->purpose == FIL_TABLESPACE);
 
4664
                ut_a(fil_node->space->id != 0);
 
4665
 
 
4666
                fil_node = UT_LIST_GET_NEXT(LRU, fil_node);
 
4667
        }
 
4668
 
 
4669
        mutex_exit(&fil_system->mutex);
 
4670
 
 
4671
        return(TRUE);
 
4672
}
 
4673
 
 
4674
/********************************************************************//**
 
4675
Returns TRUE if file address is undefined.
 
4676
@return TRUE if undefined */
 
4677
UNIV_INTERN
 
4678
ibool
 
4679
fil_addr_is_null(
 
4680
/*=============*/
 
4681
        fil_addr_t      addr)   /*!< in: address */
 
4682
{
 
4683
        return(addr.page == FIL_NULL);
 
4684
}
 
4685
 
 
4686
/********************************************************************//**
 
4687
Get the predecessor of a file page.
 
4688
@return FIL_PAGE_PREV */
 
4689
UNIV_INTERN
 
4690
ulint
 
4691
fil_page_get_prev(
 
4692
/*==============*/
 
4693
        const byte*     page)   /*!< in: file page */
 
4694
{
 
4695
        return(mach_read_from_4(page + FIL_PAGE_PREV));
 
4696
}
 
4697
 
 
4698
/********************************************************************//**
 
4699
Get the successor of a file page.
 
4700
@return FIL_PAGE_NEXT */
 
4701
UNIV_INTERN
 
4702
ulint
 
4703
fil_page_get_next(
 
4704
/*==============*/
 
4705
        const byte*     page)   /*!< in: file page */
 
4706
{
 
4707
        return(mach_read_from_4(page + FIL_PAGE_NEXT));
 
4708
}
 
4709
 
 
4710
/*********************************************************************//**
 
4711
Sets the file page type. */
 
4712
UNIV_INTERN
 
4713
void
 
4714
fil_page_set_type(
 
4715
/*==============*/
 
4716
        byte*   page,   /*!< in/out: file page */
 
4717
        ulint   type)   /*!< in: type */
 
4718
{
 
4719
        ut_ad(page);
 
4720
 
 
4721
        mach_write_to_2(page + FIL_PAGE_TYPE, type);
 
4722
}
 
4723
 
 
4724
/*********************************************************************//**
 
4725
Gets the file page type.
 
4726
@return type; NOTE that if the type has not been written to page, the
 
4727
return value not defined */
 
4728
UNIV_INTERN
 
4729
ulint
 
4730
fil_page_get_type(
 
4731
/*==============*/
 
4732
        const byte*     page)   /*!< in: file page */
 
4733
{
 
4734
        ut_ad(page);
 
4735
 
 
4736
        return(mach_read_from_2(page + FIL_PAGE_TYPE));
 
4737
}