1 /* Copyright (c) 2000, 2011, 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(MI_INFO * info)32 int mi_close(MI_INFO *info)
33 {
34 int error=0,flag;
35 MYISAM_SHARE *share=info->s;
36 DBUG_ENTER("mi_close");
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, share->kfile,
76 share->temporary ? FLUSH_IGNORE_CHANGED :
77 FLUSH_RELEASE))
78 error=my_errno;
79 if (share->kfile >= 0)
80 {
81 /*
82 If we are crashed, we can safely flush the current state as it will
83 not change the crashed state.
84 We can NOT write the state in other cases as other threads
85 may be using the file at this point
86 */
87 if (share->mode != O_RDONLY && mi_is_crashed(info))
88 mi_state_info_write(share->kfile, &share->state, 1);
89 /* Decrement open count must be last I/O on this file. */
90 _mi_decrement_open_count(info);
91 if (mysql_file_close(share->kfile, MYF(0)))
92 error = my_errno;
93 }
94 #ifdef HAVE_MMAP
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 #endif
103 if (share->decode_trees)
104 {
105 my_free(share->decode_trees);
106 my_free(share->decode_tables);
107 }
108 thr_lock_delete(&share->lock);
109 mysql_mutex_destroy(&share->intern_lock);
110 {
111 int i,keys;
112 keys = share->state.header.keys;
113 mysql_rwlock_destroy(&share->mmap_lock);
114 for(i=0; i<keys; i++) {
115 mysql_rwlock_destroy(&share->key_root_lock[i]);
116 }
117 }
118 my_free(info->s);
119 }
120 if (info->open_list.data)
121 mysql_mutex_unlock(&THR_LOCK_myisam);
122 if (info->ftparser_param)
123 {
124 my_free(info->ftparser_param);
125 info->ftparser_param= 0;
126 }
127 if (info->dfile >= 0 && mysql_file_close(info->dfile, MYF(0)))
128 error = my_errno;
129
130 myisam_log_command(MI_LOG_CLOSE,info,NULL,0,error);
131 my_free(info);
132
133 if (error)
134 {
135 DBUG_RETURN(my_errno=error);
136 }
137 DBUG_RETURN(0);
138 } /* mi_close */
139