summaryrefslogtreecommitdiffstats
path: root/storage/maria/ma_pagecrc.c
blob: 4e1389b1163c804a49e20e1a523df047861eb822 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/* Copyright (C) 2007-2008 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */

#include "maria_def.h"


/**
  @brief calculate crc of the page avoiding special values

  @param start           The value to start CRC (we use page number here)
  @param data            data pointer
  @param length          length of the data

  @return crc of the page without special values
*/

static uint32 maria_page_crc(uint32 start, uchar *data, uint length)
{
  uint32 crc= my_checksum(start, data, length);

  /* we need this assert to get following comparison working */
  compile_time_assert(MARIA_NO_CRC_BITMAP_PAGE ==
                      MARIA_NO_CRC_NORMAL_PAGE - 1 &&
                      MARIA_NO_CRC_NORMAL_PAGE == 0xffffffff);
  if (crc >= MARIA_NO_CRC_BITMAP_PAGE)
    crc= MARIA_NO_CRC_BITMAP_PAGE - 1;

  return(crc);
}

/**
  @brief Maria pages read callback (checks the page CRC)

  @param page            The page data to check
  @param page_no         The page number (<offset>/<page length>)
  @param data_ptr        pointer to MARIA_SHARE
  @param no_crc_val      Value which means CRC absence
                         (MARIA_NO_CRC_NORMAL_PAGE or MARIA_NO_CRC_BITMAP_PAGE)
  @param data_length     length of data to calculate CRC

  @retval 0 OK
  @retval 1 Error
*/

my_bool maria_page_crc_check(uchar *page,
                             pgcache_page_no_t page_no,
                             MARIA_SHARE *share,
                             uint32 no_crc_val,
                             int data_length)
{
  uint32 crc= uint4korr(page + share->block_size - CRC_SIZE), new_crc;
  my_bool res;
  DBUG_ENTER("maria_page_crc_check");

  DBUG_ASSERT((uint)data_length <= share->block_size - CRC_SIZE);

  /* we need this assert to get following comparison working */
  compile_time_assert(MARIA_NO_CRC_BITMAP_PAGE ==
                      MARIA_NO_CRC_NORMAL_PAGE - 1 &&
                      MARIA_NO_CRC_NORMAL_PAGE == 0xffffffff);
  /*
    If crc is no_crc_val then
    the page has no crc, so there is nothing to check.
  */
  if (crc >= MARIA_NO_CRC_BITMAP_PAGE)
  {
    DBUG_PRINT("info", ("No crc: %lu  crc: %lu  page: %lu  ",
                        (ulong) no_crc_val, (ulong) crc, (ulong) page_no));
    if (crc != no_crc_val)
    {
      my_errno= HA_ERR_WRONG_CRC;
      DBUG_PRINT("error", ("Wrong no CRC value"));
      DBUG_RETURN(1);
    }
    DBUG_RETURN(0);
  }
  new_crc= maria_page_crc((uint32) page_no, page, data_length);
  DBUG_ASSERT(new_crc != no_crc_val);
  res= MY_TEST(new_crc != crc);
  if (res)
  {
    /*
      Bitmap pages may be totally zero filled in some cases.
      This happens when we get a crash after the pagecache has written
      out a page that is on a newly created bitmap page and we get
      a crash before the bitmap page is written out.

      We handle this case with the following logic:
      When reading, approve of bitmap pages where all bytes are zero
      (This is after all a bitmap pages where no data is reserved and
      the CRC will be corrected at next write)
    */
    if (no_crc_val == MARIA_NO_CRC_BITMAP_PAGE &&
        crc == 0 && _ma_check_if_zero(page, data_length))
    {
      DBUG_PRINT("warning", ("Found bitmap page that was not initialized"));
      DBUG_RETURN(0);
    }

    DBUG_PRINT("error", ("Page: %lu  crc: %lu  calculated crc: %lu",
                         (ulong) page_no, (ulong) crc, (ulong) new_crc));
    my_errno= HA_ERR_WRONG_CRC;
  }
  DBUG_RETURN(res);
}


/**
  @brief Maria pages write callback (sets the page CRC for data and index
  files)

  @param page            The page data to set
  @param page_no         The page number (<offset>/<page length>)
  @param data_ptr        Write callback data pointer (pointer to MARIA_SHARE)

  @retval 0 OK
*/

