1 /* Copyright (c) 2000, 2019, 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 <fcntl.h>
31 #include <stdlib.h>
32 #include <sys/types.h>
33
34 #include "my_dbug.h"
35 #include "my_inttypes.h"
36 #include "storage/myisam/myisamdef.h"
37
mi_close_share(MI_INFO * info,bool * closed_share)38 int mi_close_share(MI_INFO *info, bool *closed_share) {
39 int error = 0, flag;
40 MYISAM_SHARE *share = info->s;
41 DBUG_TRACE;
42 DBUG_PRINT("enter", ("base: %p reopen: %u locks: %u", info,
43 (uint)share->reopen, (uint)share->tot_locks));
44
45 if (info->open_list.data) mysql_mutex_lock(&THR_LOCK_myisam);
46 if (info->lock_type == F_EXTRA_LCK)
47 info->lock_type = F_UNLCK; /* HA_EXTRA_NO_USER_CHANGE */
48
49 if (info->lock_type != F_UNLCK) {
50 if (mi_lock_database(info, F_UNLCK)) error = my_errno();
51 }
52 mysql_mutex_lock(&share->intern_lock);
53
54 if (share->options & HA_OPTION_READ_ONLY_DATA) {
55 share->r_locks--;
56 share->tot_locks--;
57 }
58 if (info->opt_flag & (READ_CACHE_USED | WRITE_CACHE_USED)) {
59 if (end_io_cache(&info->rec_cache)) error = my_errno();
60 info->opt_flag &= ~(READ_CACHE_USED | WRITE_CACHE_USED);
61 }
62 flag = !--share->reopen;
63 if (info->open_list.data)
64 myisam_open_list = list_delete(myisam_open_list, &info->open_list);
65 mysql_mutex_unlock(&share->intern_lock);
66
67 my_free(mi_get_rec_buff_ptr(info, info->rec_buff));
68 if (flag) {
69 DBUG_EXECUTE_IF("crash_before_flush_keys", if (share->kfile >= 0) abort(););
70 if (share->kfile >= 0 &&
71 flush_key_blocks(
72 share->key_cache, keycache_thread_var(), share->kfile,
73 share->temporary ? FLUSH_IGNORE_CHANGED : FLUSH_RELEASE))
74 error = my_errno();
75 if (share->kfile >= 0) {
76 /*
77 If we are crashed, we can safely flush the current state as it will
78 not change the crashed state.
79 We can NOT write the state in other cases as other threads
80 may be using the file at this point
81 */
82 if (share->mode != O_RDONLY && mi_is_crashed(info))
83 mi_state_info_write(share->kfile, &share->state, 1);
84 /* Decrement open count must be last I/O on this file. */
85 _mi_decrement_open_count(info);
86 if (mysql_file_close(share->kfile, MYF(0))) error = my_errno();
87 }
88 if (share->file_map) {
89 if (share->options & HA_OPTION_COMPRESS_RECORD)
90 _mi_unmap_file(info);
91 else
92 mi_munmap_file(info);
93 }
94 if (share->decode_trees) {
95 my_free(share->decode_trees);
96 my_free(share->decode_tables);
97 }
98 thr_lock_delete(&share->lock);
99 mysql_mutex_destroy(&share->intern_lock);
100 {
101 int i, keys;
102 keys = share->state.header.keys;
103 mysql_rwlock_destroy(&share->mmap_lock);
104 for (i = 0; i < keys; i++) {
105 mysql_rwlock_destroy(&share->key_root_lock[i]);
106 }
107 }
108 my_free(info->s);
109 if (closed_share) *closed_share = true;
110 }
111 if (info->open_list.data) mysql_mutex_unlock(&THR_LOCK_myisam);
112 if (info->ftparser_param) {
113 my_free(info->ftparser_param);
114 info->ftparser_param = nullptr;
115 }
116 if (info->dfile >= 0 && mysql_file_close(info->dfile, MYF(0)))
117 error = my_errno();
118
119 myisam_log_command(MI_LOG_CLOSE, info, nullptr, 0, error);
120 my_free(info);
121
122 if (error) {
123 set_my_errno(error);
124 return error;
125 }
126 return 0;
127 } /* mi_close_share */
128