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