1 /*****************************************************************************
2 
3 Copyright (c) 2020, Oracle and/or its affiliates. All Rights Reserved.
4 Copyright (c) 2020, MariaDB Corporation.
5 
6 This program is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License, version 2.0, as published by the
8 Free Software Foundation.
9 
10 This program is also distributed with certain software (including but not
11 limited to OpenSSL) that is licensed under separate terms, as designated in a
12 particular file or component or in included license documentation. The authors
13 of MySQL hereby grant you an additional permission to link the program and
14 your derivative works with the separately licensed software that they have
15 included with MySQL.
16 
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20 for more details.
21 
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
25 
26 *****************************************************************************/
27 
28 #include "buf0block_hint.h"
29 namespace buf {
30 
buffer_fix_block_if_still_valid()31 void Block_hint::buffer_fix_block_if_still_valid()
32 {
33   /* To check if m_block belongs to the current buf_pool, we must
34   prevent freeing memory while we check, and until we buffer-fix the
35   block. For this purpose it is enough to latch any of the many
36   latches taken by buf_pool_t::resize().
37 
38   Similar to buf_page_optimistic_get(), we must validate
39   m_block->page.id() after acquiring the hash_lock, because the object
40   may have been freed and not actually attached to buf_pool.page_hash
41   at the moment. (The block could have been reused to store a
42   different page, and that slice of buf_pool.page_hash could be protected
43   by another hash_lock that we are not holding.)
44 
45   Finally, assuming that we have correct hash bucket latched, we must
46   validate m_block->state() to ensure that the block is not being freed. */
47   if (m_block)
48   {
49     const ulint fold= m_page_id.fold();
50     page_hash_latch *hash_lock= buf_pool.page_hash.lock<false>(fold);
51     if (buf_pool.is_uncompressed(m_block) && m_page_id == m_block->page.id() &&
52         m_block->page.state() == BUF_BLOCK_FILE_PAGE)
53       buf_block_buf_fix_inc(m_block, __FILE__, __LINE__);
54     else
55       clear();
56     hash_lock->read_unlock();
57   }
58 }
59 }  // namespace buf
60