mi_dynrec.c 55.5 KB
Newer Older
unknown's avatar
unknown committed
1
/* Copyright (C) 2000-2006 MySQL AB
unknown's avatar
unknown committed
2

unknown's avatar
unknown committed
3 4
   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
unknown's avatar
unknown committed
5
   the Free Software Foundation; version 2 of the License.
unknown's avatar
unknown committed
6

unknown's avatar
unknown committed
7 8 9 10
   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.
unknown's avatar
unknown committed
11

unknown's avatar
unknown committed
12 13 14 15
   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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

16 17 18 19 20 21 22 23 24
/*
  Functions to handle space-packed-records and blobs
 
  A row may be stored in one or more linked blocks.
  The block size is between MI_MIN_BLOCK_LENGTH and MI_MAX_BLOCK_LENGTH.
  Each block is aligned on MI_DYN_ALIGN_SIZE.
  The reson for the max block size is to not have too many different types
  of blocks.  For the differnet block types, look at _mi_get_block_info()
*/
unknown's avatar
unknown committed
25 26 27 28 29 30

#include "myisamdef.h"

/* Enough for comparing if number is zero */
static char zero_string[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

31
static int write_dynamic_record(MI_INFO *info,const uchar *record,
unknown's avatar
unknown committed
32 33 34
				ulong reclength);
static int _mi_find_writepos(MI_INFO *info,ulong reclength,my_off_t *filepos,
			     ulong *length);
35
static int update_dynamic_record(MI_INFO *info,my_off_t filepos,uchar *record,
unknown's avatar
unknown committed
36 37 38
				 ulong reclength);
static int delete_dynamic_record(MI_INFO *info,my_off_t filepos,
				 uint second_read);
39
static int _mi_cmp_buffer(File file, const uchar *buff, my_off_t filepos,
unknown's avatar
unknown committed
40 41 42 43 44 45 46 47 48 49 50 51
			  uint length);

#ifdef THREAD
/* Play it safe; We have a small stack when using threads */
#undef my_alloca
#undef my_afree
#define my_alloca(A) my_malloc((A),MYF(0))
#define my_afree(A) my_free((A),MYF(0))
#endif

	/* Interface function from MI_INFO */

unknown's avatar
unknown committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
#ifdef HAVE_MMAP

/*
  Create mmaped area for MyISAM handler

  SYNOPSIS
    mi_dynmap_file()
    info		MyISAM handler

  RETURN
    0  ok
    1  error.
*/

my_bool mi_dynmap_file(MI_INFO *info, my_off_t size)
{
  DBUG_ENTER("mi_dynmap_file");
unknown's avatar
unknown committed
69 70 71 72 73
  if (size > (my_off_t) (~((size_t) 0)) - MEMMAP_EXTRA_MARGIN)
  {
    DBUG_PRINT("warning", ("File is too large for mmap"));
    DBUG_RETURN(1);
  }
74 75 76 77 78 79 80 81
  /*
    I wonder if it is good to use MAP_NORESERVE. From the Linux man page:
    MAP_NORESERVE
      Do not reserve swap space for this mapping. When swap space is
      reserved, one has the guarantee that it is possible to modify the
      mapping. When swap space is not reserved one might get SIGSEGV
      upon a write if no physical memory is available.
  */
82
  info->s->file_map= (uchar*)
unknown's avatar
unknown committed
83 84 85 86 87
                  my_mmap(0, (size_t)(size + MEMMAP_EXTRA_MARGIN),
                          info->s->mode==O_RDONLY ? PROT_READ :
                          PROT_READ | PROT_WRITE,
                          MAP_SHARED | MAP_NORESERVE,
                          info->dfile, 0L);
88
  if (info->s->file_map == (uchar*) MAP_FAILED)
unknown's avatar
unknown committed
89 90 91 92 93
  {
    info->s->file_map= NULL;
    DBUG_RETURN(1);
  }
#if defined(HAVE_MADVISE)
94
  madvise((char*) info->s->file_map, size, MADV_RANDOM);
unknown's avatar
unknown committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
#endif
  info->s->mmaped_length= size;
  DBUG_RETURN(0);
}


/*
  Resize mmaped area for MyISAM handler

  SYNOPSIS
    mi_remap_file()
    info		MyISAM handler

  RETURN
*/

void mi_remap_file(MI_INFO *info, my_off_t size)
{
  if (info->s->file_map)
  {
115
    VOID(my_munmap((char*) info->s->file_map,
unknown's avatar
unknown committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
                   (size_t) info->s->mmaped_length + MEMMAP_EXTRA_MARGIN));
    mi_dynmap_file(info, size);
  }
}
#endif


/*
  Read bytes from MySAM handler, using mmap or pread

  SYNOPSIS
    mi_mmap_pread()
    info		MyISAM handler
    Buffer              Input buffer
    Count               Count of bytes for read
    offset              Start position
    MyFlags             

  RETURN
    0  ok
*/

138 139
size_t mi_mmap_pread(MI_INFO *info, uchar *Buffer,
                    size_t Count, my_off_t offset, myf MyFlags)
unknown's avatar
unknown committed
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
{
  DBUG_PRINT("info", ("mi_read with mmap %d\n", info->dfile));
  if (info->s->concurrent_insert)
    rw_rdlock(&info->s->mmap_lock);

  /*
    The following test may fail in the following cases:
    - We failed to remap a memory area (fragmented memory?)
    - This thread has done some writes, but not yet extended the
    memory mapped area.
  */

  if (info->s->mmaped_length >= offset + Count)
  {
    memcpy(Buffer, info->s->file_map + offset, Count);
    if (info->s->concurrent_insert)
      rw_unlock(&info->s->mmap_lock);
    return 0;
  }
  else
  {
    if (info->s->concurrent_insert)
      rw_unlock(&info->s->mmap_lock);
    return my_pread(info->dfile, Buffer, Count, offset, MyFlags);
  }
}


        /* wrapper for my_pread in case if mmap isn't used */

170 171
size_t mi_nommap_pread(MI_INFO *info, uchar *Buffer,
                       size_t Count, my_off_t offset, myf MyFlags)
unknown's avatar
unknown committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
{
  return my_pread(info->dfile, Buffer, Count, offset, MyFlags);
}


/*
  Write bytes to MySAM handler, using mmap or pwrite

  SYNOPSIS
    mi_mmap_pwrite()
    info		MyISAM handler
    Buffer              Output buffer
    Count               Count of bytes for write
    offset              Start position
    MyFlags             

  RETURN
    0  ok
    !=0  error.  In this case return error from pwrite
*/

193 194
size_t mi_mmap_pwrite(MI_INFO *info, const uchar *Buffer,
                      size_t Count, my_off_t offset, myf MyFlags)
unknown's avatar
unknown committed
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
{
  DBUG_PRINT("info", ("mi_write with mmap %d\n", info->dfile));
  if (info->s->concurrent_insert)
    rw_rdlock(&info->s->mmap_lock);

  /*
    The following test may fail in the following cases:
    - We failed to remap a memory area (fragmented memory?)
    - This thread has done some writes, but not yet extended the
    memory mapped area.
  */

  if (info->s->mmaped_length >= offset + Count)
  {
    memcpy(info->s->file_map + offset, Buffer, Count); 
    if (info->s->concurrent_insert)
      rw_unlock(&info->s->mmap_lock);
    return 0;
  }
  else
  {
216
    info->s->nonmmaped_inserts++;
unknown's avatar
unknown committed
217 218 219 220 221 222 223 224 225 226
    if (info->s->concurrent_insert)
      rw_unlock(&info->s->mmap_lock);
    return my_pwrite(info->dfile, Buffer, Count, offset, MyFlags);
  }

}


        /* wrapper for my_pwrite in case if mmap isn't used */

227 228
size_t mi_nommap_pwrite(MI_INFO *info, const uchar *Buffer,
                      size_t Count, my_off_t offset, myf MyFlags)
unknown's avatar
unknown committed
229 230 231 232 233
{
  return my_pwrite(info->dfile, Buffer, Count, offset, MyFlags);
}


234
int _mi_write_dynamic_record(MI_INFO *info, const uchar *record)
unknown's avatar
unknown committed
235 236 237 238 239
{
  ulong reclength=_mi_rec_pack(info,info->rec_buff,record);
  return (write_dynamic_record(info,info->rec_buff,reclength));
}

240
int _mi_update_dynamic_record(MI_INFO *info, my_off_t pos, const uchar *record)
unknown's avatar
unknown committed
241 242 243 244 245
{
  uint length=_mi_rec_pack(info,info->rec_buff,record);
  return (update_dynamic_record(info,pos,info->rec_buff,length));
}

246
int _mi_write_blob_record(MI_INFO *info, const uchar *record)
unknown's avatar
unknown committed
247
{
248
  uchar *rec_buff;
unknown's avatar
unknown committed
249
  int error;
unknown's avatar
unknown committed
250
  ulong reclength,reclength2,extra;
unknown's avatar
unknown committed
251

252 253
  extra= (ALIGN_SIZE(MI_MAX_DYN_BLOCK_HEADER)+MI_SPLIT_LENGTH+
	  MI_DYN_DELETE_BLOCK_HEADER+1);
unknown's avatar
unknown committed
254
  reclength= (info->s->base.pack_reclength +
255
	      _my_calc_total_blob_length(info,record)+ extra);
unknown's avatar
unknown committed
256
#ifdef NOT_USED					/* We now support big rows */
unknown's avatar
unknown committed
257 258 259 260 261
  if (reclength > MI_DYN_MAX_ROW_LENGTH)
  {
    my_errno=HA_ERR_TO_BIG_ROW;
    return -1;
  }
unknown's avatar
unknown committed
262
#endif
263
  if (!(rec_buff=(uchar*) my_alloca(reclength)))
unknown's avatar
unknown committed
264
  {
265
    my_errno= HA_ERR_OUT_OF_MEM; /* purecov: inspected */
unknown's avatar
unknown committed
266 267
    return(-1);
  }
unknown's avatar
unknown committed
268 269 270 271 272
  reclength2= _mi_rec_pack(info,rec_buff+ALIGN_SIZE(MI_MAX_DYN_BLOCK_HEADER),
			   record);
  DBUG_PRINT("info",("reclength: %lu  reclength2: %lu",
		     reclength, reclength2));
  DBUG_ASSERT(reclength2 <= reclength);
unknown's avatar
unknown committed
273
  error=write_dynamic_record(info,rec_buff+ALIGN_SIZE(MI_MAX_DYN_BLOCK_HEADER),
unknown's avatar
unknown committed
274
			     reclength2);
unknown's avatar
unknown committed
275 276 277 278 279
  my_afree(rec_buff);
  return(error);
}


280
int _mi_update_blob_record(MI_INFO *info, my_off_t pos, const uchar *record)
unknown's avatar
unknown committed
281
{
282
  uchar *rec_buff;
unknown's avatar
unknown committed
283 284 285
  int error;
  ulong reclength,extra;

286 287 288 289
  extra= (ALIGN_SIZE(MI_MAX_DYN_BLOCK_HEADER)+MI_SPLIT_LENGTH+
	  MI_DYN_DELETE_BLOCK_HEADER);
  reclength= (info->s->base.pack_reclength+
	      _my_calc_total_blob_length(info,record)+ extra);
unknown's avatar
unknown committed
290
#ifdef NOT_USED					/* We now support big rows */
unknown's avatar
unknown committed
291 292 293 294 295
  if (reclength > MI_DYN_MAX_ROW_LENGTH)
  {
    my_errno=HA_ERR_TO_BIG_ROW;
    return -1;
  }
unknown's avatar
unknown committed
296
#endif
297
  if (!(rec_buff=(uchar*) my_alloca(reclength)))
unknown's avatar
unknown committed
298
  {
299
    my_errno= HA_ERR_OUT_OF_MEM; /* purecov: inspected */
unknown's avatar
unknown committed
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
    return(-1);
  }
  reclength=_mi_rec_pack(info,rec_buff+ALIGN_SIZE(MI_MAX_DYN_BLOCK_HEADER),
			 record);
  error=update_dynamic_record(info,pos,
			      rec_buff+ALIGN_SIZE(MI_MAX_DYN_BLOCK_HEADER),
			      reclength);
  my_afree(rec_buff);
  return(error);
}


int _mi_delete_dynamic_record(MI_INFO *info)
{
  return delete_dynamic_record(info,info->lastpos,0);
}


	/* Write record to data-file */

320
static int write_dynamic_record(MI_INFO *info, const uchar *record,
unknown's avatar
unknown committed
321 322 323 324 325 326 327 328
				ulong reclength)
{
  int flag;
  ulong length;
  my_off_t filepos;
  DBUG_ENTER("write_dynamic_record");

  flag=0;
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351

  /*
    Check if we have enough room for the new record.
    First we do simplified check to make usual case faster.
    Then we do more precise check for the space left.
    Though it still is not absolutely precise, as
    we always use MI_MAX_DYN_BLOCK_HEADER while it can be
    less in the most of the cases.
  */

  if (unlikely(info->s->base.max_data_file_length -
               info->state->data_file_length <
               reclength + MI_MAX_DYN_BLOCK_HEADER))
  {
    if (info->s->base.max_data_file_length - info->state->data_file_length +
        info->state->empty - info->state->del * MI_MAX_DYN_BLOCK_HEADER <
        reclength + MI_MAX_DYN_BLOCK_HEADER)
    {
      my_errno=HA_ERR_RECORD_FILE_FULL;
      DBUG_RETURN(1);
    }
  }

unknown's avatar
unknown committed
352
  do
unknown's avatar
unknown committed
353 354 355
  {
    if (_mi_find_writepos(info,reclength,&filepos,&length))
      goto err;
356 357 358
    if (_mi_write_part_record(info,filepos,length,
                              (info->append_insert_at_end ?
                               HA_OFFSET_ERROR : info->s->state.dellink),
359
			      (uchar**) &record,&reclength,&flag))
unknown's avatar
unknown committed
360
      goto err;
unknown's avatar
unknown committed
361
  } while (reclength);
unknown's avatar
unknown committed
362 363

  DBUG_RETURN(0);
unknown's avatar
unknown committed
364
err:
unknown's avatar
unknown committed
365 366 367 368 369 370 371 372 373 374 375 376
  DBUG_RETURN(1);
}


	/* Get a block for data ; The given data-area must be used !! */

static int _mi_find_writepos(MI_INFO *info,
			     ulong reclength, /* record length */
			     my_off_t *filepos, /* Return file pos */
			     ulong *length)   /* length of block at filepos */
{
  MI_BLOCK_INFO block_info;
377
  ulong tmp;
unknown's avatar
unknown committed
378 379
  DBUG_ENTER("_mi_find_writepos");

380 381
  if (info->s->state.dellink != HA_OFFSET_ERROR &&
      !info->append_insert_at_end)
unknown's avatar
unknown committed
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
  {
    /* Deleted blocks exists;  Get last used block */
    *filepos=info->s->state.dellink;
    block_info.second_read=0;
    info->rec_cache.seek_not_done=1;
    if (!(_mi_get_block_info(&block_info,info->dfile,info->s->state.dellink) &
	   BLOCK_DELETED))
    {
      DBUG_PRINT("error",("Delete link crashed"));
      my_errno=HA_ERR_WRONG_IN_RECORD;
      DBUG_RETURN(-1);
    }
    info->s->state.dellink=block_info.next_filepos;
    info->state->del--;
    info->state->empty-= block_info.block_len;
    *length= block_info.block_len;
  }
  else
  {
    /* No deleted blocks;  Allocate a new block */
    *filepos=info->state->data_file_length;
403
    if ((tmp=reclength+3 + test(reclength >= (65520-3))) <
unknown's avatar
unknown committed
404
	info->s->base.min_block_length)
405
      tmp= info->s->base.min_block_length;
unknown's avatar
unknown committed
406
    else
407 408
      tmp= ((tmp+MI_DYN_ALIGN_SIZE-1) &
	    (~ (ulong) (MI_DYN_ALIGN_SIZE-1)));
unknown's avatar
unknown committed
409
    if (info->state->data_file_length >
410
	(info->s->base.max_data_file_length - tmp))
unknown's avatar
unknown committed
411 412 413 414
    {
      my_errno=HA_ERR_RECORD_FILE_FULL;
      DBUG_RETURN(-1);
    }
415 416 417 418
    if (tmp > MI_MAX_BLOCK_LENGTH)
      tmp=MI_MAX_BLOCK_LENGTH;
    *length= tmp;
    info->state->data_file_length+= tmp;
unknown's avatar
unknown committed
419 420 421 422 423 424 425 426
    info->s->state.split++;
    info->update|=HA_STATE_WRITE_AT_END;
  }
  DBUG_RETURN(0);
} /* _mi_find_writepos */



427 428 429 430 431
/*
  Unlink a deleted block from the deleted list.
  This block will be combined with the preceding or next block to form
  a big block.
*/
unknown's avatar
unknown committed
432

433
static my_bool unlink_deleted_block(MI_INFO *info, MI_BLOCK_INFO *block_info)
unknown's avatar
unknown committed
434 435 436 437 438 439 440 441 442 443 444
{
  DBUG_ENTER("unlink_deleted_block");
  if (block_info->filepos == info->s->state.dellink)
  {
    /* First deleted block;  We can just use this ! */
    info->s->state.dellink=block_info->next_filepos;
  }
  else
  {
    MI_BLOCK_INFO tmp;
    tmp.second_read=0;
445
    /* Unlink block from the previous block */
unknown's avatar
unknown committed
446 447 448 449
    if (!(_mi_get_block_info(&tmp,info->dfile,block_info->prev_filepos)
	  & BLOCK_DELETED))
      DBUG_RETURN(1);				/* Something is wrong */
    mi_sizestore(tmp.header+4,block_info->next_filepos);
450
    if (info->s->file_write(info, tmp.header+4,8,
unknown's avatar
unknown committed
451 452
		  block_info->prev_filepos+4, MYF(MY_NABP)))
      DBUG_RETURN(1);
453
    /* Unlink block from next block */
unknown's avatar
unknown committed
454 455 456 457 458 459
    if (block_info->next_filepos != HA_OFFSET_ERROR)
    {
      if (!(_mi_get_block_info(&tmp,info->dfile,block_info->next_filepos)
	    & BLOCK_DELETED))
	DBUG_RETURN(1);				/* Something is wrong */
      mi_sizestore(tmp.header+12,block_info->prev_filepos);
460
      if (info->s->file_write(info, tmp.header+12,8,
unknown's avatar
unknown committed
461 462 463 464 465
		    block_info->next_filepos+12,
		    MYF(MY_NABP)))
	DBUG_RETURN(1);
    }
  }
466
  /* We now have one less deleted block */
unknown's avatar
unknown committed
467 468 469 470
  info->state->del--;
  info->state->empty-= block_info->block_len;
  info->s->state.split--;

471 472 473 474 475
  /*
    If this was a block that we where accessing through table scan
    (mi_rrnd() or mi_scan(), then ensure that we skip over this block
    when doing next mi_rrnd() or mi_scan().
  */
unknown's avatar
unknown committed
476 477 478 479 480 481
  if (info->nextpos == block_info->filepos)
    info->nextpos+=block_info->block_len;
  DBUG_RETURN(0);
}


482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
/*
  Add a backward link to delete block

  SYNOPSIS
    update_backward_delete_link()
    info		MyISAM handler
    delete_block	Position to delete block to update.
			If this is 'HA_OFFSET_ERROR', nothing will be done
    filepos		Position to block that 'delete_block' should point to

  RETURN
    0  ok
    1  error.  In this case my_error is set.
*/

static int update_backward_delete_link(MI_INFO *info, my_off_t delete_block,
				       my_off_t filepos)
unknown's avatar
unknown committed
499
{
500 501
  MI_BLOCK_INFO block_info;
  DBUG_ENTER("update_backward_delete_link");
unknown's avatar
unknown committed
502

503
  if (delete_block != HA_OFFSET_ERROR)
unknown's avatar
unknown committed
504 505
  {
    block_info.second_read=0;
506
    if (_mi_get_block_info(&block_info,info->dfile,delete_block)
unknown's avatar
unknown committed
507 508
	& BLOCK_DELETED)
    {
509
      uchar buff[8];
unknown's avatar
unknown committed
510
      mi_sizestore(buff,filepos);
unknown's avatar
unknown committed
511
      if (info->s->file_write(info,buff, 8, delete_block+12, MYF(MY_NABP)))
512
	DBUG_RETURN(1);				/* Error on write */
unknown's avatar
unknown committed
513 514 515 516
    }
    else
    {
      my_errno=HA_ERR_WRONG_IN_RECORD;
517
      DBUG_RETURN(1);				/* Wrong delete link */
unknown's avatar
unknown committed
518 519
    }
  }
520
  DBUG_RETURN(0);
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
}

	/* Delete datarecord from database */
	/* info->rec_cache.seek_not_done is updated in cmp_record */

static int delete_dynamic_record(MI_INFO *info, my_off_t filepos,
				 uint second_read)
{
  uint length,b_type;
  MI_BLOCK_INFO block_info,del_block;
  int error;
  my_bool remove_next_block;
  DBUG_ENTER("delete_dynamic_record");

  /* First add a link from the last block to the new one */
  error= update_backward_delete_link(info, info->s->state.dellink, filepos);
unknown's avatar
unknown committed
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568

  block_info.second_read=second_read;
  do
  {
    /* Remove block at 'filepos' */
    if ((b_type=_mi_get_block_info(&block_info,info->dfile,filepos))
	& (BLOCK_DELETED | BLOCK_ERROR | BLOCK_SYNC_ERROR |
	   BLOCK_FATAL_ERROR) ||
	(length=(uint) (block_info.filepos-filepos) +block_info.block_len) <
	MI_MIN_BLOCK_LENGTH)
    {
      my_errno=HA_ERR_WRONG_IN_RECORD;
      DBUG_RETURN(1);
    }
    /* Check if next block is a delete block */
    del_block.second_read=0;
    remove_next_block=0;
    if (_mi_get_block_info(&del_block,info->dfile,filepos+length) &
	BLOCK_DELETED && del_block.block_len+length < MI_DYN_MAX_BLOCK_LENGTH)
    {
      /* We can't remove this yet as this block may be the head block */
      remove_next_block=1;
      length+=del_block.block_len;
    }

    block_info.header[0]=0;
    mi_int3store(block_info.header+1,length);
    mi_sizestore(block_info.header+4,info->s->state.dellink);
    if (b_type & BLOCK_LAST)
      bfill(block_info.header+12,8,255);
    else
      mi_sizestore(block_info.header+12,block_info.next_filepos);
569
    if (info->s->file_write(info,(uchar*) block_info.header,20,filepos,
unknown's avatar
unknown committed
570 571 572 573 574 575 576
		  MYF(MY_NABP)))
      DBUG_RETURN(1);
    info->s->state.dellink = filepos;
    info->state->del++;
    info->state->empty+=length;
    filepos=block_info.next_filepos;

577
    /* Now it's safe to unlink the deleted block directly after this one */
unknown's avatar
unknown committed
578 579 580 581 582 583 584 585 586 587 588 589 590 591
    if (remove_next_block && unlink_deleted_block(info,&del_block))
      error=1;
  } while (!(b_type & BLOCK_LAST));

  DBUG_RETURN(error);
}


	/* Write a block to datafile */

int _mi_write_part_record(MI_INFO *info,
			  my_off_t filepos,	/* points at empty block */
			  ulong length,		/* length of block */
			  my_off_t next_filepos,/* Next empty block */
592
			  uchar **record,	/* pointer to record ptr */
unknown's avatar
unknown committed
593 594 595 596
			  ulong *reclength,	/* length of *record */
			  int *flag)		/* *flag == 0 if header */
{
  ulong head_length,res_length,extra_length,long_block,del_length;
597
  uchar *pos,*record_end;
unknown's avatar
unknown committed
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
  my_off_t  next_delete_block;
  uchar temp[MI_SPLIT_LENGTH+MI_DYN_DELETE_BLOCK_HEADER];
  DBUG_ENTER("_mi_write_part_record");

  next_delete_block=HA_OFFSET_ERROR;

  res_length=extra_length=0;
  if (length > *reclength + MI_SPLIT_LENGTH)
  {						/* Splitt big block */
    res_length=MY_ALIGN(length- *reclength - MI_EXTEND_BLOCK_LENGTH,
			MI_DYN_ALIGN_SIZE);
    length-= res_length;			/* Use this for first part */
  }
  long_block= (length < 65520L && *reclength < 65520L) ? 0 : 1;
  if (length == *reclength+ 3 + long_block)
  {
    /* Block is exactly of the right length */
    temp[0]=(uchar) (1+ *flag)+(uchar) long_block;	/* Flag is 0 or 6 */
    if (long_block)
    {
      mi_int3store(temp+1,*reclength);
      head_length=4;
    }
    else
    {
      mi_int2store(temp+1,*reclength);
      head_length=3;
    }
  }
  else if (length-long_block < *reclength+4)
  {						/* To short block */
    if (next_filepos == HA_OFFSET_ERROR)
630 631 632
      next_filepos= (info->s->state.dellink != HA_OFFSET_ERROR &&
                     !info->append_insert_at_end ?
                     info->s->state.dellink : info->state->data_file_length);
unknown's avatar
unknown committed
633 634
    if (*flag == 0)				/* First block */
    {
635
      if (*reclength > MI_MAX_BLOCK_LENGTH)
unknown's avatar
unknown committed
636
      {
637 638 639
	head_length= 16;
	temp[0]=13;
	mi_int4store(temp+1,*reclength);
unknown's avatar
unknown committed
640
	mi_int3store(temp+5,length-head_length);
641
	mi_sizestore((uchar*) temp+8,next_filepos);
unknown's avatar
unknown committed
642 643 644
      }
      else
      {
645 646 647 648 649 650
	head_length=5+8+long_block*2;
	temp[0]=5+(uchar) long_block;
	if (long_block)
	{
	  mi_int3store(temp+1,*reclength);
	  mi_int3store(temp+4,length-head_length);
651
	  mi_sizestore((uchar*) temp+7,next_filepos);
652 653 654 655 656
	}
	else
	{
	  mi_int2store(temp+1,*reclength);
	  mi_int2store(temp+3,length-head_length);
657
	  mi_sizestore((uchar*) temp+5,next_filepos);
658
	}
unknown's avatar
unknown committed
659 660 661 662 663 664 665 666 667
      }
    }
    else
    {
      head_length=3+8+long_block;
      temp[0]=11+(uchar) long_block;
      if (long_block)
      {
	mi_int3store(temp+1,length-head_length);
668
	mi_sizestore((uchar*) temp+4,next_filepos);
unknown's avatar
unknown committed
669 670 671 672
      }
      else
      {
	mi_int2store(temp+1,length-head_length);
673
	mi_sizestore((uchar*) temp+3,next_filepos);
unknown's avatar
unknown committed
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
      }
    }
  }
  else
  {					/* Block with empty info last */
    head_length=4+long_block;
    extra_length= length- *reclength-head_length;
    temp[0]= (uchar) (3+ *flag)+(uchar) long_block; /* 3,4 or 9,10 */
    if (long_block)
    {
      mi_int3store(temp+1,*reclength);
      temp[4]= (uchar) (extra_length);
    }
    else
    {
      mi_int2store(temp+1,*reclength);
      temp[3]= (uchar) (extra_length);
    }
    length=	  *reclength+head_length;	/* Write only what is needed */
  }
694
  DBUG_DUMP("header",(uchar*) temp,head_length);
unknown's avatar
unknown committed
695 696 697 698

	/* Make a long block for one write */
  record_end= *record+length-head_length;
  del_length=(res_length ? MI_DYN_DELETE_BLOCK_HEADER : 0);
699
  bmove((uchar*) (*record-head_length),(uchar*) temp,head_length);
unknown's avatar
unknown committed
700
  memcpy(temp,record_end,(size_t) (extra_length+del_length));
701
  bzero((uchar*) record_end,extra_length);
unknown's avatar
unknown committed
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740

  if (res_length)
  {
    /* Check first if we can join this block with the next one */
    MI_BLOCK_INFO del_block;
    my_off_t next_block=filepos+length+extra_length+res_length;

    del_block.second_read=0;
    if (next_block < info->state->data_file_length &&
	info->s->state.dellink != HA_OFFSET_ERROR)
    {
      if ((_mi_get_block_info(&del_block,info->dfile,next_block)
	   & BLOCK_DELETED) &&
	  res_length + del_block.block_len < MI_DYN_MAX_BLOCK_LENGTH)
      {
	if (unlink_deleted_block(info,&del_block))
	  goto err;
	res_length+=del_block.block_len;
      }
    }

    /* Create a delete link of the last part of the block */
    pos=record_end+extra_length;
    pos[0]= '\0';
    mi_int3store(pos+1,res_length);
    mi_sizestore(pos+4,info->s->state.dellink);
    bfill(pos+12,8,255);			/* End link */
    next_delete_block=info->s->state.dellink;
    info->s->state.dellink= filepos+length+extra_length;
    info->state->del++;
    info->state->empty+=res_length;
    info->s->state.split++;
  }
  if (info->opt_flag & WRITE_CACHE_USED &&
      info->update & HA_STATE_WRITE_AT_END)
  {
    if (info->update & HA_STATE_EXTEND_BLOCK)
    {
      info->update&= ~HA_STATE_EXTEND_BLOCK;
741
      if (my_block_write(&info->rec_cache,(uchar*) *record-head_length,
742
			 length+extra_length+del_length,filepos))
unknown's avatar
unknown committed
743 744
      goto err;
    }
745
    else if (my_b_write(&info->rec_cache,(uchar*) *record-head_length,
unknown's avatar
unknown committed
746 747 748 749 750 751
			length+extra_length+del_length))
      goto err;
  }
  else
  {
    info->rec_cache.seek_not_done=1;
752
    if (info->s->file_write(info,(uchar*) *record-head_length,length+extra_length+
unknown's avatar
unknown committed
753 754 755 756 757 758 759 760
		  del_length,filepos,info->s->write_flag))
      goto err;
  }
  memcpy(record_end,temp,(size_t) (extra_length+del_length));
  *record=record_end;
  *reclength-=(length-head_length);
  *flag=6;

761
  if (del_length)
unknown's avatar
unknown committed
762 763
  {
    /* link the next delete block to this */
764 765
    if (update_backward_delete_link(info, next_delete_block,
				    info->s->state.dellink))
unknown's avatar
unknown committed
766 767 768 769 770 771 772 773 774 775 776 777
      goto err;
  }

  DBUG_RETURN(0);
err:
  DBUG_PRINT("exit",("errno: %d",my_errno));
  DBUG_RETURN(1);
} /*_mi_write_part_record */


	/* update record from datafile */

778
static int update_dynamic_record(MI_INFO *info, my_off_t filepos, uchar *record,
unknown's avatar
unknown committed
779 780 781 782 783 784 785 786 787
				 ulong reclength)
{
  int flag;
  uint error;
  ulong length;
  MI_BLOCK_INFO block_info;
  DBUG_ENTER("update_dynamic_record");

  flag=block_info.second_read=0;
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
  /*
     Check if we have enough room for the record.
     First we do simplified check to make usual case faster.
     Then we do more precise check for the space left.
     Though it still is not absolutely precise, as
     we always use MI_MAX_DYN_BLOCK_HEADER while it can be
     less in the most of the cases.
  */

  /*
    compare with just the reclength as we're going
    to get some space from the old replaced record
  */
  if (unlikely(info->s->base.max_data_file_length -
        info->state->data_file_length < reclength))
  {
    /*
       let's read the old record's block to find out the length of the
       old record
    */
    if ((error=_mi_get_block_info(&block_info,info->dfile,filepos))
        & (BLOCK_DELETED | BLOCK_ERROR | BLOCK_SYNC_ERROR | BLOCK_FATAL_ERROR))
    {
      DBUG_PRINT("error",("Got wrong block info"));
      if (!(error & BLOCK_FATAL_ERROR))
        my_errno=HA_ERR_WRONG_IN_RECORD;
      goto err;
    }

    /*
      if new record isn't longer, we can go on safely
    */
    if (block_info.rec_len < reclength)
    {
      if (info->s->base.max_data_file_length - info->state->data_file_length +
          info->state->empty - info->state->del * MI_MAX_DYN_BLOCK_HEADER <
          reclength - block_info.rec_len + MI_MAX_DYN_BLOCK_HEADER)
      {
        my_errno=HA_ERR_RECORD_FILE_FULL;
        goto err;
      }
    }
    block_info.second_read=0;
  }

unknown's avatar
unknown committed
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
  while (reclength > 0)
  {
    if (filepos != info->s->state.dellink)
    {
      block_info.next_filepos= HA_OFFSET_ERROR;
      if ((error=_mi_get_block_info(&block_info,info->dfile,filepos))
	  & (BLOCK_DELETED | BLOCK_ERROR | BLOCK_SYNC_ERROR |
	     BLOCK_FATAL_ERROR))
      {
	DBUG_PRINT("error",("Got wrong block info"));
	if (!(error & BLOCK_FATAL_ERROR))
	  my_errno=HA_ERR_WRONG_IN_RECORD;
	goto err;
      }
      length=(ulong) (block_info.filepos-filepos) + block_info.block_len;
      if (length < reclength)
      {
	uint tmp=MY_ALIGN(reclength - length + 3 +
			  test(reclength >= 65520L),MI_DYN_ALIGN_SIZE);
852 853
	/* Don't create a block bigger than MI_MAX_BLOCK_LENGTH */
	tmp= min(length+tmp, MI_MAX_BLOCK_LENGTH)-length;
unknown's avatar
unknown committed
854 855 856 857 858 859 860 861 862 863 864 865 866 867
	/* Check if we can extend this block */
	if (block_info.filepos + block_info.block_len ==
	    info->state->data_file_length &&
	    info->state->data_file_length <
	    info->s->base.max_data_file_length-tmp)
	{
	  /* extend file */
	  DBUG_PRINT("info",("Extending file with %d bytes",tmp));
	  if (info->nextpos == info->state->data_file_length)
	    info->nextpos+= tmp;
	  info->state->data_file_length+= tmp;
	  info->update|= HA_STATE_WRITE_AT_END | HA_STATE_EXTEND_BLOCK;
	  length+=tmp;
	}
868
	else if (length < MI_MAX_BLOCK_LENGTH - MI_MIN_BLOCK_LENGTH)
unknown's avatar
unknown committed
869
	{
870 871 872 873 874 875 876
	  /*
	    Check if next block is a deleted block
	    Above we have MI_MIN_BLOCK_LENGTH to avoid the problem where
	    the next block is so small it can't be splited which could
	    casue problems
	  */

unknown's avatar
unknown committed
877 878 879 880 881 882 883 884 885 886
	  MI_BLOCK_INFO del_block;
	  del_block.second_read=0;
	  if (_mi_get_block_info(&del_block,info->dfile,
				 block_info.filepos + block_info.block_len) &
	      BLOCK_DELETED)
	  {
	    /* Use; Unlink it and extend the current block */
	    DBUG_PRINT("info",("Extending current block"));
	    if (unlink_deleted_block(info,&del_block))
	      goto err;
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
	    if ((length+=del_block.block_len) > MI_MAX_BLOCK_LENGTH)
	    {
	      /*
		New block was too big, link overflow part back to
		delete list
	      */
	      my_off_t next_pos;
	      ulong rest_length= length-MI_MAX_BLOCK_LENGTH;
	      set_if_bigger(rest_length, MI_MIN_BLOCK_LENGTH);
	      next_pos= del_block.filepos+ del_block.block_len - rest_length;

	      if (update_backward_delete_link(info, info->s->state.dellink,
					      next_pos))
		DBUG_RETURN(1);

	      /* create delete link for data that didn't fit into the page */
	      del_block.header[0]=0;
	      mi_int3store(del_block.header+1, rest_length);
	      mi_sizestore(del_block.header+4,info->s->state.dellink);
	      bfill(del_block.header+12,8,255);
907
	      if (info->s->file_write(info,(uchar*) del_block.header,20, next_pos,
908 909 910 911 912 913 914 915
			    MYF(MY_NABP)))
		DBUG_RETURN(1);
	      info->s->state.dellink= next_pos;
	      info->s->state.split++;
	      info->state->del++;
	      info->state->empty+= rest_length;
	      length-= rest_length;
	    }
unknown's avatar
unknown committed
916 917 918 919 920 921 922 923 924 925 926 927 928
	  }
	}
      }
    }
    else
    {
      if (_mi_find_writepos(info,reclength,&filepos,&length))
	goto err;
    }
    if (_mi_write_part_record(info,filepos,length,block_info.next_filepos,
			      &record,&reclength,&flag))
      goto err;
    if ((filepos=block_info.next_filepos) == HA_OFFSET_ERROR)
929 930
    {
      /* Start writing data on deleted blocks */
unknown's avatar
unknown committed
931
      filepos=info->s->state.dellink;
932
    }
unknown's avatar
unknown committed
933 934 935 936 937 938 939 940 941 942 943 944 945
  }

  if (block_info.next_filepos != HA_OFFSET_ERROR)
    if (delete_dynamic_record(info,block_info.next_filepos,1))
      goto err;
  DBUG_RETURN(0);
err:
  DBUG_RETURN(1);
}


	/* Pack a record. Return new reclength */

946 947
uint _mi_rec_pack(MI_INFO *info, register uchar *to,
                  register const uchar *from)
unknown's avatar
unknown committed
948 949
{
  uint		length,new_length,flag,bit,i;
950
  uchar		*pos,*end,*startpos,*packpos;
unknown's avatar
unknown committed
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
  enum en_fieldtype type;
  reg3 MI_COLUMNDEF *rec;
  MI_BLOB	*blob;
  DBUG_ENTER("_mi_rec_pack");

  flag=0 ; bit=1;
  startpos=packpos=to; to+= info->s->base.pack_bits; blob=info->blobs;
  rec=info->s->rec;

  for (i=info->s->base.fields ; i-- > 0; from+= length,rec++)
  {
    length=(uint) rec->length;
    if ((type = (enum en_fieldtype) rec->type) != FIELD_NORMAL)
    {
      if (type == FIELD_BLOB)
      {
	if (!blob->length)
	  flag|=bit;
	else
	{
	  char *temp_pos;
	  size_t tmp_length=length-mi_portable_sizeof_char_ptr;
973
	  memcpy((uchar*) to,from,tmp_length);
unknown's avatar
unknown committed
974 975 976 977 978 979
	  memcpy_fixed(&temp_pos,from+tmp_length,sizeof(char*));
	  memcpy(to+tmp_length,temp_pos,(size_t) blob->length);
	  to+=tmp_length+blob->length;
	}
	blob++;
      }
unknown's avatar
unknown committed
980
      else if (type == FIELD_SKIP_ZERO)
unknown's avatar
unknown committed
981
      {
982
	if (memcmp((uchar*) from,zero_string,length) == 0)
unknown's avatar
unknown committed
983 984 985
	  flag|=bit;
	else
	{
986
	  memcpy((uchar*) to,from,(size_t) length); to+=length;
unknown's avatar
unknown committed
987 988
	}
      }
unknown's avatar
unknown committed
989 990
      else if (type == FIELD_SKIP_ENDSPACE ||
	       type == FIELD_SKIP_PRESPACE)
unknown's avatar
unknown committed
991
      {
992
	pos= (uchar*) from; end= (uchar*) from + length;
unknown's avatar
unknown committed
993
	if (type == FIELD_SKIP_ENDSPACE)
unknown's avatar
unknown committed
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
	{					/* Pack trailing spaces */
	  while (end > from && *(end-1) == ' ')
	    end--;
	}
	else
	{					/* Pack pref-spaces */
	  while (pos < end && *pos == ' ')
	    pos++;
	}
	new_length=(uint) (end-pos);
	if (new_length +1 + test(rec->length > 255 && new_length > 127)
	    < length)
	{
	  if (rec->length > 255 && new_length > 127)
	  {
1009 1010 1011 1012 1013 1014
            to[0]= (uchar) ((new_length & 127) + 128);
            to[1]= (uchar) (new_length >> 7);
            to+=2;
          }
          else
            *to++= (uchar) new_length;
1015
	  memcpy((uchar*) to,pos,(size_t) new_length); to+=new_length;
unknown's avatar
unknown committed
1016 1017 1018 1019 1020 1021 1022 1023 1024
	  flag|=bit;
	}
	else
	{
	  memcpy(to,from,(size_t) length); to+=length;
	}
      }
      else if (type == FIELD_VARCHAR)
      {
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
        uint pack_length= HA_VARCHAR_PACKLENGTH(rec->length -1);
	uint tmp_length;
        if (pack_length == 1)
        {
          tmp_length= (uint) *(uchar*) from;
          *to++= *from;
        }
        else
        {
          tmp_length= uint2korr(from);
          store_key_length_inc(to,tmp_length);
        }
        memcpy(to, from+pack_length,tmp_length);
        to+= tmp_length;
        continue;
unknown's avatar
unknown committed
1040 1041 1042 1043 1044 1045 1046 1047
      }
      else
      {
	memcpy(to,from,(size_t) length); to+=length;
	continue;				/* Normal field */
      }
      if ((bit= bit << 1) >= 256)
      {
1048
        *packpos++= (uchar) flag;
unknown's avatar
unknown committed
1049 1050 1051 1052 1053 1054 1055 1056 1057
	bit=1; flag=0;
      }
    }
    else
    {
      memcpy(to,from,(size_t) length); to+=length;
    }
  }
  if (bit != 1)
1058
    *packpos= (uchar) flag;
unknown's avatar
unknown committed
1059
  if (info->s->calc_checksum)
1060
    *to++= (uchar) info->checksum;
unknown's avatar
unknown committed
1061 1062 1063 1064 1065 1066 1067
  DBUG_PRINT("exit",("packed length: %d",(int) (to-startpos)));
  DBUG_RETURN((uint) (to-startpos));
} /* _mi_rec_pack */



/*
1068 1069
  Check if a record was correctly packed. Used only by myisamchk
  Returns 0 if record is ok.
unknown's avatar
unknown committed
1070 1071
*/

1072
my_bool _mi_rec_check(MI_INFO *info,const uchar *record, uchar *rec_buff,
1073
                      ulong packed_length, my_bool with_checksum)
unknown's avatar
unknown committed
1074 1075
{
  uint		length,new_length,flag,bit,i;
1076
  uchar		*pos,*end,*packpos,*to;
unknown's avatar
unknown committed
1077 1078 1079 1080
  enum en_fieldtype type;
  reg3 MI_COLUMNDEF *rec;
  DBUG_ENTER("_mi_rec_check");

1081
  packpos=rec_buff; to= rec_buff+info->s->base.pack_bits;
unknown's avatar
unknown committed
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
  rec=info->s->rec;
  flag= *packpos; bit=1;

  for (i=info->s->base.fields ; i-- > 0; record+= length, rec++)
  {
    length=(uint) rec->length;
    if ((type = (enum en_fieldtype) rec->type) != FIELD_NORMAL)
    {
      if (type == FIELD_BLOB)
      {
	uint blob_length=
	  _mi_calc_blob_length(length-mi_portable_sizeof_char_ptr,record);
	if (!blob_length && !(flag & bit))
	  goto err;
	if (blob_length)
	  to+=length - mi_portable_sizeof_char_ptr+ blob_length;
      }
unknown's avatar
unknown committed
1099
      else if (type == FIELD_SKIP_ZERO)
unknown's avatar
unknown committed
1100
      {
1101
	if (memcmp((uchar*) record,zero_string,length) == 0)
unknown's avatar
unknown committed
1102 1103 1104 1105 1106 1107 1108
	{
	  if (!(flag & bit))
	    goto err;
	}
	else
	  to+=length;
      }
unknown's avatar
unknown committed
1109 1110
      else if (type == FIELD_SKIP_ENDSPACE ||
	       type == FIELD_SKIP_PRESPACE)
unknown's avatar
unknown committed
1111
      {
1112
	pos= (uchar*) record; end= (uchar*) record + length;
unknown's avatar
unknown committed
1113
	if (type == FIELD_SKIP_ENDSPACE)
unknown's avatar
unknown committed
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
	{					/* Pack trailing spaces */
	  while (end > record && *(end-1) == ' ')
	    end--;
	}
	else
	{					/* Pack pre-spaces */
	  while (pos < end && *pos == ' ')
	    pos++;
	}
	new_length=(uint) (end-pos);
	if (new_length +1 + test(rec->length > 255 && new_length > 127)
	    < length)
	{
	  if (!(flag & bit))
	    goto err;
	  if (rec->length > 255 && new_length > 127)
	  {
1131 1132 1133 1134 1135 1136 1137 1138
            /* purecov: begin inspected */
            if (to[0] != (uchar) ((new_length & 127) + 128) ||
                to[1] != (uchar) (new_length >> 7))
              goto err;
            to+=2;
            /* purecov: end */
          }
          else if (*to++ != (uchar) new_length)
unknown's avatar
unknown committed
1139 1140 1141 1142 1143 1144 1145 1146
	    goto err;
	  to+=new_length;
	}
	else
	  to+=length;
      }
      else if (type == FIELD_VARCHAR)
      {
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
        uint pack_length= HA_VARCHAR_PACKLENGTH(rec->length -1);
	uint tmp_length;
        if (pack_length == 1)
        {
          tmp_length= (uint) *(uchar*) record;
          to+= 1+ tmp_length;
          continue;
        }
        else
        {
          tmp_length= uint2korr(record);
          to+= get_pack_length(tmp_length)+tmp_length;
        }
        continue;
unknown's avatar
unknown committed
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
      }
      else
      {
	to+=length;
	continue;				/* Normal field */
      }
      if ((bit= bit << 1) >= 256)
      {
	flag= *++packpos;
	bit=1;
      }
    }
    else
1174
      to+= length;
unknown's avatar
unknown committed
1175
  }
unknown's avatar
unknown committed
1176
  if (packed_length != (uint) (to - rec_buff) + test(info->s->calc_checksum) ||
unknown's avatar
unknown committed
1177 1178
      (bit != 1 && (flag & ~(bit - 1))))
    goto err;
1179
  if (with_checksum && ((uchar) info->checksum != (uchar) *to))
unknown's avatar
unknown committed
1180
  {
1181 1182
    DBUG_PRINT("error",("wrong checksum for row"));
    goto err;
unknown's avatar
unknown committed
1183 1184 1185
  }
  DBUG_RETURN(0);

unknown's avatar
unknown committed
1186
err:
unknown's avatar
unknown committed
1187 1188 1189 1190 1191 1192 1193 1194 1195
  DBUG_RETURN(1);
}



	/* Unpacks a record */
	/* Returns -1 and my_errno =HA_ERR_RECORD_DELETED if reclength isn't */
	/* right. Returns reclength (>0) if ok */

1196
ulong _mi_rec_unpack(register MI_INFO *info, register uchar *to, uchar *from,
unknown's avatar
unknown committed
1197 1198 1199 1200
		     ulong found_length)
{
  uint flag,bit,length,rec_length,min_pack_length;
  enum en_fieldtype type;
1201
  uchar *from_end,*to_end,*packpos;
unknown's avatar
unknown committed
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
  reg3 MI_COLUMNDEF *rec,*end_field;
  DBUG_ENTER("_mi_rec_unpack");

  to_end=to + info->s->base.reclength;
  from_end=from+found_length;
  flag= (uchar) *from; bit=1; packpos=from;
  if (found_length < info->s->base.min_pack_length)
    goto err;
  from+= info->s->base.pack_bits;
  min_pack_length=info->s->base.min_pack_length - info->s->base.pack_bits;

  for (rec=info->s->rec , end_field=rec+info->s->base.fields ;
       rec < end_field ; to+= rec_length, rec++)
  {
    rec_length=rec->length;
    if ((type = (enum en_fieldtype) rec->type) != FIELD_NORMAL &&
	(type != FIELD_CHECK))
    {
      if (type == FIELD_VARCHAR)
      {
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
        uint pack_length= HA_VARCHAR_PACKLENGTH(rec_length-1);
        if (pack_length == 1)
        {
          length= (uint) *(uchar*) from;
          if (length > rec_length-1)
            goto err;
          *to= *from++;
        }
        else
        {
          get_key_length(length, from);
          if (length > rec_length-2)
            goto err;
          int2store(to,length);
        }
        if (from+length > from_end)
          goto err;
        memcpy(to+pack_length, from, length);
        from+= length;
        min_pack_length--;
        continue;
unknown's avatar
unknown committed
1243 1244 1245
      }
      if (flag & bit)
      {
unknown's avatar
unknown committed
1246
	if (type == FIELD_BLOB || type == FIELD_SKIP_ZERO)
1247
	  bzero((uchar*) to,rec_length);
unknown's avatar
unknown committed
1248 1249
	else if (type == FIELD_SKIP_ENDSPACE ||
		 type == FIELD_SKIP_PRESPACE)
unknown's avatar
unknown committed
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
	{
	  if (rec->length > 255 && *from & 128)
	  {
	    if (from + 1 >= from_end)
	      goto err;
	    length= (*from & 127)+ ((uint) (uchar) *(from+1) << 7); from+=2;
	  }
	  else
	  {
	    if (from == from_end)
	      goto err;
	    length= (uchar) *from++;
	  }
	  min_pack_length--;
	  if (length >= rec_length ||
	      min_pack_length + length > (uint) (from_end - from))
	    goto err;
unknown's avatar
unknown committed
1267
	  if (type == FIELD_SKIP_ENDSPACE)
unknown's avatar
unknown committed
1268
	  {
1269 1270
	    memcpy(to,(uchar*) from,(size_t) length);
	    bfill((uchar*) to+length,rec_length-length,' ');
unknown's avatar
unknown committed
1271 1272 1273
	  }
	  else
	  {
1274 1275
	    bfill((uchar*) to,rec_length-length,' ');
	    memcpy(to+rec_length-length,(uchar*) from,(size_t) length);
unknown's avatar
unknown committed
1276 1277 1278 1279 1280 1281 1282 1283
	  }
	  from+=length;
	}
      }
      else if (type == FIELD_BLOB)
      {
	uint size_length=rec_length- mi_portable_sizeof_char_ptr;
	ulong blob_length=_mi_calc_blob_length(size_length,from);
1284 1285 1286 1287 1288
        ulong from_left= (ulong) (from_end - from);
        if (from_left < size_length ||
            from_left - size_length < blob_length ||
            from_left - size_length - blob_length < min_pack_length)
          goto err;
1289
	memcpy((uchar*) to,(uchar*) from,(size_t) size_length);
unknown's avatar
unknown committed
1290
	from+=size_length;
1291
	memcpy_fixed((uchar*) to+size_length,(uchar*) &from,sizeof(char*));
unknown's avatar
unknown committed
1292 1293 1294 1295
	from+=blob_length;
      }
      else
      {
unknown's avatar
unknown committed
1296
	if (type == FIELD_SKIP_ENDSPACE || type == FIELD_SKIP_PRESPACE)
unknown's avatar
unknown committed
1297 1298 1299
	  min_pack_length--;
	if (min_pack_length + rec_length > (uint) (from_end - from))
	  goto err;
1300
	memcpy(to,(uchar*) from,(size_t) rec_length); from+=rec_length;
unknown's avatar
unknown committed
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
      }
      if ((bit= bit << 1) >= 256)
      {
	flag= (uchar) *++packpos; bit=1;
      }
    }
    else
    {
      if (min_pack_length > (uint) (from_end - from))
	goto err;
      min_pack_length-=rec_length;
1312
      memcpy(to, (uchar*) from, (size_t) rec_length);
1313
      from+=rec_length;
unknown's avatar
unknown committed
1314 1315 1316 1317 1318
    }
  }
  if (info->s->calc_checksum)
    from++;
  if (to == to_end && from == from_end && (bit == 1 || !(flag & ~(bit-1))))
unknown's avatar
unknown committed
1319
    DBUG_RETURN(found_length);
1320

unknown's avatar
unknown committed
1321
err:
1322
  my_errno= HA_ERR_WRONG_IN_RECORD;
unknown's avatar
unknown committed
1323 1324
  DBUG_PRINT("error",("to_end: 0x%lx -> 0x%lx  from_end: 0x%lx -> 0x%lx",
		      (long) to, (long) to_end, (long) from, (long) from_end));
1325
  DBUG_DUMP("from",(uchar*) info->rec_buff,info->s->base.min_pack_length);
unknown's avatar
unknown committed
1326 1327 1328 1329 1330 1331
  DBUG_RETURN(MY_FILE_ERROR);
} /* _mi_rec_unpack */


	/* Calc length of blob. Update info in blobs->length */

1332
ulong _my_calc_total_blob_length(MI_INFO *info, const uchar *record)
unknown's avatar
unknown committed
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
{
  ulong length;
  MI_BLOB *blob,*end;

  for (length=0, blob= info->blobs, end=blob+info->s->base.blobs ;
       blob != end;
       blob++)
  {
    blob->length=_mi_calc_blob_length(blob->pack_length,record + blob->offset);
    length+=blob->length;
  }
  return length;
}


1348
ulong _mi_calc_blob_length(uint length, const uchar *pos)
unknown's avatar
unknown committed
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
{
  switch (length) {
  case 1:
    return (uint) (uchar) *pos;
  case 2:
    return (uint) uint2korr(pos);
  case 3:
    return uint3korr(pos);
  case 4:
    return uint4korr(pos);
  default:
    break;
  }
  return 0; /* Impossible */
}


1366
void _my_store_blob_length(uchar *pos,uint pack_length,uint length)
unknown's avatar
unknown committed
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
{
  switch (pack_length) {
  case 1:
    *pos= (uchar) length;
    break;
  case 2:
    int2store(pos,length);
    break;
  case 3:
    int3store(pos,length);
    break;
  case 4:
    int4store(pos,length);
  default:
    break;
  }
  return;
}


1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
/*
  Read record from datafile.

  SYNOPSIS
    _mi_read_dynamic_record()
      info                      MI_INFO pointer to table.
      filepos                   From where to read the record.
      buf                       Destination for record.

  NOTE

    If a write buffer is active, it needs to be flushed if its contents
    intersects with the record to read. We always check if the position
    of the first byte of the write buffer is lower than the position
    past the last byte to read. In theory this is also true if the write
    buffer is completely below the read segment. That is, if there is no
    intersection. But this case is unusual. We flush anyway. Only if the
    first byte in the write buffer is above the last byte to read, we do
    not flush.

    A dynamic record may need several reads. So this check must be done
    before every read. Reading a dynamic record starts with reading the
    block header. If the record does not fit into the free space of the
    header, the block may be longer than the header. In this case a
    second read is necessary. These one or two reads repeat for every
    part of the record.

  RETURN
    0           OK
    -1          Error
*/
unknown's avatar
unknown committed
1418

1419
int _mi_read_dynamic_record(MI_INFO *info, my_off_t filepos, uchar *buf)
unknown's avatar
unknown committed
1420
{
1421
  int block_of_record;
unknown's avatar
unknown committed
1422
  uint b_type,left_length;
1423
  uchar *to;
unknown's avatar
unknown committed
1424 1425 1426 1427 1428 1429 1430 1431 1432
  MI_BLOCK_INFO block_info;
  File file;
  DBUG_ENTER("mi_read_dynamic_record");

  if (filepos != HA_OFFSET_ERROR)
  {
    LINT_INIT(to);
    LINT_INIT(left_length);
    file=info->dfile;
1433 1434
    block_of_record= 0;   /* First block of record is numbered as zero. */
    block_info.second_read= 0;
unknown's avatar
unknown committed
1435 1436
    do
    {
1437 1438 1439
      /* A corrupted table can have wrong pointers. (Bug# 19835) */
      if (filepos == HA_OFFSET_ERROR)
        goto panic;
unknown's avatar
unknown committed
1440
      if (info->opt_flag & WRITE_CACHE_USED &&
1441
	  info->rec_cache.pos_in_file < filepos + MI_BLOCK_INFO_HEADER_LENGTH &&
unknown's avatar
unknown committed
1442 1443 1444
	  flush_io_cache(&info->rec_cache))
	goto err;
      info->rec_cache.seek_not_done=1;
1445
      if ((b_type= _mi_get_block_info(&block_info, file, filepos))
unknown's avatar
unknown committed
1446 1447 1448 1449 1450 1451 1452
	  & (BLOCK_DELETED | BLOCK_ERROR | BLOCK_SYNC_ERROR |
	     BLOCK_FATAL_ERROR))
      {
	if (b_type & (BLOCK_SYNC_ERROR | BLOCK_DELETED))
	  my_errno=HA_ERR_RECORD_DELETED;
	goto err;
      }
1453
      if (block_of_record++ == 0)			/* First block */
unknown's avatar
unknown committed
1454 1455 1456 1457 1458
      {
	if (block_info.rec_len > (uint) info->s->base.max_pack_length)
	  goto panic;
	if (info->s->base.blobs)
	{
1459
	  if (!(to=mi_alloc_rec_buff(info, block_info.rec_len,
1460
				     &info->rec_buff)))
unknown's avatar
unknown committed
1461 1462 1463 1464 1465 1466 1467 1468
	    goto err;
	}
	else
	  to= info->rec_buff;
	left_length=block_info.rec_len;
      }
      if (left_length < block_info.data_len || ! block_info.data_len)
	goto panic;			/* Wrong linked record */
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
      /* copy information that is already read */
      {
        uint offset= (uint) (block_info.filepos - filepos);
        uint prefetch_len= (sizeof(block_info.header) - offset);
        filepos+= sizeof(block_info.header);

        if (prefetch_len > block_info.data_len)
          prefetch_len= block_info.data_len;
        if (prefetch_len)
        {
1479
          memcpy((uchar*) to, block_info.header + offset, prefetch_len);
1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491
          block_info.data_len-= prefetch_len;
          left_length-= prefetch_len;
          to+= prefetch_len;
        }
      }
      /* read rest of record from file */
      if (block_info.data_len)
      {
        if (info->opt_flag & WRITE_CACHE_USED &&
            info->rec_cache.pos_in_file < filepos + block_info.data_len &&
            flush_io_cache(&info->rec_cache))
          goto err;
1492 1493 1494 1495 1496
        /*
          What a pity that this method is not called 'file_pread' and that
          there is no equivalent without seeking. We are at the right
          position already. :(
        */
1497
        if (info->s->file_read(info, (uchar*) to, block_info.data_len,
1498
                               filepos, MYF(MY_NABP)))
1499 1500 1501 1502 1503
          goto panic;
        left_length-=block_info.data_len;
        to+=block_info.data_len;
      }
      filepos= block_info.next_filepos;
unknown's avatar
unknown committed
1504 1505 1506
    } while (left_length);

    info->update|= HA_STATE_AKTIV;	/* We have a aktive record */
unknown's avatar
unknown committed
1507
    fast_mi_writeinfo(info);
unknown's avatar
unknown committed
1508 1509 1510
    DBUG_RETURN(_mi_rec_unpack(info,buf,info->rec_buff,block_info.rec_len) !=
		MY_FILE_ERROR ? 0 : -1);
  }
unknown's avatar
unknown committed
1511
  fast_mi_writeinfo(info);
unknown's avatar
unknown committed
1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
  DBUG_RETURN(-1);			/* Wrong data to read */

panic:
  my_errno=HA_ERR_WRONG_IN_RECORD;
err:
  VOID(_mi_writeinfo(info,0));
  DBUG_RETURN(-1);
}

	/* compare unique constraint between stored rows */

int _mi_cmp_dynamic_unique(MI_INFO *info, MI_UNIQUEDEF *def,
1524
			   const uchar *record, my_off_t pos)
unknown's avatar
unknown committed
1525
{
1526
  uchar *rec_buff,*old_record;
unknown's avatar
unknown committed
1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
  int error;
  DBUG_ENTER("_mi_cmp_dynamic_unique");

  if (!(old_record=my_alloca(info->s->base.reclength)))
    DBUG_RETURN(1);

  /* Don't let the compare destroy blobs that may be in use */
  rec_buff=info->rec_buff;
  if (info->s->base.blobs)
    info->rec_buff=0;
  error=_mi_read_dynamic_record(info,pos,old_record);
  if (!error)
    error=mi_unique_comp(def, record, old_record, def->null_are_equal);
  if (info->s->base.blobs)
  {
1542
    my_free(mi_get_rec_buff_ptr(info, info->rec_buff), MYF(MY_ALLOW_ZERO_PTR));
unknown's avatar
unknown committed
1543 1544 1545 1546 1547 1548 1549 1550 1551
    info->rec_buff=rec_buff;
  }
  my_afree(old_record);
  DBUG_RETURN(error);
}


	/* Compare of record one disk with packed record in memory */

1552
int _mi_cmp_dynamic_record(register MI_INFO *info, register const uchar *record)
unknown's avatar
unknown committed
1553 1554 1555
{
  uint flag,reclength,b_type;
  my_off_t filepos;
1556
  uchar *buffer;
unknown's avatar
unknown committed
1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577
  MI_BLOCK_INFO block_info;
  DBUG_ENTER("_mi_cmp_dynamic_record");

	/* We are going to do changes; dont let anybody disturb */
  dont_break();				/* Dont allow SIGHUP or SIGINT */

  if (info->opt_flag & WRITE_CACHE_USED)
  {
    info->update&= ~(HA_STATE_WRITE_AT_END | HA_STATE_EXTEND_BLOCK);
    if (flush_io_cache(&info->rec_cache))
      DBUG_RETURN(-1);
  }
  info->rec_cache.seek_not_done=1;

	/* If nobody have touched the database we don't have to test rec */

  buffer=info->rec_buff;
  if ((info->opt_flag & READ_CHECK_USED))
  {						/* If check isn't disabled  */
    if (info->s->base.blobs)
    {
1578
      if (!(buffer=(uchar*) my_alloca(info->s->base.pack_reclength+
unknown's avatar
unknown committed
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623
				     _my_calc_total_blob_length(info,record))))
	DBUG_RETURN(-1);
    }
    reclength=_mi_rec_pack(info,buffer,record);
    record= buffer;

    filepos=info->lastpos;
    flag=block_info.second_read=0;
    block_info.next_filepos=filepos;
    while (reclength > 0)
    {
      if ((b_type=_mi_get_block_info(&block_info,info->dfile,
				    block_info.next_filepos))
	  & (BLOCK_DELETED | BLOCK_ERROR | BLOCK_SYNC_ERROR |
	     BLOCK_FATAL_ERROR))
      {
	if (b_type & (BLOCK_SYNC_ERROR | BLOCK_DELETED))
	  my_errno=HA_ERR_RECORD_CHANGED;
	goto err;
      }
      if (flag == 0)				/* First block */
      {
	flag=1;
	if (reclength != block_info.rec_len)
	{
	  my_errno=HA_ERR_RECORD_CHANGED;
	  goto err;
	}
      } else if (reclength < block_info.data_len)
      {
	my_errno=HA_ERR_WRONG_IN_RECORD;
	goto err;
      }
      reclength-=block_info.data_len;
      if (_mi_cmp_buffer(info->dfile,record,block_info.filepos,
			 block_info.data_len))
      {
	my_errno=HA_ERR_RECORD_CHANGED;
	goto err;
      }
      flag=1;
      record+=block_info.data_len;
    }
  }
  my_errno=0;
unknown's avatar
unknown committed
1624
err:
unknown's avatar
unknown committed
1625
  if (buffer != info->rec_buff)
1626
    my_afree((uchar*) buffer);
unknown's avatar
unknown committed
1627 1628 1629 1630 1631 1632
  DBUG_RETURN(my_errno);
}


	/* Compare file to buffert */

1633
static int _mi_cmp_buffer(File file, const uchar *buff, my_off_t filepos,
unknown's avatar
unknown committed
1634 1635 1636
			  uint length)
{
  uint next_length;
1637
  uchar temp_buff[IO_SIZE*2];
unknown's avatar
unknown committed
1638 1639 1640 1641 1642 1643
  DBUG_ENTER("_mi_cmp_buffer");

  next_length= IO_SIZE*2 - (uint) (filepos & (IO_SIZE-1));

  while (length > IO_SIZE*2)
  {
1644
    if (my_pread(file,temp_buff,next_length,filepos, MYF(MY_NABP)) ||
1645
	memcmp(buff, temp_buff, next_length))
unknown's avatar
unknown committed
1646
      goto err;
1647
    filepos+=next_length;
unknown's avatar
unknown committed
1648 1649 1650 1651
    buff+=next_length;
    length-= next_length;
    next_length=IO_SIZE*2;
  }
1652
  if (my_pread(file,temp_buff,length,filepos,MYF(MY_NABP)))
unknown's avatar
unknown committed
1653
    goto err;
1654
  DBUG_RETURN(memcmp(buff,temp_buff,length));
unknown's avatar
unknown committed
1655 1656 1657 1658 1659
err:
  DBUG_RETURN(1);
}


1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693
/*
  Read record from datafile.

  SYNOPSIS
    _mi_read_rnd_dynamic_record()
      info                      MI_INFO pointer to table.
      buf                       Destination for record.
      filepos                   From where to read the record.
      skip_deleted_blocks       If to repeat reading until a non-deleted
                                record is found.

  NOTE

    If a write buffer is active, it needs to be flushed if its contents
    intersects with the record to read. We always check if the position
    of the first byte of the write buffer is lower than the position
    past the last byte to read. In theory this is also true if the write
    buffer is completely below the read segment. That is, if there is no
    intersection. But this case is unusual. We flush anyway. Only if the
    first byte in the write buffer is above the last byte to read, we do
    not flush.

    A dynamic record may need several reads. So this check must be done
    before every read. Reading a dynamic record starts with reading the
    block header. If the record does not fit into the free space of the
    header, the block may be longer than the header. In this case a
    second read is necessary. These one or two reads repeat for every
    part of the record.

  RETURN
    0           OK
    != 0        Error
*/

1694
int _mi_read_rnd_dynamic_record(MI_INFO *info, uchar *buf,
unknown's avatar
unknown committed
1695
				register my_off_t filepos,
1696
				my_bool skip_deleted_blocks)
unknown's avatar
unknown committed
1697
{
1698
  int block_of_record, info_read, save_errno;
unknown's avatar
unknown committed
1699
  uint left_len,b_type;
1700
  uchar *to;
unknown's avatar
unknown committed
1701 1702 1703 1704 1705 1706 1707 1708 1709 1710
  MI_BLOCK_INFO block_info;
  MYISAM_SHARE *share=info->s;
  DBUG_ENTER("_mi_read_rnd_dynamic_record");

  info_read=0;
  LINT_INIT(to);

  if (info->lock_type == F_UNLCK)
  {
#ifndef UNSAFE_LOCKING
1711
    if (share->tot_locks == 0)
unknown's avatar
unknown committed
1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723
    {
      if (my_lock(share->kfile,F_RDLCK,0L,F_TO_EOF,
		  MYF(MY_SEEK_NOT_DONE) | info->lock_wait))
	DBUG_RETURN(my_errno);
    }
#else
    info->tmp_lock_type=F_RDLCK;
#endif
  }
  else
    info_read=1;				/* memory-keyinfoblock is ok */

1724 1725
  block_of_record= 0;   /* First block of record is numbered as zero. */
  block_info.second_read= 0;
unknown's avatar
unknown committed
1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745
  left_len=1;
  do
  {
    if (filepos >= info->state->data_file_length)
    {
      if (!info_read)
      {						/* Check if changed */
	info_read=1;
	info->rec_cache.seek_not_done=1;
	if (mi_state_info_read_dsk(share->kfile,&share->state,1))
	  goto panic;
      }
      if (filepos >= info->state->data_file_length)
      {
	my_errno= HA_ERR_END_OF_FILE;
	goto err;
      }
    }
    if (info->opt_flag & READ_CACHE_USED)
    {
1746
      if (_mi_read_cache(&info->rec_cache,(uchar*) block_info.header,filepos,
1747
			 sizeof(block_info.header),
1748 1749
			 (!block_of_record && skip_deleted_blocks ?
                          READING_NEXT : 0) | READING_HEADER))
unknown's avatar
unknown committed
1750 1751 1752 1753 1754 1755
	goto panic;
      b_type=_mi_get_block_info(&block_info,-1,filepos);
    }
    else
    {
      if (info->opt_flag & WRITE_CACHE_USED &&
1756
	  info->rec_cache.pos_in_file < filepos + MI_BLOCK_INFO_HEADER_LENGTH &&
unknown's avatar
unknown committed
1757 1758 1759 1760 1761 1762 1763 1764 1765 1766
	  flush_io_cache(&info->rec_cache))
	DBUG_RETURN(my_errno);
      info->rec_cache.seek_not_done=1;
      b_type=_mi_get_block_info(&block_info,info->dfile,filepos);
    }

    if (b_type & (BLOCK_DELETED | BLOCK_ERROR | BLOCK_SYNC_ERROR |
		  BLOCK_FATAL_ERROR))
    {
      if ((b_type & (BLOCK_DELETED | BLOCK_SYNC_ERROR))
1767
	  && skip_deleted_blocks)
unknown's avatar
unknown committed
1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780
      {
	filepos=block_info.filepos+block_info.block_len;
	block_info.second_read=0;
	continue;		/* Search after next_record */
      }
      if (b_type & (BLOCK_DELETED | BLOCK_SYNC_ERROR))
      {
	my_errno=HA_ERR_RECORD_DELETED;
	info->lastpos=block_info.filepos;
	info->nextpos=block_info.filepos+block_info.block_len;
      }
      goto err;
    }
1781
    if (block_of_record == 0)				/* First block */
unknown's avatar
unknown committed
1782 1783 1784 1785 1786 1787
    {
      if (block_info.rec_len > (uint) share->base.max_pack_length)
	goto panic;
      info->lastpos=filepos;
      if (share->base.blobs)
      {
1788
	if (!(to= mi_alloc_rec_buff(info, block_info.rec_len,
1789
				    &info->rec_buff)))
unknown's avatar
unknown committed
1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808
	  goto err;
      }
      else
	to= info->rec_buff;
      left_len=block_info.rec_len;
    }
    if (left_len < block_info.data_len)
      goto panic;				/* Wrong linked record */

    /* copy information that is already read */
    {
      uint offset=(uint) (block_info.filepos - filepos);
      uint tmp_length= (sizeof(block_info.header) - offset);
      filepos=block_info.filepos;

      if (tmp_length > block_info.data_len)
	tmp_length= block_info.data_len;
      if (tmp_length)
      {
1809
	memcpy((uchar*) to, block_info.header+offset,tmp_length);
unknown's avatar
unknown committed
1810 1811 1812 1813
	block_info.data_len-=tmp_length;
	left_len-=tmp_length;
	to+=tmp_length;
	filepos+=tmp_length;
1814
      }
unknown's avatar
unknown committed
1815 1816 1817 1818 1819 1820
    }
    /* read rest of record from file */
    if (block_info.data_len)
    {
      if (info->opt_flag & READ_CACHE_USED)
      {
1821
	if (_mi_read_cache(&info->rec_cache,(uchar*) to,filepos,
unknown's avatar
unknown committed
1822
			   block_info.data_len,
1823 1824
			   (!block_of_record && skip_deleted_blocks) ?
                           READING_NEXT : 0))
unknown's avatar
unknown committed
1825 1826 1827 1828
	  goto panic;
      }
      else
      {
1829 1830 1831 1832 1833
        if (info->opt_flag & WRITE_CACHE_USED &&
            info->rec_cache.pos_in_file <
            block_info.filepos + block_info.data_len &&
            flush_io_cache(&info->rec_cache))
          goto err;
unknown's avatar
unknown committed
1834
	/* VOID(my_seek(info->dfile,filepos,MY_SEEK_SET,MYF(0))); */
1835
	if (my_read(info->dfile,(uchar*) to,block_info.data_len,MYF(MY_NABP)))
unknown's avatar
unknown committed
1836 1837 1838 1839 1840 1841 1842
	{
	  if (my_errno == -1)
	    my_errno= HA_ERR_WRONG_IN_RECORD;	/* Unexpected end of file */
	  goto err;
	}
      }
    }
1843 1844 1845 1846 1847
    /*
      Increment block-of-record counter. If it was the first block,
      remember the position behind the block for the next call.
    */
    if (block_of_record++ == 0)
unknown's avatar
unknown committed
1848
    {
1849 1850
      info->nextpos= block_info.filepos + block_info.block_len;
      skip_deleted_blocks= 0;
unknown's avatar
unknown committed
1851 1852 1853 1854 1855 1856 1857
    }
    left_len-=block_info.data_len;
    to+=block_info.data_len;
    filepos=block_info.next_filepos;
  } while (left_len);

  info->update|= HA_STATE_AKTIV | HA_STATE_KEY_CHANGED;
unknown's avatar
unknown committed
1858
  fast_mi_writeinfo(info);
unknown's avatar
unknown committed
1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881
  if (_mi_rec_unpack(info,buf,info->rec_buff,block_info.rec_len) !=
      MY_FILE_ERROR)
    DBUG_RETURN(0);
  DBUG_RETURN(my_errno);			/* Wrong record */

panic:
  my_errno=HA_ERR_WRONG_IN_RECORD;		/* Something is fatal wrong */
err:
  save_errno=my_errno;
  VOID(_mi_writeinfo(info,0));
  DBUG_RETURN(my_errno=save_errno);
}


	/* Read and process header from a dynamic-record-file */

uint _mi_get_block_info(MI_BLOCK_INFO *info, File file, my_off_t filepos)
{
  uint return_val=0;
  uchar *header=info->header;

  if (file >= 0)
  {
1882 1883 1884 1885 1886
    /*
      We do not use my_pread() here because we want to have the file
      pointer set to the end of the header after this function.
      my_pread() may leave the file pointer untouched.
    */
unknown's avatar
unknown committed
1887
    VOID(my_seek(file,filepos,MY_SEEK_SET,MYF(0)));
1888
    if (my_read(file, header, sizeof(info->header),MYF(0)) !=
unknown's avatar
unknown committed
1889
	sizeof(info->header))
1890
      goto err;
unknown's avatar
unknown committed
1891
  }
1892
  DBUG_DUMP("header",header,MI_BLOCK_INFO_HEADER_LENGTH);
unknown's avatar
unknown committed
1893 1894
  if (info->second_read)
  {
unknown's avatar
unknown committed
1895
    if (info->header[0] <= 6 || info->header[0] == 13)
unknown's avatar
unknown committed
1896 1897 1898 1899
      return_val=BLOCK_SYNC_ERROR;
  }
  else
  {
1900
    if (info->header[0] > 6 && info->header[0] != 13)
unknown's avatar
unknown committed
1901 1902
      return_val=BLOCK_SYNC_ERROR;
  }
1903
  info->next_filepos= HA_OFFSET_ERROR; /* Dummy if no next block */
unknown's avatar
unknown committed
1904 1905 1906 1907 1908 1909

  switch (info->header[0]) {
  case 0:
    if ((info->block_len=(uint) mi_uint3korr(header+1)) <
	MI_MIN_BLOCK_LENGTH ||
	(info->block_len & (MI_DYN_ALIGN_SIZE -1)))
1910
      goto err;
unknown's avatar
unknown committed
1911 1912 1913 1914 1915 1916 1917 1918 1919 1920
    info->filepos=filepos;
    info->next_filepos=mi_sizekorr(header+4);
    info->prev_filepos=mi_sizekorr(header+12);
#if SIZEOF_OFF_T == 4
    if ((mi_uint4korr(header+4) != 0 &&
	 (mi_uint4korr(header+4) != (ulong) ~0 ||
	  info->next_filepos != (ulong) ~0)) ||
	(mi_uint4korr(header+12) != 0 &&
	 (mi_uint4korr(header+12) != (ulong) ~0 ||
	  info->prev_filepos != (ulong) ~0)))
1921
      goto err;
unknown's avatar
unknown committed
1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933
#endif
    return return_val | BLOCK_DELETED;		/* Deleted block */

  case 1:
    info->rec_len=info->data_len=info->block_len=mi_uint2korr(header+1);
    info->filepos=filepos+3;
    return return_val | BLOCK_FIRST | BLOCK_LAST;
  case 2:
    info->rec_len=info->data_len=info->block_len=mi_uint3korr(header+1);
    info->filepos=filepos+4;
    return return_val | BLOCK_FIRST | BLOCK_LAST;

1934 1935 1936 1937 1938 1939 1940 1941
  case 13:
    info->rec_len=mi_uint4korr(header+1);
    info->block_len=info->data_len=mi_uint3korr(header+5);
    info->next_filepos=mi_sizekorr(header+8);
    info->second_read=1;
    info->filepos=filepos+16;
    return return_val | BLOCK_FIRST;

unknown's avatar
unknown committed
1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001
  case 3:
    info->rec_len=info->data_len=mi_uint2korr(header+1);
    info->block_len=info->rec_len+ (uint) header[3];
    info->filepos=filepos+4;
    return return_val | BLOCK_FIRST | BLOCK_LAST;
  case 4:
    info->rec_len=info->data_len=mi_uint3korr(header+1);
    info->block_len=info->rec_len+ (uint) header[4];
    info->filepos=filepos+5;
    return return_val | BLOCK_FIRST | BLOCK_LAST;

  case 5:
    info->rec_len=mi_uint2korr(header+1);
    info->block_len=info->data_len=mi_uint2korr(header+3);
    info->next_filepos=mi_sizekorr(header+5);
    info->second_read=1;
    info->filepos=filepos+13;
    return return_val | BLOCK_FIRST;
  case 6:
    info->rec_len=mi_uint3korr(header+1);
    info->block_len=info->data_len=mi_uint3korr(header+4);
    info->next_filepos=mi_sizekorr(header+7);
    info->second_read=1;
    info->filepos=filepos+15;
    return return_val | BLOCK_FIRST;

    /* The following blocks are identical to 1-6 without rec_len */
  case 7:
    info->data_len=info->block_len=mi_uint2korr(header+1);
    info->filepos=filepos+3;
    return return_val | BLOCK_LAST;
  case 8:
    info->data_len=info->block_len=mi_uint3korr(header+1);
    info->filepos=filepos+4;
    return return_val | BLOCK_LAST;

  case 9:
    info->data_len=mi_uint2korr(header+1);
    info->block_len=info->data_len+ (uint) header[3];
    info->filepos=filepos+4;
    return return_val | BLOCK_LAST;
  case 10:
    info->data_len=mi_uint3korr(header+1);
    info->block_len=info->data_len+ (uint) header[4];
    info->filepos=filepos+5;
    return return_val | BLOCK_LAST;

  case 11:
    info->data_len=info->block_len=mi_uint2korr(header+1);
    info->next_filepos=mi_sizekorr(header+3);
    info->second_read=1;
    info->filepos=filepos+11;
    return return_val;
  case 12:
    info->data_len=info->block_len=mi_uint3korr(header+1);
    info->next_filepos=mi_sizekorr(header+4);
    info->second_read=1;
    info->filepos=filepos+12;
    return return_val;
  }
2002 2003 2004 2005

err:
  my_errno=HA_ERR_WRONG_IN_RECORD;	 /* Garbage */
  return BLOCK_ERROR;
unknown's avatar
unknown committed
2006
}