You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

237 lines
8.4 KiB

  1. /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
  2. // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
  3. #ident "$Id$"
  4. /*
  5. COPYING CONDITIONS NOTICE:
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of version 2 of the GNU General Public License as
  8. published by the Free Software Foundation, and provided that the
  9. following conditions are met:
  10. * Redistributions of source code must retain this COPYING
  11. CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
  12. DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
  13. PATENT MARKING NOTICE (below), and the PATENT RIGHTS
  14. GRANT (below).
  15. * Redistributions in binary form must reproduce this COPYING
  16. CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
  17. DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
  18. PATENT MARKING NOTICE (below), and the PATENT RIGHTS
  19. GRANT (below) in the documentation and/or other materials
  20. provided with the distribution.
  21. You should have received a copy of the GNU General Public License
  22. along with this program; if not, write to the Free Software
  23. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  24. 02110-1301, USA.
  25. COPYRIGHT NOTICE:
  26. TokuDB, Tokutek Fractal Tree Indexing Library.
  27. Copyright (C) 2007-2013 Tokutek, Inc.
  28. DISCLAIMER:
  29. This program is distributed in the hope that it will be useful, but
  30. WITHOUT ANY WARRANTY; without even the implied warranty of
  31. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  32. General Public License for more details.
  33. UNIVERSITY PATENT NOTICE:
  34. The technology is licensed by the Massachusetts Institute of
  35. Technology, Rutgers State University of New Jersey, and the Research
  36. Foundation of State University of New York at Stony Brook under
  37. United States of America Serial No. 11/760379 and to the patents
  38. and/or patent applications resulting from it.
  39. PATENT MARKING NOTICE:
  40. This software is covered by US Patent No. 8,185,551.
  41. PATENT RIGHTS GRANT:
  42. "THIS IMPLEMENTATION" means the copyrightable works distributed by
  43. Tokutek as part of the Fractal Tree project.
  44. "PATENT CLAIMS" means the claims of patents that are owned or
  45. licensable by Tokutek, both currently or in the future; and that in
  46. the absence of this license would be infringed by THIS
  47. IMPLEMENTATION or by using or running THIS IMPLEMENTATION.
  48. "PATENT CHALLENGE" shall mean a challenge to the validity,
  49. patentability, enforceability and/or non-infringement of any of the
  50. PATENT CLAIMS or otherwise opposing any of the PATENT CLAIMS.
  51. Tokutek hereby grants to you, for the term and geographical scope of
  52. the PATENT CLAIMS, a non-exclusive, no-charge, royalty-free,
  53. irrevocable (except as stated in this section) patent license to
  54. make, have made, use, offer to sell, sell, import, transfer, and
  55. otherwise run, modify, and propagate the contents of THIS
  56. IMPLEMENTATION, where such license applies only to the PATENT
  57. CLAIMS. This grant does not include claims that would be infringed
  58. only as a consequence of further modifications of THIS
  59. IMPLEMENTATION. If you or your agent or licensee institute or order
  60. or agree to the institution of patent litigation against any entity
  61. (including a cross-claim or counterclaim in a lawsuit) alleging that
  62. THIS IMPLEMENTATION constitutes direct or contributory patent
  63. infringement, or inducement of patent infringement, then any rights
  64. granted to you under this License shall terminate as of the date
  65. such litigation is filed. If you or your agent or exclusive
  66. licensee institute or order or agree to the institution of a PATENT
  67. CHALLENGE, then Tokutek may terminate any rights granted to you
  68. under this License.
  69. */
  70. #ident "Copyright (c) 2007-2013 Tokutek Inc. All rights reserved."
  71. #ident "The technology is licensed by the Massachusetts Institute of Technology, Rutgers State University of New Jersey, and the Research Foundation of State University of New York at Stony Brook under United States of America Serial No. 11/760379 and to the patents and/or patent applications resulting from it."
  72. #include <ft-internal.h>
  73. #include <db.h>
  74. /*
  75. * ft-node-deserialize.c -
  76. * This file contains functions used by deserializtion
  77. * code paths in and out of the engine. The functions can,
  78. * essentially, be broken up into two types. Some of these
  79. * functions return error codes based expected values inside
  80. * the fractal tree node, others merely read the specific
  81. * quantities of bytes out of the buffer. It is expeceted
  82. * that these will be called in the correct order by users
  83. * of these functions/this API.
  84. *
  85. */
  86. // Sets initial values for the given fractal tree node to be
  87. // deserialized
  88. void
  89. initialize_ftnode(FTNODE node, BLOCKNUM blocknum)
  90. {
  91. node->fullhash = 0xDEADBEEF; // <CER> Is this 'spoof' ok?
  92. node->thisnodename = blocknum;
  93. node->dirty = 0;
  94. node->bp = NULL;
  95. // <CER> Can we use this initialization as a correctness assert in
  96. // a later function?
  97. node->layout_version_read_from_disk = 0;
  98. }
  99. /************************
  100. * TODO: In other deserialization code, we check the rb size member. We
  101. * verify that it is greater than or equal to 24. Ignoring this magic
  102. * number for a moment, should we put this check in its own function? *
  103. *************************/
  104. // Read and check the 'magic' bytes on disk. Returns an error if
  105. // the magic does not match.
  106. int
  107. read_and_check_magic(struct rbuf *rb)
  108. {
  109. int r = 0;
  110. bytevec magic;
  111. rbuf_literal_bytes(rb, &magic, 8);
  112. if (memcmp(magic, "tokuleaf", 8)!=0 &&
  113. memcmp(magic, "tokunode", 8)!=0) {
  114. r = DB_BADFORMAT; // TODO: Return more meaningful error.
  115. }
  116. return r;
  117. }
  118. // Read the version number from the given buffer
  119. // and returns an error if the version is too old.
  120. int
  121. read_and_check_version(FTNODE node, struct rbuf *rb)
  122. {
  123. int r = 0;
  124. int version = rbuf_int(rb);
  125. node->layout_version_read_from_disk = version;
  126. if (version < FT_LAYOUT_MIN_SUPPORTED_VERSION) {
  127. r = 1; // TODO: Better error reporting.
  128. }
  129. return r;
  130. }
  131. // Reads the basic version, build, and child info from
  132. // the given buffer.
  133. void
  134. read_node_info(FTNODE node, struct rbuf *rb, int version)
  135. {
  136. node->layout_version = version;
  137. node->layout_version_original = rbuf_int(rb);
  138. node->build_id = rbuf_int(rb);
  139. node->n_children = rbuf_int(rb);
  140. }
  141. // Allocates the partitions based on the given node's nubmer
  142. // of children. It then reads, out of the given buffer,
  143. // the start and size of each child partition.
  144. // TODO: Should these be two seperate functions?
  145. void
  146. allocate_and_read_partition_offsets(FTNODE node, struct rbuf *rb, FTNODE_DISK_DATA *ndd)
  147. {
  148. XMALLOC_N(node->n_children, node->bp);
  149. // TODO: Fix this to use xmalloc_n
  150. XMALLOC_N(node->n_children, *ndd);
  151. // Read the partition locations.
  152. for (int i = 0; i < node->n_children; i++) {
  153. BP_START(*ndd, i) = rbuf_int(rb);
  154. BP_SIZE (*ndd, i) = rbuf_int(rb);
  155. }
  156. }
  157. // Compares checksum of stored (in the given buffer) checksum
  158. // and the checksum of the buffer itself. If these are NOT
  159. // equal, this function returns an appropriate error code.
  160. int
  161. check_node_info_checksum(struct rbuf *rb)
  162. {
  163. int r = 0;
  164. // Verify checksum of header stored.
  165. uint32_t checksum = x1764_memory(rb->buf, rb->ndone);
  166. uint32_t stored_checksum = rbuf_int(rb);
  167. if (stored_checksum != checksum) {
  168. // TODO: dump_bad_block(rb->buf, rb->size);
  169. r = TOKUDB_BAD_CHECKSUM;
  170. }
  171. return r;
  172. }
  173. // Reads node info from older (13 and 14) fractal tree nodes
  174. // out of the given buffer.
  175. void
  176. read_legacy_node_info(FTNODE node, struct rbuf *rb, int version)
  177. {
  178. (void)rbuf_int(rb); // 1. nodesize
  179. node->flags = rbuf_int(rb); // 2. flags
  180. node->height = rbuf_int(rb); // 3. height
  181. // If the version is less than 14, there are two extra ints here.
  182. // we would need to ignore them if they are there.
  183. if (version == FT_LAYOUT_VERSION_13) {
  184. (void) rbuf_int(rb); // 4. rand4
  185. (void) rbuf_int(rb); // 5. local
  186. }
  187. }
  188. // Assuming the given buffer is in the correct position,
  189. // this checks to see if the stored checksum matches the
  190. // checksum of the entire buffer.
  191. int
  192. check_legacy_end_checksum(struct rbuf *rb)
  193. {
  194. int r = 0;
  195. uint32_t expected_xsum = rbuf_int(rb);
  196. uint32_t actual_xsum = x1764_memory(rb->buf, rb->size - 4);
  197. if (expected_xsum != actual_xsum) {
  198. r = TOKUDB_BAD_CHECKSUM;
  199. }
  200. return r;
  201. }