~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000-2001, 2003 MySQL AB
2
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.
6
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.
11
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
75 by Brian Aker
Another round of cleanup of MyISAM
16
#include "myisamdef.h"
1 by brian
clean slate
17
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 */
24
25
26
int mi_panic(enum ha_panic_function flag)
27
{
28
  int error=0;
29
  LIST *list_element,*next_open;
30
  MI_INFO *info;
31
32
  pthread_mutex_lock(&THR_LOCK_myisam);
33
  for (list_element=myisam_open_list ; list_element ; list_element=next_open)
34
  {
35
    next_open=list_element->next;		/* Save if close */
36
    info=(MI_INFO*) list_element->data;
37
    switch (flag) {
38
    case HA_PANIC_CLOSE:
39
      pthread_mutex_unlock(&THR_LOCK_myisam);	/* Not exactly right... */
40
      if (mi_close(info))
41
	error=my_errno;
42
      pthread_mutex_lock(&THR_LOCK_myisam);
43
      break;
44
    case HA_PANIC_WRITE:		/* Do this to free databases */
45
#ifdef CANT_OPEN_FILES_TWICE
46
      if (info->s->options & HA_OPTION_READ_ONLY_DATA)
47
	break;
48
#endif
49
      if (flush_key_blocks(info->s->key_cache, info->s->kfile, FLUSH_RELEASE))
50
	error=my_errno;
51
      if (info->opt_flag & WRITE_CACHE_USED)
52
	if (flush_io_cache(&info->rec_cache))
53
	  error=my_errno;
54
      if (info->opt_flag & READ_CACHE_USED)
55
      {
56
	if (flush_io_cache(&info->rec_cache))
57
	  error=my_errno;
58
	reinit_io_cache(&info->rec_cache,READ_CACHE,0,
154 by Brian Aker
Removed oddball types in my_global.h
59
		       (bool) (info->lock_type != F_UNLCK),1);
1 by brian
clean slate
60
      }
61
      if (info->lock_type != F_UNLCK && ! info->was_locked)
62
      {
63
	info->was_locked=info->lock_type;
64
	if (mi_lock_database(info,F_UNLCK))
65
	  error=my_errno;
66
      }
67
#ifdef CANT_OPEN_FILES_TWICE
68
      if (info->s->kfile >= 0 && my_close(info->s->kfile,MYF(0)))
69
	error = my_errno;
70
      if (info->dfile >= 0 && my_close(info->dfile,MYF(0)))
71
	error = my_errno;
72
      info->s->kfile=info->dfile= -1;	/* Files aren't open anymore */
73
      break;
74
#endif
75
    case HA_PANIC_READ:			/* Restore to before WRITE */
76
#ifdef CANT_OPEN_FILES_TWICE
77
      {					/* Open closed files */
78
	char name_buff[FN_REFLEN];
79
	if (info->s->kfile < 0)
80
	  if ((info->s->kfile= my_open(fn_format(name_buff,info->filename,"",
81
					      N_NAME_IEXT,4),info->mode,
82
				    MYF(MY_WME))) < 0)
83
	    error = my_errno;
84
	if (info->dfile < 0)
85
	{
86
	  if ((info->dfile= my_open(fn_format(name_buff,info->filename,"",
87
					      N_NAME_DEXT,4),info->mode,
88
				    MYF(MY_WME))) < 0)
89
	    error = my_errno;
90
	  info->rec_cache.file=info->dfile;
91
	}
92
      }
93
#endif
94
      if (info->was_locked)
95
      {
96
	if (mi_lock_database(info, info->was_locked))
97
	  error=my_errno;
98
	info->was_locked=0;
99
      }
100
      break;
101
    }
102
  }
103
  if (flag == HA_PANIC_CLOSE)
104
  {
105
    VOID(mi_log(0));				/* Close log if neaded */
106
  }
107
  pthread_mutex_unlock(&THR_LOCK_myisam);
108
  if (!error)
51.1.106 by Jay Pipes
Removed/replaced DBUG symbols and TRUE/FALSE
109
    return(0);
110
  return(my_errno=error);
1 by brian
clean slate
111
} /* mi_panic */