liberasurecode  1.6.1
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 
36 void add_fragment_metadata(ec_backend_t be, char *fragment,
37  int idx, uint64_t orig_data_size, int blocksize,
38  ec_checksum_type_t ct, int add_chksum)
39 {
40  //TODO EDL we are ignoring the return codes here, fix that
41  set_libec_version(fragment);
42  set_fragment_idx(fragment, idx);
43  set_orig_data_size(fragment, orig_data_size);
44  set_fragment_payload_size(fragment, blocksize);
45  set_backend_id(fragment, be->common.id);
46  set_backend_version(fragment, be->common.ec_backend_version);
47  set_fragment_backend_metadata_size(fragment, be->common.ops->get_backend_metadata_size(
48  be->desc.backend_desc,
49  blocksize));
50 
51  if (add_chksum) {
52  set_checksum(ct, fragment, blocksize);
53  }
54 
55  fragment_header_t* header = (fragment_header_t*) fragment;
56 
57  if (header->magic != LIBERASURECODE_FRAG_HEADER_MAGIC) {
58  log_error("Invalid fragment header (add fragment metadata)!\n");
59  return;
60  }
61 
62  header->metadata_chksum = crc32(0, (unsigned char *) &header->meta,
63  sizeof(fragment_metadata_t));
64 }
65 
66 int finalize_fragments_after_encode(ec_backend_t instance,
67  int k, int m, int blocksize, uint64_t orig_data_size,
68  char **encoded_data, char **encoded_parity)
69 {
70  int i, set_chksum = 1;
71  ec_checksum_type_t ct = instance->args.uargs.ct;
72 
73  /* finalize data fragments */
74  for (i = 0; i < k; i++) {
75  char *fragment = get_fragment_ptr_from_data(encoded_data[i]);
76  add_fragment_metadata(instance, fragment, i, orig_data_size,
77  blocksize, ct, set_chksum);
78  encoded_data[i] = fragment;
79  }
80 
81  /* finalize parity fragments */
82  for (i = 0; i < m; i++) {
83  char *fragment = get_fragment_ptr_from_data(encoded_parity[i]);
84  add_fragment_metadata(instance, fragment, i + k, orig_data_size,
85  blocksize, ct, set_chksum);
86  encoded_parity[i] = fragment;
87  }
88 
89  return 0;
90 }
int set_fragment_payload_size(char *buf, int size)
int set_checksum(ec_checksum_type_t ct, char *buf, int blocksize)
int set_fragment_idx(char *buf, int idx)
char * get_fragment_ptr_from_data(char *buf)
int set_fragment_backend_metadata_size(char *buf, int size)
int set_libec_version(char *buf)
int set_backend_id(char *buf, ec_backend_id_t id)
int set_orig_data_size(char *buf, int orig_data_size)
int set_backend_version(char *buf, uint32_t version)
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)
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)