From b602127bf0225b7a33b44c1d7c7e1f06f8f4dee7 Mon Sep 17 00:00:00 2001
From: Sergey Vojtovich <svoj@sun.com>
Date: Mon, 22 Mar 2010 16:30:27 +0400
Subject: [PATCH] BUG#51868 - crash with myisam_use_mmap and partitioned       
      myisam tables

Queries following TRUNCATE of partitioned MyISAM table
may crash server if myisam_use_mmap is true.

Internally this is MyISAM bug, but limited to partitioned
tables, because MyISAM doesn't use ::delete_all_rows()
method for TRUNCATE, but goes via table recreate instead.

MyISAM didn't properly fall back to non-mmaped I/O after
mmap() failure. Was not repeatable on linux before, likely
because (quote from man mmap):
  SUSv3  specifies  that  mmap() should fail if length is 0.
  However, in kernels before 2.6.12, mmap() succeeded in
  this case: no mapping was created and the call returned
  addr. Since kernel 2.6.12, mmap() fails with the error
  EINVAL for this case.

mysql-test/r/partition.result:
  A test case for BUG#51868.
mysql-test/t/partition.test:
  A test case for BUG#51868.
storage/myisam/mi_delete_all.c:
  _mi_unmap_file() is compressed record format specific,
  which is read-only. As compressed MyISAM data files are
  read-only, we must never use _mi_unmap_file() in
  mi_delete_all_rows().
storage/myisam/mi_dynrec.c:
  Make myisam mmap code more durable to errors:
  - set file_read/file_write handlers if mmap succeeded;
  - reset file_read/file_write handlers on unmap.
storage/myisam/mi_extra.c:
  Moved file_read/file_write handlers initialization to
  mi_dynmap_file().
storage/myisam/myisamdef.h:
  Added mi_munmap_file() declaration.
---
 mysql-test/r/partition.result  | 11 +++++++++++
 mysql-test/t/partition.test    | 13 +++++++++++++
 storage/myisam/mi_delete_all.c |  2 +-
 storage/myisam/mi_dynrec.c     | 31 +++++++++++++++++++++++++++++--
 storage/myisam/mi_extra.c      |  5 -----
 storage/myisam/myisamdef.h     |  1 +
 6 files changed, 55 insertions(+), 8 deletions(-)

diff --git a/mysql-test/r/partition.result b/mysql-test/r/partition.result
index 08357795046..23a485dc5ed 100644
--- a/mysql-test/r/partition.result
+++ b/mysql-test/r/partition.result
@@ -2088,4 +2088,15 @@ SELECT  1 FROM t1 JOIN t1 AS t2 USING (a) FOR UPDATE;
 1
 1
 DROP TABLE t1;
+#
+# BUG#51868 - crash with myisam_use_mmap and partitioned myisam tables
+#
+SET GLOBAL myisam_use_mmap=1;
+CREATE TABLE t1(a INT) PARTITION BY HASH(a) PARTITIONS 1;
+INSERT INTO t1 VALUES(0);
+FLUSH TABLE t1;
+TRUNCATE TABLE t1;
+INSERT INTO t1 VALUES(0);
+DROP TABLE t1;
+SET GLOBAL myisam_use_mmap=default;
 End of 5.1 tests
diff --git a/mysql-test/t/partition.test b/mysql-test/t/partition.test
index 0cbe389dadd..22124cc1847 100644
--- a/mysql-test/t/partition.test
+++ b/mysql-test/t/partition.test
@@ -2081,4 +2081,17 @@ INSERT INTO t1 VALUES (6,8,10);
 SELECT  1 FROM t1 JOIN t1 AS t2 USING (a) FOR UPDATE;
 
 DROP TABLE t1;
+
+--echo #
+--echo # BUG#51868 - crash with myisam_use_mmap and partitioned myisam tables
+--echo #
+SET GLOBAL myisam_use_mmap=1;
+CREATE TABLE t1(a INT) PARTITION BY HASH(a) PARTITIONS 1;
+INSERT INTO t1 VALUES(0);
+FLUSH TABLE t1;
+TRUNCATE TABLE t1;
+INSERT INTO t1 VALUES(0);
+DROP TABLE t1;
+SET GLOBAL myisam_use_mmap=default;
+
 --echo End of 5.1 tests
