liberasurecode 1.8.0
Erasure Code API library
Loading...
Searching...
No Matches
flat_xor_hd.c
Go to the documentation of this file.
1/*
2 * Copyright 2014 Tushar Gohad
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 flat_xor_hd backend
25 *
26 * vi: set noai tw=79 ts=4 sw=4:
27 */
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <xor_code.h>
32
33#include "erasurecode.h"
34#include "erasurecode_backend.h"
35#include "erasurecode_helpers.h"
36
37#define FLAT_XOR_LIB_MAJOR 1
38#define FLAT_XOR_LIB_MINOR 0
39#define FLAT_XOR_LIB_REV 0
40#define FLAT_XOR_LIB_VER_STR "1.0"
41#define FLAT_XOR_LIB_NAME "flat_xor_hd"
42#if defined(__MACOS__) || defined(__MACOSX__) || defined(__OSX__) || defined(__APPLE__)
43#define FLAT_XOR_SO_NAME "libXorcode" LIBERASURECODE_SO_SUFFIX ".dylib"
44#else
45#define FLAT_XOR_SO_NAME "libXorcode" LIBERASURECODE_SO_SUFFIX ".so.1"
46#endif
47#define DEFAULT_W 32
48
49/* Forward declarations */
50struct ec_backend_common backend_flat_xor_hd;
51
52typedef xor_code_t* (*init_xor_hd_code_func)(int, int, int);
53typedef void (*xor_code_encode_func)(xor_code_t *, char **, char **, int);
54typedef int (*xor_code_decode_func)(xor_code_t *, char **, char **, int *, int, int);
55typedef int (*xor_hd_fragments_needed_func)(xor_code_t *, int *, int *, int *);
56
58 xor_code_t *xor_desc;
63};
64
65static int flat_xor_hd_encode(void *desc,
66 char **data, char **parity, int blocksize)
67{
68 struct flat_xor_hd_descriptor *xdesc =
69 (struct flat_xor_hd_descriptor *) desc;
70
71 xor_code_t *xor_desc = (xor_code_t *) xdesc->xor_desc;
72 xor_desc->encode(xor_desc, data, parity, blocksize);
73 return 0;
74}
75
76static int flat_xor_hd_decode(void *desc,
77 char **data, char **parity, int *missing_idxs,
78 int blocksize)
79{
80 struct flat_xor_hd_descriptor *xdesc =
81 (struct flat_xor_hd_descriptor *) desc;
82
83 xor_code_t *xor_desc = (xor_code_t *) xdesc->xor_desc;
84 return xor_desc->decode(xor_desc, data, parity, missing_idxs, blocksize, 1);
85}
86
87static int flat_xor_hd_reconstruct(void *desc,
88 char **data, char **parity, int *missing_idxs,
89 int destination_idx, int blocksize)
90{
91 struct flat_xor_hd_descriptor *xdesc =
92 (struct flat_xor_hd_descriptor *) desc;
93
94 xor_code_t *xor_desc = (xor_code_t *) xdesc->xor_desc;
95 return xor_reconstruct_one(xor_desc, data, parity,
96 missing_idxs, destination_idx, blocksize);
97}
98
100 int *missing_idxs,
101 int destination_idx)
102{
103 struct flat_xor_hd_descriptor *xdesc =
104 (struct flat_xor_hd_descriptor *) desc;
105
106 xor_code_t *xor_desc = (xor_code_t *) xdesc->xor_desc;
107
108 struct ec_bm missing_bm = NEW_BM;
109 convert_list_to_bitmap(missing_idxs, &missing_bm);
110 int num_available = xor_desc->k + xor_desc->m;
111 for (int i = 0; i < EC_MAX_FRAGMENTS; i++)
112 if (bm_get_value(&missing_bm, i))
113 num_available--;
114
115 if (xor_desc->hd == 3) {
116 if (num_available < 2)
117 return -EINSUFFFRAGS;
118
119 if (xor_desc->m == 5) {
120 if ((xor_desc->k == 8 || xor_desc->k == 9) && num_available < 3)
121 return -EINSUFFFRAGS;
122 if (xor_desc->k == 10 && num_available < 4)
123 return -EINSUFFFRAGS;
124 } else if (xor_desc->m == 6) {
125 if (xor_desc->k >= 9 && xor_desc->k <= 11 && num_available < 3)
126 return -EINSUFFFRAGS;
127 if (xor_desc->k >= 12 && xor_desc->k <= 14 && num_available < 4)
128 return -EINSUFFFRAGS;
129 if (xor_desc->k == 15 && num_available < 5)
130 return -EINSUFFFRAGS;
131 }
132 } else { /* hd == 4 */
133 if (num_available < 3)
134 return -EINSUFFFRAGS;
135
136 if (xor_desc->m == 5) {
137 if ((xor_desc->k == 7 || xor_desc->k == 8) && num_available < 4)
138 return -EINSUFFFRAGS;
139 if (xor_desc->k + xor_desc->m - num_available > 9)
140 return -EINSUFFFRAGS;
141 } else if (xor_desc->m == 6) {
142 if (num_available < (xor_desc->k + xor_desc->m) / 2 - 3)
143 return -EINSUFFFRAGS;
144 }
145 }
146 return 0;
147}
148
149static int flat_xor_hd_min_fragments(void *desc,
150 int *missing_idxs, int *fragments_to_exclude,
151 int *fragments_needed)
152{
153 struct flat_xor_hd_descriptor *xdesc =
154 (struct flat_xor_hd_descriptor *) desc;
155
156 xor_code_t *xor_desc = (xor_code_t *) xdesc->xor_desc;
157 xor_desc->fragments_needed(xor_desc, missing_idxs, fragments_to_exclude, fragments_needed);
158 return 0;
159}
160
165static int
167{
168 return DEFAULT_W;
169}
170
171static void * flat_xor_hd_init(struct ec_backend_args *args, void *sohandle)
172{
173 int k = args->uargs.k;
174 int m = args->uargs.m;
175 int hd = args->uargs.hd;
176
177 xor_code_t *xor_desc = NULL;
178 struct flat_xor_hd_descriptor *bdesc = NULL;
179
180 /* store w back in args so upper layer can get to it */
181 args->uargs.w = DEFAULT_W;
182
183 /* init xor_code_t descriptor */
184 xor_desc = init_xor_hd_code(k, m, hd);
185 if (NULL == xor_desc) {
186 return NULL;
187 }
188
189 /* fill in flat_xor_hd_descriptor */
190 bdesc = (struct flat_xor_hd_descriptor *)
191 malloc(sizeof(struct flat_xor_hd_descriptor));
192 if (NULL == bdesc) {
193 free (xor_desc);
194 return NULL;
195 }
196
197 bdesc->xor_desc = xor_desc;
198
199 return (void *) bdesc;
200}
201
202static int flat_xor_hd_exit(void *desc)
203{
204 struct flat_xor_hd_descriptor *bdesc =
205 (struct flat_xor_hd_descriptor *) desc;
206
207 free (bdesc->xor_desc);
208 free (bdesc);
209 return 0;
210}
211
212/*
213 * For the time being, we only claim compatibility with versions that
214 * match exactly
215 */
216static bool flat_xor_is_compatible_with(uint32_t version) {
217 return version == backend_flat_xor_hd.ec_backend_version;
218}
219
220static struct ec_backend_op_stubs flat_xor_hd_op_stubs = {
221 .INIT = flat_xor_hd_init,
222 .EXIT = flat_xor_hd_exit,
223 .ISSYSTEMATIC = 1,
224 .ENCODE = flat_xor_hd_encode,
225 .DECODE = flat_xor_hd_decode,
226 .FRAGSNEEDED = flat_xor_hd_min_fragments,
227 .RECONSTRUCT = flat_xor_hd_reconstruct,
228 .ELEMENTSIZE = flar_xor_hd_element_size,
229 .ISCOMPATIBLEWITH = flat_xor_is_compatible_with,
230 .GETMETADATASIZE = get_backend_metadata_size_zero,
231 .GETENCODEOFFSET = get_encode_offset_zero,
232 .CHECKRECONSTRUCTFRAGMENTS = flat_xor_hd_check_reconstruct_fragments,
233};
234
235__attribute__ ((visibility ("internal")))
236struct ec_backend_common backend_flat_xor_hd = {
237 .id = EC_BACKEND_FLAT_XOR_HD,
238 .name = FLAT_XOR_LIB_NAME,
239 .soname = FLAT_XOR_SO_NAME,
240 .soversion = FLAT_XOR_LIB_VER_STR,
241 .ops = &flat_xor_hd_op_stubs,
242 .ec_backend_version = _VERSION(FLAT_XOR_LIB_MAJOR,
245};
246
static int flat_xor_hd_exit(void *desc)
Definition: flat_xor_hd.c:202
struct ec_backend_common backend_flat_xor_hd
Definition: flat_xor_hd.c:50
static int flat_xor_hd_reconstruct(void *desc, char **data, char **parity, int *missing_idxs, int destination_idx, int blocksize)
Definition: flat_xor_hd.c:87
int(* xor_hd_fragments_needed_func)(xor_code_t *, int *, int *, int *)
Definition: flat_xor_hd.c:55
static bool flat_xor_is_compatible_with(uint32_t version)
Definition: flat_xor_hd.c:216
#define FLAT_XOR_LIB_REV
Definition: flat_xor_hd.c:39
static int flar_xor_hd_element_size(void *desc)
Return the element-size, which is the number of bits stored on a given device, per codeword.
Definition: flat_xor_hd.c:166
xor_code_t *(* init_xor_hd_code_func)(int, int, int)
Definition: flat_xor_hd.c:52
static int flat_xor_hd_decode(void *desc, char **data, char **parity, int *missing_idxs, int blocksize)
Definition: flat_xor_hd.c:76
#define FLAT_XOR_LIB_MINOR
Definition: flat_xor_hd.c:38
int(* xor_code_decode_func)(xor_code_t *, char **, char **, int *, int, int)
Definition: flat_xor_hd.c:54
void(* xor_code_encode_func)(xor_code_t *, char **, char **, int)
Definition: flat_xor_hd.c:53
static struct ec_backend_op_stubs flat_xor_hd_op_stubs
Definition: flat_xor_hd.c:220
static int flat_xor_hd_min_fragments(void *desc, int *missing_idxs, int *fragments_to_exclude, int *fragments_needed)
Definition: flat_xor_hd.c:149
static int flat_xor_hd_encode(void *desc, char **data, char **parity, int blocksize)
Definition: flat_xor_hd.c:65
#define FLAT_XOR_LIB_NAME
Definition: flat_xor_hd.c:41
__attribute__((visibility("internal")))
Definition: flat_xor_hd.c:235
#define FLAT_XOR_SO_NAME
Definition: flat_xor_hd.c:45
static void * flat_xor_hd_init(struct ec_backend_args *args, void *sohandle)
Definition: flat_xor_hd.c:171
#define FLAT_XOR_LIB_MAJOR
Definition: flat_xor_hd.c:37
static int flat_xor_hd_check_reconstruct_fragments(void *desc, int *missing_idxs, int destination_idx)
Definition: flat_xor_hd.c:99
#define FLAT_XOR_LIB_VER_STR
Definition: flat_xor_hd.c:40
#define DEFAULT_W
Definition: flat_xor_hd.c:47
xor_code_encode_func xor_code_encode
Definition: flat_xor_hd.c:60
xor_code_decode_func xor_code_decode
Definition: flat_xor_hd.c:61
xor_hd_fragments_needed_func xor_hd_fragments_needed
Definition: flat_xor_hd.c:62
init_xor_hd_code_func init_xor_hd_code
Definition: flat_xor_hd.c:59
xor_code_t * xor_desc
Definition: flat_xor_hd.c:58
int xor_reconstruct_one(xor_code_t *code_desc, char **data, char **parity, int *missing_idxs, int index_to_reconstruct, int blocksize)
Definition: xor_code.c:257
xor_code_t * init_xor_hd_code(int k, int m, int hd)
Definition: xor_hd_code.c:657