my_bool maria_page_crc_set_normal(PAGECACHE_IO_HOOK_ARGS *args)
{
  uchar *page= args->page;
  pgcache_page_no_t page_no= args->pageno;
  MARIA_SHARE *share= (MARIA_SHARE *)args->data;
  int data_length= share->block_size - CRC_SIZE;
  uint32 crc= maria_page_crc((uint32) page_no, page, data_length);
  DBUG_ENTER("maria_page_crc_set_normal");
  DBUG_PRINT("info", ("Page %lu  crc: %lu", (ulong) page_no, (ulong)crc));

  /* crc is on the stack so it is aligned, pagecache buffer is aligned, too */
  int4store_aligned(page + data_length, crc);
  DBUG_RETURN(0);
}


/**
  @brief Maria pages write callback (sets the page CRC for keys)

  @param page            The page data to set
  @param page_no         The page number (<offset>/<page length>)
  @param data_ptr        Write callback data pointer (pointer to MARIA_SHARE)

  @retval 0 OK
*/

my_bool maria_page_crc_set_index(PAGECACHE_IO_HOOK_ARGS *args)
{
  uchar *page= args->page;
  pgcache_page_no_t page_no= args->pageno;
  MARIA_SHARE *share= (MARIA_SHARE *)args->data;
  int data_length= _ma_get_page_used(share, page);
  uint32 crc= maria_page_crc((uint32) page_no, page, data_length);
  DBUG_ENTER("maria_page_crc_set_index");
  DBUG_PRINT("info", ("Page %lu  crc: %lu",
                      (ulong) page_no, (ulong) crc));
  DBUG_ASSERT((uint)data_length <= share->block_size - CRC_SIZE);
  /* crc is on the stack so it is aligned, pagecache buffer is aligned, too */
  int4store_aligned(page + share->block_size - CRC_SIZE, crc);
  DBUG_RETURN(0);
}


/* interface functions */


/**
  @brief Maria pages read callback (checks the page CRC) for index/data pages

  @param page            The page data to check
  @param page_no         The page number (<offset>/<page length>)
  @param data_ptr        Read callback data pointer (pointer to MARIA_SHARE)

  @retval 0 OK
  @retval 1 Error
*/

my_bool maria_page_crc_check_data(int res, PAGECACHE_IO_HOOK_ARGS *args)
{
  uchar *page= args->page;
  pgcache_page_no_t page_no= args->pageno;
  MARIA_SHARE *share= (MARIA_SHARE *)args->data;
  if (res)
  {
    return 1;
  }

  return (maria_page_crc_check(page, (uint32) page_no, share,
                               MARIA_NO_CRC_NORMAL_PAGE,
                               share->block_size - CRC_SIZE));
}


/**
  @brief Maria pages read callback (checks the page CRC) for bitmap pages

  @param page            The page data to check
  @param page_no         The page number (<offset>/<page length>)
  @param data_ptr        Read callback data pointer (pointer to MARIA_SHARE)

  @retval 0 OK
  @retval 1 Error
*/

my_bool maria_page_crc_check_bitmap(int res, PAGECACHE_IO_HOOK_ARGS *args)
{
  uchar *page= args->page;
  pgcache_page_no_t page_no= args->pageno;
  MARIA_SHARE *share= (MARIA_SHARE *)args->data;
  if (res)
  {
    return 1;
  }
  return (maria_page_crc_check(page, (uint32) page_no, share,
                               MARIA_NO_CRC_BITMAP_PAGE,
                               share->block_size - CRC_SIZE));
}


/**
  @brief Maria pages read callback (checks the page CRC) for index pages

  @param page            The page data to check
  @param page_no         The page number (<offset>/<page length>)
  @param data_ptr        Read callback data pointer (pointer to MARIA_SHARE)

  @retval 0 OK
  @retval 1 Error
*/

my_bool maria_page_crc_check_index(int res, PAGECACHE_IO_HOOK_ARGS *args)
{
  uchar *page= args->page;
  pgcache_page_no_t page_no= args->pageno;
  MARIA_SHARE *share= (MARIA_SHARE *)args->data;
  uint length= _ma_get_page_used(share, page);

  if (res)
    return 1;
  if (length > share->block_size - CRC_SIZE)
  {
    DBUG_PRINT("error", ("Wrong page length: %u", length));
    my_errno= HA_ERR_WRONG_CRC;
    return 1;
  }
  return maria_page_crc_check(page, (uint32) page_no, share,
                               MARIA_NO_CRC_NORMAL_PAGE,
                              length);
}


