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.

856 lines
33 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 "compress.h"
  73. #include "ft.h"
  74. #include "ft-internal.h"
  75. // not version-sensitive because we only serialize a descriptor using the current layout_version
  76. uint32_t
  77. toku_serialize_descriptor_size(const DESCRIPTOR desc) {
  78. //Checksum NOT included in this. Checksum only exists in header's version.
  79. uint32_t size = 4; // four bytes for size of descriptor
  80. size += desc->dbt.size;
  81. return size;
  82. }
  83. static uint32_t
  84. deserialize_descriptor_size(const DESCRIPTOR desc, int layout_version) {
  85. //Checksum NOT included in this. Checksum only exists in header's version.
  86. uint32_t size = 4; // four bytes for size of descriptor
  87. if (layout_version == FT_LAYOUT_VERSION_13)
  88. size += 4; // for version 13, include four bytes of "version"
  89. size += desc->dbt.size;
  90. return size;
  91. }
  92. void
  93. toku_serialize_descriptor_contents_to_wbuf(struct wbuf *wb, const DESCRIPTOR desc) {
  94. wbuf_bytes(wb, desc->dbt.data, desc->dbt.size);
  95. }
  96. //Descriptor is written to disk during toku_ft_handle_open iff we have a new (or changed)
  97. //descriptor.
  98. //Descriptors are NOT written during the header checkpoint process.
  99. void
  100. toku_serialize_descriptor_contents_to_fd(int fd, const DESCRIPTOR desc, DISKOFF offset) {
  101. // make the checksum
  102. int64_t size = toku_serialize_descriptor_size(desc)+4; //4 for checksum
  103. int64_t size_aligned = roundup_to_multiple(512, size);
  104. struct wbuf w;
  105. char *XMALLOC_N_ALIGNED(512, size_aligned, aligned_buf);
  106. for (int64_t i=size; i<size_aligned; i++) aligned_buf[i] = 0;
  107. wbuf_init(&w, aligned_buf, size);
  108. toku_serialize_descriptor_contents_to_wbuf(&w, desc);
  109. {
  110. //Add checksum
  111. uint32_t checksum = x1764_finish(&w.checksum);
  112. wbuf_int(&w, checksum);
  113. }
  114. lazy_assert(w.ndone==w.size);
  115. {
  116. //Actual Write translation table
  117. toku_os_full_pwrite(fd, w.buf, size_aligned, offset);
  118. }
  119. toku_free(w.buf);
  120. }
  121. static void
  122. deserialize_descriptor_from_rbuf(struct rbuf *rb, DESCRIPTOR desc, int layout_version) {
  123. if (layout_version <= FT_LAYOUT_VERSION_13) {
  124. // in older versions of TokuDB the Descriptor had a 4 byte
  125. // version, which we skip over
  126. (void) rbuf_int(rb);
  127. }
  128. uint32_t size;
  129. bytevec data;
  130. rbuf_bytes(rb, &data, &size);
  131. bytevec data_copy = data;
  132. if (size > 0) {
  133. data_copy = toku_memdup(data, size); //Cannot keep the reference from rbuf. Must copy.
  134. lazy_assert(data_copy);
  135. } else {
  136. lazy_assert(size==0);
  137. data_copy = NULL;
  138. }
  139. toku_fill_dbt(&desc->dbt, data_copy, size);
  140. }
  141. static int
  142. deserialize_descriptor_from(int fd, BLOCK_TABLE bt, DESCRIPTOR desc, int layout_version) {
  143. int r = 0;
  144. DISKOFF offset;
  145. DISKOFF size;
  146. unsigned char *dbuf = NULL;
  147. toku_get_descriptor_offset_size(bt, &offset, &size);
  148. memset(desc, 0, sizeof(*desc));
  149. if (size > 0) {
  150. lazy_assert(size>=4); //4 for checksum
  151. {
  152. ssize_t size_to_malloc = roundup_to_multiple(512, size);
  153. XMALLOC_N_ALIGNED(512, size_to_malloc, dbuf);
  154. {
  155. ssize_t sz_read = toku_os_pread(fd, dbuf, size_to_malloc, offset);
  156. lazy_assert(sz_read==size_to_malloc);
  157. }
  158. {
  159. // check the checksum
  160. uint32_t x1764 = x1764_memory(dbuf, size-4);
  161. //printf("%s:%d read from %ld (x1764 offset=%ld) size=%ld\n", __FILE__, __LINE__, block_translation_address_on_disk, offset, block_translation_size_on_disk);
  162. uint32_t stored_x1764 = toku_dtoh32(*(int*)(dbuf + size-4));
  163. if (x1764 != stored_x1764) {
  164. fprintf(stderr, "Descriptor checksum failure: calc=0x%08x read=0x%08x\n", x1764, stored_x1764);
  165. r = TOKUDB_BAD_CHECKSUM;
  166. toku_free(dbuf);
  167. goto exit;
  168. }
  169. }
  170. {
  171. struct rbuf rb = {.buf = dbuf, .size = (unsigned int) size, .ndone = 0};
  172. //Not temporary; must have a toku_memdup'd copy.
  173. deserialize_descriptor_from_rbuf(&rb, desc, layout_version);
  174. }
  175. lazy_assert(deserialize_descriptor_size(desc, layout_version)+4 == size);
  176. toku_free(dbuf);
  177. }
  178. }
  179. exit:
  180. return r;
  181. }
  182. int deserialize_ft_versioned(int fd, struct rbuf *rb, FT *ftp, uint32_t version)
  183. // Effect: Deserialize the ft header.
  184. // We deserialize brt header only once and then share everything with all the brts.
  185. {
  186. int r;
  187. FT ft = NULL;
  188. paranoid_invariant(version >= FT_LAYOUT_MIN_SUPPORTED_VERSION);
  189. paranoid_invariant(version <= FT_LAYOUT_VERSION);
  190. // We already know:
  191. // we have an rbuf representing the header.
  192. // The checksum has been validated
  193. //Verification of initial elements.
  194. //Check magic number
  195. bytevec magic;
  196. rbuf_literal_bytes(rb, &magic, 8);
  197. lazy_assert(memcmp(magic,"tokudata",8)==0);
  198. XCALLOC(ft);
  199. ft->checkpoint_header = NULL;
  200. toku_list_init(&ft->live_ft_handles);
  201. //version MUST be in network order on disk regardless of disk order
  202. ft->layout_version_read_from_disk = rbuf_network_int(rb);
  203. invariant(ft->layout_version_read_from_disk >= FT_LAYOUT_MIN_SUPPORTED_VERSION);
  204. invariant(ft->layout_version_read_from_disk <= FT_LAYOUT_VERSION);
  205. //build_id MUST be in network order on disk regardless of disk order
  206. uint32_t build_id;
  207. build_id = rbuf_network_int(rb);
  208. //Size MUST be in network order regardless of disk order.
  209. uint32_t size;
  210. size = rbuf_network_int(rb);
  211. lazy_assert(size == rb->size);
  212. bytevec tmp_byte_order_check;
  213. lazy_assert((sizeof tmp_byte_order_check) >= 8);
  214. rbuf_literal_bytes(rb, &tmp_byte_order_check, 8); //Must not translate byte order
  215. int64_t byte_order_stored;
  216. byte_order_stored = *(int64_t*)tmp_byte_order_check;
  217. lazy_assert(byte_order_stored == toku_byte_order_host);
  218. uint64_t checkpoint_count;
  219. checkpoint_count = rbuf_ulonglong(rb);
  220. LSN checkpoint_lsn;
  221. checkpoint_lsn = rbuf_lsn(rb);
  222. unsigned nodesize;
  223. nodesize = rbuf_int(rb);
  224. DISKOFF translation_address_on_disk;
  225. translation_address_on_disk = rbuf_diskoff(rb);
  226. DISKOFF translation_size_on_disk;
  227. translation_size_on_disk = rbuf_diskoff(rb);
  228. lazy_assert(translation_address_on_disk > 0);
  229. lazy_assert(translation_size_on_disk > 0);
  230. // initialize the tree lock
  231. toku_ft_init_reflock(ft);
  232. //Load translation table
  233. {
  234. size_t size_to_read = roundup_to_multiple(512, translation_size_on_disk);
  235. unsigned char *XMALLOC_N_ALIGNED(512, size_to_read, tbuf);
  236. {
  237. // This cast is messed up in 32-bits if the block translation
  238. // table is ever more than 4GB. But in that case, the
  239. // translation table itself won't fit in main memory.
  240. ssize_t readsz = toku_os_pread(fd, tbuf, size_to_read,
  241. translation_address_on_disk);
  242. assert(readsz >= translation_size_on_disk);
  243. assert(readsz <= (ssize_t)size_to_read);
  244. }
  245. // Create table and read in data.
  246. r = toku_blocktable_create_from_buffer(fd,
  247. &ft->blocktable,
  248. translation_address_on_disk,
  249. translation_size_on_disk,
  250. tbuf);
  251. toku_free(tbuf);
  252. if (r != 0) {
  253. goto exit;
  254. }
  255. }
  256. BLOCKNUM root_blocknum;
  257. root_blocknum = rbuf_blocknum(rb);
  258. unsigned flags;
  259. flags = rbuf_int(rb);
  260. if (ft->layout_version_read_from_disk <= FT_LAYOUT_VERSION_13) {
  261. // deprecate 'TOKU_DB_VALCMP_BUILTIN'. just remove the flag
  262. flags &= ~TOKU_DB_VALCMP_BUILTIN_13;
  263. }
  264. int layout_version_original;
  265. layout_version_original = rbuf_int(rb);
  266. uint32_t build_id_original;
  267. build_id_original = rbuf_int(rb);
  268. uint64_t time_of_creation;
  269. time_of_creation = rbuf_ulonglong(rb);
  270. uint64_t time_of_last_modification;
  271. time_of_last_modification = rbuf_ulonglong(rb);
  272. if (ft->layout_version_read_from_disk <= FT_LAYOUT_VERSION_18) {
  273. // 17 was the last version with these fields, we no longer store
  274. // them, so read and discard them
  275. (void) rbuf_ulonglong(rb); // num_blocks_to_upgrade_13
  276. if (ft->layout_version_read_from_disk >= FT_LAYOUT_VERSION_15) {
  277. (void) rbuf_ulonglong(rb); // num_blocks_to_upgrade_14
  278. }
  279. }
  280. // fake creation during the last checkpoint
  281. TXNID root_xid_that_created;
  282. root_xid_that_created = checkpoint_lsn.lsn;
  283. if (ft->layout_version_read_from_disk >= FT_LAYOUT_VERSION_14) {
  284. rbuf_TXNID(rb, &root_xid_that_created);
  285. }
  286. // TODO(leif): get this to default to what's specified, not the
  287. // hard-coded default
  288. unsigned basementnodesize;
  289. basementnodesize = FT_DEFAULT_BASEMENT_NODE_SIZE;
  290. uint64_t time_of_last_verification;
  291. time_of_last_verification = 0;
  292. if (ft->layout_version_read_from_disk >= FT_LAYOUT_VERSION_15) {
  293. basementnodesize = rbuf_int(rb);
  294. time_of_last_verification = rbuf_ulonglong(rb);
  295. }
  296. STAT64INFO_S on_disk_stats;
  297. on_disk_stats = ZEROSTATS;
  298. uint64_t time_of_last_optimize_begin;
  299. time_of_last_optimize_begin = 0;
  300. uint64_t time_of_last_optimize_end;
  301. time_of_last_optimize_end = 0;
  302. uint32_t count_of_optimize_in_progress;
  303. count_of_optimize_in_progress = 0;
  304. MSN msn_at_start_of_last_completed_optimize;
  305. msn_at_start_of_last_completed_optimize = ZERO_MSN;
  306. if (ft->layout_version_read_from_disk >= FT_LAYOUT_VERSION_18) {
  307. on_disk_stats.numrows = rbuf_ulonglong(rb);
  308. on_disk_stats.numbytes = rbuf_ulonglong(rb);
  309. ft->in_memory_stats = on_disk_stats;
  310. time_of_last_optimize_begin = rbuf_ulonglong(rb);
  311. time_of_last_optimize_end = rbuf_ulonglong(rb);
  312. count_of_optimize_in_progress = rbuf_int(rb);
  313. msn_at_start_of_last_completed_optimize = rbuf_msn(rb);
  314. }
  315. enum toku_compression_method compression_method;
  316. MSN highest_unused_msn_for_upgrade;
  317. highest_unused_msn_for_upgrade.msn = (MIN_MSN.msn - 1);
  318. if (ft->layout_version_read_from_disk >= FT_LAYOUT_VERSION_19) {
  319. unsigned char method = rbuf_char(rb);
  320. compression_method = (enum toku_compression_method) method;
  321. highest_unused_msn_for_upgrade = rbuf_msn(rb);
  322. } else {
  323. // we hard coded zlib until 5.2, then quicklz in 5.2
  324. if (ft->layout_version_read_from_disk < FT_LAYOUT_VERSION_18) {
  325. compression_method = TOKU_ZLIB_METHOD;
  326. } else {
  327. compression_method = TOKU_QUICKLZ_METHOD;
  328. }
  329. }
  330. MSN max_msn_in_ft;
  331. max_msn_in_ft = ZERO_MSN; // We'll upgrade it from the root node later if necessary
  332. if (ft->layout_version_read_from_disk >= FT_LAYOUT_VERSION_21) {
  333. max_msn_in_ft = rbuf_msn(rb);
  334. }
  335. (void) rbuf_int(rb); //Read in checksum and ignore (already verified).
  336. if (rb->ndone != rb->size) {
  337. fprintf(stderr, "Header size did not match contents.\n");
  338. r = EINVAL;
  339. goto exit;
  340. }
  341. {
  342. struct ft_header h = {
  343. .type = FT_CURRENT,
  344. .dirty = 0,
  345. .checkpoint_count = checkpoint_count,
  346. .checkpoint_lsn = checkpoint_lsn,
  347. .layout_version = FT_LAYOUT_VERSION,
  348. .layout_version_original = layout_version_original,
  349. .build_id = build_id,
  350. .build_id_original = build_id_original,
  351. .time_of_creation = time_of_creation,
  352. .root_xid_that_created = root_xid_that_created,
  353. .time_of_last_modification = time_of_last_modification,
  354. .time_of_last_verification = time_of_last_verification,
  355. .root_blocknum = root_blocknum,
  356. .flags = flags,
  357. .nodesize = nodesize,
  358. .basementnodesize = basementnodesize,
  359. .compression_method = compression_method,
  360. .highest_unused_msn_for_upgrade = highest_unused_msn_for_upgrade,
  361. .max_msn_in_ft = max_msn_in_ft,
  362. .time_of_last_optimize_begin = time_of_last_optimize_begin,
  363. .time_of_last_optimize_end = time_of_last_optimize_end,
  364. .count_of_optimize_in_progress = count_of_optimize_in_progress,
  365. .count_of_optimize_in_progress_read_from_disk = count_of_optimize_in_progress,
  366. .msn_at_start_of_last_completed_optimize = msn_at_start_of_last_completed_optimize,
  367. .on_disk_stats = on_disk_stats
  368. };
  369. XMEMDUP(ft->h, &h);
  370. }
  371. if (ft->layout_version_read_from_disk < FT_LAYOUT_VERSION_18) {
  372. // This needs ft->h to be non-null, so we have to do it after we
  373. // read everything else.
  374. r = toku_upgrade_subtree_estimates_to_stat64info(fd, ft);
  375. if (r != 0) {
  376. goto exit;
  377. }
  378. }
  379. if (ft->layout_version_read_from_disk < FT_LAYOUT_VERSION_21) {
  380. r = toku_upgrade_msn_from_root_to_header(fd, ft);
  381. if (r != 0) {
  382. goto exit;
  383. }
  384. }
  385. invariant((uint32_t) ft->layout_version_read_from_disk == version);
  386. r = deserialize_descriptor_from(fd, ft->blocktable, &ft->descriptor, version);
  387. if (r != 0) {
  388. goto exit;
  389. }
  390. // copy descriptor to cmp_descriptor for #4541
  391. ft->cmp_descriptor.dbt.size = ft->descriptor.dbt.size;
  392. ft->cmp_descriptor.dbt.data = toku_xmemdup(ft->descriptor.dbt.data, ft->descriptor.dbt.size);
  393. // Version 13 descriptors had an extra 4 bytes that we don't read
  394. // anymore. Since the header is going to think it's the current
  395. // version if it gets written out, we need to write the descriptor in
  396. // the new format (without those bytes) before that happens.
  397. if (version <= FT_LAYOUT_VERSION_13) {
  398. toku_ft_update_descriptor_with_fd(ft, &ft->cmp_descriptor, fd);
  399. }
  400. r = 0;
  401. exit:
  402. if (r != 0 && ft != NULL) {
  403. toku_free(ft);
  404. ft = NULL;
  405. }
  406. *ftp = ft;
  407. return r;
  408. }
  409. static size_t
  410. serialize_ft_min_size (uint32_t version) {
  411. size_t size = 0;
  412. switch(version) {
  413. case FT_LAYOUT_VERSION_24:
  414. case FT_LAYOUT_VERSION_23:
  415. case FT_LAYOUT_VERSION_22:
  416. case FT_LAYOUT_VERSION_21:
  417. size += sizeof(MSN); // max_msn_in_ft
  418. case FT_LAYOUT_VERSION_20:
  419. case FT_LAYOUT_VERSION_19:
  420. size += 1; // compression method
  421. size += sizeof(MSN); // highest_unused_msn_for_upgrade
  422. case FT_LAYOUT_VERSION_18:
  423. size += sizeof(uint64_t); // time_of_last_optimize_begin
  424. size += sizeof(uint64_t); // time_of_last_optimize_end
  425. size += sizeof(uint32_t); // count_of_optimize_in_progress
  426. size += sizeof(MSN); // msn_at_start_of_last_completed_optimize
  427. size -= 8; // removed num_blocks_to_upgrade_14
  428. size -= 8; // removed num_blocks_to_upgrade_13
  429. case FT_LAYOUT_VERSION_17:
  430. size += 16;
  431. invariant(sizeof(STAT64INFO_S) == 16);
  432. case FT_LAYOUT_VERSION_16:
  433. case FT_LAYOUT_VERSION_15:
  434. size += 4; // basement node size
  435. size += 8; // num_blocks_to_upgrade_14 (previously num_blocks_to_upgrade, now one int each for upgrade from 13, 14
  436. size += 8; // time of last verification
  437. case FT_LAYOUT_VERSION_14:
  438. size += 8; //TXNID that created
  439. case FT_LAYOUT_VERSION_13:
  440. size += ( 4 // build_id
  441. +4 // build_id_original
  442. +8 // time_of_creation
  443. +8 // time_of_last_modification
  444. );
  445. // fall through
  446. case FT_LAYOUT_VERSION_12:
  447. size += (+8 // "tokudata"
  448. +4 // version
  449. +4 // original_version
  450. +4 // size
  451. +8 // byte order verification
  452. +8 // checkpoint_count
  453. +8 // checkpoint_lsn
  454. +4 // tree's nodesize
  455. +8 // translation_size_on_disk
  456. +8 // translation_address_on_disk
  457. +4 // checksum
  458. +8 // Number of blocks in old version.
  459. +8 // diskoff
  460. +4 // flags
  461. );
  462. break;
  463. default:
  464. abort();
  465. }
  466. lazy_assert(size <= BLOCK_ALLOCATOR_HEADER_RESERVE);
  467. return size;
  468. }
  469. int deserialize_ft_from_fd_into_rbuf(int fd,
  470. toku_off_t offset_of_header,
  471. struct rbuf *rb,
  472. uint64_t *checkpoint_count,
  473. LSN *checkpoint_lsn,
  474. uint32_t * version_p)
  475. // Effect: Read and parse the header of a fractalal tree
  476. //
  477. // Simply reading the raw bytes of the header into an rbuf is insensitive
  478. // to disk format version. If that ever changes, then modify this.
  479. //
  480. // TOKUDB_DICTIONARY_NO_HEADER means we can overwrite everything in the
  481. // file AND the header is useless
  482. {
  483. int r = 0;
  484. const int64_t prefix_size = 8 + // magic ("tokudata")
  485. 4 + // version
  486. 4 + // build_id
  487. 4; // size
  488. const int64_t read_size = roundup_to_multiple(512, prefix_size);
  489. unsigned char *XMALLOC_N_ALIGNED(512, read_size, prefix);
  490. rb->buf = NULL;
  491. int64_t n = toku_os_pread(fd, prefix, read_size, offset_of_header);
  492. if (n != read_size) {
  493. if (n==0) {
  494. r = TOKUDB_DICTIONARY_NO_HEADER;
  495. } else if (n<0) {
  496. r = get_error_errno();
  497. } else {
  498. r = EINVAL;
  499. }
  500. toku_free(prefix);
  501. goto exit;
  502. }
  503. rbuf_init(rb, prefix, prefix_size);
  504. //Check magic number
  505. bytevec magic;
  506. rbuf_literal_bytes(rb, &magic, 8);
  507. if (memcmp(magic,"tokudata",8)!=0) {
  508. if ((*(uint64_t*)magic) == 0) {
  509. r = TOKUDB_DICTIONARY_NO_HEADER;
  510. } else {
  511. r = EINVAL; //Not a tokudb file! Do not use.
  512. }
  513. goto exit;
  514. }
  515. //Version MUST be in network order regardless of disk order.
  516. uint32_t version;
  517. version = rbuf_network_int(rb);
  518. *version_p = version;
  519. if (version < FT_LAYOUT_MIN_SUPPORTED_VERSION) {
  520. r = TOKUDB_DICTIONARY_TOO_OLD; //Cannot use
  521. goto exit;
  522. } else if (version > FT_LAYOUT_VERSION) {
  523. r = TOKUDB_DICTIONARY_TOO_NEW; //Cannot use
  524. goto exit;
  525. }
  526. //build_id MUST be in network order regardless of disk order.
  527. uint32_t build_id __attribute__((__unused__));
  528. build_id = rbuf_network_int(rb);
  529. int64_t min_header_size;
  530. min_header_size = serialize_ft_min_size(version);
  531. //Size MUST be in network order regardless of disk order.
  532. uint32_t size;
  533. size = rbuf_network_int(rb);
  534. //If too big, it is corrupt. We would probably notice during checksum
  535. //but may have to do a multi-gigabyte malloc+read to find out.
  536. //If its too small reading rbuf would crash, so verify.
  537. if (size > BLOCK_ALLOCATOR_HEADER_RESERVE || size < min_header_size) {
  538. r = TOKUDB_DICTIONARY_NO_HEADER;
  539. goto exit;
  540. }
  541. lazy_assert(rb->ndone==prefix_size);
  542. rb->size = size;
  543. {
  544. toku_free(rb->buf);
  545. uint32_t size_to_read = roundup_to_multiple(512, size);
  546. XMALLOC_N_ALIGNED(512, size_to_read, rb->buf);
  547. assert(offset_of_header%512==0);
  548. n = toku_os_pread(fd, rb->buf, size_to_read, offset_of_header);
  549. if (n != size_to_read) {
  550. if (n < 0) {
  551. r = get_error_errno();
  552. } else {
  553. r = EINVAL; //Header might be useless (wrong size) or could be a disk read error.
  554. }
  555. goto exit;
  556. }
  557. }
  558. //It's version 14 or later. Magic looks OK.
  559. //We have an rbuf that represents the header.
  560. //Size is within acceptable bounds.
  561. //Verify checksum (FT_LAYOUT_VERSION_13 or later, when checksum function changed)
  562. uint32_t calculated_x1764;
  563. calculated_x1764 = x1764_memory(rb->buf, rb->size-4);
  564. uint32_t stored_x1764;
  565. stored_x1764 = toku_dtoh32(*(int*)(rb->buf+rb->size-4));
  566. if (calculated_x1764 != stored_x1764) {
  567. r = TOKUDB_BAD_CHECKSUM; //Header useless
  568. fprintf(stderr, "Header checksum failure: calc=0x%08x read=0x%08x\n", calculated_x1764, stored_x1764);
  569. goto exit;
  570. }
  571. //Verify byte order
  572. bytevec tmp_byte_order_check;
  573. lazy_assert((sizeof toku_byte_order_host) == 8);
  574. rbuf_literal_bytes(rb, &tmp_byte_order_check, 8); //Must not translate byte order
  575. int64_t byte_order_stored;
  576. byte_order_stored = *(int64_t*)tmp_byte_order_check;
  577. if (byte_order_stored != toku_byte_order_host) {
  578. r = TOKUDB_DICTIONARY_NO_HEADER; //Cannot use dictionary
  579. goto exit;
  580. }
  581. //Load checkpoint count
  582. *checkpoint_count = rbuf_ulonglong(rb);
  583. *checkpoint_lsn = rbuf_lsn(rb);
  584. //Restart at beginning during regular deserialization
  585. rb->ndone = 0;
  586. exit:
  587. if (r != 0 && rb->buf != NULL) {
  588. toku_free(rb->buf);
  589. rb->buf = NULL;
  590. }
  591. return r;
  592. }
  593. // Read ft from file into struct. Read both headers and use one.
  594. // We want the latest acceptable header whose checkpoint_lsn is no later
  595. // than max_acceptable_lsn.
  596. int
  597. toku_deserialize_ft_from(int fd,
  598. LSN max_acceptable_lsn,
  599. FT *ft)
  600. {
  601. struct rbuf rb_0;
  602. struct rbuf rb_1;
  603. uint64_t checkpoint_count_0;
  604. uint64_t checkpoint_count_1;
  605. LSN checkpoint_lsn_0;
  606. LSN checkpoint_lsn_1;
  607. uint32_t version_0, version_1, version = 0;
  608. bool h0_acceptable = false;
  609. bool h1_acceptable = false;
  610. struct rbuf *rb = NULL;
  611. int r0, r1, r;
  612. toku_off_t header_0_off = 0;
  613. r0 = deserialize_ft_from_fd_into_rbuf(fd, header_0_off, &rb_0, &checkpoint_count_0, &checkpoint_lsn_0, &version_0);
  614. if (r0 == 0 && checkpoint_lsn_0.lsn <= max_acceptable_lsn.lsn) {
  615. h0_acceptable = true;
  616. }
  617. toku_off_t header_1_off = BLOCK_ALLOCATOR_HEADER_RESERVE;
  618. r1 = deserialize_ft_from_fd_into_rbuf(fd, header_1_off, &rb_1, &checkpoint_count_1, &checkpoint_lsn_1, &version_1);
  619. if (r1 == 0 && checkpoint_lsn_1.lsn <= max_acceptable_lsn.lsn) {
  620. h1_acceptable = true;
  621. }
  622. // if either header is too new, the dictionary is unreadable
  623. if (r0 == TOKUDB_DICTIONARY_TOO_NEW || r1 == TOKUDB_DICTIONARY_TOO_NEW ||
  624. !(h0_acceptable || h1_acceptable)) {
  625. // We were unable to read either header or at least one is too
  626. // new. Certain errors are higher priority than others. Order of
  627. // these if/else if is important.
  628. if (r0 == TOKUDB_DICTIONARY_TOO_NEW || r1 == TOKUDB_DICTIONARY_TOO_NEW) {
  629. r = TOKUDB_DICTIONARY_TOO_NEW;
  630. } else if (r0 == TOKUDB_DICTIONARY_TOO_OLD || r1 == TOKUDB_DICTIONARY_TOO_OLD) {
  631. r = TOKUDB_DICTIONARY_TOO_OLD;
  632. } else if (r0 == TOKUDB_BAD_CHECKSUM && r1 == TOKUDB_BAD_CHECKSUM) {
  633. fprintf(stderr, "Both header checksums failed.\n");
  634. r = TOKUDB_BAD_CHECKSUM;
  635. } else if (r0 == TOKUDB_DICTIONARY_NO_HEADER || r1 == TOKUDB_DICTIONARY_NO_HEADER) {
  636. r = TOKUDB_DICTIONARY_NO_HEADER;
  637. } else {
  638. r = r0 ? r0 : r1; //Arbitrarily report the error from the
  639. //first header, unless it's readable
  640. }
  641. // it should not be possible for both headers to be later than the max_acceptable_lsn
  642. invariant(!((r0==0 && checkpoint_lsn_0.lsn > max_acceptable_lsn.lsn) &&
  643. (r1==0 && checkpoint_lsn_1.lsn > max_acceptable_lsn.lsn)));
  644. invariant(r!=0);
  645. goto exit;
  646. }
  647. if (h0_acceptable && h1_acceptable) {
  648. if (checkpoint_count_0 > checkpoint_count_1) {
  649. invariant(checkpoint_count_0 == checkpoint_count_1 + 1);
  650. invariant(version_0 >= version_1);
  651. rb = &rb_0;
  652. version = version_0;
  653. }
  654. else {
  655. invariant(checkpoint_count_1 == checkpoint_count_0 + 1);
  656. invariant(version_1 >= version_0);
  657. rb = &rb_1;
  658. version = version_1;
  659. }
  660. } else if (h0_acceptable) {
  661. if (r1 == TOKUDB_BAD_CHECKSUM) {
  662. // print something reassuring
  663. fprintf(stderr, "Header 2 checksum failed, but header 1 ok. Proceeding.\n");
  664. }
  665. rb = &rb_0;
  666. version = version_0;
  667. } else if (h1_acceptable) {
  668. if (r0 == TOKUDB_BAD_CHECKSUM) {
  669. // print something reassuring
  670. fprintf(stderr, "Header 1 checksum failed, but header 2 ok. Proceeding.\n");
  671. }
  672. rb = &rb_1;
  673. version = version_1;
  674. }
  675. paranoid_invariant(rb);
  676. r = deserialize_ft_versioned(fd, rb, ft, version);
  677. exit:
  678. if (rb_0.buf) {
  679. toku_free(rb_0.buf);
  680. }
  681. if (rb_1.buf) {
  682. toku_free(rb_1.buf);
  683. }
  684. return r;
  685. }
  686. size_t toku_serialize_ft_size (FT_HEADER h) {
  687. size_t size = serialize_ft_min_size(h->layout_version);
  688. //There is no dynamic data.
  689. lazy_assert(size <= BLOCK_ALLOCATOR_HEADER_RESERVE);
  690. return size;
  691. }
  692. void toku_serialize_ft_to_wbuf (
  693. struct wbuf *wbuf,
  694. FT_HEADER h,
  695. DISKOFF translation_location_on_disk,
  696. DISKOFF translation_size_on_disk
  697. )
  698. {
  699. wbuf_literal_bytes(wbuf, "tokudata", 8);
  700. wbuf_network_int (wbuf, h->layout_version); //MUST be in network order regardless of disk order
  701. wbuf_network_int (wbuf, BUILD_ID); //MUST be in network order regardless of disk order
  702. wbuf_network_int (wbuf, wbuf->size); //MUST be in network order regardless of disk order
  703. wbuf_literal_bytes(wbuf, &toku_byte_order_host, 8); //Must not translate byte order
  704. wbuf_ulonglong(wbuf, h->checkpoint_count);
  705. wbuf_LSN (wbuf, h->checkpoint_lsn);
  706. wbuf_int (wbuf, h->nodesize);
  707. wbuf_DISKOFF(wbuf, translation_location_on_disk);
  708. wbuf_DISKOFF(wbuf, translation_size_on_disk);
  709. wbuf_BLOCKNUM(wbuf, h->root_blocknum);
  710. wbuf_int(wbuf, h->flags);
  711. wbuf_int(wbuf, h->layout_version_original);
  712. wbuf_int(wbuf, h->build_id_original);
  713. wbuf_ulonglong(wbuf, h->time_of_creation);
  714. wbuf_ulonglong(wbuf, h->time_of_last_modification);
  715. wbuf_TXNID(wbuf, h->root_xid_that_created);
  716. wbuf_int(wbuf, h->basementnodesize);
  717. wbuf_ulonglong(wbuf, h->time_of_last_verification);
  718. wbuf_ulonglong(wbuf, h->on_disk_stats.numrows);
  719. wbuf_ulonglong(wbuf, h->on_disk_stats.numbytes);
  720. wbuf_ulonglong(wbuf, h->time_of_last_optimize_begin);
  721. wbuf_ulonglong(wbuf, h->time_of_last_optimize_end);
  722. wbuf_int(wbuf, h->count_of_optimize_in_progress);
  723. wbuf_MSN(wbuf, h->msn_at_start_of_last_completed_optimize);
  724. wbuf_char(wbuf, (unsigned char) h->compression_method);
  725. wbuf_MSN(wbuf, h->highest_unused_msn_for_upgrade);
  726. wbuf_MSN(wbuf, h->max_msn_in_ft);
  727. uint32_t checksum = x1764_finish(&wbuf->checksum);
  728. wbuf_int(wbuf, checksum);
  729. lazy_assert(wbuf->ndone == wbuf->size);
  730. }
  731. void toku_serialize_ft_to (int fd, FT_HEADER h, BLOCK_TABLE blocktable, CACHEFILE cf) {
  732. lazy_assert(h->type==FT_CHECKPOINT_INPROGRESS);
  733. struct wbuf w_translation;
  734. int64_t size_translation;
  735. int64_t address_translation;
  736. //Must serialize translation first, to get address,size for header.
  737. toku_serialize_translation_to_wbuf(blocktable, fd, &w_translation,
  738. &address_translation,
  739. &size_translation);
  740. assert(size_translation == w_translation.ndone); // the bytes written are the size
  741. assert(w_translation.size % 512 == 0); // the number of bytes available in the buffer is 0 mod 512, and those last bytes are all initialized.
  742. struct wbuf w_main;
  743. size_t size_main = toku_serialize_ft_size(h);
  744. size_t size_main_aligned = roundup_to_multiple(512, size_main);
  745. assert(size_main_aligned<BLOCK_ALLOCATOR_HEADER_RESERVE);
  746. char *XMALLOC_N_ALIGNED(512, size_main_aligned, mainbuf);
  747. for (size_t i=size_main; i<size_main_aligned; i++) mainbuf[i]=0; // initialize the end of the buffer with zeros
  748. wbuf_init(&w_main, mainbuf, size_main);
  749. toku_serialize_ft_to_wbuf(&w_main, h, address_translation, size_translation);
  750. lazy_assert(w_main.ndone == size_main);
  751. // Actually write translation table
  752. // This write is guaranteed to read good data at the end of the buffer, since the
  753. // w_translation.buf is padded with zeros to a 512-byte boundary.
  754. toku_os_full_pwrite(fd, w_translation.buf, roundup_to_multiple(512, size_translation), address_translation);
  755. //Everything but the header MUST be on disk before header starts.
  756. //Otherwise we will think the header is good and some blocks might not
  757. //yet be on disk.
  758. //If the header has a cachefile we need to do cachefile fsync (to
  759. //prevent crash if we redirected to dev null)
  760. //If there is no cachefile we still need to do an fsync.
  761. if (cf) {
  762. toku_cachefile_fsync(cf);
  763. }
  764. else {
  765. toku_file_fsync(fd);
  766. }
  767. //Alternate writing header to two locations:
  768. // Beginning (0) or BLOCK_ALLOCATOR_HEADER_RESERVE
  769. toku_off_t main_offset;
  770. main_offset = (h->checkpoint_count & 0x1) ? 0 : BLOCK_ALLOCATOR_HEADER_RESERVE;
  771. toku_os_full_pwrite(fd, w_main.buf, size_main_aligned, main_offset);
  772. toku_free(w_main.buf);
  773. toku_free(w_translation.buf);
  774. }