mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / storage / myisam / mi_panic.c
blob41de42b4ba13e67b0593d642a74d59da973b0c00
1 /* Copyright (c) 2000, 2003, 2005, 2006 MySQL AB
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 of the License.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
16 #include "fulltext.h"
18 /* if flag == HA_PANIC_CLOSE then all misam files are closed */
19 /* if flag == HA_PANIC_WRITE then all misam files are unlocked and
20 all changed data in single user misam is written to file */
21 /* if flag == HA_PANIC_READ then all misam files that was locked when
22 mi_panic(HA_PANIC_WRITE) was done is locked. A mi_readinfo() is
23 done for all single user files to get changes in database */
26 int mi_panic(enum ha_panic_function flag)
28 int error=0;
29 LIST *list_element,*next_open;
30 MI_INFO *info;
31 DBUG_ENTER("mi_panic");
33 pthread_mutex_lock(&THR_LOCK_myisam);
34 for (list_element=myisam_open_list ; list_element ; list_element=next_open)
36 next_open=list_element->next; /* Save if close */
37 info=(MI_INFO*) list_element->data;
38 switch (flag) {
39 case HA_PANIC_CLOSE:
40 pthread_mutex_unlock(&THR_LOCK_myisam); /* Not exactly right... */
41 if (mi_close(info))
42 error=my_errno;
43 pthread_mutex_lock(&THR_LOCK_myisam);
44 break;
45 case HA_PANIC_WRITE: /* Do this to free databases */
46 #ifdef CANT_OPEN_FILES_TWICE
47 if (info->s->options & HA_OPTION_READ_ONLY_DATA)
48 break;
49 #endif
50 if (flush_key_blocks(info->s->key_cache, info->s->kfile, FLUSH_RELEASE))
51 error=my_errno;
52 if (info->opt_flag & WRITE_CACHE_USED)
53 if (flush_io_cache(&info->rec_cache))
54 error=my_errno;
55 if (info->opt_flag & READ_CACHE_USED)
57 if (flush_io_cache(&info->rec_cache))
58 error=my_errno;
59 reinit_io_cache(&info->rec_cache,READ_CACHE,0,
60 (pbool) (info->lock_type != F_UNLCK),1);
62 if (info->lock_type != F_UNLCK && ! info->was_locked)
64 info->was_locked=info->lock_type;
65 if (mi_lock_database(info,F_UNLCK))
66 error=my_errno;
68 #ifdef CANT_OPEN_FILES_TWICE
69 if (info->s->kfile >= 0 && my_close(info->s->kfile,MYF(0)))
70 error = my_errno;
71 if (info->dfile >= 0 && my_close(info->dfile,MYF(0)))
72 error = my_errno;
73 info->s->kfile=info->dfile= -1; /* Files aren't open anymore */
74 break;
75 #endif
76 case HA_PANIC_READ: /* Restore to before WRITE */
77 #ifdef CANT_OPEN_FILES_TWICE
78 { /* Open closed files */
79 char name_buff[FN_REFLEN];
80 if (info->s->kfile < 0)
81 if ((info->s->kfile= my_open(fn_format(name_buff,info->filename,"",
82 N_NAME_IEXT,4),info->mode,
83 MYF(MY_WME))) < 0)
84 error = my_errno;
85 if (info->dfile < 0)
87 if ((info->dfile= my_open(fn_format(name_buff,info->filename,"",
88 N_NAME_DEXT,4),info->mode,
89 MYF(MY_WME))) < 0)
90 error = my_errno;
91 info->rec_cache.file=info->dfile;
94 #endif
95 if (info->was_locked)
97 if (mi_lock_database(info, info->was_locked))
98 error=my_errno;
99 info->was_locked=0;
101 break;
104 if (flag == HA_PANIC_CLOSE)
106 VOID(mi_log(0)); /* Close log if neaded */
107 ft_free_stopwords();
109 pthread_mutex_unlock(&THR_LOCK_myisam);
110 if (!error)
111 DBUG_RETURN(0);
112 DBUG_RETURN(my_errno=error);
113 } /* mi_panic */