/**
  @brief Maria pages dummy read callback for temporary tables

  @retval 0 OK
  @retval 1 Error
*/

my_bool maria_page_crc_check_none(int res,
                                  PAGECACHE_IO_HOOK_ARGS *args
                                  __attribute__((unused)))
{
  return res != 0;
}


/**
  @brief Maria pages write callback (sets the page filler for index/data)

  @param page            The page data to set
  @param page_no         The page number (<offset>/<page length>)
  @param data_ptr        Write callback data pointer (pointer to MARIA_SHARE)

  @retval 0 OK
*/

my_bool maria_page_filler_set_normal(PAGECACHE_IO_HOOK_ARGS *args)
{
  uchar *page= args->page;
#ifdef DBUG_ASSERT_EXISTS
  pgcache_page_no_t page_no= args->pageno;
#endif
  MARIA_SHARE *share= (MARIA_SHARE *)args->data;
  DBUG_ENTER("maria_page_filler_set_normal");
  DBUG_ASSERT(page_no != 0);                    /* Catches some simple bugs */
  int4store_aligned(page + share->block_size - CRC_SIZE,
                    MARIA_NO_CRC_NORMAL_PAGE);
  DBUG_RETURN(0);
}


/**
  @brief Maria pages write callback (sets the page filler for bitmap)

  @param page            The page data to set
  @param page_no         The page number (<offset>/<page length>)
  @param data_ptr        Write callback data pointer (pointer to MARIA_SHARE)

  @retval 0 OK
*/

my_bool maria_page_filler_set_bitmap(PAGECACHE_IO_HOOK_ARGS *args)
{
  uchar *page= args->page;
  MARIA_SHARE *share= (MARIA_SHARE *)args->data;
  DBUG_ENTER("maria_page_filler_set_bitmap");
  int4store_aligned(page + share->block_size - CRC_SIZE,
                    MARIA_NO_CRC_BITMAP_PAGE);
  DBUG_RETURN(0);
}


/**
  @brief Maria pages dummy write callback for temporary tables

  @retval 0 OK
*/

my_bool maria_page_filler_set_none(PAGECACHE_IO_HOOK_ARGS *args
                                   __attribute__((unused)))
{
#ifdef HAVE_valgrind
  uchar *page= args->page;
  MARIA_SHARE *share= (MARIA_SHARE *)args->data;
  int4store_aligned(page + share->block_size - CRC_SIZE,
                    0);
#endif
  return 0;
}


/**
  @brief Write failure callback (mark table as corrupted)

  @param data_ptr        Write callback data pointer (pointer to MARIA_SHARE)
*/

void maria_page_write_failure(int error, PAGECACHE_IO_HOOK_ARGS *args)
{
  if (error)
    maria_mark_crashed_share((MARIA_SHARE *)args->data);
}


/**
  @brief Maria flush log log if needed

  @param page            The page data to set
  @param page_no         The page number (<offset>/<page length>)
  @param data_ptr        Write callback data pointer (pointer to MARIA_SHARE)

  @retval 0  OK
  @retval 1  error
*/

my_bool maria_flush_log_for_page(PAGECACHE_IO_HOOK_ARGS *args)
{
  LSN lsn;
  uchar *page= args->page;
  MARIA_SHARE *share= (MARIA_SHARE *)args->data;
  DBUG_ENTER("maria_flush_log_for_page");
  /* share is 0 here only in unittest */
  DBUG_ASSERT(!share || share->page_type == PAGECACHE_LSN_PAGE);
  lsn= lsn_korr(page);
  if (translog_flush(lsn))
    DBUG_RETURN(1);
  /*
    Now when log is written, it's safe to incremented 'open' counter for
    the table so that we know it was not closed properly.
  */
  if (share && !share->global_changed)
    _ma_mark_file_changed_now(share);
  DBUG_RETURN(0);
}


my_bool maria_flush_log_for_page_none(PAGECACHE_IO_HOOK_ARGS *args
                                      __attribute__((unused)))
{
  return 0;
}

my_bool maria_page_null_pre_read_hook(PAGECACHE_IO_HOOK_ARGS *args
                                      __attribute__((unused)))
{
  return 0;
}