1 /* Copyright (c) 2003, 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 /*
24   Key cache assignments
25 */
26 
27 #include "my_compiler.h"
28 #include "my_dbug.h"
29 #include "my_inttypes.h"
30 #include "storage/myisam/myisamdef.h"
31 
32 /*
33   Assign pages of the index file for a table to a key cache
34 
35   SYNOPSIS
36     mi_assign_to_key_cache()
37       info          open table
38       key_map       map of indexes to assign to the key cache
39       key_cache_ptr pointer to the key cache handle
40       assign_lock   Mutex to lock during assignment
41 
42   PREREQUESTS
43     One must have a READ lock or a WRITE lock on the table when calling
44     the function to ensure that there is no other writers to it.
45 
46     The caller must also ensure that one doesn't call this function from
47     two different threads with the same table.
48 
49   NOTES
50     At present pages for all indexes must be assigned to the same key cache.
51     In future only pages for indexes specified in the key_map parameter
52     of the table will be assigned to the specified key cache.
53 
54   RETURN VALUE
55     0  If a success
56     #  Error code
57 */
58 
mi_assign_to_key_cache(MI_INFO * info,ulonglong key_map MY_ATTRIBUTE ((unused)),KEY_CACHE * key_cache)59 int mi_assign_to_key_cache(MI_INFO *info,
60                            ulonglong key_map MY_ATTRIBUTE((unused)),
61                            KEY_CACHE *key_cache) {
62   int error = 0;
63   MYISAM_SHARE *share = info->s;
64   DBUG_TRACE;
65   DBUG_PRINT("enter", ("old_key_cache_handle: %p  new_key_cache_handle: %p",
66                        share->key_cache, key_cache));
67 
68   /*
69     Skip operation if we didn't change key cache. This can happen if we
70     call this for all open instances of the same table
71   */
72   if (share->key_cache == key_cache) return 0;
73 
74   /*
75     First flush all blocks for the table in the old key cache.
76     This is to ensure that the disk is consistent with the data pages
77     in memory (which may not be the case if the table uses delayed_key_write)
78 
79     Note that some other read thread may still fill in the key cache with
80     new blocks during this call and after, but this doesn't matter as
81     all threads will start using the new key cache for their next call to
82     myisam library and we know that there will not be any changed blocks
83     in the old key cache.
84   */
85 
86   if (flush_key_blocks(share->key_cache, keycache_thread_var(), share->kfile,
87                        FLUSH_RELEASE)) {
88     error = my_errno();
89     mi_print_error(info->s, HA_ERR_CRASHED);
90     mi_mark_crashed(info); /* Mark that table must be checked */
91   }
92 
93   /*
94     Flush the new key cache for this file.  This is needed to ensure
95     that there is no old blocks (with outdated data) left in the new key
96     cache from an earlier assign_to_keycache operation
97 
98     (This can never fail as there is never any not written data in the
99     new key cache)
100   */
101   (void)flush_key_blocks(key_cache, keycache_thread_var(), share->kfile,
102                          FLUSH_RELEASE);
103 
104   /*
105     ensure that setting the key cache and changing the multi_key_cache
106     is done atomicly
107   */
108   mysql_mutex_lock(&share->intern_lock);
109   /*
110     Tell all threads to use the new key cache
111     This should be seen at the lastes for the next call to an myisam function.
112   */
113   share->key_cache = key_cache;
114 
115   /* store the key cache in the global hash structure for future opens */
116   if (multi_key_cache_set((uchar *)share->unique_file_name,
117                           share->unique_name_length, share->key_cache))
118     error = my_errno();
119   mysql_mutex_unlock(&share->intern_lock);
120   return error;
121 }
122 
123 /*
124   Change all MyISAM entries that uses one key cache to another key cache
125 
126   SYNOPSIS
127     mi_change_key_cache()
128     old_key_cache	Old key cache
129     new_key_cache	New key cache
130 
131   NOTES
132     This is used when we delete one key cache.
133 
134     To handle the case where some other threads tries to open an MyISAM
135     table associated with the to-be-deleted key cache while this operation
136     is running, we have to call 'multi_key_cache_change()' from this
137     function while we have a lock on the MyISAM table list structure.
138 
139     This is safe as long as it's only MyISAM that is using this specific
140     key cache.
141 */
142 
mi_change_key_cache(KEY_CACHE * old_key_cache,KEY_CACHE * new_key_cache)143 void mi_change_key_cache(KEY_CACHE *old_key_cache, KEY_CACHE *new_key_cache) {
144   LIST *pos;
145   DBUG_TRACE;
146 
147   /*
148     Lock list to ensure that no one can close the table while we manipulate it
149   */
150   mysql_mutex_lock(&THR_LOCK_myisam);
151   for (pos = myisam_open_list; pos; pos = pos->next) {
152     MI_INFO *info = (MI_INFO *)pos->data;
153     MYISAM_SHARE *share = info->s;
154     if (share->key_cache == old_key_cache)
155       mi_assign_to_key_cache(info, (ulonglong)~0, new_key_cache);
156   }
157 
158   /*
159     We have to do the following call while we have the lock on the
160     MyISAM list structure to ensure that another thread is not trying to
161     open a new table that will be associted with the old key cache
162   */
163   multi_key_cache_change(old_key_cache, new_key_cache);
164   mysql_mutex_unlock(&THR_LOCK_myisam);
165 }
166