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