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