MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mi_panic.c
1 /* Copyright (c) 2000, 2003, 2005, 2006 MySQL AB, 2009 Sun Microsystems, Inc.
2  Use is subject to license terms.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
16 
17 #include "fulltext.h"
18 
19  /* if flag == HA_PANIC_CLOSE then all misam files are closed */
20  /* if flag == HA_PANIC_WRITE then all misam files are unlocked and
21  all changed data in single user misam is written to file */
22  /* if flag == HA_PANIC_READ then all misam files that was locked when
23  mi_panic(HA_PANIC_WRITE) was done is locked. A mi_readinfo() is
24  done for all single user files to get changes in database */
25 
26 
27 int mi_panic(enum ha_panic_function flag)
28 {
29  int error=0;
30  LIST *list_element,*next_open;
31  MI_INFO *info;
32  DBUG_ENTER("mi_panic");
33 
34  mysql_mutex_lock(&THR_LOCK_myisam);
35  for (list_element=myisam_open_list ; list_element ; list_element=next_open)
36  {
37  next_open=list_element->next; /* Save if close */
38  info=(MI_INFO*) list_element->data;
39  switch (flag) {
40  case HA_PANIC_CLOSE:
41  mysql_mutex_unlock(&THR_LOCK_myisam); /* Not exactly right... */
42  if (mi_close(info))
43  error=my_errno;
44  mysql_mutex_lock(&THR_LOCK_myisam);
45  break;
46  case HA_PANIC_WRITE: /* Do this to free databases */
47 #ifdef CANT_OPEN_FILES_TWICE
48  if (info->s->options & HA_OPTION_READ_ONLY_DATA)
49  break;
50 #endif
51  if (flush_key_blocks(info->s->key_cache, info->s->kfile, FLUSH_RELEASE))
52  error=my_errno;
53  if (info->opt_flag & WRITE_CACHE_USED)
54  if (flush_io_cache(&info->rec_cache))
55  error=my_errno;
56  if (info->opt_flag & READ_CACHE_USED)
57  {
58  if (flush_io_cache(&info->rec_cache))
59  error=my_errno;
60  reinit_io_cache(&info->rec_cache,READ_CACHE,0,
61  (pbool) (info->lock_type != F_UNLCK),1);
62  }
63  if (info->lock_type != F_UNLCK && ! info->was_locked)
64  {
65  info->was_locked=info->lock_type;
66  if (mi_lock_database(info,F_UNLCK))
67  error=my_errno;
68  }
69 #ifdef CANT_OPEN_FILES_TWICE
70  if (info->s->kfile >= 0 && mysql_file_close(info->s->kfile, MYF(0)))
71  error = my_errno;
72  if (info->dfile >= 0 && mysql_file_close(info->dfile, MYF(0)))
73  error = my_errno;
74  info->s->kfile=info->dfile= -1; /* Files aren't open anymore */
75  break;
76 #endif
77  case HA_PANIC_READ: /* Restore to before WRITE */
78 #ifdef CANT_OPEN_FILES_TWICE
79  { /* Open closed files */
80  char name_buff[FN_REFLEN];
81  if (info->s->kfile < 0)
82  if ((info->s->kfile= mysql_file_open(mi_key_file_kfile,
83  fn_format(name_buff,
84  info->filename, "",
85  N_NAME_IEXT, 4),
86  info->mode, MYF(MY_WME))) < 0)
87  error = my_errno;
88  if (info->dfile < 0)
89  {
90  if ((info->dfile= mysql_file_open(mi_key_file_dfile,
91  fn_format(name_buff,
92  info->filename, "",
93  N_NAME_DEXT, 4),
94  info->mode, MYF(MY_WME))) < 0)
95  error = my_errno;
96  info->rec_cache.file=info->dfile;
97  }
98  }
99 #endif
100  if (info->was_locked)
101  {
102  if (mi_lock_database(info, info->was_locked))
103  error=my_errno;
104  info->was_locked=0;
105  }
106  break;
107  }
108  }
109  if (flag == HA_PANIC_CLOSE)
110  {
111  (void) mi_log(0); /* Close log if neaded */
112  ft_free_stopwords();
113  }
114  mysql_mutex_unlock(&THR_LOCK_myisam);
115  if (!error)
116  DBUG_RETURN(0);
117  DBUG_RETURN(my_errno=error);
118 } /* mi_panic */