1 /* Copyright (c) 2010, 2016, Oracle and/or its affiliates.
2 Copyright (c) 2011, 2016, MariaDB
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */
16
17 #include "mariadb.h"
18 #include "sql_reload.h"
19 #include "sql_priv.h"
20 #include "mysqld.h" // select_errors
21 #include "sql_class.h" // THD
22 #include "sql_acl.h" // acl_reload
23 #include "sql_servers.h" // servers_reload
24 #include "sql_connect.h" // reset_mqh
25 #include "sql_base.h" // close_cached_tables
26 #include "sql_db.h" // my_dbopt_cleanup
27 #include "hostname.h" // hostname_cache_refresh
28 #include "sql_repl.h" // reset_master, reset_slave
29 #include "rpl_mi.h" // Master_info::data_lock
30 #include "sql_show.h"
31 #include "debug_sync.h"
32 #include "des_key_file.h"
33 #include "transaction.h"
34
35 static void disable_checkpoints(THD *thd);
36
37 /**
38 Reload/resets privileges and the different caches.
39
40 @param thd Thread handler (can be NULL!)
41 @param options What should be reset/reloaded (tables, privileges, slave...)
42 @param tables Tables to flush (if any)
43 @param write_to_binlog < 0 if there was an error while interacting with the binary log inside
44 reload_acl_and_cache,
45 0 if we should not write to the binary log,
46 > 0 if we can write to the binlog.
47
48
49 @note Depending on 'options', it may be very bad to write the
50 query to the binlog (e.g. FLUSH SLAVE); this is a
51 pointer where reload_acl_and_cache() will put 0 if
52 it thinks we really should not write to the binlog.
53 Otherwise it will put 1.
54
55 @return Error status code
56 @retval 0 Ok
57 @retval !=0 Error; thd->killed is set or thd->is_error() is true
58 */
59
reload_acl_and_cache(THD * thd,unsigned long long options,TABLE_LIST * tables,int * write_to_binlog)60 bool reload_acl_and_cache(THD *thd, unsigned long long options,
61 TABLE_LIST *tables, int *write_to_binlog)
62 {
63 bool result=0;
64 select_errors=0; /* Write if more errors */
65 int tmp_write_to_binlog= *write_to_binlog= 1;
66
67 DBUG_ASSERT(!thd || !thd->in_sub_stmt);
68
69 #ifndef NO_EMBEDDED_ACCESS_CHECKS
70 if (options & REFRESH_GRANT)
71 {
72 THD *tmp_thd= 0;
73 /*
74 If reload_acl_and_cache() is called from SIGHUP handler we have to
75 allocate temporary THD for execution of acl_reload()/grant_reload().
76 */
77 if (unlikely(!thd) && (thd= (tmp_thd= new THD(0))))
78 {
79 thd->thread_stack= (char*) &tmp_thd;
80 thd->store_globals();
81 }
82
83 if (likely(thd))
84 {
85 bool reload_acl_failed= acl_reload(thd);
86 bool reload_grants_failed= grant_reload(thd);
87 bool reload_servers_failed= servers_reload(thd);
88
89 if (reload_acl_failed || reload_grants_failed || reload_servers_failed)
90 {
91 result= 1;
92 /*
93 When an error is returned, my_message may have not been called and
94 the client will hang waiting for a response.
95 */
96 my_error(ER_UNKNOWN_ERROR, MYF(0));
97 }
98 }
99 opt_noacl= 0;
100
101 if (unlikely(tmp_thd))
102 {
103 delete tmp_thd;
104 thd= 0;
105 }
106 reset_mqh((LEX_USER *)NULL, TRUE);
107 }
108 #endif
109 if (options & REFRESH_LOG)
110 {
111 /*
112 Flush the normal query log, the update log, the binary log,
113 the slow query log, the relay log (if it exists) and the log
114 tables.
115 */
116
117 options|= REFRESH_BINARY_LOG;
118 options|= REFRESH_RELAY_LOG;
119 options|= REFRESH_SLOW_LOG;
120 options|= REFRESH_GENERAL_LOG;
121 options|= REFRESH_ENGINE_LOG;
122 options|= REFRESH_ERROR_LOG;
123 }
124
125 if (options & REFRESH_ERROR_LOG)
126 if (unlikely(flush_error_log()))
127 result= 1;
128
129 if ((options & REFRESH_SLOW_LOG) && global_system_variables.sql_log_slow)
130 logger.flush_slow_log();
131
132 if ((options & REFRESH_GENERAL_LOG) && opt_log)
133 logger.flush_general_log();
134
135 if (options & REFRESH_ENGINE_LOG)
136 if (ha_flush_logs(NULL))
137 result= 1;
138
139 if (options & REFRESH_BINARY_LOG)
140 {
141 /*
142 Writing this command to the binlog may result in infinite loops
143 when doing mysqlbinlog|mysql, and anyway it does not really make
144 sense to log it automatically (would cause more trouble to users
145 than it would help them)
146 */
147 tmp_write_to_binlog= 0;
148 if (mysql_bin_log.is_open())
149 {
150 DYNAMIC_ARRAY *drop_gtid_domain=
151 (thd && (thd->lex->delete_gtid_domain.elements > 0)) ?
152 &thd->lex->delete_gtid_domain : NULL;
153 if (mysql_bin_log.rotate_and_purge(true, drop_gtid_domain))
154 *write_to_binlog= -1;
155
156 if (WSREP_ON)
157 {
158 /* Wait for last binlog checkpoint event to be logged. */
159 mysql_bin_log.wait_for_last_checkpoint_event();
160 }
161 }
162 }
163 if (options & REFRESH_RELAY_LOG)
164 {
165 #ifdef HAVE_REPLICATION
166 LEX_CSTRING connection_name;
167 Master_info *mi;
168 if (thd)
169 connection_name= thd->lex->relay_log_connection_name;
170 else
171 {
172 connection_name.str= (char*) "";
173 connection_name.length= 0;
174 }
175
176 /*
177 Writing this command to the binlog may cause problems as the
178 slave is not likely to have the same connection names.
179 */
180 tmp_write_to_binlog= 0;
181 if (connection_name.length == 0)
182 {
183 if (master_info_index->flush_all_relay_logs())
184 *write_to_binlog= -1;
185 }
186 else if (!(mi= (get_master_info(&connection_name,
187 Sql_condition::WARN_LEVEL_ERROR))))
188 {
189 result= 1;
190 }
191 else
192 {
193 mysql_mutex_lock(&mi->data_lock);
194 if (rotate_relay_log(mi))
195 *write_to_binlog= -1;
196 mysql_mutex_unlock(&mi->data_lock);
197 mi->release();
198 }
199 #endif
200 }
201 #ifdef HAVE_QUERY_CACHE
202 if (options & REFRESH_QUERY_CACHE_FREE)
203 {
204 query_cache.pack(thd); // FLUSH QUERY CACHE
205 options &= ~REFRESH_QUERY_CACHE; // Don't flush cache, just free memory
206 }
207 if (options & (REFRESH_TABLES | REFRESH_QUERY_CACHE))
208 {
209 query_cache.flush(); // RESET QUERY CACHE
210 }
211 #endif /*HAVE_QUERY_CACHE*/
212
213 DBUG_ASSERT(!thd || thd->locked_tables_mode ||
214 !thd->mdl_context.has_locks() ||
215 thd->handler_tables_hash.records ||
216 thd->ull_hash.records ||
217 thd->global_read_lock.is_acquired());
218
219 /*
220 Note that if REFRESH_READ_LOCK bit is set then REFRESH_TABLES is set too
221 (see sql_yacc.yy)
222 */
223 if (options & (REFRESH_TABLES | REFRESH_READ_LOCK))
224 {
225 if ((options & REFRESH_READ_LOCK) && thd)
226 {
227 /*
228 On the first hand we need write lock on the tables to be flushed,
229 on the other hand we must not try to aspire a global read lock
230 if we have a write locked table as this would lead to a deadlock
231 when trying to reopen (and re-lock) the table after the flush.
232 */
233 if (thd->locked_tables_mode)
234 {
235 my_error(ER_LOCK_OR_ACTIVE_TRANSACTION, MYF(0));
236 return 1;
237 }
238 /*
239 Writing to the binlog could cause deadlocks, as we don't log
240 UNLOCK TABLES
241 */
242 tmp_write_to_binlog= 0;
243 if (thd->global_read_lock.lock_global_read_lock(thd))
244 return 1; // Killed
245 if (close_cached_tables(thd, tables,
246 ((options & REFRESH_FAST) ? FALSE : TRUE),
247 thd->variables.lock_wait_timeout))
248 {
249 /*
250 NOTE: my_error() has been already called by reopen_tables() within
251 close_cached_tables().
252 */
253 thd->global_read_lock.unlock_global_read_lock(thd);
254 return 1;
255 }
256
257 if (thd->global_read_lock.make_global_read_lock_block_commit(thd)) // Killed
258 {
259 /* Don't leave things in a half-locked state */
260 thd->global_read_lock.unlock_global_read_lock(thd);
261 return 1;
262 }
263 if (options & REFRESH_CHECKPOINT)
264 disable_checkpoints(thd);
265 /*
266 We need to do it second time after wsrep appliers were blocked in
267 make_global_read_lock_block_commit(thd) above since they could have
268 modified the tables too.
269 */
270 if (WSREP(thd) &&
271 close_cached_tables(thd, tables, (options & REFRESH_FAST) ?
272 FALSE : TRUE, TRUE))
273 result= 1;
274 }
275 else
276 {
277 if (thd && thd->locked_tables_mode)
278 {
279 /*
280 If we are under LOCK TABLES we should have a write
281 lock on tables which we are going to flush.
282 */
283 if (tables)
284 {
285 int err;
286 for (TABLE_LIST *t= tables; t; t= t->next_local)
287 if (!find_table_for_mdl_upgrade(thd, t->db.str, t->table_name.str, &err))
288 {
289 if (is_locked_view(thd, t))
290 t->next_local= t->next_global;
291 else
292 {
293 my_error(err, MYF(0), t->table_name.str);
294 return 1;
295 }
296 }
297 }
298 else
299 {
300 /*
301 It is not safe to upgrade the metadata lock without GLOBAL IX lock.
302 This can happen with FLUSH TABLES <list> WITH READ LOCK as we in
303 these cases don't take a GLOBAL IX lock in order to be compatible
304 with global read lock.
305 */
306 if (thd->open_tables &&
307 !thd->mdl_context.is_lock_owner(MDL_key::GLOBAL, "", "",
308 MDL_INTENTION_EXCLUSIVE))
309 {
310 my_error(ER_TABLE_NOT_LOCKED_FOR_WRITE, MYF(0),
311 thd->open_tables->s->table_name.str);
312 return true;
313 }
314
315 for (TABLE *tab= thd->open_tables; tab; tab= tab->next)
316 {
317 if (! tab->mdl_ticket->is_upgradable_or_exclusive())
318 {
319 my_error(ER_TABLE_NOT_LOCKED_FOR_WRITE, MYF(0),
320 tab->s->table_name.str);
321 return 1;
322 }
323 }
324 }
325 }
326
327 #ifdef WITH_WSREP
328 if (thd && thd->wsrep_applier)
329 {
330 /*
331 In case of applier thread, do not wait for table share(s) to be
332 removed from table definition cache.
333 */
334 options|= REFRESH_FAST;
335 }
336 #endif
337 if (close_cached_tables(thd, tables,
338 ((options & REFRESH_FAST) ? FALSE : TRUE),
339 (thd ? thd->variables.lock_wait_timeout :
340 LONG_TIMEOUT)))
341 {
342 /*
343 NOTE: my_error() has been already called by reopen_tables() within
344 close_cached_tables().
345 */
346 result= 1;
347 }
348 }
349 my_dbopt_cleanup();
350 }
351 if (options & REFRESH_HOSTS)
352 hostname_cache_refresh();
353 if (thd && (options & REFRESH_STATUS))
354 refresh_status(thd);
355 if (options & REFRESH_THREADS)
356 flush_thread_cache();
357 #ifdef HAVE_REPLICATION
358 if (options & REFRESH_MASTER)
359 {
360 DBUG_ASSERT(thd);
361 tmp_write_to_binlog= 0;
362 if (reset_master(thd, NULL, 0, thd->lex->next_binlog_file_number))
363 {
364 /* NOTE: my_error() has been already called by reset_master(). */
365 result= 1;
366 }
367 }
368 #endif
369 #ifdef HAVE_OPENSSL
370 if (options & REFRESH_DES_KEY_FILE)
371 {
372 if (des_key_file && load_des_key_file(des_key_file))
373 {
374 /* NOTE: my_error() has been already called by load_des_key_file(). */
375 result= 1;
376 }
377 }
378 #endif
379 #ifdef HAVE_REPLICATION
380 if (options & REFRESH_SLAVE)
381 {
382 LEX_MASTER_INFO* lex_mi= &thd->lex->mi;
383 Master_info *mi;
384 tmp_write_to_binlog= 0;
385
386 if (!(mi= get_master_info(&lex_mi->connection_name,
387 Sql_condition::WARN_LEVEL_ERROR)))
388 {
389 result= 1;
390 }
391 else
392 {
393 /* The following will fail if slave is running */
394 if (reset_slave(thd, mi))
395 {
396 mi->release();
397 /* NOTE: my_error() has been already called by reset_slave(). */
398 result= 1;
399 }
400 else if (mi->connection_name.length && thd->lex->reset_slave_info.all)
401 {
402 /* If not default connection and 'all' is used */
403 mi->release();
404 mysql_mutex_lock(&LOCK_active_mi);
405 if (master_info_index->remove_master_info(mi))
406 result= 1;
407 mysql_mutex_unlock(&LOCK_active_mi);
408 }
409 else
410 mi->release();
411 }
412 }
413 #endif
414 if (options & REFRESH_USER_RESOURCES)
415 reset_mqh((LEX_USER *) NULL, 0); /* purecov: inspected */
416 if (options & REFRESH_GENERIC)
417 {
418 List_iterator_fast<LEX_CSTRING> li(thd->lex->view_list);
419 LEX_CSTRING *ls;
420 while ((ls= li++))
421 {
422 ST_SCHEMA_TABLE *table= find_schema_table(thd, ls);
423 if (table->reset_table())
424 result= 1;
425 }
426 }
427 if (*write_to_binlog != -1)
428 *write_to_binlog= tmp_write_to_binlog;
429 /*
430 If the query was killed then this function must fail.
431 */
432 return result || (thd ? thd->killed : 0);
433 }
434
435
436 /**
437 Implementation of FLUSH TABLES <table_list> WITH READ LOCK
438 and FLUSH TABLES <table_list> FOR EXPORT
439
440 In brief: take exclusive locks, expel tables from the table
441 cache, reopen the tables, enter the 'LOCKED TABLES' mode,
442 downgrade the locks.
443 Note: the function is written to be called from
444 mysql_execute_command(), it is not reusable in arbitrary
445 execution context.
446
447 Required privileges
448 -------------------
449 Since the statement implicitly enters LOCK TABLES mode,
450 it requires LOCK TABLES privilege on every table.
451 But since the rest of FLUSH commands require
452 the global RELOAD_ACL, it also requires RELOAD_ACL.
453
454 Compatibility with the global read lock
455 ---------------------------------------
456 We don't wait for the GRL, since neither the
457 5.1 combination that this new statement is intended to
458 replace (LOCK TABLE <list> WRITE; FLUSH TABLES;),
459 nor FLUSH TABLES WITH READ LOCK do.
460 @todo: this is not implemented, Dmitry disagrees.
461 Currently we wait for GRL in another connection,
462 but are compatible with a GRL in our own connection.
463
464 Behaviour under LOCK TABLES
465 ---------------------------
466 Bail out: i.e. don't perform an implicit UNLOCK TABLES.
467 This is not consistent with LOCK TABLES statement, but is
468 in line with behaviour of FLUSH TABLES WITH READ LOCK, and we
469 try to not introduce any new statements with implicit
470 semantics.
471
472 Compatibility with parallel updates
473 -----------------------------------
474 As a result, we will wait for all open transactions
475 against the tables to complete. After the lock downgrade,
476 new transactions will be able to read the tables, but not
477 write to them.
478
479 Differences from FLUSH TABLES <list>
480 -------------------------------------
481 - you can't flush WITH READ LOCK a non-existent table
482 - you can't flush WITH READ LOCK under LOCK TABLES
483
484 Effect on views and temporary tables.
485 ------------------------------------
486 You can only apply this command to existing base tables.
487 If a view with such name exists, ER_WRONG_OBJECT is returned.
488 If a temporary table with such name exists, it's ignored:
489 if there is a base table, it's used, otherwise ER_NO_SUCH_TABLE
490 is returned.
491
492 Handling of MERGE tables
493 ------------------------
494 For MERGE table this statement will open and lock child tables
495 for read (it is impossible to lock parent table without it).
496 Child tables won't be flushed unless they are explicitly present
497 in the statement's table list.
498
499 Implicit commit
500 ---------------
501 This statement causes an implicit commit before and
502 after it.
503
504 HANDLER SQL
505 -----------
506 If this connection has HANDLERs open against
507 some of the tables being FLUSHed, these handlers
508 are implicitly flushed (lose their position).
509 */
510
flush_tables_with_read_lock(THD * thd,TABLE_LIST * all_tables)511 bool flush_tables_with_read_lock(THD *thd, TABLE_LIST *all_tables)
512 {
513 Lock_tables_prelocking_strategy lock_tables_prelocking_strategy;
514 TABLE_LIST *table_list;
515
516 /*
517 This is called from SQLCOM_FLUSH, the transaction has
518 been committed implicitly.
519 */
520
521 if (thd->locked_tables_mode)
522 {
523 my_error(ER_LOCK_OR_ACTIVE_TRANSACTION, MYF(0));
524 goto error;
525 }
526
527 if (thd->lex->type & REFRESH_READ_LOCK)
528 {
529 /*
530 Acquire SNW locks on tables to be flushed. Don't acquire global
531 IX and database-scope IX locks on the tables as this will make
532 this statement incompatible with FLUSH TABLES WITH READ LOCK.
533 */
534 if (lock_table_names(thd, all_tables, NULL,
535 thd->variables.lock_wait_timeout,
536 MYSQL_OPEN_SKIP_SCOPED_MDL_LOCK))
537 goto error;
538
539 DEBUG_SYNC(thd,"flush_tables_with_read_lock_after_acquire_locks");
540
541 for (table_list= all_tables; table_list;
542 table_list= table_list->next_global)
543 {
544 /* Request removal of table from cache. */
545 tdc_remove_table(thd, TDC_RT_REMOVE_UNUSED,
546 table_list->db.str,
547 table_list->table_name.str, FALSE);
548 /* Reset ticket to satisfy asserts in open_tables(). */
549 table_list->mdl_request.ticket= NULL;
550 }
551 }
552
553 thd->variables.option_bits|= OPTION_TABLE_LOCK;
554
555 /*
556 Before opening and locking tables the below call also waits
557 for old shares to go away, so the fact that we don't pass
558 MYSQL_OPEN_IGNORE_FLUSH flag to it is important.
559 Also we don't pass MYSQL_OPEN_HAS_MDL_LOCK flag as we want
560 to open underlying tables if merge table is flushed.
561 For underlying tables of the merge the below call has to
562 acquire SNW locks to ensure that they can be locked for
563 read without further waiting.
564 */
565 if (open_and_lock_tables(thd, all_tables, FALSE,
566 MYSQL_OPEN_SKIP_SCOPED_MDL_LOCK,
567 &lock_tables_prelocking_strategy))
568 goto error_reset_bits;
569
570 if (thd->lex->type & REFRESH_FOR_EXPORT)
571 {
572 // Check if all storage engines support FOR EXPORT.
573 for (TABLE_LIST *table_list= all_tables; table_list;
574 table_list= table_list->next_global)
575 {
576 if (!(table_list->table->file->ha_table_flags() & HA_CAN_EXPORT))
577 {
578 my_error(ER_ILLEGAL_HA, MYF(0),table_list->table->file->table_type(),
579 table_list->db.str, table_list->table_name.str);
580 goto error_reset_bits;
581 }
582 }
583 }
584
585 if (thd->locked_tables_list.init_locked_tables(thd))
586 goto error_reset_bits;
587
588
589 /*
590 We don't downgrade MDL_SHARED_NO_WRITE here as the intended
591 post effect of this call is identical to LOCK TABLES <...> READ,
592 and we didn't use thd->in_lock_talbes and
593 thd->sql_command= SQLCOM_LOCK_TABLES hacks to enter the LTM.
594 */
595
596 return FALSE;
597
598 error_reset_bits:
599 trans_rollback_stmt(thd);
600 close_thread_tables(thd);
601 thd->variables.option_bits&= ~OPTION_TABLE_LOCK;
602 error:
603 return TRUE;
604 }
605
606
607 /**
608 Disable checkpoints for all handlers
609 This is released in unlock_global_read_lock()
610 */
611
disable_checkpoints(THD * thd)612 static void disable_checkpoints(THD *thd)
613 {
614 if (!thd->global_disable_checkpoint)
615 {
616 thd->global_disable_checkpoint= 1;
617 if (!global_disable_checkpoint++)
618 ha_checkpoint_state(1); // Disable checkpoints
619 }
620 }
621