liberasurecode  1.6.2
Erasure Code API library
erasurecode_postprocessing.c
Go to the documentation of this file.
1 /*
2  * Copyright 2014 Tushar Gohad, Kevin M Greenan, Eric Lambert
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice, this
11  * list of conditions and the following disclaimer in the documentation and/or
12  * other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY
13  * THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
14  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
15  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
17  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
18  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
20  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
21  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  * liberasurecode postprocessing helpers implementation
25  *
26  * vi: set noai tw=79 ts=4 sw=4:
27  */
28 
29 #include <zlib.h>
30 #include "erasurecode_backend.h"
31 #include "erasurecode_helpers.h"
32 #include "erasurecode_helpers_ext.h"
33 #include "erasurecode_log.h"
34 #include "erasurecode_stdinc.h"
35 #include "alg_sig.h"
36 
37 void add_fragment_metadata(ec_backend_t be, char *fragment,
38  int idx, uint64_t orig_data_size, int blocksize,
39  ec_checksum_type_t ct, int add_chksum)
40 {
41  //TODO EDL we are ignoring the return codes here, fix that
42  set_libec_version(fragment);
43  set_fragment_idx(fragment, idx);
44  set_orig_data_size(fragment, orig_data_size);
45  set_fragment_payload_size(fragment, blocksize);
46  set_backend_id(fragment, be->common.id);
47  set_backend_version(fragment, be->common.ec_backend_version);
48  set_fragment_backend_metadata_size(fragment, be->common.ops->get_backend_metadata_size(
49  be->desc.backend_desc,
50  blocksize));
51 
52  if (add_chksum) {
53  set_checksum(ct, fragment, blocksize);
54  }
55 
56  fragment_header_t* header = (fragment_header_t*) fragment;
57 
58  if (header->magic != LIBERASURECODE_FRAG_HEADER_MAGIC) {
59  log_error("Invalid fragment header (add fragment metadata)!\n");
60  return;
61  }
62 
63  char *flag = getenv("LIBERASURECODE_WRITE_LEGACY_CRC");
64  if (flag && !(flag[0] == '\0' || (flag[0] == '0' && flag[1] == '\0'))) {
65  header->metadata_chksum = liberasurecode_crc32_alt(
66  0, &header->meta, sizeof(fragment_metadata_t));
67  } else {
68  header->metadata_chksum = crc32(0, (unsigned char *) &header->meta,
69  sizeof(fragment_metadata_t));
70  }
71 }
72 
73 int finalize_fragments_after_encode(ec_backend_t instance,
74  int k, int m, int blocksize, uint64_t orig_data_size,
75  char **encoded_data, char **encoded_parity)
76 {
77  int i, set_chksum = 1;
78  ec_checksum_type_t ct = instance->args.uargs.ct;
79 
80  /* finalize data fragments */
81  for (i = 0; i < k; i++) {
82  char *fragment = get_fragment_ptr_from_data(encoded_data[i]);
83  add_fragment_metadata(instance, fragment, i, orig_data_size,
84  blocksize, ct, set_chksum);
85  encoded_data[i] = fragment;
86  }
87 
88  /* finalize parity fragments */
89  for (i = 0; i < m; i++) {
90  char *fragment = get_fragment_ptr_from_data(encoded_parity[i]);
91  add_fragment_metadata(instance, fragment, i + k, orig_data_size,
92  blocksize, ct, set_chksum);
93  encoded_parity[i] = fragment;
94  }
95 
96  return 0;
97 }
set_backend_version
int set_backend_version(char *buf, uint32_t version)
Definition: erasurecode_helpers.c:440
set_fragment_backend_metadata_size
int set_fragment_backend_metadata_size(char *buf, int size)
Definition: erasurecode_helpers.c:331
set_orig_data_size
int set_orig_data_size(char *buf, int orig_data_size)
Definition: erasurecode_helpers.c:372
add_fragment_metadata
void add_fragment_metadata(ec_backend_t be, char *fragment, int idx, uint64_t orig_data_size, int blocksize, ec_checksum_type_t ct, int add_chksum)
Definition: erasurecode_postprocessing.c:37
liberasurecode_crc32_alt
int liberasurecode_crc32_alt(int crc, const void *buf, size_t size)
Definition: crc32.c:92
set_libec_version
int set_libec_version(char *buf)
Definition: erasurecode_helpers.c:400
get_fragment_ptr_from_data
char * get_fragment_ptr_from_data(char *buf)
Definition: erasurecode_helpers.c:257
set_backend_id
int set_backend_id(char *buf, ec_backend_id_t id)
Definition: erasurecode_helpers.c:420
finalize_fragments_after_encode
int finalize_fragments_after_encode(ec_backend_t instance, int k, int m, int blocksize, uint64_t orig_data_size, char **encoded_data, char **encoded_parity)
Definition: erasurecode_postprocessing.c:73
set_checksum
int set_checksum(ec_checksum_type_t ct, char *buf, int blocksize)
Definition: erasurecode_helpers.c:462
set_fragment_idx
int set_fragment_idx(char *buf, int idx)
Definition: erasurecode_helpers.c:275
set_fragment_payload_size
int set_fragment_payload_size(char *buf, int size)
Definition: erasurecode_helpers.c:303