1 /* Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
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, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 /* close a isam-database */
24 /*
25   TODO:
26    We need to have a separate mutex on the closed file to allow other threads
27    to open other files during the time we flush the cache and close this file
28 */
29 
30 #include "myisamdef.h"
31 
mi_close_share(register MI_INFO * info,my_bool * closed_share)32 int mi_close_share(register MI_INFO *info, my_bool *closed_share)
33 {
34   int error=0,flag;
35   MYISAM_SHARE *share=info->s;
36   DBUG_ENTER("mi_close_share");
37   DBUG_PRINT("enter",("base: 0x%lx  reopen: %u  locks: %u",
38 		      (long) info, (uint) share->reopen,
39                       (uint) share->tot_locks));
40 
41   if (info->open_list.data)
42     mysql_mutex_lock(&THR_LOCK_myisam);
43   if (info->lock_type == F_EXTRA_LCK)
44     info->lock_type=F_UNLCK;			/* HA_EXTRA_NO_USER_CHANGE */
45 
46   if (info->lock_type != F_UNLCK)
47   {
48     if (mi_lock_database(info,F_UNLCK))
49       error=my_errno();
50   }
51   mysql_mutex_lock(&share->intern_lock);
52 
53   if (share->options & HA_OPTION_READ_ONLY_DATA)
54   {
55     share->r_locks--;
56     share->tot_locks--;
57   }
58   if (info->opt_flag & (READ_CACHE_USED | WRITE_CACHE_USED))
59   {
60     if (end_io_cache(&info->rec_cache))
61       error=my_errno();
62     info->opt_flag&= ~(READ_CACHE_USED | WRITE_CACHE_USED);
63   }
64   flag= !--share->reopen;
65   if (info->open_list.data)
66     myisam_open_list= list_delete(myisam_open_list, &info->open_list);
67   mysql_mutex_unlock(&share->intern_lock);
68 
69   my_free(mi_get_rec_buff_ptr(info, info->rec_buff));
70   if (flag)
71   {
72     DBUG_EXECUTE_IF("crash_before_flush_keys",
73                     if (share->kfile >= 0) abort(););
74     if (share->kfile >= 0 &&
75         flush_key_blocks(share->key_cache, keycache_thread_var(),
76                          share->kfile,
77 			 share->temporary ? FLUSH_IGNORE_CHANGED :
78 			 FLUSH_RELEASE))
79       error=my_errno();
80     if (share->kfile >= 0)
81     {
82       /*
83         If we are crashed, we can safely flush the current state as it will
84         not change the crashed state.
85         We can NOT write the state in other cases as other threads
86         may be using the file at this point
87       */
88       if (share->mode != O_RDONLY && mi_is_crashed(info))
89 	mi_state_info_write(share->kfile, &share->state, 1);
90       /* Decrement open count must be last I/O on this file. */
91       _mi_decrement_open_count(info);
92       if (mysql_file_close(share->kfile, MYF(0)))
93         error = my_errno();
94     }
95     if (share->file_map)
96     {
97       if (share->options & HA_OPTION_COMPRESS_RECORD)
98         _mi_unmap_file(info);
99       else
100         mi_munmap_file(info);
101     }
102     if (share->decode_trees)
103     {
104       my_free(share->decode_trees);
105       my_free(share->decode_tables);
106     }
107     thr_lock_delete(&share->lock);
108     mysql_mutex_destroy(&share->intern_lock);
109     {
110       int i,keys;
111       keys = share->state.header.keys;
112       mysql_rwlock_destroy(&share->mmap_lock);
113       for(i=0; i<keys; i++) {
114         mysql_rwlock_destroy(&share->key_root_lock[i]);
115       }
116     }
117     my_free(info->s);
118     if (closed_share)
119       *closed_share= TRUE;
120   }
121   if (info->open_list.data)
122     mysql_mutex_unlock(&THR_LOCK_myisam);
123   if (info->ftparser_param)
124   {
125     my_free(info->ftparser_param);
126     info->ftparser_param= 0;
127   }
128   if (info->dfile >= 0 && mysql_file_close(info->dfile, MYF(0)))
129     error = my_errno();
130 
131   myisam_log_command(MI_LOG_CLOSE,info,NULL,0,error);
132   my_free(info);
133 
134   if (error)
135   {
136     set_my_errno(error);
137     DBUG_RETURN(error);
138   }
139   DBUG_RETURN(0);
140 } /* mi_close_share */
141