forked from meeeejin/rw-rbuf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfil0fil.cc
6956 lines (5600 loc) · 184 KB
/
fil0fil.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
Copyright (c) 1995, 2015, Oracle and/or its affiliates. All Rights Reserved.
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
*****************************************************************************/
/**************************************************//**
@file fil/fil0fil.cc
The tablespace memory cache
Created 10/25/1995 Heikki Tuuri
*******************************************************/
#include "fil0fil.h"
#include <debug_sync.h>
#include <my_dbug.h>
#include "mem0mem.h"
#include "hash0hash.h"
#include "os0file.h"
#include "mach0data.h"
#include "buf0buf.h"
#include "buf0flu.h"
#include "log0recv.h"
#include "fsp0fsp.h"
#include "srv0srv.h"
#include "srv0start.h"
#include "mtr0mtr.h"
#include "mtr0log.h"
#include "dict0dict.h"
#include "page0page.h"
#include "page0zip.h"
#include "trx0sys.h"
#include "row0mysql.h"
#ifndef UNIV_HOTBACKUP
# include "buf0lru.h"
# include "ibuf0ibuf.h"
# include "sync0sync.h"
# include "os0sync.h"
#else /* !UNIV_HOTBACKUP */
# include "srv0srv.h"
static ulint srv_data_read, srv_data_written;
#endif /* !UNIV_HOTBACKUP */
/*
IMPLEMENTATION OF THE TABLESPACE MEMORY CACHE
=============================================
The tablespace cache is responsible for providing fast read/write access to
tablespaces and logs of the database. File creation and deletion is done
in other modules which know more of the logic of the operation, however.
A tablespace consists of a chain of files. The size of the files does not
have to be divisible by the database block size, because we may just leave
the last incomplete block unused. When a new file is appended to the
tablespace, the maximum size of the file is also specified. At the moment,
we think that it is best to extend the file to its maximum size already at
the creation of the file, because then we can avoid dynamically extending
the file when more space is needed for the tablespace.
A block's position in the tablespace is specified with a 32-bit unsigned
integer. The files in the chain are thought to be catenated, and the block
corresponding to an address n is the nth block in the catenated file (where
the first block is named the 0th block, and the incomplete block fragments
at the end of files are not taken into account). A tablespace can be extended
by appending a new file at the end of the chain.
Our tablespace concept is similar to the one of Oracle.
To acquire more speed in disk transfers, a technique called disk striping is
sometimes used. This means that logical block addresses are divided in a
round-robin fashion across several disks. Windows NT supports disk striping,
so there we do not need to support it in the database. Disk striping is
implemented in hardware in RAID disks. We conclude that it is not necessary
to implement it in the database. Oracle 7 does not support disk striping,
either.
Another trick used at some database sites is replacing tablespace files by
raw disks, that is, the whole physical disk drive, or a partition of it, is
opened as a single file, and it is accessed through byte offsets calculated
from the start of the disk or the partition. This is recommended in some
books on database tuning to achieve more speed in i/o. Using raw disk
certainly prevents the OS from fragmenting disk space, but it is not clear
if it really adds speed. We measured on the Pentium 100 MHz + NT + NTFS file
system + EIDE Conner disk only a negligible difference in speed when reading
from a file, versus reading from a raw disk.
To have fast access to a tablespace or a log file, we put the data structures
to a hash table. Each tablespace and log file is given an unique 32-bit
identifier.
Some operating systems do not support many open files at the same time,
though NT seems to tolerate at least 900 open files. Therefore, we put the
open files in an LRU-list. If we need to open another file, we may close the
file at the end of the LRU-list. When an i/o-operation is pending on a file,
the file cannot be closed. We take the file nodes with pending i/o-operations
out of the LRU-list and keep a count of pending operations. When an operation
completes, we decrement the count and return the file node to the LRU-list if
the count drops to zero. */
/** When mysqld is run, the default directory "." is the mysqld datadir,
but in the MySQL Embedded Server Library and mysqlbackup it is not the default
directory, and we must set the base file path explicitly */
UNIV_INTERN const char* fil_path_to_mysql_datadir = ".";
/** The number of fsyncs done to the log */
UNIV_INTERN ulint fil_n_log_flushes = 0;
/** Number of pending redo log flushes */
UNIV_INTERN ulint fil_n_pending_log_flushes = 0;
/** Number of pending tablespace flushes */
UNIV_INTERN ulint fil_n_pending_tablespace_flushes = 0;
/** Number of files currently open */
UNIV_INTERN ulint fil_n_file_opened = 0;
/** The null file address */
UNIV_INTERN fil_addr_t fil_addr_null = {FIL_NULL, 0};
#ifdef UNIV_PFS_MUTEX
/* Key to register fil_system_mutex with performance schema */
UNIV_INTERN mysql_pfs_key_t fil_system_mutex_key;
#endif /* UNIV_PFS_MUTEX */
#ifdef UNIV_PFS_RWLOCK
/* Key to register file space latch with performance schema */
UNIV_INTERN mysql_pfs_key_t fil_space_latch_key;
#endif /* UNIV_PFS_RWLOCK */
/** File node of a tablespace or the log data space */
struct fil_node_t {
fil_space_t* space; /*!< backpointer to the space where this node
belongs */
char* name; /*!< path to the file */
ibool open; /*!< TRUE if file open */
os_file_t handle; /*!< OS handle to the file, if file open */
os_event_t sync_event;/*!< Condition event to group and
serialize calls to fsync */
ibool is_raw_disk;/*!< TRUE if the 'file' is actually a raw
device or a raw disk partition */
ulint size; /*!< size of the file in database pages, 0 if
not known yet; the possible last incomplete
megabyte may be ignored if space == 0 */
ulint n_pending;
/*!< count of pending i/o's on this file;
closing of the file is not allowed if
this is > 0 */
ulint n_pending_flushes;
/*!< count of pending flushes on this file;
closing of the file is not allowed if
this is > 0 */
ibool being_extended;
/*!< TRUE if the node is currently
being extended. */
ib_int64_t modification_counter;/*!< when we write to the file we
increment this by one */
ib_int64_t flush_counter;/*!< up to what
modification_counter value we have
flushed the modifications to disk */
UT_LIST_NODE_T(fil_node_t) chain;
/*!< link field for the file chain */
UT_LIST_NODE_T(fil_node_t) LRU;
/*!< link field for the LRU list */
ulint magic_n;/*!< FIL_NODE_MAGIC_N */
};
/** Value of fil_node_t::magic_n */
#define FIL_NODE_MAGIC_N 89389
/** Tablespace or log data space: let us call them by a common name space */
struct fil_space_t {
char* name; /*!< space name = the path to the first file in
it */
ulint id; /*!< space id */
ib_int64_t tablespace_version;
/*!< in DISCARD/IMPORT this timestamp
is used to check if we should ignore
an insert buffer merge request for a
page because it actually was for the
previous incarnation of the space */
ibool mark; /*!< this is set to TRUE at database startup if
the space corresponds to a table in the InnoDB
data dictionary; so we can print a warning of
orphaned tablespaces */
ibool stop_ios;/*!< TRUE if we want to rename the
.ibd file of tablespace and want to
stop temporarily posting of new i/o
requests on the file */
ibool stop_new_ops;
/*!< we set this TRUE when we start
deleting a single-table tablespace.
When this is set following new ops
are not allowed:
* read IO request
* ibuf merge
* file flush
Note that we can still possibly have
new write operations because we don't
check this flag when doing flush
batches. */
ulint purpose;/*!< FIL_TABLESPACE, FIL_LOG, or
FIL_ARCH_LOG */
UT_LIST_BASE_NODE_T(fil_node_t) chain;
/*!< base node for the file chain */
ulint size; /*!< space size in pages; 0 if a single-table
tablespace whose size we do not know yet;
last incomplete megabytes in data files may be
ignored if space == 0 */
ulint flags; /*!< tablespace flags; see
fsp_flags_is_valid(),
fsp_flags_get_zip_size() */
ulint n_reserved_extents;
/*!< number of reserved free extents for
ongoing operations like B-tree page split */
ulint n_pending_flushes; /*!< this is positive when flushing
the tablespace to disk; dropping of the
tablespace is forbidden if this is positive */
ulint n_pending_ops;/*!< this is positive when we
have pending operations against this
tablespace. The pending operations can
be ibuf merges or lock validation code
trying to read a block.
Dropping of the tablespace is forbidden
if this is positive */
hash_node_t hash; /*!< hash chain node */
hash_node_t name_hash;/*!< hash chain the name_hash table */
#ifndef UNIV_HOTBACKUP
rw_lock_t latch; /*!< latch protecting the file space storage
allocation */
#endif /* !UNIV_HOTBACKUP */
UT_LIST_NODE_T(fil_space_t) unflushed_spaces;
/*!< list of spaces with at least one unflushed
file we have written to */
bool is_in_unflushed_spaces;
/*!< true if this space is currently in
unflushed_spaces */
UT_LIST_NODE_T(fil_space_t) space_list;
/*!< list of all spaces */
ulint magic_n;/*!< FIL_SPACE_MAGIC_N */
};
/** Value of fil_space_t::magic_n */
#define FIL_SPACE_MAGIC_N 89472
/** The tablespace memory cache; also the totality of logs (the log
data space) is stored here; below we talk about tablespaces, but also
the ib_logfiles form a 'space' and it is handled here */
struct fil_system_t {
#ifndef UNIV_HOTBACKUP
ib_mutex_t mutex; /*!< The mutex protecting the cache */
#endif /* !UNIV_HOTBACKUP */
hash_table_t* spaces; /*!< The hash table of spaces in the
system; they are hashed on the space
id */
hash_table_t* name_hash; /*!< hash table based on the space
name */
UT_LIST_BASE_NODE_T(fil_node_t) LRU;
/*!< base node for the LRU list of the
most recently used open files with no
pending i/o's; if we start an i/o on
the file, we first remove it from this
list, and return it to the start of
the list when the i/o ends;
log files and the system tablespace are
not put to this list: they are opened
after the startup, and kept open until
shutdown */
UT_LIST_BASE_NODE_T(fil_space_t) unflushed_spaces;
/*!< base node for the list of those
tablespaces whose files contain
unflushed writes; those spaces have
at least one file node where
modification_counter > flush_counter */
ulint n_open; /*!< number of files currently open */
ulint max_n_open; /*!< n_open is not allowed to exceed
this */
ib_int64_t modification_counter;/*!< when we write to a file we
increment this by one */
ulint max_assigned_id;/*!< maximum space id in the existing
tables, or assigned during the time
mysqld has been up; at an InnoDB
startup we scan the data dictionary
and set here the maximum of the
space id's of the tables there */
ib_int64_t tablespace_version;
/*!< a counter which is incremented for
every space object memory creation;
every space mem object gets a
'timestamp' from this; in DISCARD/
IMPORT this is used to check if we
should ignore an insert buffer merge
request */
UT_LIST_BASE_NODE_T(fil_space_t) space_list;
/*!< list of all file spaces */
ibool space_id_reuse_warned;
/* !< TRUE if fil_space_create()
has issued a warning about
potential space_id reuse */
};
/** The tablespace memory cache. This variable is NULL before the module is
initialized. */
static fil_system_t* fil_system = NULL;
/** Determine if (i) is a user tablespace id or not. */
# define fil_is_user_tablespace_id(i) ((i) > srv_undo_tablespaces_open)
/** Determine if user has explicitly disabled fsync(). */
#ifndef __WIN__
# define fil_buffering_disabled(s) \
((s)->purpose == FIL_TABLESPACE \
&& srv_unix_file_flush_method \
== SRV_UNIX_O_DIRECT_NO_FSYNC)
#else /* __WIN__ */
# define fil_buffering_disabled(s) (0)
#endif /* __WIN__ */
#ifdef UNIV_DEBUG
/** Try fil_validate() every this many times */
# define FIL_VALIDATE_SKIP 17
/******************************************************************//**
Checks the consistency of the tablespace cache some of the time.
@return TRUE if ok or the check was skipped */
static
ibool
fil_validate_skip(void)
/*===================*/
{
/** The fil_validate() call skip counter. Use a signed type
because of the race condition below. */
static int fil_validate_count = FIL_VALIDATE_SKIP;
/* There is a race condition below, but it does not matter,
because this call is only for heuristic purposes. We want to
reduce the call frequency of the costly fil_validate() check
in debug builds. */
if (--fil_validate_count > 0) {
return(TRUE);
}
fil_validate_count = FIL_VALIDATE_SKIP;
return(fil_validate());
}
#endif /* UNIV_DEBUG */
/********************************************************************//**
Determines if a file node belongs to the least-recently-used list.
@return TRUE if the file belongs to fil_system->LRU mutex. */
UNIV_INLINE
ibool
fil_space_belongs_in_lru(
/*=====================*/
const fil_space_t* space) /*!< in: file space */
{
return(space->purpose == FIL_TABLESPACE
&& fil_is_user_tablespace_id(space->id));
}
/********************************************************************//**
NOTE: you must call fil_mutex_enter_and_prepare_for_io() first!
Prepares a file node for i/o. Opens the file if it is closed. Updates the
pending i/o's field in the node and the system appropriately. Takes the node
off the LRU list if it is in the LRU list. The caller must hold the fil_sys
mutex.
@return false if the file can't be opened, otherwise true */
static
bool
fil_node_prepare_for_io(
/*====================*/
fil_node_t* node, /*!< in: file node */
fil_system_t* system, /*!< in: tablespace memory cache */
fil_space_t* space); /*!< in: space */
/********************************************************************//**
Updates the data structures when an i/o operation finishes. Updates the
pending i/o's field in the node appropriately. */
static
void
fil_node_complete_io(
/*=================*/
fil_node_t* node, /*!< in: file node */
fil_system_t* system, /*!< in: tablespace memory cache */
ulint type); /*!< in: OS_FILE_WRITE or OS_FILE_READ; marks
the node as modified if
type == OS_FILE_WRITE */
/*******************************************************************//**
Frees a space object from the tablespace memory cache. Closes the files in
the chain but does not delete them. There must not be any pending i/o's or
flushes on the files.
@return TRUE on success */
static
ibool
fil_space_free(
/*===========*/
ulint id, /* in: space id */
ibool x_latched); /* in: TRUE if caller has space->latch
in X mode */
/********************************************************************//**
Reads data from a space to a buffer. Remember that the possible incomplete
blocks at the end of file are ignored: they are not taken into account when
calculating the byte offset within a space.
@return DB_SUCCESS, or DB_TABLESPACE_DELETED if we are trying to do
i/o on a tablespace which does not exist */
UNIV_INLINE
dberr_t
fil_read(
/*=====*/
bool sync, /*!< in: true if synchronous aio is desired */
ulint space_id, /*!< in: space id */
ulint zip_size, /*!< in: compressed page size in bytes;
0 for uncompressed pages */
ulint block_offset, /*!< in: offset in number of blocks */
ulint byte_offset, /*!< in: remainder of offset in bytes; in aio
this must be divisible by the OS block size */
ulint len, /*!< in: how many bytes to read; this must not
cross a file boundary; in aio this must be a
block size multiple */
void* buf, /*!< in/out: buffer where to store data read;
in aio this must be appropriately aligned */
void* message) /*!< in: message for aio handler if non-sync
aio used, else ignored */
{
return(fil_io(OS_FILE_READ, sync, space_id, zip_size, block_offset,
byte_offset, len, buf, message));
}
/********************************************************************//**
Writes data to a space from a buffer. Remember that the possible incomplete
blocks at the end of file are ignored: they are not taken into account when
calculating the byte offset within a space.
@return DB_SUCCESS, or DB_TABLESPACE_DELETED if we are trying to do
i/o on a tablespace which does not exist */
UNIV_INLINE
dberr_t
fil_write(
/*======*/
bool sync, /*!< in: true if synchronous aio is desired */
ulint space_id, /*!< in: space id */
ulint zip_size, /*!< in: compressed page size in bytes;
0 for uncompressed pages */
ulint block_offset, /*!< in: offset in number of blocks */
ulint byte_offset, /*!< in: remainder of offset in bytes; in aio
this must be divisible by the OS block size */
ulint len, /*!< in: how many bytes to write; this must
not cross a file boundary; in aio this must
be a block size multiple */
void* buf, /*!< in: buffer from which to write; in aio
this must be appropriately aligned */
void* message) /*!< in: message for aio handler if non-sync
aio used, else ignored */
{
ut_ad(!srv_read_only_mode);
return(fil_io(OS_FILE_WRITE, sync, space_id, zip_size, block_offset,
byte_offset, len, buf, message));
}
/*******************************************************************//**
Returns the table space by a given id, NULL if not found. */
UNIV_INLINE
fil_space_t*
fil_space_get_by_id(
/*================*/
ulint id) /*!< in: space id */
{
fil_space_t* space;
ut_ad(mutex_own(&fil_system->mutex));
HASH_SEARCH(hash, fil_system->spaces, id,
fil_space_t*, space,
ut_ad(space->magic_n == FIL_SPACE_MAGIC_N),
space->id == id);
return(space);
}
/*******************************************************************//**
Returns the table space by a given name, NULL if not found. */
UNIV_INLINE
fil_space_t*
fil_space_get_by_name(
/*==================*/
const char* name) /*!< in: space name */
{
fil_space_t* space;
ulint fold;
ut_ad(mutex_own(&fil_system->mutex));
fold = ut_fold_string(name);
HASH_SEARCH(name_hash, fil_system->name_hash, fold,
fil_space_t*, space,
ut_ad(space->magic_n == FIL_SPACE_MAGIC_N),
!strcmp(name, space->name));
return(space);
}
#ifndef UNIV_HOTBACKUP
/*******************************************************************//**
Returns the version number of a tablespace, -1 if not found.
@return version number, -1 if the tablespace does not exist in the
memory cache */
UNIV_INTERN
ib_int64_t
fil_space_get_version(
/*==================*/
ulint id) /*!< in: space id */
{
fil_space_t* space;
ib_int64_t version = -1;
ut_ad(fil_system);
mutex_enter(&fil_system->mutex);
space = fil_space_get_by_id(id);
if (space) {
version = space->tablespace_version;
}
mutex_exit(&fil_system->mutex);
return(version);
}
/*******************************************************************//**
Returns the latch of a file space.
@return latch protecting storage allocation */
UNIV_INTERN
rw_lock_t*
fil_space_get_latch(
/*================*/
ulint id, /*!< in: space id */
ulint* flags) /*!< out: tablespace flags */
{
fil_space_t* space;
ut_ad(fil_system);
mutex_enter(&fil_system->mutex);
space = fil_space_get_by_id(id);
ut_a(space);
if (flags) {
*flags = space->flags;
}
mutex_exit(&fil_system->mutex);
return(&(space->latch));
}
/*******************************************************************//**
Returns the type of a file space.
@return FIL_TABLESPACE or FIL_LOG */
UNIV_INTERN
ulint
fil_space_get_type(
/*===============*/
ulint id) /*!< in: space id */
{
fil_space_t* space;
ut_ad(fil_system);
mutex_enter(&fil_system->mutex);
space = fil_space_get_by_id(id);
ut_a(space);
mutex_exit(&fil_system->mutex);
return(space->purpose);
}
#endif /* !UNIV_HOTBACKUP */
/**********************************************************************//**
Checks if all the file nodes in a space are flushed. The caller must hold
the fil_system mutex.
@return true if all are flushed */
static
bool
fil_space_is_flushed(
/*=================*/
fil_space_t* space) /*!< in: space */
{
fil_node_t* node;
ut_ad(mutex_own(&fil_system->mutex));
node = UT_LIST_GET_FIRST(space->chain);
while (node) {
if (node->modification_counter > node->flush_counter) {
ut_ad(!fil_buffering_disabled(space));
return(false);
}
node = UT_LIST_GET_NEXT(chain, node);
}
return(true);
}
/*******************************************************************//**
Appends a new file to the chain of files of a space. File must be closed.
@return pointer to the file name, or NULL on error */
UNIV_INTERN
char*
fil_node_create(
/*============*/
const char* name, /*!< in: file name (file must be closed) */
ulint size, /*!< in: file size in database blocks, rounded
downwards to an integer */
ulint id, /*!< in: space id where to append */
ibool is_raw) /*!< in: TRUE if a raw device or
a raw disk partition */
{
fil_node_t* node;
fil_space_t* space;
ut_a(fil_system);
ut_a(name);
mutex_enter(&fil_system->mutex);
node = static_cast<fil_node_t*>(mem_zalloc(sizeof(fil_node_t)));
node->name = mem_strdup(name);
ut_a(!is_raw || srv_start_raw_disk_in_use);
node->sync_event = os_event_create();
node->is_raw_disk = is_raw;
node->size = size;
node->magic_n = FIL_NODE_MAGIC_N;
space = fil_space_get_by_id(id);
if (!space) {
ut_print_timestamp(stderr);
fprintf(stderr,
" InnoDB: Error: Could not find tablespace %lu for\n"
"InnoDB: file ", (ulong) id);
ut_print_filename(stderr, name);
fputs(" in the tablespace memory cache.\n", stderr);
mem_free(node->name);
mem_free(node);
mutex_exit(&fil_system->mutex);
return(NULL);
}
space->size += size;
node->space = space;
UT_LIST_ADD_LAST(chain, space->chain, node);
if (id < SRV_LOG_SPACE_FIRST_ID && fil_system->max_assigned_id < id) {
fil_system->max_assigned_id = id;
}
mutex_exit(&fil_system->mutex);
return(node->name);
}
/********************************************************************//**
Opens a file of a node of a tablespace. The caller must own the fil_system
mutex.
@return false if the file can't be opened, otherwise true */
static
bool
fil_node_open_file(
/*===============*/
fil_node_t* node, /*!< in: file node */
fil_system_t* system, /*!< in: tablespace memory cache */
fil_space_t* space) /*!< in: space */
{
os_offset_t size_bytes;
ibool ret;
ibool success;
byte* buf2;
byte* page;
ulint space_id;
ulint flags;
ulint page_size;
ut_ad(mutex_own(&(system->mutex)));
ut_a(node->n_pending == 0);
ut_a(node->open == FALSE);
if (node->size == 0) {
/* It must be a single-table tablespace and we do not know the
size of the file yet. First we open the file in the normal
mode, no async I/O here, for simplicity. Then do some checks,
and close the file again.
NOTE that we could not use the simple file read function
os_file_read() in Windows to read from a file opened for
async I/O! */
node->handle = os_file_create_simple_no_error_handling(
innodb_file_data_key, node->name, OS_FILE_OPEN,
OS_FILE_READ_ONLY, &success);
if (!success) {
/* The following call prints an error message */
os_file_get_last_error(true);
ut_print_timestamp(stderr);
ib_logf(IB_LOG_LEVEL_WARN, "InnoDB: Error: cannot "
"open %s\n. InnoDB: Have you deleted .ibd "
"files under a running mysqld server?\n",
node->name);
return(false);
}
size_bytes = os_file_get_size(node->handle);
ut_a(size_bytes != (os_offset_t) -1);
#ifdef UNIV_HOTBACKUP
if (space->id == 0) {
node->size = (ulint) (size_bytes / UNIV_PAGE_SIZE);
os_file_close(node->handle);
goto add_size;
}
#endif /* UNIV_HOTBACKUP */
ut_a(space->purpose != FIL_LOG);
ut_a(fil_is_user_tablespace_id(space->id));
if (size_bytes < FIL_IBD_FILE_INITIAL_SIZE * UNIV_PAGE_SIZE) {
fprintf(stderr,
"InnoDB: Error: the size of single-table"
" tablespace file %s\n"
"InnoDB: is only " UINT64PF ","
" should be at least %lu!\n",
node->name,
size_bytes,
(ulong) (FIL_IBD_FILE_INITIAL_SIZE
* UNIV_PAGE_SIZE));
ut_a(0);
}
/* Read the first page of the tablespace */
buf2 = static_cast<byte*>(ut_malloc(2 * UNIV_PAGE_SIZE));
/* Align the memory for file i/o if we might have O_DIRECT
set */
page = static_cast<byte*>(ut_align(buf2, UNIV_PAGE_SIZE));
success = os_file_read(node->handle, page, 0, UNIV_PAGE_SIZE);
space_id = fsp_header_get_space_id(page);
flags = fsp_header_get_flags(page);
page_size = fsp_flags_get_page_size(flags);
ut_free(buf2);
/* Close the file now that we have read the space id from it */
os_file_close(node->handle);
if (UNIV_UNLIKELY(space_id != space->id)) {
fprintf(stderr,
"InnoDB: Error: tablespace id is %lu"
" in the data dictionary\n"
"InnoDB: but in file %s it is %lu!\n",
space->id, node->name, space_id);
ut_error;
}
if (UNIV_UNLIKELY(space_id == ULINT_UNDEFINED
|| space_id == 0)) {
fprintf(stderr,
"InnoDB: Error: tablespace id %lu"
" in file %s is not sensible\n",
(ulong) space_id, node->name);
ut_error;
}
if (UNIV_UNLIKELY(fsp_flags_get_page_size(space->flags)
!= page_size)) {
fprintf(stderr,
"InnoDB: Error: tablespace file %s"
" has page size 0x%lx\n"
"InnoDB: but the data dictionary"
" expects page size 0x%lx!\n",
node->name, flags,
fsp_flags_get_page_size(space->flags));
ut_error;
}
if (UNIV_UNLIKELY(space->flags != flags)) {
fprintf(stderr,
"InnoDB: Error: table flags are 0x%lx"
" in the data dictionary\n"
"InnoDB: but the flags in file %s are 0x%lx!\n",
space->flags, node->name, flags);
ut_error;
}
if (size_bytes >= 1024 * 1024) {
/* Truncate the size to whole megabytes. */
size_bytes = ut_2pow_round(size_bytes, 1024 * 1024);
}
if (!fsp_flags_is_compressed(flags)) {
node->size = (ulint) (size_bytes / UNIV_PAGE_SIZE);
} else {
node->size = (ulint)
(size_bytes
/ fsp_flags_get_zip_size(flags));
}
#ifdef UNIV_HOTBACKUP
add_size:
#endif /* UNIV_HOTBACKUP */
space->size += node->size;
}
/* printf("Opening file %s\n", node->name); */
/* Open the file for reading and writing, in Windows normally in the
unbuffered async I/O mode, though global variables may make
os_file_create() to fall back to the normal file I/O mode. */
if (space->purpose == FIL_LOG) {
node->handle = os_file_create(innodb_file_log_key,
node->name, OS_FILE_OPEN,
OS_FILE_AIO, OS_LOG_FILE,
&ret);
} else if (node->is_raw_disk) {
node->handle = os_file_create(innodb_file_data_key,
node->name,
OS_FILE_OPEN_RAW,
OS_FILE_AIO, OS_DATA_FILE,
&ret);
} else {
node->handle = os_file_create(innodb_file_data_key,
node->name, OS_FILE_OPEN,
OS_FILE_AIO, OS_DATA_FILE,
&ret);
}
ut_a(ret);
node->open = TRUE;
system->n_open++;
fil_n_file_opened++;
if (fil_space_belongs_in_lru(space)) {
/* Put the node to the LRU list */
UT_LIST_ADD_FIRST(LRU, system->LRU, node);
}
#ifdef RW_CMD
/* TODO: Generalize the path. Currently, only normal datafiles can use the RW command. */
if (strcmp(node->name, "./tpcc10_4k/warehouse.ibd") == 0) {
log_fd = space->id;
fprintf(stderr, "setting log_fd %s to %lu\n", node->name, log_fd);
}
#endif /* RW_CMD */
/* For log */
fprintf(stderr, "%s (space_id = %lu) with fd %d\n", node->name, space->id, node->handle);
return(true);
}
/**********************************************************************//**
Closes a file. */
static
void
fil_node_close_file(
/*================*/
fil_node_t* node, /*!< in: file node */
fil_system_t* system) /*!< in: tablespace memory cache */
{
ibool ret;
ut_ad(node && system);
ut_ad(mutex_own(&(system->mutex)));
ut_a(node->open);
ut_a(node->n_pending == 0);
ut_a(node->n_pending_flushes == 0);
ut_a(!node->being_extended);
#ifndef UNIV_HOTBACKUP
ut_a(node->modification_counter == node->flush_counter
|| srv_fast_shutdown == 2);
#endif /* !UNIV_HOTBACKUP */
ret = os_file_close(node->handle);
ut_a(ret);
/* printf("Closing file %s\n", node->name); */
node->open = FALSE;
ut_a(system->n_open > 0);
system->n_open--;
fil_n_file_opened--;
if (fil_space_belongs_in_lru(node->space)) {
ut_a(UT_LIST_GET_LEN(system->LRU) > 0);
/* The node is in the LRU list, remove it */
UT_LIST_REMOVE(LRU, system->LRU, node);
}
}
/********************************************************************//**
Tries to close a file in the LRU list. The caller must hold the fil_sys
mutex.
@return TRUE if success, FALSE if should retry later; since i/o's
generally complete in < 100 ms, and as InnoDB writes at most 128 pages
from the buffer pool in a batch, and then immediately flushes the
files, there is a good chance that the next time we find a suitable
node from the LRU list */
static
ibool
fil_try_to_close_file_in_LRU(
/*=========================*/
ibool print_info) /*!< in: if TRUE, prints information why it
cannot close a file */
{
fil_node_t* node;
ut_ad(mutex_own(&fil_system->mutex));
if (print_info) {
fprintf(stderr,
"InnoDB: fil_sys open file LRU len %lu\n",
(ulong) UT_LIST_GET_LEN(fil_system->LRU));
}
for (node = UT_LIST_GET_LAST(fil_system->LRU);
node != NULL;
node = UT_LIST_GET_PREV(LRU, node)) {
if (node->modification_counter == node->flush_counter
&& node->n_pending_flushes == 0
&& !node->being_extended) {
fil_node_close_file(node, fil_system);
return(TRUE);
}
if (!print_info) {
continue;
}
if (node->n_pending_flushes > 0) {
fputs("InnoDB: cannot close file ", stderr);
ut_print_filename(stderr, node->name);
fprintf(stderr, ", because n_pending_flushes %lu\n",
(ulong) node->n_pending_flushes);
}
if (node->modification_counter != node->flush_counter) {
fputs("InnoDB: cannot close file ", stderr);
ut_print_filename(stderr, node->name);
fprintf(stderr,
", because mod_count %ld != fl_count %ld\n",
(long) node->modification_counter,
(long) node->flush_counter);
}