diff --git a/storage/myisam/mi_delete_all.c b/storage/myisam/mi_delete_all.c
index e2bbb04ab3c..7c3ed178c4c 100644
--- a/storage/myisam/mi_delete_all.c
+++ b/storage/myisam/mi_delete_all.c
@@ -55,7 +55,7 @@ int mi_delete_all_rows(MI_INFO *info)
   flush_key_blocks(share->key_cache, share->kfile, FLUSH_IGNORE_CHANGED);
 #ifdef HAVE_MMAP
   if (share->file_map)
-    _mi_unmap_file(info);
+    mi_munmap_file(info);
 #endif
   if (my_chsize(info->dfile, 0, 0, MYF(MY_WME)) ||
       my_chsize(share->kfile, share->base.keystart, 0, MYF(MY_WME))  )
diff --git a/storage/myisam/mi_dynrec.c b/storage/myisam/mi_dynrec.c
index 6518d874f4f..09152f8d013 100644
--- a/storage/myisam/mi_dynrec.c
+++ b/storage/myisam/mi_dynrec.c
@@ -94,6 +94,34 @@ my_bool mi_dynmap_file(MI_INFO *info, my_off_t size)
   madvise((char*) info->s->file_map, size, MADV_RANDOM);
 #endif
   info->s->mmaped_length= size;
+  info->s->file_read= mi_mmap_pread;
+  info->s->file_write= mi_mmap_pwrite;
+  DBUG_RETURN(0);
+}
+
+
+/*
+  Destroy mmaped area for MyISAM handler
+
+  SYNOPSIS
+    mi_munmap_file()
+    info                  MyISAM handler
+
+  RETURN
+    0  ok
+   !0  error.
+*/
+
+int mi_munmap_file(MI_INFO *info)
+{
+  int ret;
+  DBUG_ENTER("mi_unmap_file");
+  if ((ret= my_munmap(info->s->file_map, info->s->mmaped_length)))
+    DBUG_RETURN(ret);
+  info->s->file_read= mi_nommap_pread;
+  info->s->file_write= mi_nommap_pwrite;
+  info->s->file_map= 0;
+  info->s->mmaped_length= 0;
   DBUG_RETURN(0);
 }
 
@@ -112,8 +140,7 @@ void mi_remap_file(MI_INFO *info, my_off_t size)
 {
   if (info->s->file_map)
   {
-    VOID(my_munmap((char*) info->s->file_map,
-                   (size_t) info->s->mmaped_length));
+    mi_munmap_file(info);
     mi_dynmap_file(info, size);
   }
 }
diff --git a/storage/myisam/mi_extra.c b/storage/myisam/mi_extra.c
index f0ddc15b325..d861b2af6cf 100644
--- a/storage/myisam/mi_extra.c
+++ b/storage/myisam/mi_extra.c
@@ -364,11 +364,6 @@ int mi_extra(MI_INFO *info, enum ha_extra_function function, void *extra_arg)
         DBUG_PRINT("warning",("mmap failed: errno: %d",errno));
         error= my_errno= errno;
       }
-      else
-      {
-        share->file_read= mi_mmap_pread;
-        share->file_write= mi_mmap_pwrite;
-      }
     }
     pthread_mutex_unlock(&share->intern_lock);
 #endif
diff --git a/storage/myisam/myisamdef.h b/storage/myisam/myisamdef.h
index 1aa900061f3..962155e884c 100644
--- a/storage/myisam/myisamdef.h
+++ b/storage/myisam/myisamdef.h
@@ -762,6 +762,7 @@ int mi_open_datafile(MI_INFO *info, MYISAM_SHARE *share, const char *orn_name,
 int mi_open_keyfile(MYISAM_SHARE *share);
 void mi_setup_functions(register MYISAM_SHARE *share);
 my_bool mi_dynmap_file(MI_INFO *info, my_off_t size);
+int mi_munmap_file(MI_INFO *info);
 void mi_remap_file(MI_INFO *info, my_off_t size);
 
     /* Functions needed by mi_check */